Пример #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
        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);
        }
Пример #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));
        }