| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 | using ILRuntime.Runtime.Enviorment;using System.Collections;using System.IO;using UnityEngine;using FairyGUI;using System.Reflection;using GFGGame.Launcher;//using ILRuntime.Generated.CrossBinding;using System;using VEngine;using System.Linq;//下面这行为了取消使用WWW的警告,Unity2018以后推荐使用UnityWebRequest,处于兼容性考虑Demo依然使用WWW#pragma warning disable CS0618namespace GFGGame{    class ILRuntimeLauncher : SingletonMonoBase<ILRuntimeLauncher>    {        ILRuntime.Runtime.Enviorment.AppDomain appdomain;        System.IO.MemoryStream dllStream;        System.IO.MemoryStream pdbStream;        public void LoadAssembly(byte[] assBytes, byte[] pdbBytes)        {            //首先实例化ILRuntime的AppDomain,AppDomain是一个应用程序域,每个AppDomain都是一个独立的沙盒            appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();            dllStream = new MemoryStream(assBytes);            pdbStream = new MemoryStream(pdbBytes);            try            {                appdomain.LoadAssembly(dllStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());            }            catch            {                Debug.LogError("加载热更DLL失败,请确保已经编译过热更DLL");            }            InitializeILRuntime();            OnILHotFixLoaded();        }        void InitializeILRuntime()        {#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)            //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler            appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;            appdomain.DebugService.StartDebugService(56000);#endif            //在使用LitJson前,需要对LitJson进行注册            ILRuntimeHelper.RegisterILRuntimeCLRRedirection(appdomain);            //这里做一些ILRuntime的注册,如委托适配器,值类型绑定等等            appdomain.DelegateManager.RegisterMethodDelegate<EventContext>();            appdomain.DelegateManager.RegisterMethodDelegate<System.String>();            appdomain.DelegateManager.RegisterMethodDelegate<System.String, System.String, UnityEngine.LogType>();            appdomain.DelegateManager.RegisterMethodDelegate<System.Object>();             appdomain.DelegateManager.RegisterMethodDelegate<System.Int32, FairyGUI.GObject>();            appdomain.DelegateManager.RegisterMethodDelegate<FairyGUI.GTweener>();            appdomain.DelegateManager.RegisterFunctionDelegate<System.Int32, System.Int32, System.Int32>();            appdomain.DelegateManager.RegisterFunctionDelegate<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance, System.Int32>();            appdomain.DelegateManager.RegisterMethodDelegate<System.Object, System.Timers.ElapsedEventArgs>();            appdomain.DelegateManager.RegisterDelegateConvertor<EventCallback0>((action) =>            {                return new EventCallback0(() =>                {                    ((System.Action)action)();                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<EventCallback1>((action) =>            {                return new EventCallback1((e) =>                {                    ((System.Action<EventContext>)action)(e);                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Application.LogCallback>((act) =>            {                return new UnityEngine.Application.LogCallback((condition, stackTrace, type) =>                {                    ((Action<System.String, System.String, UnityEngine.LogType>)act)(condition, stackTrace, type);                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.TimerCallback>((act) =>            {                return new FairyGUI.TimerCallback((param) =>                {                    ((Action<System.Object>)act)(param);                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.ListItemRenderer>((act) =>            {                return new FairyGUI.ListItemRenderer((index, item) =>                {                    ((Action<System.Int32, FairyGUI.GObject>)act)(index, item);                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.GTweenCallback1>((act) =>            {                return new FairyGUI.GTweenCallback1((tweener) =>                {                    ((Action<FairyGUI.GTweener>)act)(tweener);                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<System.Comparison<System.Int32>>((act) =>            {                return new System.Comparison<System.Int32>((x, y) =>                {                    return ((Func<System.Int32, System.Int32, System.Int32>)act)(x, y);                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.GTweenCallback>((act) =>            {                return new FairyGUI.GTweenCallback(() =>                {                    ((Action)act)();                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.PlayCompleteCallback>((act) =>            {                return new FairyGUI.PlayCompleteCallback(() =>                {                    ((Action)act)();                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<FairyGUI.TransitionHook>((act) =>            {                return new FairyGUI.TransitionHook(() =>                {                    ((Action)act)();                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<System.Comparison<ILRuntime.Runtime.Intepreter.ILTypeInstance>>((act) =>            {                return new System.Comparison<ILRuntime.Runtime.Intepreter.ILTypeInstance>((x, y) =>                {                    return ((Func<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance, System.Int32>)act)(x, y);                });            });            appdomain.DelegateManager.RegisterDelegateConvertor<System.Timers.ElapsedEventHandler>((act) =>            {                return new System.Timers.ElapsedEventHandler((sender, e) =>                {                    ((Action<System.Object, System.Timers.ElapsedEventArgs>)act)(sender, e);                });            });            //appdomain.RegisterCrossBindingAdaptor(new ScriptableObjectAdapter());            //初始化CLR绑定请放在初始化的最后一步!!            ILRuntime.Runtime.Generated.CLRBindings.Initialize(appdomain);        }        void OnILHotFixLoaded()        {            HotUpdateCodeLoader.Instance.allTypes = appdomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray();            //第一次方法调用            appdomain.Invoke("GFGGame.HotUpdate.HotUpdateEntry", "Start", null, null);        }            }}
 |