Ejemplo n.º 1
0
    private void OnHotFixLoaded()
    {
        TestClassBase obj;

        try
        {
            obj = appdomain.Instantiate <TestClassBase>("HotFix_Project.MyTestInheritance");
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
        Debug.Log("Oops, 报错了,因为跨域继承必须要注册适配器。 如果是热更DLL里面继承热更里面的类型,不需要任何注册。");

        Debug.Log("所以现在我们来注册适配器");
        appdomain.RegisterCrossBindingAdaptor(new MyInheritanceAdapter());

        Debug.Log("现在再来尝试创建一个实例");

        obj = appdomain.Instantiate <TestClassBase>("HotFix_Project.MyTestInheritance");
        Debug.Log("现在来调用成员方法");
        obj.TestAbstract(456);
        obj.TestVirtual("Hello World");
        //Debug.Log(obj.Value);

        Debug.Log("现在换个方式创建实例");
        obj = appdomain.Invoke("HotFix_Project.MyTestInheritance", "NewObject", null, null) as TestClassBase;

        obj.TestAbstract(678);
        obj.TestVirtual("second");
    }
Ejemplo n.º 2
0
    void ILRuntimeTest()
    {
        Debug.Log(appdomain.Invoke("Hotfix.Test", "GetMsg", null, null));
        object[] param = new object[1];
        param[0] = 666;
        Debug.Log(appdomain.Invoke("Hotfix.Test", "GetMsg1", null, param));
        var testInst = appdomain.Instantiate <IUIInterface>("Hotfix.Test");

        Debug.Log(appdomain.Invoke("Hotfix.Test", "GetMsg2", testInst, null));
        testInst.Show();
        Debug.Log(testInst.GetStr());
        // var testUIInst = appdomain.Instantiate<IUIInterface>("Hotfix.TestUI");
        // testUIInst.Show();
        // Types();
    }
Ejemplo n.º 3
0
        //========================================================================
        //创建实例
        //========================================================================
        private object CreateInstanceInternal(string typeName, string assemblyName, object[] args)
        {
            if (RunMode == RunMode.Native)
            {
                if (string.IsNullOrEmpty(assemblyName))
                {
                    return(System.AppDomain.CurrentDomain.GetType().Assembly.CreateInstance(typeName));
                }
                else
                {
                    Type type = Type.GetType(typeName + "," + assemblyName);
                    if (type != null)
                    {
                        return(Activator.CreateInstance(type, args));
                    }
                }
            }
            else
            {
                ILTypeInstance obj = m_appdomain.Instantiate(typeName, args);
                if (obj != null)
                {
                    return(obj.CLRInstance);
                }
            }

            Debuger.LogError("找不到类型:" + typeName);
            return(null);
        }
Ejemplo n.º 4
0
    void OnILRuntimeInitialized()
    {
        //获取Hotfix.dll内部定义的类
        List <Type> allTypes = new List <Type>();
        var         values   = appdomain.LoadedTypes.Values.ToList();

        foreach (var v in values)
        {
            allTypes.Add(v.ReflectionType);
        }
        //去重
        allTypes = allTypes.Distinct().ToList();

        DllUIUpdateList.Clear();
        foreach (var v in allTypes)
        {
            //找到实现IUI接口的类 Adaptor 前面写的适配器IUI的类
            if (v.IsClass && v.GetInterface("Adaptor") != null)
            {
                //生成实例
                var gs = appdomain.Instantiate <IUI>(v.FullName);

                //调用接口方法
                gs.Start();
                DllUIUpdateList.Add(gs.Update);
            }
        }
    }
Ejemplo n.º 5
0
    public void Init()
    {
        using (System.IO.MemoryStream fs = new MemoryStream(HotFixDll.bytes))
        {
            m_AssemblyILR.LoadAssembly(fs);
        }
        m_AssemblyILR.AllowUnboundCLRMethod = true;

        ILRuntime.Runtime.Enviorment.AppDomain.ShowDebugInfo = false;


        //注册跨域类
        m_AssemblyILR.RegisterCrossBindingAdaptor(new IGameHotFixInterfaceAdapter());


        m_AssemblyILR.DelegateManager.RegisterMethodDelegate <UnityEngine.GameObject>();
        m_AssemblyILR.DelegateManager.RegisterMethodDelegate <System.Int32>();
        m_AssemblyILR.DelegateManager.RegisterMethodDelegate <System.Boolean>();
        m_AssemblyILR.DelegateManager.RegisterMethodDelegate <ILRuntime.Runtime.Intepreter.ILTypeInstance>();

        m_AssemblyILR.DelegateManager.RegisterMethodDelegate <System.Int32, System.Int32>();
        m_AssemblyILR.DelegateManager.RegisterMethodDelegate <System.Object>();

        LCLFunctionDelegate.Reg(m_AssemblyILR);

        ILRuntime.Runtime.Generated.CLRBindings.Initialize(m_AssemblyILR);

        string HotFixLoop = "LCL.HotFixLoop";

        m_HotFixDll = m_AssemblyILR.Instantiate <IGameHotFixInterface>(HotFixLoop);
        m_HotFixDll.Start();
    }
Ejemplo n.º 6
0
    private void OnILRuntimeInitialized()
    {
        gameManager = appDomain.Instantiate("TestHotFix.GameManager", null);

        appDomain.Invoke("TestHotFix.GameManager", "Init", gameManager, null);

        //appDomain.Invoke("TestHotFix.Class1", "Test1", null, null);
    }
Ejemplo n.º 7
0
 public void OnLoadClass(string entranceClass, GameObject rootObj, bool CanBeUnloaded = true, string arg = "")
 {
     if (CanBeUnloaded)
     {
         OnUnloadClass(entranceClass);
         dObjs.Add(entranceClass, rootObj);
     }
     appdomain.Invoke(entranceClass, "SetGameObj", appdomain.Instantiate(entranceClass), rootObj, arg);
 }
Ejemplo n.º 8
0
Archivo: PBType.cs Proyecto: wqaetly/ET
        public static unsafe void RegisterILRuntimeCLRRedirection(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
        {
            Type[]       args;
            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static |
                                BindingFlags.DeclaredOnly;
            // 注册pb反序列化
            Type pbSerializeType = typeof(ProtoBuf.Serializer);

            args = new[] { typeof(Type), typeof(Stream) };
            var pbDeserializeMethod = pbSerializeType.GetMethod("Deserialize", flag, null, args, null);

            appdomain.RegisterCLRMethodRedirection(pbDeserializeMethod, Deserialize_1);
            args = new[] { typeof(ILTypeInstance) };
            Dictionary <string, List <MethodInfo> > genericMethods = new Dictionary <string, List <MethodInfo> >();
            List <MethodInfo> lst = null;

            foreach (var m in pbSerializeType.GetMethods())
            {
                if (m.IsGenericMethodDefinition)
                {
                    if (!genericMethods.TryGetValue(m.Name, out lst))
                    {
                        lst = new List <MethodInfo>();
                        genericMethods[m.Name] = lst;
                    }

                    lst.Add(m);
                }
            }

            if (genericMethods.TryGetValue("Deserialize", out lst))
            {
                foreach (var m in lst)
                {
                    if (m.MatchGenericParameters(args, typeof(ILTypeInstance), typeof(Stream)))
                    {
                        var method = m.MakeGenericMethod(args);
                        appdomain.RegisterCLRMethodRedirection(method, Deserialize_2);
                        break;
                    }
                }
            }

            RegisterFunctionCreateInstance(typeName => appdomain.Instantiate(typeName));
            RegisterFunctionGetRealType(o =>
            {
                var type = o.GetType();
                if (type.FullName == "ILRuntime.Runtime.Intepreter.ILTypeInstance")
                {
                    var ilo = o as ILRuntime.Runtime.Intepreter.ILTypeInstance;
                    type    = FindType(ilo.Type.FullName);
                }

                return(type);
            });
        }
Ejemplo n.º 9
0
 public object CreateInstance(Type type, params object[] args)
 {
     if (type is ILRuntimeType || type is ILRuntimeWrapperType)
     {
         return(m_AppDomain.Instantiate(type.FullName, args));
     }
     else
     {
         return(Activator.CreateInstance(type, args));
     }
 }
Ejemplo n.º 10
0
    private void RunHotFixVrCoreEntity()
    {
        //热更代码入口处
        _hotFixVrCoreEntity = Appdomain.Instantiate <SubMonoBehavior>("Hotfix.HotFixMode");
#if UNITY_EDITOR
        if (_hotFixVrCoreEntity == null)
        {
            UnityEngine.Debug.LogError("_hotFixVrCoreEntity == null \n 无法进入热更代码!");
        }
#endif
    }
    public void OnLoadClass(string entranceClass, GameObject rootObj, bool CanBeUnloaded = true, string arg = "")
    {
#if ILRUNTIME
        if (CanBeUnloaded)
        {
            OnUnloadClass(entranceClass);
            dObjs.Add(entranceClass, rootObj);
        }
        appdomain.Invoke(entranceClass, "SetGameObj", appdomain.Instantiate(entranceClass), rootObj, arg);
#else
        var obj = assembly.CreateInstance(entranceClass);
        obj.GetType().GetMethod("SetGameObj").Invoke(obj, new object[] { rootObj, arg });
#endif
    }
Ejemplo n.º 12
0
    private void TestMember()
    {
        const string CLASS_NAME = "HotUpdate.TestMember";

        object obj = appdomain.Instantiate(CLASS_NAME, null);

        // 属性
        int    member1 = (int)appdomain.Invoke(CLASS_NAME, "get_Member1", obj, null);
        string member2 = (string)appdomain.Invoke(CLASS_NAME, "get_Member2", obj, null);

        Debug.LogFormat("ILManager.TestMember().member1 = {0}", member1);
        Debug.LogFormat("ILManager.TestMember().member2 = {0}", member2);

        // 成员方法
        appdomain.Invoke(CLASS_NAME, "HelloWorld", obj, null);
        string append = (string)appdomain.Invoke(CLASS_NAME, "Append", obj, new object[] { "str1", "str2" });

        Debug.LogFormat("ILManager.TestMember().append = {0}", append);
    }
Ejemplo n.º 13
0
        object GetInstance(string runtimeClassName)
        {
            object instance;

            if (_appdomain != null)
            {
                Debug.Log("runtimeClassName " + runtimeClassName);
                instance = _appdomain.Instantiate(runtimeClassName);
            }
            else
            {
                Assembly assembly1    = Assembly.Load("SampleHotProject1");
                Assembly assembly2    = Assembly.Load("SampleHotProject2");
                var      instanceType = assembly1.GetType(runtimeClassName);
                if (instanceType == null)
                {
                    instanceType = assembly2.GetType(runtimeClassName);
                }

                instance = Activator.CreateInstance(instanceType);
            }

            return(instance);
        }
Ejemplo n.º 14
0
 private static object PType_CreateInstance(ILRuntime.Runtime.Enviorment.AppDomain appDomain, string typeName)
 {
     return(appDomain.Instantiate(typeName));
 }
Ejemplo n.º 15
0
        public unsafe static StackObject *CreateInstance(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 3);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            System.Object[] @args = (System.Object[]) typeof(System.Object[]).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 2);
            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, 3);
            TinaX.XCore instance_of_this_method = (TinaX.XCore) typeof(TinaX.XCore).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            object obj_result_of_this_method = (@type is ILRuntime.Reflection.ILRuntimeType) ? __domain.Instantiate(@type.FullName, @args) : instance_of_this_method.CreateInstance(@type, @args);

            if (obj_result_of_this_method is CrossBindingAdaptorType)
            {
                return(ILIntepreter.PushObject(__ret, __mStack, ((CrossBindingAdaptorType)obj_result_of_this_method).ILInstance, true));
            }
            return(ILIntepreter.PushObject(__ret, __mStack, obj_result_of_this_method, true));
        }
Ejemplo n.º 16
0
 static object PType_CreateInstance(string typeName)
 {
     return(appDomain.Instantiate(typeName));
 }
Ejemplo n.º 17
0
 public void OnLoadClass(string entranceClass, GameObject rootObj, string arg = "")
 {
     Objs.Add(rootObj);
     appdomain.Invoke(entranceClass, "SetGameObj", appdomain.Instantiate(entranceClass), rootObj, arg);
 }
Ejemplo n.º 18
0
        public object CreateInstance(Type type)
        {
            var _type = appdomain.GetType(type);

            return(appdomain.Instantiate(_type.FullName));
        }
Ejemplo n.º 19
0
    private void OnHotFixLoaded()
    {
        appdomain.Invoke("HotFix_Project.InstanceClass", "StaticFunTest", null, null);


        //IType type = appdomain.LoadedTypes ("HotFix_Project.InstanceClass");
        IType   type   = appdomain.LoadedTypes["HotFix_Project.InstanceClass"];
        IMethod method = type.GetMethod("StaticFunTest", 0);

        appdomain.Invoke(method, null, null);

        Debug.Log("指定参数类型来获得IMethod");
        IType intType = appdomain.GetType(typeof(int));


        //参数类型列表
        List <IType> paramList = new List <IType>();

        paramList.Add(intType);
        IMethod method2 = type.GetMethod("StaticFunTest2", paramList, null);

        appdomain.Invoke(method2, null, 911);

        Debug.Log("实例化热更里的类");
        //第一种方式

        ILTypeInstance obj = appdomain.Instantiate("HotFix_Project.InstanceClass", new object[] { 111 });
        //第二种方式
        object obj2 = ((ILType)type).Instantiate();

        Debug.Log("调用成员方法");
        int id = (int)appdomain.Invoke("HotFix_Project.InstanceClass", "get_ID", obj, null);

        Debug.Log("!! HotFix_Project.InstanceClass.ID = " + id);
        id = (int)appdomain.Invoke("HotFix_Project.InstanceClass", "get_ID", obj2, null);
        Debug.Log("!! HotFix_Project.InstanceClass.ID = " + id);
        Debug.Log("!! cache method  StaticFunTest :");
        appdomain.Invoke(method, obj, null);

        Debug.Log("调用泛型方法");

        IType stringType = appdomain.GetType(typeof(string));

        IType[] genericArguments = { stringType };
        appdomain.InvokeGenericMethod("HotFix_Project.InstanceClass", "GenericMethod", genericArguments, null, "string generic method");

        Debug.Log("获取泛型方法的IMethod");
        paramList.Clear();
        paramList.Add(intType);
        genericArguments = new IType [] { intType };
        method           = type.GetMethod("GenericMethod", paramList, genericArguments);
        appdomain.Invoke(method, obj, 300);

        Debug.Log("获取泛型方法的IMethod 2");
        genericArguments = new IType[] { intType };
        method           = type.GetMethod("GenericMethod2", null, genericArguments);
        appdomain.Invoke(method, obj, null);

        Debug.Log("获取泛型方法的IMethod 3");
        genericArguments = new IType[] { intType };
        method           = obj.Type.GetMethod("GenericMethod3", null, genericArguments);
        appdomain.Invoke(method, obj, null);
    }