示例#1
0
        public static string Resolve(string rootKeyword, GrammarRequest request, string debugLabel = null, bool forceLog = false, string untranslatedRootKeyword = null)
        {
            if (LanguageDatabase.activeLanguage == LanguageDatabase.defaultLanguage)
            {
                return(GrammarResolver.ResolveUnsafe(rootKeyword, request, debugLabel, forceLog, false));
            }
            bool      flag;
            string    text;
            Exception ex;

            try
            {
                text = GrammarResolver.ResolveUnsafe(rootKeyword, request, out flag, debugLabel, forceLog, false);
                ex   = null;
            }
            catch (Exception ex2)
            {
                flag = false;
                text = string.Empty;
                ex   = ex2;
            }
            if (flag)
            {
                return(text);
            }
            string text2 = "Failed to resolve text. Trying again with English.";

            if (ex != null)
            {
                text2 = text2 + " Exception: " + ex;
            }
            Log.ErrorOnce(text2, text.GetHashCode(), false);
            string rootKeyword2 = untranslatedRootKeyword ?? rootKeyword;

            return(GrammarResolver.ResolveUnsafe(rootKeyword2, request, out flag, debugLabel, forceLog, true));
        }
示例#2
0
        private static bool TryResolveRecursive(GrammarResolver.RuleEntry entry, int depth, Dictionary <string, string> constants, out string output, bool log)
        {
            if (log)
            {
                GrammarResolver.logSb.AppendLine();
                GrammarResolver.logSb.Append(depth.ToStringCached() + " ");
                for (int i = 0; i < depth; i++)
                {
                    GrammarResolver.logSb.Append("   ");
                }
                GrammarResolver.logSb.Append(entry + " ");
            }
            GrammarResolver.loopCount++;
            if (GrammarResolver.loopCount > 1000)
            {
                Log.Error("Hit loops limit resolving grammar.", false);
                output = "HIT_LOOPS_LIMIT";
                if (log)
                {
                    GrammarResolver.logSb.Append("UNRESOLVABLE: Hit loops limit");
                }
                return(false);
            }
            if (depth > 50)
            {
                Log.Error("Grammar recurred too deep while resolving keyword (>" + 50 + " deep)", false);
                output = "DEPTH_LIMIT_REACHED";
                if (log)
                {
                    GrammarResolver.logSb.Append("UNRESOLVABLE: Depth limit reached");
                }
                return(false);
            }
            string text = entry.rule.Generate();
            bool   flag = false;
            int    num  = -1;

            for (int j = 0; j < text.Length; j++)
            {
                char c = text[j];
                if (c == '[')
                {
                    num = j;
                }
                if (c == ']')
                {
                    if (num == -1)
                    {
                        Log.Error("Could not resolve rule " + text + ": mismatched brackets.", false);
                        output = "MISMATCHED_BRACKETS";
                        if (log)
                        {
                            GrammarResolver.logSb.Append("UNRESOLVABLE: Mismatched brackets");
                        }
                        flag = true;
                    }
                    else
                    {
                        string text2 = text.Substring(num + 1, j - num - 1);
                        string str;
                        while (true)
                        {
                            GrammarResolver.RuleEntry ruleEntry = GrammarResolver.RandomPossiblyResolvableEntry(text2, constants);
                            if (ruleEntry == null)
                            {
                                break;
                            }
                            ruleEntry.uses++;
                            if (GrammarResolver.TryResolveRecursive(ruleEntry, depth + 1, constants, out str, log))
                            {
                                goto Block_13;
                            }
                            ruleEntry.MarkKnownUnresolvable();
                        }
                        entry.MarkKnownUnresolvable();
                        output = "CANNOT_RESOLVE_SUBSYMBOL:" + text2;
                        if (log)
                        {
                            GrammarResolver.logSb.Append("UNRESOLVABLE: Cannot resolve subsymbol '" + text2 + "'");
                        }
                        flag = true;
                        goto IL_219;
Block_13:
                        text = text.Substring(0, num) + str + text.Substring(j + 1);
                        j    = num;
                    }
                }
                IL_219 :;
            }
            output = text;
            return(!flag);
        }
示例#3
0
        public static string ResolveUnsafe(string rootKeyword, GrammarRequest request, string debugLabel = null, bool forceLog = false, bool useUntranslatedRules = false)
        {
            bool flag;

            return(GrammarResolver.ResolveUnsafe(rootKeyword, request, out flag, debugLabel, forceLog, useUntranslatedRules));
        }
示例#4
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;

            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 <RulePackDef>();
                    if (!hashSet.Contains(rulePackDef))
                    {
                        if (flag)
                        {
                            GrammarResolver.logSb.AppendLine(string.Format("  {0}", rulePackDef.defName));
                        }
                        hashSet.Add(rulePackDef);
                        List <Rule> list3 = (!useUntranslatedRules) ? rulePackDef.RulesImmediate : rulePackDef.UntranslatedRulesImmediate;
                        if (list3 != null)
                        {
                            foreach (Rule current in list3)
                            {
                                GrammarResolver.AddRule(current);
                            }
                        }
                        if (!rulePackDef.include.NullOrEmpty <RulePackDef>())
                        {
                            list2.AddRange(rulePackDef.include);
                        }
                    }
                }
                if (flag)
                {
                    GrammarResolver.logSb.AppendLine();
                }
            }
            List <RulePack> includesBare = request.GetIncludesBare();

            if (includesBare != null)
            {
                if (flag)
                {
                    GrammarResolver.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++)
                    {
                        GrammarResolver.AddRule(list4[k]);
                        if (flag)
                        {
                            GrammarResolver.logSb.AppendLine("  " + list4[k].ToString());
                        }
                    }
                }
                if (flag)
                {
                    GrammarResolver.logSb.AppendLine();
                }
            }
            List <Rule> list5 = (!useUntranslatedRules) ? RulePackDefOf.GlobalUtility.RulesPlusIncludes : RulePackDefOf.GlobalUtility.UntranslatedRulesPlusIncludes;

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

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

            if (!GrammarResolver.TryResolveRecursive(new GrammarResolver.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;
                }
                else if (!request.Includes.NullOrEmpty <RulePackDef>())
                {
                    text = text + " firstRulePack: " + request.Includes[0].defName;
                }
                if (flag)
                {
                    GrammarResolver.logSb.Insert(0, "GrammarResolver failed to resolve a text (rootKeyword: " + rootKeyword + ")\n");
                }
                else
                {
                    GrammarResolver.ResolveUnsafe(rootKeyword, request, debugLabel, true, false);
                }
            }
            text = GenText.CapitalizeSentences(Find.ActiveLanguageWorker.PostProcessed(text), true);
            text = GrammarResolver.Spaces.Replace(text, (Match match) => match.Groups[1].Value);
            text = text.Trim();
            if (flag && flag2)
            {
                if (DebugViewSettings.logGrammarResolution)
                {
                    Log.Error(GrammarResolver.logSb.ToString().Trim(), false);
                }
                else
                {
                    Log.ErrorOnce(GrammarResolver.logSb.ToString().Trim(), GrammarResolver.logSb.ToString().Trim().GetHashCode(), false);
                }
            }
            else if (flag)
            {
                Log.Message(GrammarResolver.logSb.ToString().Trim(), false);
            }
            success = !flag2;
            return(text);
        }
        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);
        }