public PasswordRuleSet GeneratePasswordRules(int seed)
        {
            var language      = VanillaRuleModifier._modSettings?.Settings.Language;
            var ruleset       = (PasswordRuleSet)GenerateRuleSet(seed);
            var alphabetField = ReflectionHelper.GetField <List <char> >(ruleset, "alphabet");

            if (alphabetField == null)
            {
                CommonReflectedTypeInfo.DebugLog("Failed to Generate passwords for current seed/language settings. Falling back to Seed 1 rules");
                return(ruleset);
            }

            // ReSharper disable once StringLiteralTypo
            alphabetField.Set(GetLocalizedAlphabet(language).ToCharArray().ToList());
            var possibilities = GetLocalizedWordSet(language);

            if (CommonReflectedTypeInfo.IsModdedSeed)
            {
                // Choose 17 base words
                const int numBaseWords = 17;
                const int maxWords     = 35;
                var       words        = possibilities.OrderBy(x => rand.Next()).Take(numBaseWords).ToList();

                // Find other words that are similar to these
                for (int i = 0; i < numBaseWords && words.Count < maxWords; i++)
                {
                    var addedWords = possibilities
                                     .Where(w => !words.Contains(w))
                                     .Select(w => new { word = w, pref = rand.NextDouble(), dist = Similarity(w, words[i]) })
                                     .ToList();
                    addedWords.Sort((a, b) =>
                    {
                        var r = a.dist - b.dist;
                        return(r == 0 ? (a.pref <b.pref ? -1 : a.pref> b.pref ? 1 : 0) : r);
                    });
                    words.AddRange(addedWords.Take(Math.Min(rand.Next(1, 5), maxWords - words.Count)).Select(w => w.word));
                }

                words.Sort();
                ruleset.possibleWords = words;
            }
            return(ruleset);
        }
Exemplo n.º 2
0
 public static void DebugLog(string message, params object[] args)
 {
     CommonReflectedTypeInfo.DebugLog(message, args);
 }