StartConfigComponent.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Base;
  5. namespace Model
  6. {
  7. [ObjectEvent]
  8. public class StartConfigComponentEvent : ObjectEvent<StartConfigComponent>, IAwake<string, int>
  9. {
  10. public void Awake(string a, int b)
  11. {
  12. this.Get().Awake(a, b);
  13. }
  14. }
  15. public class StartConfigComponent: Component
  16. {
  17. private readonly List<StartConfig> allConfigs = new List<StartConfig>();
  18. private readonly Dictionary<int, StartConfig> configDict = new Dictionary<int, StartConfig>();
  19. public StartConfig StartConfig { get; private set; }
  20. public void Awake(string path, int appId)
  21. {
  22. string[] ss = File.ReadAllText(path).Split('\n');
  23. foreach (string s in ss)
  24. {
  25. string s2 = s.Trim();
  26. if (s2 == "")
  27. {
  28. continue;
  29. }
  30. try
  31. {
  32. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  33. this.allConfigs.Add(startConfig);
  34. this.configDict.Add(startConfig.AppId, startConfig);
  35. }
  36. catch (Exception)
  37. {
  38. Log.Error($"config错误: {s2}");
  39. }
  40. }
  41. this.StartConfig = this.Get(appId);
  42. }
  43. public StartConfig Get(int id)
  44. {
  45. return this.configDict[id];
  46. }
  47. public StartConfig[] GetAll()
  48. {
  49. return this.allConfigs.ToArray();
  50. }
  51. }
  52. }