ILHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Reflection;
  6. using ILRuntime.CLR.Method;
  7. using ILRuntime.CLR.TypeSystem;
  8. using ILRuntime.Runtime.Enviorment;
  9. //using ILRuntime.Runtime.Generated;
  10. using ILRuntime.Runtime.Intepreter;
  11. using ProtoBuf;
  12. using UnityEngine;
  13. namespace ET
  14. {
  15. public static class ILHelper
  16. {
  17. public static List<Type> list = new List<Type>();
  18. public static void InitILRuntime(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
  19. {
  20. list.Add(typeof(Dictionary<int, ILTypeInstance>));
  21. list.Add(typeof(Dictionary<int, int>));
  22. list.Add(typeof(Dictionary<object, object>));
  23. list.Add(typeof(Dictionary<int, object>));
  24. list.Add(typeof(Dictionary<long, object>));
  25. list.Add(typeof(Dictionary<long, int>));
  26. list.Add(typeof(Dictionary<int, long>));
  27. list.Add(typeof(Dictionary<string, long>));
  28. list.Add(typeof(Dictionary<string, int>));
  29. list.Add(typeof(Dictionary<string, object>));
  30. list.Add(typeof(List<ILTypeInstance>));
  31. list.Add(typeof(List<int>));
  32. list.Add(typeof(List<long>));
  33. list.Add(typeof(List<string>));
  34. list.Add(typeof(List<object>));
  35. list.Add(typeof(ListComponent<ILTypeInstance>));
  36. list.Add(typeof(ETTask<int>));
  37. list.Add(typeof(ETTask<long>));
  38. list.Add(typeof(ETTask<string>));
  39. list.Add(typeof(ETTask<object>));
  40. list.Add(typeof(ETTask<AssetBundle>));
  41. list.Add(typeof(ETTask<UnityEngine.Object[]>));
  42. list.Add(typeof(ListComponent<ETTask>));
  43. list.Add(typeof(ListComponent<Vector3>));
  44. // 注册重定向函数
  45. // 注册委托
  46. appdomain.DelegateManager.RegisterMethodDelegate<List<object>>();
  47. appdomain.DelegateManager.RegisterMethodDelegate<object>();
  48. appdomain.DelegateManager.RegisterMethodDelegate<bool>();
  49. appdomain.DelegateManager.RegisterMethodDelegate<string>();
  50. appdomain.DelegateManager.RegisterMethodDelegate<float>();
  51. appdomain.DelegateManager.RegisterMethodDelegate<long, int>();
  52. appdomain.DelegateManager.RegisterMethodDelegate<long, MemoryStream>();
  53. appdomain.DelegateManager.RegisterMethodDelegate<long, IPEndPoint>();
  54. appdomain.DelegateManager.RegisterMethodDelegate<ILTypeInstance>();
  55. appdomain.DelegateManager.RegisterMethodDelegate<AsyncOperation>();
  56. appdomain.DelegateManager.RegisterFunctionDelegate<UnityEngine.Events.UnityAction>();
  57. appdomain.DelegateManager.RegisterFunctionDelegate<System.Object, ET.ETTask>();
  58. appdomain.DelegateManager.RegisterFunctionDelegate<ILTypeInstance, bool>();
  59. appdomain.DelegateManager.RegisterFunctionDelegate<System.Collections.Generic.KeyValuePair<System.String, System.Int32>, System.String>();
  60. appdomain.DelegateManager.RegisterFunctionDelegate<System.Collections.Generic.KeyValuePair<System.Int32, System.Int32>, System.Boolean>();
  61. appdomain.DelegateManager.RegisterFunctionDelegate<System.Collections.Generic.KeyValuePair<System.String, System.Int32>, System.Int32>();
  62. appdomain.DelegateManager.RegisterFunctionDelegate<List<int>, int>();
  63. appdomain.DelegateManager.RegisterFunctionDelegate<List<int>, bool>();
  64. appdomain.DelegateManager.RegisterFunctionDelegate<int, bool>();//Linq
  65. appdomain.DelegateManager.RegisterFunctionDelegate<int, int, int>();//Linq
  66. appdomain.DelegateManager.RegisterFunctionDelegate<KeyValuePair<int, List<int>>, bool>();
  67. appdomain.DelegateManager.RegisterFunctionDelegate<KeyValuePair<int, int>, KeyValuePair<int, int>, int>();
  68. appdomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Events.UnityAction>((act) =>
  69. {
  70. return new UnityEngine.Events.UnityAction(() =>
  71. {
  72. ((Action)act)();
  73. });
  74. });
  75. appdomain.DelegateManager.RegisterDelegateConvertor<Comparison<KeyValuePair<int, int>>>((act) =>
  76. {
  77. return new Comparison<KeyValuePair<int, int>>((x, y) =>
  78. {
  79. return ((Func<KeyValuePair<int, int>, KeyValuePair<int, int>, int>)act)(x, y);
  80. });
  81. });
  82. // 注册适配器
  83. RegisterAdaptor(appdomain);
  84. //注册Json的CLR
  85. LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(appdomain);
  86. //注册ProtoBuf的CLR
  87. PType.RegisterILRuntimeCLRRedirection(appdomain);
  88. ////////////////////////////////////
  89. // CLR绑定的注册,一定要记得将CLR绑定的注册写在CLR重定向的注册后面,因为同一个方法只能被重定向一次,只有先注册的那个才能生效
  90. ////////////////////////////////////
  91. Type t = Type.GetType("ILRuntime.Runtime.Generated.CLRBindings");
  92. if (t != null)
  93. {
  94. t.GetMethod("Initialize")?.Invoke(null, new object[] { appdomain });
  95. }
  96. // CLRBindings.Initialize(appdomain);
  97. }
  98. public static void RegisterAdaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
  99. {
  100. //注册自己写的适配器
  101. appdomain.RegisterCrossBindingAdaptor(new IAsyncStateMachineClassInheritanceAdaptor());
  102. }
  103. }
  104. }