ILHelper.cs 2.5 KB

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