Files
ArchiSteamFarm/GUI/Program.cs

251 lines
8.5 KiB
C#
Raw Normal View History

2016-08-02 06:13:58 +02:00
using System;
using System.Collections;
using System.Collections.Generic;
2017-02-05 14:32:38 +01:00
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
2017-02-05 14:32:38 +01:00
using System.Resources;
using System.Threading.Tasks;
2016-08-02 06:13:58 +02:00
using System.Windows.Forms;
2017-02-01 00:49:49 +01:00
using ArchiSteamFarm.Localization;
2017-02-05 14:32:38 +01:00
using NLog.Targets;
using SteamKit2;
2016-08-02 06:13:58 +02:00
namespace ArchiSteamFarm {
internal static class Program {
internal static GlobalConfig GlobalConfig { get; private set; }
internal static GlobalDatabase GlobalDatabase { get; private set; }
internal static WebBrowser WebBrowser { get; private set; }
private static bool ShutdownSequenceInitialized;
2017-02-01 00:49:49 +01:00
internal static async Task Exit(byte exitCode = 0) {
if (exitCode != 0) {
ASF.ArchiLogger.LogGenericError(Strings.ErrorExitingWithNonZeroErrorCode);
}
await Shutdown().ConfigureAwait(false);
2017-02-05 14:32:38 +01:00
Application.Exit();
2016-08-02 06:13:58 +02:00
}
internal static string GetUserInput(ASF.EUserInputType userInputType, string botName = SharedInfo.ASF, string extraInformation = null) => null; // TODO
2017-02-05 14:32:38 +01:00
internal static async Task InitASF() {
ASF.ArchiLogger.LogGenericInfo("ASF V" + SharedInfo.Version);
2017-02-05 14:32:38 +01:00
await InitGlobalConfigAndLanguage().ConfigureAwait(false);
2017-02-05 14:32:38 +01:00
if (!Runtime.IsRuntimeSupported) {
ASF.ArchiLogger.LogGenericError(Strings.WarningRuntimeUnsupported);
await Task.Delay(60 * 1000).ConfigureAwait(false);
}
2017-02-05 14:32:38 +01:00
await InitGlobalDatabaseAndServices().ConfigureAwait(false);
2017-02-05 14:32:38 +01:00
// If debugging is on, we prepare debug directory prior to running
if (GlobalConfig.Debug) {
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 (IOException e) {
ASF.ArchiLogger.LogGenericException(e);
}
}
2017-02-05 14:32:38 +01:00
Directory.CreateDirectory(SharedInfo.DebugDirectory);
2017-02-05 14:32:38 +01:00
DebugLog.AddListener(new Debugging.DebugListener());
DebugLog.Enabled = true;
}
2017-02-05 14:32:38 +01:00
await ASF.CheckForUpdate().ConfigureAwait(false);
await ASF.InitBots().ConfigureAwait(false);
ASF.InitEvents();
2017-02-05 14:32:38 +01:00
}
internal static void InitCore() {
string homeDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
if (!string.IsNullOrEmpty(homeDirectory)) {
Directory.SetCurrentDirectory(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("..");
2017-02-05 14:32:38 +01:00
if (Directory.Exists(SharedInfo.ConfigDirectory)) {
break;
}
}
// If config directory doesn't exist after our adjustment, abort all of that
if (!Directory.Exists(SharedInfo.ConfigDirectory)) {
Directory.SetCurrentDirectory(homeDirectory);
}
}
}
2017-02-05 14:32:38 +01:00
Logging.InitLoggers();
}
2017-02-05 14:32:38 +01:00
internal static async Task<bool> InitShutdownSequence() {
if (ShutdownSequenceInitialized) {
return false;
}
2016-08-31 13:56:09 +02:00
2017-02-05 14:32:38 +01:00
ShutdownSequenceInitialized = true;
2017-02-05 14:32:38 +01:00
IEnumerable<Task> tasks = Bot.Bots.Values.Select(bot => Task.Run(() => bot.Stop()));
await Task.WhenAny(Task.WhenAll(tasks), Task.Delay(10 * 1000));
return true;
}
internal static async Task Restart() {
if (!await InitShutdownSequence().ConfigureAwait(false)) {
return;
}
2017-02-05 14:32:38 +01:00
Application.Restart();
}
2017-02-05 14:32:38 +01:00
private static void Init() {
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
TaskScheduler.UnobservedTaskException += UnobservedTaskExceptionHandler;
// We must register our logging target as soon as possible
Target.Register<SteamTarget>("Steam");
// The rest of ASF is initialized from MainForm.cs
}
private static async Task InitGlobalConfigAndLanguage() {
string globalConfigFile = Path.Combine(SharedInfo.ConfigDirectory, SharedInfo.GlobalConfigFileName);
GlobalConfig = GlobalConfig.Load(globalConfigFile);
if (GlobalConfig == null) {
2017-02-05 14:32:38 +01:00
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorGlobalConfigNotLoaded, globalConfigFile));
await Task.Delay(5 * 1000).ConfigureAwait(false);
await Exit(1).ConfigureAwait(false);
2017-02-05 14:32:38 +01:00
return;
}
if (!string.IsNullOrEmpty(GlobalConfig.CurrentCulture)) {
try {
// GetCultureInfo() would be better but we can't use it for specifying neutral cultures such as "en"
CultureInfo culture = CultureInfo.CreateSpecificCulture(GlobalConfig.CurrentCulture);
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = culture;
} catch (CultureNotFoundException) {
ASF.ArchiLogger.LogGenericError(Strings.ErrorInvalidCurrentCulture);
}
}
if (CultureInfo.CurrentCulture.TwoLetterISOLanguageName.Equals("en")) {
return;
}
2017-02-05 14:32:38 +01:00
ResourceSet defaultResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.GetCultureInfo("en-US"), true, true);
if (defaultResourceSet == null) {
ASF.ArchiLogger.LogNullError(nameof(defaultResourceSet));
return;
2017-02-05 14:32:38 +01:00
}
HashSet<DictionaryEntry> defaultStringObjects = new HashSet<DictionaryEntry>(defaultResourceSet.Cast<DictionaryEntry>());
if (defaultStringObjects.Count == 0) {
ASF.ArchiLogger.LogNullError(nameof(defaultStringObjects));
return;
}
ResourceSet currentResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
if (currentResourceSet == null) {
ASF.ArchiLogger.LogNullError(nameof(currentResourceSet));
return;
}
HashSet<DictionaryEntry> currentStringObjects = new HashSet<DictionaryEntry>(currentResourceSet.Cast<DictionaryEntry>());
if (currentStringObjects.Count >= defaultStringObjects.Count) {
// Either we have 100% finished translation, or we're missing it entirely and using en-US
HashSet<DictionaryEntry> testStringObjects = new HashSet<DictionaryEntry>(currentStringObjects);
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-02-05 14:32:38 +01:00
}
if (currentStringObjects.Count < defaultStringObjects.Count) {
float translationCompleteness = currentStringObjects.Count / (float) defaultStringObjects.Count;
ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.TranslationIncomplete, CultureInfo.CurrentCulture.Name, translationCompleteness.ToString("P1")));
}
2017-02-05 14:32:38 +01:00
}
private static async Task InitGlobalDatabaseAndServices() {
string globalDatabaseFile = Path.Combine(SharedInfo.ConfigDirectory, SharedInfo.GlobalDatabaseFileName);
2017-02-05 14:32:38 +01:00
if (!File.Exists(globalDatabaseFile)) {
ASF.ArchiLogger.LogGenericInfo(Strings.Welcome);
ASF.ArchiLogger.LogGenericWarning(Strings.WarningPrivacyPolicy);
await Task.Delay(15 * 1000).ConfigureAwait(false);
}
GlobalDatabase = GlobalDatabase.Load(globalDatabaseFile);
if (GlobalDatabase == null) {
2017-02-05 14:32:38 +01:00
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorDatabaseInvalid, globalDatabaseFile));
await Task.Delay(5 * 1000).ConfigureAwait(false);
await Exit(1).ConfigureAwait(false);
2017-02-05 14:32:38 +01:00
return;
}
ArchiWebHandler.Init();
OS.Init(GlobalConfig.Headless);
WebBrowser.Init();
2017-02-05 14:32:38 +01:00
WebBrowser = new WebBrowser(ASF.ArchiLogger);
}
2016-08-02 06:13:58 +02:00
/// <summary>
/// The main entry point for the application.
2016-08-02 06:13:58 +02:00
/// </summary>
[STAThread]
private static void Main() {
2017-02-05 14:32:38 +01:00
Init();
2016-08-02 06:13:58 +02:00
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
2016-08-02 06:13:58 +02:00
}
2017-02-01 00:49:49 +01:00
private static async Task Shutdown() {
if (!await InitShutdownSequence().ConfigureAwait(false)) {
return;
}
Application.Exit();
}
2017-02-02 18:02:02 +01:00
private static async void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) {
if (args?.ExceptionObject == null) {
2017-02-05 14:35:27 +01:00
ASF.ArchiLogger.LogNullError(nameof(args) + " || " + nameof(args.ExceptionObject));
return;
}
2017-02-05 14:35:27 +01:00
ASF.ArchiLogger.LogFatalException((Exception) args.ExceptionObject);
2017-02-02 19:04:31 +01:00
await Task.Delay(5000).ConfigureAwait(false);
2017-02-02 18:02:02 +01:00
await Exit(1).ConfigureAwait(false);
}
private static void UnobservedTaskExceptionHandler(object sender, UnobservedTaskExceptionEventArgs args) {
if (args?.Exception == null) {
2017-02-05 14:35:27 +01:00
ASF.ArchiLogger.LogNullError(nameof(args) + " || " + nameof(args.Exception));
return;
}
2017-02-05 14:35:27 +01:00
ASF.ArchiLogger.LogFatalException(args.Exception);
2017-02-02 18:02:02 +01:00
// 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.
}
2016-08-02 06:13:58 +02:00
}
}