StartConfigComponent.cs 1.1 KB

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