CodeLoader.cs 2.8 KB

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