少年修仙传客户端基础资源
lwb
2021-01-26 f6ab248e23fb6485f4121294ff4167e8a15a47a3
Assets/ILRuntime/Src/ILRuntimeUtility.cs
@@ -1,19 +1,34 @@
using System;
using System.IO;
using ILRuntime.Mono.Cecil.Pdb;
using ILRuntime.Runtime.Enviorment;
class ILRuntimeUtility : SingletonMonobehaviour<ILRuntimeUtility>
{
    private AppDomain appdomain;
    public const string NameSpace = "LogicProject";
    FileStream dllFS, pdbFS;
    private ILRuntime.Runtime.Enviorment.AppDomain _appdomain;
    public ILRuntime.Runtime.Enviorment.AppDomain appDomain
    {
        get { return _appdomain; }
    }
    public void Init()
    {
        LoadILRuntime();
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
        //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler
        _appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
#endif
        //对LitJson进行注册
        LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(_appdomain);
    }
    void LoadILRuntime()
    {
        appdomain = new AppDomain();
        _appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
        var dllPath = string.Empty;
        var pdbPath = string.Empty;
        if (AssetSource.refdataFromEditor)
@@ -26,14 +41,28 @@
            dllPath = AssetVersionUtility.GetAssetFilePath("logic/Logic.dll.bytes");
            pdbPath = AssetVersionUtility.GetAssetFilePath("logic/Logic.pdb");
        }
        var fs = new FileStream(dllPath, FileMode.Open);
        var p = new FileStream(pdbPath, FileMode.Open);
        appdomain.LoadAssembly(fs, p, new PdbReaderProvider());
        if (!File.Exists(dllPath))
        {
            DebugEx.LogErrorFormat("找不到热更代码:{0}", dllPath);
            return;
        }
        dllFS = new FileStream(dllPath, FileMode.Open);
        if (File.Exists(pdbPath))
            pdbFS = new FileStream(pdbPath, FileMode.Open);
        _appdomain.LoadAssembly(dllFS, pdbFS, new PdbReaderProvider());
    }
    public void LaunchStart()
    {
        appdomain.Invoke("GameLogicMgr", "Init", null, null);
        _appdomain.Invoke(NameSpace + ".GameLogicMgr", "Init", null, null);
    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
        dllFS?.Close();
        pdbFS?.Close();
    }
}