CodeLoader.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using UnityEngine;
  6. #pragma warning disable CS0162
  7. namespace ET
  8. {
  9. public class CodeLoader: Singleton<CodeLoader>, ISingletonAwake
  10. {
  11. private Assembly assembly;
  12. public void Awake()
  13. {
  14. }
  15. public void Start()
  16. {
  17. if (!Define.EnableDll)
  18. {
  19. GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  20. if (globalConfig.CodeMode != CodeMode.ClientServer)
  21. {
  22. throw new Exception("!ENABLE_CODES mode must use ClientServer code mode!");
  23. }
  24. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  25. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(assemblies);
  26. World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types);
  27. foreach (Assembly ass in assemblies)
  28. {
  29. string name = ass.GetName().Name;
  30. if (name == "Unity.Model")
  31. {
  32. this.assembly = ass;
  33. }
  34. }
  35. }
  36. else
  37. {
  38. byte[] assBytes;
  39. byte[] pdbBytes;
  40. if (!Define.IsEditor)
  41. {
  42. //Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  43. //assBytes = ((TextAsset)dictionary["Model.dll"]).bytes;
  44. //pdbBytes = ((TextAsset)dictionary["Model.pdb"]).bytes;
  45. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Model.dll,真正打包要还原使用上面注释的代码
  46. assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.dll"));
  47. pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.pdb"));
  48. if (Define.EnableIL2CPP)
  49. {
  50. HybridCLRHelper.Load();
  51. }
  52. }
  53. else
  54. {
  55. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  56. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  57. }
  58. this.assembly = Assembly.Load(assBytes, pdbBytes);
  59. }
  60. {
  61. Assembly hotfixAssembly = this.LoadHotfix();
  62. Dictionary<string, Type> types =
  63. AssemblyHelper.GetAssemblyTypes(typeof (World).Assembly, typeof (Init).Assembly, this.assembly, hotfixAssembly);
  64. World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types);
  65. IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
  66. start.Run();
  67. }
  68. }
  69. private Assembly LoadHotfix()
  70. {
  71. byte[] assBytes;
  72. byte[] pdbBytes;
  73. if (!Define.IsEditor)
  74. {
  75. //Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  76. //assBytes = ((TextAsset)dictionary["Hotfix.dll"]).bytes;
  77. //pdbBytes = ((TextAsset)dictionary["Hotfix.pdb"]).bytes;
  78. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Hotfix.dll,真正打包要还原使用上面注释的代码
  79. assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.dll"));
  80. pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.pdb"));
  81. }
  82. else
  83. {
  84. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.dll"));
  85. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.pdb"));
  86. }
  87. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  88. return hotfixAssembly;
  89. }
  90. public void Reload()
  91. {
  92. Assembly hotfixAssembly = this.LoadHotfix();
  93. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly);
  94. World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types, true);
  95. World.Instance.Load();
  96. Log.Debug($"reload dll finish!");
  97. }
  98. }
  99. }