Ejemplo n.º 1
0
        public static string Resolve(string rootKeyword, GrammarRequest request, string debugLabel = null, bool forceLog = false, string untranslatedRootKeyword = null, List <string> extraTags = null, List <string> outTags = null, bool capitalizeFirstSentence = true)
        {
            if (LanguageDatabase.activeLanguage == LanguageDatabase.defaultLanguage)
            {
                return(ResolveUnsafe(rootKeyword, request, debugLabel, forceLog, useUntranslatedRules: false, extraTags, outTags, capitalizeFirstSentence));
            }
            string    text;
            bool      success;
            Exception ex;

            try
            {
                text = ResolveUnsafe(rootKeyword, request, out success, debugLabel, forceLog, useUntranslatedRules: false, extraTags, outTags, capitalizeFirstSentence);
                ex   = null;
            }
            catch (Exception ex2)
            {
                success = false;
                text    = "";
                ex      = ex2;
            }
            if (success)
            {
                return(text);
            }
            string text2 = "Failed to resolve text. Trying again with English.";

            if (ex != null)
            {
                text2 = text2 + " Exception: " + ex;
            }
            Log.ErrorOnce(text2, text.GetHashCode());
            outTags?.Clear();
            return(ResolveUnsafe(untranslatedRootKeyword ?? rootKeyword, request, out success, debugLabel, forceLog, useUntranslatedRules: true, extraTags, outTags, capitalizeFirstSentence));
        }
Ejemplo n.º 2
0
        public static string Resolve(string rootKeyword, GrammarRequest request, string debugLabel = null, bool forceLog = false, string untranslatedRootKeyword = null)
        {
            if (LanguageDatabase.activeLanguage == LanguageDatabase.defaultLanguage)
            {
                return(ResolveUnsafe(rootKeyword, request, debugLabel, forceLog));
            }
            string    text;
            bool      success;
            Exception ex;

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

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

            return(ResolveUnsafe(rootKeyword2, request, out success, debugLabel, forceLog, useUntranslatedRules: true));
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public static string ResolveUnsafe(string rootKeyword, GrammarRequest request, string debugLabel = null, bool forceLog = false, bool useUntranslatedRules = false)
        {
            bool success;

            return(ResolveUnsafe(rootKeyword, request, out success, debugLabel, forceLog, useUntranslatedRules));
        }
Ejemplo n.º 5
0
        public static string ResolveUnsafe(string rootKeyword, GrammarRequest request, out bool success, string debugLabel = null, bool forceLog = false, bool useUntranslatedRules = false, List <string> extraTags = null, List <string> outTags = null, bool capitalizeFirstSentence = true)
        {
            bool flag = forceLog || DebugViewSettings.logGrammarResolution;

            rules.Clear();
            rulePool.Clear();
            if (flag)
            {
                logSbTrace = new StringBuilder();
                logSbMid   = new StringBuilder();
                logSbRules = new StringBuilder();
            }
            List <Rule> rulesAllowNull = request.RulesAllowNull;

            if (rulesAllowNull != null)
            {
                if (flag)
                {
                    logSbRules.AppendLine("CUSTOM RULES");
                }
                for (int i = 0; i < rulesAllowNull.Count; i++)
                {
                    AddRule(rulesAllowNull[i]);
                    if (flag)
                    {
                        logSbRules.AppendLine("■" + rulesAllowNull[i].ToString());
                    }
                }
                if (flag)
                {
                    logSbRules.AppendLine();
                }
            }
            List <RulePackDef> includesAllowNull = request.IncludesAllowNull;

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

            if (includesBareAllowNull != null)
            {
                if (flag)
                {
                    logSbMid.AppendLine();
                    logSbMid.AppendLine("BARE INCLUDES");
                }
                for (int j = 0; j < includesBareAllowNull.Count; j++)
                {
                    List <Rule> list3 = useUntranslatedRules ? includesBareAllowNull[j].UntranslatedRules : includesBareAllowNull[j].Rules;
                    for (int k = 0; k < list3.Count; k++)
                    {
                        AddRule(list3[k]);
                        if (flag)
                        {
                            logSbMid.AppendLine("  " + list3[k].ToString());
                        }
                    }
                }
            }
            if (flag && !extraTags.NullOrEmpty())
            {
                logSbMid.AppendLine();
                logSbMid.AppendLine("EXTRA TAGS");
                for (int l = 0; l < extraTags.Count; l++)
                {
                    logSbMid.AppendLine("  " + extraTags[l]);
                }
            }
            List <Rule> list4 = useUntranslatedRules ? RulePackDefOf.GlobalUtility.UntranslatedRulesPlusIncludes : RulePackDefOf.GlobalUtility.RulesPlusIncludes;

            for (int m = 0; m < list4.Count; m++)
            {
                AddRule(list4[m]);
            }
            loopCount = 0;
            Dictionary <string, string> constantsAllowNull = request.ConstantsAllowNull;

            if (flag && constantsAllowNull != null)
            {
                logSbMid.AppendLine("CONSTANTS");
                foreach (KeyValuePair <string, string> item2 in constantsAllowNull)
                {
                    logSbMid.AppendLine(item2.Key.PadRight(38) + " " + item2.Value);
                }
            }
            if (flag)
            {
                logSbTrace.Append("GRAMMAR RESOLUTION TRACE");
            }
            string        output = "err";
            bool          flag2  = false;
            List <string> list5  = new List <string>();

            if (TryResolveRecursive(new RuleEntry(new Rule_String("", "[" + rootKeyword + "]")), 0, constantsAllowNull, out output, flag, extraTags, list5))
            {
                if (outTags != null)
                {
                    outTags.Clear();
                    outTags.AddRange(list5);
                }
            }
            else
            {
                flag2  = true;
                output = ((!request.Rules.NullOrEmpty()) ? ("ERR: " + request.Rules[0].Generate()) : "ERR");
                if (flag)
                {
                    logSbTrace.Insert(0, "Grammar unresolvable. Root '" + rootKeyword + "'\n\n");
                }
                else
                {
                    ResolveUnsafe(rootKeyword, request, debugLabel, forceLog: true, useUntranslatedRules, extraTags);
                }
            }
            output = GenText.CapitalizeSentences(Find.ActiveLanguageWorker.PostProcessed(output), capitalizeFirstSentence);
            output = Spaces.Replace(output, (Match match) => match.Groups[1].Value);
            output = output.Trim();
            if (flag)
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append(logSbTrace.ToString().TrimEndNewlines());
                stringBuilder.AppendLine();
                stringBuilder.AppendLine();
                stringBuilder.Append(logSbMid.ToString().TrimEndNewlines());
                stringBuilder.AppendLine();
                stringBuilder.AppendLine();
                stringBuilder.Append(logSbRules.ToString().TrimEndNewlines());
                if (flag2)
                {
                    if (DebugViewSettings.logGrammarResolution)
                    {
                        Log.Error(stringBuilder.ToString().Trim() + "\n");
                    }
                    else
                    {
                        Log.ErrorOnce(stringBuilder.ToString().Trim() + "\n", stringBuilder.ToString().Trim().GetHashCode());
                    }
                }
                else
                {
                    Log.Message(stringBuilder.ToString().Trim() + "\n");
                }
                logSbTrace = null;
                logSbMid   = null;
                logSbRules = null;
            }
            success = !flag2;
            return(output);
        }
Ejemplo n.º 6
0
        public static string ResolveUnsafe(string rootKeyword, GrammarRequest request, string debugLabel = null, bool forceLog = false, bool useUntranslatedRules = false, List <string> extraTags = null, List <string> outTags = null, bool capitalizeFirstSentence = true)
        {
            bool success;

            return(ResolveUnsafe(rootKeyword, request, out success, debugLabel, forceLog, useUntranslatedRules, extraTags, outTags, capitalizeFirstSentence));
        }
        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);
        }