tgstation-server  4.4.0
The /tg/station 13 server suite
InstanceFactory.cs
Go to the documentation of this file.
1 using Microsoft.Extensions.Logging;
2 using System;
3 using System.Threading;
4 using System.Threading.Tasks;
16 using Tgstation.Server.Host.IO;
20 
21 namespace Tgstation.Server.Host.Components
22 {
25  {
30 
35 
40 
44  readonly ILoggerFactory loggerFactory;
45 
50 
55 
60 
65 
70 
75 
80 
85 
90 
95 
100 
105 
110 
115 
120 
125 
150  IIOManager ioManager,
151  IDatabaseContextFactory databaseContextFactory,
152  IAssemblyInformationProvider assemblyInformationProvider,
153  ILoggerFactory loggerFactory,
154  ITopicClientFactory topicClientFactory,
155  ICryptographySuite cryptographySuite,
156  ISynchronousIOManager synchronousIOManager,
157  ISymlinkFactory symlinkFactory,
158  IByondInstaller byondInstaller,
159  IChatManagerFactory chatFactory,
160  IProcessExecutor processExecutor,
161  IPostWriteHandler postWriteHandler,
162  IWatchdogFactory watchdogFactory,
163  IJobManager jobManager,
164  INetworkPromptReaper networkPromptReaper,
165  IGitHubClientFactory gitHubClientFactory,
166  IPlatformIdentifier platformIdentifier,
167  ILibGit2RepositoryFactory repositoryFactory,
168  ILibGit2Commands repositoryCommands,
169  IServerPortProvider serverPortProvider)
170  {
171  this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager));
172  this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory));
173  this.assemblyInformationProvider = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider));
174  this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
175  this.topicClientFactory = topicClientFactory ?? throw new ArgumentNullException(nameof(topicClientFactory));
176  this.cryptographySuite = cryptographySuite ?? throw new ArgumentNullException(nameof(cryptographySuite));
177  this.synchronousIOManager = synchronousIOManager ?? throw new ArgumentNullException(nameof(synchronousIOManager));
178  this.symlinkFactory = symlinkFactory ?? throw new ArgumentNullException(nameof(symlinkFactory));
179  this.byondInstaller = byondInstaller ?? throw new ArgumentNullException(nameof(byondInstaller));
180  this.chatFactory = chatFactory ?? throw new ArgumentNullException(nameof(chatFactory));
181  this.processExecutor = processExecutor ?? throw new ArgumentNullException(nameof(processExecutor));
182  this.postWriteHandler = postWriteHandler ?? throw new ArgumentNullException(nameof(postWriteHandler));
183  this.watchdogFactory = watchdogFactory ?? throw new ArgumentNullException(nameof(watchdogFactory));
184  this.jobManager = jobManager ?? throw new ArgumentNullException(nameof(jobManager));
185  this.networkPromptReaper = networkPromptReaper ?? throw new ArgumentNullException(nameof(networkPromptReaper));
186  this.gitHubClientFactory = gitHubClientFactory ?? throw new ArgumentNullException(nameof(gitHubClientFactory));
187  this.platformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier));
188  this.repositoryFactory = repositoryFactory ?? throw new ArgumentNullException(nameof(repositoryFactory));
189  this.repositoryCommands = repositoryCommands ?? throw new ArgumentNullException(nameof(repositoryCommands));
190  this.serverPortProvider = serverPortProvider ?? throw new ArgumentNullException(nameof(serverPortProvider));
191  }
192 
194 #pragma warning disable CA1506 // TODO: Decomplexify
195  public IInstance CreateInstance(IBridgeRegistrar bridgeRegistrar, Models.Instance metadata)
196  {
197  // Create the ioManager for the instance
198  var instanceIoManager = new ResolvingIOManager(ioManager, metadata.Path);
199 
200  // various other ioManagers
201  var repoIoManager = new ResolvingIOManager(instanceIoManager, "Repository");
202  var byondIOManager = new ResolvingIOManager(instanceIoManager, "Byond");
203  var gameIoManager = new ResolvingIOManager(instanceIoManager, "Game");
204  var diagnosticsIOManager = new ResolvingIOManager(instanceIoManager, "Diagnostics");
205  var configurationIoManager = new ResolvingIOManager(instanceIoManager, "Configuration");
206 
207  var configuration = new StaticFiles.Configuration(configurationIoManager, synchronousIOManager, symlinkFactory, processExecutor, postWriteHandler, platformIdentifier, loggerFactory.CreateLogger<StaticFiles.Configuration>());
208  var eventConsumer = new EventConsumer(configuration);
209  var repoManager = new RepositoryManager(
210  repositoryFactory,
211  repositoryCommands,
212  repoIoManager,
213  eventConsumer,
214  loggerFactory.CreateLogger<Repository.Repository>(),
215  loggerFactory.CreateLogger<RepositoryManager>());
216  try
217  {
218  var byond = new ByondManager(byondIOManager, byondInstaller, eventConsumer, loggerFactory.CreateLogger<ByondManager>());
219 
220  var commandFactory = new CommandFactory(assemblyInformationProvider, byond, repoManager, databaseContextFactory, metadata);
221 
222  var chatManager = chatFactory.CreateChatManager(instanceIoManager, commandFactory, metadata.ChatSettings);
223  try
224  {
225  var sessionControllerFactory = new SessionControllerFactory(
226  processExecutor,
227  byond,
228  topicClientFactory,
229  cryptographySuite,
230  assemblyInformationProvider,
231  gameIoManager,
232  chatManager,
233  networkPromptReaper,
234  platformIdentifier,
235  bridgeRegistrar,
236  serverPortProvider,
237  loggerFactory,
238  loggerFactory.CreateLogger<SessionControllerFactory>(),
239  metadata.CloneMetadata());
240 
241  var dmbFactory = new DmbFactory(databaseContextFactory, gameIoManager, loggerFactory.CreateLogger<DmbFactory>(), metadata.CloneMetadata());
242  try
243  {
244  var reattachInfoHandler = new SessionPersistor(
245  databaseContextFactory,
246  dmbFactory,
247  processExecutor,
248  loggerFactory.CreateLogger<SessionPersistor>(),
249  metadata.CloneMetadata());
250  var watchdog = watchdogFactory.CreateWatchdog(
251  chatManager,
252  dmbFactory,
253  reattachInfoHandler,
254  sessionControllerFactory,
255  gameIoManager,
256  diagnosticsIOManager,
257  eventConsumer,
258  metadata.CloneMetadata(),
259  metadata.DreamDaemonSettings);
260  eventConsumer.SetWatchdog(watchdog);
261  commandFactory.SetWatchdog(watchdog);
262  try
263  {
264  Instance instance = null;
265  var dreamMaker = new DreamMaker(
266  byond,
267  gameIoManager,
268  configuration,
269  sessionControllerFactory,
270  eventConsumer,
271  chatManager,
272  processExecutor,
273  gitHubClientFactory,
274  dmbFactory,
275  repoManager,
276  loggerFactory.CreateLogger<DreamMaker>(),
277  metadata.CloneMetadata());
278 
279  instance = new Instance(
280  metadata.CloneMetadata(),
281  repoManager,
282  byond,
283  dreamMaker,
284  watchdog,
285  chatManager,
286  configuration,
287  databaseContextFactory,
288  dmbFactory,
289  jobManager,
290  eventConsumer,
291  loggerFactory.CreateLogger<Instance>());
292 
293  return instance;
294  }
295  catch
296  {
297  watchdog.Dispose();
298  throw;
299  }
300  }
301  catch
302  {
303  dmbFactory.Dispose();
304  throw;
305  }
306  }
307  catch
308  {
309  chatManager.Dispose();
310  throw;
311  }
312  }
313  catch
314  {
315  repoManager.Dispose();
316  throw;
317  }
318  }
319 #pragma warning restore CA1506
320 
322  public Task StartAsync(CancellationToken cancellationToken)
323  {
324  CheckSystemCompatibility();
325  return byondInstaller.CleanCache(cancellationToken);
326  }
327 
329  public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
330 
334  private void CheckSystemCompatibility() => repositoryFactory.CreateInMemory().Dispose();
335  }
336 }
readonly ISynchronousIOManager synchronousIOManager
The ISynchronousIOManager for the InstanceFactory
readonly IByondInstaller byondInstaller
The IByondInstaller for the InstanceFactory
Task StartAsync(CancellationToken cancellationToken)
Handles changing file modes/permissions after writing
An IIOManager that resolve relative paths from another IIOManager to a subdirectory of that ...
readonly IServerPortProvider serverPortProvider
The IServerPortProvider for the InstanceFactory.
Factory for scoping usage of IDatabaseContexts. Meant for use by Components
For low level interactions with a LibGit2Sharp.IRepository.
readonly ILoggerFactory loggerFactory
The ILoggerFactory for the InstanceFactory
readonly ICryptographySuite cryptographySuite
The ICryptographySuite for the InstanceFactory
Contains various cryptographic functions
readonly IProcessExecutor processExecutor
The IProcessExecutor for the InstanceFactory
readonly INetworkPromptReaper networkPromptReaper
The INetworkPromptReaper for the InstanceFactory
readonly IPostWriteHandler postWriteHandler
The IPostWriteHandler for the InstanceFactory
For downloading and installing BYOND extractions for a given system
For accessing the disk in a synchronous manner
readonly IDatabaseContextFactory databaseContextFactory
The IDatabaseContextFactory for the InstanceFactory
readonly IWatchdogFactory watchdogFactory
The IWatchdogFactory for the InstanceFactory
readonly ILibGit2RepositoryFactory repositoryFactory
The ILibGit2RepositoryFactory for the InstanceFactory.
For interacting with the instance services
Definition: IInstance.cs:16
readonly IIOManager ioManager
The IIOManager for the InstanceFactory
readonly IGitHubClientFactory gitHubClientFactory
The IGitHubClientFactory for the InstanceFactory
readonly IPlatformIdentifier platformIdentifier
The IPlatformIdentifier for the InstanceFactory
readonly ISymlinkFactory symlinkFactory
The ISymlinkFactory for the InstanceFactory
readonly IJobManager jobManager
The IJobManager for the InstanceFactory
readonly IChatManagerFactory chatFactory
The IChatManagerFactory for the InstanceFactory
readonly ITopicClientFactory topicClientFactory
The ITopicClientFactory for the InstanceFactory
Manages the runtime of Jobs
Definition: IJobManager.cs:13
IInstance CreateInstance(IBridgeRegistrar bridgeRegistrar, Models.Instance metadata)
Create an IInstance
On Windows, DreamDaemon will show an unskippable prompt when using /world/proc/OpenPort(). This looks out for those prompts and immediately clicks "Yes" if the owning process has registered for it
readonly ILibGit2Commands repositoryCommands
The ILibGit2Commands for the InstanceFactory.
Interface for using filesystems
Definition: IIOManager.cs:11
InstanceFactory(IIOManager ioManager, IDatabaseContextFactory databaseContextFactory, IAssemblyInformationProvider assemblyInformationProvider, ILoggerFactory loggerFactory, ITopicClientFactory topicClientFactory, ICryptographySuite cryptographySuite, ISynchronousIOManager synchronousIOManager, ISymlinkFactory symlinkFactory, IByondInstaller byondInstaller, IChatManagerFactory chatFactory, IProcessExecutor processExecutor, IPostWriteHandler postWriteHandler, IWatchdogFactory watchdogFactory, IJobManager jobManager, INetworkPromptReaper networkPromptReaper, IGitHubClientFactory gitHubClientFactory, IPlatformIdentifier platformIdentifier, ILibGit2RepositoryFactory repositoryFactory, ILibGit2Commands repositoryCommands, IServerPortProvider serverPortProvider)
Construct an InstanceFactory
Provides access to the server&#39;s HttpApiPort.
Repository(LibGit2Sharp.IRepository libGitRepo, ILibGit2Commands commands, IIOManager ioMananger, IEventConsumer eventConsumer, ICredentialsProvider credentialsProvider, ILogger< Repository > logger, Action onDispose)
Construct a Repository
Definition: Repository.cs:115
readonly IAssemblyInformationProvider assemblyInformationProvider
The IAssemblyInformationProvider for the InstanceFactory
For identifying the current platform