mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-05-07 00:34:03 +02:00
Prepare for adding non-vanilla server types
This commit is contained in:
parent
a31dda7439
commit
734d9e266e
Agent
Phantom.Agent.Minecraft/Launcher
Phantom.Agent.Services/Instances/States
Common/Phantom.Common.Data/Instance
@ -11,6 +11,8 @@ namespace Phantom.Agent.Minecraft.Launcher;
|
||||
|
||||
public abstract class BaseLauncher {
|
||||
private readonly InstanceProperties instanceProperties;
|
||||
|
||||
protected string MinecraftVersion => instanceProperties.ServerVersion;
|
||||
|
||||
private protected BaseLauncher(InstanceProperties instanceProperties) {
|
||||
this.instanceProperties = instanceProperties;
|
||||
@ -25,11 +27,34 @@ public abstract class BaseLauncher {
|
||||
return new LaunchResult.InvalidJvmArguments();
|
||||
}
|
||||
|
||||
var vanillaServerJarPath = await services.ServerExecutables.DownloadAndGetPath(instanceProperties.LaunchProperties.ServerDownloadInfo, instanceProperties.ServerVersion, downloadProgressEventHandler, cancellationToken);
|
||||
var vanillaServerJarPath = await services.ServerExecutables.DownloadAndGetPath(instanceProperties.LaunchProperties.ServerDownloadInfo, MinecraftVersion, downloadProgressEventHandler, cancellationToken);
|
||||
if (vanillaServerJarPath == null) {
|
||||
return new LaunchResult.CouldNotDownloadMinecraftServer();
|
||||
}
|
||||
|
||||
ServerJarInfo? serverJar;
|
||||
try {
|
||||
serverJar = await PrepareServerJar(logger, vanillaServerJarPath, instanceProperties.InstanceFolder, cancellationToken);
|
||||
} catch (OperationCanceledException) {
|
||||
throw;
|
||||
} catch (Exception e) {
|
||||
logger.Error(e, "Caught exception while preparing the server jar.");
|
||||
return new LaunchResult.CouldNotPrepareMinecraftServerLauncher();
|
||||
}
|
||||
|
||||
if (!File.Exists(serverJar.FilePath)) {
|
||||
logger.Error("Missing prepared server or launcher jar: {FilePath}", serverJar.FilePath);
|
||||
return new LaunchResult.CouldNotPrepareMinecraftServerLauncher();
|
||||
}
|
||||
|
||||
try {
|
||||
await AcceptEula(instanceProperties);
|
||||
await UpdateServerProperties(instanceProperties);
|
||||
} catch (Exception e) {
|
||||
logger.Error(e, "Caught exception while configuring the server.");
|
||||
return new LaunchResult.CouldNotConfigureMinecraftServer();
|
||||
}
|
||||
|
||||
var startInfo = new ProcessStartInfo {
|
||||
FileName = javaRuntimeExecutable.ExecutablePath,
|
||||
WorkingDirectory = instanceProperties.InstanceFolder,
|
||||
@ -43,24 +68,20 @@ public abstract class BaseLauncher {
|
||||
var jvmArguments = new JvmArgumentBuilder(instanceProperties.JvmProperties, instanceProperties.JvmArguments);
|
||||
CustomizeJvmArguments(jvmArguments);
|
||||
|
||||
var serverJarPath = await PrepareServerJar(vanillaServerJarPath, instanceProperties.InstanceFolder, cancellationToken);
|
||||
var processArguments = startInfo.ArgumentList;
|
||||
jvmArguments.Build(processArguments);
|
||||
|
||||
foreach (var extraArgument in serverJar.ExtraArgs) {
|
||||
processArguments.Add(extraArgument);
|
||||
}
|
||||
|
||||
processArguments.Add("-jar");
|
||||
processArguments.Add(serverJarPath);
|
||||
processArguments.Add(serverJar.FilePath);
|
||||
processArguments.Add("nogui");
|
||||
|
||||
var process = new Process { StartInfo = startInfo };
|
||||
var instanceProcess = new InstanceProcess(instanceProperties, process);
|
||||
|
||||
try {
|
||||
await AcceptEula(instanceProperties);
|
||||
await UpdateServerProperties(instanceProperties);
|
||||
} catch (Exception e) {
|
||||
logger.Error(e, "Caught exception while configuring the server.");
|
||||
return new LaunchResult.CouldNotConfigureMinecraftServer();
|
||||
}
|
||||
|
||||
try {
|
||||
process.Start();
|
||||
process.BeginOutputReadLine();
|
||||
@ -82,8 +103,8 @@ public abstract class BaseLauncher {
|
||||
|
||||
private protected virtual void CustomizeJvmArguments(JvmArgumentBuilder arguments) {}
|
||||
|
||||
private protected virtual Task<string> PrepareServerJar(string serverJarPath, string instanceFolderPath, CancellationToken cancellationToken) {
|
||||
return Task.FromResult(serverJarPath);
|
||||
private protected virtual Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, string instanceFolderPath, CancellationToken cancellationToken) {
|
||||
return Task.FromResult(new ServerJarInfo(serverJarPath));
|
||||
}
|
||||
|
||||
private static async Task AcceptEula(InstanceProperties instanceProperties) {
|
||||
|
@ -13,6 +13,8 @@ public abstract record LaunchResult {
|
||||
|
||||
public sealed record CouldNotDownloadMinecraftServer : LaunchResult;
|
||||
|
||||
public sealed record CouldNotPrepareMinecraftServerLauncher : LaunchResult;
|
||||
|
||||
public sealed record CouldNotConfigureMinecraftServer : LaunchResult;
|
||||
|
||||
public sealed record CouldNotStartMinecraftServer : LaunchResult;
|
||||
|
7
Agent/Phantom.Agent.Minecraft/Launcher/ServerJarInfo.cs
Normal file
7
Agent/Phantom.Agent.Minecraft/Launcher/ServerJarInfo.cs
Normal file
@ -0,0 +1,7 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Phantom.Agent.Minecraft.Launcher;
|
||||
|
||||
sealed record ServerJarInfo(string FilePath, ImmutableArray<string> ExtraArgs) {
|
||||
public ServerJarInfo(string filePath) : this(filePath, ImmutableArray<string>.Empty) {}
|
||||
}
|
@ -48,6 +48,9 @@ sealed class InstanceLaunchingState : IInstanceState, IDisposable {
|
||||
else if (launchResult is LaunchResult.CouldNotDownloadMinecraftServer) {
|
||||
throw new LaunchFailureException(InstanceLaunchFailReason.CouldNotDownloadMinecraftServer, "Session failed to launch, could not download Minecraft server.");
|
||||
}
|
||||
else if (launchResult is LaunchResult.CouldNotPrepareMinecraftServerLauncher) {
|
||||
throw new LaunchFailureException(InstanceLaunchFailReason.CouldNotPrepareMinecraftServerLauncher, "Session failed to launch, could not prepare Minecraft server launcher.");
|
||||
}
|
||||
else if (launchResult is LaunchResult.CouldNotConfigureMinecraftServer) {
|
||||
throw new LaunchFailureException(InstanceLaunchFailReason.CouldNotConfigureMinecraftServer, "Session failed to launch, could not configure Minecraft server.");
|
||||
}
|
||||
|
@ -10,22 +10,24 @@ public enum InstanceLaunchFailReason : byte {
|
||||
InvalidJvmArguments,
|
||||
CouldNotDownloadMinecraftServer,
|
||||
CouldNotConfigureMinecraftServer,
|
||||
CouldNotPrepareMinecraftServerLauncher,
|
||||
CouldNotStartMinecraftServer
|
||||
}
|
||||
|
||||
public static class InstanceLaunchFailReasonExtensions {
|
||||
public static string ToSentence(this InstanceLaunchFailReason reason) {
|
||||
return reason switch {
|
||||
InstanceLaunchFailReason.ServerPortNotAllowed => "Server port not allowed.",
|
||||
InstanceLaunchFailReason.ServerPortAlreadyInUse => "Server port already in use.",
|
||||
InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.",
|
||||
InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.",
|
||||
InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.",
|
||||
InstanceLaunchFailReason.InvalidJvmArguments => "Invalid JVM arguments.",
|
||||
InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotConfigureMinecraftServer => "Could not configure Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotStartMinecraftServer => "Could not start Minecraft server.",
|
||||
_ => "Unknown error."
|
||||
InstanceLaunchFailReason.ServerPortNotAllowed => "Server port not allowed.",
|
||||
InstanceLaunchFailReason.ServerPortAlreadyInUse => "Server port already in use.",
|
||||
InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.",
|
||||
InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.",
|
||||
InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.",
|
||||
InstanceLaunchFailReason.InvalidJvmArguments => "Invalid JVM arguments.",
|
||||
InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotConfigureMinecraftServer => "Could not configure Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotPrepareMinecraftServerLauncher => "Could not prepare Minecraft server launcher.",
|
||||
InstanceLaunchFailReason.CouldNotStartMinecraftServer => "Could not start Minecraft server.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user