CodeLoader.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 model;
  12. public void Start()
  13. {
  14. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  15. foreach (Assembly assembly in assemblies)
  16. {
  17. if (assembly.GetName().Name == "Model")
  18. {
  19. this.model = assembly;
  20. break;
  21. }
  22. }
  23. this.LoadHotfix();
  24. IStaticMethod start = new StaticMethod(this.model, "ET.Entry", "Start");
  25. start.Run();
  26. }
  27. public void LoadHotfix()
  28. {
  29. assemblyLoadContext?.Unload();
  30. GC.Collect();
  31. assemblyLoadContext = new AssemblyLoadContext("Hotfix", true);
  32. byte[] dllBytes = File.ReadAllBytes("./Hotfix.dll");
  33. byte[] pdbBytes = File.ReadAllBytes("./Hotfix.pdb");
  34. Assembly hotfixAssembly = assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
  35. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(Assembly.GetEntryAssembly(), typeof(Init).Assembly, typeof (Game).Assembly, this.model, hotfixAssembly);
  36. EventSystem.Instance.Add(types);
  37. }
  38. }
  39. }