diff --git a/Torch.API/IMultiplayer.cs b/Torch.API/IMultiplayer.cs index 274f075..540c73f 100644 --- a/Torch.API/IMultiplayer.cs +++ b/Torch.API/IMultiplayer.cs @@ -13,8 +13,7 @@ namespace Torch.API event Action MessageReceived; Dictionary Players { get; } List Chat { get; } - void SendMessage(string message); - void SendMessage(string message, long playerId, string author = "Server", string font = MyFontEnum.Blue); + void SendMessage(string message, string author = "Server", long playerId = 0, string font = MyFontEnum.Blue); void KickPlayer(ulong id); void BanPlayer(ulong id, bool banned = true); IMyPlayer GetPlayerBySteamId(ulong id); diff --git a/Torch.API/ITorchBase.cs b/Torch.API/ITorchBase.cs index 981cd28..5d9c841 100644 --- a/Torch.API/ITorchBase.cs +++ b/Torch.API/ITorchBase.cs @@ -27,6 +27,7 @@ namespace Torch.API public interface ITorchServer : ITorchBase { bool IsRunning { get; } + string InstancePath { get; } } public interface ITorchClient : ITorchBase diff --git a/Torch.Server/Program.cs b/Torch.Server/Program.cs index 0e6292c..fea274c 100644 --- a/Torch.Server/Program.cs +++ b/Torch.Server/Program.cs @@ -39,9 +39,9 @@ namespace Torch.Server return; } - string configName = args.Length > 0 ? args[0] : "TorchConfig.xml"; + var configName = args.FirstOrDefault() ?? "TorchConfig.xml"; var configPath = Path.Combine(Directory.GetCurrentDirectory(), configName); - var options = new ServerConfig("Torch"); + ServerConfig options; if (File.Exists(configName)) { _log.Info($"Loading config {configPath}"); @@ -50,6 +50,7 @@ namespace Torch.Server else { _log.Info($"Generating default config at {configPath}"); + options = new ServerConfig(); options.SaveTo(configPath); } diff --git a/Torch.Server/ServerConfig.cs b/Torch.Server/ServerConfig.cs index d970309..662c1bb 100644 --- a/Torch.Server/ServerConfig.cs +++ b/Torch.Server/ServerConfig.cs @@ -16,7 +16,6 @@ namespace Torch.Server public string InstancePath { get; set; } public string InstanceName { get; set; } - //public string SaveName { get; set; } public int Autosave { get; set; } public bool AutoRestart { get; set; } @@ -30,7 +29,6 @@ namespace Torch.Server public static ServerConfig LoadFrom(string path) { - _log.Info($"Loading config from '{path}'"); try { var serializer = new XmlSerializer(typeof(ServerConfig)); @@ -43,14 +41,13 @@ namespace Torch.Server } catch (Exception e) { - Console.WriteLine(e); + _log.Error(e); return null; } } public bool SaveTo(string path) { - _log.Info($"Saving config to '{path}'"); try { var serializer = new XmlSerializer(typeof(ServerConfig)); @@ -62,7 +59,7 @@ namespace Torch.Server } catch (Exception e) { - Console.WriteLine(e); + _log.Error(e); return false; } } diff --git a/Torch/Managers/MultiplayerManager.cs b/Torch/Managers/MultiplayerManager.cs index b743c62..89e2ca0 100644 --- a/Torch/Managers/MultiplayerManager.cs +++ b/Torch/Managers/MultiplayerManager.cs @@ -74,15 +74,10 @@ namespace Torch.Managers return p; } - public void SendMessage(string message) - { - SendMessage(message, 0); - } - /// /// Send a message in chat. /// - public void SendMessage(string message, long playerId, string author = "Server", string font = MyFontEnum.Red) + public void SendMessage(string message, string author = "Server", long playerId = 0, string font = MyFontEnum.Red) { var msg = new ScriptedChatMsg {Author = author, Font = font, Target = playerId, Text = message}; MyMultiplayerBase.SendScriptedChatMessage(ref msg); diff --git a/Torch/TorchBase.cs b/Torch/TorchBase.cs index 15cc773..a244835 100644 --- a/Torch/TorchBase.cs +++ b/Torch/TorchBase.cs @@ -10,6 +10,8 @@ using System.Threading.Tasks; using NLog; using Sandbox; using Sandbox.Game; +using Sandbox.Game.Multiplayer; +using Sandbox.Game.Screens.Helpers; using Sandbox.Game.World; using Sandbox.ModAPI; using SpaceEngineers.Game; @@ -23,10 +25,9 @@ namespace Torch public abstract class TorchBase : ITorchBase { /// - /// Dirty hack because *keen* - /// Use only if absolutely necessary. + /// Hack because *keen*. + /// Use only if necessary, prefer dependency injection. /// - [Obsolete] public static ITorchBase Instance { get; private set; } protected static Logger Log { get; } = LogManager.GetLogger("Torch"); public Version TorchVersion { get; protected set; } @@ -54,6 +55,37 @@ namespace Torch Multiplayer = new MultiplayerManager(this); } + public async Task SaveGameAsync() + { + Log.Info("Saving game"); + if (MySandboxGame.IsGameReady && !MyAsyncSaving.InProgress && Sync.IsServer && !(MySession.Static.LocalCharacter?.IsDead ?? true)) + { + using (var e = new AutoResetEvent(false)) + { + MyAsyncSaving.Start(() => + { + MySector.ResetEyeAdaptation = true; + e.Set(); + }); + + await Task.Run(() => + { + if (!e.WaitOne(60000)) + { + Log.Error("Save failed!"); + Multiplayer.SendMessage("Save timed out!", author: "Error"); + } + }); + } + } + else + { + Log.Error("Cannot save"); + } + } + + #region Game Actions + /// /// Invokes an action on the game thread. /// @@ -117,6 +149,8 @@ namespace Torch throw new TimeoutException("The game action timed out."); } + #endregion + public virtual void Init() { Debug.Assert(!_init, "Torch instance is already initialized.");