PRoConEvents.MULTIbalancer.SetPluginVariable C# (CSharp) Method

SetPluginVariable() public method

public SetPluginVariable ( String strVariable, String strValue ) : void
strVariable String
strValue String
return void
        public void SetPluginVariable(String strVariable, String strValue)
        {
            bool isPresetVar = false;
            bool isReminderVar = false;

            if (fIsEnabled) DebugWrite(strVariable + " <- " + strValue, 6);

            try {
            if (strVariable.Contains("Show In Log") && String.IsNullOrEmpty(strValue)) {
            DebugWrite("^8Detected pre-v1 settings, upgrading ...", 3);
            UpgradePreV1Settings();
            strValue = INVALID_NAME_TAG_GUID; // mark as upgraded
            } else if (strVariable.Contains("Settings Version")) {
            DebugWrite("^1Settings Version = " + strValue, 3);
            }
            String tmp = strVariable;
            int pipeIndex = strVariable.IndexOf('|');
            if (pipeIndex >= 0) {
            pipeIndex++;
            tmp = strVariable.Substring(pipeIndex, strVariable.Length - pipeIndex);
            }
            if (tmp.Contains("(Low, Med, High population)")) {
            tmp = tmp.Replace("(Low, Med, High population)", String.Empty);
            }

            BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            String propertyName = Regex.Replace(tmp, @"[^a-zA-Z_0-9]", String.Empty);

            if (strVariable.Contains("preset")) propertyName = "Preset";

            FieldInfo field = this.GetType().GetField(propertyName, flags);

            Type fieldType = null;

            if (!strVariable.Contains("Settings for") && field != null) {
            fieldType = field.GetValue(this).GetType();
            if (strVariable.Contains("preset")) {
                fieldType = typeof(PresetItems);
                try {
                    Preset = (PresetItems)Enum.Parse(fieldType, strValue);
                    isPresetVar = true;
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (tmp.Contains("Spelling Of Speed Names Reminder")) {
                fieldType = typeof(Speed);
                try {
                    field.SetValue(this, (Speed)Enum.Parse(fieldType, strValue));
                    isReminderVar = true;
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (tmp.Contains("Balance Speed")) {
                fieldType = typeof(Speed[]);
                try {
                    // Parse the list into an array of enum vals
                    Speed[] items = MULTIbalancerUtils.ParseSpeedArray(this, strValue); // also validates
                    field.SetValue(this, items);
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (tmp.Contains("Ticket Percentage To Unstack")) {
                fieldType = typeof(double[]);
                try {
                    // Parse the list into an array of numbers
                    double[] nums = MULTIbalancerUtils.ParseNumArray(strValue); // also validates
                    field.SetValue(this, nums);
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (tmp.Contains("Scramble By")) {
                fieldType = typeof(DefineStrong);
                try {
                    field.SetValue(this, (DefineStrong)Enum.Parse(fieldType, strValue));
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (tmp == "Divide By") {
                fieldType = typeof(DivideByChoices);
                try {
                    field.SetValue(this, (DivideByChoices)Enum.Parse(fieldType, strValue));
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (tmp.Contains("Preferred Style Of Balancing")) {
                fieldType = typeof(PresetItems);
                try {
                    field.SetValue(this, (PresetItems)Enum.Parse(fieldType, strValue));
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (tmp.Contains("Forbid Switching")) {
                fieldType = typeof(UnswitchChoice);
                try {
                    field.SetValue(this, (UnswitchChoice)Enum.Parse(fieldType, strValue));
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (tmp.Contains("Which Battlelog Stats")) {
                fieldType = typeof(BattlelogStats);
                try {
                    field.SetValue(this, (BattlelogStats)Enum.Parse(fieldType, strValue));
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (tmp.Contains("Select Fast Balance By")) {
                fieldType = typeof(ForceMove);
                try {
                    field.SetValue(this, (ForceMove)Enum.Parse(fieldType, strValue));
                } catch (Exception e) {
                    ConsoleException(e);
                }
            } else if (fEasyTypeDict.ContainsValue(fieldType)) {
                field.SetValue(this, TypeDescriptor.GetConverter(fieldType).ConvertFromString(strValue));
            } else if (fListStrDict.ContainsValue(fieldType)) {
                if (DebugLevel >= 8) ConsoleDebug("String array " + propertyName + " <- " + strValue);
                field.SetValue(this, CPluginVariable.DecodeStringArray(strValue));
                if (propertyName == "Whitelist") {
                    UpdateWhitelistModel();
                    /*
                    MergeWithFile(Whitelist, fSettingWhitelist);
                    if (EnableWhitelistingOfReservedSlotsList) MergeWhitelistWithReservedSlots();
                    UpdateAllFromWhitelist();
                    if (DebugLevel >= 8) {
                        String l = "Whitelist: ";
                        l = l + String.Join(", ", fSettingWhitelist.ToArray());
                        ConsoleDebug(l);
                    }
                    */
                } else if (propertyName == "DisperseEvenlyList") {
                    MergeWithFile(DisperseEvenlyList, fSettingDisperseEvenlyList); // clears fSettingDispersEvenlyList
                    SetDispersalListGroups();
                    AssignGroups();
                } else if (propertyName == "FriendsList") {
                    MergeWithFile(FriendsList, fSettingFriendsList); // clears fSettingFriendsList
                    SetFriends();
                }
            } else if (fBoolDict.ContainsValue(fieldType)) {
                if (fIsEnabled) DebugWrite(propertyName + " strValue = " + strValue, 6);
                if (Regex.Match(strValue, "true", RegexOptions.IgnoreCase).Success) {
                    field.SetValue(this, true);
                } else {
                    field.SetValue(this, false);
                }
            } else {
                if (DebugLevel >= 8) ConsoleDebug("Unknown var " + propertyName + " with type " + fieldType);
            }
            } else {
            Match m = Regex.Match(tmp, @"([^:]+):\s([^:]+)$");

            if (m.Success) {
                String mode = m.Groups[1].Value;
                String fieldPart = m.Groups[2].Value.Replace(" ","");
                String perModeSetting = Regex.Replace(fieldPart, @"[^a-zA-Z_0-9]", String.Empty);

                perModeSetting = Regex.Replace(perModeSetting, @"(?:AsTickets|AsMinutes)", String.Empty);

                if (!fPerMode.ContainsKey(mode)) {
                    fPerMode[mode] = new PerModeSettings(mode, fGameVersion);
                }
                PerModeSettings pms = fPerMode[mode];

                field = pms.GetType().GetField(perModeSetting, flags);

                if (fIsEnabled) DebugWrite("Mode: " + mode + ", Field: " + perModeSetting + ", Value: " + strValue, 6);

                if (field != null) {
                    fieldType = field.GetValue(pms).GetType();
                    if (fEasyTypeDict.ContainsValue(fieldType)) {
                        field.SetValue(pms, TypeDescriptor.GetConverter(fieldType).ConvertFromString(strValue));
                    } else if (fListStrDict.ContainsValue(fieldType)) {
                        field.SetValue(pms, new List<string>(CPluginVariable.DecodeStringArray(strValue)));
                    } else if (fBoolDict.ContainsValue(fieldType)) {
                        if (Regex.Match(strValue, "true", RegexOptions.IgnoreCase).Success) {
                            field.SetValue(pms, true);
                        } else {
                            field.SetValue(pms, false);
                        }
                    } else if (strVariable.Contains("Determine Strong")) {
                        fieldType = typeof(DefineStrong);
                        try {
                            field.SetValue(pms, (DefineStrong)Enum.Parse(fieldType, strValue));
                        } catch (Exception e) {
                            ConsoleException(e);
                        }
                    }
                } else {
                    if (fIsEnabled) DebugWrite("field is null", 6);
                }
            }
            }
            } catch (System.Exception e) {
            ConsoleException(e);
            } finally {

            if (!isReminderVar) {
            // Reset to show hint
            SpellingOfSpeedNamesReminder = Speed.Click_Here_For_Speed_Names;
            }

            if (isPresetVar) {
            // Update other settings based on new preset value
            MULTIbalancerUtils.UpdateSettingsForPreset(this, Preset);
            } else {
            // Update Preset value based on current settings
            UpdatePresetValue();
            }

            if (strVariable.Contains("Apply Settings Changes") && ApplySettingsChanges) {
            ApplySettingsChanges = false;
            EnableSettingsWizard = false;
            ApplyWizardSettings();
            }

            // Validate all values and correct if needed
            ValidateSettings(strVariable,  strValue);

            // Handle show in log commands
            if (!String.IsNullOrEmpty(ShowCommandInLog)) {
            CommandToLog(ShowCommandInLog);
            ShowCommandInLog = String.Empty;
            }

            // Handle risky settings
            if (!EnableRiskyFeatures) {
            if (EnableTicketLossRateLogging) {
                ConsoleWarn("^8Setting ^bEnable Ticket Loss Rate Logging^n to False. This is an experimental setting and you have not enabled risky settings.");
                EnableTicketLossRateLogging = false;
            }
            }
            }
        }
MULTIbalancer