ConfigComponent.cs 2.6 KB

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