StartConfigComponent.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. namespace ETModel
  5. {
  6. [ObjectSystem]
  7. public class StartConfigComponentSystem: AwakeSystem<StartConfigComponent, StartConfig, long>
  8. {
  9. public override void Awake(StartConfigComponent self, StartConfig allConfig, long id)
  10. {
  11. self.Awake(allConfig, id);
  12. }
  13. }
  14. public class StartConfigComponent: Entity
  15. {
  16. public static StartConfigComponent Instance { get; private set; }
  17. // 所有进程的配置
  18. public StartConfig AllConfig;
  19. // 当前进程的配置
  20. public StartConfig StartConfig;
  21. // 所有domain的字典
  22. private Dictionary<long, StartConfig> configDict = new Dictionary<long, StartConfig>();
  23. // 所有有内网地址的进程字典
  24. private Dictionary<long, string> innerAddressDict = new Dictionary<long, string>();
  25. // 所有agent
  26. public Dictionary<long, StartConfig> Agents = new Dictionary<long, StartConfig>();
  27. private Dictionary<string, StartConfig> nameDict = new Dictionary<string, StartConfig>();
  28. private Dictionary<int, StartConfig> typeDict = new Dictionary<int, StartConfig>();
  29. public List<StartConfig> Gates { get; } = new List<StartConfig>();
  30. public void Awake(StartConfig allConfig, long id)
  31. {
  32. Instance = this;
  33. this.AllConfig = allConfig;
  34. this.AllConfig.Parent = this;
  35. // 每个进程的配置
  36. foreach (StartConfig s in this.AllConfig.List)
  37. {
  38. s.SceneInstanceId = (s.Id << IdGenerater.HeadPos) + s.Id;
  39. if (s.Id == id)
  40. {
  41. this.StartConfig = s;
  42. }
  43. InnerConfig innerConfig = s.GetComponent<InnerConfig>();
  44. if (innerConfig != null)
  45. {
  46. this.innerAddressDict.Add(s.Id, innerConfig.Address);
  47. }
  48. // 每个进程里面domain的配置
  49. foreach (StartConfig startConfig in s.List)
  50. {
  51. startConfig.SceneInstanceId = (startConfig.Parent.Id << IdGenerater.HeadPos) + startConfig.Id;
  52. this.configDict.Add(startConfig.Id, startConfig);
  53. SceneConfig sceneConfig = startConfig.GetComponent<SceneConfig>();
  54. switch (sceneConfig.SceneType)
  55. {
  56. case SceneType.Gate:
  57. this.Gates.Add(startConfig);
  58. break;
  59. case SceneType.Map:
  60. this.nameDict.Add(sceneConfig.Name, startConfig);
  61. break;
  62. default:
  63. this.typeDict.Add((int)sceneConfig.SceneType, startConfig);
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. public long GetInstanceId(SceneType sceneType)
  70. {
  71. if (!this.typeDict.TryGetValue((int) sceneType, out StartConfig startConfig))
  72. {
  73. throw new Exception($"GetInstanceId cant get StartConfig: {sceneType}");
  74. }
  75. return startConfig.SceneInstanceId;
  76. }
  77. public StartConfig GetByType(SceneType sceneType)
  78. {
  79. if (!this.typeDict.TryGetValue((int) sceneType, out StartConfig startConfig))
  80. {
  81. throw new Exception($"GetByType cant get StartConfig: {sceneType}");
  82. }
  83. return startConfig;
  84. }
  85. public StartConfig GetByName(string sceneName)
  86. {
  87. if (!this.nameDict.TryGetValue(sceneName, out StartConfig startConfig))
  88. {
  89. throw new Exception($"GetByName cant get StartConfig: {sceneName}");
  90. }
  91. return startConfig;
  92. }
  93. public override void Dispose()
  94. {
  95. if (this.IsDisposed)
  96. {
  97. return;
  98. }
  99. base.Dispose();
  100. Instance = null;
  101. this.configDict.Clear();
  102. this.innerAddressDict.Clear();
  103. this.Gates.Clear();
  104. this.Agents.Clear();
  105. this.typeDict.Clear();
  106. this.nameDict.Clear();
  107. this.StartConfig = null;
  108. }
  109. public StartConfig GetAgent(long agentId)
  110. {
  111. return this.Agents[agentId];
  112. }
  113. public StartConfig Get(long id)
  114. {
  115. try
  116. {
  117. return this.configDict[id];
  118. }
  119. catch (Exception e)
  120. {
  121. throw new Exception($"not found startconfig: {id}", e);
  122. }
  123. }
  124. public string GetProcessInnerAddress(long id)
  125. {
  126. try
  127. {
  128. // 内网地址需要找到进程配置,进程配置是domain配置的parent
  129. return this.innerAddressDict[id];
  130. }
  131. catch (Exception e)
  132. {
  133. throw new Exception($"not found innerAddress: {id}", e);
  134. }
  135. }
  136. public StartConfig[] GetAll()
  137. {
  138. List<StartConfig> startConfigs = new List<StartConfig>();
  139. foreach (var kv in this.AllConfig.Children)
  140. {
  141. startConfigs.Add((StartConfig)kv.Value);
  142. }
  143. return startConfigs.ToArray();
  144. }
  145. }
  146. }