CodeLoader.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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>
  10. {
  11. private Assembly assembly;
  12. public void Start()
  13. {
  14. if (!Define.EnableDll)
  15. {
  16. GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  17. if (globalConfig.CodeMode != CodeMode.ClientServer)
  18. {
  19. throw new Exception("!ENABLE_CODES mode must use ClientServer code mode!");
  20. }
  21. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  22. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(assemblies);
  23. EventSystem.Instance.Add(types);
  24. foreach (Assembly ass in assemblies)
  25. {
  26. string name = ass.GetName().Name;
  27. if (name == "Unity.Model")
  28. {
  29. this.assembly = ass;
  30. }
  31. }
  32. }
  33. else
  34. {
  35. byte[] assBytes;
  36. byte[] pdbBytes;
  37. if (!Define.IsEditor)
  38. {
  39. //Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  40. //assBytes = ((TextAsset)dictionary["Model.dll"]).bytes;
  41. //pdbBytes = ((TextAsset)dictionary["Model.pdb"]).bytes;
  42. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Model.dll,真正打包要还原使用上面注释的代码
  43. assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.dll"));
  44. pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Model.pdb"));
  45. if (Define.EnableIL2CPP)
  46. {
  47. HybridCLRHelper.Load();
  48. }
  49. }
  50. else
  51. {
  52. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.dll"));
  53. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Model.pdb"));
  54. }
  55. this.assembly = Assembly.Load(assBytes, pdbBytes);
  56. }
  57. this.LoadHotfix();
  58. IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
  59. start.Run();
  60. }
  61. public void LoadHotfix()
  62. {
  63. byte[] assBytes;
  64. byte[] pdbBytes;
  65. if (!Define.IsEditor)
  66. {
  67. //Dictionary<string, UnityEngine.Object> dictionary = AssetsBundleHelper.LoadBundle("code.unity3d");
  68. //assBytes = ((TextAsset)dictionary["Hotfix.dll"]).bytes;
  69. //pdbBytes = ((TextAsset)dictionary["Hotfix.pdb"]).bytes;
  70. // 这里为了方便做测试,直接加载了Unity/Temp/Bin/Debug/Hotfix.dll,真正打包要还原使用上面注释的代码
  71. assBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.dll"));
  72. pdbBytes = File.ReadAllBytes(Path.Combine("../Unity", Define.BuildOutputDir, "Hotfix.pdb"));
  73. }
  74. else
  75. {
  76. assBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.dll"));
  77. pdbBytes = File.ReadAllBytes(Path.Combine(Define.BuildOutputDir, "Hotfix.pdb"));
  78. }
  79. Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
  80. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly);
  81. EventSystem.Instance.Add(types);
  82. }
  83. }
  84. }