tgstation-server 6.14.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
30 [ResponseOptions]
31 public Version? Version { get; set; }
32
37 [ResponseOptions]
38 [StringLength(Limits.MaximumCommitShaLength, MinimumLength = Limits.MaximumCommitShaLength)]
39 public string? SourceSHA { get; set; }
40
44 [ResponseOptions]
45 public int? CustomIteration { get; set; }
46
53 public static bool TryParse(string input, out EngineVersion? engineVersion)
54 {
55 if (input == null)
56 throw new ArgumentNullException(nameof(input));
57
58 var splits = input.Split(DashChar, StringSplitOptions.RemoveEmptyEntries);
59 engineVersion = null;
60
61 if (splits.Length > 3)
62 return false;
63
64 EngineType engine;
65 var hasPrefix = splits.Length > 1;
66 if (hasPrefix)
67 {
68 if (!Enum.TryParse(splits[0], out engine))
69 return false;
70 }
71 else
72 engine = EngineType.Byond;
73
74 Version? version;
75 string? sha;
76 int? customRev = null;
77 if (engine == EngineType.Byond)
78 {
79 if (!Version.TryParse(splits.Last(), out version))
80 return false;
81
82 if (version.Build > 0)
83 {
84 customRev = version.Build;
85 version = new Version(version.Major, version.Minor);
86 }
87
88 sha = null;
89 }
90 else
91 {
92 Debug.Assert(engine == EngineType.OpenDream, "This does not support whatever ungodly new engine you've added");
93
94 var shaIndex = hasPrefix ? 1 : 0;
95 sha = splits[shaIndex];
96 if (sha.Length != Limits.MaximumCommitShaLength)
97 return false;
98
99 version = null;
100
101 if (splits.Length - 1 > shaIndex)
102 {
103 if (!Int32.TryParse(splits.Last(), out var customRevResult))
104 return false;
105
106 customRev = customRevResult;
107 }
108 }
109
110 engineVersion = new EngineVersion
111 {
112 Engine = engine,
113 Version = version,
114 SourceSHA = sha,
115 CustomIteration = customRev,
116 };
117 return true;
118 }
119
126 public static EngineVersion Parse(string input)
127 {
128 if (input == null)
129 throw new ArgumentNullException(nameof(input));
130
131 if (TryParse(input, out var engineVersion))
132 return engineVersion!;
133
134 throw new InvalidOperationException($"Invalid engine version: {input}");
135 }
136
141 {
142 }
143
149 {
150 if (other == null)
151 throw new ArgumentNullException(nameof(other));
152
153 Version = other.Version;
154 Engine = other.Engine;
155 SourceSHA = other.SourceSHA;
157 }
158
160 public bool Equals(EngineVersion other)
161 {
162 // https://github.com/dotnet/roslyn-analyzers/issues/2875
163#pragma warning disable CA1062 // Validate arguments of public methods
164 return other!.Version?.Semver() == Version?.Semver()
165 && other.Engine == Engine
166 && (other.SourceSHA == SourceSHA
167 || (other.SourceSHA != null
168 && SourceSHA != null
169 && other.SourceSHA.Equals(SourceSHA, StringComparison.OrdinalIgnoreCase)))
170 && other.CustomIteration == CustomIteration;
171#pragma warning restore CA1062 // Validate arguments of public methods
172 }
173
175 public override bool Equals(object obj)
176 => obj is EngineVersion other && Equals(other);
177
179 public override string ToString()
180 {
181 var isByond = Engine == EngineType.Byond;
182
183 // BYOND encodes differently for backwards compatibility
184 var enginePrefix = !isByond
185 ? $"{Engine}-"
186 : String.Empty;
187 var displayedVersion = isByond
188 ? (CustomIteration.HasValue
189 ? new Version(Version!.Major, Version.Minor, CustomIteration.Value)
190 : Version!).ToString()
191 : SourceSHA;
192 var displayedCustomIteration = !isByond && CustomIteration.HasValue
193 ? $"-{CustomIteration}"
194 : String.Empty;
195 return $"{enginePrefix}{displayedVersion}{displayedCustomIteration}";
196 }
197
199 public override int GetHashCode() => ToString().GetHashCode();
200 }
201}
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