Provide fallback for /status API if content does not override it.

This commit is contained in:
Pieter-Jan Briers
2020-12-19 00:43:28 +01:00
parent 74eb8e3e8d
commit fdcfdffc0b
2 changed files with 17 additions and 8 deletions

View File

@@ -38,13 +38,6 @@ namespace Robust.Server.ServerStatus
return false;
}
if (OnStatusRequest == null)
{
_httpSawmill.Warning("OnStatusRequest is not set, responding with a 501.");
response.Respond(method, "Not Implemented", HttpStatusCode.NotImplemented);
return true;
}
response.StatusCode = (int) HttpStatusCode.OK;
response.ContentType = "application/json";
@@ -53,7 +46,13 @@ namespace Robust.Server.ServerStatus
return true;
}
var jObject = new JObject();
var jObject = new JObject
{
// We need to send at LEAST name and player count to have the launcher work with us.
// Content can override these if it wants (e.g. stealthmins).
["name"] = _serverNameCache,
["players"] = _playerManager.PlayerCount
};
OnStatusRequest?.Invoke(jObject);

View File

@@ -7,6 +7,8 @@ using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Robust.Server.Interfaces;
using Robust.Server.Interfaces.Player;
using Robust.Server.Interfaces.ServerStatus;
using Robust.Shared;
using Robust.Shared.Configuration;
@@ -29,6 +31,8 @@ namespace Robust.Server.ServerStatus
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] private readonly IServerNetManager _netManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IBaseServer _baseServer = default!;
private static readonly JsonSerializer JsonSerializer = new();
private readonly List<StatusHostHandler> _handlers = new();
@@ -36,6 +40,8 @@ namespace Robust.Server.ServerStatus
private TaskCompletionSource? _stopSource;
private ISawmill _httpSawmill = default!;
private string? _serverNameCache;
public Task ProcessRequestAsync(HttpListenerContext context)
{
var response = context.Response;
@@ -86,6 +92,10 @@ namespace Robust.Server.ServerStatus
_httpSawmill = Logger.GetSawmill($"{Sawmill}.http");
RegisterCVars();
// Cache this in a field to avoid thread safety shenanigans.
// Writes/reads of references are atomic in C# so no further synchronization necessary.
_configurationManager.OnValueChanged(CVars.GameHostName, n => _serverNameCache = n);
if (!_configurationManager.GetCVar(CVars.StatusEnabled))
{
return;