Skip to content

Commit d1929a3

Browse files
committed
Add support for edge cases parsing
- Allow executing rest of the logic for links that do not parse - Respect default other than app/package, and do not enforce id for name and regex
1 parent 14aa31a commit d1929a3

2 files changed

Lines changed: 34 additions & 33 deletions

File tree

ArchiSteamFarm.Tests/SteamUtilities.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ internal void TryParseGameIdentifierReturnsExpectedId(string input, EGameIdentif
5555
[DataRow("unknown/123", EGameIdentifier.Application)]
5656
[DataRow("https://store.steampowered.com/bundle/123", EGameIdentifier.Application)]
5757
[DataRow("https://example.com/app/730", EGameIdentifier.Application)]
58+
[DataRow("regex/pattern", EGameIdentifier.Application)]
59+
[DataRow("name/Half-Life", EGameIdentifier.Application)]
5860
[TestMethod]
5961
internal void TryParseGameIdentifierReturnsFalseForInvalidInput(string input, EGameIdentifier defaultType) {
6062
bool result = TryParseGameIdentifier(input, defaultType, out EGameIdentifier? type, out uint id);
@@ -68,6 +70,7 @@ internal void TryParseGameIdentifierReturnsFalseForInvalidInput(string input, EG
6870
[DataRow("r/test.*", EGameIdentifier.Application, EGameIdentifier.Regex, "test.*")]
6971
[DataRow("name/Half-Life", EGameIdentifier.Application, EGameIdentifier.Name, "Half-Life")]
7072
[DataRow("n/Portal", EGameIdentifier.Application, EGameIdentifier.Name, "Portal")]
73+
[DataRow("http:CS2", EGameIdentifier.Name, EGameIdentifier.Name, "http:CS2")]
7174
[TestMethod]
7275
internal void TryParseGameIdentifierStringReturnsExpectedValue(string input, EGameIdentifier defaultType, EGameIdentifier expectedType, string expectedValue) {
7376
bool result = TryParseGameIdentifier(input, defaultType, out EGameIdentifier? type, out string? value);
@@ -76,16 +79,5 @@ internal void TryParseGameIdentifierStringReturnsExpectedValue(string input, EGa
7679
Assert.AreEqual(expectedType, type);
7780
Assert.AreEqual(expectedValue, value);
7881
}
79-
80-
[DataRow("regex/pattern", EGameIdentifier.Application)]
81-
[DataRow("name/Half-Life", EGameIdentifier.Application)]
82-
[TestMethod]
83-
internal void TryParseGameIdentifierUintReturnsFalseForNonNumericTypes(string input, EGameIdentifier defaultType) {
84-
bool result = TryParseGameIdentifier(input, defaultType, out EGameIdentifier? type, out uint id);
85-
86-
Assert.IsFalse(result);
87-
Assert.IsNull(type);
88-
Assert.AreEqual(0U, id);
89-
}
9082
}
9183
#pragma warning restore CA1812 // False positive, the class is used during MSTest

ArchiSteamFarm/Steam/Integration/SteamUtilities.cs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -153,29 +153,22 @@ internal static bool TryParseGameIdentifier(string input, EGameIdentifier defaul
153153
throw new InvalidEnumArgumentException(nameof(defaultType), (int) defaultType, typeof(EGameIdentifier));
154154
}
155155

156-
if (input.StartsWith("http", StringComparison.OrdinalIgnoreCase)) {
157-
if (Uri.TryCreate(input, UriKind.Absolute, out Uri? uri) && uri.Host.Equals(ArchiWebHandler.SteamStoreURL.Host, StringComparison.OrdinalIgnoreCase)) {
158-
string[] segments = uri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries);
156+
if (input.StartsWith("http", StringComparison.OrdinalIgnoreCase) && Uri.TryCreate(input, UriKind.Absolute, out Uri? uri) && uri.Host.Equals(ArchiWebHandler.SteamStoreURL.Host, StringComparison.OrdinalIgnoreCase)) {
157+
string[] segments = uri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries);
159158

160-
if (segments.Length >= 2) {
161-
type = segments[0].ToUpperInvariant() switch {
162-
"APP" => EGameIdentifier.Application,
163-
"SUB" => EGameIdentifier.Package,
164-
_ => null
165-
};
159+
if (segments.Length >= 2) {
160+
type = segments[0].ToUpperInvariant() switch {
161+
"APP" => EGameIdentifier.Application,
162+
"SUB" => EGameIdentifier.Package,
163+
_ => null
164+
};
166165

167-
if ((type != null) && uint.TryParse(segments[1], out uint pathNumericValue) && (pathNumericValue > 0)) {
168-
value = segments[1];
166+
if ((type != null) && uint.TryParse(segments[1], out uint pathNumericValue) && (pathNumericValue > 0)) {
167+
value = segments[1];
169168

170-
return true;
171-
}
169+
return true;
172170
}
173171
}
174-
175-
type = null;
176-
value = null;
177-
178-
return false;
179172
}
180173

181174
int slashIndex = input.IndexOf('/', StringComparison.Ordinal);
@@ -212,11 +205,27 @@ internal static bool TryParseGameIdentifier(string input, EGameIdentifier defaul
212205
return false;
213206
}
214207

215-
if (uint.TryParse(input, out uint numericValue) && (numericValue > 0)) {
216-
type = defaultType;
217-
value = input;
208+
switch (defaultType) {
209+
case EGameIdentifier.Application:
210+
case EGameIdentifier.Package:
211+
if (uint.TryParse(input, out uint numericValue) && (numericValue > 0)) {
212+
type = defaultType;
213+
value = input;
218214

219-
return true;
215+
return true;
216+
}
217+
218+
break;
219+
case EGameIdentifier.Name:
220+
type = EGameIdentifier.Name;
221+
value = input;
222+
223+
return true;
224+
case EGameIdentifier.Regex:
225+
type = EGameIdentifier.Regex;
226+
value = input;
227+
228+
return true;
220229
}
221230

222231
type = null;

0 commit comments

Comments
 (0)