ConfigComponent.cs 2.6 KB

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