ILHelper.cs 2.7 KB

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