ILHelper.cs 5.6 KB

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