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 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模式"); this.appDomain = new ILRuntime.Runtime.Enviorment.AppDomain(); this.dllStream = new MemoryStream(assBytes); this.pdbStream = new MemoryStream(pdbBytes); this.appDomain.LoadAssembly(this.dllStream, this.pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider()); this.start = new ILStaticMethod(this.appDomain, "ETHotfix.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("ETHotfix.Init"); this.start = new MonoStaticMethod(hotfixInit, "Start"); this.hotfixTypes = this.assembly.GetTypes().ToList(); #endif Game.Scene.GetComponent <ResourcesComponent>().UnloadBundle($"code.unity3d"); }
/// <summary> /// 这里开始正式进入游戏逻辑 /// </summary> public static void GoToHotfix(byte[] dllByte, byte[] pdbByte) { if (GlobalDefine.ILRuntimeMode) { _appDomain = new ILRuntime.Runtime.Enviorment.AppDomain(); s_hotfixDllStream = new MemoryStream(dllByte); s_hotfixPdbStream = new MemoryStream(pdbByte); _appDomain.LoadAssembly(s_hotfixDllStream, s_hotfixPdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider()); ILHelper.InitILRuntime(_appDomain); _entryMethod = new ILStaticMethod(_appDomain, "ET.InitEntry", "RegFunction", 0); } else { _assembly = Assembly.Load(dllByte, pdbByte); _entryMethod = new MonoStaticMethod(_assembly, "ET.InitEntry", "RegFunction"); } _entryMethod.Run(); }