CodeLoader.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. World.Instance.AddSingleton<CodeTypes, Assembly[]>(assemblies);
  26. foreach (Assembly ass in assemblies)
  27. {
  28. string name = ass.GetName().Name;
  29. if (name == "Unity.Model")
  30. {
  31. this.assembly = ass;
  32. }
  33. }
  34. }
  35. else
  36. {
  37. byte[] assBytes;
  38. byte[] pdbBytes;
  39. if (!Define.IsEditor)
  40. {
  41. //Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  42. //assBytes = ((TextAsset)dictionary["Model.dll"]).bytes;
  43. //pdbBytes = ((TextAsset)dictionary["Model.pdb"]).bytes;
  44. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Model.dll,真正打包要还原使用上面注释的代码
  45. assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.dll"));
  46. pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.pdb"));
  47. if (Define.EnableIL2CPP)
  48. {
  49. HybridCLRHelper.Load();
  50. }
  51. }
  52. else
  53. {
  54. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  55. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  56. }
  57. this.assembly = Assembly.Load(assBytes, pdbBytes);
  58. }
  59. {
  60. Assembly hotfixAssembly = this.LoadHotfix();
  61. World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly});
  62. IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
  63. start.Run();
  64. }
  65. }
  66. private Assembly LoadHotfix()
  67. {
  68. byte[] assBytes;
  69. byte[] pdbBytes;
  70. if (!Define.IsEditor)
  71. {
  72. //Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  73. //assBytes = ((TextAsset)dictionary["Hotfix.dll"]).bytes;
  74. //pdbBytes = ((TextAsset)dictionary["Hotfix.pdb"]).bytes;
  75. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Hotfix.dll,真正打包要还原使用上面注释的代码
  76. assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.dll"));
  77. pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.pdb"));
  78. }
  79. else
  80. {
  81. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.dll"));
  82. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.pdb"));
  83. }
  84. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  85. return hotfixAssembly;
  86. }
  87. public void Reload()
  88. {
  89. Assembly hotfixAssembly = this.LoadHotfix();
  90. World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly}, true);
  91. World.Instance.Load();
  92. Log.Debug($"reload dll finish!");
  93. }
  94. }
  95. }