UnitConfig.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, IMerge
  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 void Merge(object o)
  23. {
  24. UnitConfigCategory s = o as UnitConfigCategory;
  25. this.list.AddRange(s.list);
  26. }
  27. public override void EndInit()
  28. {
  29. foreach (UnitConfig config in list)
  30. {
  31. config.EndInit();
  32. this.dict.Add(config.Id, config);
  33. }
  34. this.AfterEndInit();
  35. }
  36. public UnitConfig Get(int id)
  37. {
  38. this.dict.TryGetValue(id, out UnitConfig item);
  39. if (item == null)
  40. {
  41. throw new Exception($"配置找不到,配置表名: {nameof (UnitConfig)},配置id: {id}");
  42. }
  43. return item;
  44. }
  45. public bool Contain(int id)
  46. {
  47. return this.dict.ContainsKey(id);
  48. }
  49. public Dictionary<int, UnitConfig> GetAll()
  50. {
  51. return this.dict;
  52. }
  53. public UnitConfig GetOne()
  54. {
  55. if (this.dict == null || this.dict.Count <= 0)
  56. {
  57. return null;
  58. }
  59. return this.dict.Values.GetEnumerator().Current;
  60. }
  61. }
  62. [ProtoContract]
  63. public partial class UnitConfig: ProtoObject, IConfig
  64. {
  65. /// <summary>Id</summary>
  66. [ProtoMember(1)]
  67. public int Id { get; set; }
  68. /// <summary>Type</summary>
  69. [ProtoMember(2)]
  70. public int Type { get; set; }
  71. /// <summary>名字</summary>
  72. [ProtoMember(3)]
  73. public string Name { get; set; }
  74. /// <summary>描述</summary>
  75. [ProtoMember(4)]
  76. public string Desc { get; set; }
  77. /// <summary>位置</summary>
  78. [ProtoMember(5)]
  79. public int Position { get; set; }
  80. /// <summary>身高</summary>
  81. [ProtoMember(6)]
  82. public int Height { get; set; }
  83. /// <summary>体重</summary>
  84. [ProtoMember(7)]
  85. public int Weight { get; set; }
  86. }
  87. }