private static void AddSpecificCustomCharacterSetting(string strSettingName)
        {
            if (_intDicLoadedCharacterSettingsLoadedStatus <= 1)
            {
                return;
            }

            CharacterSettings objNewCharacterSettings = new CharacterSettings();

            if (!objNewCharacterSettings.Load(strSettingName, false) ||
                (objNewCharacterSettings.BuildMethodIsLifeModule &&
                 !GlobalSettings.LifeModuleEnabled))
            {
                objNewCharacterSettings.Dispose();
                return;
            }

            while (true)
            {
                if (s_DicLoadedCharacterSettings.TryAdd(objNewCharacterSettings.DictionaryKey,
                                                        objNewCharacterSettings))
                {
                    return;
                }
                // We somehow already have a setting loaded with this name, so just copy over the values and dispose of the new setting instead
                if (s_DicLoadedCharacterSettings.TryGetValue(objNewCharacterSettings.DictionaryKey,
                                                             out CharacterSettings objOldCharacterSettings))
                {
                    objOldCharacterSettings.CopyValues(objNewCharacterSettings);
                    objNewCharacterSettings.Dispose();
                    return;
                }
            }
        }
        private static void ReloadSpecificCustomCharacterSetting(string strSettingName)
        {
            if (_intDicLoadedCharacterSettingsLoadedStatus <= 1)
            {
                return;
            }

            CharacterSettings objNewCharacterSettings = new CharacterSettings();

            if (!objNewCharacterSettings.Load(strSettingName, false) ||
                (objNewCharacterSettings.BuildMethodIsLifeModule &&
                 !GlobalSettings.LifeModuleEnabled))
            {
                objNewCharacterSettings.Dispose();
                return;
            }

            while (true)
            {
                if (s_DicLoadedCharacterSettings.TryGetValue(objNewCharacterSettings.DictionaryKey,
                                                             out CharacterSettings objOldCharacterSettings))
                {
                    objOldCharacterSettings.CopyValues(objNewCharacterSettings);
                    objNewCharacterSettings.Dispose();
                    return;
                }

                // We ended up changing our dictionary key, so find the first custom setting without a corresponding file and delete it
                // (we assume that it's the one that got renamed)
                if (s_DicLoadedCharacterSettings.TryAdd(objNewCharacterSettings.DictionaryKey,
                                                        objNewCharacterSettings))
                {
                    foreach (CharacterSettings objExistingSettings in s_DicLoadedCharacterSettings.Values.ToList())
                    {
                        if (objExistingSettings.BuiltInOption)
                        {
                            continue;
                        }
                        if (!File.Exists(Path.Combine(Utils.GetStartupPath, "settings", objExistingSettings.FileName)))
                        {
                            foreach (Character objCharacter in Program.OpenCharacters)
                            {
                                if (objCharacter.SettingsKey == objExistingSettings.DictionaryKey)
                                {
                                    objCharacter.SettingsKey = objNewCharacterSettings.DictionaryKey;
                                }
                            }
                            s_DicLoadedCharacterSettings.Remove(objExistingSettings.DictionaryKey);
                            objExistingSettings.Dispose();
                            return;
                        }
                    }
                    break;
                }
            }
        }
Beispiel #3
0
        private static void LoadCharacterSettings()
        {
            _intDicLoadedCharacterSettingsLoadedStatus = 0;
            try
            {
                s_DicLoadedCharacterSettings.Clear();
                if (Utils.IsDesignerMode || Utils.IsRunningInVisualStudio)
                {
                    CharacterSettings objNewCharacterSettings = new CharacterSettings();
                    if (!s_DicLoadedCharacterSettings.TryAdd(GlobalSettings.DefaultCharacterSetting, objNewCharacterSettings))
                    {
                        objNewCharacterSettings.Dispose();
                    }
                    return;
                }

                IEnumerable <XPathNavigator> xmlSettingsIterator = XmlManager.LoadXPath("settings.xml")
                                                                   .SelectAndCacheExpression("/chummer/settings/setting").Cast <XPathNavigator>();
                Parallel.ForEach(xmlSettingsIterator, xmlBuiltInSetting =>
                {
                    CharacterSettings objNewCharacterSettings = new CharacterSettings();
                    if (!objNewCharacterSettings.Load(xmlBuiltInSetting) ||
                        (objNewCharacterSettings.BuildMethodIsLifeModule && !GlobalSettings.LifeModuleEnabled) ||
                        !s_DicLoadedCharacterSettings.TryAdd(objNewCharacterSettings.DictionaryKey, objNewCharacterSettings))
                    {
                        objNewCharacterSettings.Dispose();
                    }
                });
                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) ||
                            !s_DicLoadedCharacterSettings.TryAdd(objNewCharacterSettings.DictionaryKey, objNewCharacterSettings))
                        {
                            objNewCharacterSettings.Dispose();
                        }
                    });
                }
            }
            finally
            {
                _intDicLoadedCharacterSettingsLoadedStatus = 1;
            }
        }
        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);
            }
        }