CodeLoader.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. public class CodeLoader: IDisposable
  9. {
  10. private AssemblyLoadContext assemblyLoadContext;
  11. private Assembly hotfix;
  12. private static CodeLoader instance;
  13. public static CodeLoader Instance
  14. {
  15. get
  16. {
  17. return instance ??= new CodeLoader();
  18. }
  19. }
  20. private CodeLoader()
  21. {
  22. }
  23. public void Dispose()
  24. {
  25. instance = null;
  26. }
  27. public void Start()
  28. {
  29. this.LoadHotfix();
  30. Entry.Start();
  31. }
  32. public void LoadHotfix()
  33. {
  34. assemblyLoadContext?.Unload();
  35. GC.Collect();
  36. assemblyLoadContext = new AssemblyLoadContext("Hotfix", true);
  37. byte[] dllBytes = File.ReadAllBytes("./Hotfix.dll");
  38. byte[] pdbBytes = File.ReadAllBytes("./Hotfix.pdb");
  39. this.hotfix = assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
  40. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof(Init).Assembly, typeof (Game).Assembly, typeof(Entry).Assembly, this.hotfix);
  41. Game.EventSystem.Add(types);
  42. }
  43. }
  44. }