1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-26 01:42:53 +01:00

Compare commits

..

2 Commits

6 changed files with 39 additions and 45 deletions

View File

@ -1,5 +1,4 @@
using System.Collections.Immutable; using System.Collections.ObjectModel;
using System.Collections.ObjectModel;
namespace Phantom.Agent.Minecraft.Java; namespace Phantom.Agent.Minecraft.Java;
@ -7,12 +6,12 @@ sealed class JvmArgumentBuilder {
private readonly JvmProperties basicProperties; private readonly JvmProperties basicProperties;
private readonly List<string> customArguments = new (); private readonly List<string> customArguments = new ();
public JvmArgumentBuilder(JvmProperties basicProperties, ImmutableArray<string> customArguments) { public JvmArgumentBuilder(JvmProperties basicProperties) {
this.basicProperties = basicProperties; this.basicProperties = basicProperties;
}
foreach (var jvmArgument in customArguments) { public void Add(string argument) {
this.customArguments.Add(jvmArgument); customArguments.Add(argument);
}
} }
public void AddProperty(string key, string value) { public void AddProperty(string key, string value) {
@ -24,8 +23,8 @@ sealed class JvmArgumentBuilder {
target.Add(property); target.Add(property);
} }
// In case of duplicate JVM arguments, typically the last one wins.
target.Add("-Xms" + basicProperties.InitialHeapMegabytes + "M"); target.Add("-Xms" + basicProperties.InitialHeapMegabytes + "M");
target.Add("-Xmx" + basicProperties.MaximumHeapMegabytes + "M"); target.Add("-Xmx" + basicProperties.MaximumHeapMegabytes + "M");
target.Add("-Xrs");
} }
} }

View File

@ -2,7 +2,6 @@
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;
using Phantom.Common.Data.Java;
using Phantom.Utils.Runtime; using Phantom.Utils.Runtime;
using Serilog; using Serilog;
@ -22,10 +21,6 @@ public abstract class BaseLauncher : IServerLauncher {
return new LaunchResult.InvalidJavaRuntime(); return new LaunchResult.InvalidJavaRuntime();
} }
if (JvmArgumentsHelper.Validate(instanceProperties.JvmArguments) != null) {
return new LaunchResult.InvalidJvmArguments();
}
var vanillaServerJarPath = await services.ServerExecutables.DownloadAndGetPath(instanceProperties.LaunchProperties.ServerDownloadInfo, MinecraftVersion, downloadProgressEventHandler, cancellationToken); var vanillaServerJarPath = await services.ServerExecutables.DownloadAndGetPath(instanceProperties.LaunchProperties.ServerDownloadInfo, MinecraftVersion, downloadProgressEventHandler, cancellationToken);
if (vanillaServerJarPath == null) { if (vanillaServerJarPath == null) {
return new LaunchResult.CouldNotDownloadMinecraftServer(); return new LaunchResult.CouldNotDownloadMinecraftServer();
@ -61,16 +56,8 @@ public abstract class BaseLauncher : IServerLauncher {
UseShellExecute = false UseShellExecute = false
}; };
var jvmArguments = new JvmArgumentBuilder(instanceProperties.JvmProperties, instanceProperties.JvmArguments);
CustomizeJvmArguments(jvmArguments);
var processArguments = processConfigurator.ArgumentList; var processArguments = processConfigurator.ArgumentList;
jvmArguments.Build(processArguments); PrepareJvmArguments(serverJar).Build(processArguments);
foreach (var extraArgument in serverJar.ExtraArgs) {
processArguments.Add(extraArgument);
}
processArguments.Add("-jar"); processArguments.Add("-jar");
processArguments.Add(serverJar.FilePath); processArguments.Add(serverJar.FilePath);
processArguments.Add("nogui"); processArguments.Add("nogui");
@ -95,6 +82,21 @@ public abstract class BaseLauncher : IServerLauncher {
return new LaunchResult.Success(instanceProcess); return new LaunchResult.Success(instanceProcess);
} }
private JvmArgumentBuilder PrepareJvmArguments(ServerJarInfo serverJar) {
var builder = new JvmArgumentBuilder(instanceProperties.JvmProperties);
foreach (string argument in instanceProperties.JvmArguments) {
builder.Add(argument);
}
foreach (var argument in serverJar.ExtraArgs) {
builder.Add(argument);
}
CustomizeJvmArguments(builder);
return builder;
}
private protected virtual void CustomizeJvmArguments(JvmArgumentBuilder arguments) {} private protected virtual void CustomizeJvmArguments(JvmArgumentBuilder arguments) {}
private protected virtual Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) { private protected virtual Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) {

View File

@ -8,8 +8,6 @@ public abstract record LaunchResult {
public sealed record Success(InstanceProcess Process) : LaunchResult; public sealed record Success(InstanceProcess Process) : LaunchResult;
public sealed record InvalidJavaRuntime : LaunchResult; public sealed record InvalidJavaRuntime : LaunchResult;
public sealed record InvalidJvmArguments : LaunchResult;
public sealed record CouldNotDownloadMinecraftServer : LaunchResult; public sealed record CouldNotDownloadMinecraftServer : LaunchResult;

View File

@ -63,9 +63,6 @@ sealed record LaunchInstanceProcedure(InstanceConfiguration Configuration, IServ
if (launchResult is LaunchResult.InvalidJavaRuntime) { if (launchResult is LaunchResult.InvalidJavaRuntime) {
throw new LaunchFailureException(InstanceLaunchFailReason.JavaRuntimeNotFound, "Session failed to launch, invalid Java runtime."); throw new LaunchFailureException(InstanceLaunchFailReason.JavaRuntimeNotFound, "Session failed to launch, invalid Java runtime.");
} }
else if (launchResult is LaunchResult.InvalidJvmArguments) {
throw new LaunchFailureException(InstanceLaunchFailReason.InvalidJvmArguments, "Session failed to launch, invalid JVM arguments.");
}
else if (launchResult is LaunchResult.CouldNotDownloadMinecraftServer) { else if (launchResult is LaunchResult.CouldNotDownloadMinecraftServer) {
throw new LaunchFailureException(InstanceLaunchFailReason.CouldNotDownloadMinecraftServer, "Session failed to launch, could not download Minecraft server."); throw new LaunchFailureException(InstanceLaunchFailReason.CouldNotDownloadMinecraftServer, "Session failed to launch, could not download Minecraft server.");
} }

View File

@ -1,17 +1,16 @@
namespace Phantom.Common.Data.Instance; namespace Phantom.Common.Data.Instance;
public enum InstanceLaunchFailReason : byte { public enum InstanceLaunchFailReason : byte {
UnknownError, UnknownError = 0,
ServerPortNotAllowed, ServerPortNotAllowed = 1,
ServerPortAlreadyInUse, ServerPortAlreadyInUse = 2,
RconPortNotAllowed, RconPortNotAllowed = 3,
RconPortAlreadyInUse, RconPortAlreadyInUse = 4,
JavaRuntimeNotFound, JavaRuntimeNotFound = 5,
InvalidJvmArguments, CouldNotDownloadMinecraftServer = 6,
CouldNotDownloadMinecraftServer, CouldNotConfigureMinecraftServer = 7,
CouldNotConfigureMinecraftServer, CouldNotPrepareMinecraftServerLauncher = 8,
CouldNotPrepareMinecraftServerLauncher, CouldNotStartMinecraftServer = 9
CouldNotStartMinecraftServer
} }
public static class InstanceLaunchFailReasonExtensions { public static class InstanceLaunchFailReasonExtensions {
@ -22,7 +21,6 @@ public static class InstanceLaunchFailReasonExtensions {
InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.", InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.",
InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.", InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.",
InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.", InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.",
InstanceLaunchFailReason.InvalidJvmArguments => "Invalid JVM arguments.",
InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.", InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.",
InstanceLaunchFailReason.CouldNotConfigureMinecraftServer => "Could not configure Minecraft server.", InstanceLaunchFailReason.CouldNotConfigureMinecraftServer => "Could not configure Minecraft server.",
InstanceLaunchFailReason.CouldNotPrepareMinecraftServerLauncher => "Could not prepare Minecraft server launcher.", InstanceLaunchFailReason.CouldNotPrepareMinecraftServerLauncher => "Could not prepare Minecraft server launcher.",

View File

@ -1,17 +1,21 @@
using System.Collections.Immutable; using System.Collections.Immutable;
namespace Phantom.Common.Data.Java; namespace Phantom.Server.Minecraft;
public static class JvmArgumentsHelper { public static class JvmArgumentsHelper {
public static ImmutableArray<string> Split(string arguments) { public static ImmutableArray<string> Split(string arguments) {
return arguments.Split('\n', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).ToImmutableArray(); return arguments.Split('\n', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).ToImmutableArray();
} }
public static string Join(ImmutableArray<string> arguments) {
return string.Join('\n', arguments);
}
public static ValidationError? Validate(string arguments) { public static ValidationError? Validate(string arguments) {
return Validate(Split(arguments)); return Validate(Split(arguments));
} }
public static ValidationError? Validate(ImmutableArray<string> arguments) { private static ValidationError? Validate(ImmutableArray<string> arguments) {
if (!arguments.All(static argument => argument.StartsWith('-'))) { if (!arguments.All(static argument => argument.StartsWith('-'))) {
return ValidationError.InvalidFormat; return ValidationError.InvalidFormat;
} }
@ -28,10 +32,6 @@ public static class JvmArgumentsHelper {
return null; return null;
} }
public static string Join(ImmutableArray<string> arguments) {
return string.Join('\n', arguments);
}
public enum ValidationError { public enum ValidationError {
InvalidFormat, InvalidFormat,
XmxNotAllowed, XmxNotAllowed,