ConfigComponent.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: Component
  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. List<Type> types = Game.EventSystem.GetTypes();
  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. object obj = Activator.CreateInstance(type);
  43. ACategory iCategory = obj as ACategory;
  44. if (iCategory == null)
  45. {
  46. throw new Exception($"class: {type.Name} not inherit from ACategory");
  47. }
  48. iCategory.BeginInit();
  49. iCategory.EndInit();
  50. this.allConfig[iCategory.ConfigType] = iCategory;
  51. }
  52. }
  53. public IConfig GetOne(Type type)
  54. {
  55. ACategory configCategory;
  56. if (!this.allConfig.TryGetValue(type, out configCategory))
  57. {
  58. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  59. }
  60. return configCategory.GetOne();
  61. }
  62. public IConfig Get(Type type, int id)
  63. {
  64. ACategory configCategory;
  65. if (!this.allConfig.TryGetValue(type, out configCategory))
  66. {
  67. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  68. }
  69. return configCategory.TryGet(id);
  70. }
  71. public IConfig TryGet(Type type, int id)
  72. {
  73. ACategory configCategory;
  74. if (!this.allConfig.TryGetValue(type, out configCategory))
  75. {
  76. return null;
  77. }
  78. return configCategory.TryGet(id);
  79. }
  80. public IConfig[] GetAll(Type type)
  81. {
  82. ACategory configCategory;
  83. if (!this.allConfig.TryGetValue(type, out configCategory))
  84. {
  85. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  86. }
  87. return configCategory.GetAll();
  88. }
  89. public ACategory GetCategory(Type type)
  90. {
  91. ACategory configCategory;
  92. bool ret = this.allConfig.TryGetValue(type, out configCategory);
  93. return ret ? configCategory : null;
  94. }
  95. }
  96. }