Skip to content

Commit 904e1f3

Browse files
committed
Misc refactor
1 parent ffa71c7 commit 904e1f3

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

ArchiSteamFarm.Tests/SteamUtilities.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ internal void TryParseGameIdentifierReturnsExpectedId(string input, EGameIdentif
5656
[DataRow("sub/abc", EGameIdentifier.Application)]
5757
[DataRow("unknown/123", EGameIdentifier.Application)]
5858
[DataRow("https://store.steampowered.com/bundle/123", EGameIdentifier.Application)]
59-
[DataRow("https://example.com/app/730", EGameIdentifier.Application)]
59+
[DataRow("https://example.com/app/730", EGameIdentifier.Package)]
6060
[DataRow("regex/pattern", EGameIdentifier.Application)]
6161
[DataRow("name/Half-Life", EGameIdentifier.Application)]
62+
[DataRow("steam://launch/Half-Life/", EGameIdentifier.Package)]
6263
[TestMethod]
6364
internal void TryParseGameIdentifierReturnsFalseForInvalidInput(string input, EGameIdentifier defaultType) {
6465
bool result = TryParseGameIdentifier(input, defaultType, out EGameIdentifier? type, out uint id);
@@ -73,6 +74,7 @@ internal void TryParseGameIdentifierReturnsFalseForInvalidInput(string input, EG
7374
[DataRow("name/Half-Life", EGameIdentifier.Application, EGameIdentifier.Name, "Half-Life")]
7475
[DataRow("n/Portal", EGameIdentifier.Application, EGameIdentifier.Name, "Portal")]
7576
[DataRow("http:CS2", EGameIdentifier.Name, EGameIdentifier.Name, "http:CS2")]
77+
[DataRow("steam:CS2", EGameIdentifier.Name, EGameIdentifier.Name, "steam:CS2")]
7678
[TestMethod]
7779
internal void TryParseGameIdentifierStringReturnsExpectedValue(string input, EGameIdentifier defaultType, EGameIdentifier expectedType, string expectedValue) {
7880
bool result = TryParseGameIdentifier(input, defaultType, out EGameIdentifier? type, out string? value);

ArchiSteamFarm/Steam/Integration/SteamUtilities.cs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,37 +153,51 @@ 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) && Uri.TryCreate(input, UriKind.Absolute, out Uri? uri) && uri.Host.Equals(ArchiWebHandler.SteamStoreURL.Host, StringComparison.OrdinalIgnoreCase)) {
157-
string[] segments = uri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries);
156+
if (Uri.TryCreate(input, UriKind.Absolute, out Uri? uri)) {
157+
switch (uri.Scheme) {
158+
case "http" or "https" when uri.Host.Equals(ArchiWebHandler.SteamStoreURL.Host, StringComparison.OrdinalIgnoreCase):
159+
// Handle https://store.steampowered.com/<type>/<appID>
160+
string[] httpSegments = uri.AbsolutePath.Split('/', 3, StringSplitOptions.RemoveEmptyEntries);
161+
162+
if (httpSegments.Length < 2) {
163+
break;
164+
}
165+
166+
type = httpSegments[0].ToUpperInvariant() switch {
167+
"APP" => EGameIdentifier.Application,
168+
"SUB" => EGameIdentifier.Package,
169+
_ => null
170+
};
171+
172+
if (type == null) {
173+
break;
174+
}
158175

159-
if (segments.Length >= 2) {
160-
type = segments[0].ToUpperInvariant() switch {
161-
"APP" => EGameIdentifier.Application,
162-
"SUB" => EGameIdentifier.Package,
163-
_ => null
164-
};
176+
if (!uint.TryParse(httpSegments[1], out uint pathNumericValue) || (pathNumericValue == 0)) {
177+
type = null;
178+
179+
break;
180+
}
165181

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

169184
return true;
170-
}
171-
}
172-
}
185+
case "steam" when uri.Host.Equals("launch", StringComparison.OrdinalIgnoreCase):
186+
// Handle steam://launch/<appID>/ and steam://launch/<appID>/Dialog formats
187+
string[] steamSegments = uri.AbsolutePath.Split('/', 2, StringSplitOptions.RemoveEmptyEntries);
173188

174-
// Handle steam://launch/APPID/ and steam://launch/APPID/Dialog formats
175-
if (input.StartsWith("steam://launch/", StringComparison.OrdinalIgnoreCase)) {
176-
string launchPath = input["steam://launch/".Length..];
177-
string[] launchSegments = launchPath.Split('/', StringSplitOptions.RemoveEmptyEntries);
189+
if ((steamSegments.Length < 1) || !uint.TryParse(steamSegments[0], out uint launchAppId) || (launchAppId == 0)) {
190+
break;
191+
}
178192

179-
if ((launchSegments.Length >= 1) && uint.TryParse(launchSegments[0], out uint launchAppId) && (launchAppId > 0)) {
180-
type = EGameIdentifier.Application;
181-
value = launchSegments[0];
193+
type = EGameIdentifier.Application;
194+
value = steamSegments[0];
182195

183-
return true;
196+
return true;
184197
}
185198
}
186199

200+
// Handle common ASF-specific format of <format>/<input>
187201
int slashIndex = input.IndexOf('/', StringComparison.Ordinal);
188202

189203
if ((slashIndex > 0) && (input.Length > slashIndex + 1)) {

0 commit comments

Comments
 (0)