using System; using System.Collections.Generic; using MongoDB.Bson.Serialization.Attributes; using ProtoBuf; namespace ET { [ProtoContract] [Config] public partial class UnitConfigCategory : ConfigSingleton, IMerge { [ProtoIgnore] [BsonIgnore] private Dictionary dict = new Dictionary(); [BsonElement] [ProtoMember(1)] private List list = new List(); public void Merge(object o) { UnitConfigCategory s = o as UnitConfigCategory; this.list.AddRange(s.list); } [ProtoAfterDeserialization] public void ProtoEndInit() { foreach (UnitConfig config in list) { config.AfterEndInit(); this.dict.Add(config.Id, config); } this.list.Clear(); this.AfterEndInit(); } public UnitConfig Get(int id) { this.dict.TryGetValue(id, out UnitConfig item); if (item == null) { throw new Exception($"配置找不到,配置表名: {nameof (UnitConfig)},配置id: {id}"); } return item; } public bool Contain(int id) { return this.dict.ContainsKey(id); } public Dictionary GetAll() { return this.dict; } public UnitConfig GetOne() { if (this.dict == null || this.dict.Count <= 0) { return null; } return this.dict.Values.GetEnumerator().Current; } } [ProtoContract] public partial class UnitConfig: ProtoObject, IConfig { /// Id [ProtoMember(1)] public int Id { get; set; } /// Type [ProtoMember(2)] public int Type { get; set; } /// 名字 [ProtoMember(3)] public string Name { get; set; } /// 位置 [ProtoMember(4)] public int Position { get; set; } /// 身高 [ProtoMember(5)] public int Height { get; set; } } }