using System; using System.Collections.Generic; using System.Linq; using Fort23.UTool; namespace Fort23.GameData { public abstract class ConfigHolder { public bool IsChild { get; set; } public abstract Type ConfigType { get; } public abstract IConfig GetFirst(); public abstract List GetAll(); public abstract IConfig TryGet(int type); public abstract void SetConfig(List config); public abstract void AddChild(ConfigHolder child); // public abstract void Add public virtual void Init() { } } /// /// 管理所有的配置 /// /// [Serializable] public abstract class ConfigHolder : ConfigHolder where T : IConfig { public List configList; private Dictionary _dictionary; private List _children; public override void Init() { _dictionary = new Dictionary(); _children = new List(); foreach (var config in configList) { _dictionary.Add(config.GetID(), config); } } public override void AddChild(ConfigHolder child) { child.IsChild = true; _children.Add(child); } public override void SetConfig(List config) { if (configList != null) { return; } configList = new List(); for (int i = 0; i < config.Count; i++) { configList.Add((T)config[i]); } } public override Type ConfigType { get { return typeof(T); } } public override IConfig TryGet(int ID) { if (ID <= 0) { LogTool.Log($"在表{typeof(T)}中没有找到ID:{ID}"); return null; } IConfig t; if (!_dictionary.TryGetValue(ID, out t)) { for (int i = 0; i < _children.Count; i++) { IConfig config = _children[i].TryGet(ID); if (config != null) { return config; } } if (!IsChild) { LogTool.Log($"在表{typeof(T)}中没有找到ID:{ID}————{_dictionary.Count}"); } return null; } return t; } public override List GetAll() { List configs = _dictionary.Values.ToList(); for (int i = 0; i < _children.Count; i++) { configs.AddRange(_children[i].GetAll()); } return configs; } public override IConfig GetFirst() { return _dictionary.Values.First(); } } }