ConfigComponent.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using Model;
  4. namespace Hotfix
  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. Type[] types = Model.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 T GetOne<T>() where T : AConfig
  55. {
  56. Type type = typeof (T);
  57. ACategory configCategory;
  58. if (!this.allConfig.TryGetValue(type, out configCategory))
  59. {
  60. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  61. }
  62. return ((ACategory<T>) configCategory).GetOne();
  63. }
  64. public T Get<T>(long id) where T : AConfig
  65. {
  66. Type type = typeof (T);
  67. ACategory configCategory;
  68. if (!this.allConfig.TryGetValue(type, out configCategory))
  69. {
  70. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  71. }
  72. return ((ACategory<T>) configCategory)[id];
  73. }
  74. public T TryGet<T>(int id) where T : AConfig
  75. {
  76. Type type = typeof (T);
  77. ACategory configCategory;
  78. if (!this.allConfig.TryGetValue(type, out configCategory))
  79. {
  80. return default(T);
  81. }
  82. return ((ACategory<T>) configCategory).TryGet(id);
  83. }
  84. public T[] GetAll<T>() where T : AConfig
  85. {
  86. Type type = typeof (T);
  87. ACategory configCategory;
  88. if (!this.allConfig.TryGetValue(type, out configCategory))
  89. {
  90. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  91. }
  92. return ((ACategory<T>) configCategory).GetAll();
  93. }
  94. public T GetCategory<T>() where T : ACategory, new()
  95. {
  96. T t = new T();
  97. Type type = t.ConfigType;
  98. ACategory configCategory;
  99. bool ret = this.allConfig.TryGetValue(type, out configCategory);
  100. return ret? (T)configCategory : null;
  101. }
  102. }
  103. }