StartConfigComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. namespace ETModel
  7. {
  8. [ObjectSystem]
  9. public class StartConfigComponentSystem : AwakeSystem<StartConfigComponent, string, int>
  10. {
  11. public override void Awake(StartConfigComponent self, string a, int b)
  12. {
  13. self.Awake(a, b);
  14. }
  15. }
  16. public class StartConfigComponent: Component
  17. {
  18. public static StartConfigComponent Instance { get; private set; }
  19. private Dictionary<int, StartConfig> configDict;
  20. private Dictionary<int, IPEndPoint> innerAddressDict = new Dictionary<int, IPEndPoint>();
  21. public StartConfig StartConfig { get; private set; }
  22. public StartConfig DBConfig { get; private set; }
  23. public StartConfig RealmConfig { get; private set; }
  24. public StartConfig LocationConfig { get; private set; }
  25. public List<StartConfig> MapConfigs { get; private set; }
  26. public List<StartConfig> GateConfigs { get; private set; }
  27. public void Awake(string path, int appId)
  28. {
  29. Instance = this;
  30. this.configDict = new Dictionary<int, StartConfig>();
  31. this.MapConfigs = new List<StartConfig>();
  32. this.GateConfigs = new List<StartConfig>();
  33. string[] ss = File.ReadAllText(path).Split('\n');
  34. foreach (string s in ss)
  35. {
  36. string s2 = s.Trim();
  37. if (s2 == "")
  38. {
  39. continue;
  40. }
  41. try
  42. {
  43. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  44. this.configDict.Add(startConfig.AppId, startConfig);
  45. InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
  46. if (innerConfig != null)
  47. {
  48. this.innerAddressDict.Add(startConfig.AppId, innerConfig.IPEndPoint);
  49. }
  50. if (startConfig.AppType.Is(AppType.Realm))
  51. {
  52. this.RealmConfig = startConfig;
  53. }
  54. if (startConfig.AppType.Is(AppType.Location))
  55. {
  56. this.LocationConfig = startConfig;
  57. }
  58. if (startConfig.AppType.Is(AppType.DB))
  59. {
  60. this.DBConfig = startConfig;
  61. }
  62. if (startConfig.AppType.Is(AppType.Map))
  63. {
  64. this.MapConfigs.Add(startConfig);
  65. }
  66. if (startConfig.AppType.Is(AppType.Gate))
  67. {
  68. this.GateConfigs.Add(startConfig);
  69. }
  70. }
  71. catch (Exception e)
  72. {
  73. Log.Error($"config错误: {s2} {e}");
  74. }
  75. }
  76. this.StartConfig = this.Get(appId);
  77. }
  78. public override void Dispose()
  79. {
  80. if (this.IsDisposed)
  81. {
  82. return;
  83. }
  84. base.Dispose();
  85. Instance = null;
  86. }
  87. public StartConfig Get(int id)
  88. {
  89. try
  90. {
  91. return this.configDict[id];
  92. }
  93. catch (Exception e)
  94. {
  95. throw new Exception($"not found startconfig: {id}", e);
  96. }
  97. }
  98. public IPEndPoint GetInnerAddress(int id)
  99. {
  100. try
  101. {
  102. return this.innerAddressDict[id];
  103. }
  104. catch (Exception e)
  105. {
  106. throw new Exception($"not found innerAddress: {id}", e);
  107. }
  108. }
  109. public StartConfig[] GetAll()
  110. {
  111. return this.configDict.Values.ToArray();
  112. }
  113. public int Count
  114. {
  115. get
  116. {
  117. return this.configDict.Count;
  118. }
  119. }
  120. }
  121. }