ConfigComponentSystem.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using ETModel;
  4. namespace ETHotfix
  5. {
  6. [ObjectSystem]
  7. public class ConfigAwakeSystem : AwakeSystem<ConfigComponent>
  8. {
  9. public override void Awake(ConfigComponent self)
  10. {
  11. self.Awake();
  12. }
  13. }
  14. [ObjectSystem]
  15. public class ConfigLoadSystem : LoadSystem<ConfigComponent>
  16. {
  17. public override void Load(ConfigComponent self)
  18. {
  19. self.Load();
  20. }
  21. }
  22. public static class ConfigComponentHelper
  23. {
  24. public static void Awake(this ConfigComponent self)
  25. {
  26. self.Load();
  27. }
  28. public static void Load(this ConfigComponent self)
  29. {
  30. AppType appType = StartConfigComponent.Instance.StartConfig.AppType;
  31. self.AllConfig.Clear();
  32. List<Type> types = Game.EventSystem.GetTypes(typeof(ConfigAttribute));
  33. foreach (Type type in types)
  34. {
  35. object[] attrs = type.GetCustomAttributes(typeof (ConfigAttribute), false);
  36. if (attrs.Length == 0)
  37. {
  38. continue;
  39. }
  40. ConfigAttribute configAttribute = attrs[0] as ConfigAttribute;
  41. // 只加载指定的配置
  42. if (!configAttribute.Type.Is(appType))
  43. {
  44. continue;
  45. }
  46. object obj = Activator.CreateInstance(type);
  47. ACategory iCategory = obj as ACategory;
  48. if (iCategory == null)
  49. {
  50. throw new Exception($"class: {type.Name} not inherit from ACategory");
  51. }
  52. iCategory.BeginInit();
  53. iCategory.EndInit();
  54. self.AllConfig[iCategory.ConfigType] = iCategory;
  55. }
  56. }
  57. public static IConfig GetOne(this ConfigComponent self, Type type)
  58. {
  59. ACategory configCategory;
  60. if (!self.AllConfig.TryGetValue(type, out configCategory))
  61. {
  62. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  63. }
  64. return configCategory.GetOne();
  65. }
  66. public static IConfig Get(this ConfigComponent self, Type type, int id)
  67. {
  68. ACategory configCategory;
  69. if (!self.AllConfig.TryGetValue(type, out configCategory))
  70. {
  71. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  72. }
  73. return configCategory.TryGet(id);
  74. }
  75. public static IConfig TryGet(this ConfigComponent self, Type type, int id)
  76. {
  77. ACategory configCategory;
  78. if (!self.AllConfig.TryGetValue(type, out configCategory))
  79. {
  80. return null;
  81. }
  82. return configCategory.TryGet(id);
  83. }
  84. public static IConfig[] GetAll(this ConfigComponent self, Type type)
  85. {
  86. ACategory configCategory;
  87. if (!self.AllConfig.TryGetValue(type, out configCategory))
  88. {
  89. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  90. }
  91. return configCategory.GetAll();
  92. }
  93. public static ACategory GetCategory(this ConfigComponent self, Type type)
  94. {
  95. ACategory configCategory;
  96. bool ret = self.AllConfig.TryGetValue(type, out configCategory);
  97. return ret ? configCategory : null;
  98. }
  99. }
  100. }