diff --git a/Torch.API/ITorchBase.cs b/Torch.API/ITorchBase.cs index 254ee90..ba766c5 100644 --- a/Torch.API/ITorchBase.cs +++ b/Torch.API/ITorchBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using Torch.API.Managers; @@ -65,18 +66,18 @@ namespace Torch.API /// /// Invoke an action on the game thread. /// - void Invoke(Action action); + void Invoke(Action action, [CallerMemberName] string caller = ""); /// /// Invoke an action on the game thread and block until it has completed. /// If this is called on the game thread the action will execute immediately. /// - void InvokeBlocking(Action action); + void InvokeBlocking(Action action, [CallerMemberName] string caller = ""); /// /// Invoke an action on the game thread asynchronously. /// - Task InvokeAsync(Action action); + Task InvokeAsync(Action action, [CallerMemberName] string caller = ""); /// /// Start the Torch instance. diff --git a/Torch.Server/Initializer.cs b/Torch.Server/Initializer.cs index 7400c54..b025727 100644 --- a/Torch.Server/Initializer.cs +++ b/Torch.Server/Initializer.cs @@ -183,14 +183,14 @@ quit"; LogException(ex); Console.WriteLine("Exiting in 5 seconds."); Thread.Sleep(5000); + LogManager.Flush(); if (_config.RestartOnCrash) { var exe = typeof(Program).Assembly.Location; _config.WaitForPID = Process.GetCurrentProcess().Id.ToString(); Process.Start(exe, _config.ToString()); } - //1627 = Function failed during execution. - Environment.Exit(1627); + Process.GetCurrentProcess().Kill(); } } } diff --git a/Torch.Server/TorchServer.cs b/Torch.Server/TorchServer.cs index 3d2eaf5..81bf533 100644 --- a/Torch.Server/TorchServer.cs +++ b/Torch.Server/TorchServer.cs @@ -14,6 +14,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.Xml.Serialization.GeneratedAssembly; +using NLog; using Sandbox.Engine.Analytics; using Sandbox.Game.Multiplayer; using Sandbox.ModAPI; @@ -297,9 +298,11 @@ namespace Torch.Server MySandboxGame.Static.Exit(); Log.Info("Server stopped."); + LogManager.Flush(); _stopHandle.Set(); State = ServerState.Stopped; IsRunning = false; + Process.GetCurrentProcess().Kill(); } /// @@ -309,9 +312,12 @@ namespace Torch.Server { var exe = Assembly.GetExecutingAssembly().Location; ((TorchConfig)Config).WaitForPID = Process.GetCurrentProcess().Id.ToString(); + Config.Autostart = true; Process.Start(exe, Config.ToString()); + Save(0).Wait(); Stop(); - Environment.Exit(0); + LogManager.Flush(); + Process.GetCurrentProcess().Kill(); } /// diff --git a/Torch/Commands/TorchCommands.cs b/Torch/Commands/TorchCommands.cs index cdc7fd8..0b7b24e 100644 --- a/Torch/Commands/TorchCommands.cs +++ b/Torch/Commands/TorchCommands.cs @@ -151,7 +151,6 @@ namespace Torch.Commands { Context.Torch.Invoke(() => { - Context.Torch.Save(0).Wait(); Context.Torch.Restart(); }); yield break; diff --git a/Torch/TorchBase.cs b/Torch/TorchBase.cs index 013879d..0d93ac7 100644 --- a/Torch/TorchBase.cs +++ b/Torch/TorchBase.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using NLog; +using ProtoBuf.Meta; using Sandbox; using Sandbox.Engine.Multiplayer; using Sandbox.Game; @@ -191,16 +192,18 @@ namespace Torch /// Invokes an action on the game thread. /// /// - public void Invoke(Action action) + [MethodImpl(MethodImplOptions.NoInlining)] + public void Invoke(Action action, [CallerMemberName] string caller = "") { - MySandboxGame.Static.Invoke(action, "Torch"); + MySandboxGame.Static.Invoke(action, caller); } /// /// Invokes an action on the game thread asynchronously. /// /// - public Task InvokeAsync(Action action) + [MethodImpl(MethodImplOptions.NoInlining)] + public Task InvokeAsync(Action action, [CallerMemberName] string caller = "") { if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread) { @@ -209,14 +212,15 @@ namespace Torch return Task.CompletedTask; } - return Task.Run(() => InvokeBlocking(action)); + return Task.Run(() => InvokeBlocking(action, caller)); } /// /// Invokes an action on the game thread and blocks until it is completed. /// /// - public void InvokeBlocking(Action action) + [MethodImpl(MethodImplOptions.NoInlining)] + public void InvokeBlocking(Action action, [CallerMemberName] string caller = "") { if (action == null) return; @@ -240,7 +244,7 @@ namespace Torch { e.Set(); } - }, "Torch"); + }, caller); if (!e.WaitOne(60000)) throw new TimeoutException("The game action timed out.");