mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2026-03-01 05:07:53 +01:00
Compare commits
2 Commits
wip-recipe
...
aed342d17a
| Author | SHA1 | Date | |
|---|---|---|---|
|
aed342d17a
|
|||
|
7e613a107a
|
@@ -13,7 +13,7 @@ static class MinecraftServerStatusProtocol {
|
|||||||
await tcpClient.ConnectAsync(IPAddress.Loopback, serverPort, cancellationToken);
|
await tcpClient.ConnectAsync(IPAddress.Loopback, serverPort, cancellationToken);
|
||||||
var tcpStream = tcpClient.GetStream();
|
var tcpStream = tcpClient.GetStream();
|
||||||
|
|
||||||
// https://minecraft.wiki/w/Java_Edition_protocol/Server_List_Ping
|
// https://wiki.vg/Server_List_Ping
|
||||||
tcpStream.WriteByte(0xFE);
|
tcpStream.WriteByte(0xFE);
|
||||||
await tcpStream.FlushAsync(cancellationToken);
|
await tcpStream.FlushAsync(cancellationToken);
|
||||||
|
|
||||||
|
|||||||
@@ -26,18 +26,7 @@ sealed class InstanceLauncher(
|
|||||||
}
|
}
|
||||||
|
|
||||||
var stepExecutor = new StepExecutor(logger, downloadManager, pathResolver, reportStatus, cancellationToken);
|
var stepExecutor = new StepExecutor(logger, downloadManager, pathResolver, reportStatus, cancellationToken);
|
||||||
var steps = launchRecipe.Preparation;
|
if (!await RunPreparationSteps(logger, stepExecutor)) {
|
||||||
|
|
||||||
for (int stepIndex = 0; stepIndex < steps.Length; stepIndex++) {
|
|
||||||
var step = steps[stepIndex];
|
|
||||||
try {
|
|
||||||
if (await step.Run(stepExecutor)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.Error(e, "Failed preparation step {StepIndex} out of {StepCount}: {StepName}", stepIndex, steps.Length, step.GetType().Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new InstanceLaunchResult.CouldNotPrepareServerInstance();
|
return new InstanceLaunchResult.CouldNotPrepareServerInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,6 +69,27 @@ sealed class InstanceLauncher(
|
|||||||
return new InstanceLaunchResult.Success(instanceProcess);
|
return new InstanceLaunchResult.Success(instanceProcess);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<bool> RunPreparationSteps(ILogger logger, StepExecutor stepExecutor) {
|
||||||
|
var steps = launchRecipe.Preparation;
|
||||||
|
|
||||||
|
for (int stepIndex = 0; stepIndex < steps.Length; stepIndex++) {
|
||||||
|
var step = steps[stepIndex];
|
||||||
|
try {
|
||||||
|
if (await step.Run(stepExecutor)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} catch (OperationCanceledException) {
|
||||||
|
throw;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.Error(e, "Failed preparation step {StepIndex} out of {StepCount}: {StepName}", stepIndex, steps.Length, step.GetType().Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private sealed class StepExecutor(ILogger logger, FileDownloadManager downloadManager, IInstancePathResolver pathResolver, Action<IInstanceStatus?> reportStatus, CancellationToken cancellationToken) : IInstanceLaunchStepExecutor<bool> {
|
private sealed class StepExecutor(ILogger logger, FileDownloadManager downloadManager, IInstancePathResolver pathResolver, Action<IInstanceStatus?> reportStatus, CancellationToken cancellationToken) : IInstanceLaunchStepExecutor<bool> {
|
||||||
public async Task<bool> DownloadFile(FileDownloadInfo downloadInfo, IInstancePath path) {
|
public async Task<bool> DownloadFile(FileDownloadInfo downloadInfo, IInstancePath path) {
|
||||||
string? filePath = path.Resolve(pathResolver);
|
string? filePath = path.Resolve(pathResolver);
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
try {
|
try {
|
||||||
return await MinecraftServerStatusProtocol.GetPlayerCounts(serverPort, CancellationToken);
|
return await MinecraftServerStatusProtocol.GetPlayerCounts(serverPort, CancellationToken);
|
||||||
} catch (MinecraftServerStatusProtocol.ProtocolException e) {
|
} catch (MinecraftServerStatusProtocol.ProtocolException e) {
|
||||||
Logger.Error("Could not check online player count due to protocol error: {Message}", e.Message);
|
Logger.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;
|
||||||
|
|||||||
@@ -11,10 +11,6 @@ using Phantom.Utils.Collections;
|
|||||||
namespace Phantom.Controller.Minecraft;
|
namespace Phantom.Controller.Minecraft;
|
||||||
|
|
||||||
public sealed partial class MinecraftLaunchRecipes(MinecraftVersions minecraftVersions) {
|
public sealed partial class MinecraftLaunchRecipes(MinecraftVersions minecraftVersions) {
|
||||||
private static readonly ImmutableDictionary<string, string> Eula = ImmutableDictionary.From([
|
|
||||||
("eula", "true"),
|
|
||||||
]);
|
|
||||||
|
|
||||||
public async Task<Result<InstanceLaunchRecipe, MinecraftLaunchRecipeCreationFailReason>> Create(InstanceConfiguration configuration, CancellationToken cancellationToken) {
|
public async Task<Result<InstanceLaunchRecipe, MinecraftLaunchRecipeCreationFailReason>> Create(InstanceConfiguration configuration, CancellationToken cancellationToken) {
|
||||||
string minecraftVersion = configuration.MinecraftVersion;
|
string minecraftVersion = configuration.MinecraftVersion;
|
||||||
|
|
||||||
@@ -68,6 +64,10 @@ public sealed partial class MinecraftLaunchRecipes(MinecraftVersions minecraftVe
|
|||||||
return SanitizePathRegex().IsMatch(path) ? SanitizePathRegex().Replace(path, "_") : path;
|
return SanitizePathRegex().IsMatch(path) ? SanitizePathRegex().Replace(path, "_") : path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly ImmutableDictionary<string, string> Eula = ImmutableDictionary.From([
|
||||||
|
("eula", "true"),
|
||||||
|
]);
|
||||||
|
|
||||||
private static ImmutableDictionary<string, string> ServerProperties(InstanceConfiguration configuration) {
|
private static ImmutableDictionary<string, string> ServerProperties(InstanceConfiguration configuration) {
|
||||||
return ImmutableDictionary.From([
|
return ImmutableDictionary.From([
|
||||||
("server-port", configuration.ServerPort.ToString()),
|
("server-port", configuration.ServerPort.ToString()),
|
||||||
|
|||||||
Reference in New Issue
Block a user