DllHelper.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.pdb").bytes;
  31. Assembly assembly = Assembly.Load(assBytes, mdbBytes);
  32. return assembly;
  33. }
  34. #endif
  35. private static List<Type> _typeBuffer = new List<Type>();
  36. public static Type[] GetHotfixTypes()
  37. {
  38. #if ILRuntime
  39. ILRuntime.Runtime.Enviorment.AppDomain appDomain = Init.Instance.AppDomain;
  40. if (appDomain == null)
  41. {
  42. return new Type[0];
  43. }
  44. foreach (IType type in appDomain.LoadedTypes.Values.ToArray())
  45. {
  46. if (!_typeBuffer.Contains(type.ReflectionType))
  47. {
  48. _typeBuffer.Add(type.ReflectionType);
  49. }
  50. }
  51. return _typeBuffer.ToArray();
  52. #else
  53. return ObjectEvents.Instance.Get("Hotfix").GetTypes();
  54. #endif
  55. }
  56. public static Type[] GetMonoTypes()
  57. {
  58. List<Type> types = new List<Type>();
  59. foreach (Assembly assembly in ObjectEvents.Instance.GetAll())
  60. {
  61. types.AddRange(assembly.GetTypes());
  62. }
  63. return types.ToArray();
  64. }
  65. #if ILRuntime
  66. public static IMethod[] GetMethodInfo(string typeName)
  67. {
  68. ILRuntime.Runtime.Enviorment.AppDomain appDomain = Init.Instance.AppDomain;
  69. if (appDomain == null)
  70. {
  71. return new IMethod[0];
  72. }
  73. return appDomain.GetType(typeName).GetMethods().ToArray();
  74. }
  75. public static IType GetType(string typeName)
  76. {
  77. return Init.Instance.AppDomain.GetType(typeName);
  78. }
  79. #endif
  80. }
  81. }