CodeLoader.cs 3.4 KB

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