Files
ArchiSteamFarm/ArchiSteamFarm/GlobalDatabase.cs

233 lines
7.3 KiB
C#
Raw Permalink Normal View History

2019-02-16 17:34:17 +01:00
// _ _ _ ____ _ _____
2017-11-18 17:27:06 +01:00
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
2019-01-14 19:11:17 +01:00
// |
2020-02-01 23:33:35 +01:00
// Copyright 2015-2020 Ł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;
2016-03-07 02:39:55 +01:00
using System.IO;
2017-07-23 10:27:20 +02:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ArchiSteamFarm.Helpers;
2019-04-04 22:34:58 +02:00
using ArchiSteamFarm.Localization;
2018-09-08 01:03:55 +02:00
using ArchiSteamFarm.SteamKit2;
2019-01-10 23:44:32 +01:00
using JetBrains.Annotations;
using Newtonsoft.Json;
2016-03-07 02:39:55 +01:00
namespace ArchiSteamFarm {
public sealed class GlobalDatabase : SerializableFile {
2017-07-23 10:27:20 +02:00
[JsonProperty(Required = Required.DisallowNull)]
[PublicAPI]
2019-01-17 20:43:45 +01:00
public readonly Guid Guid = Guid.NewGuid();
2017-07-23 10:27:20 +02:00
[JsonIgnore]
[PublicAPI]
public IReadOnlyDictionary<uint, ulong> PackageAccessTokensReadOnly => PackagesAccessTokens;
2020-06-07 20:00:57 +02:00
[JsonIgnore]
[PublicAPI]
public IReadOnlyDictionary<uint, (uint ChangeNumber, HashSet<uint> AppIDs)> PackagesDataReadOnly => PackagesData;
2016-12-07 14:05:19 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-07-21 14:25:25 +02:00
internal readonly InMemoryServerListProvider ServerListProvider = new InMemoryServerListProvider();
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, ulong> PackagesAccessTokens = new ConcurrentDictionary<uint, ulong>();
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, (uint ChangeNumber, HashSet<uint> AppIDs)> PackagesData = new ConcurrentDictionary<uint, (uint ChangeNumber, HashSet<uint> AppIDs)>();
2017-08-01 12:41:57 +02:00
private readonly SemaphoreSlim PackagesRefreshSemaphore = new SemaphoreSlim(1, 1);
2017-07-23 10:27:20 +02:00
internal uint CellID {
get => BackingCellID;
set {
if (BackingCellID == value) {
return;
}
BackingCellID = value;
Utilities.InBackground(Save);
}
}
2020-06-07 21:06:41 +02:00
[JsonProperty(PropertyName = "_" + nameof(CellID), Required = Required.DisallowNull)]
private uint BackingCellID;
2016-06-27 18:48:25 +02:00
2019-01-10 23:44:32 +01:00
private GlobalDatabase([NotNull] string filePath) : this() {
if (string.IsNullOrEmpty(filePath)) {
throw new ArgumentNullException(nameof(filePath));
}
FilePath = filePath;
}
2019-04-04 17:56:38 +02:00
[JsonConstructor]
2017-07-21 14:25:25 +02:00
private GlobalDatabase() => ServerListProvider.ServerListUpdated += OnServerListUpdated;
public override void Dispose() {
2017-08-04 19:26:37 +02:00
// Events we registered
ServerListProvider.ServerListUpdated -= OnServerListUpdated;
// Those are objects that are always being created if constructor doesn't throw exception
PackagesRefreshSemaphore.Dispose();
// Base dispose
base.Dispose();
2017-08-04 19:26:37 +02:00
}
2019-01-10 23:44:32 +01:00
[ItemCanBeNull]
2018-10-07 03:21:32 +02:00
internal static async Task<GlobalDatabase> CreateOrLoad(string filePath) {
2016-06-27 18:48:25 +02:00
if (string.IsNullOrEmpty(filePath)) {
ASF.ArchiLogger.LogNullError(nameof(filePath));
2018-12-15 00:27:15 +01:00
2016-06-27 18:48:25 +02:00
return null;
}
if (!File.Exists(filePath)) {
return new GlobalDatabase(filePath);
}
GlobalDatabase globalDatabase;
try {
2019-04-04 22:34:58 +02:00
string json = await RuntimeCompatibility.File.ReadAllTextAsync(filePath).ConfigureAwait(false);
if (string.IsNullOrEmpty(json)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsEmpty, nameof(json)));
return null;
}
globalDatabase = JsonConvert.DeserializeObject<GlobalDatabase>(json);
2016-06-27 18:48:25 +02:00
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
2018-12-15 00:27:15 +01:00
2016-06-27 18:48:25 +02:00
return null;
}
if (globalDatabase == null) {
ASF.ArchiLogger.LogNullError(nameof(globalDatabase));
2018-12-15 00:27:15 +01:00
2016-06-27 18:48:25 +02:00
return null;
}
globalDatabase.FilePath = filePath;
2018-12-15 00:27:15 +01:00
2016-06-27 18:48:25 +02:00
return globalDatabase;
}
internal HashSet<uint> GetPackageIDs(uint appID, IEnumerable<uint> packageIDs) {
if ((appID == 0) || (packageIDs == null)) {
2019-06-30 23:33:50 +02:00
ASF.ArchiLogger.LogNullError(nameof(appID) + " || " + nameof(packageIDs));
2018-12-15 00:27:15 +01:00
2018-10-07 03:21:32 +02:00
return null;
}
2019-06-30 23:33:50 +02:00
HashSet<uint> result = new HashSet<uint>();
foreach (uint packageID in packageIDs.Where(packageID => packageID != 0)) {
if (!PackagesData.TryGetValue(packageID, out (uint ChangeNumber, HashSet<uint> AppIDs) packagesData) || (packagesData.AppIDs?.Contains(appID) != true)) {
2019-06-30 23:33:50 +02:00
continue;
}
result.Add(packageID);
}
return result;
2018-10-07 03:21:32 +02:00
}
internal void RefreshPackageAccessTokens(IReadOnlyDictionary<uint, ulong> packageAccessTokens) {
if ((packageAccessTokens == null) || (packageAccessTokens.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(packageAccessTokens));
return;
}
bool save = false;
foreach ((uint packageID, ulong currentAccessToken) in packageAccessTokens) {
if (!PackagesAccessTokens.TryGetValue(packageID, out ulong previousAccessToken) || (previousAccessToken != currentAccessToken)) {
PackagesAccessTokens[packageID] = currentAccessToken;
save = true;
}
}
if (save) {
Utilities.InBackground(Save);
}
}
2017-12-30 21:29:24 +01:00
internal async Task RefreshPackages(Bot bot, IReadOnlyDictionary<uint, uint> packages) {
2017-12-30 21:28:01 +01:00
if ((bot == null) || (packages == null) || (packages.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(bot) + " || " + nameof(packages));
2018-12-15 00:27:15 +01:00
2017-07-23 10:27:20 +02:00
return;
}
await PackagesRefreshSemaphore.WaitAsync().ConfigureAwait(false);
try {
HashSet<uint> packageIDs = packages.Where(package => (package.Key != 0) && (!PackagesData.TryGetValue(package.Key, out (uint ChangeNumber, HashSet<uint> AppIDs) packageData) || (packageData.ChangeNumber < package.Value))).Select(package => package.Key).ToHashSet();
2017-07-23 10:27:20 +02:00
2018-01-02 08:11:35 +01:00
if (packageIDs.Count == 0) {
return;
}
2017-12-30 21:28:01 +01:00
Dictionary<uint, (uint ChangeNumber, HashSet<uint> AppIDs)> packagesData = await bot.GetPackagesData(packageIDs).ConfigureAwait(false);
2018-12-15 00:27:15 +01:00
if (packagesData == null) {
2019-06-30 23:33:50 +02:00
bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed);
2017-07-23 10:27:20 +02:00
return;
}
bool save = false;
foreach ((uint packageID, (uint ChangeNumber, HashSet<uint> AppIDs) packageData) in packagesData) {
if (PackagesData.TryGetValue(packageID, out (uint ChangeNumber, HashSet<uint> AppIDs) previousData) && (packageData.ChangeNumber < previousData.ChangeNumber)) {
continue;
}
PackagesData[packageID] = packageData;
save = true;
2017-07-23 10:27:20 +02:00
}
if (save) {
Utilities.InBackground(Save);
}
2017-07-23 10:27:20 +02:00
} finally {
PackagesRefreshSemaphore.Release();
}
}
2017-08-04 19:26:37 +02:00
private async void OnServerListUpdated(object sender, EventArgs e) => await Save().ConfigureAwait(false);
// ReSharper disable UnusedMember.Global
public bool ShouldSerializeCellID() => CellID != 0;
public bool ShouldSerializePackagesData() => PackagesData.Count > 0;
public bool ShouldSerializeServerListProvider() => ServerListProvider.ShouldSerializeServerRecords();
// ReSharper restore UnusedMember.Global
2016-06-27 18:48:25 +02:00
}
2018-09-08 00:46:40 +02:00
}