Ejemplo n.º 1
0
        unsafe void CLRRedirection()
        {
            var arr = typeof(GameObject).GetMethods();

            foreach (var method in arr)
            {
                if (method.GetGenericArguments().Length == 1)
                {
                    switch (method.Name)
                    {
                    case "AddComponent":
                    {
                        appdomain.RegisterCLRMethodRedirection(method, ILRuntimeCLR.AddComponent);
                    }
                    break;

                    case "GetComponent":
                    {
                        appdomain.RegisterCLRMethodRedirection(method, ILRuntimeCLR.GetComponent);
                    }
                    break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static unsafe void SetupCLRRedirection()
        {
            //这里面的通常应该写在InitializeILRuntime,这里为了演示写这里
            var arr = typeof(GameObject).GetMethods();

            foreach (var i in arr)
            {
                if (i.Name == "AddComponent" && i.GetGenericArguments().Length == 1)
                {
                    _appdomain.RegisterCLRMethodRedirection(i, AddComponent);
                }
            }
        }
Ejemplo n.º 3
0
        unsafe public void InitializeILRuntime(ILRuntime.Runtime.Enviorment.AppDomain AppDomain)
        {
            m_AppDomain = AppDomain;
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
            //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler
            m_AppDomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
            m_AppDomain.DebugService.StartDebugService(56000);
#endif
            //这里做一些ILRuntime的注册,注册漏了的委托,运行的时候会报错提示的

            //委托,适配器,,值类型绑定等等的注册
            m_AppDomain.DelegateManager.RegisterMethodDelegate <GameObject>();

            m_AppDomain.DelegateManager.RegisterMethodDelegate <UnityEngine.Sprite>();

            m_AppDomain.DelegateManager.RegisterDelegateConvertor <UnityEngine.Events.UnityAction>((act) =>
            {
                return(new UnityEngine.Events.UnityAction(() =>
                {
                    ((System.Action)act)();
                }));
            });

            m_AppDomain.DelegateManager.RegisterMethodDelegate <System.String, UnityEngine.Object, System.Object[]>();

            m_AppDomain.DelegateManager.RegisterDelegateConvertor <Improve.OnAsyncFinish>((act) =>
            {
                return(new Improve.OnAsyncFinish((path, obj, paramList) =>
                {
                    ((System.Action <System.String, UnityEngine.Object, System.Object[]>)act)(path, obj, paramList);
                }));
            });

            //m_AppDomain.RegisterValueTypeBinder(typeof(Vector3), new Vector3Binder());
            m_AppDomain.RegisterCrossBindingAdaptor(new MonoBehaviourAdapter());
            m_AppDomain.RegisterCrossBindingAdaptor(new CoroutineAdapter());
            m_AppDomain.RegisterCrossBindingAdaptor(new GComponentAdapter());

            //CLR重定向的注册
            //DebugLog的重定向
            var mi = typeof(Debug).GetMethod("Log", new System.Type[] { typeof(object) });
            m_AppDomain.RegisterCLRMethodRedirection(mi, Log_11);

            //GameObject的get,add Commponent的注册
            SetupCLRRedirectionAddGetComponent();
            //FairyGUI的创建UI的重定向
            SetupCLRRedirectionSetPackageItemExtension();

            //初始化CLR绑定请放在初始化的最后一步!!
            //CLR绑定借助了ILRuntime的CLR重定向机制来实现,因为实质上也是将对CLR方法的反射调用重定向到我们自己定义的方法里面来。
            //但是手动编写CLR重定向方法是个工作量非常巨大的事,而且要求对ILRuntime底层机制非常了解(比如如何装拆箱基础类型,怎么处理Ref/Out引用等等),
            //因此ILRuntime提供了一个代码生成工具来自动生成CLR绑定代码。
            //在CLR绑定代码生成之后,需要将这些绑定代码注册到AppDomain中才能使CLR绑定生效,
            //但是一定要记得将CLR绑定的注册写在CLR重定向的注册后面,因为同一个方法只能被重定向一次,只有先注册的那个才能生效。
            //请在生成了绑定代码后解除下面的的注释,将这些绑定代码注册到AppDomain中
            ILRuntime.Runtime.Generated.CLRBindings.Initialize(m_AppDomain);
        }
        public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
        {
            MethodInfo[] jsonConvertMethods = typeof(JsonConvert).GetMethods();
            for (int i = 0; i < jsonConvertMethods.Length; i++)
            {
                if (jsonConvertMethods[i].Name == "DeserializeObject" && jsonConvertMethods[i].IsGenericMethodDefinition)
                {
                    var param = jsonConvertMethods[i].GetParameters();
                    if (param.Length == 1)
                    {
                        app.RegisterCLRMethodRedirection(jsonConvertMethods[i], DeserializeObject1);
                    }
                    else
                    {
                        if (param[1].ParameterType == typeof(JsonSerializerSettings))
                        {
                            app.RegisterCLRMethodRedirection(jsonConvertMethods[i], DeserializeObject2);
                        }
                        else if (param[1].ParameterType == typeof(JsonConverter[]))
                        {
                            app.RegisterCLRMethodRedirection(jsonConvertMethods[i], DeserializeObject3);
                        }
                    }
                }
            }

            MethodInfo[] jTokenMethods = typeof(JToken).GetMethods();
            for (int i = 0; i < jTokenMethods.Length; i++)
            {
                if (jTokenMethods[i].Name == "ToObject" && jTokenMethods[i].IsGenericMethodDefinition)
                {
                    var param = jTokenMethods[i].GetParameters();
                    if (param.Length == 0)
                    {
                        app.RegisterCLRMethodRedirection(jTokenMethods[i], ToObject1);
                    }
                    else if (param.Length == 1)
                    {
                        app.RegisterCLRMethodRedirection(jTokenMethods[i], ToObject2);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
        {
            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
            MethodBase   method;

            Type[] args;
            Type   type = typeof(UnityEngine.Debug);

            args   = new Type[] { typeof(System.Object) };
            method = type.GetMethod("Log", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, Log_0);
            args   = new Type[] { typeof(System.Object) };
            method = type.GetMethod("LogWarning", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogWarning_1);
            args   = new Type[] { typeof(System.Exception) };
            method = type.GetMethod("LogException", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogException_2);
            args   = new Type[] { typeof(System.Object) };
            method = type.GetMethod("LogError", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogError_3);
        }
Ejemplo n.º 6
0
 public unsafe static void RegisterILRuntimeCLRRedirection(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
 {
     appDomain = appdomain;
     foreach (var i in typeof(ConfigUtility).GetMethods())
     {
         if (i.Name == "ToObject" && i.IsGenericMethodDefinition)
         {
             //var param = i.GetParameters();
             appdomain.RegisterCLRMethodRedirection(i, ILToObject);
         }
     }
 }
Ejemplo n.º 7
0
 public unsafe static void RegisterILRuntimeCLRRedirection(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
 {
     foreach (var i in typeof(JsonMapper).GetMethods())
     {
         if (i.Name == "ToObject" && i.IsGenericMethodDefinition)
         {
             var param = i.GetParameters();
             if (param[0].ParameterType == typeof(string))
             {
                 appdomain.RegisterCLRMethodRedirection(i, JsonToObject);
             }
             else if (param[0].ParameterType == typeof(JsonReader))
             {
                 appdomain.RegisterCLRMethodRedirection(i, JsonToObject2);
             }
             else if (param[0].ParameterType == typeof(TextReader))
             {
                 appdomain.RegisterCLRMethodRedirection(i, JsonToObject3);
             }
         }
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 注册SqliteHelper的ILR重定向
 /// </summary>
 /// <param name="appdomain"></param>
 public unsafe static void RegisterCLRRedirection(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
 {
     foreach (var mi in typeof(TableQueryForILRuntime).GetMethods())
     {
         if (mi.Name == "FromAll" && mi.IsGenericMethodDefinition)
         {
             var param = mi.GetParameters();
             if (param[0].ParameterType == typeof(string))
             {
                 appdomain.RegisterCLRMethodRedirection(mi, ReDirFromAll);
             }
         }
     }
 }
Ejemplo n.º 9
0
 public unsafe static void RegisterILRuntimeCLRRedirection(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
 {
     foreach (var i in typeof(MPack).GetMethods())
     {
         if (i.Name == "ParseFromBytes" && i.IsGenericMethod)
         {
             var param = i.GetParameters();
             if (param[0].ParameterType == typeof(byte[]))
             {
                 appdomain.RegisterCLRMethodRedirection(i, MPackParseFromBytes);
             }
         }
     }
 }
        /// <summary>
        /// 注册CLR重定向
        /// </summary>
        /// <param name="appDomain">AppDomain</param>
        public static void Register(ILRuntimeDomain appDomain)
        {
            var methods = typeof(App).GetMethods();

            foreach (var method in methods)
            {
                var redirection = mapping.GetRedirection(method);
                if (redirection == null)
                {
                    continue;
                }

                appDomain.RegisterCLRMethodRedirection(method, redirection);
            }
        }
        /// <summary>
        /// 注册CLR重定向
        /// </summary>
        /// <param name="appDomain">AppDomain</param>
        public static void Register(ILRuntimeDomain appDomain)
        {
            var methods = Arr.Merge(typeof(IBindable <IBindData>).GetMethods(),
                                    typeof(Bindable <IBindData>).GetMethods());

            foreach (var method in methods)
            {
                var redirection = mapping.GetRedirection(method);

                if (redirection == null)
                {
                    continue;
                }

                appDomain.RegisterCLRMethodRedirection(method, redirection);
            }
        }
        public unsafe void Register(AppDomain appdomain)
        {
            //注册Get和Add Component
            Type gameObjectType     = typeof(GameObject);
            var  addComponentMethod = gameObjectType.GetMethods().ToList()
                                      .Find(i => i.Name == "AddComponent" && i.GetGenericArguments().Length == 1);

            appdomain.RegisterCLRMethodRedirection(addComponentMethod, AddComponent);

            var getComponentMethod = gameObjectType.GetMethods().ToList()
                                     .Find(i => i.Name == "GetComponent" && i.GetGenericArguments().Length == 1);

            appdomain.RegisterCLRMethodRedirection(getComponentMethod, GetComponent);

            //注册3种Log
            Type debugType = typeof(Debug);
            var  logMethod = debugType.GetMethod("Log", new[] { typeof(object) });

            appdomain.RegisterCLRMethodRedirection(logMethod, Log);
            var logWarningMethod = debugType.GetMethod("LogWarning", new[] { typeof(object) });

            appdomain.RegisterCLRMethodRedirection(logWarningMethod, LogWarning);
            var logErrorMethod = debugType.GetMethod("LogError", new[] { typeof(object) });

            appdomain.RegisterCLRMethodRedirection(logErrorMethod, LogError);

            //注册3种Print
            Type printType   = typeof(Log);
            var  printMethod = printType.GetMethod("Print", new[] { typeof(object) });

            appdomain.RegisterCLRMethodRedirection(printMethod, Print);
            var printWarningMethod = printType.GetMethod("PrintWarning", new[] { typeof(object) });

            appdomain.RegisterCLRMethodRedirection(printWarningMethod, PrintWarning);
            var printErrorMethod = printType.GetMethod("PrintError", new[] { typeof(object) });

            appdomain.RegisterCLRMethodRedirection(printErrorMethod, PrintError);
        }
        public void Init(AppDomain app)
        {
            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
            MethodBase   method;
            FieldInfo    field;

            Type[] args;
            Type   type = typeof(SGF.Debuger);

            args   = new Type[] { typeof(System.String) };
            method = type.GetMethod("Log", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, Log_1);

            args   = new Type[] { typeof(System.String), typeof(System.Object[]) };
            method = type.GetMethod("Log", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, Log_2);

            args   = new Type[] { typeof(SGF.ILogTag), typeof(System.String) };
            method = type.GetMethod("Log", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, Log_3);

            args   = new Type[] { typeof(SGF.ILogTag), typeof(System.String), typeof(System.Object[]) };
            method = type.GetMethod("Log", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, Log_4);

            args   = new Type[] { typeof(System.String) };
            method = type.GetMethod("LogWarning", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogWarning_5);

            args   = new Type[] { typeof(System.String), typeof(System.Object[]) };
            method = type.GetMethod("LogWarning", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogWarning_6);

            args   = new Type[] { typeof(SGF.ILogTag), typeof(System.String) };
            method = type.GetMethod("LogWarning", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogWarning_7);

            args   = new Type[] { typeof(SGF.ILogTag), typeof(System.String), typeof(System.Object[]) };
            method = type.GetMethod("LogWarning", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogWarning_8);

            args   = new Type[] { typeof(System.String) };
            method = type.GetMethod("LogError", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogError_9);

            args   = new Type[] { typeof(System.String), typeof(System.Object[]) };
            method = type.GetMethod("LogError", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogError_10);

            args   = new Type[] { typeof(SGF.ILogTag), typeof(System.String) };
            method = type.GetMethod("LogError", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogError_11);

            args   = new Type[] { typeof(SGF.ILogTag), typeof(System.String), typeof(System.Object[]) };
            method = type.GetMethod("LogError", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, LogError_12);
        }
Ejemplo n.º 14
0
        public unsafe static void Register(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
        {
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
            appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
#endif
            // 注册litjson
            LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(appdomain);

            appdomain.RegisterCrossBindingAdaptor(new MonoBehaviourAdapter());
            appdomain.RegisterCrossBindingAdaptor(new CoroutineAdapter());
            appdomain.RegisterCrossBindingAdaptor(new IAsyncStateMachineClassInheritanceAdaptor());

            //添加值绑定
            appdomain.RegisterValueTypeBinder(typeof(Vector3), new Vector3Binder());
            appdomain.RegisterValueTypeBinder(typeof(Quaternion), new QuaternionBinder());
            appdomain.RegisterValueTypeBinder(typeof(Vector2), new Vector2Binder());

            //Log 重定向 为了显示行号
            var mi = typeof(HFLog).GetMethod("L", new System.Type[] { typeof(object) });
            appdomain.RegisterCLRMethodRedirection(mi, L);

            mi = typeof(HFLog).GetMethod("C", new System.Type[] { typeof(object), typeof(LogColor) });
            appdomain.RegisterCLRMethodRedirection(mi, C);

            mi = typeof(HFLog).GetMethod("E", new System.Type[] { typeof(object) });
            appdomain.RegisterCLRMethodRedirection(mi, E);

            //==============================如果将委托实例传出给ILRuntime外部使用=============================================
            appdomain.DelegateManager.RegisterMethodDelegate <int>();
            appdomain.DelegateManager.RegisterMethodDelegate <int, MemoryStream>();
            appdomain.DelegateManager.RegisterMethodDelegate <Scene, LoadSceneMode>();
            appdomain.DelegateManager.RegisterMethodDelegate <int, object>();
            appdomain.DelegateManager.RegisterMethodDelegate <GameObject>();
            appdomain.DelegateManager.RegisterMethodDelegate <bool>();
            appdomain.DelegateManager.RegisterMethodDelegate <GameObject[]>();
            appdomain.DelegateManager.RegisterMethodDelegate <UnityEngine.Object[]>();
            appdomain.DelegateManager.RegisterMethodDelegate <Sprite[]>();
            appdomain.DelegateManager.RegisterMethodDelegate <BaseEventData>();
            appdomain.DelegateManager.RegisterMethodDelegate <float>();
            appdomain.DelegateManager.RegisterMethodDelegate <Dictionary <string, string> >();
            appdomain.DelegateManager.RegisterMethodDelegate <string>();
            appdomain.DelegateManager.RegisterMethodDelegate <NotificationMessage>();
            appdomain.DelegateManager.RegisterMethodDelegate <AssetPackage>();
            appdomain.DelegateManager.RegisterFunctionDelegate <GameObject>();
            appdomain.DelegateManager.RegisterMethodDelegate <System.Int32, System.Byte[]>();

            //===========================委托转换器(DelegateConvertor)================================================
            // 如果你需要将一个不是Action或者Func类型的委托实例传到ILRuntime外部使用的话,
            // 除了委托适配器,还需要额外写一个转换器,将Action和Func转换成你真正需要的那个委托类型
            appdomain.DelegateManager.RegisterDelegateConvertor <UnityAction>((action) =>
            {
                return(new UnityAction(() => { ((Action)action)(); }));
            });

            appdomain.DelegateManager.RegisterDelegateConvertor <UnityAction <bool> >((action) =>
            {
                return(new UnityAction <bool>((a) => { ((Action <bool>)action)(a); }));
            });

            appdomain.DelegateManager.RegisterDelegateConvertor <UnityAction <string> >((action) =>
            {
                return(new UnityAction <string>((a) => { ((Action <string>)action)(a); }));
            });

            appdomain.DelegateManager.RegisterDelegateConvertor <UnityAction <float> >((action) =>
            {
                return(new UnityAction <float>((a) => { ((Action <float>)action)(a); }));
            });

            appdomain.DelegateManager.RegisterDelegateConvertor <UnityAction <Scene, LoadSceneMode> >((action) =>
            {
                return(new UnityAction <Scene, LoadSceneMode>((s, l) =>
                {
                    ((Action <Scene, LoadSceneMode>)action)(s, l);
                }));
            });

            appdomain.DelegateManager.RegisterDelegateConvertor <TweenCallback>((action) =>
            {
                return(new TweenCallback(() => { ((Action)action)(); }));
            });

            appdomain.DelegateManager.RegisterDelegateConvertor <UnityAction <BaseEventData> >((action) =>
            {
                return(new UnityAction <BaseEventData>((p) => { ((Action <BaseEventData>)action)(p); }));
            });
        }
Ejemplo n.º 15
0
        public static void RegisterCLRRedirection(ILRuntime.Runtime.Enviorment.AppDomain app)
        {
            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
            MethodBase   method;

            Type[] args;
            Type   type = typeof(BDFramework.DataListener.ValueListenerEx);

            args   = new Type[] { typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum), typeof(System.Object), typeof(System.Boolean) };
            method = type.GetMethod("SetData", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, SetData_0);
            Dictionary <string, List <MethodInfo> > genericMethods = new Dictionary <string, List <MethodInfo> >();
            List <MethodInfo> lst = null;

            foreach (var m in type.GetMethods())
            {
                if (m.IsGenericMethodDefinition)
                {
                    if (!genericMethods.TryGetValue(m.Name, out lst))
                    {
                        lst = new List <MethodInfo>();
                        genericMethods[m.Name] = lst;
                    }
                    lst.Add(m);
                }
            }
            args = new Type[] { typeof(System.Int32) };
            if (genericMethods.TryGetValue("GetData", out lst))
            {
                foreach (var m in lst)
                {
                    if (m.MatchGenericParameters(args, typeof(System.Int32), typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum)))
                    {
                        method = m.MakeGenericMethod(args);
                        app.RegisterCLRMethodRedirection(method, GetData_1);

                        break;
                    }
                }
            }
            args   = new Type[] { typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum), typeof(System.Action <System.Object>), typeof(System.Int32), typeof(System.Int32), typeof(System.Boolean) };
            method = type.GetMethod("AddListener", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, AddListener_2);
            args   = new Type[] { typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum), typeof(System.Action <System.Object>), typeof(System.Int32), typeof(System.Boolean) };
            method = type.GetMethod("AddListenerOnce", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, AddListenerOnce_3);
            args   = new Type[] { typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum) };
            method = type.GetMethod("RemoveListener", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, RemoveListener_4);
            args   = new Type[] { typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum) };
            method = type.GetMethod("ClearListener", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, ClearListener_5);
            args   = new Type[] { typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum), typeof(System.Object), typeof(System.Boolean) };
            method = type.GetMethod("TriggerEvent", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, TriggerEvent_6);
            args = new Type[] { typeof(System.Object) };
            if (genericMethods.TryGetValue("AddListener", out lst))
            {
                foreach (var m in lst)
                {
                    if (m.MatchGenericParameters(args, typeof(void), typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum), typeof(System.Action <System.Object>), typeof(System.Int32), typeof(System.Int32), typeof(System.Boolean)))
                    {
                        method = m.MakeGenericMethod(args);
                        app.RegisterCLRMethodRedirection(method, AddListener_7);

                        break;
                    }
                }
            }
            args = new Type[] { typeof(ILRuntime.Runtime.Intepreter.ILTypeInstance) };
            if (genericMethods.TryGetValue("AddListener", out lst))
            {
                foreach (var m in lst)
                {
                    if (m.MatchGenericParameters(args, typeof(void), typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum), typeof(System.Action <ILRuntime.Runtime.Intepreter.ILTypeInstance>), typeof(System.Int32), typeof(System.Int32), typeof(System.Boolean)))
                    {
                        method = m.MakeGenericMethod(args);
                        app.RegisterCLRMethodRedirection(method, AddListener_8);

                        break;
                    }
                }
            }
            args   = new Type[] { typeof(BDFramework.DataListener.AStatusListener), typeof(System.Enum), typeof(System.Action <System.Object>) };
            method = type.GetMethod("RemoveListener", flag, null, args, null);
            app.RegisterCLRMethodRedirection(method, RemoveListener_9);
        }