From 8bb48d829e5617cd542a6fdedcdded5a6e663ab3 Mon Sep 17 00:00:00 2001 From: Archi Date: Mon, 23 Jan 2023 12:08:30 +0100 Subject: [PATCH] STD: Add support for KnownDepotIDs --- .../GlobalCache.cs | 50 +++++++++++++++++++ .../SteamTokenDumperPlugin.cs | 8 ++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs index 3e48b182d..ac3b139c1 100644 --- a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs +++ b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs @@ -22,6 +22,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Immutable; using System.Globalization; using System.IO; using System.Linq; @@ -29,6 +30,7 @@ using System.Threading.Tasks; using ArchiSteamFarm.Core; using ArchiSteamFarm.Helpers; using ArchiSteamFarm.OfficialPlugins.SteamTokenDumper.Localization; +using ArchiSteamFarm.Web.Responses; using JetBrains.Annotations; using Newtonsoft.Json; using SteamKit2; @@ -36,6 +38,8 @@ using SteamKit2; namespace ArchiSteamFarm.OfficialPlugins.SteamTokenDumper; internal sealed class GlobalCache : SerializableFile { + internal static readonly ArchiCacheable> KnownDepotIDs = new(ResolveKnownDepotIDs, TimeSpan.FromDays(7)); + private static string SharedFilePath => Path.Combine(ArchiSteamFarm.SharedInfo.ConfigDirectory, $"{nameof(SteamTokenDumper)}.cache"); [JsonProperty(Required = Required.DisallowNull)] @@ -326,4 +330,50 @@ internal sealed class GlobalCache : SerializableFile { return (depotKey.Length == 64) && Utilities.IsValidHexadecimalText(depotKey); } + + private static async Task<(bool Success, ImmutableHashSet? Result)> ResolveKnownDepotIDs() { + if (ASF.WebBrowser == null) { + throw new InvalidOperationException(nameof(ASF.WebBrowser)); + } + + Uri request = new($"{SharedInfo.ServerURL}/knowndepots.csv"); + + StreamResponse? response = await ASF.WebBrowser.UrlGetToStream(request).ConfigureAwait(false); + + if (response?.Content == null) { + return (false, null); + } + + await using (response.ConfigureAwait(false)) { + try { + using StreamReader reader = new(response.Content); + + string? countText = await reader.ReadLineAsync().ConfigureAwait(false); + + if (string.IsNullOrEmpty(countText) || !int.TryParse(countText, out int count) || (count <= 0)) { + ASF.ArchiLogger.LogNullError(nameof(countText)); + + return (false, null); + } + + HashSet result = new(count); + + while (await reader.ReadLineAsync().ConfigureAwait(false) is { Length: > 0 } line) { + if (!uint.TryParse(line, out uint depotID) || (depotID == 0)) { + ASF.ArchiLogger.LogNullError(nameof(depotID)); + + continue; + } + + result.Add(depotID); + } + + return (result.Count > 0, result.ToImmutableHashSet()); + } catch (Exception e) { + ASF.ArchiLogger.LogGenericWarningException(e); + + return (false, null); + } + } + } } diff --git a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs index 179b56a0c..a9968e918 100644 --- a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs +++ b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs @@ -22,6 +22,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Immutable; using System.ComponentModel; using System.Composition; using System.Globalization; @@ -30,6 +31,7 @@ using System.Net; using System.Threading; using System.Threading.Tasks; using ArchiSteamFarm.Core; +using ArchiSteamFarm.Helpers; using ArchiSteamFarm.OfficialPlugins.SteamTokenDumper.Data; using ArchiSteamFarm.OfficialPlugins.SteamTokenDumper.Localization; using ArchiSteamFarm.Plugins; @@ -403,6 +405,8 @@ internal sealed class SteamTokenDumperPlugin : OfficialPlugin, IASF, IBot, IBotC bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotFinishedRetrievingTotalAppAccessTokens, appIDsToRefresh.Count)); bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotRetrievingTotalDepots, appIDsToRefresh.Count)); + (_, ImmutableHashSet? knownDepotIDs) = await GlobalCache.KnownDepotIDs.GetValue(ArchiCacheable>.EFallback.SuccessPreviously).ConfigureAwait(false); + using (HashSet.Enumerator enumerator = appIDsToRefresh.GetEnumerator()) { while (true) { if (!bot.IsConnectedAndLoggedOn) { @@ -454,7 +458,7 @@ internal sealed class SteamTokenDumperPlugin : OfficialPlugin, IASF, IBot, IBotC bool shouldFetchMainKey = false; foreach (KeyValue depot in app.KeyValues["depots"].Children) { - if (!uint.TryParse(depot.Name, out uint depotID) || Config.SecretDepotIDs.Contains(depotID) || !GlobalCache.ShouldRefreshDepotKey(depotID)) { + if (!uint.TryParse(depot.Name, out uint depotID) || (knownDepotIDs?.Contains(depotID) == true) || Config.SecretDepotIDs.Contains(depotID) || !GlobalCache.ShouldRefreshDepotKey(depotID)) { continue; } @@ -490,7 +494,7 @@ internal sealed class SteamTokenDumperPlugin : OfficialPlugin, IASF, IBot, IBotC } // Consider fetching main appID key only if we've actually considered some new depots for resolving - if (shouldFetchMainKey && GlobalCache.ShouldRefreshDepotKey(app.ID)) { + if (shouldFetchMainKey && (knownDepotIDs?.Contains(app.ID) != true) && GlobalCache.ShouldRefreshDepotKey(app.ID)) { depotKeysTotal++; await depotsRateLimitingSemaphore.WaitAsync().ConfigureAwait(false);