Hotfix.cs 2.0 KB

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