ConfigComponentSystem.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. namespace KYFramework
  4. {
  5. [ObjectSystem]
  6. public class ConfigComponentAwakeSystem : AwakeSystem<ConfigComponent>
  7. {
  8. public override void Awake(ConfigComponent self)
  9. {
  10. self.Load();
  11. }
  12. }
  13. public static class ConfigComponentSystem
  14. {
  15. public static void Load(this ConfigComponent self)
  16. {
  17. self.allConfig.Clear();
  18. List<Type> types = Game.EventSystem.GetTypes(typeof(ConfigAttribute));
  19. foreach (Type type in types)
  20. {
  21. object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
  22. if(attrs.Length == 0) continue;
  23. object obj = Activator.CreateInstance(type);
  24. ACategory icategory = obj as ACategory;
  25. if (icategory == null)
  26. {
  27. throw new Exception($"class: {type.Name} not inherit from ACategory");
  28. }
  29. icategory.BeginInit();
  30. icategory.EndInit();
  31. self.allConfig[icategory.Config] = icategory;
  32. }
  33. }
  34. public static IConfig GetOne(this ConfigComponent self, Type type)
  35. {
  36. ACategory configCategory;
  37. if (!self.allConfig.TryGetValue(type, out configCategory))
  38. {
  39. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  40. }
  41. return configCategory.GetOne();
  42. }
  43. public static IConfig Get(this ConfigComponent self, Type type, int id)
  44. {
  45. ACategory configCategory;
  46. if (!self.allConfig.TryGetValue(type, out configCategory))
  47. {
  48. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  49. }
  50. return configCategory.TryGet(id);
  51. }
  52. public static IConfig TryGet(this ConfigComponent self, Type type, int id)
  53. {
  54. ACategory configCategory;
  55. if (!self.allConfig.TryGetValue(type, out configCategory))
  56. {
  57. return null;
  58. }
  59. return configCategory.TryGet(id);
  60. }
  61. public static IConfig[] GetAll(this ConfigComponent self, Type type)
  62. {
  63. ACategory configCategory;
  64. if (!self.allConfig.TryGetValue(type, out configCategory))
  65. {
  66. throw new Exception($"ConfigComponent not found key: {type.FullName}");
  67. }
  68. return configCategory.GetAll();
  69. }
  70. public static ACategory GetCategory(this ConfigComponent self, Type type)
  71. {
  72. ACategory configCategory;
  73. bool ret = self.allConfig.TryGetValue(type, out configCategory);
  74. return ret ? configCategory : null;
  75. }
  76. }
  77. }