CodeLoader.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.Loader;
  6. namespace ET
  7. {
  8. /*
  9. public class CodeLoader: Singleton<CodeLoader>, ISingletonAwake
  10. {
  11. private AssemblyLoadContext assemblyLoadContext;
  12. private Assembly assembly;
  13. public void Awake()
  14. {
  15. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  16. foreach (Assembly ass in assemblies)
  17. {
  18. if (ass.GetName().Name == "Model")
  19. {
  20. this.assembly = ass;
  21. break;
  22. }
  23. }
  24. Assembly hotfixAssembly = this.LoadHotfix();
  25. World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly });
  26. IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
  27. start.Run();
  28. }
  29. private Assembly LoadHotfix()
  30. {
  31. assemblyLoadContext?.Unload();
  32. GC.Collect();
  33. assemblyLoadContext = new AssemblyLoadContext("Hotfix", true);
  34. byte[] dllBytes = File.ReadAllBytes("./Hotfix.dll");
  35. byte[] pdbBytes = File.ReadAllBytes("./Hotfix.pdb");
  36. Assembly hotfixAssembly = assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
  37. return hotfixAssembly;
  38. }
  39. public void Reload()
  40. {
  41. Assembly hotfixAssembly = this.LoadHotfix();
  42. CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly });
  43. codeTypes.CreateCode();
  44. Log.Debug($"reload dll finish!");
  45. }
  46. }
  47. */
  48. }