ConfigComponentSystem.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. self.AllConfig.Clear();
  31. HashSet<Type> types = Game.EventSystem.GetTypes(typeof(ConfigAttribute));
  32. foreach (Type type in types)
  33. {
  34. object obj = Activator.CreateInstance(type);
  35. ACategory iCategory = obj as ACategory;
  36. if (iCategory == null)
  37. {
  38. throw new Exception($"class: {type.Name} not inherit from ACategory");
  39. }
  40. iCategory.BeginInit();
  41. iCategory.EndInit();
  42. self.AllConfig[iCategory.ConfigType] = iCategory;
  43. }
  44. }
  45. public static IConfig GetOne(this ConfigComponent self, Type type)
  46. {
  47. ACategory configCategory;
  48. if (!self.AllConfig.TryGetValue(type, out configCategory))
  49. {
  50. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  51. }
  52. return configCategory.GetOne();
  53. }
  54. public static IConfig Get(this ConfigComponent self, Type type, int id)
  55. {
  56. ACategory configCategory;
  57. if (!self.AllConfig.TryGetValue(type, out configCategory))
  58. {
  59. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  60. }
  61. return configCategory.TryGet(id);
  62. }
  63. public static IConfig TryGet(this ConfigComponent self, Type type, int id)
  64. {
  65. ACategory configCategory;
  66. if (!self.AllConfig.TryGetValue(type, out configCategory))
  67. {
  68. return null;
  69. }
  70. return configCategory.TryGet(id);
  71. }
  72. public static IConfig[] GetAll(this ConfigComponent self, Type type)
  73. {
  74. ACategory configCategory;
  75. if (!self.AllConfig.TryGetValue(type, out configCategory))
  76. {
  77. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  78. }
  79. return configCategory.GetAll();
  80. }
  81. public static ACategory GetCategory(this ConfigComponent self, Type type)
  82. {
  83. ACategory configCategory;
  84. bool ret = self.AllConfig.TryGetValue(type, out configCategory);
  85. return ret ? configCategory : null;
  86. }
  87. }
  88. }