Files
ArchiSteamFarm/ArchiSteamFarm/GlobalConfig.cs

394 lines
15 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
// |
2019-01-02 16:32:53 +01:00
// Copyright 2015-2019 Ł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 23:28:56 +01:00
using System;
using System.Collections.Generic;
2018-08-06 01:21:36 +02:00
using System.Collections.Immutable;
2016-05-13 06:32:42 +02:00
using System.Diagnostics.CodeAnalysis;
2016-03-06 23:28:56 +01:00
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
2017-01-06 16:29:34 +01:00
using ArchiSteamFarm.Localization;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SteamKit2;
2016-03-06 23:28:56 +01:00
namespace ArchiSteamFarm {
[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")]
public sealed class GlobalConfig {
2018-12-30 22:16:07 +01:00
internal const byte DefaultConnectionTimeout = 90;
2019-01-17 20:36:24 +01:00
internal const byte DefaultLoginLimiterDelay = 10;
2018-08-06 01:21:36 +02:00
private const bool DefaultAutoRestart = true;
private const string DefaultCommandPrefix = "!";
private const byte DefaultConfirmationsLimiterDelay = 10;
private const string DefaultCurrentCulture = null;
private const bool DefaultDebug = false;
private const byte DefaultFarmingDelay = 15;
private const byte DefaultGiftsLimiterDelay = 1;
private const bool DefaultHeadless = false;
private const byte DefaultIdleFarmingPeriod = 8;
private const byte DefaultInventoryLimiterDelay = 3;
private const bool DefaultIPC = false;
private const string DefaultIPCPassword = null;
private const byte DefaultMaxFarmingTime = 10;
private const byte DefaultMaxTradeHoldDuration = 15;
private const EOptimizationMode DefaultOptimizationMode = EOptimizationMode.MaxPerformance;
private const bool DefaultStatistics = true;
private const string DefaultSteamMessagePrefix = "/me ";
private const ulong DefaultSteamOwnerID = 0;
2018-10-12 16:57:02 +02:00
private const ProtocolTypes DefaultSteamProtocols = ProtocolTypes.All;
2018-08-06 01:21:36 +02:00
private const EUpdateChannel DefaultUpdateChannel = EUpdateChannel.Stable;
private const byte DefaultUpdatePeriod = 24;
2018-12-07 22:22:26 +01:00
private const ushort DefaultWebLimiterDelay = 300;
2018-08-06 01:21:36 +02:00
private const string DefaultWebProxyPassword = null;
private const string DefaultWebProxyText = null;
private const string DefaultWebProxyUsername = null;
2018-09-08 00:46:40 +02:00
private static readonly ImmutableHashSet<uint> DefaultBlacklist = ImmutableHashSet.Create<uint>();
private static readonly SemaphoreSlim WriteSemaphore = new SemaphoreSlim(1, 1);
2016-03-07 18:12:05 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly bool AutoRestart = DefaultAutoRestart;
2016-03-07 18:12:05 +01:00
2018-08-06 01:21:36 +02:00
[JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly ImmutableHashSet<uint> Blacklist = DefaultBlacklist;
2016-03-06 23:28:56 +01:00
[JsonProperty]
2019-01-14 21:50:23 +01:00
public readonly string CommandPrefix = DefaultCommandPrefix;
2018-01-12 19:43:34 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte ConfirmationsLimiterDelay = DefaultConfirmationsLimiterDelay;
2018-01-12 19:43:34 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte ConnectionTimeout = DefaultConnectionTimeout;
2017-01-08 05:32:59 +01:00
[JsonProperty]
2019-01-14 21:50:23 +01:00
public readonly string CurrentCulture = DefaultCurrentCulture;
2017-01-08 05:32:59 +01:00
2016-04-18 18:38:48 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly bool Debug = DefaultDebug;
2016-04-18 18:38:48 +02:00
2016-03-06 23:28:56 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte FarmingDelay = DefaultFarmingDelay;
2016-03-06 23:28:56 +01:00
2016-03-18 03:52:36 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte GiftsLimiterDelay = DefaultGiftsLimiterDelay;
2016-03-18 03:52:36 +01:00
2016-03-06 23:28:56 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly bool Headless = DefaultHeadless;
2016-03-11 02:07:20 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte IdleFarmingPeriod = DefaultIdleFarmingPeriod;
2016-03-11 02:07:20 +01:00
2016-03-09 19:25:45 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte InventoryLimiterDelay = DefaultInventoryLimiterDelay;
2016-03-06 23:28:56 +01:00
2018-04-18 01:04:28 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly bool IPC = DefaultIPC;
2018-04-18 01:04:28 +02:00
2017-12-03 13:26:22 +01:00
[JsonProperty]
2019-01-14 21:50:23 +01:00
public readonly string IPCPassword = DefaultIPCPassword;
2017-12-03 13:26:22 +01:00
2016-03-06 23:28:56 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte LoginLimiterDelay = DefaultLoginLimiterDelay;
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte MaxFarmingTime = DefaultMaxFarmingTime;
2016-03-06 23:28:56 +01:00
2016-06-24 22:26:52 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte MaxTradeHoldDuration = DefaultMaxTradeHoldDuration;
2016-06-24 22:26:52 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly EOptimizationMode OptimizationMode = DefaultOptimizationMode;
2016-06-27 01:45:41 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly bool Statistics = DefaultStatistics;
2016-06-27 01:45:41 +02:00
2018-07-26 02:25:57 +02:00
[JsonProperty]
2019-01-14 21:50:23 +01:00
public readonly string SteamMessagePrefix = DefaultSteamMessagePrefix;
2018-07-26 02:25:57 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly EUpdateChannel UpdateChannel = DefaultUpdateChannel;
2016-03-06 23:28:56 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly byte UpdatePeriod = DefaultUpdatePeriod;
2018-04-13 09:17:27 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2019-01-14 21:50:23 +01:00
public readonly ushort WebLimiterDelay = DefaultWebLimiterDelay;
2018-04-13 09:17:27 +02:00
2018-07-17 21:10:18 +02:00
[JsonProperty(PropertyName = nameof(WebProxy))]
2019-01-14 21:50:23 +01:00
public readonly string WebProxyText = DefaultWebProxyText;
[JsonProperty]
2019-01-14 21:50:23 +01:00
public readonly string WebProxyUsername = DefaultWebProxyUsername;
2018-07-17 21:10:18 +02:00
internal WebProxy WebProxy {
get {
if (_WebProxy != null) {
return _WebProxy;
}
2018-07-17 21:10:18 +02:00
if (string.IsNullOrEmpty(WebProxyText)) {
return null;
}
2018-07-17 21:10:18 +02:00
Uri uri;
2018-07-17 21:10:18 +02:00
try {
uri = new Uri(WebProxyText);
} catch (UriFormatException e) {
ASF.ArchiLogger.LogGenericException(e);
2018-12-15 00:27:15 +01:00
2018-07-17 21:10:18 +02:00
return null;
}
2018-07-17 21:10:18 +02:00
_WebProxy = new WebProxy {
Address = uri,
BypassProxyOnLocal = true
};
2018-07-17 21:10:18 +02:00
if (!string.IsNullOrEmpty(WebProxyUsername) || !string.IsNullOrEmpty(WebProxyPassword)) {
NetworkCredential credentials = new NetworkCredential();
2018-07-17 21:10:18 +02:00
if (!string.IsNullOrEmpty(WebProxyUsername)) {
credentials.UserName = WebProxyUsername;
}
2018-07-17 21:10:18 +02:00
if (!string.IsNullOrEmpty(WebProxyPassword)) {
credentials.Password = WebProxyPassword;
}
2018-07-17 21:10:18 +02:00
_WebProxy.Credentials = credentials;
}
2018-07-17 21:10:18 +02:00
return _WebProxy;
}
}
2019-01-17 16:37:16 +01:00
[JsonProperty(Required = Required.DisallowNull)]
public ulong SteamOwnerID { get; private set; } = DefaultSteamOwnerID;
[JsonProperty(Required = Required.DisallowNull)]
public ProtocolTypes SteamProtocols { get; private set; } = DefaultSteamProtocols;
[JsonExtensionData]
internal Dictionary<string, JToken> AdditionalProperties { get; set; }
internal bool IsWebProxyPasswordSet { get; private set; }
2018-08-06 01:21:36 +02:00
internal bool ShouldSerializeEverything { private get; set; } = true;
internal bool ShouldSerializeHelperProperties { private get; set; } = true;
2018-08-06 01:21:36 +02:00
2018-07-17 21:10:18 +02:00
[JsonProperty]
internal string WebProxyPassword {
get => _WebProxyPassword;
2018-12-15 00:27:15 +01:00
set {
IsWebProxyPasswordSet = true;
_WebProxyPassword = value;
}
}
2018-07-17 21:10:18 +02:00
private WebProxy _WebProxy;
private string _WebProxyPassword = DefaultWebProxyPassword;
private bool ShouldSerializeSensitiveDetails = true;
2018-02-11 11:11:38 +01:00
[JsonProperty(PropertyName = SharedInfo.UlongCompatibilityStringPrefix + nameof(SteamOwnerID), Required = Required.DisallowNull)]
2019-01-10 23:44:32 +01:00
[NotNull]
2018-02-11 09:58:25 +01:00
private string SSteamOwnerID {
2018-02-11 09:57:10 +01:00
get => SteamOwnerID.ToString();
2018-12-15 00:27:15 +01:00
2018-02-11 09:58:25 +01:00
set {
2017-07-01 13:39:53 +02:00
if (string.IsNullOrEmpty(value) || !ulong.TryParse(value, out ulong result)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsInvalid, nameof(SSteamOwnerID)));
2018-12-15 00:27:15 +01:00
2017-07-01 13:39:53 +02:00
return;
}
SteamOwnerID = result;
}
}
2019-01-14 21:50:23 +01:00
[JsonConstructor]
private GlobalConfig() { }
internal (bool Valid, string ErrorMessage) CheckValidation() {
if (ConnectionTimeout == 0) {
return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(ConnectionTimeout), ConnectionTimeout));
2016-04-12 19:12:45 +02:00
}
if (FarmingDelay == 0) {
return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(FarmingDelay), FarmingDelay));
2016-03-06 23:28:56 +01:00
}
if (MaxFarmingTime == 0) {
return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(MaxFarmingTime), MaxFarmingTime));
2016-03-06 23:28:56 +01:00
}
if (!Enum.IsDefined(typeof(EOptimizationMode), OptimizationMode)) {
return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(OptimizationMode), OptimizationMode));
2016-03-28 13:11:02 +02:00
}
if (!string.IsNullOrEmpty(SteamMessagePrefix) && (SteamMessagePrefix.Length > Bot.MaxMessagePrefixLength)) {
return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(SteamMessagePrefix), SteamMessagePrefix));
}
2019-02-01 22:41:25 +01:00
if ((SteamOwnerID != 0) && !new SteamID(SteamOwnerID).IsIndividualAccount) {
return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(SteamOwnerID), SteamOwnerID));
}
if ((SteamProtocols <= 0) || (SteamProtocols > ProtocolTypes.All)) {
return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(SteamProtocols), SteamProtocols));
}
return Enum.IsDefined(typeof(EUpdateChannel), UpdateChannel) ? (true, null) : (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(UpdateChannel), UpdateChannel));
}
2019-01-10 23:44:32 +01:00
[NotNull]
2018-10-07 03:21:32 +02:00
internal static GlobalConfig Create() =>
new GlobalConfig {
ShouldSerializeEverything = false,
ShouldSerializeSensitiveDetails = false
};
2019-01-10 23:44:32 +01:00
[ItemCanBeNull]
internal static async Task<GlobalConfig> Load(string filePath) {
if (string.IsNullOrEmpty(filePath)) {
ASF.ArchiLogger.LogNullError(nameof(filePath));
2018-12-15 00:27:15 +01:00
2018-03-20 10:15:52 +01:00
return null;
}
if (!File.Exists(filePath)) {
2018-07-04 19:13:51 +02:00
return null;
}
GlobalConfig globalConfig;
try {
globalConfig = JsonConvert.DeserializeObject<GlobalConfig>(await RuntimeCompatibility.File.ReadAllTextAsync(filePath).ConfigureAwait(false));
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
2018-12-15 00:27:15 +01:00
2018-07-26 02:25:57 +02:00
return null;
}
if (globalConfig == null) {
ASF.ArchiLogger.LogNullError(nameof(globalConfig));
2018-12-15 00:27:15 +01:00
2016-07-25 06:08:45 +02:00
return null;
}
(bool valid, string errorMessage) = globalConfig.CheckValidation();
2018-12-15 00:27:15 +01:00
if (!valid) {
ASF.ArchiLogger.LogGenericError(errorMessage);
2018-12-15 00:27:15 +01:00
2018-07-04 19:13:51 +02:00
return null;
}
2018-08-06 01:21:36 +02:00
globalConfig.ShouldSerializeEverything = false;
2018-07-04 19:13:51 +02:00
globalConfig.ShouldSerializeSensitiveDetails = false;
2018-12-15 00:27:15 +01:00
2018-03-09 15:43:25 +01:00
return globalConfig;
2016-03-06 23:28:56 +01:00
}
internal static async Task<bool> Write(string filePath, GlobalConfig globalConfig) {
if (string.IsNullOrEmpty(filePath) || (globalConfig == null)) {
ASF.ArchiLogger.LogNullError(nameof(filePath) + " || " + nameof(globalConfig));
2018-12-15 00:27:15 +01:00
return false;
}
string json = JsonConvert.SerializeObject(globalConfig, Formatting.Indented);
string newFilePath = filePath + ".new";
await WriteSemaphore.WaitAsync().ConfigureAwait(false);
try {
await RuntimeCompatibility.File.WriteAllTextAsync(newFilePath, json).ConfigureAwait(false);
if (File.Exists(filePath)) {
File.Replace(newFilePath, filePath, null);
} else {
File.Move(newFilePath, filePath);
}
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
2018-12-15 00:27:15 +01:00
return false;
} finally {
WriteSemaphore.Release();
}
return true;
}
2019-01-14 21:50:23 +01:00
public enum EOptimizationMode : byte {
MaxPerformance,
2017-06-29 23:12:16 +02:00
MinMemoryUsage
}
2019-01-14 21:50:23 +01:00
public enum EUpdateChannel : byte {
None,
Stable,
2019-01-14 21:50:23 +01:00
[PublicAPI]
Experimental
}
2018-08-06 01:21:36 +02:00
// ReSharper disable UnusedMember.Global
public bool ShouldSerializeAutoRestart() => ShouldSerializeEverything || (AutoRestart != DefaultAutoRestart);
public bool ShouldSerializeBlacklist() => ShouldSerializeEverything || ((Blacklist != DefaultBlacklist) && !Blacklist.SetEquals(DefaultBlacklist));
public bool ShouldSerializeCommandPrefix() => ShouldSerializeEverything || (CommandPrefix != DefaultCommandPrefix);
public bool ShouldSerializeConfirmationsLimiterDelay() => ShouldSerializeEverything || (ConfirmationsLimiterDelay != DefaultConfirmationsLimiterDelay);
public bool ShouldSerializeConnectionTimeout() => ShouldSerializeEverything || (ConnectionTimeout != DefaultConnectionTimeout);
public bool ShouldSerializeCurrentCulture() => ShouldSerializeEverything || (CurrentCulture != DefaultCurrentCulture);
public bool ShouldSerializeDebug() => ShouldSerializeEverything || (Debug != DefaultDebug);
public bool ShouldSerializeFarmingDelay() => ShouldSerializeEverything || (FarmingDelay != DefaultFarmingDelay);
public bool ShouldSerializeGiftsLimiterDelay() => ShouldSerializeEverything || (GiftsLimiterDelay != DefaultGiftsLimiterDelay);
public bool ShouldSerializeHeadless() => ShouldSerializeEverything || (Headless != DefaultHeadless);
public bool ShouldSerializeIdleFarmingPeriod() => ShouldSerializeEverything || (IdleFarmingPeriod != DefaultIdleFarmingPeriod);
public bool ShouldSerializeInventoryLimiterDelay() => ShouldSerializeEverything || (InventoryLimiterDelay != DefaultInventoryLimiterDelay);
public bool ShouldSerializeIPC() => ShouldSerializeEverything || (IPC != DefaultIPC);
public bool ShouldSerializeIPCPassword() => ShouldSerializeEverything || (IPCPassword != DefaultIPCPassword);
public bool ShouldSerializeLoginLimiterDelay() => ShouldSerializeEverything || (LoginLimiterDelay != DefaultLoginLimiterDelay);
public bool ShouldSerializeMaxFarmingTime() => ShouldSerializeEverything || (MaxFarmingTime != DefaultMaxFarmingTime);
public bool ShouldSerializeMaxTradeHoldDuration() => ShouldSerializeEverything || (MaxTradeHoldDuration != DefaultMaxTradeHoldDuration);
public bool ShouldSerializeOptimizationMode() => ShouldSerializeEverything || (OptimizationMode != DefaultOptimizationMode);
public bool ShouldSerializeSSteamOwnerID() => ShouldSerializeEverything || (ShouldSerializeHelperProperties && (SteamOwnerID != DefaultSteamOwnerID));
2018-08-06 01:21:36 +02:00
public bool ShouldSerializeStatistics() => ShouldSerializeEverything || (Statistics != DefaultStatistics);
public bool ShouldSerializeSteamMessagePrefix() => ShouldSerializeEverything || (SteamMessagePrefix != DefaultSteamMessagePrefix);
public bool ShouldSerializeSteamOwnerID() => ShouldSerializeEverything || (SteamOwnerID != DefaultSteamOwnerID);
public bool ShouldSerializeSteamProtocols() => ShouldSerializeEverything || (SteamProtocols != DefaultSteamProtocols);
public bool ShouldSerializeUpdateChannel() => ShouldSerializeEverything || (UpdateChannel != DefaultUpdateChannel);
public bool ShouldSerializeUpdatePeriod() => ShouldSerializeEverything || (UpdatePeriod != DefaultUpdatePeriod);
public bool ShouldSerializeWebLimiterDelay() => ShouldSerializeEverything || (WebLimiterDelay != DefaultWebLimiterDelay);
public bool ShouldSerializeWebProxyPassword() => ShouldSerializeSensitiveDetails && (ShouldSerializeEverything || (WebProxyPassword != DefaultWebProxyPassword));
public bool ShouldSerializeWebProxyText() => ShouldSerializeEverything || (WebProxyText != DefaultWebProxyText);
public bool ShouldSerializeWebProxyUsername() => ShouldSerializeEverything || (WebProxyUsername != DefaultWebProxyUsername);
// ReSharper restore UnusedMember.Global
2016-03-06 23:28:56 +01:00
}
2018-08-06 01:21:36 +02:00
}