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

9 Commits

37 changed files with 678 additions and 363 deletions

View File

@@ -1,5 +1,4 @@
using System.Diagnostics; using Phantom.Utils.Collections;
using Phantom.Utils.Collections;
using Phantom.Utils.Runtime; using Phantom.Utils.Runtime;
namespace Phantom.Agent.Minecraft.Instance; namespace Phantom.Agent.Minecraft.Instance;
@@ -19,10 +18,8 @@ public sealed class InstanceProcess : IDisposable {
internal InstanceProcess(InstanceProperties instanceProperties, Process process) { internal InstanceProcess(InstanceProperties instanceProperties, Process process) {
this.InstanceProperties = instanceProperties; this.InstanceProperties = instanceProperties;
this.process = process; this.process = process;
this.process.EnableRaisingEvents = true;
this.process.Exited += ProcessOnExited; this.process.Exited += ProcessOnExited;
this.process.OutputDataReceived += HandleOutputLine; this.process.OutputReceived += ProcessOutputReceived;
this.process.ErrorDataReceived += HandleOutputLine;
} }
public async Task SendCommand(string command, CancellationToken cancellationToken) { public async Task SendCommand(string command, CancellationToken cancellationToken) {
@@ -41,11 +38,9 @@ public sealed class InstanceProcess : IDisposable {
OutputEvent -= listener; OutputEvent -= listener;
} }
private void HandleOutputLine(object sender, DataReceivedEventArgs args) { private void ProcessOutputReceived(object? sender, Process.Output output) {
if (args.Data is {} line) { outputBuffer.Add(output.Line);
outputBuffer.Add(line); OutputEvent?.Invoke(this, output.Line);
OutputEvent?.Invoke(this, line);
}
} }
private void ProcessOnExited(object? sender, EventArgs e) { private void ProcessOnExited(object? sender, EventArgs e) {

View File

@@ -1,15 +1,15 @@
using System.Diagnostics; using System.Text;
using System.Text;
using Kajabity.Tools.Java; using Kajabity.Tools.Java;
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.Minecraft; using Phantom.Common.Minecraft;
using Phantom.Utils.Runtime;
using Serilog; 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;
@@ -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) {
@@ -55,20 +55,17 @@ public abstract class BaseLauncher {
return new LaunchResult.CouldNotConfigureMinecraftServer(); return new LaunchResult.CouldNotConfigureMinecraftServer();
} }
var startInfo = new ProcessStartInfo { var processConfigurator = new ProcessConfigurator {
FileName = javaRuntimeExecutable.ExecutablePath, FileName = javaRuntimeExecutable.ExecutablePath,
WorkingDirectory = instanceProperties.InstanceFolder, WorkingDirectory = instanceProperties.InstanceFolder,
RedirectStandardInput = true, RedirectInput = true,
RedirectStandardOutput = true, UseShellExecute = false
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = false
}; };
var jvmArguments = new JvmArgumentBuilder(instanceProperties.JvmProperties, instanceProperties.JvmArguments); var jvmArguments = new JvmArgumentBuilder(instanceProperties.JvmProperties, instanceProperties.JvmArguments);
CustomizeJvmArguments(jvmArguments); CustomizeJvmArguments(jvmArguments);
var processArguments = startInfo.ArgumentList; var processArguments = processConfigurator.ArgumentList;
jvmArguments.Build(processArguments); jvmArguments.Build(processArguments);
foreach (var extraArgument in serverJar.ExtraArgs) { foreach (var extraArgument in serverJar.ExtraArgs) {
@@ -79,13 +76,11 @@ public abstract class BaseLauncher {
processArguments.Add(serverJar.FilePath); processArguments.Add(serverJar.FilePath);
processArguments.Add("nogui"); processArguments.Add("nogui");
var process = new Process { StartInfo = startInfo }; var process = processConfigurator.CreateProcess();
var instanceProcess = new InstanceProcess(instanceProperties, process); var instanceProcess = new InstanceProcess(instanceProperties, process);
try { try {
process.Start(); process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
} catch (Exception launchException) { } catch (Exception launchException) {
logger.Error(launchException, "Caught exception launching the server process."); logger.Error(launchException, "Caught exception launching the server process.");
@@ -103,7 +98,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));
} }

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,12 @@
using Phantom.Agent.Minecraft.Instance;
using Phantom.Agent.Minecraft.Java;
namespace Phantom.Agent.Minecraft.Launcher.Types;
public class ForgeLauncher : BaseLauncher {
public ForgeLauncher(InstanceProperties instanceProperties) : base(instanceProperties) {}
private protected override void CustomizeJvmArguments(JvmArgumentBuilder arguments) {
arguments.AddProperty("terminal.ansi", "true"); // TODO
}
}

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

@@ -26,7 +26,7 @@ sealed class MinecraftServerExecutableDownloader {
public void Register(MinecraftServerExecutableDownloadListener listener) { public void Register(MinecraftServerExecutableDownloadListener listener) {
++listeners; ++listeners;
Logger.Verbose("Registered download listener, current listener count: {Listeners}", listeners); Logger.Debug("Registered download listener, current listener count: {Listeners}", listeners);
DownloadProgress += listener.DownloadProgressEventHandler; DownloadProgress += listener.DownloadProgressEventHandler;
listener.CancellationToken.Register(Unregister, listener); listener.CancellationToken.Register(Unregister, listener);
@@ -37,11 +37,11 @@ sealed class MinecraftServerExecutableDownloader {
DownloadProgress -= listener.DownloadProgressEventHandler; DownloadProgress -= listener.DownloadProgressEventHandler;
if (--listeners <= 0) { if (--listeners <= 0) {
Logger.Verbose("Unregistered last download listener, cancelling download."); Logger.Debug("Unregistered last download listener, cancelling download.");
cancellationTokenSource.Cancel(); cancellationTokenSource.Cancel();
} }
else { else {
Logger.Verbose("Unregistered download listener, current listener count: {Listeners}", listeners); Logger.Debug("Unregistered download listener, current listener count: {Listeners}", listeners);
} }
} }
@@ -50,7 +50,7 @@ sealed class MinecraftServerExecutableDownloader {
} }
private void OnCompleted(Task task) { private void OnCompleted(Task task) {
Logger.Verbose("Download task completed."); Logger.Debug("Download task completed.");
Completed?.Invoke(this, EventArgs.Empty); Completed?.Invoke(this, EventArgs.Empty);
Completed = null; Completed = null;
DownloadProgress = null; DownloadProgress = null;

View File

@@ -84,7 +84,7 @@ public sealed class ServerStatusProtocol {
return null; return null;
} }
logger.Verbose("Detected {OnlinePlayerCount} online player(s).", onlinePlayerCount); logger.Debug("Detected {OnlinePlayerCount} online player(s).", onlinePlayerCount);
return onlinePlayerCount; return onlinePlayerCount;
} finally { } finally {
ArrayPool<byte>.Shared.Return(messageBuffer); ArrayPool<byte>.Shared.Return(messageBuffer);

View File

@@ -69,7 +69,7 @@ public sealed class RpcLauncher : RpcRuntime<ClientSocket> {
} catch (OperationCanceledException) { } catch (OperationCanceledException) {
// Ignore. // Ignore.
} finally { } finally {
logger.Verbose("ZeroMQ client stopped receiving messages."); logger.Debug("ZeroMQ client stopped receiving messages.");
disconnectSemaphore.Wait(CancellationToken.None); disconnectSemaphore.Wait(CancellationToken.None);
keepAliveLoop.Cancel(); keepAliveLoop.Cancel();

View File

@@ -44,7 +44,7 @@ sealed class BackupArchiver {
return false; return false;
} }
public async Task ArchiveWorld(BackupCreationResult.Builder resultBuilder) { public async Task<string?> ArchiveWorld(BackupCreationResult.Builder resultBuilder) {
string guid = instanceProperties.InstanceGuid.ToString(); string guid = instanceProperties.InstanceGuid.ToString();
string currentDateTime = DateTime.Now.ToString("yyyyMMdd-HHmmss"); string currentDateTime = DateTime.Now.ToString("yyyyMMdd-HHmmss");
string backupFolderPath = Path.Combine(destinationBasePath, guid); string backupFolderPath = Path.Combine(destinationBasePath, guid);
@@ -53,7 +53,7 @@ sealed class BackupArchiver {
if (File.Exists(backupFilePath)) { if (File.Exists(backupFilePath)) {
resultBuilder.Kind = BackupCreationResultKind.BackupFileAlreadyExists; resultBuilder.Kind = BackupCreationResultKind.BackupFileAlreadyExists;
logger.Warning("Skipping backup, file already exists: {File}", backupFilePath); logger.Warning("Skipping backup, file already exists: {File}", backupFilePath);
return; return null;
} }
try { try {
@@ -61,18 +61,16 @@ sealed class BackupArchiver {
} catch (Exception e) { } catch (Exception e) {
resultBuilder.Kind = BackupCreationResultKind.CouldNotCreateBackupFolder; resultBuilder.Kind = BackupCreationResultKind.CouldNotCreateBackupFolder;
logger.Error(e, "Could not create backup folder: {Folder}", backupFolderPath); logger.Error(e, "Could not create backup folder: {Folder}", backupFolderPath);
return; return null;
} }
string temporaryFolderPath = Path.Combine(temporaryBasePath, guid + "_" + currentDateTime); string temporaryFolderPath = Path.Combine(temporaryBasePath, guid + "_" + currentDateTime);
if (!await CopyWorldAndCreateTarArchive(temporaryFolderPath, backupFilePath, resultBuilder)) { if (!await CopyWorldAndCreateTarArchive(temporaryFolderPath, backupFilePath, resultBuilder)) {
return; return null;
} }
var compressedFilePath = await BackupCompressor.Compress(backupFilePath, cancellationToken); logger.Debug("Created world backup: {FilePath}", backupFilePath);
if (compressedFilePath == null) { return backupFilePath;
resultBuilder.Warnings |= BackupCreationWarnings.CouldNotCompressWorldArchive;
}
} }
private async Task<bool> CopyWorldAndCreateTarArchive(string temporaryFolderPath, string backupFilePath, BackupCreationResult.Builder resultBuilder) { private async Task<bool> CopyWorldAndCreateTarArchive(string temporaryFolderPath, string backupFilePath, BackupCreationResult.Builder resultBuilder) {
@@ -137,7 +135,7 @@ sealed class BackupArchiver {
foreach (FileInfo file in sourceFolder.EnumerateFiles()) { foreach (FileInfo file in sourceFolder.EnumerateFiles()) {
var filePath = relativePath.Add(file.Name); var filePath = relativePath.Add(file.Name);
if (IsFileSkipped(filePath)) { if (IsFileSkipped(filePath)) {
logger.Verbose("Skipping file: {File}", string.Join('/', filePath)); logger.Debug("Skipping file: {File}", string.Join('/', filePath));
continue; continue;
} }
@@ -152,7 +150,7 @@ sealed class BackupArchiver {
foreach (DirectoryInfo directory in sourceFolder.EnumerateDirectories()) { foreach (DirectoryInfo directory in sourceFolder.EnumerateDirectories()) {
var folderPath = relativePath.Add(directory.Name); var folderPath = relativePath.Add(directory.Name);
if (IsFolderSkipped(folderPath)) { if (IsFolderSkipped(folderPath)) {
logger.Verbose("Skipping folder: {Folder}", string.Join('/', folderPath)); logger.Debug("Skipping folder: {Folder}", string.Join('/', folderPath));
continue; continue;
} }

View File

@@ -1,5 +1,4 @@
using System.Diagnostics; using Phantom.Common.Logging;
using Phantom.Common.Logging;
using Phantom.Utils.Runtime; using Phantom.Utils.Runtime;
using Serilog; using Serilog;
@@ -41,7 +40,7 @@ static class BackupCompressor {
return false; return false;
} }
var startInfo = new ProcessStartInfo { var launcher = new ProcessConfigurator {
FileName = "zstd", FileName = "zstd",
WorkingDirectory = workingDirectory, WorkingDirectory = workingDirectory,
ArgumentList = { ArgumentList = {
@@ -57,14 +56,14 @@ static class BackupCompressor {
} }
}; };
static void OnZstdOutput(object? sender, DataReceivedEventArgs e) { static void OnZstdOutput(object? sender, Process.Output output) {
if (!string.IsNullOrWhiteSpace(e.Data)) { if (!string.IsNullOrWhiteSpace(output.Line)) {
ZstdLogger.Verbose("[Output] {Line}", e.Data); ZstdLogger.Debug("[Output] {Line}", output.Line);
} }
} }
var process = new OneShotProcess(ZstdLogger, startInfo); var process = new OneShotProcess(ZstdLogger, launcher);
process.Output += OnZstdOutput; process.OutputReceived += OnZstdOutput;
return await process.Run(cancellationToken); return await process.Run(cancellationToken);
} }
} }

View File

@@ -1,13 +1,11 @@
using System.Text.RegularExpressions; using Phantom.Agent.Minecraft.Instance;
using Phantom.Agent.Minecraft.Command;
using Phantom.Agent.Minecraft.Instance;
using Phantom.Common.Data.Backups; using Phantom.Common.Data.Backups;
using Phantom.Common.Logging; using Phantom.Common.Logging;
using Serilog; using Serilog;
namespace Phantom.Agent.Services.Backups; namespace Phantom.Agent.Services.Backups;
sealed partial class BackupManager { sealed class BackupManager {
private readonly string destinationBasePath; private readonly string destinationBasePath;
private readonly string temporaryBasePath; private readonly string temporaryBasePath;
@@ -40,7 +38,6 @@ sealed partial class BackupManager {
private readonly string loggerName; private readonly string loggerName;
private readonly ILogger logger; private readonly ILogger logger;
private readonly InstanceProcess process; private readonly InstanceProcess process;
private readonly BackupCommandListener listener;
private readonly CancellationToken cancellationToken; private readonly CancellationToken cancellationToken;
public BackupCreator(string destinationBasePath, string temporaryBasePath, string loggerName, InstanceProcess process, CancellationToken cancellationToken) { public BackupCreator(string destinationBasePath, string temporaryBasePath, string loggerName, InstanceProcess process, CancellationToken cancellationToken) {
@@ -49,52 +46,44 @@ sealed partial class BackupManager {
this.loggerName = loggerName; this.loggerName = loggerName;
this.logger = PhantomLogger.Create<BackupManager>(loggerName); this.logger = PhantomLogger.Create<BackupManager>(loggerName);
this.process = process; this.process = process;
this.listener = new BackupCommandListener(logger);
this.cancellationToken = cancellationToken; this.cancellationToken = cancellationToken;
} }
public async Task<BackupCreationResult> CreateBackup() { public async Task<BackupCreationResult> CreateBackup() {
logger.Information("Backup started."); logger.Information("Backup started.");
process.AddOutputListener(listener.OnOutput, maxLinesToReadFromHistory: 0);
try {
var resultBuilder = new BackupCreationResult.Builder();
await RunBackupProcedure(resultBuilder); var resultBuilder = new BackupCreationResult.Builder();
string? backupFilePath;
using (var dispatcher = new BackupServerCommandDispatcher(logger, process, cancellationToken)) {
backupFilePath = await CreateWorldArchive(dispatcher, resultBuilder);
}
if (backupFilePath != null) {
await CompressWorldArchive(backupFilePath, resultBuilder);
}
var result = resultBuilder.Build(); var result = resultBuilder.Build();
if (result.Kind == BackupCreationResultKind.Success) { LogBackupResult(result);
var warningCount = result.Warnings.Count();
if (warningCount == 0) {
logger.Information("Backup finished successfully.");
}
else {
logger.Warning("Backup finished with {Warnings} warning(s).", warningCount);
}
}
else {
logger.Warning("Backup failed: {Reason}", result.Kind.ToSentence());
}
return result; return result;
} finally {
process.RemoveOutputListener(listener.OnOutput);
}
} }
private async Task RunBackupProcedure(BackupCreationResult.Builder resultBuilder) { private async Task<string?> CreateWorldArchive(BackupServerCommandDispatcher dispatcher, BackupCreationResult.Builder resultBuilder) {
try { try {
await DisableAutomaticSaving(); await dispatcher.DisableAutomaticSaving();
await SaveAllChunks(); await dispatcher.SaveAllChunks();
await new BackupArchiver(destinationBasePath, temporaryBasePath, loggerName, process.InstanceProperties, cancellationToken).ArchiveWorld(resultBuilder); return await new BackupArchiver(destinationBasePath, temporaryBasePath, loggerName, process.InstanceProperties, cancellationToken).ArchiveWorld(resultBuilder);
} catch (OperationCanceledException) { } catch (OperationCanceledException) {
resultBuilder.Kind = BackupCreationResultKind.BackupCancelled; resultBuilder.Kind = BackupCreationResultKind.BackupCancelled;
logger.Warning("Backup creation was cancelled."); logger.Warning("Backup creation was cancelled.");
return null;
} catch (Exception e) { } catch (Exception e) {
resultBuilder.Kind = BackupCreationResultKind.UnknownError; resultBuilder.Kind = BackupCreationResultKind.UnknownError;
logger.Error(e, "Caught exception while creating an instance backup."); logger.Error(e, "Caught exception while creating an instance backup.");
return null;
} finally { } finally {
try { try {
await EnableAutomaticSaving(); await dispatcher.EnableAutomaticSaving();
} catch (OperationCanceledException) { } catch (OperationCanceledException) {
// ignore // ignore
} catch (Exception e) { } catch (Exception e) {
@@ -104,66 +93,25 @@ sealed partial class BackupManager {
} }
} }
private async Task DisableAutomaticSaving() { private async Task CompressWorldArchive(string filePath, BackupCreationResult.Builder resultBuilder) {
await process.SendCommand(MinecraftCommand.SaveOff, cancellationToken); var compressedFilePath = await BackupCompressor.Compress(filePath, cancellationToken);
await listener.AutomaticSavingDisabled.Task.WaitAsync(cancellationToken); if (compressedFilePath == null) {
} resultBuilder.Warnings |= BackupCreationWarnings.CouldNotCompressWorldArchive;
private async Task SaveAllChunks() {
// TODO Try if not flushing and waiting a few seconds before flushing reduces lag.
await process.SendCommand(MinecraftCommand.SaveAll(flush: true), cancellationToken);
await listener.SavedTheGame.Task.WaitAsync(cancellationToken);
}
private async Task EnableAutomaticSaving() {
await process.SendCommand(MinecraftCommand.SaveOn, cancellationToken);
await listener.AutomaticSavingEnabled.Task.WaitAsync(cancellationToken);
} }
} }
private sealed partial class BackupCommandListener { private void LogBackupResult(BackupCreationResult result) {
[GeneratedRegex(@"^\[(?:.*?)\] \[Server thread/INFO\]: (.*?)$", RegexOptions.NonBacktracking)] if (result.Kind != BackupCreationResultKind.Success) {
private static partial Regex ServerThreadInfoRegex(); logger.Warning("Backup failed: {Reason}", result.Kind.ToSentence());
private readonly ILogger logger;
public BackupCommandListener(ILogger logger) {
this.logger = logger;
}
public TaskCompletionSource AutomaticSavingDisabled { get; } = new ();
public TaskCompletionSource SavedTheGame { get; } = new ();
public TaskCompletionSource AutomaticSavingEnabled { get; } = new ();
public void OnOutput(object? sender, string? line) {
if (line == null) {
return; return;
} }
var match = ServerThreadInfoRegex().Match(line); var warningCount = result.Warnings.Count();
if (!match.Success) { if (warningCount > 0) {
return; logger.Warning("Backup finished with {Warnings} warning(s).", warningCount);
}
string info = match.Groups[1].Value;
if (!AutomaticSavingDisabled.Task.IsCompleted) {
if (info == "Automatic saving is now disabled") {
logger.Verbose("Detected that automatic saving is disabled.");
AutomaticSavingDisabled.SetResult();
}
}
else if (!SavedTheGame.Task.IsCompleted) {
if (info == "Saved the game") {
logger.Verbose("Detected that the game is saved.");
SavedTheGame.SetResult();
}
}
else if (!AutomaticSavingEnabled.Task.IsCompleted) {
if (info == "Automatic saving is now enabled") {
logger.Verbose("Detected that automatic saving is enabled.");
AutomaticSavingEnabled.SetResult();
} }
else {
logger.Information("Backup finished successfully.");
} }
} }
} }

View File

@@ -79,7 +79,7 @@ sealed class BackupScheduler : CancellableBackgroundTask {
await Task.Delay(TimeSpan.FromSeconds(10), CancellationToken); await Task.Delay(TimeSpan.FromSeconds(10), CancellationToken);
Logger.Verbose("Waiting for server output before checking for online players again..."); Logger.Debug("Waiting for server output before checking for online players again...");
await serverOutputWhileWaitingForOnlinePlayers.WaitHandle.WaitOneAsync(CancellationToken); await serverOutputWhileWaitingForOnlinePlayers.WaitHandle.WaitOneAsync(CancellationToken);
} }
} finally { } finally {
@@ -90,7 +90,7 @@ sealed class BackupScheduler : CancellableBackgroundTask {
private void ServerOutputListener(object? sender, string line) { private void ServerOutputListener(object? sender, string line) {
if (!serverOutputWhileWaitingForOnlinePlayers.IsSet) { if (!serverOutputWhileWaitingForOnlinePlayers.IsSet) {
serverOutputWhileWaitingForOnlinePlayers.Set(); serverOutputWhileWaitingForOnlinePlayers.Set();
Logger.Verbose("Detected server output, signalling to check for online players again."); Logger.Debug("Detected server output, signalling to check for online players again.");
} }
} }
} }

View File

@@ -0,0 +1,80 @@
using System.Text.RegularExpressions;
using Phantom.Agent.Minecraft.Command;
using Phantom.Agent.Minecraft.Instance;
using Phantom.Utils.Runtime;
using Serilog;
namespace Phantom.Agent.Services.Backups;
sealed partial class BackupServerCommandDispatcher : IDisposable {
[GeneratedRegex(@"^\[(?:.*?)\] \[Server thread/INFO\]: (.*?)$", RegexOptions.NonBacktracking)]
private static partial Regex ServerThreadInfoRegex();
private readonly ILogger logger;
private readonly InstanceProcess process;
private readonly CancellationToken cancellationToken;
private readonly TaskCompletionSource automaticSavingDisabled = Tasks.CreateCompletionSource();
private readonly TaskCompletionSource savedTheGame = Tasks.CreateCompletionSource();
private readonly TaskCompletionSource automaticSavingEnabled = Tasks.CreateCompletionSource();
public BackupServerCommandDispatcher(ILogger logger, InstanceProcess process, CancellationToken cancellationToken) {
this.logger = logger;
this.process = process;
this.cancellationToken = cancellationToken;
this.process.AddOutputListener(OnOutput, maxLinesToReadFromHistory: 0);
}
void IDisposable.Dispose() {
process.RemoveOutputListener(OnOutput);
}
public async Task DisableAutomaticSaving() {
await process.SendCommand(MinecraftCommand.SaveOff, cancellationToken);
await automaticSavingDisabled.Task.WaitAsync(cancellationToken);
}
public async Task SaveAllChunks() {
// TODO Try if not flushing and waiting a few seconds before flushing reduces lag.
await process.SendCommand(MinecraftCommand.SaveAll(flush: true), cancellationToken);
await savedTheGame.Task.WaitAsync(cancellationToken);
}
public async Task EnableAutomaticSaving() {
await process.SendCommand(MinecraftCommand.SaveOn, cancellationToken);
await automaticSavingEnabled.Task.WaitAsync(cancellationToken);
}
private void OnOutput(object? sender, string? line) {
if (line == null) {
return;
}
var match = ServerThreadInfoRegex().Match(line);
if (!match.Success) {
return;
}
string info = match.Groups[1].Value;
if (!automaticSavingDisabled.Task.IsCompleted) {
if (info == "Automatic saving is now disabled") {
logger.Debug("Detected that automatic saving is disabled.");
automaticSavingDisabled.SetResult();
}
}
else if (!savedTheGame.Task.IsCompleted) {
if (info == "Saved the game") {
logger.Debug("Detected that the game is saved.");
savedTheGame.SetResult();
}
}
else if (!automaticSavingEnabled.Task.IsCompleted) {
if (info == "Automatic saving is now enabled") {
logger.Debug("Detected that automatic saving is enabled.");
automaticSavingEnabled.SetResult();
}
}
}
}

View File

@@ -18,15 +18,10 @@ sealed class Instance : IDisposable {
return prefix[..prefix.IndexOf('-')] + "/" + Interlocked.Increment(ref loggerSequenceId); return prefix[..prefix.IndexOf('-')] + "/" + Interlocked.Increment(ref loggerSequenceId);
} }
public static Instance Create(InstanceConfiguration configuration, InstanceServices services, BaseLauncher launcher) { private InstanceServices Services { get; }
var instance = new Instance(configuration, services, launcher);
instance.SetStatus(instance.currentStatus);
return instance;
}
public InstanceConfiguration Configuration { get; private set; } public InstanceConfiguration Configuration { get; private set; }
private InstanceServices Services { get; } private IServerLauncher Launcher { get; set; }
private BaseLauncher Launcher { get; set; }
private readonly string shortName; private readonly string shortName;
private readonly ILogger logger; private readonly ILogger logger;
@@ -41,26 +36,38 @@ sealed class Instance : IDisposable {
public event EventHandler? IsRunningChanged; public event EventHandler? IsRunningChanged;
private 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();
this.currentStatus = InstanceStatus.NotRunning; this.currentStatus = InstanceStatus.NotRunning;
} }
private void SetStatus(IInstanceStatus status) { private void TryUpdateStatus(string taskName, Func<Task> getUpdateTask) {
int myStatusUpdateCounter = Interlocked.Increment(ref statusUpdateCounter); int myStatusUpdateCounter = Interlocked.Increment(ref statusUpdateCounter);
Services.TaskManager.Run("Report status of instance " + shortName + " as " + status.GetType().Name, async () => { Services.TaskManager.Run(taskName, async () => {
if (myStatusUpdateCounter == statusUpdateCounter) { if (myStatusUpdateCounter == statusUpdateCounter) {
await getUpdateTask();
}
});
}
public void ReportLastStatus() {
TryUpdateStatus("Report last status of instance " + shortName, async () => {
await ServerMessaging.Send(new ReportInstanceStatusMessage(Configuration.InstanceGuid, currentStatus));
});
}
private void ReportAndSetStatus(IInstanceStatus status) {
TryUpdateStatus("Report status of instance " + shortName + " as " + status.GetType().Name, async () => {
currentStatus = status; currentStatus = status;
await ServerMessaging.Send(new ReportInstanceStatusMessage(Configuration.InstanceGuid, status)); await ServerMessaging.Send(new ReportInstanceStatusMessage(Configuration.InstanceGuid, status));
}
}); });
} }
@@ -78,7 +85,7 @@ sealed class Instance : IDisposable {
disposable.Dispose(); disposable.Dispose();
} }
logger.Verbose("Transitioning instance state to: {NewState}", newState.GetType().Name); logger.Debug("Transitioning instance state to: {NewState}", newState.GetType().Name);
var wasRunning = IsRunning; var wasRunning = IsRunning;
currentState = newState; currentState = newState;
@@ -94,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;
@@ -147,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;
} }
@@ -156,7 +163,7 @@ sealed class Instance : IDisposable {
public override string ShortName => instance.shortName; public override string ShortName => instance.shortName;
public override void SetStatus(IInstanceStatus newStatus) { public override void SetStatus(IInstanceStatus newStatus) {
instance.SetStatus(newStatus); instance.ReportAndSetStatus(newStatus);
} }
public override void ReportEvent(IInstanceEvent instanceEvent) { public override void ReportEvent(IInstanceEvent instanceEvent) {
@@ -170,12 +177,12 @@ sealed class Instance : IDisposable {
if (!instance.IsRunning) { if (!instance.IsRunning) {
// Only InstanceSessionManager is allowed to transition an instance out of a non-running state. // Only InstanceSessionManager is allowed to transition an instance out of a non-running state.
instance.logger.Verbose("Cancelled state transition to {State} because instance is not running.", state.GetType().Name); instance.logger.Debug("Cancelled state transition to {State} because instance is not running.", state.GetType().Name);
return; return;
} }
if (state is not InstanceNotRunningState && shutdownCancellationToken.IsCancellationRequested) { if (state is not InstanceNotRunningState && shutdownCancellationToken.IsCancellationRequested) {
instance.logger.Verbose("Cancelled state transition to {State} due to Agent shutdown.", state.GetType().Name); instance.logger.Debug("Cancelled state transition to {State} due to Agent shutdown.", state.GetType().Name);
return; return;
} }

View File

@@ -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);

View File

@@ -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);
} }
@@ -71,7 +71,7 @@ sealed class InstanceSessionManager : IDisposable {
}); });
} }
public async Task<InstanceActionResult<ConfigureInstanceResult>> Configure(InstanceConfiguration configuration, InstanceLaunchProperties launchProperties, bool launchNow) { public async Task<InstanceActionResult<ConfigureInstanceResult>> Configure(InstanceConfiguration configuration, InstanceLaunchProperties launchProperties, bool launchNow, bool alwaysReportStatus) {
return await AcquireSemaphoreAndRun(async () => { return await AcquireSemaphoreAndRun(async () => {
var instanceGuid = configuration.InstanceGuid; var instanceGuid = configuration.InstanceGuid;
var instanceFolder = Path.Combine(basePath, instanceGuid.ToString()); var instanceFolder = Path.Combine(basePath, instanceGuid.ToString());
@@ -94,16 +94,26 @@ 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);
Logger.Information("Reconfigured instance \"{Name}\" (GUID {Guid}).", configuration.InstanceName, configuration.InstanceGuid); Logger.Information("Reconfigured instance \"{Name}\" (GUID {Guid}).", configuration.InstanceName, configuration.InstanceGuid);
if (alwaysReportStatus) {
instance.ReportLastStatus();
}
} }
else { else {
instances[instanceGuid] = instance = Instance.Create(configuration, instanceServices, launcher); instances[instanceGuid] = instance = new Instance(instanceServices, configuration, launcher);
instance.IsRunningChanged += OnInstanceIsRunningChanged;
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.IsRunningChanged += OnInstanceIsRunningChanged;
} }
if (launchNow) { if (launchNow) {

View File

@@ -16,7 +16,7 @@ sealed class InstanceSession : IDisposable {
} }
private void SessionOutput(object? sender, string line) { private void SessionOutput(object? sender, string line) {
context.Logger.Verbose("[Server] {Line}", line); context.Logger.Debug("[Server] {Line}", line);
logSender.Enqueue(line); logSender.Enqueue(line);
} }

View File

@@ -110,7 +110,7 @@ sealed class InstanceRunningState : IInstanceState {
} }
} }
} catch (OperationCanceledException) { } catch (OperationCanceledException) {
context.Logger.Verbose("Cancelled delayed stop."); context.Logger.Debug("Cancelled delayed stop.");
return; return;
} catch (ObjectDisposedException) { } catch (ObjectDisposedException) {
return; return;

View File

@@ -33,7 +33,7 @@ public sealed class MessageListener : IMessageToAgentListener {
} }
foreach (var configureInstanceMessage in message.InitialInstanceConfigurations) { foreach (var configureInstanceMessage in message.InitialInstanceConfigurations) {
var result = await HandleConfigureInstance(configureInstanceMessage); var result = await HandleConfigureInstance(configureInstanceMessage, alwaysReportStatus: true);
if (!result.Is(ConfigureInstanceResult.Success)) { if (!result.Is(ConfigureInstanceResult.Success)) {
ShutdownAfterConfigurationFailed(configureInstanceMessage.Configuration); ShutdownAfterConfigurationFailed(configureInstanceMessage.Configuration);
return NoReply.Instance; return NoReply.Instance;
@@ -59,8 +59,12 @@ public sealed class MessageListener : IMessageToAgentListener {
return Task.FromResult(NoReply.Instance); return Task.FromResult(NoReply.Instance);
} }
private Task<InstanceActionResult<ConfigureInstanceResult>> HandleConfigureInstance(ConfigureInstanceMessage message, bool alwaysReportStatus) {
return agent.InstanceSessionManager.Configure(message.Configuration, message.LaunchProperties, message.LaunchNow, alwaysReportStatus);
}
public async Task<InstanceActionResult<ConfigureInstanceResult>> HandleConfigureInstance(ConfigureInstanceMessage message) { public async Task<InstanceActionResult<ConfigureInstanceResult>> HandleConfigureInstance(ConfigureInstanceMessage message) {
return await agent.InstanceSessionManager.Configure(message.Configuration, message.LaunchProperties, message.LaunchNow); return await HandleConfigureInstance(message, alwaysReportStatus: false);
} }
public async Task<InstanceActionResult<LaunchInstanceResult>> HandleLaunchInstance(LaunchInstanceMessage message) { public async Task<InstanceActionResult<LaunchInstanceResult>> HandleLaunchInstance(LaunchInstanceMessage message) {

View File

@@ -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
} }

View File

@@ -8,6 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Serilog" /> <PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Sinks.Async" />
<PackageReference Include="Serilog.Sinks.Console" /> <PackageReference Include="Serilog.Sinks.Console" />
</ItemGroup> </ItemGroup>

View File

@@ -18,7 +18,7 @@ public static class PhantomLogger {
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", DefaultLogLevel.Coerce(LogEventLevel.Warning)) .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", DefaultLogLevel.Coerce(LogEventLevel.Warning))
.Filter.ByExcluding(static e => e.Exception is OperationCanceledException) .Filter.ByExcluding(static e => e.Exception is OperationCanceledException)
.Enrich.FromLogContext() .Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: template, formatProvider: CultureInfo.InvariantCulture, theme: AnsiConsoleTheme.Literate) .WriteTo.Async(c => c.Console(outputTemplate: template, formatProvider: CultureInfo.InvariantCulture, theme: AnsiConsoleTheme.Literate))
.CreateLogger(); .CreateLogger();
} }

View File

@@ -20,6 +20,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Update="Serilog" Version="2.12.0" /> <PackageReference Update="Serilog" Version="2.12.0" />
<PackageReference Update="Serilog.AspNetCore" Version="6.1.0" /> <PackageReference Update="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Update="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Update="Serilog.Sinks.Console" Version="4.1.0" /> <PackageReference Update="Serilog.Sinks.Console" Version="4.1.0" />
</ItemGroup> </ItemGroup>

View File

@@ -0,0 +1,164 @@
using System.Collections.Immutable;
using System.Net.Http.Json;
using System.Text.Json;
using Phantom.Common.Data.Minecraft;
using Phantom.Common.Logging;
using Phantom.Utils.Cryptography;
using Phantom.Utils.IO;
using Phantom.Utils.Runtime;
using Serilog;
namespace Phantom.Server.Minecraft;
sealed class MinecraftVersionApi : IDisposable {
private static readonly ILogger Logger = PhantomLogger.Create<MinecraftVersionApi>();
private const string VersionManifestUrl = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
private readonly HttpClient http = new ();
public void Dispose() {
http.Dispose();
}
public async Task<ImmutableArray<MinecraftVersion>> GetVersions(CancellationToken cancellationToken) {
return await FetchVersions(cancellationToken) ?? ImmutableArray<MinecraftVersion>.Empty;
}
private async Task<ImmutableArray<MinecraftVersion>?> FetchVersions(CancellationToken cancellationToken) {
return await FetchOrFailSilently(async () => {
var versionManifest = await FetchJson(VersionManifestUrl, "version manifest", cancellationToken);
return GetVersionsFromManifest(versionManifest);
});
}
public async Task<FileDownloadInfo?> GetServerExecutableInfo(ImmutableArray<MinecraftVersion> versions, string version, CancellationToken cancellationToken) {
var versionObject = versions.FirstOrDefault(v => v.Id == version);
if (versionObject == null) {
Logger.Error("Version {Version} was not found in version manifest.", version);
return null;
}
return await FetchOrFailSilently(async () => {
var versionMetadata = await FetchJson(versionObject.MetadataUrl, "version metadata", cancellationToken);
return GetServerExecutableInfoFromMetadata(versionMetadata);
});
}
private static async Task<T?> FetchOrFailSilently<T>(Func<Task<T?>> task) {
try {
return await task();
} catch (StopProcedureException) {
return default;
} catch (Exception e) {
Logger.Error(e, "An unexpected error occurred.");
return default;
}
}
private async Task<JsonElement> FetchJson(string url, string description, CancellationToken cancellationToken) {
Logger.Debug("Fetching {Description} JSON from: {Url}", description, url);
try {
return await http.GetFromJsonAsync<JsonElement>(url, cancellationToken);
} catch (HttpRequestException e) {
Logger.Error(e, "Unable to download {Description}.", description);
throw StopProcedureException.Instance;
} catch (Exception e) {
Logger.Error(e, "Unable to parse {Description} as JSON.", description);
throw StopProcedureException.Instance;
}
}
private static ImmutableArray<MinecraftVersion> GetVersionsFromManifest(JsonElement versionManifest) {
JsonElement versionsElement = GetJsonPropertyOrThrow(versionManifest, "versions", JsonValueKind.Array, "version manifest");
var foundVersions = ImmutableArray.CreateBuilder<MinecraftVersion>(versionsElement.GetArrayLength());
foreach (var versionElement in versionsElement.EnumerateArray()) {
try {
foundVersions.Add(GetVersionFromManifestEntry(versionElement));
} catch (StopProcedureException) {}
}
return foundVersions.MoveToImmutable();
}
private static MinecraftVersion GetVersionFromManifestEntry(JsonElement versionElement) {
JsonElement idElement = GetJsonPropertyOrThrow(versionElement, "id", JsonValueKind.String, "version entry in version manifest");
string id = idElement.GetString() ?? throw new InvalidOperationException();
JsonElement typeElement = GetJsonPropertyOrThrow(versionElement, "type", JsonValueKind.String, "version entry in version manifest");
string? typeString = typeElement.GetString();
var type = MinecraftVersionTypes.FromString(typeString);
if (type == MinecraftVersionType.Other) {
Logger.Warning("Unknown version type: {Type} ({Version})", typeString, id);
}
JsonElement urlElement = GetJsonPropertyOrThrow(versionElement, "url", JsonValueKind.String, "version entry in version manifest");
string? url = urlElement.GetString();
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) {
Logger.Error("The \"url\" key in version entry in version manifest does not contain a valid URL: {Url}", url);
throw StopProcedureException.Instance;
}
if (uri.Scheme != "https" || !uri.AbsolutePath.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) {
Logger.Error("The \"url\" key in version entry in version manifest does not contain an accepted URL: {Url}", url);
throw StopProcedureException.Instance;
}
return new MinecraftVersion(id, type, url);
}
private static FileDownloadInfo GetServerExecutableInfoFromMetadata(JsonElement versionMetadata) {
JsonElement downloadsElement = GetJsonPropertyOrThrow(versionMetadata, "downloads", JsonValueKind.Object, "version metadata");
JsonElement serverElement = GetJsonPropertyOrThrow(downloadsElement, "server", JsonValueKind.Object, "downloads object in version metadata");
JsonElement urlElement = GetJsonPropertyOrThrow(serverElement, "url", JsonValueKind.String, "downloads.server object in version metadata");
string? url = urlElement.GetString();
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) {
Logger.Error("The \"url\" key in downloads.server object in version metadata does not contain a valid URL: {Url}", url);
throw StopProcedureException.Instance;
}
if (uri.Scheme != "https" || !uri.AbsolutePath.EndsWith(".jar", StringComparison.OrdinalIgnoreCase)) {
Logger.Error("The \"url\" key in downloads.server object in version metadata does not contain a accepted URL: {Url}", url);
throw StopProcedureException.Instance;
}
JsonElement sizeElement = GetJsonPropertyOrThrow(serverElement, "size", JsonValueKind.Number, "downloads.server object in version metadata");
ulong size;
try {
size = sizeElement.GetUInt64();
} catch (FormatException) {
Logger.Error("The \"size\" key in downloads.server object in version metadata contains an invalid file size: {Size}", sizeElement);
throw StopProcedureException.Instance;
}
JsonElement sha1Element = GetJsonPropertyOrThrow(serverElement, "sha1", JsonValueKind.String, "downloads.server object in version metadata");
Sha1String hash;
try {
hash = Sha1String.FromString(sha1Element.GetString());
} catch (Exception) {
Logger.Error("The \"sha1\" key in downloads.server object in version metadata does not contain a valid SHA-1 hash: {Sha1}", sha1Element.GetString());
throw StopProcedureException.Instance;
}
return new FileDownloadInfo(url, hash, new FileSize(size));
}
private static JsonElement GetJsonPropertyOrThrow(JsonElement parentElement, string propertyKey, JsonValueKind expectedKind, string location) {
if (!parentElement.TryGetProperty(propertyKey, out var valueElement)) {
Logger.Error("Missing \"{Property}\" key in " + location + ".", propertyKey);
throw StopProcedureException.Instance;
}
if (valueElement.ValueKind != expectedKind) {
Logger.Error("The \"{Property}\" key in " + location + " does not contain a JSON {ExpectedType}. Actual type: {ActualType}", propertyKey, expectedKind, valueElement.ValueKind);
throw StopProcedureException.Instance;
}
return valueElement;
}
}

View File

@@ -1,12 +1,7 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Net.Http.Json;
using System.Text.Json;
using Phantom.Common.Data.Minecraft; using Phantom.Common.Data.Minecraft;
using Phantom.Common.Logging; using Phantom.Common.Logging;
using Phantom.Utils.Cryptography;
using Phantom.Utils.IO;
using Phantom.Utils.Runtime;
using Serilog; using Serilog;
namespace Phantom.Server.Minecraft; namespace Phantom.Server.Minecraft;
@@ -15,178 +10,66 @@ public sealed class MinecraftVersions : IDisposable {
private static readonly ILogger Logger = PhantomLogger.Create<MinecraftVersions>(); private static readonly ILogger Logger = PhantomLogger.Create<MinecraftVersions>();
private static readonly TimeSpan CacheRetentionTime = TimeSpan.FromMinutes(10); private static readonly TimeSpan CacheRetentionTime = TimeSpan.FromMinutes(10);
private const string VersionManifestUrl = "https://launchermeta.mojang.com/mc/game/version_manifest.json"; private readonly MinecraftVersionApi api = new ();
private readonly HttpClient http = new ();
private readonly Stopwatch cacheTimer = new (); private readonly Stopwatch cacheTimer = new ();
private readonly SemaphoreSlim cachedVersionsSemaphore = new (1, 1); private readonly SemaphoreSlim cacheSemaphore = new (1, 1);
private bool IsCacheNotExpired => cacheTimer.IsRunning && cacheTimer.Elapsed < CacheRetentionTime;
private ImmutableArray<MinecraftVersion>? cachedVersions; private ImmutableArray<MinecraftVersion>? cachedVersions;
private ImmutableArray<MinecraftVersion>? CachedVersionsUnlessExpired => cacheTimer.IsRunning && cacheTimer.Elapsed < CacheRetentionTime ? cachedVersions : null; private readonly Dictionary<string, FileDownloadInfo?> cachedServerExecutables = new ();
public void Dispose() { public void Dispose() {
http.Dispose(); api.Dispose();
cachedVersionsSemaphore.Dispose(); cacheSemaphore.Dispose();
} }
public async Task<ImmutableArray<MinecraftVersion>> GetVersions(CancellationToken cancellationToken) { public async Task<ImmutableArray<MinecraftVersion>> GetVersions(CancellationToken cancellationToken) {
if (CachedVersionsUnlessExpired is {} earlyResult) { return await GetCachedObject(() => cachedVersions != null, () => cachedVersions.GetValueOrDefault(), v => cachedVersions = v, LoadVersions, cancellationToken);
return earlyResult;
} }
await cachedVersionsSemaphore.WaitAsync(cancellationToken); private async Task<ImmutableArray<MinecraftVersion>> LoadVersions(CancellationToken cancellationToken) {
try { ImmutableArray<MinecraftVersion> versions = await api.GetVersions(cancellationToken);
if (CachedVersionsUnlessExpired is {} racedResult) {
return racedResult;
}
ImmutableArray<MinecraftVersion> versions = await FetchVersions(cancellationToken) ?? ImmutableArray<MinecraftVersion>.Empty;
Logger.Information("Refreshed Minecraft version cache, {Versions} version(s) found.", versions.Length); Logger.Information("Refreshed Minecraft version cache, {Versions} version(s) found.", versions.Length);
cachedVersions = versions;
cacheTimer.Restart();
return versions; return versions;
} finally {
cachedVersionsSemaphore.Release();
}
}
private async Task<ImmutableArray<MinecraftVersion>?> FetchVersions(CancellationToken cancellationToken) {
return await FetchOrFailSilently(async () => {
var versionManifest = await FetchJson(http, VersionManifestUrl, "version manifest", cancellationToken);
return GetVersionsFromManifest(versionManifest);
});
} }
public async Task<FileDownloadInfo?> GetServerExecutableInfo(string version, CancellationToken cancellationToken) { public async Task<FileDownloadInfo?> GetServerExecutableInfo(string version, CancellationToken cancellationToken) {
return await FetchOrFailSilently(async () => {
var versions = await GetVersions(cancellationToken); var versions = await GetVersions(cancellationToken);
var versionObject = versions.FirstOrDefault(v => v.Id == version); return await GetCachedObject(() => cachedServerExecutables.ContainsKey(version), () => cachedServerExecutables[version], v => cachedServerExecutables[version] = v, ct => LoadServerExecutableInfo(versions, version, ct), cancellationToken);
if (versionObject == null) {
Logger.Error("Version {Version} was not found in version manifest.", version);
return null;
} }
var versionMetadata = await FetchJson(http, versionObject.MetadataUrl, "version metadata", cancellationToken); private async Task<FileDownloadInfo?> LoadServerExecutableInfo(ImmutableArray<MinecraftVersion> versions, string version, CancellationToken cancellationToken) {
return GetServerExecutableInfoFromMetadata(versionMetadata); var info = await api.GetServerExecutableInfo(versions, version, cancellationToken);
});
if (info == null) {
Logger.Information("Refreshed Minecraft {Version} server executable cache, no file found.", version);
}
else {
Logger.Information("Refreshed Minecraft {Version} server executable cache, found file: {Url}.", version, info.DownloadUrl);
} }
private static async Task<T?> FetchOrFailSilently<T>(Func<Task<T?>> task) { return info;
}
private async Task<T> GetCachedObject<T>(Func<bool> isLoaded, Func<T> fieldGetter, Action<T> fieldSetter, Func<CancellationToken, Task<T>> fieldLoader, CancellationToken cancellationToken) {
if (IsCacheNotExpired && isLoaded()) {
return fieldGetter();
}
await cacheSemaphore.WaitAsync(cancellationToken);
try { try {
return await task(); if (IsCacheNotExpired && isLoaded()) {
} catch (StopProcedureException) { return fieldGetter();
return default;
} catch (Exception e) {
Logger.Error(e, "An unexpected error occurred.");
return default;
}
} }
private static async Task<JsonElement> FetchJson(HttpClient http, string url, string description, CancellationToken cancellationToken) { T result = await fieldLoader(cancellationToken);
Logger.Information("Fetching {Description} JSON from: {Url}", description, url); fieldSetter(result);
try { cacheTimer.Restart();
return await http.GetFromJsonAsync<JsonElement>(url, cancellationToken); return result;
} catch (HttpRequestException e) { } finally {
Logger.Error(e, "Unable to download {Description}.", description); cacheSemaphore.Release();
throw StopProcedureException.Instance;
} catch (Exception e) {
Logger.Error(e, "Unable to parse {Description} as JSON.", description);
throw StopProcedureException.Instance;
} }
} }
private static ImmutableArray<MinecraftVersion> GetVersionsFromManifest(JsonElement versionManifest) {
JsonElement versionsElement = GetJsonPropertyOrThrow(versionManifest, "versions", JsonValueKind.Array, "version manifest");
var foundVersions = ImmutableArray.CreateBuilder<MinecraftVersion>(versionsElement.GetArrayLength());
foreach (var versionElement in versionsElement.EnumerateArray()) {
try {
foundVersions.Add(GetVersionFromManifestEntry(versionElement));
} catch (StopProcedureException) {}
}
return foundVersions.MoveToImmutable();
}
private static MinecraftVersion GetVersionFromManifestEntry(JsonElement versionElement) {
JsonElement idElement = GetJsonPropertyOrThrow(versionElement, "id", JsonValueKind.String, "version entry in version manifest");
string id = idElement.GetString() ?? throw new InvalidOperationException();
JsonElement typeElement = GetJsonPropertyOrThrow(versionElement, "type", JsonValueKind.String, "version entry in version manifest");
string? typeString = typeElement.GetString();
var type = MinecraftVersionTypes.FromString(typeString);
if (type == MinecraftVersionType.Other) {
Logger.Verbose("Unknown version type: {Type} ({Version})", typeString, id);
}
JsonElement urlElement = GetJsonPropertyOrThrow(versionElement, "url", JsonValueKind.String, "version entry in version manifest");
string? url = urlElement.GetString();
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) {
Logger.Error("The \"url\" key in version entry in version manifest does not contain a valid URL: {Url}", url);
throw StopProcedureException.Instance;
}
if (uri.Scheme != "https" || !uri.AbsolutePath.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) {
Logger.Error("The \"url\" key in version entry in version manifest does not contain an accepted URL: {Url}", url);
throw StopProcedureException.Instance;
}
return new MinecraftVersion(id, type, url);
}
private static FileDownloadInfo GetServerExecutableInfoFromMetadata(JsonElement versionMetadata) {
JsonElement downloadsElement = GetJsonPropertyOrThrow(versionMetadata, "downloads", JsonValueKind.Object, "version metadata");
JsonElement serverElement = GetJsonPropertyOrThrow(downloadsElement, "server", JsonValueKind.Object, "downloads object in version metadata");
JsonElement urlElement = GetJsonPropertyOrThrow(serverElement, "url", JsonValueKind.String, "downloads.server object in version metadata");
string? url = urlElement.GetString();
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) {
Logger.Error("The \"url\" key in downloads.server object in version metadata does not contain a valid URL: {Url}", url);
throw StopProcedureException.Instance;
}
if (uri.Scheme != "https" || !uri.AbsolutePath.EndsWith(".jar", StringComparison.OrdinalIgnoreCase)) {
Logger.Error("The \"url\" key in downloads.server object in version metadata does not contain a accepted URL: {Url}", url);
throw StopProcedureException.Instance;
}
JsonElement sizeElement = GetJsonPropertyOrThrow(serverElement, "size", JsonValueKind.Number, "downloads.server object in version metadata");
ulong size;
try {
size = sizeElement.GetUInt64();
} catch (FormatException) {
Logger.Error("The \"size\" key in downloads.server object in version metadata contains an invalid file size: {Size}", sizeElement);
throw StopProcedureException.Instance;
}
JsonElement sha1Element = GetJsonPropertyOrThrow(serverElement, "sha1", JsonValueKind.String, "downloads.server object in version metadata");
Sha1String hash;
try {
hash = Sha1String.FromString(sha1Element.GetString());
} catch (Exception) {
Logger.Error("The \"sha1\" key in downloads.server object in version metadata does not contain a valid SHA-1 hash: {Sha1}", sha1Element.GetString());
throw StopProcedureException.Instance;
}
return new FileDownloadInfo(url, hash, new FileSize(size));
}
private static JsonElement GetJsonPropertyOrThrow(JsonElement parentElement, string propertyKey, JsonValueKind expectedKind, string location) {
if (!parentElement.TryGetProperty(propertyKey, out var valueElement)) {
Logger.Error("Missing \"{Property}\" key in " + location + ".", propertyKey);
throw StopProcedureException.Instance;
}
if (valueElement.ValueKind != expectedKind) {
Logger.Error("The \"{Property}\" key in " + location + " does not contain a JSON {ExpectedType}. Actual type: {ActualType}", propertyKey, expectedKind, valueElement.ValueKind);
throw StopProcedureException.Instance;
}
return valueElement;
}
} }

View File

@@ -46,7 +46,7 @@ public sealed class RpcLauncher : RpcRuntime<ServerSocket> {
void OnConnectionClosed(object? sender, RpcClientConnectionClosedEventArgs e) { void OnConnectionClosed(object? sender, RpcClientConnectionClosedEventArgs e) {
clients.Remove(e.RoutingId); clients.Remove(e.RoutingId);
logger.Verbose("Closed connection to {RoutingId}.", e.RoutingId); logger.Debug("Closed connection to {RoutingId}.", e.RoutingId);
} }
while (!cancellationToken.IsCancellationRequested) { while (!cancellationToken.IsCancellationRequested) {

View File

@@ -9,6 +9,7 @@ using Phantom.Server.Services.Agents;
using Phantom.Server.Services.Events; using Phantom.Server.Services.Events;
using Phantom.Server.Services.Instances; using Phantom.Server.Services.Instances;
using Phantom.Utils.Rpc.Message; using Phantom.Utils.Rpc.Message;
using Phantom.Utils.Runtime;
namespace Phantom.Server.Services.Rpc; namespace Phantom.Server.Services.Rpc;
@@ -21,7 +22,7 @@ public sealed class MessageToServerListener : IMessageToServerListener {
private readonly InstanceLogManager instanceLogManager; private readonly InstanceLogManager instanceLogManager;
private readonly EventLog eventLog; private readonly EventLog eventLog;
private readonly TaskCompletionSource<Guid> agentGuidWaiter = new (); private readonly TaskCompletionSource<Guid> agentGuidWaiter = Tasks.CreateCompletionSource<Guid>();
public bool IsDisposed { get; private set; } public bool IsDisposed { get; private set; }

View File

@@ -40,7 +40,7 @@ public sealed class PhantomLoginManager {
var result = await signInManager.CheckPasswordSignInAsync(user, password, lockoutOnFailure: true); var result = await signInManager.CheckPasswordSignInAsync(user, password, lockoutOnFailure: true);
if (result == SignInResult.Success) { if (result == SignInResult.Success) {
Logger.Verbose("Created login token for {Username}.", username); Logger.Debug("Created login token for {Username}.", username);
string token = TokenGenerator.Create(60); string token = TokenGenerator.Create(60);
loginStore.Add(token, user, password, returnUrl ?? string.Empty); loginStore.Add(token, user, password, returnUrl ?? string.Empty);

View File

@@ -30,7 +30,7 @@ public sealed class PhantomLoginStore {
foreach (var (token, entry) in loginEntries) { foreach (var (token, entry) in loginEntries) {
if (entry.IsExpired) { if (entry.IsExpired) {
Logger.Verbose("Expired login entry for {Username}.", entry.User.UserName); Logger.Debug("Expired login entry for {Username}.", entry.User.UserName);
loginEntries.TryRemove(token, out _); loginEntries.TryRemove(token, out _);
} }
} }
@@ -50,7 +50,7 @@ public sealed class PhantomLoginStore {
} }
if (entry.IsExpired) { if (entry.IsExpired) {
Logger.Verbose("Expired login entry for {Username}.", entry.User.UserName); Logger.Debug("Expired login entry for {Username}.", entry.User.UserName);
return null; return null;
} }

View File

@@ -1,4 +1,5 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using Phantom.Utils.Runtime;
using Serilog; using Serilog;
namespace Phantom.Utils.Rpc.Message; namespace Phantom.Utils.Rpc.Message;
@@ -15,7 +16,7 @@ public sealed class MessageReplyTracker {
public uint RegisterReply() { public uint RegisterReply() {
var sequenceId = Interlocked.Increment(ref lastSequenceId); var sequenceId = Interlocked.Increment(ref lastSequenceId);
replyTasks[sequenceId] = new TaskCompletionSource<byte[]>(TaskCreationOptions.None); replyTasks[sequenceId] = Tasks.CreateCompletionSource<byte[]>();
return sequenceId; return sequenceId;
} }

View File

@@ -43,7 +43,11 @@ public abstract class RpcRuntime<TSocket> where TSocket : ThreadSafeSocket, new(
Connect(socket); Connect(socket);
void RunTask() { void RunTask() {
try {
Run(socket, replyTracker, taskManager); Run(socket, replyTracker, taskManager);
} catch (Exception e) {
runtimeLogger.Error(e, "Caught exception in RPC thread.");
}
} }
try { try {

View File

@@ -15,7 +15,7 @@ public abstract class CancellableBackgroundTask {
} }
private async Task Run() { private async Task Run() {
Logger.Verbose("Task started."); Logger.Debug("Task started.");
try { try {
await RunTask(); await RunTask();
@@ -25,7 +25,7 @@ public abstract class CancellableBackgroundTask {
Logger.Fatal(e, "Caught exception in task."); Logger.Fatal(e, "Caught exception in task.");
} finally { } finally {
cancellationTokenSource.Dispose(); cancellationTokenSource.Dispose();
Logger.Verbose("Task stopped."); Logger.Debug("Task stopped.");
} }
} }

View File

@@ -1,30 +1,24 @@
using System.Diagnostics; using Serilog;
using Serilog;
namespace Phantom.Utils.Runtime; namespace Phantom.Utils.Runtime;
public sealed class OneShotProcess { public sealed class OneShotProcess {
private readonly ILogger logger; private readonly ILogger logger;
private readonly ProcessStartInfo startInfo; private readonly ProcessConfigurator configurator;
public event DataReceivedEventHandler? Output; public event EventHandler<Process.Output>? OutputReceived;
public OneShotProcess(ILogger logger, ProcessStartInfo startInfo) { public OneShotProcess(ILogger logger, ProcessConfigurator configurator) {
this.logger = logger; this.logger = logger;
this.startInfo = startInfo; this.configurator = configurator;
this.startInfo.RedirectStandardOutput = true;
this.startInfo.RedirectStandardError = true;
} }
public async Task<bool> Run(CancellationToken cancellationToken) { public async Task<bool> Run(CancellationToken cancellationToken) {
using var process = new Process { StartInfo = startInfo }; using var process = configurator.CreateProcess();
process.OutputDataReceived += Output; process.OutputReceived += OutputReceived;
process.ErrorDataReceived += Output;
try { try {
process.Start(); process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
} catch (Exception e) { } catch (Exception e) {
logger.Error(e, "Caught exception launching process."); logger.Error(e, "Caught exception launching process.");
return false; return false;
@@ -50,7 +44,7 @@ public sealed class OneShotProcess {
return false; return false;
} }
logger.Verbose("Process finished successfully."); logger.Debug("Process finished successfully.");
return true; return true;
} }

View File

@@ -0,0 +1,92 @@
using System.Diagnostics;
namespace Phantom.Utils.Runtime;
public sealed class Process : IDisposable {
public readonly record struct Output(string Line, bool IsError);
public event EventHandler<Output>? OutputReceived;
public event EventHandler Exited {
add {
wrapped.EnableRaisingEvents = true;
wrapped.Exited += value;
}
remove {
wrapped.Exited -= value;
}
}
public bool HasExited => wrapped.HasExited;
public int ExitCode => wrapped.ExitCode;
public StreamWriter StandardInput => wrapped.StandardInput;
private readonly System.Diagnostics.Process wrapped;
internal Process(System.Diagnostics.Process wrapped) {
this.wrapped = wrapped;
}
public void Start() {
if (!OperatingSystem.IsWindows()) {
this.wrapped.OutputDataReceived += OnStandardOutputDataReceived;
this.wrapped.ErrorDataReceived += OnStandardErrorDataReceived;
}
this.wrapped.Start();
// https://github.com/dotnet/runtime/issues/81896
if (OperatingSystem.IsWindows()) {
Task.Factory.StartNew(ReadStandardOutputSynchronously, TaskCreationOptions.LongRunning);
Task.Factory.StartNew(ReadStandardErrorSynchronously, TaskCreationOptions.LongRunning);
}
else {
this.wrapped.BeginOutputReadLine();
this.wrapped.BeginErrorReadLine();
}
}
private void OnStandardOutputDataReceived(object sender, DataReceivedEventArgs e) {
OnStandardStreamDataReceived(e.Data, isError: false);
}
private void OnStandardErrorDataReceived(object sender, DataReceivedEventArgs e) {
OnStandardStreamDataReceived(e.Data, isError: true);
}
private void OnStandardStreamDataReceived(string? line, bool isError) {
if (line != null) {
OutputReceived?.Invoke(this, new Output(line, isError));
}
}
private void ReadStandardOutputSynchronously() {
ReadStandardStreamSynchronously(wrapped.StandardOutput, isError: false);
}
private void ReadStandardErrorSynchronously() {
ReadStandardStreamSynchronously(wrapped.StandardError, isError: true);
}
private void ReadStandardStreamSynchronously(StreamReader reader, bool isError) {
try {
while (reader.ReadLine() is {} line) {
OutputReceived?.Invoke(this, new Output(line, isError));
}
} catch (ObjectDisposedException) {
// Ignore.
}
}
public Task WaitForExitAsync(CancellationToken cancellationToken) {
return wrapped.WaitForExitAsync(cancellationToken);
}
public void Kill(bool entireProcessTree = false) {
wrapped.Kill(entireProcessTree);
}
public void Dispose() {
wrapped.Dispose();
}
}

View File

@@ -0,0 +1,37 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace Phantom.Utils.Runtime;
public sealed class ProcessConfigurator {
private readonly ProcessStartInfo startInfo = new () {
RedirectStandardOutput = true,
RedirectStandardError = true
};
public string FileName {
get => startInfo.FileName;
set => startInfo.FileName = value;
}
public Collection<string> ArgumentList => startInfo.ArgumentList;
public string WorkingDirectory {
get => startInfo.WorkingDirectory;
set => startInfo.WorkingDirectory = value;
}
public bool RedirectInput {
get => startInfo.RedirectStandardInput;
set => startInfo.RedirectStandardInput = value;
}
public bool UseShellExecute {
get => startInfo.UseShellExecute;
set => startInfo.UseShellExecute = value;
}
public Process CreateProcess() {
return new Process(new System.Diagnostics.Process { StartInfo = startInfo });
}
}

View File

@@ -0,0 +1,11 @@
namespace Phantom.Utils.Runtime;
public static class Tasks {
public static TaskCompletionSource CreateCompletionSource() {
return new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
}
public static TaskCompletionSource<T> CreateCompletionSource<T>() {
return new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
}
}