Hotfix.cs 2.0 KB

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