ACategory.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Base;
  5. namespace Model
  6. {
  7. /// <summary>
  8. /// 管理该所有的配置
  9. /// </summary>
  10. /// <typeparam name="T"></typeparam>
  11. public abstract class ACategory<T>: ICategory where T : AConfig
  12. {
  13. protected Dictionary<long, T> dict;
  14. public virtual void BeginInit()
  15. {
  16. this.dict = new Dictionary<long, T>();
  17. string configStr = ConfigHelper.GetText(typeof (T).Name);
  18. foreach (string str in configStr.Split(new[] { "\n" }, StringSplitOptions.None))
  19. {
  20. try
  21. {
  22. string str2 = str.Trim();
  23. if (str2 == "")
  24. {
  25. continue;
  26. }
  27. T t = MongoHelper.FromJson<T>(str2);
  28. this.dict.Add(t.Id, t);
  29. }
  30. catch (Exception e)
  31. {
  32. throw new Exception($"parser json fail: {str}", e);
  33. }
  34. }
  35. }
  36. public Type ConfigType
  37. {
  38. get
  39. {
  40. return typeof (T);
  41. }
  42. }
  43. public virtual void EndInit()
  44. {
  45. }
  46. public T this[long type]
  47. {
  48. get
  49. {
  50. if (!this.dict.TryGetValue(type, out T t))
  51. {
  52. throw new KeyNotFoundException($"{typeof(T)} 没有找到配置, key: {type}");
  53. }
  54. return t;
  55. }
  56. }
  57. public T TryGet(int type)
  58. {
  59. if (!this.dict.TryGetValue(type, out T t))
  60. {
  61. return null;
  62. }
  63. return t;
  64. }
  65. public T[] GetAll()
  66. {
  67. return this.dict.Values.ToArray();
  68. }
  69. public T GetOne()
  70. {
  71. return this.dict.Values.First();
  72. }
  73. }
  74. }