Files
ArchiSteamFarm/ArchiSteamFarm/Program.cs

583 lines
19 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.
2015-10-28 19:21:27 +01:00
using System;
using System.Collections;
2016-03-09 03:10:33 +01:00
using System.Collections.Generic;
using System.Diagnostics;
2017-01-08 05:32:59 +01:00
using System.Globalization;
2015-10-25 06:16:50 +01:00
using System.IO;
2016-03-09 03:52:04 +01:00
using System.Linq;
using System.Reflection;
2017-01-20 22:48:44 +01:00
using System.Resources;
2021-05-11 23:10:16 +02:00
using System.Text;
2015-10-29 17:36:16 +01:00
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers;
using ArchiSteamFarm.IPC;
2017-01-06 13:20:36 +01:00
using ArchiSteamFarm.Localization;
2018-09-08 01:03:55 +02:00
using ArchiSteamFarm.NLog;
using ArchiSteamFarm.NLog.Targets;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Storage;
2021-05-06 20:16:06 +02:00
using ArchiSteamFarm.Web;
2018-02-25 18:57:06 +01:00
using Newtonsoft.Json;
2017-06-26 01:37:14 +02:00
using NLog;
using NLog.Targets;
using SteamKit2;
2015-10-25 06:16:50 +01:00
namespace ArchiSteamFarm {
internal static class Program {
2021-05-20 22:42:30 +02:00
internal static bool ConfigMigrate { get; private set; } = true;
internal static bool ConfigWatch { get; private set; } = true;
internal static string? NetworkGroup { get; private set; }
2018-04-18 01:04:28 +02:00
internal static bool ProcessRequired { get; private set; }
internal static bool RestartAllowed { get; private set; } = true;
2019-02-23 04:31:58 +01:00
internal static bool ShutdownSequenceInitialized { get; private set; }
2017-08-02 19:36:43 +02:00
2020-11-14 22:37:00 +01:00
private static readonly TaskCompletionSource<byte> ShutdownResetEvent = new();
private static bool IgnoreUnsupportedEnvironment;
2018-04-18 01:11:46 +02:00
private static bool SystemRequired;
internal static async Task Exit(byte exitCode = 0) {
2017-01-06 13:20:36 +01:00
if (exitCode != 0) {
ASF.ArchiLogger.LogGenericError(Strings.ErrorExitingWithNonZeroErrorCode);
2017-01-06 13:20:36 +01:00
}
2018-04-18 01:04:28 +02:00
await Shutdown(exitCode).ConfigureAwait(false);
2016-04-02 13:08:43 +02:00
Environment.Exit(exitCode);
}
internal static async Task Restart() {
if (!await InitShutdownSequence().ConfigureAwait(false)) {
return;
}
2020-08-23 20:45:24 +02:00
string executableName = Path.GetFileNameWithoutExtension(OS.ProcessFileName);
2018-12-15 00:27:15 +01:00
2018-06-11 01:24:54 +02:00
if (string.IsNullOrEmpty(executableName)) {
throw new ArgumentNullException(nameof(executableName));
2018-06-11 01:24:54 +02:00
}
2020-11-14 22:37:00 +01:00
IEnumerable<string> arguments = Environment.GetCommandLineArgs().Skip(executableName.Equals(SharedInfo.AssemblyName, StringComparison.Ordinal) ? 1 : 0);
try {
2019-12-08 00:32:59 +01:00
Process.Start(OS.ProcessFileName, string.Join(" ", arguments));
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
2017-07-05 07:07:03 +02:00
// Give new process some time to take over the window (if needed)
2017-07-05 07:49:40 +02:00
await Task.Delay(2000).ConfigureAwait(false);
2017-07-05 07:07:03 +02:00
2018-04-18 01:04:28 +02:00
ShutdownResetEvent.TrySetResult(0);
Environment.Exit(0);
}
private static void HandleCryptKeyArgument(string cryptKey) {
if (string.IsNullOrEmpty(cryptKey)) {
throw new ArgumentNullException(nameof(cryptKey));
}
2018-08-19 14:37:23 +02:00
ArchiCryptoHelper.SetEncryptionKey(cryptKey);
}
2020-05-26 23:24:43 +02:00
private static void HandleNetworkGroupArgument(string networkGroup) {
if (string.IsNullOrEmpty(networkGroup)) {
throw new ArgumentNullException(nameof(networkGroup));
2020-05-26 23:24:43 +02:00
}
NetworkGroup = networkGroup;
}
private static void HandlePathArgument(string path) {
if (string.IsNullOrEmpty(path)) {
throw new ArgumentNullException(nameof(path));
}
try {
Directory.SetCurrentDirectory(path);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
}
private static async Task Init(IReadOnlyCollection<string>? args) {
2017-07-10 17:07:48 +02:00
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
2021-05-11 23:10:16 +02:00
// Add support for custom encodings
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Add support for custom logging targets
2018-01-31 02:42:23 +01:00
Target.Register<HistoryTarget>(HistoryTarget.TargetName);
2017-07-02 10:10:30 +02:00
Target.Register<SteamTarget>(SteamTarget.TargetName);
2016-07-19 22:18:00 +02:00
if (!await InitCore(args).ConfigureAwait(false) || !await InitASF().ConfigureAwait(false)) {
await Exit(1).ConfigureAwait(false);
}
2017-02-05 14:32:38 +01:00
}
private static async Task<bool> InitASF() {
if (!await InitGlobalConfigAndLanguage().ConfigureAwait(false)) {
return false;
}
if (ASF.GlobalConfig == null) {
2020-11-14 22:37:00 +01:00
throw new InvalidOperationException(nameof(ASF.GlobalConfig));
}
2017-02-05 09:02:27 +01:00
OS.Init(ASF.GlobalConfig.OptimizationMode);
2018-04-18 01:11:46 +02:00
2020-11-19 21:08:50 +01:00
if (!await InitGlobalDatabaseAndServices().ConfigureAwait(false)) {
return false;
}
await ASF.Init().ConfigureAwait(false);
return true;
}
private static async Task<bool> InitCore(IReadOnlyCollection<string>? args) {
2018-05-23 15:28:02 +02:00
Directory.SetCurrentDirectory(SharedInfo.HomeDirectory);
// Allow loading configs from source tree if it's a debug build
if (Debugging.IsDebugBuild) {
// Common structure is bin/(x64/)Debug/ArchiSteamFarm.exe, so we allow up to 4 directories up
for (byte i = 0; i < 4; i++) {
Directory.SetCurrentDirectory("..");
2018-12-15 00:27:15 +01:00
2018-05-23 15:28:02 +02:00
if (Directory.Exists(SharedInfo.ConfigDirectory)) {
break;
2017-02-05 14:32:38 +01:00
}
2018-05-23 15:28:02 +02:00
}
2017-02-05 14:32:38 +01:00
2018-05-23 15:28:02 +02:00
// If config directory doesn't exist after our adjustment, abort all of that
if (!Directory.Exists(SharedInfo.ConfigDirectory)) {
Directory.SetCurrentDirectory(SharedInfo.HomeDirectory);
2017-02-05 14:32:38 +01:00
}
}
// Parse args
2017-02-05 14:32:38 +01:00
if (args != null) {
ParseArgs(args);
2017-02-05 14:32:38 +01:00
}
2020-11-26 18:22:55 +01:00
bool uniqueInstance = await OS.RegisterProcess().ConfigureAwait(false);
Logging.InitCoreLoggers(uniqueInstance);
if (!uniqueInstance) {
ASF.ArchiLogger.LogGenericError(Strings.ErrorSingleInstanceRequired);
await Task.Delay(10000).ConfigureAwait(false);
return false;
}
OS.CoreInit(SystemRequired);
Console.Title = SharedInfo.ProgramIdentifier;
ASF.ArchiLogger.LogGenericInfo(SharedInfo.ProgramIdentifier);
string? copyright = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyCopyrightAttribute>()?.Copyright;
if (!string.IsNullOrEmpty(copyright)) {
ASF.ArchiLogger.LogGenericInfo(copyright!);
}
if (!IgnoreUnsupportedEnvironment) {
if (!OS.VerifyEnvironment()) {
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnsupportedEnvironment, SharedInfo.BuildInfo.Variant, OS.Variant));
await Task.Delay(10000).ConfigureAwait(false);
return false;
}
}
if (!Directory.Exists(SharedInfo.ConfigDirectory)) {
ASF.ArchiLogger.LogGenericError(Strings.ErrorConfigDirectoryNotFound);
await Task.Delay(10000).ConfigureAwait(false);
return false;
}
return true;
2017-02-05 14:32:38 +01:00
}
private static async Task<bool> InitGlobalConfigAndLanguage() {
string globalConfigFile = ASF.GetFilePath(ASF.EFileType.Config);
if (string.IsNullOrEmpty(globalConfigFile)) {
throw new ArgumentNullException(nameof(globalConfigFile));
}
2016-07-31 17:38:14 +02:00
string? latestJson = null;
GlobalConfig? globalConfig;
2019-01-14 21:50:23 +01:00
2018-10-07 03:21:32 +02:00
if (File.Exists(globalConfigFile)) {
(globalConfig, latestJson) = await GlobalConfig.Load(globalConfigFile).ConfigureAwait(false);
2018-12-15 00:27:15 +01:00
2019-01-14 21:50:23 +01:00
if (globalConfig == null) {
2020-11-14 22:37:00 +01:00
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorGlobalConfigNotLoaded, globalConfigFile));
2020-11-19 21:08:50 +01:00
await Task.Delay(5000).ConfigureAwait(false);
2018-12-15 00:27:15 +01:00
return false;
2018-10-07 03:21:32 +02:00
}
} else {
globalConfig = new GlobalConfig();
2017-01-08 05:32:59 +01:00
}
if (globalConfig.Debug) {
2021-03-29 21:47:25 +02:00
ASF.ArchiLogger.LogGenericDebug(globalConfigFile + ": " + JsonConvert.SerializeObject(globalConfig, Formatting.Indented));
2018-02-25 18:57:06 +01:00
}
2021-03-29 21:47:25 +02:00
if (!string.IsNullOrEmpty(globalConfig.CurrentCulture)) {
2017-01-08 05:32:59 +01:00
try {
// GetCultureInfo() would be better but we can't use it for specifying neutral cultures such as "en"
2021-03-29 21:47:25 +02:00
CultureInfo culture = CultureInfo.CreateSpecificCulture(globalConfig.CurrentCulture!);
2017-01-08 05:32:59 +01:00
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = culture;
} catch (Exception e) {
ASF.ArchiLogger.LogGenericWarningException(e);
ASF.ArchiLogger.LogGenericError(Strings.ErrorInvalidCurrentCulture);
2017-01-08 05:32:59 +01:00
}
2021-03-29 21:42:42 +02:00
} else {
// April Fools easter egg
DateTime now = DateTime.Now;
if ((now.Month == 4) && (now.Day == 1)) {
try {
2021-03-30 10:14:23 +02:00
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("qps-Ploc");
2021-03-29 21:42:42 +02:00
} catch (Exception e) {
ASF.ArchiLogger.LogGenericDebuggingException(e);
}
}
2016-03-06 23:28:56 +01:00
}
if (!string.IsNullOrEmpty(latestJson)) {
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.AutomaticFileMigration, globalConfigFile));
await SerializableFile.Write(globalConfigFile, latestJson!).ConfigureAwait(false);
ASF.ArchiLogger.LogGenericInfo(Strings.Done);
}
2021-06-23 12:51:24 +02:00
ASF.GlobalConfig = globalConfig;
// Skip translation progress for English and invariant (such as "C") cultures
switch (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName) {
case "en":
case "iv":
2021-03-29 21:42:42 +02:00
case "qps":
return true;
}
// We can't dispose this resource set, as we can't be sure if it isn't used somewhere else, rely on GC in this case
ResourceSet? defaultResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.GetCultureInfo("en-US"), true, true);
2018-12-15 00:27:15 +01:00
if (defaultResourceSet == null) {
ASF.ArchiLogger.LogNullError(nameof(defaultResourceSet));
2018-12-15 00:27:15 +01:00
return true;
2017-01-20 22:48:44 +01:00
}
2018-06-09 00:45:15 +02:00
HashSet<DictionaryEntry> defaultStringObjects = defaultResourceSet.Cast<DictionaryEntry>().ToHashSet();
2018-12-15 00:27:15 +01:00
if (defaultStringObjects.Count == 0) {
ASF.ArchiLogger.LogNullError(nameof(defaultStringObjects));
2018-12-15 00:27:15 +01:00
return true;
2017-02-07 17:25:32 +01:00
}
// We can't dispose this resource set, as we can't be sure if it isn't used somewhere else, rely on GC in this case
ResourceSet? currentResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
2018-12-15 00:27:15 +01:00
if (currentResourceSet == null) {
ASF.ArchiLogger.LogNullError(nameof(currentResourceSet));
2018-12-15 00:27:15 +01:00
return true;
2017-01-20 22:48:44 +01:00
}
2018-06-09 00:45:15 +02:00
HashSet<DictionaryEntry> currentStringObjects = currentResourceSet.Cast<DictionaryEntry>().ToHashSet();
2018-12-15 00:27:15 +01:00
if (currentStringObjects.Count >= defaultStringObjects.Count) {
// Either we have 100% finished translation, or we're missing it entirely and using en-US
2018-06-09 00:45:15 +02:00
HashSet<DictionaryEntry> testStringObjects = currentStringObjects.ToHashSet();
testStringObjects.ExceptWith(defaultStringObjects);
// If we got 0 as final result, this is the missing language
// Otherwise it's just a small amount of strings that happen to be the same
if (testStringObjects.Count == 0) {
currentStringObjects = testStringObjects;
}
2017-01-20 22:48:44 +01:00
}
if (currentStringObjects.Count < defaultStringObjects.Count) {
float translationCompleteness = currentStringObjects.Count / (float) defaultStringObjects.Count;
2020-11-25 21:15:42 +01:00
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.TranslationIncomplete, CultureInfo.CurrentUICulture.Name + " (" + CultureInfo.CurrentUICulture.EnglishName + ")", translationCompleteness.ToString("P1", CultureInfo.CurrentCulture)));
}
return true;
2017-02-05 09:02:27 +01:00
}
2017-01-20 22:48:44 +01:00
2020-11-19 21:08:50 +01:00
private static async Task<bool> InitGlobalDatabaseAndServices() {
string globalDatabaseFile = ASF.GetFilePath(ASF.EFileType.Database);
if (string.IsNullOrEmpty(globalDatabaseFile)) {
throw new ArgumentNullException(nameof(globalDatabaseFile));
}
2016-07-31 17:38:14 +02:00
2017-01-06 22:18:41 +01:00
if (!File.Exists(globalDatabaseFile)) {
ASF.ArchiLogger.LogGenericInfo(Strings.Welcome);
2020-11-19 21:08:50 +01:00
await Task.Delay(10000).ConfigureAwait(false);
2017-11-29 02:07:57 +01:00
ASF.ArchiLogger.LogGenericWarning(Strings.WarningPrivacyPolicy);
2020-11-19 21:08:50 +01:00
await Task.Delay(5000).ConfigureAwait(false);
2017-01-06 22:18:41 +01:00
}
GlobalDatabase? globalDatabase = await GlobalDatabase.CreateOrLoad(globalDatabaseFile).ConfigureAwait(false);
2018-12-15 00:27:15 +01:00
2019-01-17 20:43:45 +01:00
if (globalDatabase == null) {
2020-11-14 22:37:00 +01:00
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorDatabaseInvalid, globalDatabaseFile));
2020-11-19 21:08:50 +01:00
await Task.Delay(5000).ConfigureAwait(false);
2018-12-15 00:27:15 +01:00
2020-11-19 21:08:50 +01:00
return false;
2016-03-07 02:39:55 +01:00
}
2021-06-23 12:51:24 +02:00
ASF.GlobalDatabase = globalDatabase;
2019-01-17 20:43:45 +01:00
// If debugging is on, we prepare debug directory prior to running
2018-05-18 21:12:33 +02:00
if (Debugging.IsUserDebugging) {
2019-09-17 19:05:56 +02:00
if (Debugging.IsDebugConfigured) {
ASF.ArchiLogger.LogGenericDebug(globalDatabaseFile + ": " + JsonConvert.SerializeObject(ASF.GlobalDatabase, Formatting.Indented));
}
2018-04-18 01:04:28 +02:00
Logging.EnableTraceLogging();
if (Debugging.IsDebugConfigured) {
DebugLog.AddListener(new Debugging.DebugListener());
DebugLog.Enabled = true;
if (Directory.Exists(SharedInfo.DebugDirectory)) {
try {
Directory.Delete(SharedInfo.DebugDirectory, true);
await Task.Delay(1000).ConfigureAwait(false); // Dirty workaround giving Windows some time to sync
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
2018-04-18 01:04:28 +02:00
}
}
2019-04-03 18:02:25 +02:00
try {
Directory.CreateDirectory(SharedInfo.DebugDirectory);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
2018-04-18 01:04:28 +02:00
}
WebBrowser.Init();
2020-11-19 21:08:50 +01:00
return true;
2015-11-25 16:31:39 +01:00
}
private static async Task<bool> InitShutdownSequence() {
if (ShutdownSequenceInitialized) {
return false;
}
ShutdownSequenceInitialized = true;
// Sockets created by IPC might still be running for a short while after complete app shutdown
2017-11-30 23:39:35 +01:00
// Ensure that IPC is stopped before we finalize shutdown sequence
await ArchiKestrel.Stop().ConfigureAwait(false);
2017-11-30 23:39:35 +01:00
// Stop all the active bots so they can disconnect cleanly
2019-01-17 16:14:47 +01:00
if (Bot.Bots?.Count > 0) {
// Stop() function can block due to SK2 sockets, don't forget a maximum delay
await Task.WhenAny(Utilities.InParallel(Bot.Bots.Values.Select(bot => Task.Run(() => bot.Stop(true)))), Task.Delay(Bot.Bots.Count * WebBrowser.MaxTries * 1000)).ConfigureAwait(false);
2017-04-27 01:57:04 +02:00
2017-08-09 00:18:38 +02:00
// Extra second for Steam requests to go through
await Task.Delay(1000).ConfigureAwait(false);
2017-04-27 01:57:04 +02:00
}
// Flush all the pending writes to log files
2017-06-26 01:37:14 +02:00
LogManager.Flush();
2018-12-15 00:27:15 +01:00
// Unregister the process from single instancing
OS.UnregisterProcess();
return true;
}
private static async Task<int> Main(string[]? args) {
2017-08-04 18:27:30 +02:00
// Initialize
await Init(args).ConfigureAwait(false);
2017-06-26 03:36:51 +02:00
2017-08-04 18:27:30 +02:00
// Wait for shutdown event
2018-04-18 01:04:28 +02:00
return await ShutdownResetEvent.Task.ConfigureAwait(false);
}
private static async void OnProcessExit(object? sender, EventArgs e) => await Shutdown().ConfigureAwait(false);
2017-07-10 17:07:48 +02:00
private static async void OnUnhandledException(object? sender, UnhandledExceptionEventArgs e) {
2020-08-23 20:45:24 +02:00
if (e == null) {
throw new ArgumentNullException(nameof(e));
}
if (e.ExceptionObject == null) {
2020-11-14 22:37:00 +01:00
throw new ArgumentNullException(nameof(e));
2017-07-10 17:07:48 +02:00
}
2018-04-08 05:24:21 +02:00
await ASF.ArchiLogger.LogFatalException((Exception) e.ExceptionObject).ConfigureAwait(false);
2017-07-10 17:07:48 +02:00
await Exit(1).ConfigureAwait(false);
}
private static async void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e) {
2020-08-23 20:45:24 +02:00
if (e == null) {
throw new ArgumentNullException(nameof(e));
}
if (e.Exception == null) {
2020-11-14 22:37:00 +01:00
throw new ArgumentNullException(nameof(e));
2017-07-10 17:07:48 +02:00
}
2018-04-08 05:24:21 +02:00
await ASF.ArchiLogger.LogFatalException(e.Exception).ConfigureAwait(false);
// Normally we should abort the application here, but many tasks are in fact failing in SK2 code which we can't easily fix
// Thanks Valve.
e.SetObserved();
2017-07-10 17:07:48 +02:00
}
private static void ParseArgs(IReadOnlyCollection<string> args) {
2016-07-24 00:36:15 +02:00
if (args == null) {
throw new ArgumentNullException(nameof(args));
2016-07-24 00:36:15 +02:00
}
2019-04-24 10:49:37 +02:00
try {
string? envCryptKey = Environment.GetEnvironmentVariable(SharedInfo.EnvironmentVariableCryptKey);
2019-04-24 10:49:37 +02:00
if (!string.IsNullOrEmpty(envCryptKey)) {
2020-08-23 20:45:24 +02:00
HandleCryptKeyArgument(envCryptKey!);
2019-04-24 10:49:37 +02:00
}
2016-05-13 06:32:42 +02:00
string? envNetworkGroup = Environment.GetEnvironmentVariable(SharedInfo.EnvironmentVariableNetworkGroup);
2020-05-26 23:24:43 +02:00
if (!string.IsNullOrEmpty(envNetworkGroup)) {
2020-08-23 20:45:24 +02:00
HandleNetworkGroupArgument(envNetworkGroup!);
2020-05-26 23:24:43 +02:00
}
string? envPath = Environment.GetEnvironmentVariable(SharedInfo.EnvironmentVariablePath);
2019-07-07 11:39:44 +02:00
if (!string.IsNullOrEmpty(envPath)) {
2020-08-23 20:45:24 +02:00
HandlePathArgument(envPath!);
2019-07-07 11:39:44 +02:00
}
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
bool cryptKeyNext = false;
2020-05-26 23:24:43 +02:00
bool networkGroupNext = false;
bool pathNext = false;
2016-01-03 20:36:13 +01:00
foreach (string arg in args) {
switch (arg) {
case "--cryptkey" when !cryptKeyNext && !networkGroupNext && !pathNext:
cryptKeyNext = true;
break;
case "--ignore-unsupported-environment" when !cryptKeyNext && !networkGroupNext && !pathNext:
IgnoreUnsupportedEnvironment = true;
break;
case "--network-group" when !cryptKeyNext && !networkGroupNext && !pathNext:
2020-05-26 23:24:43 +02:00
networkGroupNext = true;
2021-05-20 22:42:30 +02:00
break;
case "--no-config-migrate" when !cryptKeyNext && !networkGroupNext && !pathNext:
ConfigMigrate = false;
break;
case "--no-config-watch" when !cryptKeyNext && !networkGroupNext && !pathNext:
ConfigWatch = false;
2020-05-26 23:24:43 +02:00
break;
case "--no-restart" when !cryptKeyNext && !networkGroupNext && !pathNext:
RestartAllowed = false;
break;
case "--process-required" when !cryptKeyNext && !networkGroupNext && !pathNext:
ProcessRequired = true;
break;
case "--path" when !cryptKeyNext && !networkGroupNext && !pathNext:
pathNext = true;
2018-12-15 00:27:15 +01:00
break;
case "--system-required" when !cryptKeyNext && !networkGroupNext && !pathNext:
SystemRequired = true;
2016-05-30 01:57:06 +02:00
break;
2016-01-03 20:36:13 +01:00
default:
if (cryptKeyNext) {
cryptKeyNext = false;
HandleCryptKeyArgument(arg);
} else if (networkGroupNext) {
2020-05-26 23:24:43 +02:00
networkGroupNext = false;
HandleNetworkGroupArgument(arg);
} else if (pathNext) {
pathNext = false;
HandlePathArgument(arg);
2020-11-14 22:37:00 +01:00
} else {
switch (arg.Length) {
case > 16 when arg.StartsWith("--network-group=", StringComparison.Ordinal):
HandleNetworkGroupArgument(arg[16..]);
2020-11-14 22:37:00 +01:00
break;
case > 11 when arg.StartsWith("--cryptkey=", StringComparison.Ordinal):
HandleCryptKeyArgument(arg[11..]);
2020-11-14 22:37:00 +01:00
break;
case > 7 when arg.StartsWith("--path=", StringComparison.Ordinal):
HandlePathArgument(arg[7..]);
2020-11-14 22:37:00 +01:00
break;
default:
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownCommandLineArgument, arg));
2020-11-14 22:37:00 +01:00
break;
}
2016-01-03 20:36:13 +01:00
}
break;
}
}
}
2018-04-18 01:04:28 +02:00
private static async Task Shutdown(byte exitCode = 0) {
if (!await InitShutdownSequence().ConfigureAwait(false)) {
2017-01-06 16:29:34 +01:00
return;
}
2018-04-18 01:04:28 +02:00
ShutdownResetEvent.TrySetResult(exitCode);
2017-01-06 16:29:34 +01:00
}
2016-06-28 06:58:59 +02:00
}
2018-07-31 16:17:24 +02:00
}