CodeLoader.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  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: Singleton<CodeLoader>
  9. {
  10. private AssemblyLoadContext assemblyLoadContext;
  11. private Assembly hotfix;
  12. public void Start()
  13. {
  14. this.LoadHotfix();
  15. Entry.Start();
  16. }
  17. public void LoadHotfix()
  18. {
  19. assemblyLoadContext?.Unload();
  20. GC.Collect();
  21. assemblyLoadContext = new AssemblyLoadContext("Hotfix", true);
  22. byte[] dllBytes = File.ReadAllBytes("./Hotfix.dll");
  23. byte[] pdbBytes = File.ReadAllBytes("./Hotfix.pdb");
  24. this.hotfix = assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
  25. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof(Init).Assembly, typeof (Game).Assembly, typeof(Entry).Assembly, this.hotfix);
  26. EventSystem.Instance.Add(types);
  27. }
  28. }
  29. }