Beispiel #1
0
        public static string ResolveUnsafe(string rootKeyword, GrammarRequest request, out bool success, string debugLabel = null, bool forceLog = false, bool useUntranslatedRules = false)
        {
            bool flag = forceLog || DebugViewSettings.logGrammarResolution;

            rules.Clear();
            rulePool.Clear();
            if (flag)
            {
                logSb = new StringBuilder();
            }
            List <Rule> list = request.GetRules();

            if (list != null)
            {
                if (flag)
                {
                    logSb.AppendLine("Custom rules:");
                }
                for (int i = 0; i < list.Count; i++)
                {
                    AddRule(list[i]);
                    if (flag)
                    {
                        logSb.AppendLine("  " + list[i].ToString());
                    }
                }
                if (flag)
                {
                    logSb.AppendLine();
                }
            }
            List <RulePackDef> includes = request.GetIncludes();

            if (includes != null)
            {
                HashSet <RulePackDef> hashSet = new HashSet <RulePackDef>();
                List <RulePackDef>    list2   = new List <RulePackDef>(includes);
                if (flag)
                {
                    logSb.AppendLine("Includes:");
                }
                while (list2.Count > 0)
                {
                    RulePackDef rulePackDef = list2[list2.Count - 1];
                    list2.RemoveLast();
                    if (!hashSet.Contains(rulePackDef))
                    {
                        if (flag)
                        {
                            logSb.AppendLine($"  {rulePackDef.defName}");
                        }
                        hashSet.Add(rulePackDef);
                        List <Rule> list3 = (!useUntranslatedRules) ? rulePackDef.RulesImmediate : rulePackDef.UntranslatedRulesImmediate;
                        if (list3 != null)
                        {
                            foreach (Rule item in list3)
                            {
                                AddRule(item);
                            }
                        }
                        if (!rulePackDef.include.NullOrEmpty())
                        {
                            list2.AddRange(rulePackDef.include);
                        }
                    }
                }
                if (flag)
                {
                    logSb.AppendLine();
                }
            }
            List <RulePack> includesBare = request.GetIncludesBare();

            if (includesBare != null)
            {
                if (flag)
                {
                    logSb.AppendLine("Bare includes:");
                }
                for (int j = 0; j < includesBare.Count; j++)
                {
                    List <Rule> list4 = (!useUntranslatedRules) ? includesBare[j].Rules : includesBare[j].UntranslatedRules;
                    for (int k = 0; k < list4.Count; k++)
                    {
                        AddRule(list4[k]);
                        if (flag)
                        {
                            logSb.AppendLine("  " + list4[k].ToString());
                        }
                    }
                }
                if (flag)
                {
                    logSb.AppendLine();
                }
            }
            List <Rule> list5 = (!useUntranslatedRules) ? RulePackDefOf.GlobalUtility.RulesPlusIncludes : RulePackDefOf.GlobalUtility.UntranslatedRulesPlusIncludes;

            for (int l = 0; l < list5.Count; l++)
            {
                AddRule(list5[l]);
            }
            loopCount = 0;
            Dictionary <string, string> constants = request.GetConstants();

            if (flag && constants != null)
            {
                logSb.AppendLine("Constants:");
                foreach (KeyValuePair <string, string> item2 in constants)
                {
                    logSb.AppendLine($"  {item2.Key}: {item2.Value}");
                }
            }
            string output = "err";
            bool   flag2  = false;

            if (!TryResolveRecursive(new RuleEntry(new Rule_String(string.Empty, "[" + rootKeyword + "]")), 0, constants, out output, flag))
            {
                flag2  = true;
                output = "Could not resolve any root: " + rootKeyword;
                if (!debugLabel.NullOrEmpty())
                {
                    output = output + " debugLabel: " + debugLabel;
                }
                else if (!request.Includes.NullOrEmpty())
                {
                    output = output + " firstRulePack: " + request.Includes[0].defName;
                }
                if (flag)
                {
                    logSb.Insert(0, "GrammarResolver failed to resolve a text (rootKeyword: " + rootKeyword + ")\n");
                }
                else
                {
                    ResolveUnsafe(rootKeyword, request, debugLabel, forceLog: true);
                }
            }
            output = GenText.CapitalizeSentences(Find.ActiveLanguageWorker.PostProcessed(output));
            output = Spaces.Replace(output, (Match match) => match.Groups[1].Value);
            output = output.Trim();
            if (flag && flag2)
            {
                if (DebugViewSettings.logGrammarResolution)
                {
                    Log.Error(logSb.ToString().Trim());
                }
                else
                {
                    Log.ErrorOnce(logSb.ToString().Trim(), logSb.ToString().Trim().GetHashCode());
                }
            }
            else if (flag)
            {
                Log.Message(logSb.ToString().Trim());
            }
            success = !flag2;
            return(output);
        }
        public static string Resolve(string rootKeyword, GrammarRequest request, string debugLabel = null, bool forceLog = false)
        {
            bool flag = forceLog || DebugViewSettings.logGrammarResolution;

            GrammarResolver.rules.Clear();
            GrammarResolver.rulePool.Clear();
            if (flag)
            {
                GrammarResolver.logSb = new StringBuilder();
            }
            List <Rule> list = request.GetRules();

            if (list != null)
            {
                if (flag)
                {
                    GrammarResolver.logSb.AppendLine("Custom rules:");
                }
                for (int i = 0; i < list.Count; i++)
                {
                    GrammarResolver.AddRule(list[i]);
                    if (flag)
                    {
                        GrammarResolver.logSb.AppendLine("  " + list[i].ToString());
                    }
                }
                if (flag)
                {
                    GrammarResolver.logSb.AppendLine();
                }
            }
            List <RulePackDef> includes = request.GetIncludes();

            if (includes != null)
            {
                HashSet <RulePackDef> hashSet = new HashSet <RulePackDef>();
                List <RulePackDef>    list2   = new List <RulePackDef>(includes);
                if (flag)
                {
                    GrammarResolver.logSb.AppendLine("Includes:");
                }
                while (list2.Count > 0)
                {
                    RulePackDef rulePackDef = list2[list2.Count - 1];
                    list2.RemoveLast();
                    if (!hashSet.Contains(rulePackDef))
                    {
                        if (flag)
                        {
                            GrammarResolver.logSb.AppendLine(string.Format("  {0}", rulePackDef.defName));
                        }
                        hashSet.Add(rulePackDef);
                        List <Rule> rulesImmediate = rulePackDef.RulesImmediate;
                        if (rulesImmediate != null)
                        {
                            foreach (Rule item in rulePackDef.RulesImmediate)
                            {
                                GrammarResolver.AddRule(item);
                            }
                        }
                        if (!rulePackDef.include.NullOrEmpty())
                        {
                            list2.AddRange(rulePackDef.include);
                        }
                    }
                }
                if (flag)
                {
                    GrammarResolver.logSb.AppendLine();
                }
            }
            for (int j = 0; j < RulePackDefOf.GlobalUtility.RulesPlusIncludes.Count; j++)
            {
                GrammarResolver.AddRule(RulePackDefOf.GlobalUtility.RulesPlusIncludes[j]);
            }
            GrammarResolver.loopCount = 0;
            Dictionary <string, string> constants = request.GetConstants();

            if (flag && constants != null)
            {
                GrammarResolver.logSb.AppendLine("Constants:");
                foreach (KeyValuePair <string, string> item2 in constants)
                {
                    GrammarResolver.logSb.AppendLine(string.Format("  {0}: {1}", item2.Key, item2.Value));
                }
            }
            string text  = "err";
            bool   flag2 = false;

            if (!GrammarResolver.TryResolveRecursive(new RuleEntry(new Rule_String(string.Empty, "[" + rootKeyword + "]")), 0, constants, out text, flag))
            {
                flag2 = true;
                text  = "Could not resolve any root: " + rootKeyword;
                if (!debugLabel.NullOrEmpty())
                {
                    text = text + " debugLabel: " + debugLabel;
                }
                if (flag)
                {
                    GrammarResolver.logSb.Insert(0, "FAILED TO RESOLVE\n");
                }
                else
                {
                    GrammarResolver.Resolve(rootKeyword, request, debugLabel, true);
                }
            }
            text = GenText.CapitalizeSentences(Find.ActiveLanguageWorker.PostProcessed(text));
            text = GrammarResolver.Spaces.Replace(text, (Match match) => match.Groups[1].Value);
            if (flag && flag2)
            {
                if (DebugViewSettings.logGrammarResolution)
                {
                    Log.Error(GrammarResolver.logSb.ToString().Trim());
                }
                else
                {
                    Log.ErrorOnce(GrammarResolver.logSb.ToString().Trim(), GrammarResolver.logSb.ToString().GetHashCode());
                }
            }
            else if (flag)
            {
                Log.Message(GrammarResolver.logSb.ToString().Trim());
            }
            return(text);
        }