CodeLoader.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace ET
  7. {
  8. public class CodeLoader: Singleton<CodeLoader>
  9. {
  10. private Assembly model;
  11. public void Start()
  12. {
  13. if (Define.EnableCodes)
  14. {
  15. GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  16. if (globalConfig.CodeMode != CodeMode.ClientServer)
  17. {
  18. throw new Exception("ENABLE_CODES mode must use ClientServer code mode!");
  19. }
  20. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  21. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(assemblies);
  22. EventSystem.Instance.Add(types);
  23. foreach (Assembly ass in assemblies)
  24. {
  25. string name = ass.GetName().Name;
  26. if (name == "Unity.Model.Codes")
  27. {
  28. this.model = ass;
  29. }
  30. }
  31. }
  32. else
  33. {
  34. byte[] assBytes;
  35. byte[] pdbBytes;
  36. if (!Define.IsEditor)
  37. {
  38. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  39. assBytes = ((TextAsset)dictionary["Model.dll"]).bytes;
  40. pdbBytes = ((TextAsset)dictionary["Model.pdb"]).bytes;
  41. if (Define.EnableIL2CPP)
  42. {
  43. HybridCLRHelper.Load();
  44. }
  45. }
  46. else
  47. {
  48. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  49. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  50. }
  51. this.model = Assembly.Load(assBytes, pdbBytes);
  52. this.LoadHotfix();
  53. }
  54. IStaticMethod start = new StaticMethod(this.model, "ET.Entry", "Start");
  55. start.Run();
  56. }
  57. // 热重载调用该方法
  58. public void LoadHotfix()
  59. {
  60. byte[] assBytes;
  61. byte[] pdbBytes;
  62. if (!Define.IsEditor)
  63. {
  64. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  65. assBytes = ((TextAsset)dictionary["Hotfix.dll"]).bytes;
  66. pdbBytes = ((TextAsset)dictionary["Hotfix.pdb"]).bytes;
  67. }
  68. else
  69. {
  70. // 傻屌Unity在这里搞了个傻逼优化,认为同一个路径的dll,返回的程序集就一样。所以这里每次编译都要随机名字
  71. string[] logicFiles = Directory.GetFiles(Define.BuildOutputDir, "Hotfix_*.dll");
  72. if (logicFiles.Length != 1)
  73. {
  74. throw new Exception("Logic dll count != 1");
  75. }
  76. string logicName = Path.GetFileNameWithoutExtension(logicFiles[0]);
  77. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.dll"));
  78. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.pdb"));
  79. }
  80. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  81. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly, typeof(Init).Assembly, this.model, hotfixAssembly);
  82. EventSystem.Instance.Add(types);
  83. }
  84. }
  85. }