tgstation-server 6.1.2
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 [RequestOptions(FieldPresence.Required)]
19 public EngineType? Engine { get; set; }
20
24 [ResponseOptions]
25 public Version? Version { get; set; }
26
30 [ResponseOptions]
31 [StringLength(Limits.MaximumCommitShaLength, MinimumLength = Limits.MaximumCommitShaLength)]
32 public string? SourceSHA { get; set; }
33
37 [ResponseOptions]
38 public int? CustomIteration { get; set; }
39
46 public static bool TryParse(string input, out EngineVersion? engineVersion)
47 {
48 if (input == null)
49 throw new ArgumentNullException(nameof(input));
50
51 var splits = input.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
52 engineVersion = null;
53
54 if (splits.Length > 3)
55 return false;
56
57 EngineType engine;
58 var hasPrefix = splits.Length > 1;
59 if (hasPrefix)
60 {
61 if (!Enum.TryParse(splits[0], out engine))
62 return false;
63 }
64 else
65 engine = EngineType.Byond;
66
67 Version? version;
68 string? sha;
69 int? customRev = null;
70 if (engine == EngineType.Byond)
71 {
72 if (!Version.TryParse(splits.Last(), out version))
73 return false;
74
75 if (version.Build > 0)
76 {
77 customRev = version.Build;
78 version = new Version(version.Major, version.Minor);
79 }
80
81 sha = null;
82 }
83 else
84 {
85 Debug.Assert(engine == EngineType.OpenDream, "This does not support whatever ungodly new engine you've added");
86
87 var shaIndex = hasPrefix ? 1 : 0;
88 sha = splits[shaIndex];
89 if (sha.Length != Limits.MaximumCommitShaLength)
90 return false;
91
92 version = null;
93
94 if (splits.Length - 1 > shaIndex)
95 {
96 if (!Int32.TryParse(splits.Last(), out var customRevResult))
97 return false;
98
99 customRev = customRevResult;
100 }
101 }
102
103 engineVersion = new EngineVersion
104 {
105 Engine = engine,
106 Version = version,
107 SourceSHA = sha,
108 CustomIteration = customRev,
109 };
110 return true;
111 }
112
119 public static EngineVersion Parse(string input)
120 {
121 if (input == null)
122 throw new ArgumentNullException(nameof(input));
123
124 if (TryParse(input, out var engineVersion))
125 return engineVersion!;
126
127 throw new InvalidOperationException($"Invalid engine version: {input}");
128 }
129
134 {
135 }
136
142 {
143 if (other == null)
144 throw new ArgumentNullException(nameof(other));
145
146 Version = other.Version;
147 Engine = other.Engine;
148 SourceSHA = other.SourceSHA;
150 }
151
153 public bool Equals(EngineVersion other)
154 {
155 // https://github.com/dotnet/roslyn-analyzers/issues/2875
156#pragma warning disable CA1062 // Validate arguments of public methods
157 return other!.Version?.Semver() == Version?.Semver()
158 && other.Engine == Engine
159 && (other.SourceSHA == SourceSHA
160 || (other.SourceSHA != null
161 && SourceSHA != null
162 && other.SourceSHA.Equals(SourceSHA, StringComparison.OrdinalIgnoreCase)))
163 && other.CustomIteration == CustomIteration;
164#pragma warning restore CA1062 // Validate arguments of public methods
165 }
166
168 public override bool Equals(object obj)
169 => obj is EngineVersion other && Equals(other);
170
172 public override string ToString()
173 {
174 var isByond = Engine == EngineType.Byond;
175
176 // BYOND encodes differently for backwards compatibility
177 var enginePrefix = !isByond
178 ? $"{Engine}-"
179 : String.Empty;
180 var displayedVersion = isByond
181 ? (CustomIteration.HasValue
182 ? new Version(Version!.Major, Version.Minor, CustomIteration.Value)
183 : Version!).ToString()
184 : SourceSHA;
185 var displayedCustomIteration = !isByond && CustomIteration.HasValue
186 ? $"-{CustomIteration}"
187 : String.Empty;
188 return $"{enginePrefix}{displayedVersion}{displayedCustomIteration}";
189 }
190
192 public override int GetHashCode() => ToString().GetHashCode();
193 }
194}
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.
EngineType? Engine
The EngineType.
int? CustomIteration
The revision of the custom build.
Sanity limits to prevent users from overloading.
Definition: Limits.cs:9
const int MaximumCommitShaLength
Length limit for git commit SHAs.
Definition: Limits.cs:23
FieldPresence
Indicates whether a request field is Required or Ignored.
Definition: FieldPresence.cs:7
EngineType
The type of engine the codebase is using.
Definition: EngineType.cs:7