Skip to content

Commit

Permalink
Sync inventory parsing with ASFB
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Mar 27, 2024
1 parent b63161c commit 6e30b31
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
15 changes: 10 additions & 5 deletions ArchiSteamFarm/Steam/Integration/ArchiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,21 @@ public async IAsyncEnumerable<Asset> GetMyInventoryAsync(uint appID = Asset.Stea

// Interpret the result and see what we should do about it
switch (serviceMethodResponse.Result) {
case EResult.OK:
// Success, we can continue
break;
case EResult.Busy:
case EResult.DuplicateRequest:
case EResult.RemoteCallFailed:
case EResult.ServiceUnavailable:
// Those are generic failures that we should be able to retry
case EResult.Timeout:
// Expected failures that we should be able to retry
ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, serviceMethodResponse.Result));

continue;
case EResult.OK:
// Success, we can continue
break;
case EResult.NoMatch:
// Expected failures that we're not going to retry
throw new TimeoutException(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, serviceMethodResponse.Result));
default:
// Unknown failures, report them and do not retry since we're unsure if we should
ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownValuePleaseReport, nameof(serviceMethodResponse.Result), serviceMethodResponse.Result));
Expand All @@ -261,7 +266,7 @@ public async IAsyncEnumerable<Asset> GetMyInventoryAsync(uint appID = Asset.Stea
}

if (response.descriptions.Count == 0) {
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(response.descriptions)));
throw new InvalidOperationException(nameof(response.descriptions));
}

if (response.total_inventory_count > Array.MaxLength) {
Expand Down
16 changes: 10 additions & 6 deletions ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public async IAsyncEnumerable<Asset> GetInventoryAsync(ulong steamID = 0, uint a
ObjectResponse<InventoryResponse>? response = null;

try {
for (byte i = 0; (i < WebBrowser.MaxTries) && (response?.StatusCode.IsSuccessCode() != true); i++) {
for (byte i = 0; (i < WebBrowser.MaxTries) && (response?.Content is not { Result: EResult.OK } || !response.StatusCode.IsSuccessCode()); i++) {
if ((i > 0) && (rateLimitingDelay > 0)) {
await Task.Delay(rateLimitingDelay).ConfigureAwait(false);
}
Expand All @@ -300,12 +300,16 @@ public async IAsyncEnumerable<Asset> GetInventoryAsync(ulong steamID = 0, uint a

// Try to interpret the failure reason and see if we should try again
switch (response.Content.ErrorCode) {
case null:
case EResult.Busy:
case EResult.DuplicateRequest:
case EResult.RemoteCallFailed:
case EResult.ServiceUnavailable:
// Those are generic failures that we should be able to retry
case EResult.Timeout:
// Expected failures that we should be able to retry
continue;
case EResult.NoMatch:
// Expected failures that we're not going to retry
throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, response.Content.ErrorText), null, response.StatusCode);
default:
// Unknown failures, report them and do not retry since we're unsure if we should
Bot.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownValuePleaseReport, nameof(response.Content.ErrorText), response.Content.ErrorText));
Expand All @@ -331,8 +335,8 @@ public async IAsyncEnumerable<Asset> GetInventoryAsync(ulong steamID = 0, uint a
throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(response)));
}

if ((response.Content == null) || (response.StatusCode.IsSuccessCode() != true)) {
throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, !string.IsNullOrEmpty(response.Content?.ErrorText) ? response.Content.ErrorText : response.Content?.Result.HasValue == true ? response.Content.Result : response.StatusCode));
if (response.Content is not { Result: EResult.OK } || !response.StatusCode.IsSuccessCode()) {
throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, response.Content?.ErrorText ?? response.Content?.Result?.ToString() ?? response.StatusCode.ToString()));
}

if ((response.Content.TotalInventoryCount == 0) || (response.Content.Assets.Count == 0)) {
Expand All @@ -341,7 +345,7 @@ public async IAsyncEnumerable<Asset> GetInventoryAsync(ulong steamID = 0, uint a
}

if (response.Content.Descriptions.Count == 0) {
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(response.Content.Descriptions)));
throw new InvalidOperationException(nameof(response.Content.Descriptions));
}

if (response.Content.TotalInventoryCount > Array.MaxLength) {
Expand Down
8 changes: 2 additions & 6 deletions ArchiSteamFarm/Steam/Integration/SteamUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ public static string ToSteamClientLanguage(this CultureInfo cultureInfo) {
internal static EResult? InterpretError(string errorText) {
ArgumentException.ThrowIfNullOrEmpty(errorText);

if (errorText.StartsWith("EYldRefreshAppIfNecessary", StringComparison.Ordinal)) {
return EResult.ServiceUnavailable;
}

int startIndex = errorText.LastIndexOf('(');

if (startIndex < 0) {
Expand All @@ -96,15 +92,15 @@ public static string ToSteamClientLanguage(this CultureInfo cultureInfo) {
string errorCodeText = errorText[startIndex..endIndex];

if (!byte.TryParse(errorCodeText, out byte errorCode)) {
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownValuePleaseReport, nameof(errorCodeText), errorCodeText));
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownValuePleaseReport, nameof(errorText), errorText));

return null;
}

EResult result = (EResult) errorCode;

if (!Enum.IsDefined(result)) {
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownValuePleaseReport, nameof(EResult), result));
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownValuePleaseReport, nameof(errorText), errorText));

return null;
}
Expand Down

0 comments on commit 6e30b31

Please sign in to comment.