1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-04-10 20:15:44 +02:00

Add Fabric support

This commit is contained in:
chylex 2023-02-26 19:07:13 +01:00
parent dd0d9b3ddb
commit 33de01f564
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
8 changed files with 102 additions and 19 deletions
Agent
Common/Phantom.Common.Data/Minecraft

View File

@ -9,9 +9,9 @@ using Serilog;
namespace Phantom.Agent.Minecraft.Launcher;
public abstract class BaseLauncher {
public abstract class BaseLauncher : IServerLauncher {
private readonly InstanceProperties instanceProperties;
protected string MinecraftVersion => instanceProperties.ServerVersion;
private protected BaseLauncher(InstanceProperties instanceProperties) {
@ -34,7 +34,7 @@ public abstract class BaseLauncher {
ServerJarInfo? serverJar;
try {
serverJar = await PrepareServerJar(logger, vanillaServerJarPath, instanceProperties.InstanceFolder, cancellationToken);
serverJar = await PrepareServerJar(logger, vanillaServerJarPath, cancellationToken);
} catch (OperationCanceledException) {
throw;
} catch (Exception e) {
@ -103,7 +103,7 @@ public abstract class BaseLauncher {
private protected virtual void CustomizeJvmArguments(JvmArgumentBuilder arguments) {}
private protected virtual Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, string instanceFolderPath, CancellationToken cancellationToken) {
private protected virtual Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) {
return Task.FromResult(new ServerJarInfo(serverJarPath));
}

View File

@ -0,0 +1,8 @@
using Phantom.Agent.Minecraft.Server;
using Serilog;
namespace Phantom.Agent.Minecraft.Launcher;
public interface IServerLauncher {
Task<LaunchResult> Launch(ILogger logger, LaunchServices services, EventHandler<DownloadProgressEventArgs> downloadProgressEventHandler, CancellationToken cancellationToken);
}

View File

@ -0,0 +1,55 @@
using System.Collections.Immutable;
using Phantom.Agent.Minecraft.Instance;
using Phantom.Utils.IO;
using Serilog;
namespace Phantom.Agent.Minecraft.Launcher.Types;
public sealed class FabricLauncher : BaseLauncher {
public FabricLauncher(InstanceProperties instanceProperties) : base(instanceProperties) {}
private protected override async Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) {
var serverJarParentFolderPath = Directory.GetParent(serverJarPath);
if (serverJarParentFolderPath == null) {
throw new ArgumentException("Could not get parent folder from: " + serverJarPath, nameof(serverJarPath));
}
var launcherJarPath = Path.Combine(serverJarParentFolderPath.FullName, "fabric.jar");
if (!File.Exists(launcherJarPath)) {
await DownloadLauncher(logger, launcherJarPath, cancellationToken);
}
return new ServerJarInfo(launcherJarPath, ImmutableArray.Create("-Dfabric.installer.server.gameJar=" + Paths.NormalizeSlashes(serverJarPath)));
}
private async Task DownloadLauncher(ILogger logger, string targetFilePath, CancellationToken cancellationToken) {
// TODO customizable loader version, probably with a dedicated temporary folder
string installerUrl = $"https://meta.fabricmc.net/v2/versions/loader/{MinecraftVersion}/stable/stable/server/jar";
logger.Information("Downloading Fabric launcher from: {Url}", installerUrl);
using var http = new HttpClient();
var response = await http.GetAsync(installerUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
response.EnsureSuccessStatusCode();
try {
await using var fileStream = new FileStream(targetFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.Read);
await using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
await responseStream.CopyToAsync(fileStream, cancellationToken);
} catch (Exception) {
TryDeleteLauncherAfterFailure(logger, targetFilePath);
throw;
}
}
private static void TryDeleteLauncherAfterFailure(ILogger logger, string filePath) {
if (File.Exists(filePath)) {
try {
File.Delete(filePath);
} catch (Exception e) {
logger.Warning(e, "Could not clean up partially downloaded Fabric launcher: {FilePath}", filePath);
}
}
}
}

View File

@ -0,0 +1,14 @@
using Phantom.Agent.Minecraft.Server;
using Serilog;
namespace Phantom.Agent.Minecraft.Launcher.Types;
public sealed class InvalidLauncher : IServerLauncher {
public static InvalidLauncher Instance { get; } = new ();
private InvalidLauncher() {}
public Task<LaunchResult> Launch(ILogger logger, LaunchServices services, EventHandler<DownloadProgressEventArgs> downloadProgressEventHandler, CancellationToken cancellationToken) {
return Task.FromResult<LaunchResult>(new LaunchResult.CouldNotPrepareMinecraftServerLauncher());
}
}

View File

@ -18,9 +18,10 @@ sealed class Instance : IDisposable {
return prefix[..prefix.IndexOf('-')] + "/" + Interlocked.Increment(ref loggerSequenceId);
}
public InstanceConfiguration Configuration { get; private set; }
private InstanceServices Services { get; }
private BaseLauncher Launcher { get; set; }
public InstanceConfiguration Configuration { get; private set; }
private IServerLauncher Launcher { get; set; }
private readonly string shortName;
private readonly ILogger logger;
@ -35,12 +36,12 @@ sealed class Instance : IDisposable {
public event EventHandler? IsRunningChanged;
public Instance(InstanceConfiguration configuration, InstanceServices services, BaseLauncher launcher) {
public Instance(InstanceServices services, InstanceConfiguration configuration, IServerLauncher launcher) {
this.shortName = GetLoggerName(configuration.InstanceGuid);
this.logger = PhantomLogger.Create<Instance>(shortName);
this.Configuration = configuration;
this.Services = services;
this.Configuration = configuration;
this.Launcher = launcher;
this.currentState = new InstanceNotRunningState();
@ -100,7 +101,7 @@ sealed class Instance : IDisposable {
return newStateAndResult.Result;
}
public async Task Reconfigure(InstanceConfiguration configuration, BaseLauncher launcher, CancellationToken cancellationToken) {
public async Task Reconfigure(InstanceConfiguration configuration, IServerLauncher launcher, CancellationToken cancellationToken) {
await stateTransitioningActionSemaphore.WaitAsync(cancellationToken);
try {
Configuration = configuration;
@ -153,7 +154,7 @@ sealed class Instance : IDisposable {
private readonly Instance instance;
private readonly CancellationToken shutdownCancellationToken;
public InstanceContextImpl(Instance instance, CancellationToken shutdownCancellationToken) : base(instance.Configuration, instance.Launcher, instance.Services) {
public InstanceContextImpl(Instance instance, CancellationToken shutdownCancellationToken) : base(instance.Services, instance.Configuration, instance.Launcher) {
this.instance = instance;
this.shutdownCancellationToken = shutdownCancellationToken;
}

View File

@ -6,17 +6,17 @@ using Serilog;
namespace Phantom.Agent.Services.Instances;
abstract class InstanceContext {
public InstanceConfiguration Configuration { get; }
public BaseLauncher Launcher { get; }
public InstanceServices Services { get; }
public InstanceConfiguration Configuration { get; }
public IServerLauncher Launcher { get; }
public abstract ILogger Logger { get; }
public abstract string ShortName { get; }
protected InstanceContext(InstanceConfiguration configuration, BaseLauncher launcher, InstanceServices services) {
protected InstanceContext(InstanceServices services, InstanceConfiguration configuration, IServerLauncher launcher) {
Services = services;
Configuration = configuration;
Launcher = launcher;
Services = services;
}
public abstract void SetStatus(IInstanceStatus newStatus);

View File

@ -27,7 +27,6 @@ sealed class InstanceSessionManager : IDisposable {
private readonly AgentInfo agentInfo;
private readonly string basePath;
private readonly MinecraftServerExecutables minecraftServerExecutables;
private readonly InstanceServices instanceServices;
private readonly Dictionary<Guid, Instance> instances = new ();
@ -38,11 +37,12 @@ sealed class InstanceSessionManager : IDisposable {
public InstanceSessionManager(AgentInfo agentInfo, AgentFolders agentFolders, JavaRuntimeRepository javaRuntimeRepository, TaskManager taskManager, BackupManager backupManager) {
this.agentInfo = agentInfo;
this.basePath = agentFolders.InstancesFolderPath;
this.minecraftServerExecutables = new MinecraftServerExecutables(agentFolders.ServerExecutableFolderPath);
this.shutdownCancellationToken = shutdownCancellationTokenSource.Token;
var minecraftServerExecutables = new MinecraftServerExecutables(agentFolders.ServerExecutableFolderPath);
var launchServices = new LaunchServices(minecraftServerExecutables, javaRuntimeRepository);
var portManager = new PortManager(agentInfo.AllowedServerPorts, agentInfo.AllowedRconPorts);
this.instanceServices = new InstanceServices(taskManager, portManager, backupManager, launchServices);
}
@ -94,7 +94,11 @@ sealed class InstanceSessionManager : IDisposable {
launchProperties
);
BaseLauncher launcher = new VanillaLauncher(properties);
IServerLauncher launcher = configuration.MinecraftServerKind switch {
MinecraftServerKind.Vanilla => new VanillaLauncher(properties),
MinecraftServerKind.Fabric => new FabricLauncher(properties),
_ => InvalidLauncher.Instance
};
if (instances.TryGetValue(instanceGuid, out var instance)) {
await instance.Reconfigure(configuration, launcher, shutdownCancellationToken);
@ -105,7 +109,7 @@ sealed class InstanceSessionManager : IDisposable {
}
}
else {
instances[instanceGuid] = instance = new Instance(configuration, instanceServices, launcher);
instances[instanceGuid] = instance = new Instance(instanceServices, configuration, launcher);
Logger.Information("Created instance \"{Name}\" (GUID {Guid}).", configuration.InstanceName, configuration.InstanceGuid);
instance.ReportLastStatus();

View File

@ -1,5 +1,6 @@
namespace Phantom.Common.Data.Minecraft;
public enum MinecraftServerKind : ushort {
Vanilla = 1
Vanilla = 1,
Fabric = 2
}