public static bool ResolveUnsafe(ref string __result, 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) { string output; lock (ResolveLock) { bool flag = forceLog || DebugViewSettings.logGrammarResolution; rules.Clear(); //rulePool.Clear(); if (flag) { GrammarResolver.logSbTrace = new StringBuilder(); GrammarResolver.logSbMid = new StringBuilder(); GrammarResolver.logSbRules = new StringBuilder(); } List <Rule> rulesAllowNull = request.RulesAllowNull; if (rulesAllowNull != null) { if (flag) { GrammarResolver.logSbRules.AppendLine("CUSTOM RULES"); } for (int i = 0; i < rulesAllowNull.Count; i++) { AddRule(rulesAllowNull[i]); if (flag) { GrammarResolver.logSbRules.AppendLine("■" + rulesAllowNull[i].ToString()); } } if (flag) { GrammarResolver.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) { GrammarResolver.logSbMid.AppendLine("INCLUDES"); } while (list.Count > 0) { RulePackDef rulePackDef = list[list.Count - 1]; list.RemoveLast(); if (!hashSet.Contains(rulePackDef)) { if (flag) { GrammarResolver.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) { GrammarResolver.logSbMid.AppendLine(); GrammarResolver.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) { GrammarResolver.logSbMid.AppendLine(" " + list3[k].ToString()); } } } } if (flag && !extraTags.NullOrEmpty()) { GrammarResolver.logSbMid.AppendLine(); GrammarResolver.logSbMid.AppendLine("EXTRA TAGS"); for (int l = 0; l < extraTags.Count; l++) { GrammarResolver.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]); } GrammarResolver.loopCount = 0; Dictionary <string, string> constantsAllowNull = request.ConstantsAllowNull; if (flag && constantsAllowNull != null) { GrammarResolver.logSbMid.AppendLine("CONSTANTS"); foreach (KeyValuePair <string, string> item2 in constantsAllowNull) { GrammarResolver.logSbMid.AppendLine(item2.Key.PadRight(38) + " " + item2.Value); } } if (flag) { GrammarResolver.logSbTrace.Append("GRAMMAR RESOLUTION TRACE"); } 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) { GrammarResolver.logSbTrace.Insert(0, "Grammar unresolvable. Root '" + rootKeyword + "'\n\n"); } else { GrammarResolver.ResolveUnsafe(rootKeyword, request, debugLabel, forceLog: true, useUntranslatedRules, extraTags); } } output = GenText.CapitalizeSentences(Find.ActiveLanguageWorker.PostProcessed(output), capitalizeFirstSentence); output = GrammarResolver.Spaces.Replace(output, (Match match) => match.Groups[1].Value); output = output.Trim(); if (flag) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(GrammarResolver.logSbTrace.ToString().TrimEndNewlines()); stringBuilder.AppendLine(); stringBuilder.AppendLine(); stringBuilder.Append(GrammarResolver.logSbMid.ToString().TrimEndNewlines()); stringBuilder.AppendLine(); stringBuilder.AppendLine(); stringBuilder.Append(GrammarResolver.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"); } GrammarResolver.logSbTrace = null; GrammarResolver.logSbMid = null; GrammarResolver.logSbRules = null; } success = !flag2; } __result = output; return(false); }
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); }