Hotfix.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using UnityEngine;
  7. namespace ETModel
  8. {
  9. public sealed class Hotfix : Object
  10. {
  11. #if ILRuntime
  12. private ILRuntime.Runtime.Enviorment.AppDomain appDomain;
  13. #else
  14. private Assembly assembly;
  15. #endif
  16. private IStaticMethod start;
  17. private List<Type> hotfixTypes;
  18. public Action Update;
  19. public Action LateUpdate;
  20. public Action OnApplicationQuit;
  21. public void GotoHotfix()
  22. {
  23. #if ILRuntime
  24. ILHelper.InitILRuntime(this.appDomain);
  25. #endif
  26. this.start.Run();
  27. }
  28. public List<Type> GetHotfixTypes()
  29. {
  30. return this.hotfixTypes;
  31. }
  32. public void LoadHotfixAssembly()
  33. {
  34. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
  35. GameObject code = (GameObject)Game.Scene.GetComponent<ResourcesComponent>().GetAsset("code.unity3d", "Code");
  36. byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
  37. byte[] pdbBytes = code.Get<TextAsset>("Hotfix.pdb").bytes;
  38. #if ILRuntime
  39. Log.Debug($"当前使用的是ILRuntime模式");
  40. this.appDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  41. using (MemoryStream fs = new MemoryStream(assBytes))
  42. using (MemoryStream p = new MemoryStream(pdbBytes))
  43. {
  44. this.appDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
  45. }
  46. this.start = new ILStaticMethod(this.appDomain, "ETHotfix.Init", "Start", 0);
  47. this.hotfixTypes = this.appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToList();
  48. #else
  49. Log.Debug($"当前使用的是Mono模式");
  50. this.assembly = Assembly.Load(assBytes, pdbBytes);
  51. Type hotfixInit = this.assembly.GetType("ETHotfix.Init");
  52. this.start = new MonoStaticMethod(hotfixInit, "Start");
  53. this.hotfixTypes = this.assembly.GetTypes().ToList();
  54. #endif
  55. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
  56. }
  57. }
  58. }