tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
Program.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text;
6using System.Threading;
7using System.Threading.Tasks;
8
13
15{
19 sealed class Program
20 {
24 internal static Version HostWatchdogVersion => Version.Parse(MasterVersionsAttribute.Instance.RawHostWatchdogVersion);
25
29 internal IServerFactory ServerFactory { get; set; }
30
38
44 public static async Task<int> Main(string[] args)
45 {
46 // first arg is 100% always the update path, starting it otherwise is solely for debugging purposes
47 var updatePath = TopLevelArgsParse(ref args);
48
49 var program = new Program();
50 return (int)await program.Main(args, updatePath);
51 }
52
58 static string? TopLevelArgsParse(ref string[] args)
59 {
60 if (args.Length == 0)
61 return null;
62
63 var potentialUpdatePath = args[0];
64 if (potentialUpdatePath.Equals("cli", StringComparison.OrdinalIgnoreCase))
65 return null;
66
67 var listArgs = new List<string>(args);
68 listArgs.RemoveAt(0);
69
70 // second arg should be host watchdog version
71 if (listArgs.Count > 0)
72 {
73 var expectedHostWatchdogVersion = HostWatchdogVersion;
74 if (Version.TryParse(listArgs.First(), out var actualHostWatchdogVersion)
75 && actualHostWatchdogVersion.Major != expectedHostWatchdogVersion.Major)
76 throw new InvalidOperationException(
77 $"Incompatible host watchdog version ({actualHostWatchdogVersion}) for server ({expectedHostWatchdogVersion})! A major update was released and a full restart will be required. Please manually offline your servers!");
78 }
79
80 if (listArgs.Remove("--attach-debugger"))
81 Debugger.Launch();
82
83 args = [.. listArgs];
84 return potentialUpdatePath;
85 }
86
93 internal async ValueTask<HostExitCode> Main(string[] args, string? updatePath)
94 {
95 try
96 {
97 using var shutdownNotifier = new ProgramShutdownTokenSource();
98 var cancellationToken = shutdownNotifier.Token;
99 IServer? server;
100 try
101 {
102 server = await ServerFactory.CreateServer(
103 args,
104 updatePath,
105 cancellationToken);
106 }
107 catch (OperationCanceledException)
108 {
109 // Console cancelled
110 return HostExitCode.CompleteExecution;
111 }
112
113 if (server == null)
114 return HostExitCode.CompleteExecution;
115
116 await server.Run(cancellationToken);
117
118 return server.RestartRequested
119 ? HostExitCode.RestartRequested
120 : HostExitCode.CompleteExecution;
121 }
122 catch (Exception e)
123 {
124 if (updatePath != null)
125 {
126 // DCT: None available, operation should always run
127 await ServerFactory.IOManager.WriteAllBytes(updatePath, Encoding.UTF8.GetBytes(e.ToString()), CancellationToken.None);
128 return HostExitCode.Error;
129 }
130
131 // If you hit an exception debug break on this line it caused the application to crash
132 throw;
133 }
134 }
135 }
136}
Sets up dependency injection.
static IServerFactory CreateDefaultServerFactory()
Create the default IServerFactory.
Entrypoint for the Process.
Definition Program.cs:20
Program()
Initializes a new instance of the Program class.
Definition Program.cs:34
static async Task< int > Main(string[] args)
Entrypoint for the Program.
Definition Program.cs:44
static ? string TopLevelArgsParse(ref string[] args)
Parse Program top level args .
Definition Program.cs:58
Attribute for bringing in the master versions list from MSBuild that aren't embedded into assemblies ...
string RawHostWatchdogVersion
The Version string of the host watchdog that was built alongside this TGS version.
static MasterVersionsAttribute Instance
Return the Assembly's instance of the MasterVersionsAttribute.
Implementation of IServerFactory.
IIOManager IOManager
The IIOManager for the IServerFactory.
async ValueTask< IServer?> CreateServer(string[] args, string? updatePath, CancellationToken cancellationToken)
Create a IServer.A ValueTask<TResult> resulting in a new IServer if it should be run,...
Contains a CancellationToken that triggers when the operating system requests the program shuts down.
Represents the host.
Definition IServer.cs:10
ValueTask Run(CancellationToken cancellationToken)
Runs the IServer.
HostExitCode
Represents the exit code of the Host program.