DllHelper.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using ILRuntime.CLR.Method;
  7. using ILRuntime.CLR.TypeSystem;
  8. using UnityEngine;
  9. namespace Model
  10. {
  11. public static class DllHelper
  12. {
  13. #if ILRuntime
  14. public static void LoadHotfixAssembly()
  15. {
  16. GameObject code = Game.Scene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("code.unity3d", "Code");
  17. byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
  18. byte[] mdbBytes = code.Get<TextAsset>("Hotfix.pdb").bytes;
  19. using (MemoryStream fs = new MemoryStream(assBytes))
  20. using (MemoryStream p = new MemoryStream(mdbBytes))
  21. {
  22. Init.Instance.AppDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
  23. }
  24. }
  25. #else
  26. public static Assembly LoadHotfixAssembly()
  27. {
  28. GameObject code = Game.Scene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("code.unity3d", "Code");
  29. byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
  30. byte[] mdbBytes = code.Get<TextAsset>("Hotfix.mdb").bytes;
  31. Assembly assembly = Assembly.Load(assBytes, mdbBytes);
  32. return assembly;
  33. }
  34. #endif
  35. public static Type[] GetHotfixTypes()
  36. {
  37. #if ILRuntime
  38. ILRuntime.Runtime.Enviorment.AppDomain appDomain = Init.Instance.AppDomain;
  39. if (appDomain == null)
  40. {
  41. return new Type[0];
  42. }
  43. return appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray();
  44. #else
  45. if (Game.EventSystem.HotfixAssembly == null)
  46. {
  47. return new Type[0];
  48. }
  49. return Game.EventSystem.HotfixAssembly.GetTypes();
  50. #endif
  51. }
  52. public static Type[] GetMonoTypes()
  53. {
  54. List<Type> types = new List<Type>();
  55. foreach (Assembly assembly in Game.EventSystem.GetAll())
  56. {
  57. types.AddRange(assembly.GetTypes());
  58. }
  59. return types.ToArray();
  60. }
  61. public static Type[] GetAllTypes()
  62. {
  63. List<Type> types = new List<Type>();
  64. foreach (Assembly assembly in Game.EventSystem.GetAll())
  65. {
  66. types.AddRange(assembly.GetTypes());
  67. }
  68. types.AddRange(GetHotfixTypes());
  69. return types.ToArray();
  70. }
  71. #if ILRuntime
  72. public static IMethod[] GetMethodInfo(string typeName)
  73. {
  74. ILRuntime.Runtime.Enviorment.AppDomain appDomain = Init.Instance.AppDomain;
  75. if (appDomain == null)
  76. {
  77. return new IMethod[0];
  78. }
  79. return appDomain.GetType(typeName).GetMethods().ToArray();
  80. }
  81. public static IType GetType(string typeName)
  82. {
  83. return Init.Instance.AppDomain.GetType(typeName);
  84. }
  85. #endif
  86. }
  87. }