Files
ArchiSteamFarm/ArchiSteamFarm/BotDatabase.cs

218 lines
6.5 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
// |
// Copyright 2015-2021 Ł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;
using System.Collections.Specialized;
2020-11-14 22:37:00 +01:00
using System.Globalization;
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;
2016-03-06 02:20:41 +01:00
namespace ArchiSteamFarm {
internal sealed class BotDatabase : SerializableFile {
[JsonProperty(Required = Required.DisallowNull)]
internal readonly ConcurrentHashSet<ulong> BlacklistedFromTradesSteamIDs = new();
[JsonProperty(Required = Required.DisallowNull)]
internal readonly ConcurrentHashSet<uint> IdlingBlacklistedAppIDs = new();
[JsonProperty(Required = Required.DisallowNull)]
internal readonly ConcurrentHashSet<uint> IdlingPriorityAppIDs = new();
internal uint GamesToRedeemInBackgroundCount {
get {
lock (GamesToRedeemInBackground) {
return (uint) GamesToRedeemInBackground.Count;
}
}
}
internal bool HasGamesToRedeemInBackground => GamesToRedeemInBackgroundCount > 0;
2017-03-28 21:27:01 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2020-11-14 22:37:00 +01:00
private readonly OrderedDictionary GamesToRedeemInBackground = new();
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() {
BlacklistedFromTradesSteamIDs.OnModified += OnObjectModified;
IdlingBlacklistedAppIDs.OnModified += OnObjectModified;
IdlingPriorityAppIDs.OnModified += OnObjectModified;
}
public override void Dispose() {
BlacklistedFromTradesSteamIDs.OnModified -= OnObjectModified;
IdlingBlacklistedAppIDs.OnModified -= OnObjectModified;
IdlingPriorityAppIDs.OnModified -= OnObjectModified;
BackingMobileAuthenticator?.Dispose();
base.Dispose();
}
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 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);
}
2020-12-25 23:12:42 +01:00
BotDatabase? botDatabase;
2018-10-07 03:21:32 +02:00
try {
2019-04-04 22:34:58 +02:00
string json = await RuntimeCompatibility.File.ReadAllTextAsync(filePath).ConfigureAwait(false);
if (string.IsNullOrEmpty(json)) {
2020-11-14 22:37:00 +01:00
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(json)));
2019-04-04 22:34:58 +02:00
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-12-25 23:12:42 +01:00
// ReSharper disable once ConditionIsAlwaysTrueOrFalse - wrong, "null" json deserializes 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 (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);
}
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);
}
private async void OnObjectModified(object? sender, EventArgs e) {
if (string.IsNullOrEmpty(FilePath)) {
return;
}
await Save().ConfigureAwait(false);
}
// ReSharper disable UnusedMember.Global
public bool ShouldSerializeBackingLoginKey() => !string.IsNullOrEmpty(BackingLoginKey);
public bool ShouldSerializeBackingMobileAuthenticator() => BackingMobileAuthenticator != null;
public bool ShouldSerializeBlacklistedFromTradesSteamIDs() => BlacklistedFromTradesSteamIDs.Count > 0;
public bool ShouldSerializeGamesToRedeemInBackground() => HasGamesToRedeemInBackground;
public bool ShouldSerializeIdlingBlacklistedAppIDs() => IdlingBlacklistedAppIDs.Count > 0;
public bool ShouldSerializeIdlingPriorityAppIDs() => IdlingPriorityAppIDs.Count > 0;
// ReSharper restore UnusedMember.Global
2016-06-27 18:48:25 +02:00
}
2018-07-27 04:52:14 +02:00
}