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. IStaticMethod start = new StaticMethod(this.model, "ET.Entry", "Start");
  32. start.Run();
  33. }
  34. else
  35. {
  36. byte[] assBytes;
  37. byte[] pdbBytes;
  38. if (!Define.IsEditor)
  39. {
  40. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  41. assBytes = ((TextAsset)dictionary["Model.dll"]).bytes;
  42. pdbBytes = ((TextAsset)dictionary["Model.pdb"]).bytes;
  43. HybridCLRHelper.Load();
  44. }
  45. else
  46. {
  47. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  48. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  49. }
  50. this.model = Assembly.Load(assBytes, pdbBytes);
  51. this.LoadHotfix();
  52. IStaticMethod start = new StaticMethod(this.model, "ET.Entry", "Start");
  53. start.Run();
  54. }
  55. }
  56. // 热重载调用该方法
  57. public void LoadHotfix()
  58. {
  59. byte[] assBytes;
  60. byte[] pdbBytes;
  61. if (!Define.IsEditor)
  62. {
  63. Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  64. assBytes = ((TextAsset)dictionary["Hotfix.dll"]).bytes;
  65. pdbBytes = ((TextAsset)dictionary["Hotfix.pdb"]).bytes;
  66. }
  67. else
  68. {
  69. // 傻屌Unity在这里搞了个傻逼优化,认为同一个路径的dll,返回的程序集就一样。所以这里每次编译都要随机名字
  70. string[] logicFiles = Directory.GetFiles(Define.BuildOutputDir, "Hotfix_*.dll");
  71. if (logicFiles.Length != 1)
  72. {
  73. throw new Exception("Logic dll count != 1");
  74. }
  75. string logicName = Path.GetFileNameWithoutExtension(logicFiles[0]);
  76. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.dll"));
  77. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, $"{logicName}.pdb"));
  78. }
  79. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  80. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly, typeof(Init).Assembly, this.model, hotfixAssembly);
  81. EventSystem.Instance.Add(types);
  82. }
  83. }
  84. }