mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2024-11-26 01:42:53 +01:00
Compare commits
No commits in common. "dd57c442af945621526e484d62cf130dcf0dcdaf" and "89e67e16904e120e5e95086b84a79f3e015424f6" have entirely different histories.
dd57c442af
...
89e67e1690
@ -1,4 +1,5 @@
|
|||||||
using Phantom.Utils.Collections;
|
using System.Diagnostics;
|
||||||
|
using Phantom.Utils.Collections;
|
||||||
using Phantom.Utils.Runtime;
|
using Phantom.Utils.Runtime;
|
||||||
|
|
||||||
namespace Phantom.Agent.Minecraft.Instance;
|
namespace Phantom.Agent.Minecraft.Instance;
|
||||||
@ -18,8 +19,10 @@ 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.OutputReceived += ProcessOutputReceived;
|
this.process.OutputDataReceived += HandleOutputLine;
|
||||||
|
this.process.ErrorDataReceived += HandleOutputLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendCommand(string command, CancellationToken cancellationToken) {
|
public async Task SendCommand(string command, CancellationToken cancellationToken) {
|
||||||
@ -38,9 +41,11 @@ public sealed class InstanceProcess : IDisposable {
|
|||||||
OutputEvent -= listener;
|
OutputEvent -= listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessOutputReceived(object? sender, Process.Output output) {
|
private void HandleOutputLine(object sender, DataReceivedEventArgs args) {
|
||||||
outputBuffer.Add(output.Line);
|
if (args.Data is {} line) {
|
||||||
OutputEvent?.Invoke(this, output.Line);
|
outputBuffer.Add(line);
|
||||||
|
OutputEvent?.Invoke(this, line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessOnExited(object? sender, EventArgs e) {
|
private void ProcessOnExited(object? sender, EventArgs e) {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
using System.Text;
|
using System.Diagnostics;
|
||||||
|
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;
|
||||||
@ -55,17 +55,20 @@ public abstract class BaseLauncher : IServerLauncher {
|
|||||||
return new LaunchResult.CouldNotConfigureMinecraftServer();
|
return new LaunchResult.CouldNotConfigureMinecraftServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
var processConfigurator = new ProcessConfigurator {
|
var startInfo = new ProcessStartInfo {
|
||||||
FileName = javaRuntimeExecutable.ExecutablePath,
|
FileName = javaRuntimeExecutable.ExecutablePath,
|
||||||
WorkingDirectory = instanceProperties.InstanceFolder,
|
WorkingDirectory = instanceProperties.InstanceFolder,
|
||||||
RedirectInput = true,
|
RedirectStandardInput = true,
|
||||||
UseShellExecute = false
|
RedirectStandardOutput = true,
|
||||||
|
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 = processConfigurator.ArgumentList;
|
var processArguments = startInfo.ArgumentList;
|
||||||
jvmArguments.Build(processArguments);
|
jvmArguments.Build(processArguments);
|
||||||
|
|
||||||
foreach (var extraArgument in serverJar.ExtraArgs) {
|
foreach (var extraArgument in serverJar.ExtraArgs) {
|
||||||
@ -76,11 +79,13 @@ public abstract class BaseLauncher : IServerLauncher {
|
|||||||
processArguments.Add(serverJar.FilePath);
|
processArguments.Add(serverJar.FilePath);
|
||||||
processArguments.Add("nogui");
|
processArguments.Add("nogui");
|
||||||
|
|
||||||
var process = processConfigurator.CreateProcess();
|
var process = new Process { StartInfo = startInfo };
|
||||||
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.");
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Phantom.Common.Logging;
|
using System.Diagnostics;
|
||||||
|
using Phantom.Common.Logging;
|
||||||
using Phantom.Utils.Runtime;
|
using Phantom.Utils.Runtime;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ static class BackupCompressor {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var launcher = new ProcessConfigurator {
|
var startInfo = new ProcessStartInfo {
|
||||||
FileName = "zstd",
|
FileName = "zstd",
|
||||||
WorkingDirectory = workingDirectory,
|
WorkingDirectory = workingDirectory,
|
||||||
ArgumentList = {
|
ArgumentList = {
|
||||||
@ -56,14 +57,14 @@ static class BackupCompressor {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static void OnZstdOutput(object? sender, Process.Output output) {
|
static void OnZstdOutput(object? sender, DataReceivedEventArgs e) {
|
||||||
if (!string.IsNullOrWhiteSpace(output.Line)) {
|
if (!string.IsNullOrWhiteSpace(e.Data)) {
|
||||||
ZstdLogger.Debug("[Output] {Line}", output.Line);
|
ZstdLogger.Debug("[Output] {Line}", e.Data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var process = new OneShotProcess(ZstdLogger, launcher);
|
var process = new OneShotProcess(ZstdLogger, startInfo);
|
||||||
process.OutputReceived += OnZstdOutput;
|
process.Output += OnZstdOutput;
|
||||||
return await process.Run(cancellationToken);
|
return await process.Run(cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Phantom.Agent.Minecraft.Command;
|
using Phantom.Agent.Minecraft.Command;
|
||||||
using Phantom.Agent.Minecraft.Instance;
|
using Phantom.Agent.Minecraft.Instance;
|
||||||
using Phantom.Utils.Runtime;
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
namespace Phantom.Agent.Services.Backups;
|
namespace Phantom.Agent.Services.Backups;
|
||||||
@ -14,9 +13,9 @@ sealed partial class BackupServerCommandDispatcher : IDisposable {
|
|||||||
private readonly InstanceProcess process;
|
private readonly InstanceProcess process;
|
||||||
private readonly CancellationToken cancellationToken;
|
private readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
private readonly TaskCompletionSource automaticSavingDisabled = Tasks.CreateCompletionSource();
|
private readonly TaskCompletionSource automaticSavingDisabled = new ();
|
||||||
private readonly TaskCompletionSource savedTheGame = Tasks.CreateCompletionSource();
|
private readonly TaskCompletionSource savedTheGame = new ();
|
||||||
private readonly TaskCompletionSource automaticSavingEnabled = Tasks.CreateCompletionSource();
|
private readonly TaskCompletionSource automaticSavingEnabled = new ();
|
||||||
|
|
||||||
public BackupServerCommandDispatcher(ILogger logger, InstanceProcess process, CancellationToken cancellationToken) {
|
public BackupServerCommandDispatcher(ILogger logger, InstanceProcess process, CancellationToken cancellationToken) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
<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>
|
||||||
|
|
||||||
|
@ -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.Async(c => c.Console(outputTemplate: template, formatProvider: CultureInfo.InvariantCulture, theme: AnsiConsoleTheme.Literate))
|
.WriteTo.Console(outputTemplate: template, formatProvider: CultureInfo.InvariantCulture, theme: AnsiConsoleTheme.Literate)
|
||||||
.CreateLogger();
|
.CreateLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@
|
|||||||
<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>
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ 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;
|
||||||
|
|
||||||
@ -22,7 +21,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 = Tasks.CreateCompletionSource<Guid>();
|
private readonly TaskCompletionSource<Guid> agentGuidWaiter = new ();
|
||||||
|
|
||||||
public bool IsDisposed { get; private set; }
|
public bool IsDisposed { get; private set; }
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
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;
|
||||||
@ -16,7 +15,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] = Tasks.CreateCompletionSource<byte[]>();
|
replyTasks[sequenceId] = new TaskCompletionSource<byte[]>(TaskCreationOptions.None);
|
||||||
return sequenceId;
|
return sequenceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,24 +1,30 @@
|
|||||||
using Serilog;
|
using System.Diagnostics;
|
||||||
|
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 ProcessConfigurator configurator;
|
private readonly ProcessStartInfo startInfo;
|
||||||
|
|
||||||
public event EventHandler<Process.Output>? OutputReceived;
|
public event DataReceivedEventHandler? Output;
|
||||||
|
|
||||||
public OneShotProcess(ILogger logger, ProcessConfigurator configurator) {
|
public OneShotProcess(ILogger logger, ProcessStartInfo startInfo) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.configurator = configurator;
|
this.startInfo = startInfo;
|
||||||
|
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 = configurator.CreateProcess();
|
using var process = new Process { StartInfo = startInfo };
|
||||||
process.OutputReceived += OutputReceived;
|
process.OutputDataReceived += Output;
|
||||||
|
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;
|
||||||
|
@ -1,92 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
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 });
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user