StartConfigComponent.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 List<StartConfig> allConfigs;
  17. private Dictionary<int, StartConfig> configDict;
  18. public StartConfig StartConfig { get; private set; }
  19. public StartConfig DBConfig { get; private set; }
  20. public StartConfig RealmConfig { get; private set; }
  21. public StartConfig LocationConfig { get; private set; }
  22. public List<StartConfig> MapConfigs { get; private set; }
  23. public List<StartConfig> GateConfigs { get; private set; }
  24. public void Awake(string path, int appId)
  25. {
  26. this.allConfigs = new List<StartConfig>();
  27. this.configDict = new Dictionary<int, StartConfig>();
  28. this.MapConfigs = new List<StartConfig>();
  29. this.GateConfigs = new List<StartConfig>();
  30. string[] ss = File.ReadAllText(path).Split('\n');
  31. foreach (string s in ss)
  32. {
  33. string s2 = s.Trim();
  34. if (s2 == "")
  35. {
  36. continue;
  37. }
  38. try
  39. {
  40. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  41. this.allConfigs.Add(startConfig);
  42. this.configDict.Add(startConfig.AppId, startConfig);
  43. if (startConfig.AppType.Is(AppType.Realm))
  44. {
  45. this.RealmConfig = startConfig;
  46. }
  47. if (startConfig.AppType.Is(AppType.Location))
  48. {
  49. this.LocationConfig = startConfig;
  50. }
  51. if (startConfig.AppType.Is(AppType.DB))
  52. {
  53. this.DBConfig = startConfig;
  54. }
  55. if (startConfig.AppType.Is(AppType.Map))
  56. {
  57. this.MapConfigs.Add(startConfig);
  58. }
  59. if (startConfig.AppType.Is(AppType.Gate))
  60. {
  61. this.GateConfigs.Add(startConfig);
  62. }
  63. }
  64. catch (Exception)
  65. {
  66. Log.Error($"config错误: {s2}");
  67. }
  68. }
  69. this.StartConfig = this.Get(appId);
  70. }
  71. public StartConfig Get(int id)
  72. {
  73. try
  74. {
  75. return this.configDict[id];
  76. }
  77. catch (Exception e)
  78. {
  79. throw new Exception($"not found startconfig: {id}", e);
  80. }
  81. }
  82. public StartConfig[] GetAll()
  83. {
  84. return this.allConfigs.ToArray();
  85. }
  86. public int Count
  87. {
  88. get
  89. {
  90. return this.allConfigs.Count;
  91. }
  92. }
  93. }
  94. }