Skip to content

Commit

Permalink
Use ASF's global database for STD package access tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Dec 4, 2023
1 parent 92858de commit 8d1d508
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 31 deletions.
35 changes: 8 additions & 27 deletions ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ internal sealed class GlobalCache : SerializableFile {
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, string> DepotKeys = new();

[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, ulong> PackageTokens = new();

[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, ulong> SubmittedApps = new();

Expand All @@ -81,9 +78,6 @@ internal sealed class GlobalCache : SerializableFile {
[UsedImplicitly]
public bool ShouldSerializeLastChangeNumber() => LastChangeNumber > 0;

[UsedImplicitly]
public bool ShouldSerializePackageTokens() => !PackageTokens.IsEmpty;

[UsedImplicitly]
public bool ShouldSerializeSubmittedApps() => !SubmittedApps.IsEmpty;

Expand All @@ -97,7 +91,14 @@ internal sealed class GlobalCache : SerializableFile {

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);

internal Dictionary<uint, ulong> GetPackageTokensForSubmission() {
if (ASF.GlobalDatabase == null) {
throw new InvalidOperationException(nameof(ASF.GlobalDatabase));
}

return ASF.GlobalDatabase.PackageAccessTokensReadOnly.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);
}

internal static async Task<GlobalCache?> Load() {
if (!File.Exists(SharedFilePath)) {
Expand Down Expand Up @@ -180,7 +181,6 @@ internal void Reset(bool clear = false) {
if (clear) {
AppTokens.Clear();
DepotKeys.Clear();
PackageTokens.Clear();
}

Utilities.InBackground(Save);
Expand Down Expand Up @@ -261,25 +261,6 @@ internal void UpdateDepotKey(SteamApps.DepotKeyCallback depotKeyResult) {
Utilities.InBackground(Save);
}

internal void UpdatePackageTokens(IReadOnlyCollection<KeyValuePair<uint, ulong>> packageTokens) {
ArgumentNullException.ThrowIfNull(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<uint, ulong> apps, IReadOnlyDictionary<uint, ulong> packages, IReadOnlyDictionary<uint, string> depots) {
ArgumentNullException.ThrowIfNull(apps);
ArgumentNullException.ThrowIfNull(packages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,9 @@ private static async void OnLicenseList(Bot bot, SteamApps.LicenseListCallback c
throw new InvalidOperationException(nameof(GlobalCache));
}

Dictionary<uint, ulong> packageTokens = callback.LicenseList.Where(static license => !Config.SecretPackageIDs.Contains(license.PackageID) && ((license.PaymentMethod != EPaymentMethod.AutoGrant) || !Config.SkipAutoGrantPackages)).GroupBy(static license => license.PackageID).ToDictionary(static group => group.Key, static group => group.OrderByDescending(static license => license.TimeCreated).First().AccessToken);
HashSet<uint> packageIDs = callback.LicenseList.Where(static license => !Config.SecretPackageIDs.Contains(license.PackageID) && ((license.PaymentMethod != EPaymentMethod.AutoGrant) || !Config.SkipAutoGrantPackages)).Select(static license => license.PackageID).ToHashSet();

GlobalCache.UpdatePackageTokens(packageTokens);

await Refresh(bot, packageTokens.Keys).ConfigureAwait(false);
await Refresh(bot, packageIDs).ConfigureAwait(false);
}

private static async void OnSubmissionTimer(object? state = null) => await SubmitData().ConfigureAwait(false);
Expand Down

0 comments on commit 8d1d508

Please sign in to comment.