コード例 #1
0
ファイル: Program.cs プロジェクト: stonalampa/ArchiSteamFarm
        private static async Task InitGlobalConfigAndLanguage()
        {
            string globalConfigFile = Path.Combine(SharedInfo.ConfigDirectory, SharedInfo.GlobalConfigFileName);

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

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

            if (GlobalConfig.BackgroundGCPeriod > 0)
            {
                Hacks.EnableBackgroundGC(GlobalConfig.BackgroundGCPeriod);
            }

            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 (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 = new HashSet <DictionaryEntry>(defaultResourceSet.Cast <DictionaryEntry>());

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

            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")));
            }
        }
コード例 #2
0
        internal static GlobalConfig Load(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                ASF.ArchiLogger.LogNullError(nameof(filePath));
                return(null);
            }

            if (!File.Exists(filePath))
            {
                return(null);
            }

            GlobalConfig globalConfig;

            try {
                globalConfig = JsonConvert.DeserializeObject <GlobalConfig>(File.ReadAllText(filePath));
            } catch (Exception e) {
                ASF.ArchiLogger.LogGenericException(e);
                return(null);
            }

            if (globalConfig == null)
            {
                ASF.ArchiLogger.LogNullError(nameof(globalConfig));
                return(null);
            }

            // SK2 supports only TCP and UDP steam protocols
            // Ensure that user can't screw this up
            switch (globalConfig.SteamProtocol)
            {
            case ProtocolType.Tcp:
            case ProtocolType.Udp:
                break;

            default:
                ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.SteamProtocol), globalConfig.SteamProtocol));
                return(null);
            }

            // User might not know what he's doing
            // Ensure that he can't screw core ASF variables
            if (globalConfig.MaxFarmingTime == 0)
            {
                ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.MaxFarmingTime), globalConfig.MaxFarmingTime));
                return(null);
            }

            if (globalConfig.FarmingDelay == 0)
            {
                ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.FarmingDelay), globalConfig.FarmingDelay));
                return(null);
            }

            if (globalConfig.ConnectionTimeout == 0)
            {
                ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.ConnectionTimeout), globalConfig.ConnectionTimeout));
                return(null);
            }

            if (globalConfig.IPCPort == 0)
            {
                ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.IPCPort), globalConfig.IPCPort));
                return(null);
            }

            GlobalConfig result = globalConfig;

            return(result);
        }