ConfigComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 = ETModel.Game.Hotfix.GetHotfixTypes();
  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. 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. }