Files
ArchiSteamFarm/ArchiSteamFarm/BotDatabase.cs

295 lines
8.7 KiB
C#
Raw 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-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;
2019-08-10 17:38:49 +02:00
using System.Linq;
2017-08-04 19:26:37 +02:00
using System.Threading.Tasks;
2018-09-08 01:03:55 +02:00
using ArchiSteamFarm.Collections;
using ArchiSteamFarm.Helpers;
2019-04-04 22:34:58 +02:00
using ArchiSteamFarm.Localization;
using Newtonsoft.Json;
2019-04-03 16:41:47 +02:00
using SteamKit2;
2016-03-06 02:20:41 +01:00
namespace ArchiSteamFarm {
internal sealed class BotDatabase : SerializableFile {
internal uint GamesToRedeemInBackgroundCount {
get {
lock (GamesToRedeemInBackground) {
return (uint) GamesToRedeemInBackground.Count;
}
}
}
internal bool HasGamesToRedeemInBackground => GamesToRedeemInBackgroundCount > 0;
internal bool HasIdlingPriorityAppIDs => IdlingPriorityAppIDs.Count > 0;
2017-03-28 21:27:01 +02:00
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentHashSet<ulong> BlacklistedFromTradesSteamIDs = new ConcurrentHashSet<ulong>();
[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>();
internal string? LoginKey {
get => BackingLoginKey;
set {
if (BackingLoginKey == value) {
return;
}
BackingLoginKey = value;
Utilities.InBackground(Save);
}
}
internal MobileAuthenticator? MobileAuthenticator {
get => BackingMobileAuthenticator;
set {
if (BackingMobileAuthenticator == value) {
return;
}
BackingMobileAuthenticator = value;
Utilities.InBackground(Save);
}
}
2020-06-07 21:06:41 +02:00
[JsonProperty(PropertyName = "_" + nameof(LoginKey))]
private string? BackingLoginKey;
2020-06-07 21:06:41 +02:00
[JsonProperty(PropertyName = "_" + nameof(MobileAuthenticator))]
private MobileAuthenticator? BackingMobileAuthenticator;
2016-06-27 18:48:25 +02:00
private BotDatabase(string filePath) {
if (string.IsNullOrEmpty(filePath)) {
throw new ArgumentNullException(nameof(filePath));
}
FilePath = filePath;
}
2019-04-04 17:56:38 +02:00
[JsonConstructor]
private BotDatabase() { }
public override void Dispose() {
BackingMobileAuthenticator?.Dispose();
base.Dispose();
}
internal void AddBlacklistedFromTradesSteamIDs(IReadOnlyCollection<ulong> steamIDs) {
2017-03-28 21:27:01 +02:00
if ((steamIDs == null) || (steamIDs.Count == 0)) {
throw new ArgumentNullException(nameof(steamIDs));
2017-03-28 21:27:01 +02:00
}
if (BlacklistedFromTradesSteamIDs.AddRange(steamIDs)) {
Utilities.InBackground(Save);
2017-03-28 21:27:01 +02:00
}
}
internal void AddGamesToRedeemInBackground(IOrderedDictionary games) {
if ((games == null) || (games.Count == 0)) {
throw new ArgumentNullException(nameof(games));
}
2018-02-26 08:53:14 +01:00
bool save = false;
lock (GamesToRedeemInBackground) {
foreach (DictionaryEntry game in games.OfType<DictionaryEntry>().Where(game => !GamesToRedeemInBackground.Contains(game.Key))) {
2018-02-26 08:53:14 +01:00
GamesToRedeemInBackground.Add(game.Key, game.Value);
save = true;
}
}
if (save) {
Utilities.InBackground(Save);
}
}
internal void AddIdlingBlacklistedAppIDs(IReadOnlyCollection<uint> appIDs) {
2017-10-19 00:13:08 +02:00
if ((appIDs == null) || (appIDs.Count == 0)) {
throw new ArgumentNullException(nameof(appIDs));
2017-10-19 00:13:08 +02:00
}
if (IdlingBlacklistedAppIDs.AddRange(appIDs)) {
Utilities.InBackground(Save);
2017-10-19 00:13:08 +02:00
}
}
internal void AddIdlingPriorityAppIDs(IReadOnlyCollection<uint> appIDs) {
2017-07-23 02:11:16 +02:00
if ((appIDs == null) || (appIDs.Count == 0)) {
throw new ArgumentNullException(nameof(appIDs));
2017-07-23 02:11:16 +02:00
}
if (IdlingPriorityAppIDs.AddRange(appIDs)) {
Utilities.InBackground(Save);
2017-07-23 02:11:16 +02:00
}
}
internal static async Task<BotDatabase?> CreateOrLoad(string filePath) {
2018-10-07 03:21:32 +02:00
if (string.IsNullOrEmpty(filePath)) {
throw new ArgumentNullException(nameof(filePath));
2018-10-07 03:21:32 +02:00
}
if (!File.Exists(filePath)) {
return new BotDatabase(filePath);
}
BotDatabase botDatabase;
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;
}
botDatabase = JsonConvert.DeserializeObject<BotDatabase>(json);
2018-10-07 03:21:32 +02:00
} 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;
}
2020-08-23 20:45:24 +02:00
// ReSharper disable once ConditionIsAlwaysTrueOrFalse - wrong, "null" json serializes into null object
2018-10-07 03:21:32 +02:00
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;
2020-08-23 20:45:24 +02:00
#pragma warning disable CS8605
internal (string? Key, string? Name) GetGameToRedeemInBackground() {
lock (GamesToRedeemInBackground) {
2020-08-23 20:45:24 +02:00
foreach (DictionaryEntry game in GamesToRedeemInBackground) {
return (game.Key as string, game.Value as string);
2018-02-26 08:53:14 +01:00
}
}
2018-02-26 08:53:14 +01:00
return (null, null);
}
2020-08-23 20:45:24 +02:00
#pragma warning restore CS8605
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) {
throw new ArgumentNullException(nameof(appID));
2017-10-19 00:13:08 +02:00
}
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) {
2019-04-03 16:41:47 +02:00
if ((steamID == 0) || !new SteamID(steamID).IsIndividualAccount) {
throw new ArgumentNullException(nameof(steamID));
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) {
throw new ArgumentNullException(nameof(appID));
2017-07-23 02:11:16 +02:00
}
2018-03-09 15:43:25 +01:00
return IdlingPriorityAppIDs.Contains(appID);
2017-03-28 21:27:01 +02:00
}
internal void RemoveBlacklistedFromTradesSteamIDs(IReadOnlyCollection<ulong> steamIDs) {
2017-03-28 21:27:01 +02:00
if ((steamIDs == null) || (steamIDs.Count == 0)) {
throw new ArgumentNullException(nameof(steamIDs));
2017-03-28 21:27:01 +02:00
}
if (BlacklistedFromTradesSteamIDs.RemoveRange(steamIDs)) {
Utilities.InBackground(Save);
2017-03-28 21:27:01 +02:00
}
}
internal void RemoveGameToRedeemInBackground(string key) {
if (string.IsNullOrEmpty(key)) {
throw new ArgumentNullException(nameof(key));
}
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
Utilities.InBackground(Save);
}
internal void RemoveIdlingBlacklistedAppIDs(IReadOnlyCollection<uint> appIDs) {
2017-10-19 00:13:08 +02:00
if ((appIDs == null) || (appIDs.Count == 0)) {
throw new ArgumentNullException(nameof(appIDs));
2017-10-19 00:13:08 +02:00
}
if (IdlingBlacklistedAppIDs.RemoveRange(appIDs)) {
Utilities.InBackground(Save);
2017-10-19 00:13:08 +02:00
}
}
internal void RemoveIdlingPriorityAppIDs(IReadOnlyCollection<uint> appIDs) {
2017-07-23 02:11:16 +02:00
if ((appIDs == null) || (appIDs.Count == 0)) {
throw new ArgumentNullException(nameof(appIDs));
2017-07-23 02:11:16 +02:00
}
if (IdlingPriorityAppIDs.RemoveRange(appIDs)) {
Utilities.InBackground(Save);
2016-06-27 18:48:25 +02:00
}
}
// ReSharper disable UnusedMember.Global
public bool ShouldSerializeBlacklistedFromTradesSteamIDs() => BlacklistedFromTradesSteamIDs.Count > 0;
public bool ShouldSerializeGamesToRedeemInBackground() => HasGamesToRedeemInBackground;
public bool ShouldSerializeIdlingBlacklistedAppIDs() => IdlingBlacklistedAppIDs.Count > 0;
public bool ShouldSerializeIdlingPriorityAppIDs() => IdlingPriorityAppIDs.Count > 0;
public bool ShouldSerializeLoginKey() => !string.IsNullOrEmpty(LoginKey);
public bool ShouldSerializeMobileAuthenticator() => MobileAuthenticator != null;
// ReSharper restore UnusedMember.Global
2016-06-27 18:48:25 +02:00
}
2018-07-27 04:52:14 +02:00
}