ConfigManager.cs 980 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using Helper;
  6. namespace Component
  7. {
  8. public abstract class ConfigManager<T> : ISupportInitialize, IConfigInitialize where T : IType
  9. {
  10. protected readonly Dictionary<int, T> dict = new Dictionary<int, T>();
  11. public T this[int type]
  12. {
  13. get
  14. {
  15. return dict[type];
  16. }
  17. }
  18. public string ConfigName
  19. {
  20. get
  21. {
  22. return typeof (T).Name;
  23. }
  24. }
  25. public Dictionary<int, T> GetAll()
  26. {
  27. return this.dict;
  28. }
  29. public void Init(string configsDir)
  30. {
  31. string path = Path.Combine(configsDir, this.ConfigName);
  32. if (!Directory.Exists(path))
  33. {
  34. throw new Exception(string.Format("not found config path: {0}", path));
  35. }
  36. foreach (var file in Directory.GetFiles(path))
  37. {
  38. var t = MongoHelper.FromJson<T>(File.ReadAllText(file));
  39. this.dict.Add(t.Type, t);
  40. }
  41. }
  42. public void BeginInit()
  43. {
  44. }
  45. public void EndInit()
  46. {
  47. }
  48. }
  49. }