Files
ArchiSteamFarm/ArchiSteamFarm/BotConfig.cs

331 lines
12 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;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
2016-03-06 02:20:41 +01:00
using System.IO;
2017-12-06 08:14:12 +01:00
using System.Threading;
using System.Threading.Tasks;
2017-12-14 08:23:17 +01:00
using ArchiSteamFarm.Json;
2017-01-06 16:29:34 +01:00
using ArchiSteamFarm.Localization;
using Newtonsoft.Json;
2018-06-13 00:48:33 +02:00
using SteamKit2;
2016-03-06 02:20:41 +01:00
namespace ArchiSteamFarm {
[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")]
internal sealed class BotConfig {
2017-12-06 08:14:12 +01:00
private static readonly SemaphoreSlim WriteSemaphore = new SemaphoreSlim(1, 1);
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-04-05 14:30:14 +02:00
internal readonly bool AcceptGifts;
2016-03-06 02:20:41 +01:00
2017-07-23 01:34:02 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2017-12-21 19:38:45 +01:00
internal readonly bool AutoSteamSaleEvent;
2017-07-23 01:34:02 +02:00
2018-07-14 16:29:33 +02:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly EBotBehaviour BotBehaviour;
2016-03-06 02:20:41 +01:00
[JsonProperty]
2017-04-05 14:30:14 +02:00
internal readonly string CustomGamePlayedWhileFarming;
2016-03-06 02:20:41 +01:00
[JsonProperty]
2017-04-05 14:30:14 +02:00
internal readonly string CustomGamePlayedWhileIdle;
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-04-05 14:30:14 +02:00
internal readonly bool Enabled;
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2018-07-04 23:43:18 +02:00
internal readonly HashSet<EFarmingOrder> FarmingOrders = new HashSet<EFarmingOrder>();
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly HashSet<uint> GamesPlayedWhileIdle = new HashSet<uint>();
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-04-05 14:30:14 +02:00
internal readonly bool HandleOfflineMessages;
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte HoursUntilCardDrops = 3;
2017-07-23 10:27:20 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2018-02-28 02:14:29 +01:00
internal readonly bool IdlePriorityQueueOnly;
[JsonProperty(Required = Required.DisallowNull)]
2017-07-23 10:27:20 +02:00
internal readonly bool IdleRefundableGames = true;
[JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, Required = Required.DisallowNull)]
internal readonly HashSet<Steam.Asset.EType> LootableTypes = new HashSet<Steam.Asset.EType> {
Steam.Asset.EType.BoosterPack,
Steam.Asset.EType.FoilTradingCard,
Steam.Asset.EType.TradingCard
2017-01-05 21:08:13 +01:00
};
2016-12-25 05:52:17 +01:00
[JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, Required = Required.DisallowNull)]
2017-11-28 21:31:45 +01:00
internal readonly HashSet<Steam.Asset.EType> MatchableTypes = new HashSet<Steam.Asset.EType> { Steam.Asset.EType.TradingCard };
2018-07-04 19:13:51 +02:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly EPersonaState OnlineStatus = EPersonaState.Online;
2016-06-04 22:02:38 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2018-07-14 16:29:33 +02:00
internal readonly CryptoHelper.ECryptoMethod PasswordFormat;
2016-06-04 22:02:38 +02:00
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-04-05 14:30:14 +02:00
internal readonly bool Paused;
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2018-07-14 16:29:33 +02:00
internal readonly ERedeemingPreferences RedeemingPreferences;
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-04-05 14:30:14 +02:00
internal readonly bool SendOnFarmingFinished;
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-04-05 14:30:14 +02:00
internal readonly byte SendTradePeriod;
2016-03-06 02:20:41 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-04-05 14:30:14 +02:00
internal readonly bool ShutdownOnFarmingFinished;
2016-03-06 02:20:41 +01:00
[JsonProperty]
2017-04-05 14:30:14 +02:00
internal readonly string SteamTradeToken;
[JsonProperty(Required = Required.DisallowNull)]
internal readonly Dictionary<ulong, EPermission> SteamUserPermissions = new Dictionary<ulong, EPermission>();
[JsonProperty(Required = Required.DisallowNull)]
2018-07-14 16:29:33 +02:00
internal readonly ETradingPreferences TradingPreferences;
2016-03-10 21:17:48 +01:00
2017-11-28 21:31:45 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly bool UseLoginKeys = true;
[JsonProperty]
internal string SteamLogin { get; set; }
2017-07-01 13:39:53 +02:00
[JsonProperty(Required = Required.DisallowNull)]
internal ulong SteamMasterClanID { get; private set; }
2016-03-08 03:18:50 +01:00
[JsonProperty]
internal string SteamParentalPIN { get; set; } = "0";
2016-03-08 03:18:50 +01:00
[JsonProperty]
internal string SteamPassword { get; set; }
private bool ShouldSerializeSensitiveDetails = true;
2018-02-11 11:11:38 +01:00
[JsonProperty(PropertyName = SharedInfo.UlongCompatibilityStringPrefix + nameof(SteamMasterClanID), Required = Required.DisallowNull)]
2018-02-11 09:58:25 +01:00
private string SSteamMasterClanID {
get => SteamMasterClanID.ToString();
set {
if (string.IsNullOrEmpty(value) || !ulong.TryParse(value, out ulong result)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsInvalid, nameof(SSteamMasterClanID)));
return;
}
SteamMasterClanID = result;
}
}
public bool ShouldSerializeSteamLogin() => ShouldSerializeSensitiveDetails;
public bool ShouldSerializeSteamParentalPIN() => ShouldSerializeSensitiveDetails;
public bool ShouldSerializeSteamPassword() => ShouldSerializeSensitiveDetails;
2017-08-09 18:58:51 +02:00
internal static async Task<BotConfig> Load(string filePath) {
2016-05-30 01:57:06 +02:00
if (string.IsNullOrEmpty(filePath)) {
ASF.ArchiLogger.LogNullError(nameof(filePath));
2016-05-30 01:57:06 +02:00
return null;
}
if (!File.Exists(filePath)) {
2016-03-06 02:20:41 +01:00
return null;
}
BotConfig botConfig;
2016-05-30 01:57:06 +02:00
2016-03-06 02:20:41 +01:00
try {
botConfig = JsonConvert.DeserializeObject<BotConfig>(await RuntimeCompatibility.File.ReadAllTextAsync(filePath).ConfigureAwait(false));
2016-03-06 02:20:41 +01:00
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
2016-03-06 02:20:41 +01:00
return null;
}
2016-07-31 17:38:14 +02:00
if (botConfig == null) {
ASF.ArchiLogger.LogNullError(nameof(botConfig));
2016-07-31 17:38:14 +02:00
return null;
}
2018-07-04 19:13:51 +02:00
if (botConfig.BotBehaviour > EBotBehaviour.All) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.BotBehaviour), botConfig.BotBehaviour));
return null;
}
2018-07-04 23:43:18 +02:00
foreach (EFarmingOrder farmingOrder in botConfig.FarmingOrders) {
if (!Enum.IsDefined(typeof(EFarmingOrder), farmingOrder)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.FarmingOrders), farmingOrder));
return null;
}
2018-07-04 19:13:51 +02:00
}
if (botConfig.GamesPlayedWhileIdle.Count > ArchiHandler.MaxGamesPlayedConcurrently) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.GamesPlayedWhileIdle), botConfig.GamesPlayedWhileIdle.Count + " > " + ArchiHandler.MaxGamesPlayedConcurrently));
return null;
}
foreach (Steam.Asset.EType lootableType in botConfig.LootableTypes) {
if (!Enum.IsDefined(typeof(Steam.Asset.EType), lootableType)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.LootableTypes), lootableType));
return null;
}
}
foreach (Steam.Asset.EType matchableType in botConfig.MatchableTypes) {
if (!Enum.IsDefined(typeof(Steam.Asset.EType), matchableType)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.MatchableTypes), matchableType));
return null;
}
}
2018-06-13 14:52:55 +02:00
if ((botConfig.OnlineStatus < EPersonaState.Offline) || (botConfig.OnlineStatus >= EPersonaState.Max)) {
2018-06-13 00:48:33 +02:00
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.OnlineStatus), botConfig.OnlineStatus));
return null;
}
2018-07-04 19:13:51 +02:00
if (!Enum.IsDefined(typeof(CryptoHelper.ECryptoMethod), botConfig.PasswordFormat)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.PasswordFormat), botConfig.PasswordFormat));
return null;
}
2016-07-25 06:08:45 +02:00
2018-07-04 19:13:51 +02:00
if (botConfig.RedeemingPreferences > ERedeemingPreferences.All) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.RedeemingPreferences), botConfig.RedeemingPreferences));
return null;
2018-06-13 00:48:33 +02:00
}
2018-07-04 19:13:51 +02:00
foreach (EPermission permission in botConfig.SteamUserPermissions.Values) {
if (!Enum.IsDefined(typeof(EPermission), permission)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.SteamUserPermissions), permission));
return null;
}
}
if (botConfig.TradingPreferences > ETradingPreferences.All) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.TradingPreferences), botConfig.TradingPreferences));
return null;
}
// Support encrypted passwords
if ((botConfig.PasswordFormat != CryptoHelper.ECryptoMethod.PlainText) && !string.IsNullOrEmpty(botConfig.SteamPassword)) {
// In worst case password will result in null, which will have to be corrected by user during runtime
botConfig.SteamPassword = CryptoHelper.Decrypt(botConfig.PasswordFormat, botConfig.SteamPassword);
}
botConfig.ShouldSerializeSensitiveDetails = false;
2016-03-06 02:20:41 +01:00
return botConfig;
}
2017-12-06 08:14:12 +01:00
internal static async Task<bool> Write(string filePath, BotConfig botConfig) {
if (string.IsNullOrEmpty(filePath) || (botConfig == null)) {
ASF.ArchiLogger.LogNullError(nameof(filePath) + " || " + nameof(botConfig));
return false;
}
string json = JsonConvert.SerializeObject(botConfig, Formatting.Indented);
string newFilePath = filePath + ".new";
await WriteSemaphore.WaitAsync().ConfigureAwait(false);
try {
await RuntimeCompatibility.File.WriteAllTextAsync(newFilePath, json).ConfigureAwait(false);
2017-12-06 08:14:12 +01:00
if (File.Exists(filePath)) {
File.Replace(newFilePath, filePath, null);
} else {
File.Move(newFilePath, filePath);
}
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
return false;
} finally {
WriteSemaphore.Release();
}
return true;
}
2018-04-23 23:51:34 +02:00
[Flags]
internal enum EBotBehaviour : byte {
2018-07-21 19:30:32 +02:00
[SuppressMessage("ReSharper", "UnusedMember.Global")]
2018-04-23 23:51:34 +02:00
None = 0,
2018-07-21 19:30:32 +02:00
2018-04-23 23:51:34 +02:00
RejectInvalidFriendInvites = 1,
RejectInvalidTrades = 2,
RejectInvalidGroupInvites = 4,
2018-07-04 19:13:51 +02:00
DismissInventoryNotifications = 8,
All = RejectInvalidFriendInvites | RejectInvalidTrades | RejectInvalidGroupInvites | DismissInventoryNotifications
2018-04-23 23:51:34 +02:00
}
internal enum EFarmingOrder : byte {
Unordered,
AppIDsAscending,
AppIDsDescending,
CardDropsAscending,
CardDropsDescending,
HoursAscending,
HoursDescending,
NamesAscending,
2017-07-02 10:00:02 +02:00
NamesDescending,
2017-07-23 05:06:11 +02:00
Random,
BadgeLevelsAscending,
BadgeLevelsDescending,
RedeemDateTimesAscending,
2018-07-04 19:13:51 +02:00
RedeemDateTimesDescending,
MarketableAscending,
MarketableDescending
}
internal enum EPermission : byte {
None,
FamilySharing,
Operator,
Master
}
[Flags]
internal enum ERedeemingPreferences : byte {
2018-07-21 19:30:32 +02:00
[SuppressMessage("ReSharper", "UnusedMember.Global")]
None = 0,
2018-07-21 19:30:32 +02:00
Forwarding = 1,
2017-02-18 01:03:13 +01:00
Distributing = 2,
2018-07-04 19:13:51 +02:00
KeepMissingGames = 4,
All = Forwarding | Distributing | KeepMissingGames
}
[Flags]
internal enum ETradingPreferences : byte {
2018-07-21 19:30:32 +02:00
[SuppressMessage("ReSharper", "UnusedMember.Global")]
None = 0,
2018-07-21 19:30:32 +02:00
AcceptDonations = 1,
SteamTradeMatcher = 2,
2017-02-05 07:51:04 +01:00
MatchEverything = 4,
2018-07-04 19:13:51 +02:00
DontAcceptBotTrades = 8,
All = AcceptDonations | SteamTradeMatcher | MatchEverything | DontAcceptBotTrades
}
2016-03-06 02:20:41 +01:00
}
2018-07-14 16:29:33 +02:00
}