2using System.Collections.Generic;
8using System.Threading.Tasks;
10using Microsoft.Extensions.Logging;
71 readonly ILogger<EngineManager>
logger;
76 readonly Dictionary<EngineVersion, ReferenceCountingContainer<IEngineInstallation, EngineExecutableLock>>
installedVersions;
94 ArgumentNullException.ThrowIfNull(version);
96 if (!version.
Engine.HasValue)
97 throw new InvalidOperationException(
"version.Engine cannot be null!");
100 throw new InvalidOperationException(
"version.CustomIteration cannot be 0!");
116 ILogger<EngineManager>
logger)
122 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
124 installedVersions =
new Dictionary<EngineVersion, ReferenceCountingContainer<IEngineInstallation, EngineExecutableLock>>();
136 Stream? customVersionStream,
137 bool allowInstallation,
138 CancellationToken cancellationToken)
155 var stringVersion = version.
ToString();
161 ActiveVersion?.ToString(),
169 logger.LogInformation(
"Active version changed to {version}", version);
176 public async ValueTask<IEngineExecutableLock>
UseExecutables(
EngineVersion? requiredVersion,
string? trustDmbFullPath, CancellationToken cancellationToken)
179 "Acquiring lock on BYOND version {version}...",
180 requiredVersion?.ToString() ?? $
"{ActiveVersion} (active)");
189 requiredVersion !=
null,
194 if (trustDmbFullPath !=
null)
201 installLock.Dispose();
209 ArgumentNullException.ThrowIfNull(progressReporter);
213 logger.LogTrace(
"DeleteVersion {version}", version);
216 if (activeVersion !=
null && version.
Equals(activeVersion))
220 logger.LogTrace(
"Waiting to acquire installedVersions lock...");
225 logger.LogTrace(
"Version {version} already deleted.", version);
229 container = containerNullable;
230 logger.LogTrace(
"Installation container acquired for deletion");
233 progressReporter.StageName =
"Waiting for version to not be in use...";
239 Task activeVersionUpdate;
243 logger.LogTrace(
"Waiting for container.OnZeroReferences or switch of active version...");
244 if (!containerTask.IsCompleted)
250 .WaitAsync(cancellationToken);
252 if (containerTask.IsCompleted)
253 logger.LogTrace(
"All locks for version {version} are gone", version);
255 logger.LogTrace(
"activeVersion changed, we may have to wait again. Acquiring semaphore...");
261 if (activeVersion !=
null && version.
Equals(activeVersion))
265 logger.LogTrace(
"Locking installedVersions...");
271 logger.LogWarning(
"Unable to remove engine installation {version} from list! Is there a duplicate job running?", version);
274 if (container != newerContainer)
279 logger.LogDebug(
"Extreme race condition encountered, applying concentrated copium...");
280 container = newerContainer;
286 logger.LogTrace(
"Proceeding with installation deletion...");
294 logger.LogInformation(
"Deleting version {version}...", version);
295 progressReporter.StageName =
"Deleting installation...";
298 var installPath = version.
ToString();
306 if (containerTask.IsCompleted)
308 "Another lock was acquired before we could remove version {version} from the list. We will have to wait again.",
311 logger.LogTrace(
"Not proceeding for some reason or another");
317 public async Task
StartAsync(CancellationToken cancellationToken)
319 async ValueTask<byte[]?> GetActiveVersion()
325 var activeVersionBytesTask = GetActiveVersion();
330 var installedVersionPaths =
new Dictionary<string, EngineVersion>();
332 async ValueTask ReadVersion(
string path)
343 var text = Encoding.UTF8.GetString(bytes);
352 version = versionNullable!;
358 logger.LogDebug(
"Added detected BYOND version {versionKey}...", version);
364 "It seems that there are multiple directories that say they contain BYOND version {version}. We're ignoring and cleaning the duplicate: {duplicatePath}",
371 lock (installedVersionPaths)
377 .Select(ReadVersion));
379 logger.LogTrace(
"Upgrading BYOND installations...");
381 installedVersionPaths
384 var activeVersionBytes = await activeVersionBytesTask;
385 if (activeVersionBytes !=
null)
387 var activeVersionString = Encoding.UTF8.GetString(activeVersionBytes);
390 bool hasRequestedActiveVersion;
395 if (hasRequestedActiveVersion)
399 logger.LogWarning(
"Failed to load saved active version {activeVersion}!", activeVersionString);
406 public Task
StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
421 Stream? customVersionStream,
423 bool allowInstallation,
424 CancellationToken cancellationToken)
426 var ourTcs =
new TaskCompletionSource();
429 bool installedOrInstalling;
436 if (customVersionStream !=
null)
438 var customInstallationNumber = 1;
441 version.CustomIteration = customInstallationNumber++;
458 installedOrInstalling =
installedVersions.TryGetValue(version, out var installationContainerNullable);
460 if (!installedOrInstalling)
462 if (!allowInstallation)
463 throw new InvalidOperationException($
"Engine version {version} not installed!");
468 installationContainer = installationContainerNullable!;
470 installation = installationContainer.
Instance;
474 var deploymentPipelineProcesses = !neededForLock;
477 if (installedOrInstalling)
479 progressReporter.StageName =
"Waiting for existing installation job...";
482 logger.LogWarning(
"The required engine version ({version}) is not readily available! We will have to wait for it to install.", version);
489 string? installPath =
null;
492 if (customVersionStream !=
null)
493 logger.LogInformation(
"Installing custom engine version as {version}...", version);
494 else if (neededForLock)
499 logger.LogWarning(
"The required engine version ({version}) is not readily available! We will have to install it.", version);
502 logger.LogInformation(
"Requested engine version {version} not currently installed. Doing so now...", version);
504 progressReporter.StageName =
"Running event";
506 var versionString = version.
ToString();
509 installPath = await
InstallVersionFiles(progressReporter, version, customVersionStream, deploymentPipelineProcesses, cancellationToken);
516 if (installPath !=
null)
520 logger.LogDebug(
"Cleaning up failed installation at {path}...", installPath);
525 logger.LogError(ex2,
"Error cleaning up failed installation!");
528 else if (ex is not OperationCanceledException)
534 ourTcs.SetException(ex);
560 Stream? customVersionStream,
561 bool deploymentPipelineProcesses,
562 CancellationToken cancellationToken)
565 async ValueTask DirectoryCleanup()
571 var directoryCleanupTask = DirectoryCleanup();
575 var remainingProgress = 1.0;
576 if (customVersionStream ==
null)
578 using var subReporter = progressReporter.
CreateSection(
"Downloading Version", 0.5);
579 remainingProgress -= 0.5;
583#pragma warning disable CA2000
586 customVersionStream);
587#pragma warning restore CA2000
592 remainingReporter = progressReporter.
CreateSection(
null, remainingProgress);
596 await engineInstallationData.DisposeAsync();
600 using (remainingReporter)
602 await
using (engineInstallationData)
604 remainingReporter.StageName =
"Cleaning target directory";
606 await directoryCleanupTask;
608 remainingReporter.StageName =
"Extracting data";
610 logger.LogTrace(
"Extracting engine to {extractPath}...", installFullPath);
611 await engineInstallationData.
ExtractToPath(installFullPath, cancellationToken);
615 remainingReporter.StageName =
"Running installation actions";
617 var installation = await
engineInstaller.
Install(version, installFullPath, deploymentPipelineProcesses, cancellationToken);
620 var serverInstallTask =
ioManager.
FileExists(installation.ServerExePath, cancellationToken);
623 logger.LogError(
"Compiler executable does not exist after engine installation!");
627 if (!await serverInstallTask)
629 logger.LogError(
"Server executable does not exist after engine installation!");
634 remainingReporter.StageName =
"Writing version file";
639 Encoding.UTF8.GetBytes(version.
ToString()),
643 catch (HttpRequestException ex)
648 catch (OperationCanceledException)
658 return installFullPath;
673 return installationContainer;
Information about an engine installation.
override string ToString()
static bool TryParse(string input, out EngineVersion? engineVersion)
Attempts to parse a stringified EngineVersion.
bool Equals(EngineVersion other)
EngineType? Engine
The EngineType.
int? CustomIteration
The revision of the custom build.
Extension methods for the ValueTask and ValueTask<TResult> classes.
static async ValueTask WhenAll(IEnumerable< ValueTask > tasks)
Fully await a given list of tasks .
static void CheckVersionParameter(EngineVersion version)
Validates a given version parameter.
readonly Dictionary< EngineVersion, ReferenceCountingContainer< IEngineInstallation, EngineExecutableLock > > installedVersions
Map of byond EngineVersions to Tasks that complete when they are installed.
Task StopAsync(CancellationToken cancellationToken)
const string VersionFileName
The file in which we store the Version for installations.
async Task StartAsync(CancellationToken cancellationToken)
IReadOnlyList< EngineVersion > InstalledVersions
The installed EngineVersions.
async ValueTask< EngineExecutableLock > AssertAndLockVersion(JobProgressReporter progressReporter, EngineVersion version, Stream? customVersionStream, bool neededForLock, bool allowInstallation, CancellationToken cancellationToken)
Ensures a BYOND version is installed if it isn't already.
volatile TaskCompletionSource activeVersionChanged
TaskCompletionSource that notifes when the ActiveVersion changes.
const string ActiveVersionFileName
The file in which we store the ActiveVersion.
async ValueTask< IEngineExecutableLock > UseExecutables(EngineVersion? requiredVersion, string? trustDmbFullPath, CancellationToken cancellationToken)
Lock the current installation's location and return a IEngineExecutableLock.A ValueTask<TResult> resu...
async ValueTask DeleteVersion(JobProgressReporter progressReporter, EngineVersion version, CancellationToken cancellationToken)
Deletes a given version from the disk.A Task representing the running operation.
EngineVersion? ActiveVersion
The currently active EngineVersion.
readonly SemaphoreSlim changeDeleteSemaphore
The SemaphoreSlim for changing or deleting the active BYOND version.
async ValueTask ChangeVersion(JobProgressReporter progressReporter, EngineVersion version, Stream? customVersionStream, bool allowInstallation, CancellationToken cancellationToken)
Change the active EngineVersion.A ValueTask representing the running operation.
readonly IEventConsumer eventConsumer
The IEventConsumer for the EngineManager.
readonly IIOManager ioManager
The IIOManager for the EngineManager.
async ValueTask< string > InstallVersionFiles(JobProgressReporter progressReporter, EngineVersion version, Stream? customVersionStream, bool deploymentPipelineProcesses, CancellationToken cancellationToken)
Installs the files for a given BYOND version .
ReferenceCountingContainer< IEngineInstallation, EngineExecutableLock > AddInstallationContainer(IEngineInstallation installation)
Create and add a new IEngineInstallation to installedVersions.
readonly IEngineInstaller engineInstaller
The IEngineInstaller for the EngineManager.
EngineManager(IIOManager ioManager, IEngineInstaller engineInstaller, IEventConsumer eventConsumer, IDmbFactory dmbFactory, ILogger< EngineManager > logger)
Initializes a new instance of the EngineManager class.
readonly ILogger< EngineManager > logger
The ILogger for the EngineManager.
readonly IDmbFactory dmbFactory
The IDmbFactory for the EngineManager.
Implementation of IEngineInstallationData for a zip file in a Stream.
IIOManager that resolves paths to Environment.CurrentDirectory.
const string CurrentDirectory
Path to the current working directory for the IIOManager.
Operation exceptions thrown from the context of a Models.Job.
Progress reporter for a Job.
JobProgressReporter CreateSection(string? newStageName, double percentage)
Create a subsection of the JobProgressReporter with its optional own stage name.
void ReportProgress(double? progress)
Report progress.
Wrapper for managing some TWrapped .
TReference AddReference()
Create a new TReference to the Instance.
TWrapped Instance
The TWrapped .
Task OnZeroReferences
A Task that completes when there are no TReference s active for the Instance.
Async lock context helper.
static async ValueTask< SemaphoreSlimContext > Lock(SemaphoreSlim semaphore, CancellationToken cancellationToken, ILogger? logger=null)
Asyncronously locks a semaphore .
Factory for IDmbProviders.
void LogLockStates()
Log the states of all active IDmbProviders.
Wraps data containing an engine installation.
ValueTask ExtractToPath(string path, CancellationToken cancellationToken)
Extracts the installation to a given path.
Represents a BYOND installation.
Task InstallationTask
The Task that completes when the BYOND version finished installing.
EngineVersion Version
The EngineVersion of the IEngineInstallation.
For downloading and installing game engines for a given system.
ValueTask< IEngineInstallationData > DownloadVersion(EngineVersion version, JobProgressReporter jobProgressReporter, CancellationToken cancellationToken)
Download a given engine version .
ValueTask< IEngineInstallation > Install(EngineVersion version, string path, bool deploymentPipelineProcesses, CancellationToken cancellationToken)
Does actions necessary to get an extracted installation working.
ValueTask UpgradeInstallation(EngineVersion version, string path, CancellationToken cancellationToken)
Does actions necessary to get upgrade a version installed by a previous version of TGS.
ValueTask< IEngineInstallation > GetInstallation(EngineVersion version, string path, Task installationTask, CancellationToken cancellationToken)
Creates an IEngineInstallation for a given version .
ValueTask TrustDmbPath(EngineVersion version, string fullDmbPath, CancellationToken cancellationToken)
Add a given fullDmbPath to the trusted DMBs list in BYOND's config.
For managing the engine installations.
Consumes EventTypes and takes the appropriate actions.
ValueTask HandleEvent(EventType eventType, IEnumerable< string?> parameters, bool deploymentPipeline, CancellationToken cancellationToken)
Handle a given eventType .
Interface for using filesystems.
string ResolvePath()
Retrieve the full path of the current working directory.
ValueTask< byte[]> ReadAllBytes(string path, CancellationToken cancellationToken)
Returns all the contents of a file at path as a byte array.
string ConcatPath(params string[] paths)
Combines an array of strings into a path.
Task< IReadOnlyList< string > > GetDirectories(string path, CancellationToken cancellationToken)
Returns full directory names in a given path .
Task CreateDirectory(string path, CancellationToken cancellationToken)
Create a directory at path .
Task DeleteFile(string path, CancellationToken cancellationToken)
Deletes a file at path .
ValueTask WriteAllBytes(string path, byte[] contents, CancellationToken cancellationToken)
Writes some contents to a file at path overwriting previous content.
Task DeleteDirectory(string path, CancellationToken cancellationToken)
Recursively delete a directory, removes and does not enter any symlinks encounterd.
Task< bool > FileExists(string path, CancellationToken cancellationToken)
Check that the file at path exists.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
EventType
Types of events. Mirror in tgs.dm. Prefer last listed name for script.