Files
ArchiSteamFarm/ArchiSteamFarm/GlobalConfig.cs

231 lines
7.8 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 23:28:56 +01:00
using System;
using System.Collections.Generic;
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.Threading;
using System.Threading.Tasks;
2017-01-06 16:29:34 +01:00
using ArchiSteamFarm.Localization;
using Newtonsoft.Json;
using SteamKit2;
2016-03-06 23:28:56 +01:00
namespace ArchiSteamFarm {
[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")]
2016-03-06 23:28:56 +01:00
internal sealed class GlobalConfig {
internal const byte DefaultConnectionTimeout = 60;
internal const ushort DefaultIPCPort = 1242;
2017-06-26 02:17:02 +02:00
internal const byte DefaultLoginLimiterDelay = 10;
2018-03-20 10:15:52 +01:00
private const ProtocolTypes DefaultSteamProtocols = ProtocolTypes.Tcp | ProtocolTypes.Udp;
2017-12-14 18:34:02 +01:00
internal static readonly HashSet<uint> SalesBlacklist = new HashSet<uint> { 267420, 303700, 335590, 368020, 425280, 480730, 566020, 639900, 762800 }; // Steam Summer/Winter sales
2016-03-06 23:28:56 +01:00
private static readonly SemaphoreSlim WriteSemaphore = new SemaphoreSlim(1, 1);
2016-03-07 18:12:05 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly bool AutoRestart = true;
2016-03-07 18:12:05 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte BackgroundGCPeriod;
[JsonProperty(Required = Required.DisallowNull)]
internal readonly HashSet<uint> Blacklist = new HashSet<uint>();
2016-03-06 23:28:56 +01:00
[JsonProperty]
2018-03-15 17:33:09 +01:00
internal readonly string CommandPrefix = "!";
2018-01-12 19:43:34 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte ConfirmationsLimiterDelay = 10;
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte ConnectionTimeout = DefaultConnectionTimeout;
2017-01-08 05:32:59 +01:00
[JsonProperty]
2017-04-05 14:30:14 +02:00
internal readonly string CurrentCulture;
2017-01-08 05:32:59 +01:00
2016-04-18 18:38:48 +02:00
[JsonProperty(Required = Required.DisallowNull)]
2017-04-05 14:30:14 +02:00
internal readonly bool Debug;
2016-04-18 18:38:48 +02:00
2016-03-06 23:28:56 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte FarmingDelay = 15;
2016-03-06 23:28:56 +01:00
2016-03-18 03:52:36 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte GiftsLimiterDelay = 1;
2016-03-18 03:52:36 +01:00
2016-03-06 23:28:56 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-04-05 14:30:14 +02:00
internal readonly bool Headless;
2016-03-11 02:07:20 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-08-16 01:08:30 +02:00
internal readonly byte IdleFarmingPeriod = 8;
2016-03-11 02:07:20 +01:00
2016-03-09 19:25:45 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte InventoryLimiterDelay = 3;
2016-03-06 23:28:56 +01:00
2017-12-03 13:26:22 +01:00
[JsonProperty]
internal readonly string IPCPassword;
[JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, Required = Required.DisallowNull)]
internal readonly HashSet<string> IPCPrefixes = new HashSet<string> { "http://127.0.0.1:" + DefaultIPCPort + "/" };
2016-03-06 23:28:56 +01:00
[JsonProperty(Required = Required.DisallowNull)]
2017-06-26 02:17:02 +02:00
internal readonly byte LoginLimiterDelay = DefaultLoginLimiterDelay;
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte MaxFarmingTime = 10;
2016-03-06 23:28:56 +01:00
2016-06-24 22:26:52 +02:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte MaxTradeHoldDuration = 15;
2016-06-24 22:26:52 +02:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly EOptimizationMode OptimizationMode = EOptimizationMode.MaxPerformance;
2016-06-27 01:45:41 +02:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly bool Statistics = true;
2016-06-27 01:45:41 +02:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly EUpdateChannel UpdateChannel = EUpdateChannel.Stable;
2016-03-06 23:28:56 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly byte UpdatePeriod = 24;
2018-04-13 09:17:27 +02:00
[JsonProperty(Required = Required.DisallowNull)]
internal readonly ushort WebLimiterDelay = 200;
2018-02-11 09:58:25 +01:00
[JsonProperty(Required = Required.DisallowNull)]
internal ulong SteamOwnerID { get; private set; }
[JsonProperty(Required = Required.DisallowNull)]
2018-03-20 10:15:52 +01:00
internal ProtocolTypes SteamProtocols { get; private set; } = DefaultSteamProtocols;
2018-02-11 09:58:25 +01:00
2018-02-11 11:11:38 +01:00
[JsonProperty(PropertyName = SharedInfo.UlongCompatibilityStringPrefix + nameof(SteamOwnerID), Required = Required.DisallowNull)]
2018-02-11 09:58:25 +01:00
private string SSteamOwnerID {
2018-02-11 09:57:10 +01:00
get => SteamOwnerID.ToString();
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)));
return;
}
SteamOwnerID = result;
}
}
2016-04-12 19:12:45 +02:00
internal static GlobalConfig Load(string filePath) {
if (string.IsNullOrEmpty(filePath)) {
ASF.ArchiLogger.LogNullError(nameof(filePath));
2016-04-12 19:12:45 +02:00
return null;
}
2016-03-06 23:28:56 +01:00
if (!File.Exists(filePath)) {
return null;
}
GlobalConfig globalConfig;
2016-05-30 01:57:06 +02:00
2016-03-06 23:28:56 +01:00
try {
globalConfig = JsonConvert.DeserializeObject<GlobalConfig>(File.ReadAllText(filePath));
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
2016-03-06 23:28:56 +01:00
return null;
}
2016-03-28 13:11:02 +02:00
if (globalConfig == null) {
ASF.ArchiLogger.LogNullError(nameof(globalConfig));
2016-03-28 13:11:02 +02:00
return null;
}
// User might not know what he's doing
// Ensure that he can't screw core ASF variables
2018-03-20 10:15:52 +01:00
if (globalConfig.ConnectionTimeout == 0) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.ConnectionTimeout), globalConfig.ConnectionTimeout));
2016-07-25 06:08:45 +02:00
return null;
}
if (globalConfig.FarmingDelay == 0) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.FarmingDelay), globalConfig.FarmingDelay));
2016-07-25 06:08:45 +02:00
return null;
}
2018-03-20 10:15:52 +01:00
if (globalConfig.MaxFarmingTime == 0) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.MaxFarmingTime), globalConfig.MaxFarmingTime));
return null;
}
if (globalConfig.SteamProtocols == 0) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.SteamProtocols), globalConfig.SteamProtocols));
2016-07-25 06:08:45 +02:00
return null;
}
if (globalConfig.SteamProtocols.HasFlag(ProtocolTypes.WebSocket) && !OS.SupportsWebSockets()) {
2017-09-26 06:31:30 +02:00
globalConfig.SteamProtocols &= ~ProtocolTypes.WebSocket;
if (globalConfig.SteamProtocols == 0) {
2018-03-20 10:15:52 +01:00
globalConfig.SteamProtocols = DefaultSteamProtocols;
}
}
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));
return false;
}
string json = JsonConvert.SerializeObject(globalConfig, Formatting.Indented);
string newFilePath = filePath + ".new";
await WriteSemaphore.WaitAsync().ConfigureAwait(false);
try {
await 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);
return false;
} finally {
WriteSemaphore.Release();
}
return true;
}
internal enum EOptimizationMode : byte {
MaxPerformance,
2017-06-29 23:12:16 +02:00
MinMemoryUsage
}
internal enum EUpdateChannel : byte {
None,
Stable,
2017-09-21 03:16:42 +02:00
[SuppressMessage("ReSharper", "UnusedMember.Global")]
Experimental
}
2016-03-06 23:28:56 +01:00
}
}