2016-08-02 06:13:58 +02:00
|
|
|
|
using System;
|
2017-03-11 17:05:39 +01:00
|
|
|
|
using System.Collections;
|
2017-02-01 00:44:52 +01:00
|
|
|
|
using System.Collections.Generic;
|
2017-02-05 14:32:38 +01:00
|
|
|
|
using System.Globalization;
|
2016-08-02 08:50:31 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
2017-02-05 14:32:38 +01:00
|
|
|
|
using System.Resources;
|
2016-08-02 08:50:31 +02:00
|
|
|
|
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;
|
2016-11-24 07:32:16 +01:00
|
|
|
|
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; }
|
|
|
|
|
|
|
2017-02-01 00:44:52 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-05 17:01:18 +02:00
|
|
|
|
internal static string GetUserInput(ASF.EUserInputType userInputType, string botName = SharedInfo.ASF, string extraInformation = null) => null; // TODO
|
2017-02-01 00:44:52 +01:00
|
|
|
|
|
2017-02-05 14:32:38 +01:00
|
|
|
|
internal static async Task InitASF() {
|
|
|
|
|
|
ASF.ArchiLogger.LogGenericInfo("ASF V" + SharedInfo.Version);
|
2017-02-01 00:44:52 +01:00
|
|
|
|
|
2017-02-05 14:32:38 +01:00
|
|
|
|
await InitGlobalConfigAndLanguage().ConfigureAwait(false);
|
2017-02-01 00:44:52 +01:00
|
|
|
|
|
2017-02-05 14:32:38 +01:00
|
|
|
|
if (!Runtime.IsRuntimeSupported) {
|
|
|
|
|
|
ASF.ArchiLogger.LogGenericError(Strings.WarningRuntimeUnsupported);
|
|
|
|
|
|
await Task.Delay(60 * 1000).ConfigureAwait(false);
|
2016-08-02 08:50:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-05 14:32:38 +01:00
|
|
|
|
await InitGlobalDatabaseAndServices().ConfigureAwait(false);
|
2016-08-02 08:50:31 +02:00
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-08-02 08:50:31 +02:00
|
|
|
|
|
2017-02-05 14:32:38 +01:00
|
|
|
|
Directory.CreateDirectory(SharedInfo.DebugDirectory);
|
2016-08-02 08:50:31 +02:00
|
|
|
|
|
2017-02-05 14:32:38 +01:00
|
|
|
|
DebugLog.AddListener(new Debugging.DebugListener());
|
|
|
|
|
|
DebugLog.Enabled = true;
|
2016-08-02 08:50:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-05 14:32:38 +01:00
|
|
|
|
await ASF.CheckForUpdate().ConfigureAwait(false);
|
|
|
|
|
|
await ASF.InitBots().ConfigureAwait(false);
|
2017-02-13 20:53:06 +01:00
|
|
|
|
ASF.InitEvents();
|
2017-02-05 14:32:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal static void InitCore() {
|
2016-08-02 08:50:31 +02:00
|
|
|
|
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;
|
2016-08-02 08:50:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
}
|
2016-08-02 08:50:31 +02:00
|
|
|
|
|
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;
|
2016-08-02 08:50:31 +02:00
|
|
|
|
|
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;
|
2016-08-02 08:50:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-05 14:32:38 +01:00
|
|
|
|
Application.Restart();
|
2016-08-02 08:50:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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() {
|
2016-11-24 07:32:16 +01:00
|
|
|
|
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);
|
2017-02-01 00:44:52 +01:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-11 17:05:39 +01:00
|
|
|
|
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);
|
2017-03-11 17:05:39 +01:00
|
|
|
|
if (defaultResourceSet == null) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(defaultResourceSet));
|
|
|
|
|
|
return;
|
2017-02-05 14:32:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-11 17:05:39 +01:00
|
|
|
|
HashSet<DictionaryEntry> defaultStringObjects = new HashSet<DictionaryEntry>(defaultResourceSet.Cast<DictionaryEntry>());
|
|
|
|
|
|
if (defaultStringObjects.Count == 0) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(defaultStringObjects));
|
2017-02-25 10:52:10 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-11 17:05:39 +01:00
|
|
|
|
ResourceSet currentResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
|
|
|
|
|
|
if (currentResourceSet == null) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(currentResourceSet));
|
|
|
|
|
|
return;
|
2016-11-24 07:32:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-11 17:05:39 +01:00
|
|
|
|
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);
|
2017-02-25 10:52:10 +01:00
|
|
|
|
|
2017-03-11 17:05:39 +01:00
|
|
|
|
// 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-25 10:52:10 +01:00
|
|
|
|
}
|
2017-02-05 14:32:38 +01:00
|
|
|
|
}
|
2017-03-11 17:05:39 +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() {
|
2016-11-24 07:32:16 +01:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-24 07:32:16 +01:00
|
|
|
|
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);
|
2017-02-01 00:44:52 +01:00
|
|
|
|
await Exit(1).ConfigureAwait(false);
|
2017-02-05 14:32:38 +01:00
|
|
|
|
return;
|
2016-11-24 07:32:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ArchiWebHandler.Init();
|
2017-03-07 03:47:56 +01:00
|
|
|
|
OS.Init(GlobalConfig.Headless);
|
2016-11-24 07:32:16 +01:00
|
|
|
|
WebBrowser.Init();
|
|
|
|
|
|
|
2017-02-05 14:32:38 +01:00
|
|
|
|
WebBrowser = new WebBrowser(ASF.ArchiLogger);
|
2016-11-24 07:32:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-02 06:13:58 +02:00
|
|
|
|
/// <summary>
|
2016-11-24 07:32:16 +01:00
|
|
|
|
/// 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);
|
2016-08-02 08:50:31 +02:00
|
|
|
|
Application.Run(new MainForm());
|
2016-08-02 06:13:58 +02:00
|
|
|
|
}
|
2016-11-24 07:32:16 +01: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) {
|
2016-11-24 07:32:16 +01:00
|
|
|
|
if (args?.ExceptionObject == null) {
|
2017-02-05 14:35:27 +01:00
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(args) + " || " + nameof(args.ExceptionObject));
|
2016-11-24 07:32:16 +01:00
|
|
|
|
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);
|
2016-11-24 07:32:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
2016-11-24 07:32:16 +01:00
|
|
|
|
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-11-24 07:32:16 +01:00
|
|
|
|
}
|
2016-08-02 06:13:58 +02:00
|
|
|
|
}
|
2016-11-24 07:32:16 +01:00
|
|
|
|
}
|