StartConfigComponent.cs 1.1 KB

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