Skip to content

Commit ab94855

Browse files
committed
Allow GameVersion to parse < 3-number format
1 parent 8f80354 commit ab94855

File tree

1 file changed

+48
-44
lines changed
  • CollapseLauncher/Classes/Interfaces/Class

1 file changed

+48
-44
lines changed

CollapseLauncher/Classes/Interfaces/Class/Structs.cs

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Hi3Helper;
33
using Hi3Helper.Data;
44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.IO;
67
// ReSharper disable CheckNamespace
78
// ReSharper disable InconsistentNaming
@@ -51,9 +52,9 @@ internal class TotalPerFileStatus
5152
public bool IsIncludePerFileIndicator { get; set; }
5253
}
5354

55+
#nullable enable
5456
internal struct GameVendorProp
5557
{
56-
#nullable enable
5758
public GameVendorProp(string gamePath, string execName, GameVendorType fallbackVendorType)
5859
{
5960
// Set the fallback value of the Vendor Type first (in case the game isn't yet installed)
@@ -89,7 +90,6 @@ public GameVendorProp(string gamePath, string execName, GameVendorType fallbackV
8990

9091
public GameVendorType? VendorType { get; set; }
9192
public string? GameName { get; set; }
92-
#nullable disable
9393
}
9494

9595
public static class GameVersionExt
@@ -109,70 +109,70 @@ public static bool Equals(this GameVersion? fromVersion, GameVersion? toVersion)
109109

110110
public readonly record struct GameVersion
111111
{
112-
public GameVersion(int major, int minor, int build, int revision = 0)
113-
{
114-
Major = major;
115-
Minor = minor;
116-
Build = build;
117-
Revision = revision;
118-
}
119-
120-
public GameVersion(ReadOnlySpan<int> ver)
112+
public GameVersion(params ReadOnlySpan<int> ver)
121113
{
122-
if (ver.Length is not (3 or 4))
114+
if (ver.Length == 0)
123115
{
124-
throw new ArgumentException("Version array entered should have length of 3 or 4!");
116+
throw new ArgumentException("Version array entered should have length at least 1 or max. 4!");
125117
}
126118

127-
Major = ver[0];
128-
Minor = ver[1];
129-
Build = ver[2];
130-
if (ver.Length == 4)
131-
{
132-
Revision = ver[3];
133-
}
119+
Major = ver[0];
120+
Minor = ver.Length >= 2 ? ver[1] : 0;
121+
Build = ver.Length >= 3 ? ver[2] : 0;
122+
Revision = ver.Length >= 4 ? ver[3] : 0;
134123
}
135124

136125
public GameVersion(Version version)
137126
{
138-
Major = version.Major;
139-
Minor = version.Minor;
140-
Build = version.Build;
127+
Major = version.Major;
128+
Minor = version.Minor;
129+
Build = version.Build;
130+
Revision = version.Revision;
141131
}
142132

143-
public GameVersion(string version)
133+
public GameVersion(string? version)
144134
{
145-
string[] ver = version.Split('.', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
146-
if (ver.Length is not (3 or 4))
147-
{
148-
throw new ArgumentException($"Version in the config.ini should be in \"x.x.x\" or \"x.x.x.x\" format! (current value: \"{version}\")");
149-
}
150-
151-
if (!int.TryParse(ver[0], out Major)) throw new ArgumentException($"Major version is not a number! (current value: {ver[0]}");
152-
if (!int.TryParse(ver[1], out Minor)) throw new ArgumentException($"Minor version is not a number! (current value: {ver[1]}");
153-
if (!int.TryParse(ver[2], out Build)) throw new ArgumentException($"Build version is not a number! (current value: {ver[2]}");
154-
if (ver.Length != 4)
135+
if (!TryParse(version, out GameVersion? versionOut) || !versionOut.HasValue)
155136
{
156-
return;
137+
throw new ArgumentException($"Version in the config.ini should be either in \"x\", \"x.x\", \"x.x.x\" or \"x.x.x.x\" format or all the values aren't numbers! (current value: \"{version}\")");
157138
}
158139

159-
if (!int.TryParse(ver[3], out Revision)) throw new ArgumentException($"Revision version is not a number! (current value: {ver[3]}");
140+
Major = versionOut.Value.Major;
141+
Minor = versionOut.Value.Minor;
142+
Build = versionOut.Value.Build;
143+
Revision = versionOut.Value.Revision;
160144
}
161145

162-
public static bool TryParse(string version, out GameVersion? result)
146+
public static bool TryParse(string? version, [NotNullWhen(true)] out GameVersion? result)
163147
{
164148
result = null;
165-
Span<Range> ranges = stackalloc Range[8];
149+
if (string.IsNullOrEmpty(version))
150+
{
151+
return false;
152+
}
153+
154+
Span<Range> ranges = stackalloc Range[8];
166155
ReadOnlySpan<char> versionSpan = version.AsSpan();
167-
int splitRanges = versionSpan.Split(ranges, '.', StringSplitOptions.TrimEntries);
156+
int splitRanges = versionSpan.Split(ranges, '.', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
168157

169-
if (splitRanges is not (3 or 4)) return false;
158+
if (splitRanges == 0)
159+
{
160+
if (!int.TryParse(versionSpan, null, out int majorOnly))
161+
{
162+
return false;
163+
}
164+
165+
result = new GameVersion(majorOnly);
166+
return true;
167+
}
170168

171169
Span<int> versionSplits = stackalloc int[4];
172170
for (int i = 0; i < splitRanges; i++)
173171
{
174172
if (!int.TryParse(versionSpan[ranges[i]], null, out int versionParsed))
173+
{
175174
return false;
175+
}
176176

177177
versionSplits[i] = versionParsed;
178178
}
@@ -189,8 +189,11 @@ public bool IsMatch(string versionToCompare)
189189

190190
public bool IsMatch(GameVersion? versionToCompare)
191191
{
192-
if (versionToCompare == null) return false;
193-
return Major == versionToCompare.Value.Major && Minor == versionToCompare.Value.Minor && Build == versionToCompare.Value.Build && Revision == versionToCompare.Value.Revision;
192+
if (!versionToCompare.HasValue) return false;
193+
return Major == versionToCompare.Value.Major &&
194+
Minor == versionToCompare.Value.Minor &&
195+
Build == versionToCompare.Value.Build &&
196+
Revision == versionToCompare.Value.Revision;
194197
}
195198

196199
public GameVersion GetIncrementedVersion()
@@ -201,13 +204,13 @@ public GameVersion GetIncrementedVersion()
201204
nextMinor++;
202205
if (nextMinor < 10)
203206
{
204-
return new GameVersion([nextMajor, nextMinor, Build, Revision]);
207+
return new GameVersion(nextMajor, nextMinor, Build, Revision);
205208
}
206209

207210
nextMinor = 0;
208211
nextMajor++;
209212

210-
return new GameVersion([nextMajor, nextMinor, Build, Revision]);
213+
return new GameVersion(nextMajor, nextMinor, Build, Revision);
211214
}
212215

213216
public Version ToVersion() => new(Major, Minor, Build, Revision);
@@ -222,6 +225,7 @@ public GameVersion GetIncrementedVersion()
222225
public readonly int Build;
223226
public readonly int Revision;
224227
}
228+
#nullable restore
225229

226230
public interface IAssetProperty
227231
{

0 commit comments

Comments
 (0)