PRoConEvents.MULTIbalancer.SetDispersalListGroups C# (CSharp) Method

SetDispersalListGroups() private method

private SetDispersalListGroups ( ) : void
return void
        private void SetDispersalListGroups()
        {
            /*
            This function scans the Disperse Evenly List for lines that specify
            a group. A group starts with a single digit, 1 thru 4, followed by
            whitespace, followed by a whitespace separated list of name/tag/guid items
            that should be assigned to the specified group list.
            */
            if (fSettingDisperseEvenlyList.Count == 0) return;
            if (fSettingDisperseEvenlyList.Count == 1 && fSettingDisperseEvenlyList[0] == DEFAULT_LIST_ITEM) return;
            foreach (List<String> gl in fDispersalGroups) {
            if (gl == null) continue;
            gl.Clear();
            }
            List<String> copy = new List<String>(fSettingDisperseEvenlyList);
            foreach (String line in copy) {
            try {
            if (String.IsNullOrEmpty(line)) continue;
            String[] tokens = Regex.Split(line, @"\s+");
            if (tokens != null && tokens.Length == 1 && !String.IsNullOrEmpty(tokens[0])) {
                // Not a group, so retain
                continue;
            }
            // Otherwise, check for a group specifier
            bool first = true;
            bool remove = false;
            List<String> group = null;
            bool bad = false;
            int groupId = 0;
            // Scan one line
            foreach (String token in tokens) {
                if (String.IsNullOrEmpty(token)) continue;
                // First token might be group specifier, if so move remaining tokens to group list
                if (first) {
                    if (Regex.Match(token, @"^[1234]").Success) {
                        // It's a group
                        if (Int32.TryParse(token, out groupId)) {
                            if (groupId >= 1 && groupId <= 4) {
                                group = fDispersalGroups[groupId];
                                remove = true;
                            } else bad = true;
                        }
                    }
                    first = false;
                    if (group != null) continue; // skip group id
                }
                if (group == null) {
                    break; // not a group specification, get out of this token parsing loop
                } else if (group.Contains(token)) {
                    ConsoleWarn("In Disperse Evenly List in Group " + groupId + ", ^b" + token + "^n is duplicated, please remove all duplicates");
                } else {
                    // Add the rest of the tokens to the group
                    group.Add(token);
                }
            }
            if (bad) {
                // Warn, leave line in original as is
                ConsoleWarn("In Disperse Evenly List, unrecognized grouping, possible typo? " + line);
            } else if (remove) {
                // Remove lines that define groups from the normal list
                fSettingDisperseEvenlyList.Remove(line);
            }
            } catch (Exception e) {
            ConsoleWarn("In Disperse Evenly List, skipping bad line: " + line);
            ConsoleWarn(e.Message);
            }
            }
            // Check for uniqueness
            List<String> uniq = new List<String>();
            for (int i = 1; i <= 4; ++i) {
            copy = new List<String>(fDispersalGroups[i]);
            foreach (String s in copy) {
            if (uniq.Contains(s)) {
                ConsoleWarn("In Disperse Evenly List in Group " + i + ", ^b" + s + "^n is duplicated, please remove all duplicates");
                fDispersalGroups[i].Remove(s);
            } else {
                uniq.Add(s);
            }
            }
            }
            copy = new List<String>(fSettingDisperseEvenlyList);
            foreach (String s in copy) {
            if (uniq.Contains(s)) {
            ConsoleWarn("In Disperse Evenly List, ^b" + s + "^n is duplicated, please remove all duplicates");
            fSettingDisperseEvenlyList.Remove(s);
            } else {
            uniq.Add(s);
            }
            }
            // debugging
            if (DebugLevel >= 6) {
            String g1 = "Group 1: ";
            String g2 = "Group 2: ";
            String g3 = "Group 3: ";
            String g4 = "Group 4: ";
            if (fDispersalGroups[1].Count > 0) {
            g1 = g1 + String.Join(", ", fDispersalGroups[1].ToArray());
            ConsoleDebug("SetDispersalListGroups " + g1);
            }
            if (fDispersalGroups[2].Count > 0) {
            g2 = g2 + String.Join(", ", fDispersalGroups[2].ToArray());
            ConsoleDebug("SetDispersalListGroups " + g2);
            }
            if (fDispersalGroups[3].Count > 0) {
            g3 = g3 + String.Join(", ", fDispersalGroups[3].ToArray());
            ConsoleDebug("SetDispersalListGroups " + g3);
            }
            if (fDispersalGroups[4].Count > 0) {
            g4 = g4 + String.Join(", ", fDispersalGroups[4].ToArray());
            ConsoleDebug("SetDispersalListGroups " + g4);
            }
            ConsoleDebug("SetDispersalListGroups remaining list: " + String.Join(", ", fSettingDisperseEvenlyList.ToArray()));
            }
        }
MULTIbalancer