2019-02-16 17:34:17 +01:00
|
|
|
// _ _ _ ____ _ _____
|
2017-11-18 17:27:06 +01:00
|
|
|
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
|
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
|
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
|
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
2019-01-14 19:11:17 +01:00
|
|
|
// |
|
2022-01-06 20:22:38 +01:00
|
|
|
// Copyright 2015-2022 Łukasz "JustArchi" Domeradzki
|
2018-07-27 04:52:14 +02:00
|
|
|
// Contact: JustArchi@JustArchi.net
|
2019-01-14 19:11:17 +01:00
|
|
|
// |
|
2018-07-27 04:52:14 +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
|
2019-01-14 19:11:17 +01:00
|
|
|
// |
|
2018-07-27 04:52:14 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2019-01-14 19:11:17 +01:00
|
|
|
// |
|
2018-07-27 04:52:14 +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.
|
2016-03-07 02:39:55 +01:00
|
|
|
|
|
|
|
|
using System;
|
2017-07-23 10:27:20 +02:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
2021-01-03 21:37:16 +01:00
|
|
|
using System.Collections.Immutable;
|
2020-11-14 22:37:00 +01:00
|
|
|
using System.Globalization;
|
2021-11-09 16:33:09 +01:00
|
|
|
using System.IO;
|
2017-07-23 10:27:20 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-02-11 00:05:43 +01:00
|
|
|
using ArchiSteamFarm.Collections;
|
2021-05-08 01:37:22 +02:00
|
|
|
using ArchiSteamFarm.Core;
|
2019-07-25 17:09:20 +02:00
|
|
|
using ArchiSteamFarm.Helpers;
|
2019-04-04 22:34:58 +02:00
|
|
|
using ArchiSteamFarm.Localization;
|
2021-05-08 01:37:22 +02:00
|
|
|
using ArchiSteamFarm.Steam;
|
|
|
|
|
using ArchiSteamFarm.Steam.SteamKit2;
|
2019-01-10 23:44:32 +01:00
|
|
|
using JetBrains.Annotations;
|
2016-11-24 07:32:16 +01:00
|
|
|
using Newtonsoft.Json;
|
2021-06-18 20:56:32 +02:00
|
|
|
using Newtonsoft.Json.Linq;
|
2021-05-06 20:16:06 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
namespace ArchiSteamFarm.Storage;
|
2020-06-09 22:56:04 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
public sealed class GlobalDatabase : SerializableFile {
|
|
|
|
|
[JsonIgnore]
|
|
|
|
|
[PublicAPI]
|
|
|
|
|
public IReadOnlyDictionary<uint, ulong> PackageAccessTokensReadOnly => PackagesAccessTokens;
|
2016-12-07 14:05:19 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[JsonIgnore]
|
|
|
|
|
[PublicAPI]
|
|
|
|
|
public IReadOnlyDictionary<uint, (uint ChangeNumber, ImmutableHashSet<uint>? AppIDs)> PackagesDataReadOnly => PackagesData;
|
2016-11-24 07:32:16 +01:00
|
|
|
|
2022-02-11 00:05:43 +01:00
|
|
|
[JsonProperty(Required = Required.DisallowNull)]
|
|
|
|
|
internal readonly ObservableConcurrentDictionary<uint, byte> CardCountsPerGame = new();
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[JsonProperty(Required = Required.DisallowNull)]
|
|
|
|
|
internal readonly InMemoryServerListProvider ServerListProvider = new();
|
2021-06-18 20:56:32 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[JsonProperty(Required = Required.DisallowNull)]
|
|
|
|
|
private readonly ConcurrentDictionary<string, JToken> KeyValueJsonStorage = new();
|
2020-06-09 22:56:04 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[JsonProperty(Required = Required.DisallowNull)]
|
|
|
|
|
private readonly ConcurrentDictionary<uint, ulong> PackagesAccessTokens = new();
|
2020-06-07 20:00:06 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[JsonProperty(Required = Required.DisallowNull)]
|
|
|
|
|
private readonly ConcurrentDictionary<uint, (uint ChangeNumber, ImmutableHashSet<uint>? AppIDs)> PackagesData = new();
|
2017-07-23 10:27:20 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
private readonly SemaphoreSlim PackagesRefreshSemaphore = new(1, 1);
|
2020-11-29 16:03:25 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[JsonProperty(Required = Required.DisallowNull)]
|
|
|
|
|
[PublicAPI]
|
|
|
|
|
public Guid Identifier { get; private set; } = Guid.NewGuid();
|
2019-07-25 17:09:20 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
internal uint CellID {
|
|
|
|
|
get => BackingCellID;
|
2016-07-08 07:41:36 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
set {
|
|
|
|
|
if (BackingCellID == value) {
|
|
|
|
|
return;
|
2019-07-25 17:09:20 +02:00
|
|
|
}
|
2021-11-10 21:23:24 +01:00
|
|
|
|
|
|
|
|
BackingCellID = value;
|
|
|
|
|
Utilities.InBackground(Save);
|
2019-07-25 17:09:20 +02:00
|
|
|
}
|
2021-11-10 21:23:24 +01:00
|
|
|
}
|
2019-07-25 17:09:20 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
internal uint LastChangeNumber {
|
|
|
|
|
get => BackingLastChangeNumber;
|
2021-01-03 21:58:41 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
set {
|
|
|
|
|
if (BackingLastChangeNumber == value) {
|
|
|
|
|
return;
|
2021-01-03 21:58:41 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
BackingLastChangeNumber = value;
|
|
|
|
|
Utilities.InBackground(Save);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-27 18:48:25 +02:00
|
|
|
|
2022-03-22 16:33:47 +01:00
|
|
|
[JsonProperty($"_{nameof(CellID)}", Required = Required.DisallowNull)]
|
2021-11-10 21:23:24 +01:00
|
|
|
private uint BackingCellID;
|
2021-01-03 21:58:41 +01:00
|
|
|
|
2022-03-22 16:33:47 +01:00
|
|
|
[JsonProperty($"_{nameof(LastChangeNumber)}", Required = Required.DisallowNull)]
|
2021-11-10 21:23:24 +01:00
|
|
|
private uint BackingLastChangeNumber;
|
2016-11-24 07:32:16 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
private GlobalDatabase(string filePath) : this() {
|
|
|
|
|
if (string.IsNullOrEmpty(filePath)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(filePath));
|
2016-11-24 07:32:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
FilePath = filePath;
|
|
|
|
|
}
|
2016-11-24 07:32:16 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[JsonConstructor]
|
2022-02-11 00:05:43 +01:00
|
|
|
private GlobalDatabase() {
|
|
|
|
|
CardCountsPerGame.OnModified += OnObjectModified;
|
|
|
|
|
ServerListProvider.ServerListUpdated += OnObjectModified;
|
|
|
|
|
}
|
2021-06-18 23:27:51 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[PublicAPI]
|
|
|
|
|
public void DeleteFromJsonStorage(string key) {
|
|
|
|
|
if (string.IsNullOrEmpty(key)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(key));
|
|
|
|
|
}
|
2021-06-18 23:27:51 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (!KeyValueJsonStorage.TryRemove(key, out _)) {
|
|
|
|
|
return;
|
2021-06-18 23:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
Utilities.InBackground(Save);
|
|
|
|
|
}
|
2021-06-18 23:27:51 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[PublicAPI]
|
|
|
|
|
public JToken? LoadFromJsonStorage(string key) {
|
|
|
|
|
if (string.IsNullOrEmpty(key)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(key));
|
2021-06-18 23:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
return KeyValueJsonStorage.TryGetValue(key, out JToken? value) ? value : null;
|
|
|
|
|
}
|
2021-06-18 23:27:51 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[PublicAPI]
|
|
|
|
|
public void SaveToJsonStorage(string key, JToken value) {
|
|
|
|
|
if (string.IsNullOrEmpty(key)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(key));
|
|
|
|
|
}
|
2021-06-18 23:27:51 +02:00
|
|
|
|
2021-12-12 01:12:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(value);
|
2021-06-18 23:29:27 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (value.Type == JTokenType.Null) {
|
|
|
|
|
DeleteFromJsonStorage(key);
|
2021-06-18 23:29:27 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 23:27:51 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (KeyValueJsonStorage.TryGetValue(key, out JToken? currentValue) && JToken.DeepEquals(currentValue, value)) {
|
|
|
|
|
return;
|
2021-06-18 23:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
KeyValueJsonStorage[key] = value;
|
|
|
|
|
Utilities.InBackground(Save);
|
|
|
|
|
}
|
2021-09-15 12:43:25 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[UsedImplicitly]
|
|
|
|
|
public bool ShouldSerializeBackingCellID() => BackingCellID != 0;
|
2021-09-15 12:43:25 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[UsedImplicitly]
|
|
|
|
|
public bool ShouldSerializeBackingLastChangeNumber() => LastChangeNumber != 0;
|
2021-09-15 12:43:25 +02:00
|
|
|
|
2022-02-11 00:05:43 +01:00
|
|
|
[UsedImplicitly]
|
|
|
|
|
public bool ShouldSerializeCardCountsPerGame() => !CardCountsPerGame.IsEmpty;
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[UsedImplicitly]
|
|
|
|
|
public bool ShouldSerializeKeyValueJsonStorage() => !KeyValueJsonStorage.IsEmpty;
|
2021-09-15 12:43:25 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[UsedImplicitly]
|
|
|
|
|
public bool ShouldSerializePackagesAccessTokens() => !PackagesAccessTokens.IsEmpty;
|
2021-09-15 12:43:25 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[UsedImplicitly]
|
|
|
|
|
public bool ShouldSerializePackagesData() => !PackagesData.IsEmpty;
|
2021-09-15 12:43:25 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
[UsedImplicitly]
|
|
|
|
|
public bool ShouldSerializeServerListProvider() => ServerListProvider.ShouldSerializeServerRecords();
|
2017-08-04 19:26:37 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
protected override void Dispose(bool disposing) {
|
|
|
|
|
if (disposing) {
|
|
|
|
|
// Events we registered
|
2022-02-11 00:05:43 +01:00
|
|
|
CardCountsPerGame.OnModified -= OnObjectModified;
|
2021-11-10 21:23:24 +01:00
|
|
|
ServerListProvider.ServerListUpdated -= OnObjectModified;
|
2019-07-25 17:09:20 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
// Those are objects that are always being created if constructor doesn't throw exception
|
|
|
|
|
PackagesRefreshSemaphore.Dispose();
|
2017-08-04 19:26:37 +02:00
|
|
|
}
|
2016-11-24 07:32:16 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
// Base dispose
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
2020-12-24 21:30:16 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
internal static async Task<GlobalDatabase?> CreateOrLoad(string filePath) {
|
|
|
|
|
if (string.IsNullOrEmpty(filePath)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(filePath));
|
|
|
|
|
}
|
2020-12-24 21:30:16 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (!File.Exists(filePath)) {
|
|
|
|
|
GlobalDatabase result = new(filePath);
|
2016-06-27 18:48:25 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
Utilities.InBackground(result.Save);
|
2016-06-27 18:48:25 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
2019-04-04 22:34:58 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
GlobalDatabase? globalDatabase;
|
2019-04-04 22:34:58 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
try {
|
|
|
|
|
string json = await File.ReadAllTextAsync(filePath).ConfigureAwait(false);
|
2019-04-04 22:34:58 +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)));
|
2018-12-15 00:27:15 +01:00
|
|
|
|
2016-06-27 18:48:25 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
globalDatabase = JsonConvert.DeserializeObject<GlobalDatabase>(json);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
ASF.ArchiLogger.LogGenericException(e);
|
2018-12-15 00:27:15 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
2016-06-27 18:48:25 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (globalDatabase == null) {
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(globalDatabase));
|
2018-12-15 00:27:15 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
return null;
|
2016-06-27 18:48:25 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
globalDatabase.FilePath = filePath;
|
2018-10-07 03:21:32 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
return globalDatabase;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal HashSet<uint> GetPackageIDs(uint appID, IEnumerable<uint> packageIDs) {
|
|
|
|
|
if (appID == 0) {
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(appID));
|
|
|
|
|
}
|
2020-11-14 22:37:00 +01:00
|
|
|
|
2021-12-12 01:12:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(packageIDs);
|
2019-06-30 23:33:50 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
HashSet<uint> result = new();
|
2019-06-30 23:33:50 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
foreach (uint packageID in packageIDs.Where(static packageID => packageID != 0)) {
|
|
|
|
|
if (!PackagesData.TryGetValue(packageID, out (uint ChangeNumber, ImmutableHashSet<uint>? AppIDs) packagesData) || (packagesData.AppIDs?.Contains(appID) != true)) {
|
|
|
|
|
continue;
|
2019-06-30 23:33:50 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
result.Add(packageID);
|
2018-10-07 03:21:32 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
2021-02-16 09:27:15 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
internal async Task OnPICSChangesRestart(uint currentChangeNumber) {
|
|
|
|
|
if (currentChangeNumber == 0) {
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(currentChangeNumber));
|
|
|
|
|
}
|
2021-01-03 21:37:16 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (Bot.Bots == null) {
|
|
|
|
|
throw new InvalidOperationException(nameof(Bot.Bots));
|
|
|
|
|
}
|
2021-01-03 21:37:16 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (currentChangeNumber <= LastChangeNumber) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-16 19:27:00 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
LastChangeNumber = currentChangeNumber;
|
2021-01-03 21:37:16 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
Bot? refreshBot = Bot.Bots.Values.FirstOrDefault(static bot => bot.IsConnectedAndLoggedOn);
|
2021-02-16 19:27:00 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (refreshBot == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-20 17:29:13 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (PackagesData.IsEmpty) {
|
|
|
|
|
return;
|
2021-01-03 21:37:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
Dictionary<uint, uint> packageIDs = PackagesData.Keys.ToDictionary(static packageID => packageID, _ => currentChangeNumber);
|
2020-06-09 22:56:04 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
await RefreshPackages(refreshBot, packageIDs).ConfigureAwait(false);
|
|
|
|
|
}
|
2020-06-09 22:56:04 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
internal void RefreshPackageAccessTokens(IReadOnlyDictionary<uint, ulong> packageAccessTokens) {
|
|
|
|
|
if ((packageAccessTokens == null) || (packageAccessTokens.Count == 0)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(packageAccessTokens));
|
|
|
|
|
}
|
2020-06-09 22:56:04 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
bool save = false;
|
|
|
|
|
|
|
|
|
|
foreach ((uint packageID, ulong currentAccessToken) in packageAccessTokens) {
|
|
|
|
|
if (!PackagesAccessTokens.TryGetValue(packageID, out ulong previousAccessToken) || (previousAccessToken != currentAccessToken)) {
|
|
|
|
|
PackagesAccessTokens[packageID] = currentAccessToken;
|
|
|
|
|
save = true;
|
2020-06-09 22:56:04 +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 async Task RefreshPackages(Bot bot, IReadOnlyDictionary<uint, uint> packages) {
|
2021-12-12 01:12:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(bot);
|
2017-07-23 10:27:20 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if ((packages == null) || (packages.Count == 0)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(packages));
|
|
|
|
|
}
|
2017-07-23 10:27:20 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
await PackagesRefreshSemaphore.WaitAsync().ConfigureAwait(false);
|
2017-07-23 10:27:20 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
try {
|
|
|
|
|
HashSet<uint> packageIDs = packages.Where(package => (package.Key != 0) && (!PackagesData.TryGetValue(package.Key, out (uint ChangeNumber, ImmutableHashSet<uint>? AppIDs) previousData) || (previousData.ChangeNumber < package.Value))).Select(static package => package.Key).ToHashSet();
|
2018-01-02 08:11:35 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (packageIDs.Count == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-15 00:27:15 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
Dictionary<uint, (uint ChangeNumber, ImmutableHashSet<uint>? AppIDs)>? packagesData = await bot.GetPackagesData(packageIDs).ConfigureAwait(false);
|
2019-06-30 23:33:50 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (packagesData == null) {
|
|
|
|
|
bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed);
|
2017-07-23 10:27:20 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2020-06-10 22:43:06 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
bool save = false;
|
2020-06-10 22:43:06 +02:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
foreach ((uint packageID, (uint ChangeNumber, ImmutableHashSet<uint>? AppIDs) packageData) in packagesData) {
|
|
|
|
|
if (PackagesData.TryGetValue(packageID, out (uint ChangeNumber, ImmutableHashSet<uint>? AppIDs) previousData) && (packageData.ChangeNumber <= previousData.ChangeNumber)) {
|
|
|
|
|
continue;
|
2017-07-23 10:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
PackagesData[packageID] = packageData;
|
|
|
|
|
save = true;
|
2017-07-23 10:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
if (save) {
|
|
|
|
|
Utilities.InBackground(Save);
|
2020-12-01 23:32:00 +01:00
|
|
|
}
|
2021-11-10 21:23:24 +01:00
|
|
|
} finally {
|
|
|
|
|
PackagesRefreshSemaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-01 23:32:00 +01:00
|
|
|
|
2021-11-10 21:23:24 +01:00
|
|
|
private async void OnObjectModified(object? sender, EventArgs e) {
|
|
|
|
|
if (string.IsNullOrEmpty(FilePath)) {
|
|
|
|
|
return;
|
2020-12-01 23:32:00 +01:00
|
|
|
}
|
2021-11-10 21:23:24 +01:00
|
|
|
|
|
|
|
|
await Save().ConfigureAwait(false);
|
2016-06-27 18:48:25 +02:00
|
|
|
}
|
2018-09-08 00:46:40 +02:00
|
|
|
}
|