using System; 
 | 
using System.Collections.Generic; 
 | 
using System.IO; 
 | 
using ILCrossBinding; 
 | 
using ILRuntime.CLR.Method; 
 | 
using ILRuntime.Mono.Cecil.Pdb; 
 | 
using ILRuntime.Runtime.Intepreter; 
 | 
using Snxxz.UI; 
 | 
using UnityEngine; 
 | 
  
 | 
public class ILLauncherProxy : SingletonMonobehaviour<ILLauncherProxy> 
 | 
{ 
 | 
    FileStream dllFS, pdbFS; 
 | 
  
 | 
    private ILRuntime.Runtime.Enviorment.AppDomain _appdomain; 
 | 
  
 | 
    public ILRuntime.Runtime.Enviorment.AppDomain appdomain { 
 | 
        get { 
 | 
            return _appdomain; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public bool started; 
 | 
  
 | 
    ILTypeInstance launcherInstance; 
 | 
  
 | 
    IMethod update, onDestroy; 
 | 
  
 | 
    public void Init() 
 | 
    { 
 | 
        started = false; 
 | 
        LoadILRuntime(); 
 | 
        //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler 
 | 
#if UNITY_EDITOR 
 | 
        _appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId; 
 | 
#endif 
 | 
        //对LitJson进行注册 
 | 
        LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(_appdomain); 
 | 
        RegisterCrossBindingAdaptor(appdomain); 
 | 
        RegisterValueTypeBinder(); 
 | 
        RegisterDelegate(); 
 | 
#if UNITY_EDITOR 
 | 
        // CLRBindings 是动态生成出来的,editor下可能不存在,所以使用反射方式调用 
 | 
        var type = Type.GetType("ILRuntime.Runtime.Generated.CLRBindings"); 
 | 
        if (type != null) 
 | 
        { 
 | 
            var m = type.GetMethod("Initialize"); 
 | 
            m.Invoke(null, new object[] { appdomain }); 
 | 
        } 
 | 
#else 
 | 
        ILRuntime.Runtime.Generated.CLRBindings.Initialize(_appdomain); 
 | 
#endif 
 | 
    } 
 | 
  
 | 
    void LoadILRuntime() 
 | 
    { 
 | 
        _appdomain = new ILRuntime.Runtime.Enviorment.AppDomain(); 
 | 
        var dllPath = string.Empty; 
 | 
        var pdbPath = string.Empty; 
 | 
        if (AssetSource.logicFromEditor) 
 | 
        { 
 | 
            dllPath = ResourcesPath.ResourcesOutAssetPath + "logic/Logic.dll.bytes"; 
 | 
            pdbPath = ResourcesPath.ResourcesOutAssetPath + "logic/Logic.pdb"; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            dllPath = AssetVersionUtility.GetAssetFilePath("logic/Logic.dll.bytes"); 
 | 
            pdbPath = AssetVersionUtility.GetAssetFilePath("logic/Logic.pdb"); 
 | 
        } 
 | 
        if (!File.Exists(dllPath)) 
 | 
        { 
 | 
            DebugEx.LogErrorFormat("找不到热更代码:{0}", dllPath); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        dllFS = new FileStream(dllPath, FileMode.Open); 
 | 
        if (VersionConfig.Get().debugVersion && File.Exists(pdbPath)) 
 | 
            pdbFS = new FileStream(pdbPath, FileMode.Open); 
 | 
        _appdomain.LoadAssembly(dllFS, pdbFS, new PdbReaderProvider()); 
 | 
    } 
 | 
  
 | 
    //注册跨域继承适配器 
 | 
    public static void RegisterCrossBindingAdaptor(ILRuntime.Runtime.Enviorment.AppDomain domain) 
 | 
    { 
 | 
        domain.RegisterCrossBindingAdaptor(new CoroutineAdapter()); 
 | 
        domain.RegisterCrossBindingAdaptor(new DtcBasicAdapter()); 
 | 
        domain.RegisterCrossBindingAdaptor(new GameNetPackBasicAdapter()); 
 | 
        domain.RegisterCrossBindingAdaptor(new OperationBaseAdapter()); 
 | 
        domain.RegisterCrossBindingAdaptor(new IConfigPostProcessAdapter()); 
 | 
    } 
 | 
  
 | 
    //注册值类型绑定 
 | 
    private void RegisterValueTypeBinder() 
 | 
    { 
 | 
        appdomain.RegisterValueTypeBinder(typeof(Vector3), new Vector3Binder()); 
 | 
        appdomain.RegisterValueTypeBinder(typeof(Quaternion), new QuaternionBinder()); 
 | 
        appdomain.RegisterValueTypeBinder(typeof(Vector2), new Vector2Binder()); 
 | 
    } 
 | 
  
 | 
    //注册委托 
 | 
    private void RegisterDelegate() 
 | 
    { 
 | 
        //无返回值 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<int>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<long>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<string>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<float>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<double>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<bool>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<byte[]>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<bool, bool>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<bool, string>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<int, string>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<int, float>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<string, int>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<int, bool, string>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<int, int>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<int, bool>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<bool, int>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<ulong>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<uint>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Snxxz.UI.Operation>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Snxxz.UI.Operation, int>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<float, Vector2>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<int, Transform>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Vector2>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Vector3>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Transform>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<GameObject>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Component>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<E_AttackMode>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<PlayerDataType>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<PlayerDataType, int>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<List<Vector3>>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<ChatData>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<VoiceHttpRequest.VoiceDecodec>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Snxxz.UI.ComposeMatCell, NeedMatType>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<ItemCompoundConfig, int>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Snxxz.UI.ComposeMatCell, int, SelectItemType>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<GroupType, bool>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<PackType, int>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<PackType, string>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<PackType, int, int>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<SystemSwitch, bool>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<ChatBoolType, bool>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<E_NpcType, int, uint>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<TeamInviteType>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Snxxz.UI.PlayerDetails.DetailType>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<FunctionUnlockType>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<TreasureCategory>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<Window>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<TimeMgr.SyntonyType>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<DateTime>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<System.Object>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<global::ScrollerDataType, global::CellView>(); 
 | 
        appdomain.DelegateManager.RegisterMethodDelegate<int, LitJson.JsonData>(); 
 | 
  
 | 
        //有返回值 
 | 
        appdomain.DelegateManager.RegisterFunctionDelegate<int>(); 
 | 
        appdomain.DelegateManager.RegisterFunctionDelegate<bool>(); 
 | 
        appdomain.DelegateManager.RegisterFunctionDelegate<long>(); 
 | 
        appdomain.DelegateManager.RegisterFunctionDelegate<string>(); 
 | 
        appdomain.DelegateManager.RegisterFunctionDelegate<float>(); 
 | 
        appdomain.DelegateManager.RegisterFunctionDelegate<double>(); 
 | 
        appdomain.DelegateManager.RegisterFunctionDelegate<int, bool>(); 
 | 
        appdomain.DelegateManager.RegisterFunctionDelegate<System.Int32, System.Int32, System.Int32>(); 
 | 
        appdomain.DelegateManager.RegisterFunctionDelegate<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance, System.Int32>(); 
 | 
  
 | 
        //自定义委托转换 
 | 
        appdomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Events.UnityAction>((action) => 
 | 
        { 
 | 
            return new UnityEngine.Events.UnityAction(() => 
 | 
            { 
 | 
                ((System.Action)action)(); 
 | 
            }); 
 | 
        }); 
 | 
        appdomain.DelegateManager.RegisterDelegateConvertor<System.Threading.WaitCallback>((act) => 
 | 
        { 
 | 
            return new System.Threading.WaitCallback((state) => 
 | 
             { 
 | 
                 ((Action<System.Object>)act)(state); 
 | 
             }); 
 | 
        }); 
 | 
        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<global::ScrollerController.OnRefreshCellDelegate>((act) => 
 | 
        { 
 | 
            return new global::ScrollerController.OnRefreshCellDelegate((type, cell) => 
 | 
            { 
 | 
                ((Action<global::ScrollerDataType, global::CellView>)act)(type, cell); 
 | 
            }); 
 | 
        }); 
 | 
        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); 
 | 
            }); 
 | 
        }); 
 | 
  
 | 
    } 
 | 
  
 | 
    public void LaunchStart() 
 | 
    { 
 | 
#if UNITY_EDITOR 
 | 
        _appdomain.DebugService.StartDebugService(56000); 
 | 
#endif 
 | 
        launcherInstance = appdomain.Instantiate("LogicLauncher"); 
 | 
        var type = launcherInstance.Type; 
 | 
        update = type.GetMethod("Update"); 
 | 
        onDestroy = type.GetMethod("OnDestroy", 0); 
 | 
  
 | 
        _appdomain.Invoke("LogicLauncher", "LaunchStart", launcherInstance, null); 
 | 
    } 
 | 
  
 | 
    private void Update() 
 | 
    { 
 | 
        if (update != null) 
 | 
            appdomain.Invoke(update, launcherInstance); 
 | 
    } 
 | 
  
 | 
    protected override void OnDestroy() 
 | 
    { 
 | 
        base.OnDestroy(); 
 | 
  
 | 
        if (onDestroy != null) 
 | 
            appdomain.Invoke(onDestroy, launcherInstance); 
 | 
  
 | 
#if UNITY_EDITOR 
 | 
        var type = Type.GetType("ILRuntime.Runtime.Generated.CLRBindings"); 
 | 
        if (type != null) 
 | 
        { 
 | 
            var m = type.GetMethod("Shutdown"); 
 | 
            m.Invoke(null, new object[] { appdomain }); 
 | 
        } 
 | 
#else 
 | 
        ILRuntime.Runtime.Generated.CLRBindings.Shutdown(appdomain); 
 | 
#endif 
 | 
        dllFS?.Close(); 
 | 
        pdbFS?.Close(); 
 | 
    } 
 | 
} 
 |