mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-11-08 06:04:08 +01:00
Compare commits
3 Commits
wip-forge
...
25d0e2b0d1
| Author | SHA1 | Date | |
|---|---|---|---|
|
25d0e2b0d1
|
|||
|
792f633a09
|
|||
|
04c3a53f95
|
@@ -1,5 +1,4 @@
|
|||||||
using System.Collections.ObjectModel;
|
using System.Text;
|
||||||
using System.Text;
|
|
||||||
using Phantom.Agent.Minecraft.Instance;
|
using Phantom.Agent.Minecraft.Instance;
|
||||||
using Phantom.Agent.Minecraft.Java;
|
using Phantom.Agent.Minecraft.Java;
|
||||||
using Phantom.Agent.Minecraft.Server;
|
using Phantom.Agent.Minecraft.Server;
|
||||||
@@ -12,7 +11,6 @@ public abstract class BaseLauncher : IServerLauncher {
|
|||||||
private readonly InstanceProperties instanceProperties;
|
private readonly InstanceProperties instanceProperties;
|
||||||
|
|
||||||
protected string MinecraftVersion => instanceProperties.ServerVersion;
|
protected string MinecraftVersion => instanceProperties.ServerVersion;
|
||||||
protected string InstanceFolder => instanceProperties.InstanceFolder;
|
|
||||||
|
|
||||||
private protected BaseLauncher(InstanceProperties instanceProperties) {
|
private protected BaseLauncher(InstanceProperties instanceProperties) {
|
||||||
this.instanceProperties = instanceProperties;
|
this.instanceProperties = instanceProperties;
|
||||||
@@ -53,14 +51,16 @@ public abstract class BaseLauncher : IServerLauncher {
|
|||||||
|
|
||||||
var processConfigurator = new ProcessConfigurator {
|
var processConfigurator = new ProcessConfigurator {
|
||||||
FileName = javaRuntimeExecutable.ExecutablePath,
|
FileName = javaRuntimeExecutable.ExecutablePath,
|
||||||
WorkingDirectory = InstanceFolder,
|
WorkingDirectory = instanceProperties.InstanceFolder,
|
||||||
RedirectInput = true,
|
RedirectInput = true,
|
||||||
UseShellExecute = false
|
UseShellExecute = false
|
||||||
};
|
};
|
||||||
|
|
||||||
var processArguments = processConfigurator.ArgumentList;
|
var processArguments = processConfigurator.ArgumentList;
|
||||||
PrepareJvmArguments(serverJar).Build(processArguments);
|
PrepareJvmArguments(serverJar).Build(processArguments);
|
||||||
PrepareJavaProcessArguments(processArguments, serverJar.FilePath);
|
processArguments.Add("-jar");
|
||||||
|
processArguments.Add(serverJar.FilePath);
|
||||||
|
processArguments.Add("nogui");
|
||||||
|
|
||||||
var process = processConfigurator.CreateProcess();
|
var process = processConfigurator.CreateProcess();
|
||||||
var instanceProcess = new InstanceProcess(instanceProperties, process);
|
var instanceProcess = new InstanceProcess(instanceProperties, process);
|
||||||
@@ -99,12 +99,6 @@ public abstract class BaseLauncher : IServerLauncher {
|
|||||||
|
|
||||||
private protected virtual void CustomizeJvmArguments(JvmArgumentBuilder arguments) {}
|
private protected virtual void CustomizeJvmArguments(JvmArgumentBuilder arguments) {}
|
||||||
|
|
||||||
protected virtual void PrepareJavaProcessArguments(Collection<string> processArguments, string serverJarFilePath) {
|
|
||||||
processArguments.Add("-jar");
|
|
||||||
processArguments.Add(serverJarFilePath);
|
|
||||||
processArguments.Add("nogui");
|
|
||||||
}
|
|
||||||
|
|
||||||
private protected virtual Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) {
|
private protected virtual Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) {
|
||||||
return Task.FromResult(new ServerJarInfo(serverJarPath));
|
return Task.FromResult(new ServerJarInfo(serverJarPath));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
using System.Collections.ObjectModel;
|
|
||||||
using Phantom.Agent.Minecraft.Instance;
|
|
||||||
using Phantom.Agent.Minecraft.Java;
|
|
||||||
using Serilog;
|
|
||||||
|
|
||||||
namespace Phantom.Agent.Minecraft.Launcher.Types;
|
|
||||||
|
|
||||||
public sealed class ForgeLauncher : BaseLauncher {
|
|
||||||
public ForgeLauncher(InstanceProperties instanceProperties) : base(instanceProperties) {}
|
|
||||||
|
|
||||||
private protected override void CustomizeJvmArguments(JvmArgumentBuilder arguments) {
|
|
||||||
arguments.AddProperty("terminal.ansi", "true"); // TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void PrepareJavaProcessArguments(Collection<string> processArguments, string serverJarFilePath) {
|
|
||||||
if (OperatingSystem.IsWindows()) {
|
|
||||||
processArguments.Add("@libraries/net/minecraftforge/forge/1.20.1-47.2.0/win_args.txt");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
processArguments.Add("@libraries/net/minecraftforge/forge/1.20.1-47.2.0/unix_args.txt");
|
|
||||||
}
|
|
||||||
|
|
||||||
processArguments.Add("nogui");
|
|
||||||
}
|
|
||||||
|
|
||||||
private protected override Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) {
|
|
||||||
return Task.FromResult(new ServerJarInfo(Path.Combine(InstanceFolder, "run.sh")));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,12 +3,28 @@ using System.Buffers.Binary;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Phantom.Common.Data.Instance;
|
using Phantom.Utils.Logging;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace Phantom.Agent.Minecraft.Server;
|
namespace Phantom.Agent.Minecraft.Server;
|
||||||
|
|
||||||
public static class ServerStatusProtocol {
|
public sealed class ServerStatusProtocol {
|
||||||
public static async Task<InstancePlayerCounts> GetPlayerCounts(ushort serverPort, CancellationToken cancellationToken) {
|
private readonly ILogger logger;
|
||||||
|
|
||||||
|
public ServerStatusProtocol(string loggerName) {
|
||||||
|
this.logger = PhantomLogger.Create<ServerStatusProtocol>(loggerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int?> GetOnlinePlayerCount(int serverPort, CancellationToken cancellationToken) {
|
||||||
|
try {
|
||||||
|
return await GetOnlinePlayerCountOrThrow(serverPort, cancellationToken);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.Error(e, "Caught exception checking online player count.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<int?> GetOnlinePlayerCountOrThrow(int serverPort, CancellationToken cancellationToken) {
|
||||||
using var tcpClient = new TcpClient();
|
using var tcpClient = new TcpClient();
|
||||||
await tcpClient.ConnectAsync(IPAddress.Loopback, serverPort, cancellationToken);
|
await tcpClient.ConnectAsync(IPAddress.Loopback, serverPort, cancellationToken);
|
||||||
var tcpStream = tcpClient.GetStream();
|
var tcpStream = tcpClient.GetStream();
|
||||||
@@ -17,22 +33,24 @@ public static class ServerStatusProtocol {
|
|||||||
tcpStream.WriteByte(0xFE);
|
tcpStream.WriteByte(0xFE);
|
||||||
await tcpStream.FlushAsync(cancellationToken);
|
await tcpStream.FlushAsync(cancellationToken);
|
||||||
|
|
||||||
short messageLength = await ReadStreamHeader(tcpStream, cancellationToken);
|
short? messageLength = await ReadStreamHeader(tcpStream, cancellationToken);
|
||||||
return await ReadPlayerCounts(tcpStream, messageLength * 2, cancellationToken);
|
return messageLength == null ? null : await ReadOnlinePlayerCount(tcpStream, messageLength.Value * 2, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<short> ReadStreamHeader(NetworkStream tcpStream, CancellationToken cancellationToken) {
|
private async Task<short?> ReadStreamHeader(NetworkStream tcpStream, CancellationToken cancellationToken) {
|
||||||
var headerBuffer = ArrayPool<byte>.Shared.Rent(3);
|
var headerBuffer = ArrayPool<byte>.Shared.Rent(3);
|
||||||
try {
|
try {
|
||||||
await tcpStream.ReadExactlyAsync(headerBuffer, 0, 3, cancellationToken);
|
await tcpStream.ReadExactlyAsync(headerBuffer, 0, 3, cancellationToken);
|
||||||
|
|
||||||
if (headerBuffer[0] != 0xFF) {
|
if (headerBuffer[0] != 0xFF) {
|
||||||
throw new ProtocolException("Unexpected first byte in response from server: " + headerBuffer[0]);
|
logger.Error("Unexpected first byte in response from server: {FirstByte}.", headerBuffer[0]);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
short messageLength = BinaryPrimitives.ReadInt16BigEndian(headerBuffer.AsSpan(1));
|
short messageLength = BinaryPrimitives.ReadInt16BigEndian(headerBuffer.AsSpan(1));
|
||||||
if (messageLength <= 0) {
|
if (messageLength <= 0) {
|
||||||
throw new ProtocolException("Unexpected message length in response from server: " + messageLength);
|
logger.Error("Unexpected message length in response from server: {MessageLength}.", messageLength);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return messageLength;
|
return messageLength;
|
||||||
@@ -41,54 +59,35 @@ public static class ServerStatusProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<InstancePlayerCounts> ReadPlayerCounts(NetworkStream tcpStream, int messageLength, CancellationToken cancellationToken) {
|
private async Task<int?> ReadOnlinePlayerCount(NetworkStream tcpStream, int messageLength, CancellationToken cancellationToken) {
|
||||||
var messageBuffer = ArrayPool<byte>.Shared.Rent(messageLength);
|
var messageBuffer = ArrayPool<byte>.Shared.Rent(messageLength);
|
||||||
try {
|
try {
|
||||||
await tcpStream.ReadExactlyAsync(messageBuffer, 0, messageLength, cancellationToken);
|
await tcpStream.ReadExactlyAsync(messageBuffer, 0, messageLength, cancellationToken);
|
||||||
return ReadPlayerCountsFromResponse(messageBuffer.AsSpan(0, messageLength));
|
|
||||||
|
// Valid response separator encoded in UTF-16BE is 0x00 0xA7 (§).
|
||||||
|
const byte SeparatorSecondByte = 0xA7;
|
||||||
|
|
||||||
|
static bool IsValidSeparator(ReadOnlySpan<byte> buffer, int index) {
|
||||||
|
return index > 0 && buffer[index - 1] == 0x00;
|
||||||
|
}
|
||||||
|
|
||||||
|
int separator2 = Array.LastIndexOf(messageBuffer, SeparatorSecondByte);
|
||||||
|
int separator1 = separator2 == -1 ? -1 : Array.LastIndexOf(messageBuffer, SeparatorSecondByte, separator2 - 1);
|
||||||
|
if (!IsValidSeparator(messageBuffer, separator1) || !IsValidSeparator(messageBuffer, separator2)) {
|
||||||
|
logger.Error("Could not find message separators in response from server.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
string onlinePlayerCountStr = Encoding.BigEndianUnicode.GetString(messageBuffer.AsSpan((separator1 + 1)..(separator2 - 1)));
|
||||||
|
if (!int.TryParse(onlinePlayerCountStr, out int onlinePlayerCount)) {
|
||||||
|
logger.Error("Could not parse online player count in response from server: {OnlinePlayerCount}.", onlinePlayerCountStr);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Debug("Detected {OnlinePlayerCount} online player(s).", onlinePlayerCount);
|
||||||
|
return onlinePlayerCount;
|
||||||
} finally {
|
} finally {
|
||||||
ArrayPool<byte>.Shared.Return(messageBuffer);
|
ArrayPool<byte>.Shared.Return(messageBuffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Legacy query protocol uses the paragraph symbol (§) as separator encoded in UTF-16BE.
|
|
||||||
/// </summary>
|
|
||||||
private static readonly byte[] Separator = { 0x00, 0xA7 };
|
|
||||||
|
|
||||||
private static InstancePlayerCounts ReadPlayerCountsFromResponse(ReadOnlySpan<byte> messageBuffer) {
|
|
||||||
int lastSeparator = messageBuffer.LastIndexOf(Separator);
|
|
||||||
int middleSeparator = messageBuffer[..lastSeparator].LastIndexOf(Separator);
|
|
||||||
|
|
||||||
if (lastSeparator == -1 || middleSeparator == -1) {
|
|
||||||
throw new ProtocolException("Could not find message separators in response from server.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var onlinePlayerCountBuffer = messageBuffer[(middleSeparator + Separator.Length)..lastSeparator];
|
|
||||||
var maximumPlayerCountBuffer = messageBuffer[(lastSeparator + Separator.Length)..];
|
|
||||||
|
|
||||||
// Player counts are integers, whose maximum string length is 10 characters.
|
|
||||||
Span<char> integerStringBuffer = stackalloc char[10];
|
|
||||||
|
|
||||||
return new InstancePlayerCounts(
|
|
||||||
DecodeAndParsePlayerCount(onlinePlayerCountBuffer, integerStringBuffer, "online"),
|
|
||||||
DecodeAndParsePlayerCount(maximumPlayerCountBuffer, integerStringBuffer, "maximum")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int DecodeAndParsePlayerCount(ReadOnlySpan<byte> inputBuffer, Span<char> tempCharBuffer, string countType) {
|
|
||||||
if (!Encoding.BigEndianUnicode.TryGetChars(inputBuffer, tempCharBuffer, out int charCount)) {
|
|
||||||
throw new ProtocolException("Could not decode " + countType + " player count in response from server.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!int.TryParse(tempCharBuffer, out int playerCount)) {
|
|
||||||
throw new ProtocolException("Could not parse " + countType + " player count in response from server: " + tempCharBuffer[..charCount].ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return playerCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed class ProtocolException : Exception {
|
|
||||||
internal ProtocolException(string message) : base(message) {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ sealed class BackupArchiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private bool IsFolderSkipped(ImmutableList<string> relativePath) {
|
private bool IsFolderSkipped(ImmutableList<string> relativePath) {
|
||||||
return relativePath is ["cache" or "crash-reports" or "debug" or "libraries" or "logs" or "mods" or "servermods" or "versions"];
|
return relativePath is ["cache" or "crash-reports" or "debug" or "libraries" or "logs" or "mods" or "versions"];
|
||||||
}
|
}
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "ConvertIfStatementToReturnStatement")]
|
[SuppressMessage("ReSharper", "ConvertIfStatementToReturnStatement")]
|
||||||
|
|||||||
@@ -102,7 +102,6 @@ sealed class InstanceManagerActor : ReceiveActor<InstanceManagerActor.ICommand>
|
|||||||
IServerLauncher launcher = configuration.MinecraftServerKind switch {
|
IServerLauncher launcher = configuration.MinecraftServerKind switch {
|
||||||
MinecraftServerKind.Vanilla => new VanillaLauncher(properties),
|
MinecraftServerKind.Vanilla => new VanillaLauncher(properties),
|
||||||
MinecraftServerKind.Fabric => new FabricLauncher(properties),
|
MinecraftServerKind.Fabric => new FabricLauncher(properties),
|
||||||
MinecraftServerKind.Forge => new ForgeLauncher(properties),
|
|
||||||
_ => InvalidLauncher.Instance
|
_ => InvalidLauncher.Instance
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
using Phantom.Agent.Minecraft.Instance;
|
using Phantom.Agent.Minecraft.Instance;
|
||||||
using Phantom.Agent.Minecraft.Server;
|
using Phantom.Agent.Minecraft.Server;
|
||||||
using Phantom.Agent.Rpc;
|
|
||||||
using Phantom.Common.Data.Instance;
|
|
||||||
using Phantom.Common.Messages.Agent.ToController;
|
|
||||||
using Phantom.Utils.Logging;
|
using Phantom.Utils.Logging;
|
||||||
using Phantom.Utils.Tasks;
|
using Phantom.Utils.Tasks;
|
||||||
using Phantom.Utils.Threading;
|
using Phantom.Utils.Threading;
|
||||||
@@ -10,35 +7,34 @@ using Phantom.Utils.Threading;
|
|||||||
namespace Phantom.Agent.Services.Instances.State;
|
namespace Phantom.Agent.Services.Instances.State;
|
||||||
|
|
||||||
sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
||||||
private readonly ControllerConnection controllerConnection;
|
|
||||||
private readonly Guid instanceGuid;
|
|
||||||
private readonly ushort serverPort;
|
|
||||||
private readonly InstanceProcess process;
|
private readonly InstanceProcess process;
|
||||||
|
private readonly ushort serverPort;
|
||||||
|
|
||||||
|
private readonly ServerStatusProtocol serverStatusProtocol;
|
||||||
|
|
||||||
private readonly TaskCompletionSource firstDetection = AsyncTasks.CreateCompletionSource();
|
private readonly TaskCompletionSource firstDetection = AsyncTasks.CreateCompletionSource();
|
||||||
private readonly ManualResetEventSlim serverOutputEvent = new ();
|
private readonly ManualResetEventSlim serverOutputEvent = new ();
|
||||||
|
|
||||||
private InstancePlayerCounts? playerCounts;
|
private int? onlinePlayerCount;
|
||||||
|
|
||||||
public InstancePlayerCounts? PlayerCounts {
|
public int? OnlinePlayerCount {
|
||||||
get {
|
get {
|
||||||
lock (this) {
|
lock (this) {
|
||||||
return playerCounts;
|
return onlinePlayerCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private set {
|
private set {
|
||||||
EventHandler<int?>? onlinePlayerCountChanged;
|
EventHandler<int?>? onlinePlayerCountChanged;
|
||||||
lock (this) {
|
lock (this) {
|
||||||
if (playerCounts == value) {
|
if (onlinePlayerCount == value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
playerCounts = value;
|
onlinePlayerCount = value;
|
||||||
onlinePlayerCountChanged = OnlinePlayerCountChanged;
|
onlinePlayerCountChanged = OnlinePlayerCountChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
onlinePlayerCountChanged?.Invoke(this, value?.Online);
|
onlinePlayerCountChanged?.Invoke(this, value);
|
||||||
controllerConnection.Send(new ReportInstancePlayerCountsMessage(instanceGuid, value));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,10 +43,9 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
private bool isDisposed = false;
|
private bool isDisposed = false;
|
||||||
|
|
||||||
public InstancePlayerCountTracker(InstanceContext context, InstanceProcess process, ushort serverPort) : base(PhantomLogger.Create<InstancePlayerCountTracker>(context.ShortName)) {
|
public InstancePlayerCountTracker(InstanceContext context, InstanceProcess process, ushort serverPort) : base(PhantomLogger.Create<InstancePlayerCountTracker>(context.ShortName)) {
|
||||||
this.controllerConnection = context.Services.ControllerConnection;
|
|
||||||
this.instanceGuid = context.InstanceGuid;
|
|
||||||
this.process = process;
|
this.process = process;
|
||||||
this.serverPort = serverPort;
|
this.serverPort = serverPort;
|
||||||
|
this.serverStatusProtocol = new ServerStatusProtocol(context.ShortName);
|
||||||
Start();
|
Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +59,7 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
while (!CancellationToken.IsCancellationRequested) {
|
while (!CancellationToken.IsCancellationRequested) {
|
||||||
serverOutputEvent.Reset();
|
serverOutputEvent.Reset();
|
||||||
|
|
||||||
PlayerCounts = await TryGetPlayerCounts();
|
OnlinePlayerCount = await serverStatusProtocol.GetOnlinePlayerCount(serverPort, CancellationToken);
|
||||||
|
|
||||||
if (!firstDetection.Task.IsCompleted) {
|
if (!firstDetection.Task.IsCompleted) {
|
||||||
firstDetection.SetResult();
|
firstDetection.SetResult();
|
||||||
@@ -72,21 +67,7 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
|
|
||||||
await Task.Delay(TimeSpan.FromSeconds(10), CancellationToken);
|
await Task.Delay(TimeSpan.FromSeconds(10), CancellationToken);
|
||||||
await serverOutputEvent.WaitHandle.WaitOneAsync(CancellationToken);
|
await serverOutputEvent.WaitHandle.WaitOneAsync(CancellationToken);
|
||||||
await Task.Delay(TimeSpan.FromSeconds(1), CancellationToken);
|
await Task.Delay(TimeSpan.FromSeconds(2), CancellationToken);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<InstancePlayerCounts?> TryGetPlayerCounts() {
|
|
||||||
try {
|
|
||||||
var result = await ServerStatusProtocol.GetPlayerCounts(serverPort, CancellationToken);
|
|
||||||
Logger.Debug("Detected {OnlinePlayerCount} / {MaximumPlayerCount} online player(s).", result.Online, result.Maximum);
|
|
||||||
return result;
|
|
||||||
} catch (ServerStatusProtocol.ProtocolException e) {
|
|
||||||
Logger.Error(e.Message);
|
|
||||||
return null;
|
|
||||||
} catch (Exception e) {
|
|
||||||
Logger.Error(e, "Caught exception while checking online player count.");
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,12 +77,12 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
var onlinePlayersDetected = AsyncTasks.CreateCompletionSource();
|
var onlinePlayersDetected = AsyncTasks.CreateCompletionSource();
|
||||||
|
|
||||||
lock (this) {
|
lock (this) {
|
||||||
if (playerCounts is { Online: > 0 }) {
|
if (onlinePlayerCount == null) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (playerCounts == null) {
|
|
||||||
throw new InvalidOperationException();
|
throw new InvalidOperationException();
|
||||||
}
|
}
|
||||||
|
else if (onlinePlayerCount > 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
OnlinePlayerCountChanged += OnOnlinePlayerCountChanged;
|
OnlinePlayerCountChanged += OnOnlinePlayerCountChanged;
|
||||||
|
|
||||||
@@ -131,7 +112,7 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
protected override void Dispose() {
|
protected override void Dispose() {
|
||||||
lock (this) {
|
lock (this) {
|
||||||
isDisposed = true;
|
isDisposed = true;
|
||||||
playerCounts = null;
|
onlinePlayerCount = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
process.RemoveOutputListener(OnOutput);
|
process.RemoveOutputListener(OnOutput);
|
||||||
|
|||||||
@@ -8,10 +8,9 @@ public sealed partial record Instance(
|
|||||||
[property: MemoryPackOrder(0)] Guid InstanceGuid,
|
[property: MemoryPackOrder(0)] Guid InstanceGuid,
|
||||||
[property: MemoryPackOrder(1)] InstanceConfiguration Configuration,
|
[property: MemoryPackOrder(1)] InstanceConfiguration Configuration,
|
||||||
[property: MemoryPackOrder(2)] IInstanceStatus Status,
|
[property: MemoryPackOrder(2)] IInstanceStatus Status,
|
||||||
[property: MemoryPackOrder(3)] InstancePlayerCounts? PlayerCounts,
|
[property: MemoryPackOrder(3)] bool LaunchAutomatically
|
||||||
[property: MemoryPackOrder(4)] bool LaunchAutomatically
|
|
||||||
) {
|
) {
|
||||||
public static Instance Offline(Guid instanceGuid, InstanceConfiguration configuration, bool launchAutomatically = false) {
|
public static Instance Offline(Guid instanceGuid, InstanceConfiguration configuration, bool launchAutomatically = false) {
|
||||||
return new Instance(instanceGuid, configuration, InstanceStatus.Offline, PlayerCounts: null, launchAutomatically);
|
return new Instance(instanceGuid, configuration, InstanceStatus.Offline, launchAutomatically);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
using MemoryPack;
|
|
||||||
|
|
||||||
namespace Phantom.Common.Data.Instance;
|
|
||||||
|
|
||||||
[MemoryPackable(GenerateType.VersionTolerant)]
|
|
||||||
public readonly partial record struct InstancePlayerCounts(
|
|
||||||
[property: MemoryPackOrder(0)] int Online,
|
|
||||||
[property: MemoryPackOrder(1)] int Maximum
|
|
||||||
);
|
|
||||||
@@ -2,6 +2,5 @@
|
|||||||
|
|
||||||
public enum MinecraftServerKind : ushort {
|
public enum MinecraftServerKind : ushort {
|
||||||
Vanilla = 1,
|
Vanilla = 1,
|
||||||
Fabric = 2,
|
Fabric = 2
|
||||||
Forge = 3
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ public static class AgentMessageRegistries {
|
|||||||
ToController.Add<InstanceOutputMessage>(5);
|
ToController.Add<InstanceOutputMessage>(5);
|
||||||
ToController.Add<ReportAgentStatusMessage>(6);
|
ToController.Add<ReportAgentStatusMessage>(6);
|
||||||
ToController.Add<ReportInstanceEventMessage>(7);
|
ToController.Add<ReportInstanceEventMessage>(7);
|
||||||
ToController.Add<ReportInstancePlayerCountsMessage>(8);
|
|
||||||
ToController.Add<ReplyMessage>(127);
|
ToController.Add<ReplyMessage>(127);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
using MemoryPack;
|
|
||||||
using Phantom.Common.Data.Instance;
|
|
||||||
|
|
||||||
namespace Phantom.Common.Messages.Agent.ToController;
|
|
||||||
|
|
||||||
[MemoryPackable(GenerateType.VersionTolerant)]
|
|
||||||
public sealed partial record ReportInstancePlayerCountsMessage(
|
|
||||||
[property: MemoryPackOrder(0)] Guid InstanceGuid,
|
|
||||||
[property: MemoryPackOrder(1)] InstancePlayerCounts? PlayerCounts
|
|
||||||
) : IMessageToController;
|
|
||||||
@@ -96,7 +96,6 @@ sealed class AgentActor : ReceiveActor<AgentActor.ICommand> {
|
|||||||
Receive<UpdateJavaRuntimesCommand>(UpdateJavaRuntimes);
|
Receive<UpdateJavaRuntimesCommand>(UpdateJavaRuntimes);
|
||||||
ReceiveAndReplyLater<CreateOrUpdateInstanceCommand, Result<CreateOrUpdateInstanceResult, InstanceActionFailure>>(CreateOrUpdateInstance);
|
ReceiveAndReplyLater<CreateOrUpdateInstanceCommand, Result<CreateOrUpdateInstanceResult, InstanceActionFailure>>(CreateOrUpdateInstance);
|
||||||
Receive<UpdateInstanceStatusCommand>(UpdateInstanceStatus);
|
Receive<UpdateInstanceStatusCommand>(UpdateInstanceStatus);
|
||||||
Receive<UpdateInstancePlayerCountsCommand>(UpdateInstancePlayerCounts);
|
|
||||||
ReceiveAndReplyLater<LaunchInstanceCommand, Result<LaunchInstanceResult, InstanceActionFailure>>(LaunchInstance);
|
ReceiveAndReplyLater<LaunchInstanceCommand, Result<LaunchInstanceResult, InstanceActionFailure>>(LaunchInstance);
|
||||||
ReceiveAndReplyLater<StopInstanceCommand, Result<StopInstanceResult, InstanceActionFailure>>(StopInstance);
|
ReceiveAndReplyLater<StopInstanceCommand, Result<StopInstanceResult, InstanceActionFailure>>(StopInstance);
|
||||||
ReceiveAndReplyLater<SendCommandToInstanceCommand, Result<SendCommandToInstanceResult, InstanceActionFailure>>(SendMinecraftCommand);
|
ReceiveAndReplyLater<SendCommandToInstanceCommand, Result<SendCommandToInstanceResult, InstanceActionFailure>>(SendMinecraftCommand);
|
||||||
@@ -160,7 +159,7 @@ sealed class AgentActor : ReceiveActor<AgentActor.ICommand> {
|
|||||||
private async Task<ImmutableArray<ConfigureInstanceMessage>> PrepareInitialConfigurationMessages() {
|
private async Task<ImmutableArray<ConfigureInstanceMessage>> PrepareInitialConfigurationMessages() {
|
||||||
var configurationMessages = ImmutableArray.CreateBuilder<ConfigureInstanceMessage>();
|
var configurationMessages = ImmutableArray.CreateBuilder<ConfigureInstanceMessage>();
|
||||||
|
|
||||||
foreach (var (instanceGuid, instanceConfiguration, _, _, launchAutomatically) in instanceDataByGuid.Values.ToImmutableArray()) {
|
foreach (var (instanceGuid, instanceConfiguration, _, launchAutomatically) in instanceDataByGuid.Values.ToImmutableArray()) {
|
||||||
var serverExecutableInfo = await minecraftVersions.GetServerExecutableInfo(instanceConfiguration.MinecraftVersion, cancellationToken);
|
var serverExecutableInfo = await minecraftVersions.GetServerExecutableInfo(instanceConfiguration.MinecraftVersion, cancellationToken);
|
||||||
configurationMessages.Add(new ConfigureInstanceMessage(instanceGuid, instanceConfiguration, new InstanceLaunchProperties(serverExecutableInfo), launchAutomatically));
|
configurationMessages.Add(new ConfigureInstanceMessage(instanceGuid, instanceConfiguration, new InstanceLaunchProperties(serverExecutableInfo), launchAutomatically));
|
||||||
}
|
}
|
||||||
@@ -187,8 +186,6 @@ sealed class AgentActor : ReceiveActor<AgentActor.ICommand> {
|
|||||||
public sealed record CreateOrUpdateInstanceCommand(Guid LoggedInUserGuid, Guid InstanceGuid, InstanceConfiguration Configuration) : ICommand, ICanReply<Result<CreateOrUpdateInstanceResult, InstanceActionFailure>>;
|
public sealed record CreateOrUpdateInstanceCommand(Guid LoggedInUserGuid, Guid InstanceGuid, InstanceConfiguration Configuration) : ICommand, ICanReply<Result<CreateOrUpdateInstanceResult, InstanceActionFailure>>;
|
||||||
|
|
||||||
public sealed record UpdateInstanceStatusCommand(Guid InstanceGuid, IInstanceStatus Status) : ICommand;
|
public sealed record UpdateInstanceStatusCommand(Guid InstanceGuid, IInstanceStatus Status) : ICommand;
|
||||||
|
|
||||||
public sealed record UpdateInstancePlayerCountsCommand(Guid InstanceGuid, InstancePlayerCounts? PlayerCounts) : ICommand;
|
|
||||||
|
|
||||||
public sealed record LaunchInstanceCommand(Guid LoggedInUserGuid, Guid InstanceGuid) : ICommand, ICanReply<Result<LaunchInstanceResult, InstanceActionFailure>>;
|
public sealed record LaunchInstanceCommand(Guid LoggedInUserGuid, Guid InstanceGuid) : ICommand, ICanReply<Result<LaunchInstanceResult, InstanceActionFailure>>;
|
||||||
|
|
||||||
@@ -344,10 +341,6 @@ sealed class AgentActor : ReceiveActor<AgentActor.ICommand> {
|
|||||||
private void UpdateInstanceStatus(UpdateInstanceStatusCommand command) {
|
private void UpdateInstanceStatus(UpdateInstanceStatusCommand command) {
|
||||||
TellInstance(command.InstanceGuid, new InstanceActor.SetStatusCommand(command.Status));
|
TellInstance(command.InstanceGuid, new InstanceActor.SetStatusCommand(command.Status));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateInstancePlayerCounts(UpdateInstancePlayerCountsCommand command) {
|
|
||||||
TellInstance(command.InstanceGuid, new InstanceActor.SetPlayerCountsCommand(command.PlayerCounts));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Task<Result<LaunchInstanceResult, InstanceActionFailure>> LaunchInstance(LaunchInstanceCommand command) {
|
private Task<Result<LaunchInstanceResult, InstanceActionFailure>> LaunchInstance(LaunchInstanceCommand command) {
|
||||||
return RequestInstance<InstanceActor.LaunchInstanceCommand, LaunchInstanceResult>(command.InstanceGuid, new InstanceActor.LaunchInstanceCommand(command.LoggedInUserGuid));
|
return RequestInstance<InstanceActor.LaunchInstanceCommand, LaunchInstanceResult>(command.InstanceGuid, new InstanceActor.LaunchInstanceCommand(command.LoggedInUserGuid));
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ sealed class InstanceActor : ReceiveActor<InstanceActor.ICommand> {
|
|||||||
|
|
||||||
private InstanceConfiguration configuration;
|
private InstanceConfiguration configuration;
|
||||||
private IInstanceStatus status;
|
private IInstanceStatus status;
|
||||||
private InstancePlayerCounts? playerCounts;
|
|
||||||
private bool launchAutomatically;
|
private bool launchAutomatically;
|
||||||
|
|
||||||
private readonly ActorRef<InstanceDatabaseStorageActor.ICommand> databaseStorageActor;
|
private readonly ActorRef<InstanceDatabaseStorageActor.ICommand> databaseStorageActor;
|
||||||
@@ -36,12 +35,11 @@ sealed class InstanceActor : ReceiveActor<InstanceActor.ICommand> {
|
|||||||
this.agentConnection = init.AgentConnection;
|
this.agentConnection = init.AgentConnection;
|
||||||
this.cancellationToken = init.CancellationToken;
|
this.cancellationToken = init.CancellationToken;
|
||||||
|
|
||||||
(this.instanceGuid, this.configuration, this.status, this.playerCounts, this.launchAutomatically) = init.Instance;
|
(this.instanceGuid, this.configuration, this.status, this.launchAutomatically) = init.Instance;
|
||||||
|
|
||||||
this.databaseStorageActor = Context.ActorOf(InstanceDatabaseStorageActor.Factory(new InstanceDatabaseStorageActor.Init(instanceGuid, init.DbProvider, init.CancellationToken)), "DatabaseStorage");
|
this.databaseStorageActor = Context.ActorOf(InstanceDatabaseStorageActor.Factory(new InstanceDatabaseStorageActor.Init(instanceGuid, init.DbProvider, init.CancellationToken)), "DatabaseStorage");
|
||||||
|
|
||||||
Receive<SetStatusCommand>(SetStatus);
|
Receive<SetStatusCommand>(SetStatus);
|
||||||
Receive<SetPlayerCountsCommand>(SetPlayerCounts);
|
|
||||||
ReceiveAsyncAndReply<ConfigureInstanceCommand, Result<ConfigureInstanceResult, InstanceActionFailure>>(ConfigureInstance);
|
ReceiveAsyncAndReply<ConfigureInstanceCommand, Result<ConfigureInstanceResult, InstanceActionFailure>>(ConfigureInstance);
|
||||||
ReceiveAsyncAndReply<LaunchInstanceCommand, Result<LaunchInstanceResult, InstanceActionFailure>>(LaunchInstance);
|
ReceiveAsyncAndReply<LaunchInstanceCommand, Result<LaunchInstanceResult, InstanceActionFailure>>(LaunchInstance);
|
||||||
ReceiveAsyncAndReply<StopInstanceCommand, Result<StopInstanceResult, InstanceActionFailure>>(StopInstance);
|
ReceiveAsyncAndReply<StopInstanceCommand, Result<StopInstanceResult, InstanceActionFailure>>(StopInstance);
|
||||||
@@ -49,7 +47,7 @@ sealed class InstanceActor : ReceiveActor<InstanceActor.ICommand> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void NotifyInstanceUpdated() {
|
private void NotifyInstanceUpdated() {
|
||||||
agentActor.Tell(new AgentActor.ReceiveInstanceDataCommand(new Instance(instanceGuid, configuration, status, playerCounts, launchAutomatically)));
|
agentActor.Tell(new AgentActor.ReceiveInstanceDataCommand(new Instance(instanceGuid, configuration, status, launchAutomatically)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetLaunchAutomatically(bool newValue) {
|
private void SetLaunchAutomatically(bool newValue) {
|
||||||
@@ -67,8 +65,6 @@ sealed class InstanceActor : ReceiveActor<InstanceActor.ICommand> {
|
|||||||
public interface ICommand {}
|
public interface ICommand {}
|
||||||
|
|
||||||
public sealed record SetStatusCommand(IInstanceStatus Status) : ICommand;
|
public sealed record SetStatusCommand(IInstanceStatus Status) : ICommand;
|
||||||
|
|
||||||
public sealed record SetPlayerCountsCommand(InstancePlayerCounts? PlayerCounts) : ICommand;
|
|
||||||
|
|
||||||
public sealed record ConfigureInstanceCommand(Guid AuditLogUserGuid, Guid InstanceGuid, InstanceConfiguration Configuration, InstanceLaunchProperties LaunchProperties, bool IsCreatingInstance) : ICommand, ICanReply<Result<ConfigureInstanceResult, InstanceActionFailure>>;
|
public sealed record ConfigureInstanceCommand(Guid AuditLogUserGuid, Guid InstanceGuid, InstanceConfiguration Configuration, InstanceLaunchProperties LaunchProperties, bool IsCreatingInstance) : ICommand, ICanReply<Result<ConfigureInstanceResult, InstanceActionFailure>>;
|
||||||
|
|
||||||
@@ -80,16 +76,6 @@ sealed class InstanceActor : ReceiveActor<InstanceActor.ICommand> {
|
|||||||
|
|
||||||
private void SetStatus(SetStatusCommand command) {
|
private void SetStatus(SetStatusCommand command) {
|
||||||
status = command.Status;
|
status = command.Status;
|
||||||
|
|
||||||
if (!status.IsRunning() && status != InstanceStatus.Offline /* Guard against temporary disconnects */) {
|
|
||||||
playerCounts = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
NotifyInstanceUpdated();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetPlayerCounts(SetPlayerCountsCommand command) {
|
|
||||||
playerCounts = command.PlayerCounts;
|
|
||||||
NotifyInstanceUpdated();
|
NotifyInstanceUpdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ sealed class AgentMessageHandlerActor : ReceiveActor<IMessageToController> {
|
|||||||
Receive<AdvertiseJavaRuntimesMessage>(HandleAdvertiseJavaRuntimes);
|
Receive<AdvertiseJavaRuntimesMessage>(HandleAdvertiseJavaRuntimes);
|
||||||
Receive<ReportAgentStatusMessage>(HandleReportAgentStatus);
|
Receive<ReportAgentStatusMessage>(HandleReportAgentStatus);
|
||||||
Receive<ReportInstanceStatusMessage>(HandleReportInstanceStatus);
|
Receive<ReportInstanceStatusMessage>(HandleReportInstanceStatus);
|
||||||
Receive<ReportInstancePlayerCountsMessage>(HandleReportInstancePlayerCounts);
|
|
||||||
Receive<ReportInstanceEventMessage>(HandleReportInstanceEvent);
|
Receive<ReportInstanceEventMessage>(HandleReportInstanceEvent);
|
||||||
Receive<InstanceOutputMessage>(HandleInstanceOutput);
|
Receive<InstanceOutputMessage>(HandleInstanceOutput);
|
||||||
Receive<ReplyMessage>(HandleReply);
|
Receive<ReplyMessage>(HandleReply);
|
||||||
@@ -75,10 +74,6 @@ sealed class AgentMessageHandlerActor : ReceiveActor<IMessageToController> {
|
|||||||
agentManager.TellAgent(agentGuid, new AgentActor.UpdateInstanceStatusCommand(message.InstanceGuid, message.InstanceStatus));
|
agentManager.TellAgent(agentGuid, new AgentActor.UpdateInstanceStatusCommand(message.InstanceGuid, message.InstanceStatus));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleReportInstancePlayerCounts(ReportInstancePlayerCountsMessage message) {
|
|
||||||
agentManager.TellAgent(agentGuid, new AgentActor.UpdateInstancePlayerCountsCommand(message.InstanceGuid, message.PlayerCounts));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleReportInstanceEvent(ReportInstanceEventMessage message) {
|
private void HandleReportInstanceEvent(ReportInstanceEventMessage message) {
|
||||||
message.Event.Accept(eventLogManager.CreateInstanceEventVisitor(message.EventGuid, message.UtcTime, agentGuid, message.InstanceGuid));
|
message.Event.Accept(eventLogManager.CreateInstanceEventVisitor(message.EventGuid, message.UtcTime, agentGuid, message.InstanceGuid));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ WORKDIR /data
|
|||||||
COPY --from=eclipse-temurin:8-jre /opt/java/openjdk /opt/java/8
|
COPY --from=eclipse-temurin:8-jre /opt/java/openjdk /opt/java/8
|
||||||
COPY --from=eclipse-temurin:16-jdk /opt/java/openjdk /opt/java/16
|
COPY --from=eclipse-temurin:16-jdk /opt/java/openjdk /opt/java/16
|
||||||
COPY --from=eclipse-temurin:17-jre /opt/java/openjdk /opt/java/17
|
COPY --from=eclipse-temurin:17-jre /opt/java/openjdk /opt/java/17
|
||||||
COPY --from=eclipse-temurin:21-jre /opt/java/openjdk /opt/java/21
|
COPY --from=eclipse-temurin:20-jre /opt/java/openjdk /opt/java/20
|
||||||
|
|
||||||
ARG DEBIAN_FRONTEND=noninteractive
|
ARG DEBIAN_FRONTEND=noninteractive
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
<Column Width="40%">Agent</Column>
|
<Column Width="40%">Agent</Column>
|
||||||
<Column Width="40%">Name</Column>
|
<Column Width="40%">Name</Column>
|
||||||
<Column MinWidth="215px">Status</Column>
|
<Column MinWidth="215px">Status</Column>
|
||||||
<Column Class="text-center" MinWidth="120px">Players</Column>
|
|
||||||
<Column Width="20%">Version</Column>
|
<Column Width="20%">Version</Column>
|
||||||
<Column Class="text-center" MinWidth="110px">Server Port</Column>
|
<Column Class="text-center" MinWidth="110px">Server Port</Column>
|
||||||
<Column Class="text-center" MinWidth="110px">Rcon Port</Column>
|
<Column Class="text-center" MinWidth="110px">Rcon Port</Column>
|
||||||
@@ -41,14 +40,6 @@
|
|||||||
<Cell>
|
<Cell>
|
||||||
<InstanceStatusText Status="instance.Status" />
|
<InstanceStatusText Status="instance.Status" />
|
||||||
</Cell>
|
</Cell>
|
||||||
<Cell class="text-center">
|
|
||||||
@if (instance.PlayerCounts is var (online, maximum)) {
|
|
||||||
<p class="font-monospace">@online.ToString() / @maximum.ToString()</p>
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
<p class="font-monospace">-</p>
|
|
||||||
}
|
|
||||||
</Cell>
|
|
||||||
<Cell>@configuration.MinecraftServerKind @configuration.MinecraftVersion</Cell>
|
<Cell>@configuration.MinecraftServerKind @configuration.MinecraftVersion</Cell>
|
||||||
<Cell class="text-center">
|
<Cell class="text-center">
|
||||||
<p class="font-monospace">@configuration.ServerPort.ToString()</p>
|
<p class="font-monospace">@configuration.ServerPort.ToString()</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user