CodeLoader.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. this.LoadHotfix();
  61. IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
  62. start.Run();
  63. }
  64. public void 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. Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly);
  84. World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types);
  85. }
  86. }
  87. }