// _ _ _ ____ _ _____ // / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ // / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ // / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | // /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| // | // 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. #if NETFRAMEWORK using ArchiSteamFarm.RuntimeCompatibility; using File = System.IO.File; using Path = System.IO.Path; #else using System.IO; #endif using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading.Tasks; using ArchiSteamFarm.Core; using ArchiSteamFarm.Helpers; using ArchiSteamFarm.Localization; using Newtonsoft.Json; using SteamKit2; namespace ArchiSteamFarm.OfficialPlugins.SteamTokenDumper { internal sealed class GlobalCache : SerializableFile { private static string SharedFilePath => Path.Combine(ArchiSteamFarm.SharedInfo.ConfigDirectory, nameof(SteamTokenDumper) + ".cache"); [JsonProperty(Required = Required.DisallowNull)] private readonly ConcurrentDictionary AppChangeNumbers = new(); [JsonProperty(Required = Required.DisallowNull)] private readonly ConcurrentDictionary AppTokens = new(); [JsonProperty(Required = Required.DisallowNull)] private readonly ConcurrentDictionary DepotKeys = new(); [JsonProperty(Required = Required.DisallowNull)] private readonly ConcurrentDictionary PackageTokens = new(); [JsonProperty(Required = Required.DisallowNull)] private readonly ConcurrentDictionary SubmittedApps = new(); [JsonProperty(Required = Required.DisallowNull)] private readonly ConcurrentDictionary SubmittedDepots = new(); [JsonProperty(Required = Required.DisallowNull)] private readonly ConcurrentDictionary SubmittedPackages = new(); [JsonProperty(Required = Required.DisallowNull)] internal uint LastChangeNumber { get; private set; } internal GlobalCache() => FilePath = SharedFilePath; internal ulong GetAppToken(uint appID) => AppTokens[appID]; internal Dictionary GetAppTokensForSubmission() => AppTokens.Where(appToken => (SteamTokenDumperPlugin.Config?.SecretAppIDs.Contains(appToken.Key) == false) && (appToken.Value > 0) && (!SubmittedApps.TryGetValue(appToken.Key, out ulong token) || (appToken.Value != token))).ToDictionary(appToken => appToken.Key, appToken => appToken.Value); internal Dictionary GetDepotKeysForSubmission() => DepotKeys.Where(depotKey => (SteamTokenDumperPlugin.Config?.SecretDepotIDs.Contains(depotKey.Key) == false) && !string.IsNullOrEmpty(depotKey.Value) && (!SubmittedDepots.TryGetValue(depotKey.Key, out string? key) || (depotKey.Value != key))).ToDictionary(depotKey => depotKey.Key, depotKey => depotKey.Value); internal Dictionary GetPackageTokensForSubmission() => PackageTokens.Where(packageToken => (SteamTokenDumperPlugin.Config?.SecretPackageIDs.Contains(packageToken.Key) == false) && (packageToken.Value > 0) && (!SubmittedPackages.TryGetValue(packageToken.Key, out ulong token) || (packageToken.Value != token))).ToDictionary(packageToken => packageToken.Key, packageToken => packageToken.Value); internal static async Task Load() { if (!File.Exists(SharedFilePath)) { GlobalCache result = new(); Utilities.InBackground(result.Save); return result; } GlobalCache? globalCache; try { string json = await RuntimeCompatibility.File.ReadAllTextAsync(SharedFilePath).ConfigureAwait(false); if (string.IsNullOrEmpty(json)) { ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(json))); return null; } globalCache = JsonConvert.DeserializeObject(json); } catch (Exception e) { ASF.ArchiLogger.LogGenericException(e); return null; } if (globalCache == null) { ASF.ArchiLogger.LogNullError(nameof(globalCache)); return null; } return globalCache; } internal void OnPICSChanges(uint currentChangeNumber, IReadOnlyCollection> appChanges) { if (currentChangeNumber == 0) { throw new ArgumentOutOfRangeException(nameof(currentChangeNumber)); } if (appChanges == null) { throw new ArgumentNullException(nameof(appChanges)); } if (currentChangeNumber <= LastChangeNumber) { return; } LastChangeNumber = currentChangeNumber; foreach ((uint appID, SteamApps.PICSChangesCallback.PICSChangeData appData) in appChanges) { if (!AppChangeNumbers.TryGetValue(appID, out uint previousChangeNumber) || (appData.ChangeNumber <= previousChangeNumber)) { continue; } AppChangeNumbers.TryRemove(appID, out _); } Utilities.InBackground(Save); } internal void OnPICSChangesRestart(uint currentChangeNumber) { if (currentChangeNumber == 0) { throw new ArgumentOutOfRangeException(nameof(currentChangeNumber)); } if (currentChangeNumber <= LastChangeNumber) { return; } LastChangeNumber = currentChangeNumber; AppChangeNumbers.Clear(); Utilities.InBackground(Save); } internal bool ShouldRefreshAppInfo(uint appID) => !AppChangeNumbers.ContainsKey(appID); internal bool ShouldRefreshDepotKey(uint depotID) => !DepotKeys.ContainsKey(depotID); internal void UpdateAppChangeNumbers(IReadOnlyCollection> appChangeNumbers) { if (appChangeNumbers == null) { throw new ArgumentNullException(nameof(appChangeNumbers)); } bool save = false; foreach ((uint appID, uint changeNumber) in appChangeNumbers) { if (AppChangeNumbers.TryGetValue(appID, out uint previousChangeNumber) && (previousChangeNumber == changeNumber)) { continue; } AppChangeNumbers[appID] = changeNumber; save = true; } if (save) { Utilities.InBackground(Save); } } internal void UpdateAppTokens(IReadOnlyCollection> appTokens, IReadOnlyCollection publicAppIDs) { if (appTokens == null) { throw new ArgumentNullException(nameof(appTokens)); } if (publicAppIDs == null) { throw new ArgumentNullException(nameof(publicAppIDs)); } bool save = false; foreach ((uint appID, ulong appToken) in appTokens) { if (AppTokens.TryGetValue(appID, out ulong previousAppToken) && (previousAppToken == appToken)) { continue; } AppTokens[appID] = appToken; save = true; } foreach (uint appID in publicAppIDs) { if (AppTokens.TryGetValue(appID, out ulong previousAppToken) && (previousAppToken == 0)) { continue; } AppTokens[appID] = 0; save = true; } if (save) { Utilities.InBackground(Save); } } internal void UpdateDepotKeys(ICollection depotKeyResults) { if (depotKeyResults == null) { throw new ArgumentNullException(nameof(depotKeyResults)); } bool save = false; foreach (SteamApps.DepotKeyCallback depotKeyResult in depotKeyResults) { if (depotKeyResult.Result != EResult.OK) { continue; } string depotKey = BitConverter.ToString(depotKeyResult.DepotKey).Replace("-", "", StringComparison.Ordinal); if (DepotKeys.TryGetValue(depotKeyResult.DepotID, out string? previousDepotKey) && (previousDepotKey == depotKey)) { continue; } DepotKeys[depotKeyResult.DepotID] = depotKey; save = true; } if (save) { Utilities.InBackground(Save); } } internal void UpdatePackageTokens(IReadOnlyCollection> packageTokens) { if (packageTokens == null) { throw new ArgumentNullException(nameof(packageTokens)); } bool save = false; foreach ((uint packageID, ulong packageToken) in packageTokens) { if (PackageTokens.TryGetValue(packageID, out ulong previousPackageToken) && (previousPackageToken == packageToken)) { continue; } PackageTokens[packageID] = packageToken; save = true; } if (save) { Utilities.InBackground(Save); } } internal void UpdateSubmittedData(IReadOnlyDictionary apps, IReadOnlyDictionary packages, IReadOnlyDictionary depots) { if (apps == null) { throw new ArgumentNullException(nameof(apps)); } if (packages == null) { throw new ArgumentNullException(nameof(packages)); } if (depots == null) { throw new ArgumentNullException(nameof(depots)); } foreach ((uint appID, ulong token) in apps) { SubmittedApps[appID] = token; } foreach ((uint packageID, ulong token) in packages) { SubmittedPackages[packageID] = token; } foreach ((uint depotID, string key) in depots) { SubmittedDepots[depotID] = key; } Utilities.InBackground(Save); } } }