2020-06-13 10:08:21 +00:00
|
|
|
// _ _ _ ____ _ _____
|
|
|
|
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
|
|
|
// |
|
2021-01-03 21:24:22 +00:00
|
|
|
// Copyright 2015-2021 Łukasz "JustArchi" Domeradzki
|
2020-06-13 10:08:21 +00: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-01-03 20:37:16 +00:00
|
|
|
using System.Collections.Immutable;
|
2020-06-13 10:08:21 +00:00
|
|
|
using System.Composition;
|
2020-11-15 23:44:37 +00:00
|
|
|
using System.Globalization;
|
2020-06-13 10:08:21 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-04-25 17:55:30 +00:00
|
|
|
using ArchiSteamFarm.OfficialPlugins.SteamTokenDumper.Localization;
|
2020-06-13 10:08:21 +00:00
|
|
|
using ArchiSteamFarm.Plugins;
|
2020-06-24 18:54:41 +00:00
|
|
|
using Newtonsoft.Json;
|
2020-06-13 10:08:21 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using SteamKit2;
|
|
|
|
|
|
|
|
namespace ArchiSteamFarm.OfficialPlugins.SteamTokenDumper {
|
|
|
|
[Export(typeof(IPlugin))]
|
2020-06-13 13:09:12 +00:00
|
|
|
internal sealed class SteamTokenDumperPlugin : OfficialPlugin, IASF, IBot, IBotSteamClient, ISteamPICSChanges {
|
2021-05-02 23:08:12 +00:00
|
|
|
[JsonProperty]
|
|
|
|
internal static SteamTokenDumperConfig? Config { get; private set; }
|
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
private static readonly ConcurrentDictionary<Bot, IDisposable> BotSubscriptions = new();
|
|
|
|
private static readonly ConcurrentDictionary<Bot, (SemaphoreSlim RefreshSemaphore, Timer RefreshTimer)> BotSynchronizations = new();
|
|
|
|
private static readonly SemaphoreSlim SubmissionSemaphore = new(1, 1);
|
|
|
|
private static readonly Timer SubmissionTimer = new(SubmitData);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2020-08-22 19:41:01 +00:00
|
|
|
private static GlobalCache? GlobalCache;
|
2020-06-24 18:49:55 +00:00
|
|
|
|
2020-06-24 18:54:41 +00:00
|
|
|
[JsonProperty]
|
2020-06-13 13:09:12 +00:00
|
|
|
public override string Name => nameof(SteamTokenDumperPlugin);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2020-06-24 18:54:41 +00:00
|
|
|
[JsonProperty]
|
2020-11-15 23:44:37 +00:00
|
|
|
public override Version Version => typeof(SteamTokenDumperPlugin).Assembly.GetName().Version ?? throw new InvalidOperationException(nameof(Version));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
public Task<uint> GetPreferredChangeNumberToStartFrom() => Task.FromResult(Config?.Enabled == true ? GlobalCache?.LastChangeNumber ?? 0 : 0);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2020-12-02 23:10:52 +00:00
|
|
|
public async void OnASFInit(IReadOnlyDictionary<string, JToken>? additionalConfigProperties = null) {
|
2020-06-13 13:35:56 +00:00
|
|
|
if (!SharedInfo.HasValidToken) {
|
2021-04-25 17:55:30 +00:00
|
|
|
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.PluginDisabledMissingBuildToken, nameof(SteamTokenDumperPlugin)));
|
2020-06-13 13:35:56 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
bool isEnabled = false;
|
|
|
|
SteamTokenDumperConfig? config = null;
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
if (additionalConfigProperties != null) {
|
|
|
|
foreach ((string configProperty, JToken configValue) in additionalConfigProperties) {
|
|
|
|
try {
|
2021-05-02 23:08:12 +00:00
|
|
|
switch (configProperty) {
|
2021-05-03 10:21:00 +00:00
|
|
|
case nameof(GlobalConfigExtension.SteamTokenDumperPlugin):
|
2021-05-02 23:08:12 +00:00
|
|
|
config = configValue.Value<SteamTokenDumperConfig>();
|
2021-04-25 17:55:30 +00:00
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
break;
|
|
|
|
case nameof(GlobalConfigExtension.SteamTokenDumperPluginEnabled):
|
|
|
|
isEnabled = configValue.Value<bool>();
|
|
|
|
|
|
|
|
break;
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
ASF.ArchiLogger.LogGenericException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
config ??= new SteamTokenDumperConfig();
|
|
|
|
|
|
|
|
if (isEnabled) {
|
|
|
|
config.Enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config = config;
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (!config.Enabled) {
|
2021-04-25 17:55:30 +00:00
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginDisabledInConfig, nameof(SteamTokenDumperPlugin)));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (!config.SecretAppIDs.IsEmpty) {
|
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginSecretListInitialized, nameof(config.SecretAppIDs), string.Join(", ", config.SecretAppIDs)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.SecretPackageIDs.IsEmpty) {
|
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginSecretListInitialized, nameof(config.SecretPackageIDs), string.Join(", ", config.SecretPackageIDs)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.SecretDepotIDs.IsEmpty) {
|
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginSecretListInitialized, nameof(config.SecretDepotIDs), string.Join(", ", config.SecretDepotIDs)));
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
if (GlobalCache == null) {
|
|
|
|
GlobalCache? globalCache = await GlobalCache.Load().ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (globalCache == null) {
|
|
|
|
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.FileCouldNotBeLoadedFreshInit, nameof(GlobalCache)));
|
|
|
|
|
|
|
|
globalCache = new GlobalCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalCache = globalCache;
|
|
|
|
}
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
TimeSpan startIn = TimeSpan.FromMinutes(Utilities.RandomNext(SharedInfo.MinimumMinutesBeforeFirstUpload, SharedInfo.MaximumMinutesBeforeFirstUpload));
|
|
|
|
|
|
|
|
lock (SubmissionTimer) {
|
|
|
|
SubmissionTimer.Change(startIn, TimeSpan.FromHours(SharedInfo.MinimumHoursBetweenUploads));
|
|
|
|
}
|
|
|
|
|
2021-04-25 17:55:30 +00:00
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginInitializedAndEnabled, nameof(SteamTokenDumperPlugin), startIn.ToHumanReadable()));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async void OnBotDestroy(Bot bot) {
|
|
|
|
if (bot == null) {
|
|
|
|
throw new ArgumentNullException(nameof(bot));
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:41:01 +00:00
|
|
|
if (BotSubscriptions.TryRemove(bot, out IDisposable? subscription)) {
|
2020-06-13 10:08:21 +00:00
|
|
|
subscription.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BotSynchronizations.TryRemove(bot, out (SemaphoreSlim RefreshSemaphore, Timer RefreshTimer) synchronization)) {
|
|
|
|
synchronization.RefreshSemaphore.Dispose();
|
|
|
|
|
|
|
|
await synchronization.RefreshTimer.DisposeAsync().ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async void OnBotInit(Bot bot) {
|
|
|
|
if (bot == null) {
|
|
|
|
throw new ArgumentNullException(nameof(bot));
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (Config == null) {
|
|
|
|
throw new InvalidOperationException(nameof(Config));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config.Enabled) {
|
2020-06-13 10:08:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
SemaphoreSlim refreshSemaphore = new(1, 1);
|
|
|
|
Timer refreshTimer = new(async _ => await Refresh(bot).ConfigureAwait(false));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
if (!BotSynchronizations.TryAdd(bot, (refreshSemaphore, refreshTimer))) {
|
|
|
|
refreshSemaphore.Dispose();
|
|
|
|
|
|
|
|
await refreshTimer.DisposeAsync().ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnBotSteamCallbacksInit(Bot bot, CallbackManager callbackManager) {
|
2020-11-14 21:37:00 +00:00
|
|
|
if (bot == null) {
|
|
|
|
throw new ArgumentNullException(nameof(bot));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callbackManager == null) {
|
|
|
|
throw new ArgumentNullException(nameof(callbackManager));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:41:01 +00:00
|
|
|
if (BotSubscriptions.TryRemove(bot, out IDisposable? subscription)) {
|
2020-06-13 10:08:21 +00:00
|
|
|
subscription.Dispose();
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (Config == null) {
|
|
|
|
throw new InvalidOperationException(nameof(Config));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config.Enabled) {
|
2020-06-13 10:08:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription = callbackManager.Subscribe<SteamApps.LicenseListCallback>(callback => OnLicenseList(bot, callback));
|
|
|
|
|
|
|
|
if (!BotSubscriptions.TryAdd(bot, subscription)) {
|
|
|
|
subscription.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:41:01 +00:00
|
|
|
public IReadOnlyCollection<ClientMsgHandler>? OnBotSteamHandlersInit(Bot bot) => null;
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2020-06-13 13:09:12 +00:00
|
|
|
public override void OnLoaded() { }
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-01-03 21:02:24 +00:00
|
|
|
public void OnPICSChanges(uint currentChangeNumber, IReadOnlyDictionary<uint, SteamApps.PICSChangesCallback.PICSChangeData> appChanges, IReadOnlyDictionary<uint, SteamApps.PICSChangesCallback.PICSChangeData> packageChanges) {
|
2020-11-14 21:37:00 +00:00
|
|
|
if (currentChangeNumber == 0) {
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(currentChangeNumber));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appChanges == null) {
|
|
|
|
throw new ArgumentNullException(nameof(appChanges));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packageChanges == null) {
|
|
|
|
throw new ArgumentNullException(nameof(packageChanges));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (Config == null) {
|
|
|
|
throw new InvalidOperationException(nameof(Config));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config.Enabled) {
|
2020-06-13 10:08:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GlobalCache == null) {
|
2020-11-15 23:44:37 +00:00
|
|
|
throw new InvalidOperationException(nameof(GlobalCache));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 21:02:24 +00:00
|
|
|
GlobalCache.OnPICSChanges(currentChangeNumber, appChanges);
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 21:02:24 +00:00
|
|
|
public void OnPICSChangesRestart(uint currentChangeNumber) {
|
2020-06-13 10:08:21 +00:00
|
|
|
if (currentChangeNumber == 0) {
|
2020-11-14 21:37:00 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(currentChangeNumber));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (Config == null) {
|
|
|
|
throw new InvalidOperationException(nameof(Config));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config.Enabled) {
|
2020-06-13 10:08:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GlobalCache == null) {
|
2020-11-14 21:37:00 +00:00
|
|
|
throw new InvalidOperationException(nameof(GlobalCache));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 21:02:24 +00:00
|
|
|
GlobalCache.OnPICSChangesRestart(currentChangeNumber);
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:41:01 +00:00
|
|
|
private static async void OnLicenseList(Bot bot, SteamApps.LicenseListCallback callback) {
|
2020-11-14 21:37:00 +00:00
|
|
|
if (bot == null) {
|
|
|
|
throw new ArgumentNullException(nameof(bot));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback == null) {
|
2020-06-13 10:08:21 +00:00
|
|
|
throw new ArgumentNullException(nameof(callback));
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (Config == null) {
|
|
|
|
throw new InvalidOperationException(nameof(Config));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config.Enabled) {
|
2020-06-13 10:08:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GlobalCache == null) {
|
2020-11-14 21:37:00 +00:00
|
|
|
throw new InvalidOperationException(nameof(GlobalCache));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
Dictionary<uint, ulong> packageTokens = callback.LicenseList.Where(license => !Config.SecretPackageIDs.Contains(license.PackageID)).GroupBy(license => license.PackageID).ToDictionary(group => group.Key, group => group.OrderByDescending(license => license.TimeCreated).First().AccessToken);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-01-03 21:02:24 +00:00
|
|
|
GlobalCache.UpdatePackageTokens(packageTokens);
|
|
|
|
|
2020-06-13 10:08:21 +00:00
|
|
|
await Refresh(bot, packageTokens.Keys).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:41:01 +00:00
|
|
|
private static async Task Refresh(Bot bot, IReadOnlyCollection<uint>? packageIDs = null) {
|
2020-06-13 10:08:21 +00:00
|
|
|
if (bot == null) {
|
|
|
|
throw new ArgumentNullException(nameof(bot));
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (Config == null) {
|
|
|
|
throw new InvalidOperationException(nameof(Config));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config.Enabled) {
|
2020-06-13 10:08:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
if (GlobalCache == null) {
|
|
|
|
throw new InvalidOperationException(nameof(GlobalCache));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ASF.GlobalDatabase == null) {
|
|
|
|
throw new InvalidOperationException(nameof(GlobalCache));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!BotSynchronizations.TryGetValue(bot, out (SemaphoreSlim RefreshSemaphore, Timer RefreshTimer) synchronization)) {
|
2020-11-15 23:44:37 +00:00
|
|
|
throw new InvalidOperationException(nameof(synchronization));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!await synchronization.RefreshSemaphore.WaitAsync(0).ConfigureAwait(false)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!bot.IsConnectedAndLoggedOn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
packageIDs ??= bot.OwnedPackageIDsReadOnly;
|
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
HashSet<uint> appIDsToRefresh = new();
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
foreach (uint packageID in packageIDs.Where(packageID => !Config.SecretPackageIDs.Contains(packageID))) {
|
2021-01-03 20:37:16 +00:00
|
|
|
if (!ASF.GlobalDatabase.PackagesDataReadOnly.TryGetValue(packageID, out (uint ChangeNumber, ImmutableHashSet<uint>? AppIDs) packageData) || (packageData.AppIDs == null)) {
|
2020-06-13 10:08:21 +00:00
|
|
|
// ASF might not have the package info for us at the moment, we'll retry later
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
appIDsToRefresh.UnionWith(packageData.AppIDs.Where(appID => !Config.SecretAppIDs.Contains(appID) && GlobalCache.ShouldRefreshAppInfo(appID)));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (appIDsToRefresh.Count == 0) {
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericDebug(Strings.BotNoAppsToRefresh);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotRetrievingTotalAppAccessTokens, appIDsToRefresh.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
HashSet<uint> appIDsThisRound = new(Math.Min(appIDsToRefresh.Count, SharedInfo.AppInfosPerSingleRequest));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
using (HashSet<uint>.Enumerator enumerator = appIDsToRefresh.GetEnumerator()) {
|
|
|
|
while (true) {
|
2020-06-15 14:25:28 +00:00
|
|
|
while ((appIDsThisRound.Count < SharedInfo.AppInfosPerSingleRequest) && enumerator.MoveNext()) {
|
2020-06-13 10:08:21 +00:00
|
|
|
appIDsThisRound.Add(enumerator.Current);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appIDsThisRound.Count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-20 15:12:18 +00:00
|
|
|
if (!bot.IsConnectedAndLoggedOn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotRetrievingAppAccessTokens, appIDsThisRound.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
SteamApps.PICSTokensCallback response;
|
|
|
|
|
|
|
|
try {
|
2020-06-15 14:55:57 +00:00
|
|
|
response = await bot.SteamApps.PICSGetAccessTokens(appIDsThisRound, Enumerable.Empty<uint>()).ToLongRunningTask().ConfigureAwait(false);
|
2020-06-13 10:08:21 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
bot.ArchiLogger.LogGenericWarningException(e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotFinishedRetrievingAppAccessTokens, appIDsThisRound.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
appIDsThisRound.Clear();
|
|
|
|
|
2021-01-03 21:02:24 +00:00
|
|
|
GlobalCache.UpdateAppTokens(response.AppTokens, response.AppTokensDenied);
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotFinishedRetrievingTotalAppAccessTokens, appIDsToRefresh.Count));
|
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotRetrievingTotalDepots, appIDsToRefresh.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
using (HashSet<uint>.Enumerator enumerator = appIDsToRefresh.GetEnumerator()) {
|
|
|
|
while (true) {
|
2020-06-15 14:25:28 +00:00
|
|
|
while ((appIDsThisRound.Count < SharedInfo.AppInfosPerSingleRequest) && enumerator.MoveNext()) {
|
2020-06-13 10:08:21 +00:00
|
|
|
appIDsThisRound.Add(enumerator.Current);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appIDsThisRound.Count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-20 15:12:18 +00:00
|
|
|
if (!bot.IsConnectedAndLoggedOn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotRetrievingAppInfos, appIDsThisRound.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
AsyncJobMultiple<SteamApps.PICSProductInfoCallback>.ResultSet response;
|
|
|
|
|
|
|
|
try {
|
2020-06-15 14:55:57 +00:00
|
|
|
response = await bot.SteamApps.PICSGetProductInfo(appIDsThisRound.Select(appID => new SteamApps.PICSRequest { ID = appID, AccessToken = GlobalCache.GetAppToken(appID), Public = false }), Enumerable.Empty<SteamApps.PICSRequest>()).ToLongRunningTask().ConfigureAwait(false);
|
2020-06-13 10:08:21 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
bot.ArchiLogger.LogGenericWarningException(e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.Results == null) {
|
2021-04-25 17:55:30 +00:00
|
|
|
bot.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, ArchiSteamFarm.Localization.Strings.WarningFailedWithError, nameof(response.Results)));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotFinishedRetrievingAppInfos, appIDsThisRound.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
appIDsThisRound.Clear();
|
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
Dictionary<uint, uint> appChangeNumbers = new();
|
2020-06-15 14:25:28 +00:00
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
HashSet<Task<SteamApps.DepotKeyCallback>> depotTasks = new();
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
foreach (SteamApps.PICSProductInfoCallback.PICSProductInfo app in response.Results.SelectMany(result => result.Apps.Values)) {
|
|
|
|
appChangeNumbers[app.ID] = app.ChangeNumber;
|
|
|
|
|
|
|
|
if (GlobalCache.ShouldRefreshDepotKey(app.ID)) {
|
|
|
|
depotTasks.Add(bot.SteamApps.GetDepotDecryptionKey(app.ID, app.ID).ToLongRunningTask());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (KeyValue depot in app.KeyValues["depots"].Children) {
|
2021-05-02 23:08:12 +00:00
|
|
|
if (uint.TryParse(depot.Name, out uint depotID) && !Config.SecretDepotIDs.Contains(depotID) && GlobalCache.ShouldRefreshDepotKey(depotID)) {
|
2020-06-13 10:08:21 +00:00
|
|
|
depotTasks.Add(bot.SteamApps.GetDepotDecryptionKey(depotID, app.ID).ToLongRunningTask());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 21:02:24 +00:00
|
|
|
GlobalCache.UpdateAppChangeNumbers(appChangeNumbers);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
if (depotTasks.Count > 0) {
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotRetrievingDepotKeys, depotTasks.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2020-07-15 14:43:59 +00:00
|
|
|
IList<SteamApps.DepotKeyCallback> results;
|
2020-06-25 15:53:37 +00:00
|
|
|
|
|
|
|
try {
|
2020-07-15 14:43:59 +00:00
|
|
|
results = await Utilities.InParallel(depotTasks).ConfigureAwait(false);
|
2020-06-25 15:53:37 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
bot.ArchiLogger.LogGenericWarningException(e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotFinishedRetrievingDepotKeys, depotTasks.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-01-03 21:02:24 +00:00
|
|
|
GlobalCache.UpdateDepotKeys(results);
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotFinishedRetrievingTotalDepots, appIDsToRefresh.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
} finally {
|
|
|
|
TimeSpan timeSpan = TimeSpan.FromHours(SharedInfo.MaximumHoursBetweenRefresh);
|
|
|
|
|
|
|
|
synchronization.RefreshTimer.Change(timeSpan, timeSpan);
|
|
|
|
synchronization.RefreshSemaphore.Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
private static async void SubmitData(object? state) {
|
2020-08-22 19:41:01 +00:00
|
|
|
if (Bot.Bots == null) {
|
2020-11-14 21:37:00 +00:00
|
|
|
throw new InvalidOperationException(nameof(Bot.Bots));
|
2020-08-22 19:41:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (Config == null) {
|
|
|
|
throw new InvalidOperationException(nameof(Config));
|
|
|
|
}
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
if (!Config.Enabled) {
|
2020-06-13 10:08:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:08:12 +00:00
|
|
|
const string request = SharedInfo.ServerURL + "/submit";
|
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
if (GlobalCache == null) {
|
|
|
|
throw new InvalidOperationException(nameof(GlobalCache));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ASF.GlobalConfig == null) {
|
|
|
|
throw new InvalidOperationException(nameof(ASF.GlobalConfig));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ASF.WebBrowser == null) {
|
|
|
|
throw new InvalidOperationException(nameof(ASF.WebBrowser));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!await SubmissionSemaphore.WaitAsync(0).ConfigureAwait(false)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Dictionary<uint, ulong> appTokens = GlobalCache.GetAppTokensForSubmission();
|
|
|
|
Dictionary<uint, ulong> packageTokens = GlobalCache.GetPackageTokensForSubmission();
|
|
|
|
Dictionary<uint, string> depotKeys = GlobalCache.GetDepotKeysForSubmission();
|
|
|
|
|
|
|
|
if ((appTokens.Count == 0) && (packageTokens.Count == 0) && (depotKeys.Count == 0)) {
|
2021-04-25 21:44:47 +00:00
|
|
|
ASF.ArchiLogger.LogGenericInfo(Strings.SubmissionNoNewData);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:41:01 +00:00
|
|
|
ulong contributorSteamID = (ASF.GlobalConfig.SteamOwnerID > 0) && new SteamID(ASF.GlobalConfig.SteamOwnerID).IsIndividualAccount ? ASF.GlobalConfig.SteamOwnerID : Bot.Bots.Values.Where(bot => bot.SteamID > 0).OrderByDescending(bot => bot.OwnedPackageIDsReadOnly.Count).FirstOrDefault()?.SteamID ?? 0;
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
if (contributorSteamID == 0) {
|
2021-04-25 21:44:47 +00:00
|
|
|
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.SubmissionNoContributorSet, nameof(ASF.GlobalConfig.SteamOwnerID)));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:37:00 +00:00
|
|
|
RequestData requestData = new(contributorSteamID, appTokens, packageTokens, depotKeys);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.SubmissionInProgress, appTokens.Count, packageTokens.Count, depotKeys.Count));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2020-09-13 19:43:25 +00:00
|
|
|
WebBrowser.ObjectResponse<ResponseData>? response = await ASF.WebBrowser.UrlPostToJsonObject<ResponseData, RequestData>(request, data: requestData, requestOptions: WebBrowser.ERequestOptions.ReturnClientErrors).ConfigureAwait(false);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-01-04 16:42:31 +00:00
|
|
|
if (response == null) {
|
2021-04-25 17:55:30 +00:00
|
|
|
ASF.ArchiLogger.LogGenericWarning(ArchiSteamFarm.Localization.Strings.WarningFailed);
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-01-04 16:42:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.StatusCode.IsClientErrorCode()) {
|
2021-04-25 17:55:30 +00:00
|
|
|
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, ArchiSteamFarm.Localization.Strings.WarningFailedWithError, response.StatusCode));
|
2021-01-04 16:42:31 +00:00
|
|
|
|
2020-06-13 10:08:21 +00:00
|
|
|
#if NETFRAMEWORK
|
2021-01-04 16:42:31 +00:00
|
|
|
if (response.StatusCode == (HttpStatusCode) 429) {
|
2020-06-13 10:08:21 +00:00
|
|
|
#else
|
2021-01-04 16:42:31 +00:00
|
|
|
if (response.StatusCode == HttpStatusCode.TooManyRequests) {
|
2020-06-13 10:08:21 +00:00
|
|
|
#endif
|
|
|
|
TimeSpan startIn = TimeSpan.FromMinutes(Utilities.RandomNext(SharedInfo.MinimumMinutesBeforeFirstUpload, SharedInfo.MaximumMinutesBeforeFirstUpload));
|
|
|
|
|
|
|
|
lock (SubmissionTimer) {
|
|
|
|
SubmissionTimer.Change(startIn, TimeSpan.FromHours(SharedInfo.MinimumHoursBetweenUploads));
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.SubmissionFailedTooManyRequests, startIn.ToHumanReadable()));
|
2020-06-13 10:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:25:29 +00:00
|
|
|
if (!response.Content.Success) {
|
2021-04-25 21:44:47 +00:00
|
|
|
ASF.ArchiLogger.LogGenericError(ArchiSteamFarm.Localization.Strings.WarningFailed);
|
2021-02-22 17:25:29 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:56:03 +00:00
|
|
|
if (response.Content.Data == null) {
|
2021-04-25 17:55:30 +00:00
|
|
|
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, ArchiSteamFarm.Localization.Strings.ErrorIsInvalid), nameof(response.Content.Data));
|
2021-02-22 16:56:03 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:44:47 +00:00
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.SubmissionSuccessful, response.Content.Data.NewAppsCount, response.Content.Data.NewSubsCount, response.Content.Data.NewDepotsCount));
|
2020-06-13 10:08:21 +00:00
|
|
|
|
2021-01-03 21:02:24 +00:00
|
|
|
GlobalCache.UpdateSubmittedData(appTokens, packageTokens, depotKeys);
|
2020-06-13 10:08:21 +00:00
|
|
|
} finally {
|
|
|
|
SubmissionSemaphore.Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|