diff --git a/ArchiSteamFarm/Localization/Strings.Designer.cs b/ArchiSteamFarm/Localization/Strings.Designer.cs index 2cc4a7ead..72a7f7392 100644 --- a/ArchiSteamFarm/Localization/Strings.Designer.cs +++ b/ArchiSteamFarm/Localization/Strings.Designer.cs @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by a tool. // Runtime Version:4.0.30319.42000 @@ -538,14 +538,14 @@ namespace ArchiSteamFarm.Localization { } /// - /// Looks up a localized string similar to Points balance:{0}.. + /// Looks up a localized string similar to Points balance:{0}. /// public static string BotPointsBalance { get { return ResourceManager.GetString("BotPointsBalance", resourceCulture); } } - + /// /// Looks up a localized string similar to Rate limit exceeded, we will retry after {0} of cooldown.... /// diff --git a/ArchiSteamFarm/Steam/Data/AccessTokenResponse.cs b/ArchiSteamFarm/Steam/Data/AccessTokenResponse.cs index cecfaaa89..c5eef3ebe 100644 --- a/ArchiSteamFarm/Steam/Data/AccessTokenResponse.cs +++ b/ArchiSteamFarm/Steam/Data/AccessTokenResponse.cs @@ -1,3 +1,23 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2021 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json; @@ -6,12 +26,18 @@ namespace ArchiSteamFarm.Steam.Data { [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] internal sealed class AccessTokenResponse : EResultResponse { [JsonProperty(PropertyName = "data", Required = Required.Always)] - internal AccessTokenData AccessTokenData { get; private set; } = new(); - } + internal readonly AccessTokenData Data = new(); - [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] - internal sealed class AccessTokenData { - [JsonProperty(PropertyName = "webapi_token", Required = Required.Always)] - internal string WebAPIToken { get; private set; } = ""; + [JsonConstructor] + private AccessTokenResponse() { } + + [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] + internal sealed class AccessTokenData { + [JsonProperty(PropertyName = "webapi_token", Required = Required.Always)] + internal readonly string WebAPIToken = ""; + + [JsonConstructor] + internal AccessTokenData() { } + } } } diff --git a/ArchiSteamFarm/Steam/Data/EResultResponse.cs b/ArchiSteamFarm/Steam/Data/EResultResponse.cs index 9c65d6d61..2cd7b5477 100644 --- a/ArchiSteamFarm/Steam/Data/EResultResponse.cs +++ b/ArchiSteamFarm/Steam/Data/EResultResponse.cs @@ -27,6 +27,7 @@ using SteamKit2; namespace ArchiSteamFarm.Steam.Data { [PublicAPI] [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] + [SuppressMessage("ReSharper", "InconsistentNaming")] public class EResultResponse { [JsonProperty(PropertyName = "success", Required = Required.Always)] public EResult Result { get; private set; } diff --git a/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs b/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs index 13da9a19f..533c42463 100644 --- a/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs +++ b/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs @@ -57,11 +57,11 @@ namespace ArchiSteamFarm.Steam.Integration { internal const ushort MaxItemsInSingleInventoryRequest = 5000; private const string EconService = "IEconService"; + private const string LoyaltyRewardsService = "ILoyaltyRewardsService"; private const string PlayerService = "IPlayerService"; private const string SteamAppsService = "ISteamApps"; private const string SteamUserAuthService = "ISteamUserAuth"; private const string TwoFactorService = "ITwoFactorService"; - private const string LoyaltyRewardsService = "ILoyaltyRewardsService"; [PublicAPI] public static Uri SteamCommunityURL => new("https://steamcommunity.com"); @@ -74,11 +74,12 @@ namespace ArchiSteamFarm.Steam.Integration { private static readonly ConcurrentDictionary CachedCardCountsForGame = new(); - [PublicAPI] - public ArchiCacheable CachedApiKey { get; } [PublicAPI] public ArchiCacheable CachedAccessToken { get; } + [PublicAPI] + public ArchiCacheable CachedApiKey { get; } + [PublicAPI] public WebBrowser WebBrowser { get; } @@ -385,6 +386,12 @@ namespace ArchiSteamFarm.Steam.Integration { return null; } + // Extra entry for format + Dictionary arguments = new(3, StringComparer.Ordinal) { + { "access_token", accessToken! }, + { "steamid", Bot.SteamID.ToString(CultureInfo.InvariantCulture) } + }; + KeyValue? response = null; for (byte i = 0; (i < WebBrowser.MaxTries) && (response == null); i++) { @@ -396,13 +403,9 @@ namespace ArchiSteamFarm.Steam.Integration { response = await WebLimitRequest( WebAPI.DefaultBaseAddress, + // TODO: Remove this ToDictionary() call after https://github.com/SteamRE/SteamKit/pull/992 is released // ReSharper disable once AccessToDisposedClosure - async () => await loyaltyRewardsService.CallAsync( - HttpMethod.Get, "GetSummary", args: new Dictionary(2, StringComparer.Ordinal) { - { "access_token", accessToken! }, - { "steamid", Bot.SteamID.ToString(CultureInfo.InvariantCulture) } - } - ).ConfigureAwait(false) + async () => await loyaltyRewardsService.CallAsync(HttpMethod.Get, "GetSummary", args: arguments.ToDictionary(kv => kv.Key, kv => kv.Value)).ConfigureAwait(false) ).ConfigureAwait(false); } catch (TaskCanceledException e) { Bot.ArchiLogger.LogGenericDebuggingException(e); @@ -413,16 +416,27 @@ namespace ArchiSteamFarm.Steam.Integration { if (response == null) { Bot.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)); + return null; } KeyValue pointsInfo = response["summary"]["points"]; if (pointsInfo == KeyValue.Invalid) { + Bot.ArchiLogger.LogNullError(nameof(pointsInfo)); + return null; } - return uint.TryParse(pointsInfo.Value, out uint points) ? points : null; + uint result = pointsInfo.AsUnsignedInteger(uint.MaxValue); + + if (result == uint.MaxValue) { + Bot.ArchiLogger.LogNullError(nameof(result)); + + return null; + } + + return result; } [PublicAPI] @@ -2686,6 +2700,14 @@ namespace ArchiSteamFarm.Steam.Integration { return await UrlPostWithSession(request, data: data).ConfigureAwait(false); } + private async Task<(bool Success, string? Result)> ResolveAccessToken() { + Uri request = new(SteamStoreURL, "/pointssummary/ajaxgetasyncconfig"); + + ObjectResponse? response = await UrlGetToJsonObjectWithSession(request).ConfigureAwait(false); + + return !string.IsNullOrEmpty(response?.Content.Data.WebAPIToken) ? (true, response!.Content.Data.WebAPIToken) : (false, null); + } + private async Task<(bool Success, string? Result)> ResolveApiKey() { if (Bot.IsAccountLimited) { // API key is permanently unavailable for limited accounts @@ -2735,17 +2757,6 @@ namespace ArchiSteamFarm.Steam.Integration { return (false, null); } } - private async Task<(bool Success, string? Result)> ResolveAccessToken() { - Uri request = new(SteamStoreURL, "/pointssummary/ajaxgetasyncconfig"); - - ObjectResponse? response = await UrlGetToJsonObjectWithSession(request).ConfigureAwait(false); - - if (string.IsNullOrEmpty(response?.Content.AccessTokenData.WebAPIToken )) { - return (false, null); - } - - return (true, response!.Content.AccessTokenData.WebAPIToken ); - } private async Task UnlockParentalAccount(string parentalCode) { if (string.IsNullOrEmpty(parentalCode)) {