ConfigComponent.cs 2.5 KB

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