tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
EngineVersion.cs
Go to the documentation of this file.
1using System;
2using System.ComponentModel.DataAnnotations;
3using System.Diagnostics;
4using System.Linq;
5
7
9{
13 public sealed class EngineVersion : IEquatable<EngineVersion>
14 {
18 static readonly char[] DashChar = ['-'];
19
23 [RequestOptions(FieldPresence.Required)]
24 public EngineType? Engine { get; set; }
25
29 [ResponseOptions]
30 public Version? Version { get; set; }
31
35 [ResponseOptions]
36 [StringLength(Limits.MaximumCommitShaLength, MinimumLength = Limits.MaximumCommitShaLength)]
37 public string? SourceSHA { get; set; }
38
42 [ResponseOptions]
43 public int? CustomIteration { get; set; }
44
51 public static bool TryParse(string input, out EngineVersion? engineVersion)
52 {
53 if (input == null)
54 throw new ArgumentNullException(nameof(input));
55
56 var splits = input.Split(DashChar, StringSplitOptions.RemoveEmptyEntries);
57 engineVersion = null;
58
59 if (splits.Length > 3)
60 return false;
61
62 EngineType engine;
63 var hasPrefix = splits.Length > 1;
64 if (hasPrefix)
65 {
66 if (!Enum.TryParse(splits[0], out engine))
67 return false;
68 }
69 else
70 engine = EngineType.Byond;
71
72 Version? version;
73 string? sha;
74 int? customRev = null;
75 if (engine == EngineType.Byond)
76 {
77 if (!Version.TryParse(splits.Last(), out version))
78 return false;
79
80 if (version.Build > 0)
81 {
82 customRev = version.Build;
83 version = new Version(version.Major, version.Minor);
84 }
85
86 sha = null;
87 }
88 else
89 {
90 Debug.Assert(engine == EngineType.OpenDream, "This does not support whatever ungodly new engine you've added");
91
92 var shaIndex = hasPrefix ? 1 : 0;
93 sha = splits[shaIndex];
94 if (sha.Length != Limits.MaximumCommitShaLength)
95 return false;
96
97 version = null;
98
99 if (splits.Length - 1 > shaIndex)
100 {
101 if (!Int32.TryParse(splits.Last(), out var customRevResult))
102 return false;
103
104 customRev = customRevResult;
105 }
106 }
107
108 engineVersion = new EngineVersion
109 {
110 Engine = engine,
111 Version = version,
112 SourceSHA = sha,
113 CustomIteration = customRev,
114 };
115 return true;
116 }
117
124 public static EngineVersion Parse(string input)
125 {
126 if (input == null)
127 throw new ArgumentNullException(nameof(input));
128
129 if (TryParse(input, out var engineVersion))
130 return engineVersion!;
131
132 throw new InvalidOperationException($"Invalid engine version: {input}");
133 }
134
139 {
140 }
141
147 {
148 if (other == null)
149 throw new ArgumentNullException(nameof(other));
150
151 Version = other.Version;
152 Engine = other.Engine;
153 SourceSHA = other.SourceSHA;
155 }
156
158 public bool Equals(EngineVersion other)
159 {
160 // https://github.com/dotnet/roslyn-analyzers/issues/2875
161#pragma warning disable CA1062 // Validate arguments of public methods
162 return other!.Version?.Semver() == Version?.Semver()
163 && other.Engine == Engine
164 && (other.SourceSHA == SourceSHA
165 || (other.SourceSHA != null
166 && SourceSHA != null
167 && other.SourceSHA.Equals(SourceSHA, StringComparison.OrdinalIgnoreCase)))
168 && other.CustomIteration == CustomIteration;
169#pragma warning restore CA1062 // Validate arguments of public methods
170 }
171
173 public override bool Equals(object obj)
174 => obj is EngineVersion other && Equals(other);
175
177 public override string ToString()
178 {
179 var isByond = Engine == EngineType.Byond;
180
181 // BYOND encodes differently for backwards compatibility
182 var enginePrefix = !isByond
183 ? $"{Engine}-"
184 : String.Empty;
185 var displayedVersion = isByond
186 ? (CustomIteration.HasValue
187 ? new Version(Version!.Major, Version.Minor, CustomIteration.Value)
188 : Version!).ToString()
189 : SourceSHA;
190 var displayedCustomIteration = !isByond && CustomIteration.HasValue
191 ? $"-{CustomIteration}"
192 : String.Empty;
193 return $"{enginePrefix}{displayedVersion}{displayedCustomIteration}";
194 }
195
197 public override int GetHashCode() => ToString().GetHashCode();
198 }
199}
Information about an engine installation.
Version? Version
The System.Version of the engine. Currently only valid when Engine is EngineType.Byond.
static bool TryParse(string input, out EngineVersion? engineVersion)
Attempts to parse a stringified EngineVersion.
static EngineVersion Parse(string input)
Parses a stringified EngineVersion.
override bool Equals(object obj)
EngineVersion()
Initializes a new instance of the EngineVersion class.
EngineVersion(EngineVersion other)
Initializes a new instance of the EngineVersion class.
string? SourceSHA
The git commit SHA of the engine. Currently only valid when Engine is EngineType.OpenDream.
int? CustomIteration
The revision of the custom build.
static readonly char[] DashChar
An array of a single '-' char.
Sanity limits to prevent users from overloading.
Definition Limits.cs:9
const int MaximumCommitShaLength
Length limit for git commit SHAs.
Definition Limits.cs:28
FieldPresence
Indicates whether a request field is Required or Ignored.
EngineType
The type of engine the codebase is using.
Definition EngineType.cs:7