tgstation-server  4.4.0
The /tg/station 13 server suite
ByondExecutableLock.cs
Go to the documentation of this file.
1 using System;
2 using System.Text;
3 using System.Threading;
4 using System.Threading.Tasks;
6 using Tgstation.Server.Host.IO;
7 
8 namespace Tgstation.Server.Host.Components.Byond
9 {
12  {
14  public Version Version { get; }
15 
17  public string DreamDaemonPath { get; }
18 
20  public string DreamMakerPath { get; }
21 
26 
30  readonly SemaphoreSlim trustedFileSemaphore;
31 
35  readonly string trustedFilePath;
36 
47  IIOManager ioManager,
48  SemaphoreSlim trustedFileSemaphore,
49  Version version,
50  string dreamDaemonPath,
51  string dreamMakerPath,
52  string trustedFilePath)
53  {
54  this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager));
55  this.trustedFileSemaphore = trustedFileSemaphore ?? throw new ArgumentNullException(nameof(trustedFileSemaphore));
56  Version = version ?? throw new ArgumentNullException(nameof(version));
57  DreamDaemonPath = dreamDaemonPath ?? throw new ArgumentNullException(nameof(dreamDaemonPath));
58  DreamMakerPath = dreamMakerPath ?? throw new ArgumentNullException(nameof(dreamMakerPath));
59  this.trustedFilePath = trustedFilePath ?? throw new ArgumentNullException(nameof(trustedFilePath));
60  }
61 
62  // at one point in design, byond versions were to delete themselves if they weren't the active version
63  // That changed at some point so these functions are intentioanlly left blank
64 
66  public void Dispose() { }
67 
69  public void DoNotDeleteThisSession() { }
70 
72  public async Task TrustDmbPath(string fullDmbPath, CancellationToken cancellationToken)
73  {
74  if (fullDmbPath == null)
75  throw new ArgumentNullException(nameof(fullDmbPath));
76 
77  using (await SemaphoreSlimContext.Lock(trustedFileSemaphore, cancellationToken).ConfigureAwait(false))
78  {
79  string trustedFileText;
80 
81  if (await ioManager.FileExists(trustedFilePath, cancellationToken).ConfigureAwait(false))
82  {
83  var trustedFileBytes = await ioManager.ReadAllBytes(trustedFilePath, cancellationToken).ConfigureAwait(false);
84  trustedFileText = Encoding.UTF8.GetString(trustedFileBytes);
85  trustedFileText = $"{trustedFileText.Trim()}{Environment.NewLine}";
86  }
87  else
88  {
89  trustedFileText = String.Empty;
90  }
91 
92  if (trustedFileText.Contains(fullDmbPath, StringComparison.Ordinal))
93  return;
94 
95  trustedFileText = $"{trustedFileText}{fullDmbPath}{Environment.NewLine}";
96 
97  var newTrustedFileBytes = Encoding.UTF8.GetBytes(trustedFileText);
98  await ioManager.WriteAllBytes(trustedFilePath, newTrustedFileBytes, cancellationToken).ConfigureAwait(false);
99  }
100  }
101  }
102 }
readonly IIOManager ioManager
The IIOManager for the ByondExecutableLock.
async Task TrustDmbPath(string fullDmbPath, CancellationToken cancellationToken)
Add a given fullDmbPath to the trusted DMBs list in BYOND's config.
ByondExecutableLock(IIOManager ioManager, SemaphoreSlim trustedFileSemaphore, Version version, string dreamDaemonPath, string dreamMakerPath, string trustedFilePath)
Construct a ByondExecutableLock
void DoNotDeleteThisSession()
Call if, during a detach, this version should not be deleted
readonly SemaphoreSlim trustedFileSemaphore
SemaphoreSlim used to guard access to the trustedFilePath.
Represents usage of the two primary BYOND server executables
readonly string trustedFilePath
The path to the BYOND trusted .dmbs configuration file.
Interface for using filesystems
Definition: IIOManager.cs:11
static async Task< SemaphoreSlimContext > Lock(SemaphoreSlim semaphore, CancellationToken cancellationToken)
Asyncronously locks a semaphore