From 6fbc06081e39d9e8dc481cbfa1b2710ce58a0b4d Mon Sep 17 00:00:00 2001 From: John Gross Date: Wed, 24 Jan 2018 12:48:01 -0800 Subject: [PATCH] Add block limit editor, various UI tweaks --- Torch.Server/FlowDocumentTarget.cs | 3 + Torch.Server/Initializer.cs | 31 ++-- Torch.Server/TorchServer.cs | 166 ++++++++---------- Torch.Server/Views/ConfigControl.xaml | 125 ++++++------- .../Views/Entities/Blocks/BlockView.xaml | 4 +- Torch.Server/Views/EntitiesControl.xaml | 5 +- Torch.Server/Views/TorchUI.xaml | 9 +- Torch.Server/Views/TorchUI.xaml.cs | 6 +- Torch/Torch.csproj | 7 + Torch/Views/DictionaryEditor.xaml | 32 ++++ Torch/Views/DictionaryEditor.xaml.cs | 109 ++++++++++++ Torch/Views/PropertyGrid.xaml.cs | 39 +++- 12 files changed, 346 insertions(+), 190 deletions(-) create mode 100644 Torch/Views/DictionaryEditor.xaml create mode 100644 Torch/Views/DictionaryEditor.xaml.cs diff --git a/Torch.Server/FlowDocumentTarget.cs b/Torch.Server/FlowDocumentTarget.cs index daef655..353dcf7 100644 --- a/Torch.Server/FlowDocumentTarget.cs +++ b/Torch.Server/FlowDocumentTarget.cs @@ -10,6 +10,9 @@ using NLog.Targets; namespace Torch.Server { + /// + /// NLog target that writes to a . + /// [Target("flowDocument")] public sealed class FlowDocumentTarget : TargetWithLayout { diff --git a/Torch.Server/Initializer.cs b/Torch.Server/Initializer.cs index d3f9205..1a36e4c 100644 --- a/Torch.Server/Initializer.cs +++ b/Torch.Server/Initializer.cs @@ -86,30 +86,21 @@ quit"; public void Run() { _server = new TorchServer(_config); - try + var init = Task.Run(() => _server.Init()); + if (!_config.NoGui) { - var init = Task.Run(() => _server.Init()); - if (!_config.NoGui) - { - if (_config.Autostart) - init.ContinueWith(x => _server.Start()); + if (_config.Autostart) + init.ContinueWith(x => _server.Start()); - Log.Info("Showing UI"); - Console.SetOut(TextWriter.Null); - NativeMethods.FreeConsole(); - new TorchUI(_server).ShowDialog(); - } - else - { - init.Wait(); - _server.Start(); - } + Log.Info("Showing UI"); + Console.SetOut(TextWriter.Null); + NativeMethods.FreeConsole(); + new TorchUI(_server).ShowDialog(); } - catch + else { - if (_server.IsRunning) - _server.Stop(); - _server.Destroy(); + init.Wait(); + _server.Start(); } } diff --git a/Torch.Server/TorchServer.cs b/Torch.Server/TorchServer.cs index 0e76378..fe67436 100644 --- a/Torch.Server/TorchServer.cs +++ b/Torch.Server/TorchServer.cs @@ -1,20 +1,31 @@ -using NLog; -using Sandbox; -using Sandbox.Game.Multiplayer; -using Sandbox.Game.World; +#region + using System; using System.Collections.Generic; using System.Diagnostics; +using System.Net; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Windows.Forms; +using NLog; +using Sandbox; +using Sandbox.Engine.Networking; +using Sandbox.Game.Multiplayer; +using Sandbox.Game.World; using Torch.API; using Torch.API.Managers; using Torch.API.Session; using Torch.Server.Managers; using Torch.Utils; -using VRage.Game; +using VRage; +using VRage.Dedicated; +using VRage.GameServices; +using VRage.Steam; +using Timer = System.Threading.Timer; + +#endregion #pragma warning disable 618 @@ -22,76 +33,14 @@ namespace Torch.Server { public class TorchServer : TorchBase, ITorchServer { - //public MyConfigDedicated DedicatedConfig { get; set; } - /// - public float SimulationRatio - { - get => _simRatio; - set - { - _simRatio = value; - OnPropertyChanged(); - } - } - - /// - public TimeSpan ElapsedPlayTime - { - get => _elapsedPlayTime; - set - { - _elapsedPlayTime = value; - OnPropertyChanged(); - } - } - - /// - public Thread GameThread { get; private set; } - - /// - public ServerState State - { - get => _state; - private set - { - _state = value; - OnPropertyChanged(); - } - } - - /// - public bool IsRunning - { - get => _isRunning; - set - { - _isRunning = value; - OnPropertyChanged(); - } - } - private bool _canRun; - public bool CanRun { get => _canRun; set => SetValue(ref _canRun, value); } - - private bool _hasRun; - - public event Action Initialized; - - /// - public InstanceManager DedicatedInstance { get; } - - /// - public string InstanceName => Config?.InstanceName; - - /// - public string InstancePath => Config?.InstancePath; - - private bool _isRunning; - private ServerState _state; private TimeSpan _elapsedPlayTime; + private bool _hasRun; + private bool _isRunning; private float _simRatio; - private Timer _watchdog; + private ServerState _state; private Stopwatch _uptime; + private Timer _watchdog; /// public TorchServer(TorchConfig config = null) @@ -102,22 +51,50 @@ namespace Torch.Server Config = config ?? new TorchConfig(); var sessionManager = Managers.GetManager(); - sessionManager.AddFactory((x) => new MultiplayerManagerDedicated(this)); + sessionManager.AddFactory(x => new MultiplayerManagerDedicated(this)); } - /// + //public MyConfigDedicated DedicatedConfig { get; set; } + /// + public float SimulationRatio { get => _simRatio; set => SetValue(ref _simRatio, value); } + + /// + public TimeSpan ElapsedPlayTime { get => _elapsedPlayTime; set => SetValue(ref _elapsedPlayTime, value); } + + /// + public Thread GameThread { get; private set; } + + /// + public bool IsRunning { get => _isRunning; set => SetValue(ref _isRunning, value); } + + public bool CanRun { get => _canRun; set => SetValue(ref _canRun, value); } + + /// + public InstanceManager DedicatedInstance { get; } + + /// + public string InstanceName => Config?.InstanceName; + + /// protected override uint SteamAppId => 244850; - /// + /// protected override string SteamAppName => "SpaceEngineersDedicated"; + /// + public ServerState State { get => _state; private set => SetValue(ref _state, value); } + + public event Action Initialized; + + /// + public string InstancePath => Config?.InstancePath; + /// public override void Init() { Log.Info("Initializing server"); - Sandbox.Engine.Platform.Game.IsDedicated = true; + MySandboxGame.IsDedicated = true; base.Init(); - Managers.GetManager().SessionStateChanged += OnSessionStateChanged; GetManager().LoadInstance(Config.InstancePath); CanRun = true; @@ -125,15 +102,6 @@ namespace Torch.Server Log.Info($"Initialized server '{Config.InstanceName}' at '{Config.InstancePath}'"); } - private void OnSessionStateChanged(ITorchSession session, TorchSessionState newState) - { - if (newState == TorchSessionState.Unloading || newState == TorchSessionState.Unloaded) - { - _watchdog?.Dispose(); - _watchdog = null; - } - } - /// public override void Start() { @@ -173,7 +141,7 @@ namespace Torch.Server } /// - /// Restart the program. + /// Restart the program. /// public override void Restart() { @@ -200,20 +168,24 @@ namespace Torch.Server } } + private void OnSessionStateChanged(ITorchSession session, TorchSessionState newState) + { + if (newState == TorchSessionState.Unloading || newState == TorchSessionState.Unloaded) + { + _watchdog?.Dispose(); + _watchdog = null; + } + } + /// public override void Init(object gameInstance) { base.Init(gameInstance); var game = gameInstance as MySandboxGame; if (game != null && MySession.Static != null) - { State = ServerState.Running; -// SteamServerAPI.Instance.GameServer.SetKeyValue("SM", "Torch"); - } else - { State = ServerState.Stopped; - } } /// @@ -238,7 +210,7 @@ namespace Torch.Server private static void CheckServerResponding(object state) { var mre = new ManualResetEvent(false); - ((TorchServer) state).Invoke(() => mre.Set()); + ((TorchServer)state).Invoke(() => mre.Set()); if (!mre.WaitOne(TimeSpan.FromSeconds(Instance.Config.TickTimeout))) { #if DEBUG @@ -250,10 +222,8 @@ namespace Torch.Server throw new TimeoutException($"Server watchdog detected that the server was frozen for at least {((TorchServer)state).Config.TickTimeout} seconds."); #endif } - else - { - Log.Debug("Server watchdog responded"); - } + + Log.Debug("Server watchdog responded"); } private static string DumpFrozenThread(Thread thread, int traces = 3, int pause = 5000) @@ -267,6 +237,7 @@ namespace Torch.Server stacks.Add(dump); Thread.Sleep(pause); } + string commonPrefix = StringUtils.CommonSuffix(stacks); // Advance prefix to include the line terminator. commonPrefix = commonPrefix.Substring(commonPrefix.IndexOf('\n') + 1); @@ -280,6 +251,7 @@ namespace Torch.Server result.AppendLine($"Suffix {i}"); result.AppendLine(stacks[i].Substring(0, stacks[i].Length - commonPrefix.Length)); } + return result.ToString(); } @@ -293,6 +265,7 @@ namespace Torch.Server { // ignored } + var stack = new StackTrace(thread, true); try { @@ -302,6 +275,7 @@ namespace Torch.Server { // ignored } + return stack; } diff --git a/Torch.Server/Views/ConfigControl.xaml b/Torch.Server/Views/ConfigControl.xaml index 22fa24b..06ee641 100644 --- a/Torch.Server/Views/ConfigControl.xaml +++ b/Torch.Server/Views/ConfigControl.xaml @@ -13,7 +13,7 @@ - + @@ -38,8 +38,8 @@