mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-05-29 10:34:05 +02:00
Add Fabric support
This commit is contained in:
parent
dd0d9b3ddb
commit
33de01f564
Agent
Phantom.Agent.Minecraft/Launcher
Phantom.Agent.Services/Instances
Common/Phantom.Common.Data/Minecraft
@ -9,9 +9,9 @@ using Serilog;
|
|||||||
|
|
||||||
namespace Phantom.Agent.Minecraft.Launcher;
|
namespace Phantom.Agent.Minecraft.Launcher;
|
||||||
|
|
||||||
public abstract class BaseLauncher {
|
public abstract class BaseLauncher : IServerLauncher {
|
||||||
private readonly InstanceProperties instanceProperties;
|
private readonly InstanceProperties instanceProperties;
|
||||||
|
|
||||||
protected string MinecraftVersion => instanceProperties.ServerVersion;
|
protected string MinecraftVersion => instanceProperties.ServerVersion;
|
||||||
|
|
||||||
private protected BaseLauncher(InstanceProperties instanceProperties) {
|
private protected BaseLauncher(InstanceProperties instanceProperties) {
|
||||||
@ -34,7 +34,7 @@ public abstract class BaseLauncher {
|
|||||||
|
|
||||||
ServerJarInfo? serverJar;
|
ServerJarInfo? serverJar;
|
||||||
try {
|
try {
|
||||||
serverJar = await PrepareServerJar(logger, vanillaServerJarPath, instanceProperties.InstanceFolder, cancellationToken);
|
serverJar = await PrepareServerJar(logger, vanillaServerJarPath, cancellationToken);
|
||||||
} catch (OperationCanceledException) {
|
} catch (OperationCanceledException) {
|
||||||
throw;
|
throw;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -103,7 +103,7 @@ public abstract class BaseLauncher {
|
|||||||
|
|
||||||
private protected virtual void CustomizeJvmArguments(JvmArgumentBuilder arguments) {}
|
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));
|
return Task.FromResult(new ServerJarInfo(serverJarPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
@ -18,9 +18,10 @@ sealed class Instance : IDisposable {
|
|||||||
return prefix[..prefix.IndexOf('-')] + "/" + Interlocked.Increment(ref loggerSequenceId);
|
return prefix[..prefix.IndexOf('-')] + "/" + Interlocked.Increment(ref loggerSequenceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public InstanceConfiguration Configuration { get; private set; }
|
|
||||||
private InstanceServices Services { get; }
|
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 string shortName;
|
||||||
private readonly ILogger logger;
|
private readonly ILogger logger;
|
||||||
@ -35,12 +36,12 @@ sealed class Instance : IDisposable {
|
|||||||
|
|
||||||
public event EventHandler? IsRunningChanged;
|
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.shortName = GetLoggerName(configuration.InstanceGuid);
|
||||||
this.logger = PhantomLogger.Create<Instance>(shortName);
|
this.logger = PhantomLogger.Create<Instance>(shortName);
|
||||||
|
|
||||||
this.Configuration = configuration;
|
|
||||||
this.Services = services;
|
this.Services = services;
|
||||||
|
this.Configuration = configuration;
|
||||||
this.Launcher = launcher;
|
this.Launcher = launcher;
|
||||||
|
|
||||||
this.currentState = new InstanceNotRunningState();
|
this.currentState = new InstanceNotRunningState();
|
||||||
@ -100,7 +101,7 @@ sealed class Instance : IDisposable {
|
|||||||
return newStateAndResult.Result;
|
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);
|
await stateTransitioningActionSemaphore.WaitAsync(cancellationToken);
|
||||||
try {
|
try {
|
||||||
Configuration = configuration;
|
Configuration = configuration;
|
||||||
@ -153,7 +154,7 @@ sealed class Instance : IDisposable {
|
|||||||
private readonly Instance instance;
|
private readonly Instance instance;
|
||||||
private readonly CancellationToken shutdownCancellationToken;
|
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.instance = instance;
|
||||||
this.shutdownCancellationToken = shutdownCancellationToken;
|
this.shutdownCancellationToken = shutdownCancellationToken;
|
||||||
}
|
}
|
||||||
|
@ -6,17 +6,17 @@ using Serilog;
|
|||||||
namespace Phantom.Agent.Services.Instances;
|
namespace Phantom.Agent.Services.Instances;
|
||||||
|
|
||||||
abstract class InstanceContext {
|
abstract class InstanceContext {
|
||||||
public InstanceConfiguration Configuration { get; }
|
|
||||||
public BaseLauncher Launcher { get; }
|
|
||||||
public InstanceServices Services { get; }
|
public InstanceServices Services { get; }
|
||||||
|
public InstanceConfiguration Configuration { get; }
|
||||||
|
public IServerLauncher Launcher { get; }
|
||||||
|
|
||||||
public abstract ILogger Logger { get; }
|
public abstract ILogger Logger { get; }
|
||||||
public abstract string ShortName { 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;
|
Configuration = configuration;
|
||||||
Launcher = launcher;
|
Launcher = launcher;
|
||||||
Services = services;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void SetStatus(IInstanceStatus newStatus);
|
public abstract void SetStatus(IInstanceStatus newStatus);
|
||||||
|
@ -27,7 +27,6 @@ sealed class InstanceSessionManager : IDisposable {
|
|||||||
private readonly AgentInfo agentInfo;
|
private readonly AgentInfo agentInfo;
|
||||||
private readonly string basePath;
|
private readonly string basePath;
|
||||||
|
|
||||||
private readonly MinecraftServerExecutables minecraftServerExecutables;
|
|
||||||
private readonly InstanceServices instanceServices;
|
private readonly InstanceServices instanceServices;
|
||||||
private readonly Dictionary<Guid, Instance> instances = new ();
|
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) {
|
public InstanceSessionManager(AgentInfo agentInfo, AgentFolders agentFolders, JavaRuntimeRepository javaRuntimeRepository, TaskManager taskManager, BackupManager backupManager) {
|
||||||
this.agentInfo = agentInfo;
|
this.agentInfo = agentInfo;
|
||||||
this.basePath = agentFolders.InstancesFolderPath;
|
this.basePath = agentFolders.InstancesFolderPath;
|
||||||
this.minecraftServerExecutables = new MinecraftServerExecutables(agentFolders.ServerExecutableFolderPath);
|
|
||||||
this.shutdownCancellationToken = shutdownCancellationTokenSource.Token;
|
this.shutdownCancellationToken = shutdownCancellationTokenSource.Token;
|
||||||
|
|
||||||
|
var minecraftServerExecutables = new MinecraftServerExecutables(agentFolders.ServerExecutableFolderPath);
|
||||||
var launchServices = new LaunchServices(minecraftServerExecutables, javaRuntimeRepository);
|
var launchServices = new LaunchServices(minecraftServerExecutables, javaRuntimeRepository);
|
||||||
var portManager = new PortManager(agentInfo.AllowedServerPorts, agentInfo.AllowedRconPorts);
|
var portManager = new PortManager(agentInfo.AllowedServerPorts, agentInfo.AllowedRconPorts);
|
||||||
|
|
||||||
this.instanceServices = new InstanceServices(taskManager, portManager, backupManager, launchServices);
|
this.instanceServices = new InstanceServices(taskManager, portManager, backupManager, launchServices);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +94,11 @@ sealed class InstanceSessionManager : IDisposable {
|
|||||||
launchProperties
|
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)) {
|
if (instances.TryGetValue(instanceGuid, out var instance)) {
|
||||||
await instance.Reconfigure(configuration, launcher, shutdownCancellationToken);
|
await instance.Reconfigure(configuration, launcher, shutdownCancellationToken);
|
||||||
@ -105,7 +109,7 @@ sealed class InstanceSessionManager : IDisposable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
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);
|
Logger.Information("Created instance \"{Name}\" (GUID {Guid}).", configuration.InstanceName, configuration.InstanceGuid);
|
||||||
|
|
||||||
instance.ReportLastStatus();
|
instance.ReportLastStatus();
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
namespace Phantom.Common.Data.Minecraft;
|
namespace Phantom.Common.Data.Minecraft;
|
||||||
|
|
||||||
public enum MinecraftServerKind : ushort {
|
public enum MinecraftServerKind : ushort {
|
||||||
Vanilla = 1
|
Vanilla = 1,
|
||||||
|
Fabric = 2
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user