CodeLoader.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #define ILRuntime
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using UnityEngine;
  7. using System.Linq;
  8. using AppDomain = ILRuntime.Runtime.Enviorment.AppDomain;
  9. namespace ET
  10. {
  11. public class CodeLoader
  12. {
  13. public static CodeLoader Instance = new CodeLoader();
  14. public Action Update;
  15. public Action LateUpdate;
  16. public Action OnApplicationQuit;
  17. private Assembly assembly;
  18. private Type[] allTypes;
  19. private CodeLoader()
  20. {
  21. }
  22. public void Start()
  23. {
  24. switch (Define.CodeMode)
  25. {
  26. case Define.CodeMode_Mono:
  27. {
  28. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  29. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  30. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  31. assembly = Assembly.Load(assBytes, pdbBytes);
  32. this.allTypes = assembly.GetTypes();
  33. IStaticMethod start = new MonoStaticMethod(assembly, "ET.Entry", "Start");
  34. start.Run();
  35. break;
  36. }
  37. case Define.CodeMode_ILRuntime:
  38. {
  39. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  40. byte[] assBytes = ((TextAsset)dictionary["Code.dll"]).bytes;
  41. byte[] pdbBytes = ((TextAsset)dictionary["Code.pdb"]).bytes;
  42. AppDomain appDomain = new AppDomain();
  43. MemoryStream assStream = new MemoryStream(assBytes);
  44. MemoryStream pdbStream = new MemoryStream(pdbBytes);
  45. appDomain.LoadAssembly(assStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
  46. ILHelper.InitILRuntime(appDomain);
  47. this.allTypes = appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray();
  48. IStaticMethod start = new ILStaticMethod(appDomain, "ET.Entry", "Start", 0);
  49. start.Run();
  50. break;
  51. }
  52. case Define.CodeMode_Reload:
  53. {
  54. byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Data.dll"));
  55. byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Data.pdb"));
  56. assembly = Assembly.Load(assBytes, pdbBytes);
  57. LoadHotfix();
  58. IStaticMethod start = new MonoStaticMethod(assembly, "ET.Entry", "Start");
  59. start.Run();
  60. break;
  61. }
  62. }
  63. }
  64. // 热重载调用下面三个方法
  65. // CodeLoader.Instance.LoadHotfix();
  66. // Game.EventSystem.Add(CodeLoader.Instance.GetTypes());
  67. // Game.EventSystem.Load();
  68. public void LoadHotfix()
  69. {
  70. byte[] assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Logic.dll"));
  71. byte[] pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Logic.pdb"));
  72. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  73. List<Type> listType = new List<Type>();
  74. listType.AddRange(this.assembly.GetTypes());
  75. listType.AddRange(hotfixAssembly.GetTypes());
  76. this.allTypes = listType.ToArray();
  77. }
  78. public Type[] GetTypes()
  79. {
  80. return this.allTypes;
  81. }
  82. }
  83. }