Files
ArchiSteamFarm/ArchiSteamFarm/BotDatabase.cs

357 lines
9.3 KiB
C#
Raw Normal View History

2017-11-18 17:27:06 +01:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
//
2018-07-27 04:52:14 +02:00
// Copyright 2015-2018 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
2017-11-18 17:27:06 +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
2017-11-18 17:27:06 +01:00
//
2018-07-27 04:52:14 +02:00
// 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.
2016-03-06 02:20:41 +01:00
using System;
2018-02-26 08:53:14 +01:00
using System.Collections;
2017-03-28 21:27:01 +02:00
using System.Collections.Generic;
2018-02-26 08:53:14 +01:00
using System.Collections.Specialized;
2016-03-06 02:20:41 +01:00
using System.IO;
2017-08-04 19:26:37 +02:00
using System.Threading;
using System.Threading.Tasks;
2018-09-08 01:03:55 +02:00
using ArchiSteamFarm.Collections;
using Newtonsoft.Json;
2016-03-06 02:20:41 +01:00
namespace ArchiSteamFarm {
2017-08-04 19:26:37 +02:00
internal sealed class BotDatabase : IDisposable {
internal bool HasGamesToRedeemInBackground {
get {
lock (GamesToRedeemInBackground) {
return GamesToRedeemInBackground.Count > 0;
}
}
}
2017-03-28 21:27:01 +02:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentHashSet<ulong> BlacklistedFromTradesSteamIDs = new ConcurrentHashSet<ulong>();
2017-08-04 19:26:37 +02:00
private readonly SemaphoreSlim FileSemaphore = new SemaphoreSlim(1, 1);
2016-06-27 18:48:25 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2018-02-26 08:53:14 +01:00
private readonly OrderedDictionary GamesToRedeemInBackground = new OrderedDictionary();
2017-10-19 00:13:08 +02:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentHashSet<uint> IdlingBlacklistedAppIDs = new ConcurrentHashSet<uint>();
2017-07-23 02:11:16 +02:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentHashSet<uint> IdlingPriorityAppIDs = new ConcurrentHashSet<uint>();
2017-08-04 19:26:37 +02:00
[JsonProperty(PropertyName = "_LoginKey")]
internal string LoginKey { get; private set; }
2017-08-04 19:26:37 +02:00
[JsonProperty(PropertyName = "_MobileAuthenticator")]
internal MobileAuthenticator MobileAuthenticator { get; private set; }
2016-06-27 18:48:25 +02:00
private string FilePath;
2017-12-06 08:14:12 +01:00
private bool ReadOnly;
2016-06-27 18:48:25 +02:00
// This constructor is used when creating new database
private BotDatabase(string filePath) {
if (string.IsNullOrEmpty(filePath)) {
throw new ArgumentNullException(nameof(filePath));
}
FilePath = filePath;
}
// This constructor is used only by deserializer
private BotDatabase() { }
2018-01-12 19:43:34 +01:00
public void Dispose() => FileSemaphore.Dispose();
2017-08-04 19:26:37 +02:00
internal async Task AddBlacklistedFromTradesSteamIDs(IReadOnlyCollection<ulong> steamIDs) {
2017-03-28 21:27:01 +02:00
if ((steamIDs == null) || (steamIDs.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(steamIDs));
2018-12-15 00:27:15 +01:00
2017-03-28 21:27:01 +02:00
return;
}
if (BlacklistedFromTradesSteamIDs.AddRange(steamIDs)) {
2017-08-04 19:26:37 +02:00
await Save().ConfigureAwait(false);
2017-03-28 21:27:01 +02:00
}
}
internal async Task AddGamesToRedeemInBackground(IOrderedDictionary games) {
if ((games == null) || (games.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(games));
2018-12-15 00:27:15 +01:00
return;
}
2018-02-26 08:53:14 +01:00
bool save = false;
lock (GamesToRedeemInBackground) {
2018-02-26 08:53:14 +01:00
foreach (DictionaryEntry game in games) {
if (GamesToRedeemInBackground.Contains(game.Key)) {
continue;
}
GamesToRedeemInBackground.Add(game.Key, game.Value);
save = true;
}
}
if (save) {
await Save().ConfigureAwait(false);
}
}
internal async Task AddIdlingBlacklistedAppIDs(IReadOnlyCollection<uint> appIDs) {
2017-10-19 00:13:08 +02:00
if ((appIDs == null) || (appIDs.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(appIDs));
2018-12-15 00:27:15 +01:00
2017-10-19 00:13:08 +02:00
return;
}
if (IdlingBlacklistedAppIDs.AddRange(appIDs)) {
await Save().ConfigureAwait(false);
}
}
internal async Task AddIdlingPriorityAppIDs(IReadOnlyCollection<uint> appIDs) {
2017-07-23 02:11:16 +02:00
if ((appIDs == null) || (appIDs.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(appIDs));
2018-12-15 00:27:15 +01:00
2017-07-23 02:11:16 +02:00
return;
}
if (IdlingPriorityAppIDs.AddRange(appIDs)) {
2017-08-04 19:26:37 +02:00
await Save().ConfigureAwait(false);
2017-07-23 02:11:16 +02:00
}
}
2017-08-04 19:26:37 +02:00
internal async Task CorrectMobileAuthenticatorDeviceID(string deviceID) {
2017-07-23 02:11:16 +02:00
if (string.IsNullOrEmpty(deviceID) || (MobileAuthenticator == null)) {
ASF.ArchiLogger.LogNullError(nameof(deviceID) + " || " + nameof(MobileAuthenticator));
2018-12-15 00:27:15 +01:00
2017-07-23 02:11:16 +02:00
return;
}
if (MobileAuthenticator.CorrectDeviceID(deviceID)) {
2017-08-04 19:26:37 +02:00
await Save().ConfigureAwait(false);
2017-07-23 02:11:16 +02:00
}
}
2018-10-07 03:21:32 +02:00
internal static async Task<BotDatabase> CreateOrLoad(string filePath) {
if (string.IsNullOrEmpty(filePath)) {
ASF.ArchiLogger.LogNullError(nameof(filePath));
2018-12-15 00:27:15 +01:00
2018-10-07 03:21:32 +02:00
return null;
}
if (!File.Exists(filePath)) {
return new BotDatabase(filePath);
}
BotDatabase botDatabase;
try {
botDatabase = JsonConvert.DeserializeObject<BotDatabase>(await RuntimeCompatibility.File.ReadAllTextAsync(filePath).ConfigureAwait(false));
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
2018-12-15 00:27:15 +01:00
2018-10-07 03:21:32 +02:00
return null;
}
if (botDatabase == null) {
ASF.ArchiLogger.LogNullError(nameof(botDatabase));
2018-12-15 00:27:15 +01:00
2018-10-07 03:21:32 +02:00
return null;
}
botDatabase.FilePath = filePath;
2018-12-15 00:27:15 +01:00
2018-10-07 03:21:32 +02:00
return botDatabase;
}
internal IReadOnlyCollection<ulong> GetBlacklistedFromTradesSteamIDs() => BlacklistedFromTradesSteamIDs;
2018-02-26 08:53:14 +01:00
internal (string Key, string Name) GetGameToRedeemInBackground() {
lock (GamesToRedeemInBackground) {
2018-02-26 08:53:14 +01:00
foreach (DictionaryEntry game in GamesToRedeemInBackground) {
return (game.Key as string, game.Value as string);
}
}
2018-02-26 08:53:14 +01:00
return (null, null);
}
internal IReadOnlyCollection<uint> GetIdlingBlacklistedAppIDs() => IdlingBlacklistedAppIDs;
internal IReadOnlyCollection<uint> GetIdlingPriorityAppIDs() => IdlingPriorityAppIDs;
2017-10-19 00:13:08 +02:00
internal bool IsBlacklistedFromIdling(uint appID) {
if (appID == 0) {
ASF.ArchiLogger.LogNullError(nameof(appID));
2018-12-15 00:27:15 +01:00
2017-10-19 00:13:08 +02:00
return false;
}
2018-03-09 15:43:25 +01:00
return IdlingBlacklistedAppIDs.Contains(appID);
2017-10-19 00:13:08 +02:00
}
2017-03-28 21:27:01 +02:00
internal bool IsBlacklistedFromTrades(ulong steamID) {
2017-07-23 02:11:16 +02:00
if (steamID == 0) {
ASF.ArchiLogger.LogNullError(nameof(steamID));
2018-12-15 00:27:15 +01:00
2017-07-23 02:11:16 +02:00
return false;
2017-03-28 21:27:01 +02:00
}
2018-03-09 15:43:25 +01:00
return BlacklistedFromTradesSteamIDs.Contains(steamID);
2017-07-23 02:11:16 +02:00
}
internal bool IsPriorityIdling(uint appID) {
if (appID == 0) {
ASF.ArchiLogger.LogNullError(nameof(appID));
2018-12-15 00:27:15 +01:00
2017-07-23 02:11:16 +02:00
return false;
}
2018-03-09 15:43:25 +01:00
return IdlingPriorityAppIDs.Contains(appID);
2017-03-28 21:27:01 +02:00
}
2017-12-06 08:14:12 +01:00
internal async Task MakeReadOnly() {
if (ReadOnly) {
return;
}
2017-12-06 08:14:12 +01:00
await FileSemaphore.WaitAsync().ConfigureAwait(false);
try {
if (ReadOnly) {
return;
}
2017-12-06 08:14:12 +01:00
ReadOnly = true;
} finally {
FileSemaphore.Release();
}
}
internal async Task RemoveBlacklistedFromTradesSteamIDs(IReadOnlyCollection<ulong> steamIDs) {
2017-03-28 21:27:01 +02:00
if ((steamIDs == null) || (steamIDs.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(steamIDs));
2018-12-15 00:27:15 +01:00
2017-03-28 21:27:01 +02:00
return;
}
if (BlacklistedFromTradesSteamIDs.RemoveRange(steamIDs)) {
2017-08-04 19:26:37 +02:00
await Save().ConfigureAwait(false);
2017-03-28 21:27:01 +02:00
}
}
internal async Task RemoveGameToRedeemInBackground(string key) {
if (string.IsNullOrEmpty(key)) {
ASF.ArchiLogger.LogNullError(nameof(key));
2018-12-15 00:27:15 +01:00
return;
}
lock (GamesToRedeemInBackground) {
2018-02-26 08:53:14 +01:00
if (!GamesToRedeemInBackground.Contains(key)) {
return;
}
2018-02-26 08:53:14 +01:00
GamesToRedeemInBackground.Remove(key);
}
2018-02-26 08:53:14 +01:00
await Save().ConfigureAwait(false);
}
internal async Task RemoveIdlingBlacklistedAppIDs(IReadOnlyCollection<uint> appIDs) {
2017-10-19 00:13:08 +02:00
if ((appIDs == null) || (appIDs.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(appIDs));
2018-12-15 00:27:15 +01:00
2017-10-19 00:13:08 +02:00
return;
}
if (IdlingBlacklistedAppIDs.RemoveRange(appIDs)) {
await Save().ConfigureAwait(false);
}
}
internal async Task RemoveIdlingPriorityAppIDs(IReadOnlyCollection<uint> appIDs) {
2017-07-23 02:11:16 +02:00
if ((appIDs == null) || (appIDs.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(appIDs));
2018-12-15 00:27:15 +01:00
2017-07-23 02:11:16 +02:00
return;
}
if (IdlingPriorityAppIDs.RemoveRange(appIDs)) {
2017-08-04 19:26:37 +02:00
await Save().ConfigureAwait(false);
2017-07-23 02:11:16 +02:00
}
}
2017-08-04 19:26:37 +02:00
internal async Task SetLoginKey(string value = null) {
if (value == LoginKey) {
return;
}
LoginKey = value;
await Save().ConfigureAwait(false);
}
internal async Task SetMobileAuthenticator(MobileAuthenticator value = null) {
if (value == MobileAuthenticator) {
return;
}
MobileAuthenticator = value;
await Save().ConfigureAwait(false);
}
private async Task Save() {
2017-12-06 08:14:12 +01:00
if (ReadOnly) {
return;
}
2016-06-27 18:48: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:16:41 +01:00
if (ReadOnly) {
return;
}
2017-08-04 19:26:37 +02:00
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
}
}
}
2018-07-27 04:52:14 +02:00
}