tgstation-server 6.19.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
EngineInstallationBase.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using System.Threading.Tasks;
7using System.Web;
8
9using DotEnv.Core;
10
11using Microsoft.Extensions.Logging;
12
18
20{
22 abstract class EngineInstallationBase : IEngineInstallation
23 {
25 public abstract EngineVersion Version { get; }
26
28 public abstract string ServerExePath { get; }
29
31 public abstract string CompilerExePath { get; }
32
34 public abstract bool HasStandardOutput { get; }
35
37 public abstract bool PreferFileLogging { get; }
38
40 public abstract bool PromptsForNetworkAccess { get; }
41
43 public abstract bool UseDotnetDump { get; }
44
46 public abstract Task InstallationTask { get; }
47
51 protected IIOManager InstallationIOManager { get; }
52
59 protected static string EncodeParameters(
60 IReadOnlyDictionary<string, string>? parameters,
61 DreamDaemonLaunchParameters launchParameters)
62 {
63 var parametersString = parameters != null
64 ? $"{String.Join('&', parameters.Select(kvp => $"{HttpUtility.UrlEncode(kvp.Key)}={HttpUtility.UrlEncode(kvp.Value)}"))}&"
65 : String.Empty;
66
67 if (!String.IsNullOrEmpty(launchParameters.AdditionalParameters))
68 parametersString += launchParameters.AdditionalParameters;
69
70 return parametersString;
71 }
72
77 public EngineInstallationBase(IIOManager installationIOManager)
78 {
79 InstallationIOManager = installationIOManager ?? throw new ArgumentNullException(nameof(installationIOManager));
80 }
81
83 public abstract string FormatCompilerArguments(string dmePath, string? additionalArguments);
84
86 public abstract string FormatServerArguments(
87 IDmbProvider dmbProvider,
88 IReadOnlyDictionary<string, string>? parameters,
89 DreamDaemonLaunchParameters launchParameters,
90 string accessIdentifier,
91 string? logFilePath);
92
94 public virtual async ValueTask StopServerProcess(ILogger logger, IProcess process, string accessIdentifier, ushort port, CancellationToken cancellationToken)
95 {
96 ArgumentNullException.ThrowIfNull(logger);
97 cancellationToken.ThrowIfCancellationRequested();
98 logger.LogTrace("Terminating engine server process...");
99 process.Terminate();
100 await process.Lifetime;
101 }
102
104 public async ValueTask<Dictionary<string, string>?> LoadEnv(ILogger logger, bool forCompiler, CancellationToken cancellationToken)
105 {
106 ArgumentNullException.ThrowIfNull(logger);
107
108 var envFile = forCompiler
109 ? "compiler.env"
110 : "server.env";
111
112 if (!await InstallationIOManager.FileExists(envFile, cancellationToken))
113 {
114 logger.LogTrace("No {envFile} present in engine installation {version}", envFile, Version);
115 return null;
116 }
117
118 logger.LogDebug("Loading {envFile} for engine installation {version}...", envFile, Version);
119
120 var fileBytes = await InstallationIOManager.ReadAllBytes(envFile, cancellationToken);
121 var fileContents = Encoding.UTF8.GetString(fileBytes);
122 var parser = new EnvParser();
123
124 try
125 {
126 var variables = parser.Parse(fileContents);
127
128 return variables.ToDictionary();
129 }
130 catch (Exception ex)
131 {
132 logger.LogWarning(ex, "Unable to parse {envFile}!", envFile);
133 return null;
134 }
135 }
136 }
137}
Information about an engine installation.
Provides absolute paths to the latest compiled .dmbs.
Interface for using filesystems.
Definition IIOManager.cs:14
Task< int?> Lifetime
The Task<TResult> resulting in the exit code of the process or null if the process was detached.
Abstraction over a global::System.Diagnostics.Process.
Definition IProcess.cs:11
void Terminate()
Asycnhronously terminates the process.
await base. StopServerProcess(logger, process, accessIdentifier, port, cancellationToken)