Files
ArchiSteamFarm/ArchiSteamFarm/BotDatabase.cs

347 lines
9.5 KiB
C#
Raw Normal View History

2017-11-18 17:27:06 +01:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
//
2018-01-01 02:56:53 +01:00
// Copyright 2015-2018 Łukasz "JustArchi" Domeradzki
2017-11-18 17:27:06 +01: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.
2016-03-06 02:20:41 +01:00
using System;
2017-03-28 21:27:01 +02:00
using System.Collections.Generic;
2016-03-06 02:20:41 +01:00
using System.IO;
using System.Linq;
2017-08-04 19:26:37 +02:00
using System.Threading;
using System.Threading.Tasks;
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)]
private readonly SortedDictionary<string, string> GamesToRedeemInBackground = new SortedDictionary<string, string>();
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;
2017-08-04 19:26:37 +02:00
Save().Wait();
}
// 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));
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(IReadOnlyDictionary<string, string> games) {
if ((games == null) || (games.Count == 0)) {
ASF.ArchiLogger.LogNullError(nameof(games));
return;
}
bool save;
lock (GamesToRedeemInBackground) {
// We use Count() and not Any() because we must ensure full loop pass
save = games.Count(game => GamesToRedeemInBackground.TryAdd(game.Key, game.Value)) > 0;
}
if (save) {
await Save().ConfigureAwait(false);
}
}
internal async Task AddGameToRedeemInBackground(string key, string game) {
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(game)) {
ASF.ArchiLogger.LogNullError(nameof(key) + " || " + nameof(game));
return;
}
bool save;
lock (GamesToRedeemInBackground) {
save = GamesToRedeemInBackground.TryAdd(key, game);
}
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));
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));
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));
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
}
}
internal IReadOnlyCollection<ulong> GetBlacklistedFromTradesSteamIDs() => BlacklistedFromTradesSteamIDs;
internal KeyValuePair<string, string> GetGameToRedeemInBackground() {
lock (GamesToRedeemInBackground) {
return GamesToRedeemInBackground.FirstOrDefault();
}
}
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));
return false;
}
bool result = IdlingBlacklistedAppIDs.Contains(appID);
return result;
}
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));
return false;
2017-03-28 21:27:01 +02:00
}
2017-07-23 02:11:16 +02:00
bool result = BlacklistedFromTradesSteamIDs.Contains(steamID);
return result;
}
internal bool IsPriorityIdling(uint appID) {
if (appID == 0) {
ASF.ArchiLogger.LogNullError(nameof(appID));
return false;
}
bool result = IdlingPriorityAppIDs.Contains(appID);
return result;
2017-03-28 21:27:01 +02:00
}
2016-06-27 18:48:25 +02:00
internal static BotDatabase Load(string filePath) {
if (string.IsNullOrEmpty(filePath)) {
ASF.ArchiLogger.LogNullError(nameof(filePath));
2016-06-27 18:48:25 +02:00
return null;
}
if (!File.Exists(filePath)) {
return new BotDatabase(filePath);
}
BotDatabase botDatabase;
try {
botDatabase = JsonConvert.DeserializeObject<BotDatabase>(File.ReadAllText(filePath));
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
2016-06-27 18:48:25 +02:00
return null;
}
if (botDatabase == null) {
ASF.ArchiLogger.LogNullError(nameof(botDatabase));
2016-06-27 18:48:25 +02:00
return null;
}
botDatabase.FilePath = filePath;
return botDatabase;
}
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));
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));
return;
}
bool save;
lock (GamesToRedeemInBackground) {
save = GamesToRedeemInBackground.Remove(key, out _);
}
if (save) {
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));
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));
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);
if (string.IsNullOrEmpty(json)) {
ASF.ArchiLogger.LogNullError(nameof(json));
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 File.WriteAllTextAsync(newFilePath, json).ConfigureAwait(false);
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
}
}
}
}