using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Reflection; using FairyGUI; using ILRuntime.CLR.Method; using ILRuntime.CLR.Utils; using ILRuntime.Runtime.Intepreter; using ILRuntime.Runtime.Stack; using ProtoBuf; using UnityEngine; namespace ET { public static class ILHelper { public static List list = new List(); public static void InitILRuntime(ILRuntime.Runtime.Enviorment.AppDomain appdomain) { list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(Dictionary)); list.Add(typeof(List)); list.Add(typeof(List)); list.Add(typeof(List)); list.Add(typeof(List)); list.Add(typeof(List)); list.Add(typeof(ETTask)); list.Add(typeof(ETTask)); list.Add(typeof(ETTask)); list.Add(typeof(ETTask)); list.Add(typeof(ETTask)); list.Add(typeof(ETTask)); list.Add(typeof(ListComponent)); list.Add(typeof(ListComponent)); list.Add(typeof(ListComponent)); // 注册重定向函数 FUICLRMethod(appdomain); // 注册委托 appdomain.DelegateManager.RegisterMethodDelegate>(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterFunctionDelegate(); appdomain.DelegateManager.RegisterFunctionDelegate(); appdomain.DelegateManager.RegisterFunctionDelegate(); appdomain.DelegateManager.RegisterFunctionDelegate, System.String>(); appdomain.DelegateManager.RegisterFunctionDelegate, System.Boolean>(); appdomain.DelegateManager.RegisterFunctionDelegate, System.Int32>(); appdomain.DelegateManager.RegisterFunctionDelegate, int>(); appdomain.DelegateManager.RegisterFunctionDelegate, bool>(); appdomain.DelegateManager.RegisterFunctionDelegate();//Linq appdomain.DelegateManager.RegisterFunctionDelegate();//Linq appdomain.DelegateManager.RegisterFunctionDelegate>, bool>(); appdomain.DelegateManager.RegisterFunctionDelegate, KeyValuePair, int>(); appdomain.DelegateManager.RegisterMethodDelegate>(); appdomain.DelegateManager.RegisterMethodDelegate>(); appdomain.DelegateManager.RegisterFunctionDelegate, System.Boolean>(); appdomain.DelegateManager.RegisterFunctionDelegate, System.Boolean>(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new UnityEngine.Events.UnityAction(() => { ((Action)act)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor>>((act) => { return new Comparison>((x, y) => { return ((Func, KeyValuePair, int>)act)(x, y); }); }); appdomain.DelegateManager.RegisterMethodDelegate(); #region FGUI appdomain.DelegateManager.RegisterFunctionDelegate(); appdomain.DelegateManager .RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterFunctionDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterFunctionDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.GTweenCallback1((tweener) => { ((Action)act)(tweener); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new DG.Tweening.TweenCallback(() => { ((Action)act)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.UIPackage.LoadResource((string name, string extension, Type type, out DestroyMethod destroyMethod) => { destroyMethod = DestroyMethod.None; return ((Func)act)(name, extension, type, destroyMethod); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.UIPackage.LoadResourceAsync((name, extension, type, item) => { ((Action)act)(name, extension, type, item); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.UIPackage.CreateObjectCallback((result) => { ((Action)act)(result); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.UIObjectFactory.GComponentCreator(() => { return ((Func)act)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.EventCallback0(() => { ((Action)act)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.EventCallback1((context) => { ((Action)act)(context); }); }); appdomain.DelegateManager.RegisterDelegateConvertor>((act) => { return new UnityEngine.Events.UnityAction((arg0) => { ((Action)act)(arg0); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.GTweenCallback(() => { ((Action)act)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.PlayCompleteCallback(() => { ((Action)act)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor>((act) => { return new System.Comparison((x, y) => { return ((Func)act)(x, y); }); }); appdomain.DelegateManager.RegisterDelegateConvertor>((act) => { return new System.Comparison((x, y) => { return ((Func)act)(x, y); }); }); #endregion // 注册适配器 RegisterAdaptor(appdomain); //注册Json的CLR LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(appdomain); //注册ProtoBuf的CLR PType.RegisterILRuntime(appdomain, typeFullName => CodeLoader.Instance.GetHotfixType(typeFullName)); //////////////////////////////////// // CLR绑定的注册,一定要记得将CLR绑定的注册写在CLR重定向的注册后面,因为同一个方法只能被重定向一次,只有先注册的那个才能生效 //////////////////////////////////// Type t = Type.GetType("ILRuntime.Runtime.Generated.CLRBindings"); if (t != null) { t.GetMethod("Initialize")?.Invoke(null, new object[] { appdomain }); } //ILRuntime.Runtime.Generated.CLRBindings.Initialize(appdomain); } public static void RegisterAdaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain) { //注册自己写的适配器 appdomain.RegisterCrossBindingAdaptor(new IAsyncStateMachineClassInheritanceAdaptor()); appdomain.RegisterValueTypeBinder(typeof(Vector2), new Vector2Binder()); appdomain.RegisterValueTypeBinder(typeof(Vector3), new Vector3Binder()); appdomain.RegisterValueTypeBinder(typeof(Quaternion), new QuaternionBinder()); appdomain.RegisterCrossBindingAdaptor(new GButtonAdapter()); appdomain.RegisterCrossBindingAdaptor(new GLoaderAdapter()); appdomain.RegisterCrossBindingAdaptor(new WindowAdapter()); appdomain.RegisterCrossBindingAdaptor(new GComponentAdapter()); } unsafe static void FUICLRMethod (ILRuntime.Runtime.Enviorment.AppDomain appdomain) { BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; MethodBase method; Type[] args; Type type = typeof (FairyGUI.UIObjectFactory); args = new Type[] { typeof (System.String), typeof (System.Type) }; method = type.GetMethod ("SetPackageItemExtension", flag, null, args, null); appdomain.RegisterCLRMethodRedirection (method, SetPackageItemExtension_0); args = new Type[] { typeof (System.Type) }; method = type.GetMethod ("SetLoaderExtension", flag, null, args, null); appdomain.RegisterCLRMethodRedirection (method, SetLoaderExtension_0); } unsafe static StackObject * SetPackageItemExtension_0 (ILIntepreter __intp, StackObject * __esp, IList __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject * ptr_of_this_method; StackObject * __ret = ILIntepreter.Minus (__esp, 2); ptr_of_this_method = ILIntepreter.Minus (__esp, 1); System.Type @type = (System.Type) typeof (System.Type).CheckCLRTypes (StackObject.ToObject (ptr_of_this_method, __domain, __mStack)); __intp.Free (ptr_of_this_method); ptr_of_this_method = ILIntepreter.Minus (__esp, 2); System.String @url = (System.String) typeof (System.String).CheckCLRTypes (StackObject.ToObject (ptr_of_this_method, __domain, __mStack)); __intp.Free (ptr_of_this_method); FairyGUI.UIObjectFactory.SetPackageItemExtension (@url, () => { return __domain.Instantiate (@type.FullName); }); // FairyGUI.UIObjectFactory.SetPackageItemExtension (@url, @type); return __ret; } unsafe static StackObject * SetLoaderExtension_0 (ILIntepreter __intp, StackObject * __esp, IList __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject * ptr_of_this_method; StackObject * __ret = ILIntepreter.Minus (__esp, 1); ptr_of_this_method = ILIntepreter.Minus (__esp, 1); System.Type @type = (System.Type) typeof (System.Type).CheckCLRTypes (StackObject.ToObject (ptr_of_this_method, __domain, __mStack)); __intp.Free (ptr_of_this_method); FairyGUI.UIObjectFactory.SetLoaderExtension(() => { return __domain.Instantiate (@type.FullName); }); return __ret; } } }