1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| public class CodeLoader: IDisposable { public static CodeLoader Instance = new CodeLoader();
public Action Update; public Action LateUpdate; public Action OnApplicationQuit;
private Assembly assembly;
private ILRuntime.Runtime.Enviorment.AppDomain appDomain; private Type[] allTypes; public CodeMode CodeMode { get; set; }
private CodeLoader() { }
public void Dispose() { this.appDomain?.Dispose(); } public void Start() { switch (this.CodeMode) { case CodeMode.Mono: { Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d"); byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes; byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes; assembly = Assembly.Load(assBytes, pdbBytes); this.allTypes = assembly.GetTypes(); IStaticMethod start = new MonoStaticMethod(assembly, "ET.Entry", "Start"); start.Run(); break; } case CodeMode.ILRuntime: { Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d"); byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes; byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes; appDomain = new ILRuntime.Runtime.Enviorment.AppDomain(); MemoryStream assStream = new MemoryStream(assBytes); MemoryStream pdbStream = new MemoryStream(pdbBytes); appDomain.LoadAssembly(assStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
ILHelper.InitILRuntime(appDomain);
this.allTypes = appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray(); IStaticMethod start = new ILStaticMethod(appDomain, "ET.Entry", "Start", 0); start.Run(); break; } case CodeMode.Reload: { byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Data.dll")); byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Data.pdb")); assembly = Assembly.Load(assBytes, pdbBytes); this.LoadLogic(); IStaticMethod start = new MonoStaticMethod(assembly, "ET.Entry", "Start"); start.Run(); break; } } }
public void LoadLogic() { if (this.CodeMode != CodeMode.Reload) { throw new Exception("CodeMode != Reload!"); } string[] logicFiles = Directory.GetFiles(Define.BuildOutputDir, "Logic_*.dll"); if (logicFiles.Length != 1) { throw new Exception("Logic dll count != 1"); }
string logicName = Path.GetFileNameWithoutExtension(logicFiles[0]); byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.dll")); byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.pdb"));
Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes); List<Type> listType = new List<Type>(); listType.AddRange(this.assembly.GetTypes()); listType.AddRange(hotfixAssembly.GetTypes()); this.allTypes = listType.ToArray(); }
public Type[] GetTypes() { return this.allTypes; } }
|