using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; namespace ETModel { [ObjectSystem] public class AppManagerComponentAwakeSystem : AwakeSystem { public override void Awake(AppManagerComponent self) { self.Awake(); } } public class AppManagerComponent: Component { private readonly Dictionary processes = new Dictionary(); public void Awake() { string[] ips = NetHelper.GetAddressIPs(); StartConfig[] startConfigs = StartConfigComponent.Instance.GetAll(); foreach (StartConfig startConfig in startConfigs) { Game.Scene.GetComponent().WaitAsync(100); if (!ips.Contains(startConfig.ServerIP) && startConfig.ServerIP != "*") { continue; } if (startConfig.AppType.Is(AppType.Manager)) { continue; } StartProcess(startConfig.AppId); } this.WatchProcessAsync().Coroutine(); } private void StartProcess(int appId) { OptionComponent optionComponent = Game.Scene.GetComponent(); StartConfigComponent startConfigComponent = StartConfigComponent.Instance; string configFile = optionComponent.Options.Config; StartConfig startConfig = startConfigComponent.Get(appId); const string exe = "dotnet"; string arguments = $"App.dll --appId={startConfig.AppId} --appType={startConfig.AppType} --config={configFile}"; Log.Info($"{exe} {arguments}"); try { Process process = ProcessHelper.Run(exe, arguments); this.processes.Add(startConfig.AppId, process); } catch (Exception e) { Log.Error(e); } } /// /// 监控启动的进程,如果进程挂掉了,重新拉起 /// private async ETVoid WatchProcessAsync() { long instanceId = this.InstanceId; while (true) { await Game.Scene.GetComponent().WaitAsync(5000); if (this.InstanceId != instanceId) { return; } foreach (int appId in this.processes.Keys.ToArray()) { Process process = this.processes[appId]; if (!process.HasExited) { continue; } this.processes.Remove(appId); process.Dispose(); this.StartProcess(appId); } } } } }