StartConfigComponent.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace ETModel
  6. {
  7. [ObjectSystem]
  8. public class StartConfigComponentSystem : AwakeSystem<StartConfigComponent, string, int>
  9. {
  10. public override void Awake(StartConfigComponent self, string a, int b)
  11. {
  12. self.Awake(a, b);
  13. }
  14. }
  15. public class StartConfigComponent: Component
  16. {
  17. public static StartConfigComponent Instance { get; private set; }
  18. private Dictionary<int, StartConfig> configDict;
  19. public StartConfig StartConfig { get; private set; }
  20. public StartConfig DBConfig { get; private set; }
  21. public StartConfig RealmConfig { get; private set; }
  22. public StartConfig LocationConfig { get; private set; }
  23. public List<StartConfig> MapConfigs { get; private set; }
  24. public List<StartConfig> GateConfigs { get; private set; }
  25. public void Awake(string path, int appId)
  26. {
  27. Instance = this;
  28. this.configDict = new Dictionary<int, StartConfig>();
  29. this.MapConfigs = new List<StartConfig>();
  30. this.GateConfigs = new List<StartConfig>();
  31. string[] ss = File.ReadAllText(path).Split('\n');
  32. foreach (string s in ss)
  33. {
  34. string s2 = s.Trim();
  35. if (s2 == "")
  36. {
  37. continue;
  38. }
  39. try
  40. {
  41. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  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 e)
  65. {
  66. Log.Error($"config错误: {s2} {e}");
  67. }
  68. }
  69. this.StartConfig = this.Get(appId);
  70. }
  71. public override void Dispose()
  72. {
  73. if (this.IsDisposed)
  74. {
  75. return;
  76. }
  77. base.Dispose();
  78. Instance = null;
  79. }
  80. public StartConfig Get(int id)
  81. {
  82. try
  83. {
  84. return this.configDict[id];
  85. }
  86. catch (Exception e)
  87. {
  88. throw new Exception($"not found startconfig: {id}", e);
  89. }
  90. }
  91. public StartConfig[] GetAll()
  92. {
  93. return this.configDict.Values.ToArray();
  94. }
  95. public int Count
  96. {
  97. get
  98. {
  99. return this.configDict.Count;
  100. }
  101. }
  102. }
  103. }