From 1fcfe6fb5f7c855ae21d1a04342fedac134908e1 Mon Sep 17 00:00:00 2001 From: John Gross Date: Sat, 22 Jul 2017 23:11:16 -0700 Subject: [PATCH] Refactor instance management, assorted bugfixes/tweaks --- Torch.API/ITorchConfig.cs | 45 ++--- Torch.API/Torch.API.csproj | 2 +- Torch.Client/Properties/AssemblyInfo.cs | 4 +- Torch.Server/Managers/ConfigManager.cs | 64 ------- Torch.Server/Managers/InstanceManager.cs | 158 ++++++++++++++++++ Torch.Server/Properties/AssemblyInfo.cs | 4 +- Torch.Server/Torch.Server.csproj | 6 +- Torch.Server/TorchConfig.cs | 34 ++-- Torch.Server/TorchServer.cs | 30 +--- .../ViewModels/ConfigDedicatedViewModel.cs | 4 +- Torch.Server/Views/ConfigControl.xaml.cs | 96 +---------- Torch.Server/Views/TorchUI.xaml | 4 +- Torch.Server/Views/TorchUI.xaml.cs | 11 +- Torch/Commands/TorchCommands.cs | 3 +- Torch/Managers/PluginManager.cs | 4 +- Torch/Managers/UpdateManager.cs | 3 +- Torch/Persistent.cs | 21 +-- Torch/TorchBase.cs | 2 +- 18 files changed, 238 insertions(+), 257 deletions(-) delete mode 100644 Torch.Server/Managers/ConfigManager.cs create mode 100644 Torch.Server/Managers/InstanceManager.cs diff --git a/Torch.API/ITorchConfig.cs b/Torch.API/ITorchConfig.cs index 574d837..2b41621 100644 --- a/Torch.API/ITorchConfig.cs +++ b/Torch.API/ITorchConfig.cs @@ -4,44 +4,21 @@ namespace Torch { public interface ITorchConfig { - /// - /// (server) Name of the instance. - /// - string InstanceName { get; set; } - - /// - /// (server) Dedicated instance path. - /// - string InstancePath { get; set; } - - /// - /// Enable automatic Torch updates. - /// - bool GetTorchUpdates { get; set; } - - /// - /// Enable automatic Torch updates. - /// + bool Autostart { get; set; } + bool ForceUpdate { get; set; } bool GetPluginUpdates { get; set; } - - /// - /// Restart Torch automatically if it crashes. - /// + bool GetTorchUpdates { get; set; } + string InstanceName { get; set; } + string InstancePath { get; set; } + bool NoGui { get; set; } + bool NoUpdate { get; set; } + List Plugins { get; set; } bool RestartOnCrash { get; set; } - - /// - /// Time-out in seconds for the Torch watchdog (to detect a hung session). - /// + bool ShouldUpdatePlugins { get; } + bool ShouldUpdateTorch { get; } int TickTimeout { get; set; } + string WaitForPID { get; set; } - /// - /// A list of plugins that should be installed. - /// - List Plugins { get; } - - /// - /// Saves the config. - /// bool Save(string path = null); } } \ No newline at end of file diff --git a/Torch.API/Torch.API.csproj b/Torch.API/Torch.API.csproj index 60f537f..c10adf2 100644 --- a/Torch.API/Torch.API.csproj +++ b/Torch.API/Torch.API.csproj @@ -158,12 +158,12 @@ + - diff --git a/Torch.Client/Properties/AssemblyInfo.cs b/Torch.Client/Properties/AssemblyInfo.cs index 9cc9553..cba9e1a 100644 --- a/Torch.Client/Properties/AssemblyInfo.cs +++ b/Torch.Client/Properties/AssemblyInfo.cs @@ -1,4 +1,4 @@ using System.Reflection; -[assembly: AssemblyVersion("1.0.198.562")] -[assembly: AssemblyFileVersion("1.0.198.562")] \ No newline at end of file +[assembly: AssemblyVersion("1.0.203.595")] +[assembly: AssemblyFileVersion("1.0.203.595")] \ No newline at end of file diff --git a/Torch.Server/Managers/ConfigManager.cs b/Torch.Server/Managers/ConfigManager.cs deleted file mode 100644 index 98515bc..0000000 --- a/Torch.Server/Managers/ConfigManager.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Sandbox.Engine.Utils; -using Torch.API; -using Torch.API.Managers; -using Torch.Managers; -using Torch.Server.ViewModels; -using VRage.Game; - -namespace Torch.Server.Managers -{ - //TODO - public class ConfigManager : Manager - { - private const string CONFIG_NAME = "SpaceEngineers-Dedicated.cfg"; - public ConfigDedicatedViewModel DedicatedConfig { get; set; } - public TorchConfig TorchConfig { get; set; } - - public ConfigManager(ITorchBase torchInstance) : base(torchInstance) - { - - } - - /// - public override void Init() - { - LoadInstance(Torch.Config.InstancePath); - } - - public void LoadInstance(string path) - { - if (!Directory.Exists(path)) - throw new FileNotFoundException($"Instance directory not found at '{path}'"); - - var configPath = Path.Combine(path, CONFIG_NAME); - var config = new MyConfigDedicated(configPath); - config.Load(); - DedicatedConfig = new ConfigDedicatedViewModel(config); - } - - /// - /// Creates a skeleton of a DS instance folder at the given directory. - /// - /// - public void CreateInstance(string path) - { - if (Directory.Exists(path)) - return; - - Directory.CreateDirectory(path); - var savesPath = Path.Combine(path, "Saves"); - Directory.CreateDirectory(savesPath); - var modsPath = Path.Combine(path, "Mods"); - Directory.CreateDirectory(modsPath); - var configPath = Path.Combine(path, "SpaceEngineers-Dedicated.cfg"); - new MyConfigDedicated(configPath).Save(); - LoadInstance(path); - } - } -} diff --git a/Torch.Server/Managers/InstanceManager.cs b/Torch.Server/Managers/InstanceManager.cs new file mode 100644 index 0000000..4767d49 --- /dev/null +++ b/Torch.Server/Managers/InstanceManager.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Havok; +using NLog; +using Sandbox.Engine.Networking; +using Sandbox.Engine.Utils; +using Torch.API; +using Torch.API.Managers; +using Torch.Managers; +using Torch.Server.ViewModels; +using VRage.FileSystem; +using VRage.Game; +using VRage.ObjectBuilders; + +namespace Torch.Server.Managers +{ + public class InstanceManager : Manager + { + private const string CONFIG_NAME = "SpaceEngineers-Dedicated.cfg"; + public ConfigDedicatedViewModel DedicatedConfig { get; set; } + private static readonly Logger Log = LogManager.GetLogger(nameof(InstanceManager)); + + public InstanceManager(ITorchBase torchInstance) : base(torchInstance) + { + + } + + /// + public override void Init() + { + LoadInstance(Torch.Config.InstancePath); + } + + public void LoadInstance(string path, bool validate = true) + { + if (validate) + ValidateInstance(path); + + MyFileSystem.Reset(); + MyFileSystem.ExePath = Path.Combine(Torch.GetManager().TorchDirectory, "DedicatedServer64"); + MyFileSystem.Init("Content", path); + + var configPath = Path.Combine(path, CONFIG_NAME); + if (!File.Exists(configPath)) + { + Log.Error($"Failed to load dedicated config at {path}"); + return; + } + + var config = new MyConfigDedicated(configPath); + config.Load(configPath); + + DedicatedConfig = new ConfigDedicatedViewModel(config); + var worldFolders = Directory.EnumerateDirectories(Path.Combine(Torch.Config.InstancePath, "Saves")); + + foreach (var f in worldFolders) + DedicatedConfig.WorldPaths.Add(f); + + if (DedicatedConfig.WorldPaths.Count == 0) + { + Log.Warn($"No worlds found in the current instance {path}."); + return; + } + + /* + if (string.IsNullOrEmpty(DedicatedConfig.LoadWorld)) + { + Log.Warn("No world specified, importing first available world."); + SelectWorld(DedicatedConfig.WorldPaths[0], false); + }*/ + } + + public void SelectWorld(string worldPath, bool modsOnly = true) + { + DedicatedConfig.LoadWorld = worldPath; + LoadWorldMods(modsOnly); + } + + + private void LoadWorldMods(bool modsOnly = true) + { + if (DedicatedConfig.LoadWorld == null) + return; + + var sandboxPath = Path.Combine(DedicatedConfig.LoadWorld, "Sandbox.sbc"); + + if (!File.Exists(sandboxPath)) + return; + + MyObjectBuilderSerializer.DeserializeXML(sandboxPath, out MyObjectBuilder_Checkpoint checkpoint, out ulong sizeInBytes); + if (checkpoint == null) + { + Log.Error($"Failed to load {DedicatedConfig.LoadWorld}, checkpoint null ({sizeInBytes} bytes, instance {TorchBase.Instance.Config.InstancePath})"); + return; + } + + var sb = new StringBuilder(); + foreach (var mod in checkpoint.Mods) + sb.AppendLine(mod.PublishedFileId.ToString()); + + DedicatedConfig.Mods = sb.ToString(); + + Log.Info("Loaded mod list from world"); + + if (!modsOnly) + DedicatedConfig.SessionSettings = new SessionSettingsViewModel(checkpoint.Settings); + } + + public void SaveConfig() + { + DedicatedConfig.Model.Save(); + Log.Info("Saved dedicated config."); + + try + { + MyObjectBuilderSerializer.DeserializeXML(Path.Combine(DedicatedConfig.LoadWorld, "Sandbox.sbc"), out MyObjectBuilder_Checkpoint checkpoint, out ulong sizeInBytes); + if (checkpoint == null) + { + Log.Error($"Failed to load {DedicatedConfig.LoadWorld}, checkpoint null ({sizeInBytes} bytes, instance {TorchBase.Instance.Config.InstancePath})"); + return; + } + checkpoint.Settings = DedicatedConfig.SessionSettings; + checkpoint.Mods.Clear(); + foreach (var modId in DedicatedConfig.Model.Mods) + checkpoint.Mods.Add(new MyObjectBuilder_Checkpoint.ModItem(modId)); + + MyLocalCache.SaveCheckpoint(checkpoint, DedicatedConfig.LoadWorld); + Log.Info("Saved world config."); + } + catch (Exception e) + { + Log.Error("Failed to write sandbox config, changes will not appear on server"); + Log.Error(e); + } + } + + /// + /// Ensures that the given path is a valid server instance. + /// + private void ValidateInstance(string path) + { + Directory.CreateDirectory(Path.Combine(path, "Saves")); + Directory.CreateDirectory(Path.Combine(path, "Mods")); + var configPath = Path.Combine(path, CONFIG_NAME); + if (File.Exists(configPath)) + return; + + var config = new MyConfigDedicated(configPath); + config.Save(configPath); + } + } +} diff --git a/Torch.Server/Properties/AssemblyInfo.cs b/Torch.Server/Properties/AssemblyInfo.cs index f1ea321..e61981a 100644 --- a/Torch.Server/Properties/AssemblyInfo.cs +++ b/Torch.Server/Properties/AssemblyInfo.cs @@ -1,4 +1,4 @@ using System.Reflection; -[assembly: AssemblyVersion("1.1.198.562")] -[assembly: AssemblyFileVersion("1.1.198.562")] \ No newline at end of file +[assembly: AssemblyVersion("1.1.203.596")] +[assembly: AssemblyFileVersion("1.1.203.596")] \ No newline at end of file diff --git a/Torch.Server/Torch.Server.csproj b/Torch.Server/Torch.Server.csproj index fd2018f..34742d8 100644 --- a/Torch.Server/Torch.Server.csproj +++ b/Torch.Server/Torch.Server.csproj @@ -185,7 +185,7 @@ - + True @@ -366,7 +366,9 @@ - copy "$(SolutionDir)NLog.config" "$(TargetDir)" + cd "$(TargetDir)" +copy "$(SolutionDir)NLog.config" "$(TargetDir)" +"Torch Server Release.bat"