Adjust behavior of restarts. Closes #337

This commit is contained in:
Brant Martin
2019-08-25 11:55:14 -04:00
parent ef444730b7
commit 997a3ca31c
4 changed files with 16 additions and 9 deletions

View File

@@ -104,7 +104,7 @@ namespace Torch.API
/// <summary> /// <summary>
/// Restart the Torch instance, blocking until the restart has been performed. /// Restart the Torch instance, blocking until the restart has been performed.
/// </summary> /// </summary>
void Restart(); void Restart(bool save = true);
/// <summary> /// <summary>
/// Initializes a save of the game. /// Initializes a save of the game.

View File

@@ -152,14 +152,15 @@ namespace Torch.Server
/// <summary> /// <summary>
/// Restart the program. /// Restart the program.
/// </summary> /// </summary>
public override void Restart() public override void Restart(bool save = true)
{ {
if (Config.DisconnectOnRestart) if (Config.DisconnectOnRestart)
{ {
ModCommunication.SendMessageToClients(new JoinServerMessage("0.0.0.0:25555")); ModCommunication.SendMessageToClients(new JoinServerMessage("0.0.0.0:25555"));
Log.Info("Ejected all players from server for restart.");
} }
if (IsRunning) if (IsRunning && save)
Save().ContinueWith(DoRestart, this, TaskContinuationOptions.RunContinuationsAsynchronously); Save().ContinueWith(DoRestart, this, TaskContinuationOptions.RunContinuationsAsynchronously);
else else
DoRestart(null, this); DoRestart(null, this);

View File

@@ -8,6 +8,7 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers; using System.Timers;
using NLog;
using Sandbox.Game.Multiplayer; using Sandbox.Game.Multiplayer;
using Sandbox.ModAPI; using Sandbox.ModAPI;
using Steamworks; using Steamworks;
@@ -28,6 +29,7 @@ namespace Torch.Commands
{ {
private static bool _restartPending = false; private static bool _restartPending = false;
private static bool _cancelRestart = false; private static bool _cancelRestart = false;
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
[Command("whatsmyip")] [Command("whatsmyip")]
[Permission(MyPromoteLevel.None)] [Permission(MyPromoteLevel.None)]
@@ -176,6 +178,7 @@ namespace Torch.Commands
} }
_restartPending = true; _restartPending = true;
Task.Run(() => Task.Run(() =>
{ {
var countdown = RestartCountdown(countdownSeconds, save).GetEnumerator(); var countdown = RestartCountdown(countdownSeconds, save).GetEnumerator();
@@ -232,15 +235,18 @@ namespace Torch.Commands
else else
{ {
if (save) if (save)
Context.Torch.Save().ContinueWith(x => Restart()); {
else Log.Info("Savin game before restart.");
Restart(); Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Saving game before restart.");
}
Log.Info("Restarting server.");
Context.Torch.Invoke(() => Context.Torch.Restart(save));
yield break; yield break;
} }
} }
void Restart() => Context.Torch.Invoke(() => Context.Torch.Restart());
} }
private string Pluralize(int num) private string Pluralize(int num)

View File

@@ -427,7 +427,7 @@ namespace Torch
} }
/// <inheritdoc /> /// <inheritdoc />
public abstract void Restart(); public abstract void Restart(bool save = true);
/// <inheritdoc /> /// <inheritdoc />
public virtual void Init(object gameInstance) public virtual void Init(object gameInstance)