using System;
|
using System.IO;
|
using ILRuntime.Mono.Cecil.Pdb;
|
|
class ILRuntimeUtility : SingletonMonobehaviour<ILRuntimeUtility>
|
{
|
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 ILRuntime.Runtime.Enviorment.AppDomain();
|
var dllPath = string.Empty;
|
var pdbPath = string.Empty;
|
if (AssetSource.refdataFromEditor)
|
{
|
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 (File.Exists(pdbPath))
|
pdbFS = new FileStream(pdbPath, FileMode.Open);
|
|
_appdomain.LoadAssembly(dllFS, pdbFS, new PdbReaderProvider());
|
}
|
|
public void LaunchStart()
|
{
|
_appdomain.Invoke(NameSpace + ".GameLogicMgr", "Init", null, null);
|
}
|
|
protected override void OnDestroy()
|
{
|
base.OnDestroy();
|
dllFS?.Close();
|
pdbFS?.Close();
|
}
|
}
|