1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2026-03-01 05:07:53 +01:00

3 Commits

Author SHA1 Message Date
da206aacb2 wip status protocol 2026-02-23 04:05:44 +01:00
4ad9534d27 Merge Phantom.Agent.Minecraft project into Phantom.Agent.Services 2026-02-23 01:14:46 +01:00
2c63c1d7e0 Introduce instance launch recipes 2026-02-23 01:14:46 +01:00
46 changed files with 155 additions and 155 deletions

View File

@@ -1,6 +0,0 @@
namespace Phantom.Agent.Minecraft.Instance;
public sealed record InstanceProperties(
Guid InstanceGuid,
string InstanceFolder
);

View File

@@ -1,5 +0,0 @@
using Phantom.Common.Data.Java;
namespace Phantom.Agent.Minecraft.Java;
public sealed record JavaRuntimeExecutable(string ExecutablePath, JavaRuntime Runtime);

View File

@@ -1,15 +0,0 @@
using Phantom.Agent.Minecraft.Instance;
namespace Phantom.Agent.Minecraft.Launcher;
public abstract record LaunchResult {
private LaunchResult() {}
public sealed record Success(InstanceProcess Process) : LaunchResult;
public sealed record CouldNotPrepareServerInstance : LaunchResult;
public sealed record CouldNotFindServerExecutable : LaunchResult;
public sealed record CouldNotStartServerExecutable : LaunchResult;
}

View File

@@ -1,18 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Phantom.Agent.Minecraft.Tests" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Common\Phantom.Common.Data.Agent\Phantom.Common.Data.Agent.csproj" />
<ProjectReference Include="..\..\Utils\Phantom.Utils\Phantom.Utils.csproj" />
<ProjectReference Include="..\..\Utils\Phantom.Utils.Logging\Phantom.Utils.Logging.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,9 +1,9 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using NUnit.Framework; using NUnit.Framework;
using Phantom.Agent.Minecraft.Java; using Phantom.Agent.Services.Java;
using Phantom.Utils.Collections; using Phantom.Utils.Collections;
namespace Phantom.Agent.Minecraft.Tests.Java; namespace Phantom.Agent.Services.Tests.Java;
[TestFixture] [TestFixture]
public sealed class JavaPropertiesStreamTests { public sealed class JavaPropertiesStreamTests {

View File

@@ -17,7 +17,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Phantom.Agent.Minecraft\Phantom.Agent.Minecraft.csproj" /> <ProjectReference Include="..\Phantom.Agent.Services\Phantom.Agent.Services.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,7 +1,7 @@
using Akka.Actor; using Akka.Actor;
using Phantom.Agent.Minecraft.Java;
using Phantom.Agent.Services.Backups; using Phantom.Agent.Services.Backups;
using Phantom.Agent.Services.Instances; using Phantom.Agent.Services.Instances;
using Phantom.Agent.Services.Java;
using Phantom.Agent.Services.Rpc; using Phantom.Agent.Services.Rpc;
using Phantom.Common.Data.Agent; using Phantom.Common.Data.Agent;
using Phantom.Utils.Actor; using Phantom.Utils.Actor;

View File

@@ -1,7 +1,7 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Formats.Tar; using System.Formats.Tar;
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Services.Instances;
using Phantom.Common.Data.Backups; using Phantom.Common.Data.Backups;
using Phantom.Utils.IO; using Phantom.Utils.IO;
using Phantom.Utils.Logging; using Phantom.Utils.Logging;

View File

@@ -1,4 +1,4 @@
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Services.Instances.State;
using Phantom.Common.Data.Backups; using Phantom.Common.Data.Backups;
using Phantom.Utils.Logging; using Phantom.Utils.Logging;
using Serilog; using Serilog;

View File

@@ -1,7 +1,7 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Phantom.Agent.Minecraft.Command; using Phantom.Agent.Services.Games;
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Services.Instances.State;
using Phantom.Utils.Tasks; using Phantom.Utils.Tasks;
using Serilog; using Serilog;

View File

@@ -1,6 +1,6 @@
namespace Phantom.Agent.Minecraft.Server; namespace Phantom.Agent.Services.Downloads;
public sealed class DownloadProgressEventArgs : EventArgs { sealed class DownloadProgressEventArgs : EventArgs {
public ulong DownloadedBytes { get; } public ulong DownloadedBytes { get; }
public ulong? TotalBytes { get; } public ulong? TotalBytes { get; }

View File

@@ -1,3 +1,3 @@
namespace Phantom.Agent.Minecraft.Server; namespace Phantom.Agent.Services.Downloads;
sealed record FileDownloadListener(EventHandler<DownloadProgressEventArgs> DownloadProgressEventHandler, CancellationToken CancellationToken); sealed record FileDownloadListener(EventHandler<DownloadProgressEventArgs> DownloadProgressEventHandler, CancellationToken CancellationToken);

View File

@@ -3,14 +3,14 @@ using Phantom.Utils.IO;
using Phantom.Utils.Logging; using Phantom.Utils.Logging;
using Serilog; using Serilog;
namespace Phantom.Agent.Minecraft.Server; namespace Phantom.Agent.Services.Downloads;
public sealed class FileDownloadManager { sealed class FileDownloadManager {
private static readonly ILogger Logger = PhantomLogger.Create<FileDownloadManager>(); private static readonly ILogger Logger = PhantomLogger.Create<FileDownloadManager>();
private readonly Dictionary<string, FileDownloader> runningDownloadersByPath = new (); private readonly Dictionary<string, FileDownloader> runningDownloadersByPath = new ();
internal async Task<string?> DownloadAndGetPath(FileDownloadInfo fileDownloadInfo, string filePath, EventHandler<DownloadProgressEventArgs> progressEventHandler, CancellationToken cancellationToken) { public async Task<string?> DownloadAndGetPath(FileDownloadInfo fileDownloadInfo, string filePath, EventHandler<DownloadProgressEventArgs> progressEventHandler, CancellationToken cancellationToken) {
var fileInfo = new FileInfo(filePath); var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists) { if (fileInfo.Exists) {
return filePath; return filePath;

View File

@@ -7,7 +7,7 @@ using Phantom.Utils.Net;
using Phantom.Utils.Runtime; using Phantom.Utils.Runtime;
using Serilog; using Serilog;
namespace Phantom.Agent.Minecraft.Server; namespace Phantom.Agent.Services.Downloads;
sealed class FileDownloader { sealed class FileDownloader {
private static readonly ILogger Logger = PhantomLogger.Create<FileDownloader>(); private static readonly ILogger Logger = PhantomLogger.Create<FileDownloader>();
@@ -103,7 +103,7 @@ sealed class FileDownloader {
} }
private static async Task DownloadFile(string filePath, FileDownloadInfo fileDownloadInfo, DownloadProgressCallback progressCallback, CancellationToken cancellationToken) { private static async Task DownloadFile(string filePath, FileDownloadInfo fileDownloadInfo, DownloadProgressCallback progressCallback, CancellationToken cancellationToken) {
string downloadUrl = fileDownloadInfo.DownloadUrl; string downloadUrl = fileDownloadInfo.Url;
DownloadResult result; DownloadResult result;
try { try {

View File

@@ -1,6 +1,6 @@
namespace Phantom.Agent.Minecraft.Command; namespace Phantom.Agent.Services.Games;
public static class MinecraftCommand { static class MinecraftCommand {
public const string SaveOn = "save-on"; public const string SaveOn = "save-on";
public const string SaveOff = "save-off"; public const string SaveOff = "save-off";
public const string Stop = "stop"; public const string Stop = "stop";

View File

@@ -5,15 +5,15 @@ using System.Net.Sockets;
using System.Text; using System.Text;
using Phantom.Common.Data.Instance; using Phantom.Common.Data.Instance;
namespace Phantom.Agent.Minecraft.Server; namespace Phantom.Agent.Services.Games;
public static class ServerStatusProtocol { static class MinecraftServerStatusProtocol {
public static async Task<InstancePlayerCounts> GetPlayerCounts(ushort serverPort, CancellationToken cancellationToken) { public static async Task<InstancePlayerCounts> GetPlayerCounts(ushort 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();
// https://wiki.vg/Server_List_Ping // https://minecraft.wiki/w/Java_Edition_protocol/Server_List_Ping
tcpStream.WriteByte(0xFE); tcpStream.WriteByte(0xFE);
await tcpStream.FlushAsync(cancellationToken); await tcpStream.FlushAsync(cancellationToken);

View File

@@ -1,5 +1,5 @@
using Phantom.Agent.Minecraft.Launcher; using Phantom.Agent.Services.Backups;
using Phantom.Agent.Services.Backups; using Phantom.Agent.Services.Instances.Launch;
using Phantom.Agent.Services.Instances.State; using Phantom.Agent.Services.Instances.State;
using Phantom.Agent.Services.Rpc; using Phantom.Agent.Services.Rpc;
using Phantom.Common.Data.Agent.Instance; using Phantom.Common.Data.Agent.Instance;

View File

@@ -1,8 +1,7 @@
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Services.Backups;
using Phantom.Agent.Minecraft.Java; using Phantom.Agent.Services.Downloads;
using Phantom.Agent.Minecraft.Launcher; using Phantom.Agent.Services.Instances.Launch;
using Phantom.Agent.Minecraft.Server; using Phantom.Agent.Services.Java;
using Phantom.Agent.Services.Backups;
using Phantom.Agent.Services.Rpc; using Phantom.Agent.Services.Rpc;
using Phantom.Common.Data; using Phantom.Common.Data;
using Phantom.Common.Data.Agent.Instance; using Phantom.Common.Data.Agent.Instance;

View File

@@ -0,0 +1,6 @@
namespace Phantom.Agent.Services.Instances;
sealed record InstanceProperties(
Guid InstanceGuid,
string InstanceFolder
);

View File

@@ -1,6 +1,6 @@
using Phantom.Agent.Minecraft.Java; using Phantom.Agent.Services.Backups;
using Phantom.Agent.Minecraft.Server; using Phantom.Agent.Services.Downloads;
using Phantom.Agent.Services.Backups; using Phantom.Agent.Services.Java;
using Phantom.Agent.Services.Rpc; using Phantom.Agent.Services.Rpc;
namespace Phantom.Agent.Services.Instances; namespace Phantom.Agent.Services.Instances;

View File

@@ -1,10 +1,9 @@
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Services.Instances.State;
using Phantom.Agent.Minecraft.Launcher;
using Phantom.Common.Data; using Phantom.Common.Data;
using Phantom.Common.Data.Agent.Instance; using Phantom.Common.Data.Agent.Instance;
using Phantom.Common.Data.Instance; using Phantom.Common.Data.Instance;
namespace Phantom.Agent.Services.Instances.State; namespace Phantom.Agent.Services.Instances.Launch;
static class InstanceLaunchProcedure { static class InstanceLaunchProcedure {
public static async Task<InstanceRunningState?> Run(InstanceContext context, InstanceInfo info, InstanceLauncher launcher, InstanceTicketManager ticketManager, InstanceTicketManager.Ticket ticket, Action<IInstanceStatus?> reportStatus, CancellationToken cancellationToken) { public static async Task<InstanceRunningState?> Run(InstanceContext context, InstanceInfo info, InstanceLauncher launcher, InstanceTicketManager ticketManager, InstanceTicketManager.Ticket ticket, Action<IInstanceStatus?> reportStatus, CancellationToken cancellationToken) {
@@ -44,18 +43,18 @@ static class InstanceLaunchProcedure {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
switch (await launcher.Launch(context.Logger, reportStatus, cancellationToken)) { switch (await launcher.Launch(context.Logger, reportStatus, cancellationToken)) {
case LaunchResult.Success launchSuccess: case InstanceLaunchResult.Success launchSuccess:
return launchSuccess.Process; return launchSuccess.Process;
case LaunchResult.CouldNotPrepareServerInstance: case InstanceLaunchResult.CouldNotPrepareServerInstance:
context.Logger.Error("Session failed to launch, could not prepare server instance."); context.Logger.Error("Session failed to launch, could not prepare server instance.");
return InstanceLaunchFailReason.CouldNotPrepareServerInstance; return InstanceLaunchFailReason.CouldNotPrepareServerInstance;
case LaunchResult.CouldNotFindServerExecutable: case InstanceLaunchResult.CouldNotFindServerExecutable:
context.Logger.Error("Session failed to launch, could not find server executable."); context.Logger.Error("Session failed to launch, could not find server executable.");
return InstanceLaunchFailReason.CouldNotFindServerExecutable; return InstanceLaunchFailReason.CouldNotFindServerExecutable;
case LaunchResult.CouldNotStartServerExecutable: case InstanceLaunchResult.CouldNotStartServerExecutable:
context.Logger.Error("Session failed to launch, could not start server executable."); context.Logger.Error("Session failed to launch, could not start server executable.");
return InstanceLaunchFailReason.CouldNotStartServerExecutable; return InstanceLaunchFailReason.CouldNotStartServerExecutable;

View File

@@ -0,0 +1,15 @@
using Phantom.Agent.Services.Instances.State;
namespace Phantom.Agent.Services.Instances.Launch;
abstract record InstanceLaunchResult {
private InstanceLaunchResult() {}
public sealed record Success(InstanceProcess Process) : InstanceLaunchResult;
public sealed record CouldNotPrepareServerInstance : InstanceLaunchResult;
public sealed record CouldNotFindServerExecutable : InstanceLaunchResult;
public sealed record CouldNotStartServerExecutable : InstanceLaunchResult;
}

View File

@@ -1,7 +1,7 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Services.Downloads;
using Phantom.Agent.Minecraft.Java; using Phantom.Agent.Services.Instances.State;
using Phantom.Agent.Minecraft.Server; using Phantom.Agent.Services.Java;
using Phantom.Common.Data.Agent; using Phantom.Common.Data.Agent;
using Phantom.Common.Data.Agent.Instance; using Phantom.Common.Data.Agent.Instance;
using Phantom.Common.Data.Agent.Instance.Launch; using Phantom.Common.Data.Agent.Instance.Launch;
@@ -9,33 +9,36 @@ using Phantom.Common.Data.Instance;
using Phantom.Utils.Processes; using Phantom.Utils.Processes;
using Serilog; using Serilog;
namespace Phantom.Agent.Minecraft.Launcher; namespace Phantom.Agent.Services.Instances.Launch;
public sealed class InstanceLauncher( sealed class InstanceLauncher(
FileDownloadManager downloadManager, FileDownloadManager downloadManager,
IInstancePathResolver pathResolver, IInstancePathResolver pathResolver,
IInstanceValueResolver valueResolver, IInstanceValueResolver valueResolver,
InstanceProperties instanceProperties, InstanceProperties instanceProperties,
InstanceLaunchRecipe launchRecipe InstanceLaunchRecipe launchRecipe
) { ) {
public async Task<LaunchResult> Launch(ILogger logger, Action<IInstanceStatus?> reportStatus, CancellationToken cancellationToken) { public async Task<InstanceLaunchResult> Launch(ILogger logger, Action<IInstanceStatus?> reportStatus, CancellationToken cancellationToken) {
string? executablePath = launchRecipe.Executable.Resolve(pathResolver); string? executablePath = launchRecipe.Executable.Resolve(pathResolver);
if (executablePath == null) { if (executablePath == null) {
logger.Error("Could not resolve server executable path"); logger.Error("Could not resolve server executable path: {Path}", launchRecipe.Executable);
return new LaunchResult.CouldNotFindServerExecutable(); return new InstanceLaunchResult.CouldNotFindServerExecutable();
} }
var stepExecutor = new StepExecutor(downloadManager, pathResolver, reportStatus, cancellationToken); var stepExecutor = new StepExecutor(logger, downloadManager, pathResolver, reportStatus, cancellationToken);
var steps = launchRecipe.Preparation; var steps = launchRecipe.Preparation;
for (int stepIndex = 0; stepIndex < steps.Length; stepIndex++) { for (int stepIndex = 0; stepIndex < steps.Length; stepIndex++) {
var step = steps[stepIndex]; var step = steps[stepIndex];
try { try {
await step.Run(stepExecutor); if (await step.Run(stepExecutor)) {
} catch (Exception e) { continue;
logger.Error(e, "Failed preparation step {StepIndex} out of {StepCount}: {Step}", stepIndex, steps.Length, step.GetType().Name);
return new LaunchResult.CouldNotPrepareServerInstance();
} }
} catch (Exception e) {
logger.Error(e, "Failed preparation step {StepIndex} out of {StepCount}: {StepName}", stepIndex, steps.Length, step.GetType().Name);
}
return new InstanceLaunchResult.CouldNotPrepareServerInstance();
} }
var processConfigurator = new ProcessConfigurator { var processConfigurator = new ProcessConfigurator {
@@ -47,13 +50,13 @@ public sealed class InstanceLauncher(
var processArguments = processConfigurator.ArgumentList; var processArguments = processConfigurator.ArgumentList;
foreach (var value in launchRecipe.Arguments) { foreach (IInstanceValue value in launchRecipe.Arguments) {
if (value.Resolve(valueResolver) is {} resolved) { if (value.Resolve(valueResolver) is {} resolved) {
processArguments.Add(resolved); processArguments.Add(resolved);
} }
else { else {
logger.Error("Could not resolve server executable argument: {Value}", value); logger.Error("Could not resolve server executable argument: {Value}", value);
return new LaunchResult.CouldNotPrepareServerInstance(); return new InstanceLaunchResult.CouldNotPrepareServerInstance();
} }
} }
@@ -71,18 +74,18 @@ public sealed class InstanceLauncher(
logger.Error(killException, "Caught exception trying to kill the server process after a failed launch."); logger.Error(killException, "Caught exception trying to kill the server process after a failed launch.");
} }
return new LaunchResult.CouldNotStartServerExecutable(); return new InstanceLaunchResult.CouldNotStartServerExecutable();
} }
return new LaunchResult.Success(instanceProcess); return new InstanceLaunchResult.Success(instanceProcess);
} }
private sealed class StepExecutor(FileDownloadManager downloadManager, IInstancePathResolver pathResolver, Action<IInstanceStatus?> reportStatus, CancellationToken cancellationToken) : IInstanceLaunchStepExecutor { private sealed class StepExecutor(ILogger logger, FileDownloadManager downloadManager, IInstancePathResolver pathResolver, Action<IInstanceStatus?> reportStatus, CancellationToken cancellationToken) : IInstanceLaunchStepExecutor<bool> {
public async Task DownloadFile(FileDownloadInfo downloadInfo, IInstancePath path) { public async Task<bool> DownloadFile(FileDownloadInfo downloadInfo, IInstancePath path) {
string? filePath = path.Resolve(pathResolver); string? filePath = path.Resolve(pathResolver);
if (filePath == null) { if (filePath == null) {
// TODO avoid exc logger.Error("Could not resolve download file path: {Path}", path);
throw new FileNotFoundException("Could not resolve path"); return false;
} }
byte? lastDownloadProgress = null; byte? lastDownloadProgress = null;
@@ -97,21 +100,32 @@ public sealed class InstanceLauncher(
} }
if (await downloadManager.DownloadAndGetPath(downloadInfo, filePath, OnDownloadProgress, cancellationToken) == null) { if (await downloadManager.DownloadAndGetPath(downloadInfo, filePath, OnDownloadProgress, cancellationToken) == null) {
throw new FileNotFoundException("Could not download file"); logger.Error("Could not download file: {Url}", downloadInfo.Url);
return false;
} }
reportStatus(null); reportStatus(null);
return true;
} }
public async Task EditPropertiesFile(InstancePath.Local path, string comment, ImmutableDictionary<string, string> newValues) { public async Task<bool> EditPropertiesFile(InstancePath.Local path, string comment, ImmutableDictionary<string, string> newValues) {
string? filePath = path.Resolve(pathResolver); string? filePath = path.Resolve(pathResolver);
if (filePath == null) { if (filePath == null) {
throw new FileNotFoundException("Could not resolve path"); logger.Error("Could not resolve properties file path: {Path}", path);
return false;
} }
var editor = new JavaPropertiesFileEditor(); var editor = new JavaPropertiesFileEditor();
editor.SetAll(newValues); editor.SetAll(newValues);
try {
await editor.EditOrCreate(filePath, comment, cancellationToken); await editor.EditOrCreate(filePath, comment, cancellationToken);
} catch (Exception e) {
logger.Error(e, "Could not edit properties file: {Path}", filePath);
return false;
}
return true;
} }
} }
} }

View File

@@ -1,10 +1,9 @@
using System.Buffers; using System.Buffers;
using System.Collections.Immutable; using System.Collections.Immutable;
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Services.Java;
using Phantom.Agent.Minecraft.Java;
using Phantom.Common.Data.Agent.Instance; using Phantom.Common.Data.Agent.Instance;
namespace Phantom.Agent.Services.Instances; namespace Phantom.Agent.Services.Instances.Launch;
sealed class InstancePathResolver(AgentFolders agentFolders, JavaRuntimeRepository javaRuntimeRepository, InstanceProperties instanceProperties) : IInstancePathResolver { sealed class InstancePathResolver(AgentFolders agentFolders, JavaRuntimeRepository javaRuntimeRepository, InstanceProperties instanceProperties) : IInstancePathResolver {
public string? Global(ImmutableArray<string> segments) { public string? Global(ImmutableArray<string> segments) {

View File

@@ -1,6 +1,6 @@
using Phantom.Common.Data.Agent.Instance; using Phantom.Common.Data.Agent.Instance;
namespace Phantom.Agent.Services.Instances; namespace Phantom.Agent.Services.Instances.Launch;
sealed class InstanceValueResolver(IInstancePathResolver pathResolver) : IInstanceValueResolver { sealed class InstanceValueResolver(IInstancePathResolver pathResolver) : IInstanceValueResolver {
public string? Path(IInstancePath value) { public string? Path(IInstancePath value) {

View File

@@ -1,6 +1,5 @@
using System.Net.Sockets; using System.Net.Sockets;
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Services.Games;
using Phantom.Agent.Minecraft.Server;
using Phantom.Agent.Services.Rpc; using Phantom.Agent.Services.Rpc;
using Phantom.Common.Data.Instance; using Phantom.Common.Data.Instance;
using Phantom.Common.Messages.Agent.ToController; using Phantom.Common.Messages.Agent.ToController;
@@ -60,9 +59,9 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
private async Task<InstancePlayerCounts?> TryGetPlayerCounts() { private async Task<InstancePlayerCounts?> TryGetPlayerCounts() {
try { try {
return await ServerStatusProtocol.GetPlayerCounts(serverPort, CancellationToken); return await MinecraftServerStatusProtocol.GetPlayerCounts(serverPort, CancellationToken);
} catch (ServerStatusProtocol.ProtocolException e) { } catch (MinecraftServerStatusProtocol.ProtocolException e) {
Logger.Error("{Message}", e.Message); Logger.Error("Could not check online player count due to protocol error: {Message}", e.Message);
return null; return null;
} catch (SocketException e) { } catch (SocketException e) {
bool waitingForServerStart = e.SocketErrorCode == SocketError.ConnectionRefused && WaitingForFirstDetection; bool waitingForServerStart = e.SocketErrorCode == SocketError.ConnectionRefused && WaitingForFirstDetection;

View File

@@ -2,9 +2,9 @@
using Phantom.Utils.Processes; using Phantom.Utils.Processes;
using Phantom.Utils.Tasks; using Phantom.Utils.Tasks;
namespace Phantom.Agent.Minecraft.Instance; namespace Phantom.Agent.Services.Instances.State;
public sealed class InstanceProcess : IDisposable { sealed class InstanceProcess : IDisposable {
public InstanceProperties InstanceProperties { get; } public InstanceProperties InstanceProperties { get; }
private readonly RingBuffer<string> outputBuffer = new (100); private readonly RingBuffer<string> outputBuffer = new (100);

View File

@@ -1,6 +1,5 @@
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Services.Backups;
using Phantom.Agent.Minecraft.Launcher; using Phantom.Agent.Services.Instances.Launch;
using Phantom.Agent.Services.Backups;
using Phantom.Common.Data.Agent.Instance; using Phantom.Common.Data.Agent.Instance;
using Phantom.Common.Data.Backups; using Phantom.Common.Data.Backups;
using Phantom.Common.Data.Instance; using Phantom.Common.Data.Instance;

View File

@@ -1,6 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using Phantom.Agent.Minecraft.Command; using Phantom.Agent.Services.Games;
using Phantom.Agent.Minecraft.Instance;
using Phantom.Common.Data.Instance; using Phantom.Common.Data.Instance;
using Phantom.Common.Data.Minecraft; using Phantom.Common.Data.Minecraft;

View File

@@ -1,4 +1,4 @@
namespace Phantom.Agent.Minecraft.Java; namespace Phantom.Agent.Services.Java;
sealed class JavaPropertiesFileEditor { sealed class JavaPropertiesFileEditor {
private readonly Dictionary<string, string> overriddenProperties = new (); private readonly Dictionary<string, string> overriddenProperties = new ();

View File

@@ -4,7 +4,7 @@ using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using Phantom.Utils.Collections; using Phantom.Utils.Collections;
namespace Phantom.Agent.Minecraft.Java; namespace Phantom.Agent.Services.Java;
static class JavaPropertiesStream { static class JavaPropertiesStream {
internal static readonly Encoding Encoding = Encoding.GetEncoding("ISO-8859-1"); internal static readonly Encoding Encoding = Encoding.GetEncoding("ISO-8859-1");

View File

@@ -6,7 +6,7 @@ using Phantom.Utils.IO;
using Phantom.Utils.Logging; using Phantom.Utils.Logging;
using Serilog; using Serilog;
namespace Phantom.Agent.Minecraft.Java; namespace Phantom.Agent.Services.Java;
public sealed class JavaRuntimeDiscovery { public sealed class JavaRuntimeDiscovery {
private static readonly ILogger Logger = PhantomLogger.Create(nameof(JavaRuntimeDiscovery)); private static readonly ILogger Logger = PhantomLogger.Create(nameof(JavaRuntimeDiscovery));

View File

@@ -0,0 +1,5 @@
using Phantom.Common.Data.Java;
namespace Phantom.Agent.Services.Java;
sealed record JavaRuntimeExecutable(string ExecutablePath, JavaRuntime Runtime);

View File

@@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis;
using Phantom.Common.Data.Java; using Phantom.Common.Data.Java;
using Phantom.Utils.Cryptography; using Phantom.Utils.Cryptography;
namespace Phantom.Agent.Minecraft.Java; namespace Phantom.Agent.Services.Java;
public sealed class JavaRuntimeRepository { public sealed class JavaRuntimeRepository {
private readonly ImmutableDictionary<Guid, JavaRuntimeExecutable> runtimesByGuid; private readonly ImmutableDictionary<Guid, JavaRuntimeExecutable> runtimesByGuid;
@@ -25,7 +25,7 @@ public sealed class JavaRuntimeRepository {
.ToImmutableArray(); .ToImmutableArray();
} }
public bool TryGetByGuid(Guid guid, [MaybeNullWhen(false)] out JavaRuntimeExecutable runtime) { internal bool TryGetByGuid(Guid guid, [MaybeNullWhen(false)] out JavaRuntimeExecutable runtime) {
return runtimesByGuid.TryGetValue(guid, out runtime); return runtimesByGuid.TryGetValue(guid, out runtime);
} }

View File

@@ -5,9 +5,12 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Phantom.Agent.Services.Tests" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Common\Phantom.Common.Messages.Agent\Phantom.Common.Messages.Agent.csproj" /> <ProjectReference Include="..\..\Common\Phantom.Common.Messages.Agent\Phantom.Common.Messages.Agent.csproj" />
<ProjectReference Include="..\Phantom.Agent.Minecraft\Phantom.Agent.Minecraft.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -12,7 +12,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Utils\Phantom.Utils\Phantom.Utils.csproj" /> <ProjectReference Include="..\..\Utils\Phantom.Utils\Phantom.Utils.csproj" />
<ProjectReference Include="..\Phantom.Agent.Minecraft\Phantom.Agent.Minecraft.csproj" />
<ProjectReference Include="..\Phantom.Agent.Services\Phantom.Agent.Services.csproj" /> <ProjectReference Include="..\Phantom.Agent.Services\Phantom.Agent.Services.csproj" />
</ItemGroup> </ItemGroup>

View File

@@ -1,7 +1,7 @@
using System.Reflection; using System.Reflection;
using Phantom.Agent; using Phantom.Agent;
using Phantom.Agent.Minecraft.Java;
using Phantom.Agent.Services; using Phantom.Agent.Services;
using Phantom.Agent.Services.Java;
using Phantom.Agent.Services.Rpc; using Phantom.Agent.Services.Rpc;
using Phantom.Common.Data.Agent; using Phantom.Common.Data.Agent;
using Phantom.Common.Messages.Agent; using Phantom.Common.Messages.Agent;

View File

@@ -1,4 +1,4 @@
using Phantom.Agent.Minecraft.Java; using Phantom.Agent.Services.Java;
using Phantom.Common.Data; using Phantom.Common.Data;
using Phantom.Utils.Logging; using Phantom.Utils.Logging;
using Phantom.Utils.Runtime; using Phantom.Utils.Runtime;

View File

@@ -6,7 +6,7 @@ namespace Phantom.Common.Data.Agent;
[MemoryPackable(GenerateType.VersionTolerant)] [MemoryPackable(GenerateType.VersionTolerant)]
public sealed partial class FileDownloadInfo { public sealed partial class FileDownloadInfo {
[MemoryPackOrder(0)] [MemoryPackOrder(0)]
public string DownloadUrl { get; } public string Url { get; }
[MemoryPackOrder(1)] [MemoryPackOrder(1)]
[MemoryPackInclude] [MemoryPackInclude]
@@ -15,11 +15,11 @@ public sealed partial class FileDownloadInfo {
[MemoryPackIgnore] [MemoryPackIgnore]
public Sha1String? Hash => hash == null ? null : Sha1String.FromString(hash); public Sha1String? Hash => hash == null ? null : Sha1String.FromString(hash);
public FileDownloadInfo(string downloadUrl, Sha1String? hash = null) : this(downloadUrl, hash?.ToString()) {} public FileDownloadInfo(string url, Sha1String? hash = null) : this(url, hash?.ToString()) {}
[MemoryPackConstructor] [MemoryPackConstructor]
private FileDownloadInfo(string downloadUrl, string? hash) { private FileDownloadInfo(string url, string? hash) {
this.DownloadUrl = downloadUrl; this.Url = url;
this.hash = hash; this.hash = hash;
} }
} }

View File

@@ -21,7 +21,7 @@ public static partial class InstancePath {
} }
public override string ToString() { public override string ToString() {
return string.Join(separator: '/', Segments); return "Global[" + string.Join(separator: '/', Segments) + "]";
} }
} }
@@ -34,7 +34,7 @@ public static partial class InstancePath {
} }
public override string ToString() { public override string ToString() {
return string.Join(separator: '/', Segments); return "Local[" + string.Join(separator: '/', Segments) + "]";
} }
} }
@@ -45,5 +45,9 @@ public static partial class InstancePath {
public string? Resolve(IInstancePathResolver resolver) { public string? Resolve(IInstancePathResolver resolver) {
return resolver.Runtime(Guid); return resolver.Runtime(Guid);
} }
public override string ToString() {
return "Runtime[" + Guid + "]";
}
} }
} }

View File

@@ -29,6 +29,10 @@ public static partial class InstanceValues {
return result.ToString(); return result.ToString();
} }
public override string ToString() {
return "Concatenation[" + string.Join(",", Values) + "]";
}
} }
[MemoryPackable] [MemoryPackable]
@@ -36,6 +40,10 @@ public static partial class InstanceValues {
public string Resolve(IInstanceValueResolver resolver) { public string Resolve(IInstanceValueResolver resolver) {
return Value; return Value;
} }
public override string ToString() {
return "Text[" + Value + "]";
}
} }
[MemoryPackable] [MemoryPackable]
@@ -43,5 +51,9 @@ public static partial class InstanceValues {
public string? Resolve(IInstanceValueResolver resolver) { public string? Resolve(IInstanceValueResolver resolver) {
return resolver.Path(Value); return resolver.Path(Value);
} }
public override string ToString() {
return "Path[" + Value + "]";
}
} }
} }

View File

@@ -7,7 +7,7 @@ namespace Phantom.Common.Data.Agent.Instance.Launch;
[MemoryPackUnion(tag: 0, typeof(InstanceLaunchStep.DownloadFile))] [MemoryPackUnion(tag: 0, typeof(InstanceLaunchStep.DownloadFile))]
[MemoryPackUnion(tag: 1, typeof(InstanceLaunchStep.EditPropertiesFile))] [MemoryPackUnion(tag: 1, typeof(InstanceLaunchStep.EditPropertiesFile))]
public partial interface IInstanceLaunchStep { public partial interface IInstanceLaunchStep {
Task Run(IInstanceLaunchStepExecutor executor); Task<TResult> Run<TResult>(IInstanceLaunchStepExecutor<TResult> executor);
} }
public static partial class InstanceLaunchStep { public static partial class InstanceLaunchStep {
@@ -16,7 +16,7 @@ public static partial class InstanceLaunchStep {
[property: MemoryPackOrder(0)] FileDownloadInfo DownloadInfo, [property: MemoryPackOrder(0)] FileDownloadInfo DownloadInfo,
[property: MemoryPackOrder(1)] IInstancePath Path [property: MemoryPackOrder(1)] IInstancePath Path
) : IInstanceLaunchStep { ) : IInstanceLaunchStep {
public Task Run(IInstanceLaunchStepExecutor executor) { public Task<TResult> Run<TResult>(IInstanceLaunchStepExecutor<TResult> executor) {
return executor.DownloadFile(DownloadInfo, Path); return executor.DownloadFile(DownloadInfo, Path);
} }
} }
@@ -27,7 +27,7 @@ public static partial class InstanceLaunchStep {
[property: MemoryPackOrder(1)] string Comment, [property: MemoryPackOrder(1)] string Comment,
[property: MemoryPackOrder(2)] ImmutableDictionary<string, string> NewValues [property: MemoryPackOrder(2)] ImmutableDictionary<string, string> NewValues
) : IInstanceLaunchStep { ) : IInstanceLaunchStep {
public Task Run(IInstanceLaunchStepExecutor executor) { public Task<TResult> Run<TResult>(IInstanceLaunchStepExecutor<TResult> executor) {
return executor.EditPropertiesFile(Path, Comment, NewValues); return executor.EditPropertiesFile(Path, Comment, NewValues);
} }
} }

View File

@@ -1,9 +1,8 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using Phantom.Common.Data.Minecraft;
namespace Phantom.Common.Data.Agent.Instance.Launch; namespace Phantom.Common.Data.Agent.Instance.Launch;
public interface IInstanceLaunchStepExecutor { public interface IInstanceLaunchStepExecutor<TResult> {
Task DownloadFile(FileDownloadInfo downloadInfo, IInstancePath path); Task<TResult> DownloadFile(FileDownloadInfo downloadInfo, IInstancePath path);
Task EditPropertiesFile(InstancePath.Local path, string comment, ImmutableDictionary<string, string> newValues); Task<TResult> EditPropertiesFile(InstancePath.Local path, string comment, ImmutableDictionary<string, string> newValues);
} }

View File

@@ -53,8 +53,8 @@ public sealed partial class MinecraftLaunchRecipes(MinecraftVersions minecraftVe
return new InstanceLaunchRecipe(steps.ToImmutable(), new InstancePath.Runtime(configuration.JavaRuntimeGuid), [ return new InstanceLaunchRecipe(steps.ToImmutable(), new InstancePath.Runtime(configuration.JavaRuntimeGuid), [
..configuration.JvmArguments.Select(static arg => new InstanceValues.Text(arg)), ..configuration.JvmArguments.Select(static arg => new InstanceValues.Text(arg)),
..additionalJvmArguments, ..additionalJvmArguments,
new InstanceValues.Text("-Xms" + initialHeapSizeMegabytes + "M"), new InstanceValues.Text($"-Xms{initialHeapSizeMegabytes}M"),
new InstanceValues.Text("-Xmx" + maximumHeapSizeMegabvtes + "M"), new InstanceValues.Text($"-Xmx{maximumHeapSizeMegabvtes}M"),
new InstanceValues.Text("-jar"), new InstanceValues.Text("-jar"),
new InstanceValues.Path(serverExecutableFilePath), new InstanceValues.Path(serverExecutableFilePath),
new InstanceValues.Text("-nogui"), new InstanceValues.Text("-nogui"),

View File

@@ -47,7 +47,7 @@ public sealed class MinecraftVersions : IDisposable {
Logger.Information("Refreshed Minecraft {Version} server executable cache, no file found.", version); Logger.Information("Refreshed Minecraft {Version} server executable cache, no file found.", version);
} }
else { else {
Logger.Information("Refreshed Minecraft {Version} server executable cache, found file: {Url}.", version, info.DownloadUrl); Logger.Information("Refreshed Minecraft {Version} server executable cache, found file: {Url}.", version, info.Url);
} }
return info; return info;

View File

@@ -18,9 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Web", "Web", "{92B26F48-235
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Agent", "Agent\Phantom.Agent\Phantom.Agent.csproj", "{418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Agent", "Agent\Phantom.Agent\Phantom.Agent.csproj", "{418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Agent.Minecraft", "Agent\Phantom.Agent.Minecraft\Phantom.Agent.Minecraft.csproj", "{9FE000D0-91AC-4CB4-8956-91CCC0270015}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Agent.Services.Tests", "Agent\Phantom.Agent.Services.Tests\Phantom.Agent.Services.Tests.csproj", "{065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Agent.Minecraft.Tests", "Agent\Phantom.Agent.Minecraft.Tests\Phantom.Agent.Minecraft.Tests.csproj", "{065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Agent.Services", "Agent\Phantom.Agent.Services\Phantom.Agent.Services.csproj", "{AEE8B77E-AB07-423F-9981-8CD829ACB834}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Agent.Services", "Agent\Phantom.Agent.Services\Phantom.Agent.Services.csproj", "{AEE8B77E-AB07-423F-9981-8CD829ACB834}"
EndProject EndProject
@@ -76,10 +74,6 @@ Global
{418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7}.Debug|Any CPU.Build.0 = Debug|Any CPU {418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7}.Release|Any CPU.ActiveCfg = Release|Any CPU {418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7}.Release|Any CPU.Build.0 = Release|Any CPU {418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7}.Release|Any CPU.Build.0 = Release|Any CPU
{9FE000D0-91AC-4CB4-8956-91CCC0270015}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9FE000D0-91AC-4CB4-8956-91CCC0270015}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9FE000D0-91AC-4CB4-8956-91CCC0270015}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9FE000D0-91AC-4CB4-8956-91CCC0270015}.Release|Any CPU.Build.0 = Release|Any CPU
{065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB}.Debug|Any CPU.Build.0 = Debug|Any CPU {065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB}.Release|Any CPU.ActiveCfg = Release|Any CPU {065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -171,7 +165,6 @@ Global
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7} = {F5878792-64C8-4ECF-A075-66341FF97127} {418BE1BF-9F63-4B46-B4E4-DF64C3B3DDA7} = {F5878792-64C8-4ECF-A075-66341FF97127}
{9FE000D0-91AC-4CB4-8956-91CCC0270015} = {F5878792-64C8-4ECF-A075-66341FF97127}
{065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB} = {94C1E464-3F91-49EA-99FF-3A3082C54CE8} {065FFFA0-DFF4-43DB-AB3D-B92EE9848DDB} = {94C1E464-3F91-49EA-99FF-3A3082C54CE8}
{AEE8B77E-AB07-423F-9981-8CD829ACB834} = {F5878792-64C8-4ECF-A075-66341FF97127} {AEE8B77E-AB07-423F-9981-8CD829ACB834} = {F5878792-64C8-4ECF-A075-66341FF97127}
{6C3DB1E5-F695-4D70-8F3A-78C2957274BE} = {01CB1A81-8950-471C-BFDF-F135FDDB2C18} {6C3DB1E5-F695-4D70-8F3A-78C2957274BE} = {01CB1A81-8950-471C-BFDF-F135FDDB2C18}