public ILRuntimeILWorker(byte[] dllBytes, string dllDir, string dllNameWithoutExt, bool isDebug, bool isNeedPdbFile) { this.dllDir = dllDir; this.dllNameWithoutExt = dllNameWithoutExt; //首先实例化ILRuntime的AppDomain,AppDomain是一个应用程序域,每个AppDomain都是一个独立的沙盒 appDomain = new ILRuntime.Runtime.Enviorment.AppDomain(); if (isDebug) { //启动调试监听 appDomain.DebugService.StartDebugService(56000); } MemoryStream fs = new MemoryStream(dllBytes); if (isNeedPdbFile) { string pdbPath = Path.Combine(dllDir, dllNameWithoutExt + ".pdb"); byte[] pdbBytes = File.ReadAllBytes(pdbPath); MemoryStream p = new MemoryStream(pdbBytes); appDomain.LoadAssembly(fs, p, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider()); } else { appDomain.LoadAssembly(fs); } InitializeILRuntime(); _ilRuntimeTypes = appDomain.LoadedTypes.Values.ToArray(); }
/// <summary> /// 加载Hotfix程序集 /// </summary> /// <param name="dllPath"></param> /// <param name="isRegisterBindings"></param> public static void LoadHotfix(string dllPath, bool isRegisterBindings = true) { // IsRunning = true; string pdbPath = dllPath + ".pdb"; BDebug.Log("DLL加载路径:" + dllPath, "red"); // AppDomain = new AppDomain(); if (File.Exists(pdbPath)) { //这里的流不能释放,头铁的老哥别试了 fsDll = new FileStream(dllPath, FileMode.Open, FileAccess.Read); fsPdb = new FileStream(pdbPath, FileMode.Open, FileAccess.Read); AppDomain.LoadAssembly(fsDll, fsPdb, new PdbReaderProvider()); } else { //这里的流不能释放,头铁的老哥别试了 fsDll = new FileStream(dllPath, FileMode.Open, FileAccess.Read); AppDomain.LoadAssembly(fsDll); } #if UNITY_EDITOR AppDomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId; #endif //绑定的初始化 //ada绑定 AdapterRegister.RegisterCrossBindingAdaptor(AppDomain); //delegate绑定 ILRuntimeDelegateHelper.Register(AppDomain); //值类型绑定 AppDomain.RegisterValueTypeBinder(typeof(Vector2), new Vector2Binder()); AppDomain.RegisterValueTypeBinder(typeof(Vector3), new Vector3Binder()); AppDomain.RegisterValueTypeBinder(typeof(Vector4), new Vector4Binder()); AppDomain.RegisterValueTypeBinder(typeof(Quaternion), new QuaternionBinder()); //是否注册各种binding if (isRegisterBindings) { CLRBindings.Initialize(AppDomain); CLRManualBindings.Initialize(AppDomain); // ILRuntime.Runtime.Generated.PreCLRBuilding.Initialize(AppDomain); } JsonMapper.RegisterILRuntimeCLRRedirection(AppDomain); if (BDLauncher.Inst != null && Config.Inst.Data.IsDebuggerILRuntime) { AppDomain.DebugService.StartDebugService(56000); Debug.Log("热更调试器 准备待命~"); } // AppDomain.Invoke("HotfixCheck", "Log", null, null); }
public void Load(byte[] dat, byte[] pdb = null) { if (dllStream != null) { dllStream.Dispose(); } if (pdbStream != null) { pdbStream.Dispose(); } dll = dat; dllStream = new MemoryStream(dat); if (pdb != null) { pdbStream = new MemoryStream(pdb); _app.LoadAssembly(dllStream, pdbStream, new ILRuntime.Mono.Cecil.Cil.PortablePdbReaderProvider()); } else { _app.LoadAssembly(dllStream); } mainScript = _app.GetType("Main") as ILType; start = mainScript.GetMethod("Start"); Update = mainScript.GetMethod("Update"); Resize = mainScript.GetMethod("Resize"); Cmd = mainScript.GetMethod("Cmd"); FullCmd = mainScript.GetMethod("FullCmd"); Dispose = mainScript.GetMethod("Dispose"); }
public ILRuntimeILWorker(byte[] dllBytes, string libDir, string libName, bool isDebug, bool isNeedPdbFile) { Debug.Log(Log.Zero1("外部程序集执行方式:[ILRuntime]")); //首先实例化ILRuntime的AppDomain,AppDomain是一个应用程序域,每个AppDomain都是一个独立的沙盒 _appdomain = new ILRuntime.Runtime.Enviorment.AppDomain(); if (isDebug) { //启动调试监听 _appdomain.DebugService.StartDebugService(56000); } MemoryStream fs = new MemoryStream(dllBytes); if (isNeedPdbFile) { string pdbPath = Path.Combine(libDir, libName + ".pdb"); byte[] pdbBytes = File.ReadAllBytes(pdbPath); MemoryStream p = new MemoryStream(pdbBytes); _appdomain.LoadAssembly(fs, p, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider()); } else { _appdomain.LoadAssembly(fs); } InitializeILRuntime(); }
/// <summary> /// 加载热更新DLL /// </summary> public async void LoadHotfixDLL() { AppDomain = new AppDomain(); ILRuntimeHelper.InitILRuntime(AppDomain); TextAsset dllAsset = await GameEntry.Resource.AwaitLoadAsset <TextAsset>(AssetUtility.GetHotfixDLLAsset("Hotfix.dll")); byte[] dll = dllAsset.bytes; Log.Info("hotfix dll加载完毕"); #if DEBUG && !DISABLE_ILRUNTIME_DEBUG TextAsset pdbAsset = await GameEntry.Resource.AwaitLoadAsset <TextAsset>(AssetUtility.GetHotfixDLLAsset("Hotfix.pdb")); byte[] pdb = pdbAsset.bytes; Log.Info("hotfix pdb加载完毕"); AppDomain.LoadAssembly(new MemoryStream(dll), new MemoryStream(pdb), new Mono.Cecil.Pdb.PdbReaderProvider()); //启动调试服务器 AppDomain.DebugService.StartDebugService(56000); #else AppDomain.LoadAssembly(new MemoryStream(dll)); #endif //设置Unity主线程ID 这样就可以用Profiler看性能消耗了 AppDomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId; HotfixStart(); }
IEnumerator LoadHotFixAssembly() { //首先实例化ILRuntime的AppDomain,AppDomain是一个应用程序域,每个AppDomain都是一个独立的沙盒 appdomain = new ILRuntime.Runtime.Enviorment.AppDomain(); //正常项目中应该是自行从其他地方下载dll,或者打包在AssetBundle中读取,平时开发以及为了演示方便直接从StreammingAssets中读取, //正式发布的时候需要大家自行从其他地方读取dll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //这个DLL文件是直接编译HotFix_Project.sln生成的,已经在项目中设置好输出目录为StreamingAssets,在VS里直接编译即可生成到对应目录,无需手动拷贝 #if UNITY_ANDROID string dllPath = Application.streamingAssetsPath + "/HotFix.dll"; #else string dllPath = "file:///" + Application.streamingAssetsPath + "/HotFix.dll"; #endif WWW www = new WWW(dllPath); while (!www.isDone) { yield return(null); } if (!string.IsNullOrEmpty(www.error)) { UnityEngine.Debug.LogError(www.error); } byte[] dll = www.bytes; www.Dispose(); #if !LOAD_PDB using (System.IO.MemoryStream fs = new MemoryStream(dll)) { appdomain.LoadAssembly(fs, null, new Mono.Cecil.Pdb.PdbReaderProvider()); } #else //PDB文件是调试数据库,如需要在日志中显示报错的行号,则必须提供PDB文件,不过由于会额外耗用内存,正式发布时请将PDB去掉,下面LoadAssembly的时候pdb传null即可 #if UNITY_ANDROID string pdbPath = Application.streamingAssetsPath + "/HotFix.pdb"; #else string pdbPath = "file:///" + Application.streamingAssetsPath + "/HotFix.pdb"; #endif www = new WWW(pdbPath); while (!www.isDone) { yield return(null); } if (!string.IsNullOrEmpty(www.error)) { UnityEngine.Debug.LogError(www.error); } byte[] pdb = www.bytes; www.Dispose(); using (System.IO.MemoryStream fs = new MemoryStream(dll)) { using (System.IO.MemoryStream p = new MemoryStream(pdb)) { appdomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider()); } } #endif InitializeILRuntime(); OnHotFixLoaded(); }
public static void LoadHotfix(string dllPath, bool isRegisterBindings = true) { // IsRunning = true; string pdbPath = dllPath.Replace(".dll", ".pdb"); BDebug.Log("DLL加载路径:" + dllPath, "red"); // AppDomain = new AppDomain(); if (File.Exists(pdbPath)) { //这里的流不能释放,头铁的老哥别试了 fsDll = new FileStream(dllPath, FileMode.Open, FileAccess.Read); fsPdb = new FileStream(pdbPath, FileMode.Open, FileAccess.Read); AppDomain.LoadAssembly(fsDll, fsPdb, new PdbReaderProvider()); } else { //这里的流不能释放,头铁的老哥别试了 fsDll = new FileStream(dllPath, FileMode.Open, FileAccess.Read); AppDomain.LoadAssembly(fsDll); } //绑定的初始化 //ada绑定 AdapterRegister.RegisterCrossBindingAdaptor(AppDomain); //delegate绑定 ILRuntimeDelegateHelper.Register(AppDomain); //值类型绑定 AppDomain.RegisterValueTypeBinder(typeof(Vector2), new Vector2Binder()); AppDomain.RegisterValueTypeBinder(typeof(Vector3), new Vector3Binder()); AppDomain.RegisterValueTypeBinder(typeof(Vector4), new Vector4Binder()); AppDomain.RegisterValueTypeBinder(typeof(Quaternion), new QuaternionBinder()); //是否注册各种binding if (isRegisterBindings) { ILRuntime.Runtime.Generated.CLRBindings.Initialize(AppDomain); ILRuntime.Runtime.Generated.CLRManualBindings.Initialize(AppDomain); // ILRuntime.Runtime.Generated.PreCLRBuilding.Initialize(AppDomain); } JsonMapper.RegisterILRuntimeCLRRedirection(AppDomain); if (Application.isEditor) { AppDomain.DebugService.StartDebugService(56000); Debug.Log("热更调试器 准备待命~"); } // AppDomain.Invoke("HotfixCheck", "Log", null, null); }
IEnumerator LoadHotFixAssembly() { appDomain = new ILRuntime.Runtime.Enviorment.AppDomain(); #if UNITY_ANDROID WWW www = new WWW(Application.streamingAssetsPath + "/GameModelTest.dll"); #else WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/GameModelTest.dll"); #endif while (!www.isDone) { yield return(null); } if (!string.IsNullOrEmpty(www.error)) { Debug.LogError(www.error); } dllBytes = www.bytes; www.Dispose(); if (usePdb) { #if UNITY_ANDROID www = new WWW(Application.streamingAssetsPath + "/GameModelTest.pdb"); #else www = new WWW("file:///" + Application.streamingAssetsPath + "/GameModelTest.pdb"); #endif while (!www.isDone) { yield return(null); } if (!string.IsNullOrEmpty(www.error)) { Debug.LogError(www.error); } pdbBytes = www.bytes; } using (MemoryStream fs = new MemoryStream(dllBytes)) { if (pdbBytes != null) { using (MemoryStream p = new MemoryStream(pdbBytes)) { appDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider()); } } else { appDomain.LoadAssembly(fs, null, new Mono.Cecil.Pdb.PdbReaderProvider()); } } InitializeILRuntime(); }
public static void LoadHotfix(string root) { if (AppDomain != null) { //AppDomain.FreeILIntepreter(AppDomain.); } // IsRunning = true; string dllPath = root + "/" + Utils.GetPlatformPath(Application.platform) + "/hotfix/hotfix.dll"; string pdbPath = root + "/" + Utils.GetPlatformPath(Application.platform) + "/hotfix/hotfix.pdb"; BDebug.Log("DLL加载路径:" + dllPath, "red"); // AppDomain = new AppDomain(); if (File.Exists(pdbPath)) { var dllfs = File.ReadAllBytes(dllPath); var pdbfs = File.ReadAllBytes(pdbPath); using (MemoryStream dll = new MemoryStream(dllfs)) { using (MemoryStream pdb = new MemoryStream(pdbfs)) { AppDomain.LoadAssembly(dll, pdb, new PdbReaderProvider()); } } } else { UnityWebRequest request; using (System.IO.FileStream fs = new System.IO.FileStream(dllPath, FileMode.Open, FileAccess.Read)) { AppDomain.LoadAssembly(fs); } } //绑定的初始化 AdapterRegister.RegisterCrossBindingAdaptor(AppDomain); ILRuntime.Runtime.Generated.CLRBindings.Initialize(AppDomain); ILRuntime.Runtime.Generated.CLRManualBindings.Initialize(AppDomain); // ILRuntime.Runtime.Generated.PreCLRBuilding.Initialize(AppDomain); // ILRuntimeDelegateHelper.Register(AppDomain); JsonMapper.RegisterILRuntimeCLRRedirection(AppDomain); if (Application.isEditor) { AppDomain.DebugService.StartDebugService(56000); Debug.Log("热更调试器 准备待命~"); } // AppDomain.Invoke("HotfixCheck", "Log", null, null); }
public void LoadHotfixAssembly() { TextAsset dllAsset = AssetManager.Instance.LoadAsset <TextAsset>("Unity.Hotfix.dll", ".bytes", false, true, AssetType.DLL); MemoryStream dll = new MemoryStream(RijndaelUtil.RijndaelDecrypt("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", dllAsset.bytes)); #if Release appDomain.LoadAssembly(dll, null, new PdbReaderProvider()); #else TextAsset pdbAsset = AssetManager.Instance.LoadAsset <TextAsset>("Unity.Hotfix.pdb", ".bytes", false, true, AssetType.DLL); MemoryStream pdb = new MemoryStream(pdbAsset.bytes); appDomain.LoadAssembly(dll, pdb, new PdbReaderProvider()); #endif InitializeILRuntime(); OnHotfixLoaded(); }
public static void LoadHotfix(string root, bool isRegisterBindings = true) { // IsRunning = true; string dllPath = root + "/" + Utils.GetPlatformPath(Application.platform) + "/hotfix/hotfix.dll"; string pdbPath = root + "/" + Utils.GetPlatformPath(Application.platform) + "/hotfix/hotfix.pdb"; BDebug.Log("DLL加载路径:" + dllPath, "red"); // AppDomain = new AppDomain(); if (File.Exists(pdbPath)) { //这里的流不能释放,头铁的老哥别试了 fsDll = new FileStream(dllPath, FileMode.Open, FileAccess.Read); fsPdb = new FileStream(pdbPath, FileMode.Open, FileAccess.Read); AppDomain.LoadAssembly(fsDll, fsPdb, new PdbReaderProvider()); } else { //这里的流不能释放,头铁的老哥别试了 fsDll = new FileStream(dllPath, FileMode.Open, FileAccess.Read); AppDomain.LoadAssembly(fsDll); } //绑定的初始化 AdapterRegister.RegisterCrossBindingAdaptor(AppDomain); ILRuntimeDelegateHelper.Register(AppDomain); //是否注册各种binding if (isRegisterBindings) { ILRuntime.Runtime.Generated.CLRBindings.Initialize(AppDomain); ILRuntime.Runtime.Generated.CLRManualBindings.Initialize(AppDomain); // ILRuntime.Runtime.Generated.PreCLRBuilding.Initialize(AppDomain); } JsonMapper.RegisterILRuntimeCLRRedirection(AppDomain); if (Application.isEditor) { AppDomain.DebugService.StartDebugService(56000); Debug.Log("热更调试器 准备待命~"); } // AppDomain.Invoke("HotfixCheck", "Log", null, null); }
public static void GenerateCLRBindingByAnalysis(bool showTips = true) { if (showTips && !EditorUtility.DisplayDialog("注意", "确定要生成吗", "是的", "点错")) { return; } //用新的分析热更dll调用引用来生成绑定代码 ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain(); var ilrConfig = ConfigBase.Load <FrameworkRuntimeConfig>().ILRConfig; using (System.IO.FileStream fs = new System.IO.FileStream($"Assets/StreamingAssets/{ilrConfig.DllName}.dll", System.IO.FileMode.Open, System.IO.FileAccess.Read)) { domain.LoadAssembly(fs); //Crossbind Adapter is needed to generate the correct binding code InitILRuntime(domain); var path = "Assets/_Scripts/ILRuntime/Generated"; if (Directory.Exists(path)) { Directory.Delete(path, true); } ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, "Assets/_Scripts/ILRuntime/Generated"); } AssetDatabase.Refresh(); }
public IlRuntime(byte[] dat, RectTransform uiRoot) { _app = new ILRuntime.Runtime.Enviorment.AppDomain(); RegDelegate(); using (MemoryStream m = new MemoryStream(dat)) { _app.LoadAssembly(m); } mainScript = _app.GetType("Main") as ILType; var start = mainScript.GetMethod("Start"); if (start != null) { try { _app.Invoke(mainScript.FullName, start.Name, mainScript, uiRoot); } catch (Exception ex) { Debug.Log(ex.StackTrace); } } Update = mainScript.GetMethod("Update"); Resize = mainScript.GetMethod("Resize"); Cmd = mainScript.GetMethod("Cmd"); }
static void GenerateCLRBindingByAnalysis() { //CLR绑定 从热更DLL中调用Unity主工程或者Unity的接口 //用新的分析热更dll调用引用来生成绑定代码 var domain = new ILRuntime.Runtime.Enviorment.AppDomain(); //string path = Application.dataPath + "/ILRuntime/DllData/"; foreach (var full_path in Directory.GetFiles(LC_RuntimeManager.DLL_PATH, "*_dlc.txt", SearchOption.TopDirectoryOnly)) { var relative_path = full_path.Substring(full_path.IndexOf("Assets")); using (var fs = new FileStream(relative_path, FileMode.Open, FileAccess.Read)) { domain.LoadAssembly(fs); } } //Crossbind Adaptor is needed to generate the correct binding code LC_AdaptorHelper.Init(domain); var generated_path = Application.dataPath + "/ILRuntime/Generated/"; var relative = generated_path.Substring(generated_path.IndexOf("Assets")); ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, relative); Debug.LogWarning("== ILRuntime/Generate CLR Binding Code by Analysis Complete!!! =="); }
public override void InitApp(byte[] rDLLBytes, byte[] rPDBBytes) { mApp = new AppDomain(); MemoryStream rDLLMS = new MemoryStream(rDLLBytes); MemoryStream rPDBMS = new MemoryStream(rPDBBytes); mApp.LoadAssembly(rDLLMS, rPDBMS, new Mono.Cecil.Pdb.PdbReaderProvider()); rDLLMS.Close(); rPDBMS.Close(); rDLLMS.Dispose(); rPDBMS.Dispose(); // 注册Value Type Binder this.RegisterValueTypeBinder(); // 注册Adaptor this.RegisterCrossBindingAdaptor(); // 注册重定向方法 this.RegisterCLRMethodRedirection(); // 注册委托 this.RegisterDelegates(); }
private void OnLoadHotfixDLLSuccess(string assetName, object asset, float duration, object userData) { if ((int)userData == 1) { Log.Info("Hotfix.dll加载成功"); m_DLLLoaded = true; m_DLL = (asset as TextAsset).bytes; } else { Log.Info("Hotfix.pdb加载成功"); m_PDBLoaded = true; m_PDB = (asset as TextAsset).bytes; } if (m_DLLLoaded && m_PDBLoaded) { HotfixLoaded = true; m_DLLLoaded = false; m_PDBLoaded = false; m_DLLStream = new MemoryStream(m_DLL); m_PDBStream = new MemoryStream(m_PDB); AppDomain.LoadAssembly(m_DLLStream, m_PDBStream, new Mono.Cecil.Pdb.PdbReaderProvider()); } }
private void ILHotfixDllLoadDone(TextAsset asset) { _ILHotfixDll = asset; if (_ILHotfixDll != null) { _isStartUp = true; _appDomain = new AppDomain(); MemoryStream dllStream = new MemoryStream(_ILHotfixDll.bytes); _appDomain.LoadAssembly(dllStream, null, new PdbReaderProvider()); _ILHotFixTypes.Clear(); foreach (var item in _appDomain.LoadedTypes.Values) { _ILHotFixTypes.Add(item.ReflectionType); } ILHotfixInitialize(); _ILHotfixEnvironment = _appDomain.Instantiate("ILHotfixEnvironment"); if (_ILHotfixEnvironment == null) { _isStartUp = false; Log.Error("热更新初始化失败:热更新库中不存在热更新环境 ILHotfixEnvironment!"); } dllStream.Dispose(); } else { _isStartUp = false; Log.Error("热更新初始化失败:未正确加载热更新库文件!"); } }
//[MenuItem("ILRuntime/Generate CLR Binding Code by Analysis")] public static void GenerateCLRBindingByAnalysis() { if (Directory.Exists(GeneratedPath)) Directory.Delete(GeneratedPath, true); GenerateCLRBinding(); //用新的分析热更dll调用引用来生成绑定代码 ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain(); using (System.IO.FileStream fs = new System.IO.FileStream(Utility.Path.GetCombinePath(RuntimeAssetUtility.HotfixPath, RuntimeAssetUtility.HotfixDllName), System.IO.FileMode.Open, System.IO.FileAccess.Read)) { domain.LoadAssembly(fs); } //Crossbind Adapter is needed to generate the correct binding code ILRuntimeUtility.InitILRuntime(domain); try { ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, GeneratedPath); } catch (Exception) { } AssetDatabase.Refresh(); }
private void InitPdb(byte[] pdb) { this.dllStream = new MemoryStream(dll); this.pdbStream = new MemoryStream(pdb); appdomain.LoadAssembly(this.dllStream, this.pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider()); this.OnScriptLoadedInitialized(); }
private CodeLoader() { Dictionary <string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d"); byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes; byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes; #if ILRuntime ILRuntime.Runtime.Enviorment.AppDomain appDomain = new ILRuntime.Runtime.Enviorment.AppDomain(); using (System.IO.MemoryStream assStream = new System.IO.MemoryStream(assBytes)) using (System.IO.MemoryStream pdbStream = new System.IO.MemoryStream(pdbBytes)) { appDomain.LoadAssembly(assStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider()); } ILHelper.InitILRuntime(appDomain); this.hotfixTypes = Type.EmptyTypes; this.hotfixTypes = appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray(); this.start = new ILStaticMethod(appDomain, "ET.Entry", "Start", 0); #else System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(assBytes, pdbBytes); hotfixTypes = assembly.GetTypes(); this.start = new MonoStaticMethod(assembly, "ET.Entry", "Start"); #endif }
public override void InitApp(byte[] rDLLBytes, byte[] rPDBBytes) { mApp = new AppDomain(); MemoryStream rDLLMS = (rDLLBytes != null) ? new MemoryStream(rDLLBytes) : null; MemoryStream rPDBMS = (rPDBBytes != null) ? new MemoryStream(rPDBBytes) : null; mApp.LoadAssembly(rDLLMS, rPDBMS, new Mono.Cecil.Pdb.PdbReaderProvider()); rDLLMS?.Close(); rPDBMS?.Close(); rDLLMS?.Dispose(); rPDBMS?.Dispose(); // 重定向Json解析器的传入的类型 ITypeRedirect.GetRedirectTypeHandler = (rType) => { return((rType is ILRuntime.Reflection.ILRuntimeWrapperType) ? ((ILRuntime.Reflection.ILRuntimeWrapperType)rType).CLRType.TypeForCLR : rType); }; // 注册Value Type Binder this.RegisterValueTypeBinder(); // 注册Adaptor this.RegisterCrossBindingAdaptor(); // 注册重定向方法 this.RegisterCLRMethodRedirection(); // 注册委托 this.RegisterDelegates(); }
public void LoadHotfixAssembly() { Game.Scene.GetComponent <ResourcesComponent>().LoadBundle($"code.unity3d"); GameObject code = (GameObject)Game.Scene.GetComponent <ResourcesComponent>().GetAsset("code.unity3d", "Code"); byte[] assBytes = code.Get <TextAsset>("Hotfix.dll").bytes; byte[] pdbBytes = code.Get <TextAsset>("Hotfix.pdb").bytes; #if ILRuntime Log.Debug($"当前使用的是ILRuntime模式"); appDomain = new ILRuntime.Runtime.Enviorment.AppDomain(); dllStream = new MemoryStream(assBytes); pdbStream = new MemoryStream(pdbBytes); appDomain.LoadAssembly(this.dllStream, this.pdbStream, new Mono.Cecil.Pdb.PdbReaderProvider()); this.start = new ILStaticMethod(this.appDomain, "Hotfix.Init", "Start", 0); this.hotfixTypes = this.appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToList(); #else Log.Debug($"当前使用的是Mono模式"); this.assembly = Assembly.Load(assBytes, pdbBytes); Type hotfixInit = this.assembly.GetType("Hotfix.Init"); this.start = new MonoStaticMethod(assembly, hotfixInit.FullName, "Start"); this.hotfixTypes = this.assembly.GetTypes().ToList(); #endif Game.Scene.GetComponent <ResourcesComponent>().UnLoadBundle($"code.unity3d"); }
private void ILHotfixDllLoadDone(TextAsset asset) { _ILHotfixDll = asset; if (_ILHotfixDll != null) { _isStartUp = true; _appDomain = new AppDomain(); MemoryStream dllStream = new MemoryStream(_ILHotfixDll.bytes); _appDomain.LoadAssembly(dllStream, null, new PdbReaderProvider()); ILHotfixInitialize(); _ILHotfixEnvironment = _appDomain.Instantiate("ILHotfixEnvironment"); if (_ILHotfixEnvironment == null) { _isStartUp = false; GlobalTools.LogError("热更新初始化失败:热更新库中不存在热更新环境 ILHotfixEnvironment!"); } dllStream.Dispose(); } else { _isStartUp = false; GlobalTools.LogError("热更新初始化失败:未正确加载热更新库文件!"); } }
/// <summary> /// load hotfix code && initialize adapter and binding /// </summary> private void InitializeAppDomain() { if (string.IsNullOrWhiteSpace(_hotfix_dll_path)) { UnityEngine.Debug.LogError("hotfix dll path == null"); return; } try { using (FileStream fs = new FileStream(_hotfix_dll_path, FileMode.Open, FileAccess.Read)) { var path = Path.GetDirectoryName(_hotfix_dll_path); var mdbPath = Path.Combine(path, "Hotfix.dll.mdb.bytes"); if (!File.Exists(mdbPath)) { UnityEngine.Debug.LogError("hotfix.mdb.bytes 不存在!"); } //mdb 调试有问题,故此处省去 App.LoadAssembly(fs, null, null); _isLoadAssembly = true; //委托 //ILRuntimeHelper.Init(App); //绑定 ILRuntime.Runtime.Generated.CLRBindings.Initialize(App); } } catch (Exception ex) { UnityEngine.Debug.LogError("read hot fix dll error! \n" + ex.Message + "\n source: " + ex.Source + "\n Trace: \n" + ex.StackTrace); return; } }
public void Load(string assemblyPath, bool useRegister) { fs = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read); { var path = Path.GetDirectoryName(assemblyPath); var name = Path.GetFileNameWithoutExtension(assemblyPath); var pdbPath = Path.Combine(path, name) + ".pdb"; if (!File.Exists(pdbPath)) { name = Path.GetFileName(assemblyPath); pdbPath = Path.Combine(path, name) + ".mdb"; } _app = new ILRuntime.Runtime.Enviorment.AppDomain(useRegister ? ILRuntime.Runtime.ILRuntimeJITFlags.JITImmediately : ILRuntime.Runtime.ILRuntimeJITFlags.None); _app.DebugService.StartDebugService(56000); fs2 = new System.IO.FileStream(pdbPath, FileMode.Open); { ILRuntime.Mono.Cecil.Cil.ISymbolReaderProvider symbolReaderProvider = null; if (pdbPath.EndsWith(".pdb")) { symbolReaderProvider = new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider(); }/* else if (pdbPath.EndsWith (".mdb")) { * symbolReaderProvider = new Mono.Cecil.Mdb.MdbReaderProvider (); * }*/ _app.LoadAssembly(fs, fs2, symbolReaderProvider); } ILRuntimeHelper.Init(_app); ILRuntime.Runtime.Generated.CLRBindings.Initialize(_app); _app.InitializeBindings(true); LoadTest(); } lastSession = this; }
private static void GenerateCLRBindingByAnalysis() { //用新的分析热更dll调用引用来生成绑定代码 ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain(); using (System.IO.FileStream fs = new System.IO.FileStream("Assets/UpdatableLogic/HotFix/HotFix.dll.bytes", System.IO.FileMode.Open, System.IO.FileAccess.Read)) { domain.LoadAssembly(fs); //Crossbind Adapter is needed to generate the correct binding code domain.RegisterCrossBindingAdaptor(new Dictionary_2_Object_ObjectAdapter()); domain.RegisterCrossBindingAdaptor(new ExceptionAdapter()); domain.RegisterCrossBindingAdaptor(new HashSet_1_ObjectAdapter()); domain.RegisterCrossBindingAdaptor(new IEnumerable_1_ILTypeInstanceAdapter()); domain.RegisterCrossBindingAdaptor(new IEnumerable_1_ObjectAdapter()); domain.RegisterCrossBindingAdaptor(new IEnumerator_1_ILTypeInstanceAdapter()); domain.RegisterCrossBindingAdaptor(new IEnumerator_1_ObjectAdapter()); domain.RegisterCrossBindingAdaptor(new LinkedList_1_ObjectAdapter()); domain.RegisterCrossBindingAdaptor(new List_1_ObjectAdapter()); domain.RegisterCrossBindingAdaptor(new Queue_1_ObjectAdapter()); domain.RegisterCrossBindingAdaptor(new Stack_1_ObjectAdapter()); ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode( domain, "Assets/ILRuntime/Generated"); } AssetDatabase.Refresh(); }
public bool LoadAssembly(RName dll, RName pdb) { var dataDll = LoadAllFile(dll); if (dataDll == null) { return(false); } var dataPdb = LoadAllFile(pdb); if (dataPdb == null) { return(false); } using (System.IO.MemoryStream fs = new System.IO.MemoryStream(dataDll)) { using (System.IO.MemoryStream p = new System.IO.MemoryStream(dataPdb)) { mDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider()); } } return(true); }
/// <summary> /// 加载热更的动态库文件 /// </summary> private void LoadHotfixAssembly() { // TODO:正式发布时去掉PDB,下面LoadAssembly的PDB参数设置为null TextAsset dllAsset = Resources.Load <TextAsset>($"Assembly/{ILRDefine.StrMyHotfixDLLFileName}"); TextAsset pdbAsset = Resources.Load <TextAsset>($"Assembly/{ILRDefine.StrMyHotfixPDBFileName}"); if (EnableILRuntime) { LogSystem.Log(ELogType.Log, "ILRuntime模式"); var symbolReader = new Mono.Cecil.Pdb.PdbReaderProvider(); if (dllAsset != null) { _dllStream = new MemoryStream(dllAsset.bytes); } if (pdbAsset != null) { _pdbStream = new MemoryStream(pdbAsset.bytes); } ILRDomain = new ILRuntime.Runtime.Enviorment.AppDomain(); ILRDomain.LoadAssembly(_dllStream, _pdbStream, symbolReader); } else { LogSystem.Log(ELogType.Log, "Mono模式"); _monoAssembly = Assembly.Load(dllAsset.bytes, pdbAsset.bytes); } }
public override void InitApp(byte[] rDLLBytes, byte[] rPDBBytes) { mApp = new AppDomain(); mDLLMS = (rDLLBytes != null) ? new MemoryStream(rDLLBytes) : null; mPDBMS = (rPDBBytes != null) ? new MemoryStream(rPDBBytes) : null; mApp.LoadAssembly(mDLLMS, mPDBMS, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider()); // 启用调试 if (this.mApp.DebugService != null) { this.mApp.DebugService.StartDebugService(56000); } // 重定向Json解析器的传入的类型 ITypeRedirect.GetRedirectTypeHandler = (rType) => { return((rType is ILRuntime.Reflection.ILRuntimeWrapperType) ? ((ILRuntime.Reflection.ILRuntimeWrapperType)rType).CLRType.TypeForCLR : rType); }; // 注册Value Type Binder this.RegisterValueTypeBinder(); // 注册Adaptor this.RegisterCrossBindingAdaptor(); // 注册重定向方法 this.RegisterCLRMethodRedirection(); // 注册委托 this.RegisterDelegates(); Debug.Log("ILRT Initialize complete."); }
static void LoadDll(ILRuntime.Runtime.Enviorment.AppDomain domain, string dllName, out MemoryStream dllStream) { TextAsset dllAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(dllName); dllStream = new MemoryStream(EncryptionUtility.AESDecrypt(dllAsset.bytes)); domain.LoadAssembly(dllStream); }