ACategory.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. T t;
  51. if (!this.dict.TryGetValue(type, out t))
  52. {
  53. throw new KeyNotFoundException($"{typeof (T)} 没有找到配置, key: {type}");
  54. }
  55. return t;
  56. }
  57. }
  58. public T TryGet(int type)
  59. {
  60. T t;
  61. if (!this.dict.TryGetValue(type, out t))
  62. {
  63. return null;
  64. }
  65. return t;
  66. }
  67. public T[] GetAll()
  68. {
  69. return this.dict.Values.ToArray();
  70. }
  71. public T GetOne()
  72. {
  73. return this.dict.Values.First();
  74. }
  75. }
  76. }