using System; using System.Collections.Generic; using System.IO; using ILCrossBinding; using ILRuntime.Mono.Cecil.Pdb; using Snxxz.UI; using UnityEngine; public class ILRuntimeLoader : SingletonMonobehaviour { FileStream dllFS, pdbFS; private ILRuntime.Runtime.Enviorment.AppDomain _appdomain; public ILRuntime.Runtime.Enviorment.AppDomain appDomain { get { if (_appdomain == null) DebugEx.Log("AppDomain 调用时为空!"); return _appdomain; } } public void Init() { LoadILRuntime(); //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler #if UNITY_EDITOR _appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId; #endif //对LitJson进行注册 LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(_appdomain); RegisterCrossBindingAdaptor(); 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()); } //注册跨域继承适配器 private void RegisterCrossBindingAdaptor() { appDomain.RegisterCrossBindingAdaptor(new CoroutineAdapter()); appDomain.RegisterCrossBindingAdaptor(new DtcBasicAdapter()); appDomain.RegisterCrossBindingAdaptor(new GameNetPackBasicAdapter()); } //注册值类型绑定 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(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate>(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterMethodDelegate(); appDomain.DelegateManager.RegisterDelegateConvertor((action) => { return new UnityEngine.Events.UnityAction(() => { ((System.Action)action)(); }); }); //有返回值 appDomain.DelegateManager.RegisterFunctionDelegate(); appDomain.DelegateManager.RegisterFunctionDelegate(); appDomain.DelegateManager.RegisterFunctionDelegate(); appDomain.DelegateManager.RegisterFunctionDelegate(); appDomain.DelegateManager.RegisterFunctionDelegate(); appDomain.DelegateManager.RegisterFunctionDelegate(); appDomain.DelegateManager.RegisterFunctionDelegate(); } public void LaunchStart() { #if UNITY_EDITOR _appdomain.DebugService.StartDebugService(56000); #endif _appdomain.Invoke("GameLogicMgr", "Init", null, null); } protected override void OnDestroy() { base.OnDestroy(); #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(); } }