Files
ArchiSteamFarm/ArchiSteamFarm/GlobalDatabase.cs

196 lines
6.4 KiB
C#
Raw Normal View History

2017-11-18 17:27:06 +01:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
2019-01-14 19:11:17 +01:00
// |
2019-01-02 16:32:53 +01:00
// Copyright 2015-2019 Ł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;
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 {
2019-01-17 20:43:45 +01:00
public sealed class GlobalDatabase : IDisposable {
2017-07-23 10:27:20 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-17 20:43:45 +01:00
public readonly Guid Guid = Guid.NewGuid();
2017-07-23 10:27:20 +02:00
2016-12-07 14:05:19 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-12-30 21:28:01 +01:00
internal readonly ConcurrentDictionary<uint, (uint ChangeNumber, HashSet<uint> AppIDs)> PackagesData = new ConcurrentDictionary<uint, (uint ChangeNumber, HashSet<uint> AppIDs)>();
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();
2017-08-04 19:26:37 +02:00
private readonly SemaphoreSlim FileSemaphore = new SemaphoreSlim(1, 1);
2017-08-01 12:41:57 +02:00
private readonly SemaphoreSlim PackagesRefreshSemaphore = new SemaphoreSlim(1, 1);
2017-07-23 10:27:20 +02:00
2017-08-04 19:26:37 +02:00
[JsonProperty(PropertyName = "_CellID", Required = Required.DisallowNull)]
internal uint CellID { get; private set; }
2016-06-27 18:48:25 +02:00
private string FilePath;
// This constructor is used when creating new database
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;
}
// This constructor is used only by deserializer
2017-07-21 14:25:25 +02:00
private GlobalDatabase() => ServerListProvider.ServerListUpdated += OnServerListUpdated;
2017-08-04 19:26:37 +02:00
public void Dispose() {
// Events we registered
ServerListProvider.ServerListUpdated -= OnServerListUpdated;
// Those are objects that are always being created if constructor doesn't throw exception
FileSemaphore.Dispose();
PackagesRefreshSemaphore.Dispose();
}
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 {
globalDatabase = JsonConvert.DeserializeObject<GlobalDatabase>(await RuntimeCompatibility.File.ReadAllTextAsync(filePath).ConfigureAwait(false));
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;
}
2018-10-07 03:21:32 +02:00
internal HashSet<uint> GetPackageIDs(uint appID) {
if (appID == 0) {
ASF.ArchiLogger.LogNullError(nameof(appID));
2018-12-15 00:27:15 +01:00
2018-10-07 03:21:32 +02:00
return null;
}
return PackagesData.Where(package => package.Value.AppIDs?.Contains(appID) == true).Select(package => package.Key).ToHashSet();
}
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 {
2018-06-09 00:45:15 +02:00
HashSet<uint> packageIDs = packages.Where(package => (package.Key != 0) && (!PackagesData.TryGetValue(package.Key, out (uint ChangeNumber, HashSet<uint> _) 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
2017-12-30 21:28:01 +01:00
if ((packagesData == null) || (packagesData.Count == 0)) {
2017-07-23 10:27:20 +02:00
return;
}
foreach ((uint packageID, (uint ChangeNumber, HashSet<uint> AppIDs) package) in packagesData) {
PackagesData[packageID] = package;
2017-07-23 10:27:20 +02:00
}
2017-08-04 19:26:37 +02:00
await Save().ConfigureAwait(false);
2017-07-23 10:27:20 +02:00
} finally {
PackagesRefreshSemaphore.Release();
}
}
2017-08-04 19:26:37 +02:00
internal async Task SetCellID(uint value = 0) {
if (value == CellID) {
return;
}
CellID = value;
await Save().ConfigureAwait(false);
}
2016-08-19 05:25:08 +02:00
2017-08-04 19:26:37 +02:00
private async void OnServerListUpdated(object sender, EventArgs e) => await Save().ConfigureAwait(false);
private async Task Save() {
2017-07-21 14:25:25 +02:00
string json = JsonConvert.SerializeObject(this);
2018-12-15 00:27:15 +01:00
2016-06-27 18:48:25 +02:00
if (string.IsNullOrEmpty(json)) {
ASF.ArchiLogger.LogNullError(nameof(json));
2018-12-15 00:27:15 +01:00
2016-06-27 18:48:25 +02:00
return;
}
2017-08-04 19:26:37 +02:00
string newFilePath = FilePath + ".new";
2017-08-04 19:26:37 +02:00
await FileSemaphore.WaitAsync().ConfigureAwait(false);
2017-07-10 17:07:48 +02:00
2017-08-04 19:26:37 +02:00
try {
2018-01-02 08:19:37 +01:00
// We always want to write entire content to temporary file first, in order to never load corrupted data, also when target file doesn't exist
await RuntimeCompatibility.File.WriteAllTextAsync(newFilePath, json).ConfigureAwait(false);
2018-01-02 08:19:37 +01:00
2017-08-04 19:26:37 +02:00
if (File.Exists(FilePath)) {
File.Replace(newFilePath, FilePath, null);
} else {
2018-01-02 08:19:37 +01:00
File.Move(newFilePath, FilePath);
}
2017-08-04 19:26:37 +02:00
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
} finally {
FileSemaphore.Release();
2016-06-27 18:48:25 +02:00
}
}
// 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
}