Пример #1
0
        private static void LoadCustomCharacterSettings()
        {
            // Don't attempt to load custom character settings if we're still loading all settings
            if (Interlocked.CompareExchange(ref _intDicLoadedCharacterSettingsLoadedStatus, 1, 2) <= 1)
            {
                return;
            }
            try
            {
                using (LockingDictionary <string, CharacterSettings> dicNewLoadedCharacterSettings = new LockingDictionary <string, CharacterSettings>())
                {
                    string strSettingsPath = Path.Combine(Utils.GetStartupPath, "settings");
                    if (Directory.Exists(strSettingsPath))
                    {
                        Parallel.ForEach(Directory.EnumerateFiles(strSettingsPath, "*.xml"), strSettingsFilePath =>
                        {
                            string strSettingName = Path.GetFileName(strSettingsFilePath);
                            CharacterSettings objNewCharacterSettings = new CharacterSettings();
                            if (!objNewCharacterSettings.Load(strSettingName, false) ||
                                (objNewCharacterSettings.BuildMethodIsLifeModule &&
                                 !GlobalSettings.LifeModuleEnabled)
                                // ReSharper disable once AccessToDisposedClosure
                                || !dicNewLoadedCharacterSettings.TryAdd(objNewCharacterSettings.DictionaryKey,
                                                                         objNewCharacterSettings))
                            {
                                objNewCharacterSettings.Dispose();
                            }
                        });
                    }

                    using (new FetchSafelyFromPool <HashSet <string> >(Utils.StringHashSetPool, out HashSet <string> setRemovedSettingsKeys))
                    {
                        foreach (CharacterSettings objExistingSettings in s_DicLoadedCharacterSettings.Values.ToList())
                        {
                            if (objExistingSettings.BuiltInOption)
                            {
                                continue;
                            }
                            if (!dicNewLoadedCharacterSettings.TryRemove(objExistingSettings.DictionaryKey,
                                                                         out CharacterSettings objNewSettings))
                            {
                                setRemovedSettingsKeys.Add(objExistingSettings.DictionaryKey);
                            }
                            else
                            {
                                objExistingSettings.CopyValues(objNewSettings);
                                objNewSettings.Dispose();
                            }
                        }

                        foreach (CharacterSettings objNewSettings in dicNewLoadedCharacterSettings.Values)
                        {
                            if (!s_DicLoadedCharacterSettings.TryAdd(objNewSettings.DictionaryKey, objNewSettings))
                            {
                                objNewSettings.Dispose();
                            }
                        }

                        foreach (string strSettingToRemove in setRemovedSettingsKeys)
                        {
                            CharacterSettings objSettingsToDelete = s_DicLoadedCharacterSettings[strSettingToRemove];
                            try
                            {
                                Lazy <string> strBestMatchNewSettingsKey = new Lazy <string>(() =>
                                {
                                    int intBestScore = int.MinValue;
                                    string strReturn = string.Empty;
                                    foreach (CharacterSettings objExistingSettings in s_DicLoadedCharacterSettings.Values)
                                    {
                                        if (setRemovedSettingsKeys.Contains(objExistingSettings.DictionaryKey))
                                        {
                                            continue;
                                        }
                                        // ReSharper disable once AccessToDisposedClosure
                                        int intLoopScore = CalculateCharacterSettingsMatchScore(objSettingsToDelete, objExistingSettings);
                                        if (intLoopScore > intBestScore)
                                        {
                                            intBestScore = intLoopScore;
                                            strReturn    = objExistingSettings.DictionaryKey;
                                        }
                                    }
                                    return(strReturn);
                                });
                                foreach (Character objCharacter in Program.OpenCharacters)
                                {
                                    if (objCharacter.SettingsKey == objSettingsToDelete.DictionaryKey)
                                    {
                                        objCharacter.SettingsKey = strBestMatchNewSettingsKey.Value;
                                    }
                                }
                            }
                            finally
                            {
                                s_DicLoadedCharacterSettings.Remove(objSettingsToDelete.DictionaryKey);
                                objSettingsToDelete.Dispose();
                            }
                        }
                    }
                }
            }
            finally
            {
                Interlocked.Increment(ref _intDicLoadedCharacterSettingsLoadedStatus);
            }
        }