tgstation-server 6.12.3
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 ValueTask<IEngineInstallation> CreateInstallation(EngineVersion version, string path, Task installationTask, CancellationToken cancellationToken)
78 {
79 CheckVersionValidity(version);
80
81 var installationIOManager = new ResolvingIOManager(IOManager, path);
82 var supportsMapThreads = version.Version >= MapThreadsVersion;
83
84 return ValueTask.FromResult<IEngineInstallation>(
86 installationIOManager,
87 installationTask,
88 version,
89 installationIOManager.ResolvePath(
90 installationIOManager.ConcatPath(
93 version.Version!,
94 out var supportsCli))),
95 installationIOManager.ResolvePath(
96 installationIOManager.ConcatPath(
99 supportsCli,
100 supportsMapThreads));
101 }
102
104 public override async Task CleanCache(CancellationToken cancellationToken)
105 {
106 try
107 {
108 var byondDir = PathToUserFolder;
109
110 Logger.LogDebug("Cleaning BYOND cache...");
111 async Task CleanDirectorySafe()
112 {
113 try
114 {
117 byondDir,
119 cancellationToken);
120 }
121 catch (Exception ex)
122 {
123 Logger.LogWarning(ex, "Failed to clean BYOND cache!");
124 }
125 }
126
127 var cacheCleanTask = CleanDirectorySafe();
128
129 // Create local cfg directory in case it doesn't exist
130 var localCfgDirectory = IOManager.ConcatPath(
131 byondDir,
133
134 var cfgCreateTask = IOManager.CreateDirectory(
135 localCfgDirectory,
136 cancellationToken);
137
138 var additionalCleanTasks = AdditionalCacheCleanFilePaths(localCfgDirectory)
139 .Select(path => IOManager.DeleteFile(path, cancellationToken));
140
141 await Task.WhenAll(cacheCleanTask, cfgCreateTask, Task.WhenAll(additionalCleanTasks));
142 }
143 catch (Exception ex) when (ex is not OperationCanceledException)
144 {
145 Logger.LogWarning(ex, "Error cleaning BYOND cache!");
146 }
147 }
148
150 public override async ValueTask<IEngineInstallationData> DownloadVersion(EngineVersion version, JobProgressReporter progressReporter, CancellationToken cancellationToken)
151 {
152 CheckVersionValidity(version);
153
154 var url = GetDownloadZipUrl(version);
155 Logger.LogTrace("Downloading {engineType} version {version} from {url}...", TargetEngineType, version, url);
156
157 await using var download = fileDownloader.DownloadFile(url, null);
158 await using var buffer = new BufferedFileStreamProvider(
159 await download.GetResult(cancellationToken));
160
161 var stream = await buffer.GetOwnedResult(cancellationToken);
162 try
163 {
165 IOManager,
166 stream);
167 }
168 catch
169 {
170 await stream.DisposeAsync();
171 throw;
172 }
173 }
174
181 protected abstract string GetDreamDaemonName(Version byondVersion, out bool supportsCli);
182
188 protected virtual IEnumerable<string> AdditionalCacheCleanFilePaths(string configDirectory) => Enumerable.Empty<string>();
189
196 {
197 CheckVersionValidity(version);
198 var url = String.Format(CultureInfo.InvariantCulture, ByondRevisionsUrlTemplate, version.Version!.Major, version.Version.Minor);
199 return new Uri(url);
200 }
201 }
202}
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.
override ValueTask< IEngineInstallation > CreateInstallation(EngineVersion version, string path, Task installationTask, CancellationToken cancellationToken)
Creates an IEngineInstallation for a given version .A ValueTask<TResult> resulting in the IEngineInst...
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.
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