Example #1
0
        private static async Task InitGlobalDatabaseAndServices()
        {
            string globalDatabaseFile = ASF.GetFilePath(ASF.EFileType.Database);

            if (string.IsNullOrEmpty(globalDatabaseFile))
            {
                ASF.ArchiLogger.LogNullError(nameof(globalDatabaseFile));

                return;
            }

            if (!File.Exists(globalDatabaseFile))
            {
                ASF.ArchiLogger.LogGenericInfo(Strings.Welcome);
                await Task.Delay(10 * 1000).ConfigureAwait(false);

                ASF.ArchiLogger.LogGenericWarning(Strings.WarningPrivacyPolicy);
                await Task.Delay(5 * 1000).ConfigureAwait(false);
            }

            GlobalDatabase globalDatabase = await GlobalDatabase.CreateOrLoad(globalDatabaseFile).ConfigureAwait(false);

            if (globalDatabase == null)
            {
                ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorDatabaseInvalid, globalDatabaseFile));
                await Task.Delay(5 * 1000).ConfigureAwait(false);
                await Exit(1).ConfigureAwait(false);

                return;
            }

            ASF.InitGlobalDatabase(globalDatabase);

            // If debugging is on, we prepare debug directory prior to running
            if (Debugging.IsUserDebugging)
            {
                ASF.ArchiLogger.LogGenericDebug(globalDatabaseFile + ": " + JsonConvert.SerializeObject(ASF.GlobalDatabase, Formatting.Indented));
                Logging.EnableTraceLogging();

                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);
                    }
                }

                Directory.CreateDirectory(SharedInfo.DebugDirectory);

                DebugLog.AddListener(new Debugging.DebugListener());
                DebugLog.Enabled = true;
            }

            WebBrowser.Init();
        }
Example #2
0
        private static async Task InitGlobalConfigAndLanguage()
        {
            string globalConfigFile = ASF.GetFilePath(ASF.EFileType.Config);

            if (string.IsNullOrEmpty(globalConfigFile))
            {
                ASF.ArchiLogger.LogNullError(nameof(globalConfigFile));

                return;
            }

            GlobalConfig globalConfig;

            if (File.Exists(globalConfigFile))
            {
                globalConfig = await GlobalConfig.Load(globalConfigFile).ConfigureAwait(false);

                if (globalConfig == null)
                {
                    ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorGlobalConfigNotLoaded, globalConfigFile));
                    await Task.Delay(5 * 1000).ConfigureAwait(false);
                    await Exit(1).ConfigureAwait(false);

                    return;
                }
            }
            else
            {
                globalConfig = GlobalConfig.Create();
            }

            ASF.InitGlobalConfig(globalConfig);

            if (Debugging.IsUserDebugging)
            {
                ASF.ArchiLogger.LogGenericDebug(globalConfigFile + ": " + JsonConvert.SerializeObject(ASF.GlobalConfig, Formatting.Indented));
            }

            if (!string.IsNullOrEmpty(ASF.GlobalConfig.CurrentCulture))
            {
                try {
                    // GetCultureInfo() would be better but we can't use it for specifying neutral cultures such as "en"
                    CultureInfo culture = CultureInfo.CreateSpecificCulture(ASF.GlobalConfig.CurrentCulture);
                    CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = culture;
                } catch (Exception) {
                    ASF.ArchiLogger.LogGenericError(Strings.ErrorInvalidCurrentCulture);
                }
            }

            if (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Equals("en"))
            {
                return;
            }

            ResourceSet defaultResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.GetCultureInfo("en-US"), true, true);

            if (defaultResourceSet == null)
            {
                ASF.ArchiLogger.LogNullError(nameof(defaultResourceSet));

                return;
            }

            HashSet <DictionaryEntry> defaultStringObjects = defaultResourceSet.Cast <DictionaryEntry>().ToHashSet();

            if (defaultStringObjects.Count == 0)
            {
                ASF.ArchiLogger.LogNullError(nameof(defaultStringObjects));

                return;
            }

            ResourceSet currentResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);

            if (currentResourceSet == null)
            {
                ASF.ArchiLogger.LogNullError(nameof(currentResourceSet));

                return;
            }

            HashSet <DictionaryEntry> currentStringObjects = currentResourceSet.Cast <DictionaryEntry>().ToHashSet();

            if (currentStringObjects.Count >= defaultStringObjects.Count)
            {
                // Either we have 100% finished translation, or we're missing it entirely and using en-US
                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;
                }
            }

            if (currentStringObjects.Count < defaultStringObjects.Count)
            {
                float translationCompleteness = currentStringObjects.Count / (float)defaultStringObjects.Count;
                ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.TranslationIncomplete, CultureInfo.CurrentUICulture.Name, translationCompleteness.ToString("P1")));
            }
        }
Example #3
0
        private static async Task <bool> InitGlobalConfigAndLanguage()
        {
            string globalConfigFile = ASF.GetFilePath(ASF.EFileType.Config);

            if (string.IsNullOrEmpty(globalConfigFile))
            {
                throw new ArgumentNullException(nameof(globalConfigFile));
            }

            GlobalConfig?globalConfig;

            if (File.Exists(globalConfigFile))
            {
                globalConfig = await GlobalConfig.Load(globalConfigFile).ConfigureAwait(false);

                if (globalConfig == null)
                {
                    ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorGlobalConfigNotLoaded, globalConfigFile));
                    await Task.Delay(5 * 1000).ConfigureAwait(false);
                    await Exit(1).ConfigureAwait(false);

                    return(false);
                }
            }
            else
            {
                globalConfig = new GlobalConfig();
            }

            ASF.InitGlobalConfig(globalConfig);

            if (Debugging.IsDebugConfigured)
            {
                ASF.ArchiLogger.LogGenericDebug(globalConfigFile + ": " + JsonConvert.SerializeObject(ASF.GlobalConfig, Formatting.Indented));
            }

            if (!string.IsNullOrEmpty(ASF.GlobalConfig?.CurrentCulture))
            {
                try {
                    // GetCultureInfo() would be better but we can't use it for specifying neutral cultures such as "en"
                    CultureInfo culture = CultureInfo.CreateSpecificCulture(ASF.GlobalConfig !.CurrentCulture !);
                    CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = culture;
                } catch (Exception e) {
                    ASF.ArchiLogger.LogGenericWarningException(e);

                    ASF.ArchiLogger.LogGenericError(Strings.ErrorInvalidCurrentCulture);
                }
            }

            // Skip translation progress for English and invariant (such as "C") cultures
            switch (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
            {
            case "en":
            case "iv":
                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);

            if (defaultResourceSet == null)
            {
                ASF.ArchiLogger.LogNullError(nameof(defaultResourceSet));

                return(true);
            }

            HashSet <DictionaryEntry> defaultStringObjects = defaultResourceSet.Cast <DictionaryEntry>().ToHashSet();

            if (defaultStringObjects.Count == 0)
            {
                ASF.ArchiLogger.LogNullError(nameof(defaultStringObjects));

                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?currentResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);

            if (currentResourceSet == null)
            {
                ASF.ArchiLogger.LogNullError(nameof(currentResourceSet));

                return(true);
            }

            HashSet <DictionaryEntry> currentStringObjects = currentResourceSet.Cast <DictionaryEntry>().ToHashSet();

            if (currentStringObjects.Count >= defaultStringObjects.Count)
            {
                // Either we have 100% finished translation, or we're missing it entirely and using en-US
                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;
                }
            }

            if (currentStringObjects.Count < defaultStringObjects.Count)
            {
                float translationCompleteness = currentStringObjects.Count / (float)defaultStringObjects.Count;
                ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.TranslationIncomplete, CultureInfo.CurrentUICulture.Name, translationCompleteness.ToString("P1")));
            }

            return(true);
        }