ConfigLoader.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. namespace ET.Client
  7. {
  8. [Invoke]
  9. public class GetAllConfigBytes: AInvokeHandler<ConfigComponent.GetAllConfigBytes, Dictionary<Type, byte[]>>
  10. {
  11. public override Dictionary<Type, byte[]> Handle(ConfigComponent.GetAllConfigBytes args)
  12. {
  13. Dictionary<Type, byte[]> output = new Dictionary<Type, byte[]>();
  14. HashSet<Type> configTypes = EventSystem.Instance.GetTypes(typeof (ConfigAttribute));
  15. if (Define.IsEditor)
  16. {
  17. string ct = "cs";
  18. GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  19. CodeMode codeMode = globalConfig.CodeMode;
  20. switch (codeMode)
  21. {
  22. case CodeMode.Client:
  23. ct = "c";
  24. break;
  25. case CodeMode.Server:
  26. ct = "s";
  27. break;
  28. case CodeMode.ClientServer:
  29. ct = "cs";
  30. break;
  31. default:
  32. throw new ArgumentOutOfRangeException();
  33. }
  34. List<string> startConfigs = new List<string>()
  35. {
  36. "StartMachineConfigCategory",
  37. "StartProcessConfigCategory",
  38. "StartSceneConfigCategory",
  39. "StartZoneConfigCategory",
  40. };
  41. foreach (Type configType in configTypes)
  42. {
  43. string configFilePath;
  44. if (startConfigs.Contains(configType.Name))
  45. {
  46. configFilePath = $"../Config/GenFromExcel/{ct}/{Options.Instance.StartConfig}/{configType.Name}.bytes";
  47. }
  48. else
  49. {
  50. configFilePath = $"../Config/GenFromExcel/{ct}/{configType.Name}.bytes";
  51. }
  52. output[configType] = File.ReadAllBytes(configFilePath);
  53. }
  54. }
  55. else
  56. {
  57. using (Root.Instance.Scene.AddComponent<ResourcesComponent>())
  58. {
  59. const string configBundleName = "config.unity3d";
  60. ResourcesComponent.Instance.LoadBundle(configBundleName);
  61. foreach (Type configType in configTypes)
  62. {
  63. TextAsset v = ResourcesComponent.Instance.GetAsset(configBundleName, configType.Name) as TextAsset;
  64. output[configType] = v.bytes;
  65. }
  66. }
  67. }
  68. return output;
  69. }
  70. }
  71. [Invoke]
  72. public class GetOneConfigBytes: AInvokeHandler<ConfigComponent.GetOneConfigBytes, byte[]>
  73. {
  74. public override byte[] Handle(ConfigComponent.GetOneConfigBytes args)
  75. {
  76. var abNames = args.ConfigName.Split('.');
  77. TextAsset v = ResourcesComponent.Instance.GetAsset("config.unity3d", abNames[1]) as TextAsset;
  78. return v.bytes;
  79. //throw new NotImplementedException("client cant use LoadOneConfig");
  80. }
  81. }
  82. }