DllHelper.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 = (GameObject)Resources.Load("Code");
  17. byte[] assBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.dll").bytes;
  18. byte[] mdbBytes = code.GetComponent<ReferenceCollector>().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 = (GameObject)Resources.Load("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. return ObjectEvents.Instance.HotfixAssembly.GetTypes();
  46. #endif
  47. }
  48. public static Type[] GetMonoTypes()
  49. {
  50. List<Type> types = new List<Type>();
  51. foreach (Assembly assembly in ObjectEvents.Instance.GetAll())
  52. {
  53. types.AddRange(assembly.GetTypes());
  54. }
  55. return types.ToArray();
  56. }
  57. #if ILRuntime
  58. public static IMethod[] GetMethodInfo(string typeName)
  59. {
  60. ILRuntime.Runtime.Enviorment.AppDomain appDomain = Init.Instance.AppDomain;
  61. if (appDomain == null)
  62. {
  63. return new IMethod[0];
  64. }
  65. return appDomain.GetType(typeName).GetMethods().ToArray();
  66. }
  67. public static IType GetType(string typeName)
  68. {
  69. return Init.Instance.AppDomain.GetType(typeName);
  70. }
  71. #endif
  72. }
  73. }