Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Nitrox.Launcher/Models/Design/ServerEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,21 @@ public void Start(string savesDir, int existingProcessId = 0, Action? onExited =
if (Process != null)
{
Process.PlayerCountChanged += count => Dispatcher.UIThread.Post(() => Players = count);
Process.RestartRequested += () => Dispatcher.UIThread.Post(() => _ = RestartAsync(savesDir, onExited));
}

IsNewServer = false;
IsOnline = true;
}

private async Task RestartAsync(string savesDir, Action? onExited)
{
Log.Info($"Restarting server '{Name}'...");
await StopAsync();
Start(savesDir, onExited: onExited);
Log.Info($"Server '{Name}' restarted successfully");
}

[RelayCommand(AllowConcurrentExecutions = false)]
public async Task StopAsync()
{
Expand Down Expand Up @@ -346,6 +355,12 @@ private ServerProcess(string saveDir, Action onExited, bool isEmbeddedMode = fal
return;
}

if (output == Ipc.Messages.RestartMessage)
{
RestartRequested?.Invoke();
return;
}

// Ignore any messages that are part of the IPC message protocol
if (Ipc.Messages.AllMessages.Any(msg => output.StartsWith($"{msg}:", StringComparison.Ordinal)))
{
Expand Down Expand Up @@ -396,6 +411,7 @@ private ServerProcess(string saveDir, Action onExited, bool isEmbeddedMode = fal

public static ServerProcess Start(string saveDir, Action onExited, bool isEmbedded, int processId) => new(saveDir, onExited, isEmbedded, processId);
public event Action<int>? PlayerCountChanged;
public event Action? RestartRequested;

/// <summary>
/// Tries to close the server gracefully with a timeout of 7 seconds. If it fails, returns false.
Expand Down
3 changes: 2 additions & 1 deletion Nitrox.Model/Helper/Ipc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public static class Messages
public static string StopMessage => "__SERVER_STOPPED__";
public static string SaveNameMessage => "__SAVE_NAME__";
public static string PlayerCountMessage => "__PLAYER_COUNT__";
public static string RestartMessage => "__SERVER_RESTART__";

public static List<string> AllMessages { get; } = [StopMessage, SaveNameMessage, PlayerCountMessage];
public static List<string> AllMessages { get; } = [StopMessage, SaveNameMessage, PlayerCountMessage, RestartMessage];
}

public sealed class ServerIpc : IDisposable
Expand Down
30 changes: 30 additions & 0 deletions Nitrox.Server.Subnautica/Models/Commands/RestartCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using Nitrox.Model.DataStructures.GameLogic;
using Nitrox.Model.Helper;
using Nitrox.Server.Subnautica.Models.Commands.Abstract;

namespace Nitrox.Server.Subnautica.Models.Commands;

internal class RestartCommand : Command
{
public override IEnumerable<string> Aliases { get; } = ["reboot"];

public RestartCommand() : base("restart", Perms.ADMIN, "Saves and restarts the server")
{
}

protected override void Execute(CallArgs args)
{
Server server = Server.Instance;
if (!server.IsRunning)
{
SendMessage(args.Sender, "Server is not running");
return;
}

SendMessageToAllPlayers("Server is restarting...");
Log.Info("Server restart requested via command");

server.RequestRestart();
}
}
4 changes: 4 additions & 0 deletions Nitrox.Server.Subnautica/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ private static async Task StartServer(string[] args)
{
_ = ipc.SendOutput($"{Ipc.Messages.PlayerCountMessage}:[{count}]");
};
server.RestartRequested += () =>
{
_ = ipc.SendOutput(Ipc.Messages.RestartMessage);
};
string serverSaveName = Server.GetSaveName(args);
Log.SaveName = serverSaveName;

Expand Down
11 changes: 11 additions & 0 deletions Nitrox.Server.Subnautica/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class Server
public int Port => serverConfig?.ServerPort ?? -1;

public event Action<int>? PlayerCountChanged;
public event Action? RestartRequested;

public Server(WorldPersistence worldPersistence, World world, SubnauticaServerConfig serverConfig, Models.Communication.NitroxServer server, WorldEntityManager worldEntityManager, EntityRegistry entityRegistry)
{
Expand Down Expand Up @@ -339,6 +340,16 @@ public void ResumeServer()
Log.Info("Server has resumed");
}

public void RequestRestart()
{
if (!IsRunning)
{
return;
}
Log.Info("Server restart requested");
RestartRequested?.Invoke();
}

private static List<ServerListing> GetSaves()
{
try
Expand Down
Loading