ConfigComponent.cs 2.5 KB

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