StartConfigComponent.cs 1.7 KB

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