ILHelper.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Reflection;
  3. using ILRuntime.CLR.Method;
  4. using ILRuntime.Runtime.Enviorment;
  5. using ILRuntime.Runtime.Intepreter;
  6. using UnityEngine;
  7. namespace Model
  8. {
  9. public static class ILHelper
  10. {
  11. public static unsafe void InitILRuntime()
  12. {
  13. // 注册重定向函数
  14. MethodInfo mi = typeof(Log).GetMethod("Debug", new Type[] { typeof(string) });
  15. Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
  16. MethodInfo mi2 = typeof(Log).GetMethod("Info", new Type[] { typeof(string) });
  17. Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi2, ILRedirection.LogInfo);
  18. MethodInfo mi3 = typeof(Log).GetMethod("Error", new Type[] { typeof(string) });
  19. Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi3, ILRedirection.LogError);
  20. // 注册委托
  21. Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
  22. Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
  23. Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<IResponse>();
  24. // 注册适配器
  25. Assembly assembly = typeof(Init).Assembly;
  26. foreach (Type type in assembly.GetTypes())
  27. {
  28. object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
  29. if (attrs.Length == 0)
  30. {
  31. continue;
  32. }
  33. object obj = Activator.CreateInstance(type);
  34. CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
  35. if (adaptor == null)
  36. {
  37. continue;
  38. }
  39. Init.Instance.AppDomain.RegisterCrossBindingAdaptor(adaptor);
  40. }
  41. // 初始化ILRuntime的protobuf
  42. InitializeILRuntimeProtobuf();
  43. }
  44. public static void AvoidAot(GameObject gameObject)
  45. {
  46. Input input = gameObject.Get<Input>("11");
  47. }
  48. public static void InitializeILRuntimeProtobuf()
  49. {
  50. ProtoBuf.PType.RegisterFunctionCreateInstance(PType_CreateInstance);
  51. ProtoBuf.PType.RegisterFunctionGetRealType(PType_GetRealType);
  52. }
  53. private static object PType_CreateInstance(string typeName)
  54. {
  55. return Init.Instance.AppDomain.Instantiate(typeName);
  56. }
  57. private static Type PType_GetRealType(object o)
  58. {
  59. Type type = o.GetType();
  60. if (type.FullName == "ILRuntime.Runtime.Intepreter.ILTypeInstance")
  61. {
  62. ILTypeInstance ilo = o as ILTypeInstance;
  63. type = ProtoBuf.PType.FindType(ilo.Type.FullName);
  64. }
  65. return type;
  66. }
  67. }
  68. }