From 9900f9213393bc8d478d20b6f17343f45a66f5a4 Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Thu, 29 Jun 2023 14:20:23 +0700 Subject: [PATCH] init server before starting ui due to static ctors --- Torch.Server/Initializer.cs | 6 ++-- Torch.Server/Managers/InstanceManager.cs | 43 ++++++++++++++++++++++-- Torch/VRageGame.cs | 1 - 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Torch.Server/Initializer.cs b/Torch.Server/Initializer.cs index 6f4c2fe..3d6a4cd 100644 --- a/Torch.Server/Initializer.cs +++ b/Torch.Server/Initializer.cs @@ -104,8 +104,8 @@ namespace Torch.Server } #endif - SpaceEngineersGame.SetupBasicGameInfo(); - SpaceEngineersGame.SetupPerGameSettings(); + _server.Init(); + var uiThread = new Thread(() => { var ui = new TorchUI(_server); @@ -118,9 +118,7 @@ namespace Torch.Server uiThread.SetApartmentState(ApartmentState.STA); uiThread.Start(); - _server.Init(); - if (Config.Autostart || Config.TempAutostart) { Config.TempAutostart = false; diff --git a/Torch.Server/Managers/InstanceManager.cs b/Torch.Server/Managers/InstanceManager.cs index 25a8196..e45ac83 100644 --- a/Torch.Server/Managers/InstanceManager.cs +++ b/Torch.Server/Managers/InstanceManager.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Reflection; using System.Runtime.Serialization; using System.Text; +using System.Threading; using System.Threading.Tasks; using Havok; using NLog; @@ -34,8 +35,46 @@ namespace Torch.Server.Managers public class InstanceManager : Manager, IInstanceManager { private const string CONFIG_NAME = "SpaceEngineers-Dedicated.cfg"; + + private Action _instanceLoaded; + + /// + /// Gets or sets the instance loaded event. + /// + /// + /// Called when the instance is loaded and immediately if subscribed after the instance is loaded. + /// + public event Action InstanceLoaded + { + add + { + var action = _instanceLoaded; + Action action2; + do + { + action2 = action; + var action3 = (Action)Delegate.Combine(action2, value); + action = Interlocked.CompareExchange(ref _instanceLoaded, action3, action2); + } + while (action != action2); + + if (DedicatedConfig is not null) + value(DedicatedConfig); + } + remove + { + var action = _instanceLoaded; + Action action2; + do + { + action2 = action; + var action3 = (Action)Delegate.Remove(action2, value); + action = Interlocked.CompareExchange(ref _instanceLoaded, action3, action2); + } + while (action != action2); + } + } - public event Action InstanceLoaded; public ConfigDedicatedViewModel DedicatedConfig { get; set; } private static readonly Logger Log = LogManager.GetLogger(nameof(InstanceManager)); [Dependency] @@ -102,7 +141,7 @@ namespace Torch.Server.Managers SelectWorld(DedicatedConfig.LoadWorld ?? DedicatedConfig.Worlds.First().WorldPath, false); - InstanceLoaded?.Invoke(DedicatedConfig); + _instanceLoaded?.Invoke(DedicatedConfig); } public void SelectCreatedWorld(string worldPath) diff --git a/Torch/VRageGame.cs b/Torch/VRageGame.cs index 61e905e..e7c9af9 100644 --- a/Torch/VRageGame.cs +++ b/Torch/VRageGame.cs @@ -154,7 +154,6 @@ namespace Torch Environment.SetEnvironmentVariable("SteamAppId", _appSteamId.ToString()); MyVRageWindows.Init("SpaceEngineersDedicated", MySandboxGame.Log, null, false); SpaceEngineersGame.SetupPerGameSettings(); - SpaceEngineersGame.SetupBasicGameInfo(); MySessionComponentExtDebug.ForceDisable = true; MyPerGameSettings.SendLogToKeen = false; // SpaceEngineersGame.SetupAnalytics();