ACategory.cs 1.4 KB

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