UnitConfig.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using MongoDB.Bson.Serialization.Attributes;
  4. using ProtoBuf;
  5. namespace ET
  6. {
  7. [ProtoContract]
  8. [Config]
  9. public partial class UnitConfigCategory : ProtoObject
  10. {
  11. public static UnitConfigCategory Instance;
  12. [ProtoIgnore]
  13. [BsonIgnore]
  14. private Dictionary<int, UnitConfig> dict = new Dictionary<int, UnitConfig>();
  15. [BsonElement]
  16. [ProtoMember(1)]
  17. private List<UnitConfig> list = new List<UnitConfig>();
  18. public UnitConfigCategory()
  19. {
  20. Instance = this;
  21. }
  22. public override void EndInit()
  23. {
  24. foreach (UnitConfig config in list)
  25. {
  26. this.dict.Add(config.Id, config);
  27. }
  28. this.AfterEndInit();
  29. }
  30. public UnitConfig Get(int id)
  31. {
  32. this.dict.TryGetValue(id, out UnitConfig item);
  33. if (item == null)
  34. {
  35. throw new Exception($"配置找不到,配置表名: {nameof (UnitConfig)},配置id: {id}");
  36. }
  37. return item;
  38. }
  39. public bool Contain(int id)
  40. {
  41. return this.dict.ContainsKey(id);
  42. }
  43. public Dictionary<int, UnitConfig> GetAll()
  44. {
  45. return this.dict;
  46. }
  47. public UnitConfig GetOne()
  48. {
  49. if (this.dict == null || this.dict.Count <= 0)
  50. {
  51. return null;
  52. }
  53. return this.dict.Values.GetEnumerator().Current;
  54. }
  55. }
  56. [ProtoContract]
  57. public partial class UnitConfig: ProtoObject, IConfig
  58. {
  59. [ProtoMember(1)]
  60. public int Id { get; set; }
  61. [ProtoMember(2)]
  62. public string Name { get; set; }
  63. [ProtoMember(3)]
  64. public string Desc { get; set; }
  65. [ProtoMember(4)]
  66. public int Position { get; set; }
  67. [ProtoMember(5)]
  68. public int Height { get; set; }
  69. [ProtoMember(6)]
  70. public int Weight { get; set; }
  71. }
  72. }