using System; using System.Collections.Generic; namespace ETModel { [ObjectSystem] public class ConfigComponentAwakeSystem : AwakeSystem { public override void Awake(ConfigComponent self) { self.Awake(); } } [ObjectSystem] public class ConfigComponentLoadSystem : LoadSystem { public override void Load(ConfigComponent self) { self.Load(); } } /// /// Config组件会扫描所有的有ConfigAttribute标签的配置,加载进来 /// public class ConfigComponent: Component { private Dictionary allConfig = new Dictionary(); public void Awake() { this.Load(); } public void Load() { this.allConfig.Clear(); List types = Game.EventSystem.GetTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof (ConfigAttribute), false); if (attrs.Length == 0) { continue; } object obj = Activator.CreateInstance(type); ACategory iCategory = obj as ACategory; if (iCategory == null) { throw new Exception($"class: {type.Name} not inherit from ACategory"); } iCategory.BeginInit(); iCategory.EndInit(); this.allConfig[iCategory.ConfigType] = iCategory; } } public IConfig GetOne(Type type) { ACategory configCategory; if (!this.allConfig.TryGetValue(type, out configCategory)) { throw new Exception($"ConfigComponent not found key: {type.FullName}"); } return configCategory.GetOne(); } public IConfig Get(Type type, int id) { ACategory configCategory; if (!this.allConfig.TryGetValue(type, out configCategory)) { throw new Exception($"ConfigComponent not found key: {type.FullName}"); } return configCategory.TryGet(id); } public IConfig TryGet(Type type, int id) { ACategory configCategory; if (!this.allConfig.TryGetValue(type, out configCategory)) { return null; } return configCategory.TryGet(id); } public IConfig[] GetAll(Type type) { ACategory configCategory; if (!this.allConfig.TryGetValue(type, out configCategory)) { throw new Exception($"ConfigComponent not found key: {type.FullName}"); } return configCategory.GetAll(); } public ACategory GetCategory(Type type) { ACategory configCategory; bool ret = this.allConfig.TryGetValue(type, out configCategory); return ret ? configCategory : null; } } }