Files
ArchiSteamFarm/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs

308 lines
9.9 KiB
C#
Raw Normal View History

2020-06-13 12:08:21 +02:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// |
// Copyright 2015-2021 Łukasz "JustArchi" Domeradzki
2020-06-13 12:08:21 +02:00
// 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;
using System.Collections.Concurrent;
using System.Collections.Generic;
2021-04-25 23:44:47 +02:00
using System.Globalization;
using System.IO;
2020-06-13 12:08:21 +02:00
using System.Linq;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
2020-06-13 12:08:21 +02:00
using ArchiSteamFarm.Helpers;
2021-04-25 23:44:47 +02:00
using ArchiSteamFarm.Localization;
2021-09-15 12:43:25 +02:00
using JetBrains.Annotations;
2020-06-13 12:08:21 +02:00
using Newtonsoft.Json;
using SteamKit2;
2021-11-10 21:23:24 +01:00
namespace ArchiSteamFarm.OfficialPlugins.SteamTokenDumper;
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
internal sealed class GlobalCache : SerializableFile {
private static string SharedFilePath => Path.Combine(ArchiSteamFarm.SharedInfo.ConfigDirectory, $"{nameof(SteamTokenDumper)}.cache");
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, uint> AppChangeNumbers = new();
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, ulong> AppTokens = new();
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, string> DepotKeys = new();
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, ulong> PackageTokens = new();
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, ulong> SubmittedApps = new();
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, string> SubmittedDepots = new();
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, ulong> SubmittedPackages = new();
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal uint LastChangeNumber { get; private set; }
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
internal GlobalCache() => FilePath = SharedFilePath;
2021-09-15 12:43:25 +02:00
2021-11-10 21:23:24 +01:00
[UsedImplicitly]
public bool ShouldSerializeAppChangeNumbers() => !AppChangeNumbers.IsEmpty;
2021-09-15 12:43:25 +02:00
2021-11-10 21:23:24 +01:00
[UsedImplicitly]
public bool ShouldSerializeAppTokens() => !AppTokens.IsEmpty;
2021-09-15 12:43:25 +02:00
2021-11-10 21:23:24 +01:00
[UsedImplicitly]
public bool ShouldSerializeDepotKeys() => !DepotKeys.IsEmpty;
2021-09-15 12:43:25 +02:00
2021-11-10 21:23:24 +01:00
[UsedImplicitly]
public bool ShouldSerializeLastChangeNumber() => LastChangeNumber > 0;
2021-09-15 12:43:25 +02:00
2021-11-10 21:23:24 +01:00
[UsedImplicitly]
public bool ShouldSerializePackageTokens() => !PackageTokens.IsEmpty;
2021-09-15 12:43:25 +02:00
2021-11-10 21:23:24 +01:00
[UsedImplicitly]
public bool ShouldSerializeSubmittedApps() => !SubmittedApps.IsEmpty;
2021-09-15 12:43:25 +02:00
2021-11-10 21:23:24 +01:00
[UsedImplicitly]
public bool ShouldSerializeSubmittedDepots() => !SubmittedDepots.IsEmpty;
2021-09-15 12:43:25 +02:00
2021-11-10 21:23:24 +01:00
[UsedImplicitly]
public bool ShouldSerializeSubmittedPackages() => !SubmittedPackages.IsEmpty;
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
internal ulong GetAppToken(uint appID) => AppTokens[appID];
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
internal Dictionary<uint, ulong> 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(static appToken => appToken.Key, static appToken => appToken.Value);
internal Dictionary<uint, string> 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(static depotKey => depotKey.Key, static depotKey => depotKey.Value);
internal Dictionary<uint, ulong> 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(static packageToken => packageToken.Key, static packageToken => packageToken.Value);
2021-04-25 23:44:47 +02:00
2021-11-10 21:23:24 +01:00
internal static async Task<GlobalCache?> Load() {
if (!File.Exists(SharedFilePath)) {
GlobalCache result = new();
2021-04-25 23:44:47 +02:00
2021-11-10 21:23:24 +01:00
Utilities.InBackground(result.Save);
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
return result;
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
GlobalCache? globalCache;
2021-04-25 23:44:47 +02:00
2021-11-10 21:23:24 +01:00
try {
string json = await File.ReadAllTextAsync(SharedFilePath).ConfigureAwait(false);
2021-04-25 23:44:47 +02:00
2021-11-10 21:23:24 +01:00
if (string.IsNullOrEmpty(json)) {
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(json)));
2021-04-25 23:44:47 +02:00
return null;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
globalCache = JsonConvert.DeserializeObject<GlobalCache>(json);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
return null;
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
if (globalCache == null) {
ASF.ArchiLogger.LogNullError(nameof(globalCache));
return null;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
return globalCache;
}
2020-11-14 22:37:00 +01:00
2021-11-10 21:23:24 +01:00
internal void OnPICSChanges(uint currentChangeNumber, IReadOnlyCollection<KeyValuePair<uint, SteamApps.PICSChangesCallback.PICSChangeData>> appChanges) {
if (currentChangeNumber == 0) {
throw new ArgumentOutOfRangeException(nameof(currentChangeNumber));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
if (appChanges == null) {
throw new ArgumentNullException(nameof(appChanges));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
if (currentChangeNumber <= LastChangeNumber) {
return;
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
LastChangeNumber = currentChangeNumber;
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach ((uint appID, SteamApps.PICSChangesCallback.PICSChangeData appData) in appChanges) {
if (!AppChangeNumbers.TryGetValue(appID, out uint previousChangeNumber) || (appData.ChangeNumber <= previousChangeNumber)) {
continue;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
AppChangeNumbers.TryRemove(appID, out _);
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
Utilities.InBackground(Save);
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
internal void OnPICSChangesRestart(uint currentChangeNumber) {
if (currentChangeNumber == 0) {
throw new ArgumentOutOfRangeException(nameof(currentChangeNumber));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
if (currentChangeNumber <= LastChangeNumber) {
return;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
LastChangeNumber = currentChangeNumber;
AppChangeNumbers.Clear();
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
Utilities.InBackground(Save);
}
internal bool ShouldRefreshAppInfo(uint appID) => !AppChangeNumbers.ContainsKey(appID);
internal bool ShouldRefreshDepotKey(uint depotID) => !DepotKeys.ContainsKey(depotID);
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
internal void UpdateAppChangeNumbers(IReadOnlyCollection<KeyValuePair<uint, uint>> appChangeNumbers) {
if (appChangeNumbers == null) {
throw new ArgumentNullException(nameof(appChangeNumbers));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
bool save = false;
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach ((uint appID, uint changeNumber) in appChangeNumbers) {
if (AppChangeNumbers.TryGetValue(appID, out uint previousChangeNumber) && (previousChangeNumber == changeNumber)) {
continue;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
AppChangeNumbers[appID] = changeNumber;
save = true;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
if (save) {
Utilities.InBackground(Save);
}
}
2020-11-14 22:37:00 +01:00
2021-11-10 21:23:24 +01:00
internal void UpdateAppTokens(IReadOnlyCollection<KeyValuePair<uint, ulong>> appTokens, IReadOnlyCollection<uint> publicAppIDs) {
if (appTokens == null) {
throw new ArgumentNullException(nameof(appTokens));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
if (publicAppIDs == null) {
throw new ArgumentNullException(nameof(publicAppIDs));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
bool save = false;
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach ((uint appID, ulong appToken) in appTokens) {
if (AppTokens.TryGetValue(appID, out ulong previousAppToken) && (previousAppToken == appToken)) {
continue;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
AppTokens[appID] = appToken;
save = true;
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach (uint appID in publicAppIDs) {
if (AppTokens.TryGetValue(appID, out ulong previousAppToken) && (previousAppToken == 0)) {
continue;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
AppTokens[appID] = 0;
save = true;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
if (save) {
Utilities.InBackground(Save);
}
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
internal void UpdateDepotKeys(ICollection<SteamApps.DepotKeyCallback> depotKeyResults) {
if (depotKeyResults == null) {
throw new ArgumentNullException(nameof(depotKeyResults));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
bool save = false;
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach (SteamApps.DepotKeyCallback depotKeyResult in depotKeyResults) {
if (depotKeyResult.Result != EResult.OK) {
continue;
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
string depotKey = BitConverter.ToString(depotKeyResult.DepotKey).Replace("-", "", StringComparison.Ordinal);
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
if (DepotKeys.TryGetValue(depotKeyResult.DepotID, out string? previousDepotKey) && (previousDepotKey == depotKey)) {
continue;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
DepotKeys[depotKeyResult.DepotID] = depotKey;
save = true;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
if (save) {
Utilities.InBackground(Save);
}
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
internal void UpdatePackageTokens(IReadOnlyCollection<KeyValuePair<uint, ulong>> packageTokens) {
if (packageTokens == null) {
throw new ArgumentNullException(nameof(packageTokens));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
bool save = false;
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach ((uint packageID, ulong packageToken) in packageTokens) {
if (PackageTokens.TryGetValue(packageID, out ulong previousPackageToken) && (previousPackageToken == packageToken)) {
continue;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
PackageTokens[packageID] = packageToken;
save = true;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
if (save) {
Utilities.InBackground(Save);
}
}
2020-11-14 22:37:00 +01:00
2021-11-10 21:23:24 +01:00
internal void UpdateSubmittedData(IReadOnlyDictionary<uint, ulong> apps, IReadOnlyDictionary<uint, ulong> packages, IReadOnlyDictionary<uint, string> depots) {
if (apps == null) {
throw new ArgumentNullException(nameof(apps));
}
2020-11-14 22:37:00 +01:00
2021-11-10 21:23:24 +01:00
if (packages == null) {
throw new ArgumentNullException(nameof(packages));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
if (depots == null) {
throw new ArgumentNullException(nameof(depots));
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach ((uint appID, ulong token) in apps) {
SubmittedApps[appID] = token;
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach ((uint packageID, ulong token) in packages) {
SubmittedPackages[packageID] = token;
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach ((uint depotID, string key) in depots) {
SubmittedDepots[depotID] = key;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
Utilities.InBackground(Save);
2020-06-13 12:08:21 +02:00
}
}