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

372 lines
12 KiB
C#
Raw Normal View History

2024-03-17 00:06:13 +01:00
// ----------------------------------------------------------------------------------------------
2020-06-13 12:08:21 +02:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
2024-03-17 00:06:13 +01:00
// ----------------------------------------------------------------------------------------------
2024-03-17 02:35:40 +01:00
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
2020-06-13 12:08:21 +02:00
// Contact: JustArchi@JustArchi.net
2024-03-17 02:35:40 +01:00
// |
2020-06-13 12:08:21 +02:00
// 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
2024-03-17 02:35:40 +01:00
// |
2020-06-13 12:08:21 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-03-17 02:35:40 +01:00
// |
2020-06-13 12:08:21 +02:00
// 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;
2024-01-03 13:46:54 +01:00
using System.Collections.Frozen;
2020-06-13 12:08:21 +02:00
using System.Collections.Generic;
using System.IO;
2020-06-13 12:08:21 +02:00
using System.Linq;
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
using System.Text.Json.Serialization;
2023-11-14 21:10:35 +01:00
using System.Threading;
2020-06-13 12:08:21 +02:00
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
2020-06-13 12:08:21 +02:00
using ArchiSteamFarm.Helpers;
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
using ArchiSteamFarm.Helpers.Json;
2022-01-06 20:22:38 +01:00
using ArchiSteamFarm.OfficialPlugins.SteamTokenDumper.Localization;
2023-01-23 12:08:30 +01:00
using ArchiSteamFarm.Web.Responses;
2021-09-15 12:43:25 +02:00
using JetBrains.Annotations;
2020-06-13 12:08:21 +02:00
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 {
2024-01-03 13:46:54 +01:00
internal static readonly ArchiCacheable<FrozenSet<uint>> KnownDepotIDs = new(ResolveKnownDepotIDs, TimeSpan.FromDays(7));
2023-01-23 12:08:30 +01:00
2021-11-10 21:23:24 +01:00
private static string SharedFilePath => Path.Combine(ArchiSteamFarm.SharedInfo.ConfigDirectory, $"{nameof(SteamTokenDumper)}.cache");
2020-06-13 12:08:21 +02:00
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
[JsonInclude]
internal uint LastChangeNumber { get; private set; }
2020-06-13 12:08:21 +02:00
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
[JsonDisallowNull]
[JsonInclude]
private ConcurrentDictionary<uint, uint> AppChangeNumbers { get; init; } = new();
2020-06-13 12:08:21 +02:00
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
[JsonDisallowNull]
[JsonInclude]
private ConcurrentDictionary<uint, ulong> AppTokens { get; init; } = new();
2020-06-13 12:08:21 +02:00
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
[JsonDisallowNull]
[JsonInclude]
private ConcurrentDictionary<uint, string> DepotKeys { get; init; } = new();
2020-06-13 12:08:21 +02:00
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
[JsonDisallowNull]
[JsonInclude]
private ConcurrentDictionary<uint, ulong> SubmittedApps { get; init; } = new();
2020-06-13 12:08:21 +02:00
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
[JsonDisallowNull]
[JsonInclude]
private ConcurrentDictionary<uint, string> SubmittedDepots { get; init; } = new();
2020-06-13 12:08:21 +02:00
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
[JsonDisallowNull]
[JsonInclude]
private ConcurrentDictionary<uint, ulong> SubmittedPackages { get; init; } = new();
2020-06-13 12:08:21 +02:00
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
[JsonConstructor]
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 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
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
protected override Task Save() => Save(this);
2021-11-10 21:23:24 +01:00
internal ulong GetAppToken(uint appID) => AppTokens[appID];
2020-06-13 12:08:21 +02:00
2023-12-05 01:43:55 +01:00
internal Dictionary<uint, ulong> GetAppTokensForSubmission() => AppTokens.Where(appToken => (SteamTokenDumperPlugin.Config?.SecretAppIDs.Contains(appToken.Key) != true) && (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) != true) && !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() {
if (ASF.GlobalDatabase == null) {
throw new InvalidOperationException(nameof(ASF.GlobalDatabase));
}
2023-12-05 01:43:55 +01:00
return ASF.GlobalDatabase.PackageAccessTokensReadOnly.Where(packageToken => (SteamTokenDumperPlugin.Config?.SecretPackageIDs.Contains(packageToken.Key) != true) && (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)) {
2021-12-11 15:17:28 +01:00
return new GlobalCache();
2021-11-10 21:23:24 +01:00
}
2020-06-13 12:08:21 +02:00
2022-01-06 20:22:38 +01:00
ASF.ArchiLogger.LogGenericInfo(Strings.LoadingGlobalCache);
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)) {
2024-08-05 02:45:53 +02:00
ASF.ArchiLogger.LogGenericError(ArchiSteamFarm.Localization.Strings.FormatErrorIsEmpty(nameof(json)));
2021-04-25 23:44:47 +02:00
return null;
2020-06-13 12:08:21 +02:00
}
Closes #3061 (#3145) * Good start * Misc * Make ApiAuthenticationMiddleware use new json * Remove first newtonsoft dependency * Pull latest ASFB json enhancements * Start reimplementing newtonsoft! * One thing at a time * Keep doing all kind of breaking changes which need to be tested later * Add back ShouldSerialize() support * Misc * Eradicate remaining parts of newtonsoft * WIP * Workaround STJ stupidity in regards to derived types STJ can't serialize derived type properties by default, so we'll use another approach in our serializable file function * Make CI happy * Bunch of further fixes * Fix AddFreeLicense() after rewrite * Add full support for JsonDisallowNullAttribute * Optimize our json utilities even further * Misc * Add support for fields in disallow null * Misc optimization * Fix deserialization of GlobalCache in STD * Fix non-public [JsonExtensionData] * Fix IM missing method exception, correct db storage helpers * Fix saving into generic databases Thanks STJ * Make Save() function abstract to force inheritors to implement it properly * Correct ShouldSerializeAdditionalProperties to be a method * Misc cleanup * Code review * Allow JSON comments in configs, among other * Allow trailing commas in configs Users very often add them accidentally, no reason to throw on them * Fix confirmation ID Probably needs further fixes, will need to check later * Correct confirmations deserialization * Use JsonNumberHandling * Misc * Misc * [JsonDisallowNull] corrections * Forbid [JsonDisallowNull] on non-nullable structs * Not really but okay * Add and use ToJson() helpers * Misc * Misc
2024-02-21 03:09:36 +01:00
globalCache = json.ToJsonObject<GlobalCache>();
2021-11-10 21:23:24 +01:00
} 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(globalCache);
2021-11-10 21:23:24 +01:00
return null;
2020-06-13 12:08:21 +02:00
}
2022-01-06 20:22:38 +01:00
ASF.ArchiLogger.LogGenericInfo(Strings.ValidatingGlobalCacheIntegrity);
if (globalCache.DepotKeys.Values.Any(static depotKey => !IsValidDepotKey(depotKey))) {
2022-01-06 20:22:38 +01:00
ASF.ArchiLogger.LogGenericError(Strings.GlobalCacheIntegrityValidationFailed);
2021-12-11 15:17:28 +01:00
return null;
}
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) {
ArgumentOutOfRangeException.ThrowIfZero(currentChangeNumber);
2021-12-12 01:12:54 +01:00
ArgumentNullException.ThrowIfNull(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) {
2022-02-26 01:21:37 +01:00
if (!AppChangeNumbers.TryGetValue(appID, out uint previousChangeNumber) || (previousChangeNumber >= appData.ChangeNumber)) {
2021-11-10 21:23:24 +01:00
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) {
ArgumentOutOfRangeException.ThrowIfZero(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;
Reset();
}
internal void Reset(bool clear = false) {
2021-11-10 21:23:24 +01:00
AppChangeNumbers.Clear();
2020-06-13 12:08:21 +02:00
if (clear) {
AppTokens.Clear();
DepotKeys.Clear();
}
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) {
2021-12-12 01:12:54 +01:00
ArgumentNullException.ThrowIfNull(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) {
2022-02-26 01:21:37 +01:00
if (AppChangeNumbers.TryGetValue(appID, out uint previousChangeNumber) && (previousChangeNumber >= changeNumber)) {
2021-11-10 21:23:24 +01:00
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) {
2021-12-12 01:12:54 +01:00
ArgumentNullException.ThrowIfNull(appTokens);
ArgumentNullException.ThrowIfNull(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
internal void UpdateDepotKey(SteamApps.DepotKeyCallback depotKeyResult) {
ArgumentNullException.ThrowIfNull(depotKeyResult);
2020-06-13 12:08:21 +02:00
if (depotKeyResult.Result != EResult.OK) {
return;
}
string depotKey = Convert.ToHexString(depotKeyResult.DepotKey);
if (!IsValidDepotKey(depotKey)) {
2024-08-05 02:45:53 +02:00
ASF.ArchiLogger.LogGenericWarning(ArchiSteamFarm.Localization.Strings.FormatErrorIsInvalid(nameof(depotKey)));
2020-06-13 12:08:21 +02:00
return;
2020-06-13 12:08:21 +02:00
}
if (DepotKeys.TryGetValue(depotKeyResult.DepotID, out string? previousDepotKey) && (previousDepotKey == depotKey)) {
return;
2021-11-10 21:23:24 +01:00
}
DepotKeys[depotKeyResult.DepotID] = depotKey;
Utilities.InBackground(Save);
2021-11-10 21:23:24 +01:00
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
internal void UpdateSubmittedData(IReadOnlyDictionary<uint, ulong> apps, IReadOnlyDictionary<uint, ulong> packages, IReadOnlyDictionary<uint, string> depots) {
2021-12-12 01:12:54 +01:00
ArgumentNullException.ThrowIfNull(apps);
ArgumentNullException.ThrowIfNull(packages);
ArgumentNullException.ThrowIfNull(depots);
2020-06-13 12:08:21 +02:00
2022-02-26 01:26:13 +01:00
bool save = false;
2021-11-10 21:23:24 +01:00
foreach ((uint appID, ulong token) in apps) {
2022-02-26 01:26:13 +01:00
if (SubmittedApps.TryGetValue(appID, out ulong previousToken) && (previousToken == token)) {
continue;
}
2021-11-10 21:23:24 +01:00
SubmittedApps[appID] = token;
2022-02-26 01:26:13 +01:00
save = true;
2021-11-10 21:23:24 +01:00
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach ((uint packageID, ulong token) in packages) {
2022-02-26 01:26:13 +01:00
if (SubmittedPackages.TryGetValue(packageID, out ulong previousToken) && (previousToken == token)) {
continue;
}
2021-11-10 21:23:24 +01:00
SubmittedPackages[packageID] = token;
2022-02-26 01:26:13 +01:00
save = true;
2021-11-10 21:23:24 +01:00
}
2020-06-13 12:08:21 +02:00
2021-11-10 21:23:24 +01:00
foreach ((uint depotID, string key) in depots) {
2022-02-26 01:26:13 +01:00
if (SubmittedDepots.TryGetValue(depotID, out string? previousKey) && (previousKey == key)) {
continue;
}
2021-11-10 21:23:24 +01:00
SubmittedDepots[depotID] = key;
2022-02-26 01:26:13 +01:00
save = true;
2020-06-13 12:08:21 +02:00
}
2021-11-10 21:23:24 +01:00
2022-02-26 01:26:13 +01:00
if (save) {
Utilities.InBackground(Save);
}
2020-06-13 12:08:21 +02:00
}
private static bool IsValidDepotKey(string depotKey) {
ArgumentException.ThrowIfNullOrEmpty(depotKey);
return (depotKey.Length == 64) && Utilities.IsValidHexadecimalText(depotKey);
}
2023-01-23 12:08:30 +01:00
private static async Task<(bool Success, FrozenSet<uint>? Result)> ResolveKnownDepotIDs(CancellationToken cancellationToken = default) {
2023-01-23 12:08:30 +01:00
if (ASF.WebBrowser == null) {
throw new InvalidOperationException(nameof(ASF.WebBrowser));
}
Uri request = new($"{SharedInfo.ServerURL}/knowndepots.csv");
2023-11-14 21:10:35 +01:00
StreamResponse? response = await ASF.WebBrowser.UrlGetToStream(request, cancellationToken: cancellationToken).ConfigureAwait(false);
2023-01-23 12:08:30 +01:00
2024-05-19 21:35:20 +02:00
if (response == null) {
return (false, null);
2023-01-23 12:08:30 +01:00
}
2024-05-19 21:36:37 +02:00
HashSet<uint> result;
2023-01-23 12:08:30 +01:00
await using (response.ConfigureAwait(false)) {
2024-05-19 21:35:20 +02:00
if (response.Content == null) {
return (false, null);
}
2023-01-23 12:08:30 +01:00
try {
using StreamReader reader = new(response.Content);
2023-11-14 21:10:35 +01:00
string? countText = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);
2023-01-23 12:08:30 +01:00
if (string.IsNullOrEmpty(countText) || !int.TryParse(countText, out int count) || (count <= 0)) {
2023-10-30 22:44:07 +01:00
ASF.ArchiLogger.LogNullError(countText);
2023-01-23 12:08:30 +01:00
return (false, null);
2023-01-23 12:08:30 +01:00
}
2024-05-19 21:36:37 +02:00
result = new HashSet<uint>(count);
2023-01-23 12:08:30 +01:00
2023-11-14 21:10:35 +01:00
while (await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false) is { Length: > 0 } line) {
2023-01-23 12:08:30 +01:00
if (!uint.TryParse(line, out uint depotID) || (depotID == 0)) {
2023-10-30 22:44:07 +01:00
ASF.ArchiLogger.LogNullError(depotID);
2023-01-23 12:08:30 +01:00
continue;
}
result.Add(depotID);
}
} catch (Exception e) {
ASF.ArchiLogger.LogGenericWarningException(e);
return (false, null);
2023-01-23 12:08:30 +01:00
}
}
2024-05-19 21:36:37 +02:00
return (result.Count > 0, result.ToFrozenSet());
2023-01-23 12:08:30 +01:00
}
2020-06-13 12:08:21 +02:00
}