tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ByondInstallerBase.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Threading;
6using System.Threading.Tasks;
7
8using Microsoft.Extensions.Logging;
9
13
15{
20 {
24 protected const string ByondBinPath = "byond/bin";
25
29 protected const string CfgDirectoryName = "cfg";
30
34 const string CacheDirectoryName = "cache";
35
39 static readonly Version MapThreadsVersion = new(515, 1609);
40
42 protected override EngineType TargetEngineType => EngineType.Byond;
43
47 protected abstract string PathToUserFolder { get; }
48
52 protected abstract string DreamMakerName { get; }
53
57 protected abstract string ByondRevisionsUrlTemplate { get; }
58
63
70 protected ByondInstallerBase(IIOManager ioManager, ILogger<ByondInstallerBase> logger, IFileDownloader fileDownloader)
71 : base(ioManager, logger)
72 {
73 this.fileDownloader = fileDownloader ?? throw new ArgumentNullException(nameof(fileDownloader));
74 }
75
77 public override IEngineInstallation CreateInstallation(EngineVersion version, string path, Task installationTask)
78 {
79 CheckVersionValidity(version);
80
81 var installationIOManager = new ResolvingIOManager(IOManager, path);
82 var supportsMapThreads = version.Version >= MapThreadsVersion;
83
84 return new ByondInstallation(
85 installationIOManager,
86 installationTask,
87 version,
88 installationIOManager.ResolvePath(
89 installationIOManager.ConcatPath(
92 version.Version!,
93 out var supportsCli))),
94 installationIOManager.ResolvePath(
95 installationIOManager.ConcatPath(
98 supportsCli,
99 supportsMapThreads);
100 }
101
103 public override async Task CleanCache(CancellationToken cancellationToken)
104 {
105 try
106 {
107 var byondDir = PathToUserFolder;
108
109 Logger.LogDebug("Cleaning BYOND cache...");
110 async Task CleanDirectorySafe()
111 {
112 try
113 {
116 byondDir,
118 cancellationToken);
119 }
120 catch (Exception ex)
121 {
122 Logger.LogWarning(ex, "Failed to clean BYOND cache!");
123 }
124 }
125
126 var cacheCleanTask = CleanDirectorySafe();
127
128 // Create local cfg directory in case it doesn't exist
129 var localCfgDirectory = IOManager.ConcatPath(
130 byondDir,
132
133 var cfgCreateTask = IOManager.CreateDirectory(
134 localCfgDirectory,
135 cancellationToken);
136
137 var additionalCleanTasks = AdditionalCacheCleanFilePaths(localCfgDirectory)
138 .Select(path => IOManager.DeleteFile(path, cancellationToken));
139
140 await Task.WhenAll(cacheCleanTask, cfgCreateTask, Task.WhenAll(additionalCleanTasks));
141 }
142 catch (Exception ex) when (ex is not OperationCanceledException)
143 {
144 Logger.LogWarning(ex, "Error cleaning BYOND cache!");
145 }
146 }
147
149 public override async ValueTask<IEngineInstallationData> DownloadVersion(EngineVersion version, JobProgressReporter progressReporter, CancellationToken cancellationToken)
150 {
151 CheckVersionValidity(version);
152
153 var url = GetDownloadZipUrl(version);
154 Logger.LogTrace("Downloading {engineType} version {version} from {url}...", TargetEngineType, version, url);
155
156 await using var download = fileDownloader.DownloadFile(url, null);
157 await using var buffer = new BufferedFileStreamProvider(
158 await download.GetResult(cancellationToken));
159
160 var stream = await buffer.GetOwnedResult(cancellationToken);
161 try
162 {
164 IOManager,
165 stream);
166 }
167 catch
168 {
169 await stream.DisposeAsync();
170 throw;
171 }
172 }
173
180 protected abstract string GetDreamDaemonName(Version byondVersion, out bool supportsCli);
181
187 protected virtual IEnumerable<string> AdditionalCacheCleanFilePaths(string configDirectory) => Enumerable.Empty<string>();
188
195 {
196 CheckVersionValidity(version);
197 var url = String.Format(CultureInfo.InvariantCulture, ByondRevisionsUrlTemplate, version.Version!.Major, version.Version.Minor);
198 return new Uri(url);
199 }
200 }
201}
Information about an engine installation.
Version? Version
The System.Version of the engine. Currently only valid when Engine is EngineType.Byond.
Implementation of IEngineInstallation for EngineType.Byond.
Base implementation of IEngineInstaller for EngineType.Byond.
readonly IFileDownloader fileDownloader
The IFileDownloader for the ByondInstallerBase.
string GetDreamDaemonName(Version byondVersion, out bool supportsCli)
Get the file name of the DreamDaemon executable.
const string CacheDirectoryName
The name of BYOND's cache directory.
const string CfgDirectoryName
The path to the cfg directory.
static readonly Version MapThreadsVersion
The first Version of BYOND that supports the '-map-threads' parameter on DreamDaemon.
string ByondRevisionsUrlTemplate
Gets the URL formatter string for downloading a byond version of {0:Major} {1:Minor}.
const string ByondBinPath
The path to the BYOND bin folder.
virtual IEnumerable< string > AdditionalCacheCleanFilePaths(string configDirectory)
List off additional file paths in the configDirectory to delete.
ByondInstallerBase(IIOManager ioManager, ILogger< ByondInstallerBase > logger, IFileDownloader fileDownloader)
Initializes a new instance of the ByondInstallerBase class.
override async Task CleanCache(CancellationToken cancellationToken)
Attempts to cleans the engine's cache folder for the system.A Task representing the running operation...
Uri GetDownloadZipUrl(EngineVersion version)
Create a Uri pointing to the location of the download for a given version .
string PathToUserFolder
Path to the system user's local BYOND folder.
override async ValueTask< IEngineInstallationData > DownloadVersion(EngineVersion version, JobProgressReporter progressReporter, CancellationToken cancellationToken)
Download a given engine version .A ValueTask<TResult> resulting in the IEngineInstallationData for th...
string DreamMakerName
Path to the DreamMaker executable.
override IEngineInstallation CreateInstallation(EngineVersion version, string path, Task installationTask)
Creates an IEngineInstallation for a given version .The IEngineInstallation.
void CheckVersionValidity(EngineVersion version)
Check that a given version is of type EngineType.Byond.
IIOManager IOManager
Gets the IIOManager for the EngineInstallerBase.
ILogger< EngineInstallerBase > Logger
Gets the ILogger for the EngineInstallerBase.
Implementation of IEngineInstallationData for a zip file in a Stream.
IFileStreamProvider that provides a ISeekableFileStreamProvider from an input Stream.
An IIOManager that resolve relative paths from another IIOManager to a subdirectory of that.
IFileStreamProvider DownloadFile(Uri url, string? bearerToken)
Downloads a file from a given url .
Interface for using filesystems.
Definition IIOManager.cs:13
string ConcatPath(params string[] paths)
Combines an array of strings into a path.
Task CreateDirectory(string path, CancellationToken cancellationToken)
Create a directory at path .
Task DeleteFile(string path, CancellationToken cancellationToken)
Deletes a file at path .
Task DeleteDirectory(string path, CancellationToken cancellationToken)
Recursively delete a directory, removes and does not enter any symlinks encounterd.
EngineType
The type of engine the codebase is using.
Definition EngineType.cs:7