private static void CleanupDefInjectionsForDefType(Type defType, string defInjectionsFolderPath, ModMetaData mod) { List <KeyValuePair <string, DefInjectionPackage.DefInjection> > list = (from x in LanguageDatabase.activeLanguage.defInjections.Where((DefInjectionPackage x) => x.defType == defType).SelectMany((DefInjectionPackage x) => x.injections) where !x.Value.isPlaceholder && x.Value.ModifiesDefFromModOrNullCore(mod, defType) select x).ToList(); Dictionary <string, DefInjectionPackage.DefInjection> dictionary = new Dictionary <string, DefInjectionPackage.DefInjection>(); foreach (KeyValuePair <string, DefInjectionPackage.DefInjection> item2 in list) { if (!dictionary.ContainsKey(item2.Value.normalizedPath)) { dictionary.Add(item2.Value.normalizedPath, item2.Value); } } List <PossibleDefInjection> possibleDefInjections = new List <PossibleDefInjection>(); DefInjectionUtility.ForEachPossibleDefInjection(defType, delegate(string suggestedPath, string normalizedPath, bool isCollection, string str, IEnumerable <string> collection, bool translationAllowed, bool fullListTranslationAllowed, FieldInfo fieldInfo, Def def) { if (translationAllowed) { PossibleDefInjection item = new PossibleDefInjection { suggestedPath = suggestedPath, normalizedPath = normalizedPath, isCollection = isCollection, fullListTranslationAllowed = fullListTranslationAllowed, curValue = str, curValueCollection = collection, fieldInfo = fieldInfo, def = def }; possibleDefInjections.Add(item); } }, mod); if (!possibleDefInjections.Any() && !list.Any()) { return; } List <KeyValuePair <string, DefInjectionPackage.DefInjection> > source = list.Where((KeyValuePair <string, DefInjectionPackage.DefInjection> x) => !x.Value.injected).ToList(); foreach (string fileName in possibleDefInjections.Select((PossibleDefInjection x) => GetSourceFile(x.def)).Concat(source.Select((KeyValuePair <string, DefInjectionPackage.DefInjection> x) => x.Value.fileSource)).Distinct()) { try { XDocument xDocument = new XDocument(); bool flag = false; try { XElement xElement = new XElement("LanguageData"); xDocument.Add(xElement); xElement.Add(new XComment("NEWLINE")); List <PossibleDefInjection> source2 = possibleDefInjections.Where((PossibleDefInjection x) => GetSourceFile(x.def) == fileName).ToList(); List <KeyValuePair <string, DefInjectionPackage.DefInjection> > source3 = source.Where((KeyValuePair <string, DefInjectionPackage.DefInjection> x) => x.Value.fileSource == fileName).ToList(); foreach (string defName in from x in source2.Select((PossibleDefInjection x) => x.def.defName).Concat(source3.Select((KeyValuePair <string, DefInjectionPackage.DefInjection> x) => x.Value.DefName)).Distinct() orderby x select x) { try { IEnumerable <PossibleDefInjection> enumerable = source2.Where((PossibleDefInjection x) => x.def.defName == defName); IEnumerable <KeyValuePair <string, DefInjectionPackage.DefInjection> > enumerable2 = source3.Where((KeyValuePair <string, DefInjectionPackage.DefInjection> x) => x.Value.DefName == defName); if (enumerable.Any()) { bool flag2 = false; foreach (PossibleDefInjection item3 in enumerable) { if (item3.isCollection) { IEnumerable <string> englishList = GetEnglishList(item3.normalizedPath, item3.curValueCollection, dictionary); bool flag3 = false; if (englishList != null) { int num = 0; foreach (string item4 in englishList) { _ = item4; if (dictionary.ContainsKey(item3.normalizedPath + "." + num)) { flag3 = true; break; } num++; } } if (flag3 || !item3.fullListTranslationAllowed) { if (englishList == null) { continue; } int num2 = -1; foreach (string item5 in englishList) { num2++; string text = item3.normalizedPath + "." + num2; string suggestedPath2 = item3.suggestedPath + "." + num2; if (TKeySystem.TrySuggestTKeyPath(text, out var tKeyPath)) { suggestedPath2 = tKeyPath; } if (!dictionary.TryGetValue(text, out var value)) { value = null; } if (value == null && !DefInjectionUtility.ShouldCheckMissingInjection(item5, item3.fieldInfo, item3.def)) { continue; } flag2 = true; flag = true; try { if (!item5.NullOrEmpty()) { xElement.Add(new XComment(SanitizeXComment(" EN: " + item5.Replace("\n", "\\n") + " "))); } } catch (Exception ex) { Log.Error("Could not add comment node in " + fileName + ": " + ex); } xElement.Add(GetDefInjectableFieldNode(suggestedPath2, value)); } continue; } bool flag4 = false; if (englishList != null) { foreach (string item6 in englishList) { if (DefInjectionUtility.ShouldCheckMissingInjection(item6, item3.fieldInfo, item3.def)) { flag4 = true; break; } } } if (!dictionary.TryGetValue(item3.normalizedPath, out var value2)) { value2 = null; } if (value2 == null && !flag4) { continue; } flag2 = true; flag = true; try { string text2 = ListToLiNodesString(englishList); if (!text2.NullOrEmpty()) { xElement.Add(new XComment(SanitizeXComment(" EN:\n" + text2.Indented() + "\n "))); } } catch (Exception ex2) { Log.Error("Could not add comment node in " + fileName + ": " + ex2); } xElement.Add(GetDefInjectableFieldNode(item3.suggestedPath, value2)); continue; } if (!dictionary.TryGetValue(item3.normalizedPath, out var value3)) { value3 = null; } string text3 = ((value3 != null && value3.injected) ? value3.replacedString : item3.curValue); if (value3 == null && !DefInjectionUtility.ShouldCheckMissingInjection(text3, item3.fieldInfo, item3.def)) { continue; } flag2 = true; flag = true; try { if (!text3.NullOrEmpty()) { xElement.Add(new XComment(SanitizeXComment(" EN: " + text3.Replace("\n", "\\n") + " "))); } } catch (Exception ex3) { Log.Error("Could not add comment node in " + fileName + ": " + ex3); } xElement.Add(GetDefInjectableFieldNode(item3.suggestedPath, value3)); } if (flag2) { xElement.Add(new XComment("NEWLINE")); } } if (!enumerable2.Any()) { continue; } flag = true; xElement.Add(new XComment(" UNUSED ")); foreach (KeyValuePair <string, DefInjectionPackage.DefInjection> item7 in enumerable2) { xElement.Add(GetDefInjectableFieldNode(item7.Value.path, item7.Value)); } xElement.Add(new XComment("NEWLINE")); } catch (Exception ex4) { Log.Error("Could not process def-injections for def " + defName + ": " + ex4); } } } finally { if (flag) { string text4 = Path.Combine(defInjectionsFolderPath, defType.Name); Directory.CreateDirectory(text4); SaveXMLDocumentWithProcessedNewlineTags(xDocument, Path.Combine(text4, fileName)); } } } catch (Exception ex5) { Log.Error("Could not process def-injections for file " + fileName + ": " + ex5); } } }
private static void CleanupDefInjectionsForDefType(Type defType, string defInjectionsFolderPath) { LoadedLanguage activeLanguage = LanguageDatabase.activeLanguage; List <KeyValuePair <string, DefInjectionPackage.DefInjection> > list = (from x in (from x in activeLanguage.defInjections where x.defType == defType select x).SelectMany((DefInjectionPackage x) => x.injections) where !x.Value.isPlaceholder select x).ToList(); Dictionary <string, DefInjectionPackage.DefInjection> dictionary = new Dictionary <string, DefInjectionPackage.DefInjection>(); foreach (KeyValuePair <string, DefInjectionPackage.DefInjection> item2 in list) { if (!dictionary.ContainsKey(item2.Value.normalizedPath)) { dictionary.Add(item2.Value.normalizedPath, item2.Value); } } List <PossibleDefInjection> possibleDefInjections = new List <PossibleDefInjection>(); DefInjectionUtility.ForEachPossibleDefInjection(defType, delegate(string suggestedPath, string normalizedPath, bool isCollection, string str, IEnumerable <string> collection, bool translationAllowed, bool fullListTranslationAllowed, FieldInfo fieldInfo, Def def) { if (translationAllowed) { PossibleDefInjection item = new PossibleDefInjection { suggestedPath = suggestedPath, normalizedPath = normalizedPath, isCollection = isCollection, fullListTranslationAllowed = fullListTranslationAllowed, curValue = str, curValueCollection = collection, fieldInfo = fieldInfo, def = def }; possibleDefInjections.Add(item); } }); if (possibleDefInjections.Any() || list.Any()) { List <KeyValuePair <string, DefInjectionPackage.DefInjection> > source = (from x in list where !x.Value.injected select x).ToList(); foreach (string item3 in (from x in possibleDefInjections select GetSourceFile(x.def)).Concat(from x in source select x.Value.fileSource).Distinct()) { try { XDocument xDocument = new XDocument(); bool flag = false; try { XElement xElement = new XElement("LanguageData"); xDocument.Add(xElement); xElement.Add(new XComment("NEWLINE")); List <PossibleDefInjection> source2 = (from x in possibleDefInjections where GetSourceFile(x.def) == item3 select x).ToList(); List <KeyValuePair <string, DefInjectionPackage.DefInjection> > source3 = (from x in source where x.Value.fileSource == item3 select x).ToList(); foreach (string item4 in from x in (from x in source2 select x.def.defName).Concat(from x in source3 select x.Value.DefName).Distinct() orderby x select x) { try { IEnumerable <PossibleDefInjection> enumerable = source2.Where((PossibleDefInjection x) => x.def.defName == item4); IEnumerable <KeyValuePair <string, DefInjectionPackage.DefInjection> > enumerable2 = source3.Where((KeyValuePair <string, DefInjectionPackage.DefInjection> x) => x.Value.DefName == item4); if (enumerable.Any()) { bool flag2 = false; foreach (PossibleDefInjection item5 in enumerable) { if (item5.isCollection) { IEnumerable <string> englishList = GetEnglishList(item5.normalizedPath, item5.curValueCollection, dictionary); bool flag3 = false; if (englishList != null) { int num = 0; foreach (string item6 in englishList) { if (dictionary.ContainsKey(item5.normalizedPath + "." + num)) { flag3 = true; break; } num++; } } if (flag3 || !item5.fullListTranslationAllowed) { if (englishList != null) { int num2 = -1; foreach (string item7 in englishList) { num2++; string key = item5.normalizedPath + "." + num2; string suggestedPath2 = item5.suggestedPath + "." + num2; if (!dictionary.TryGetValue(key, out DefInjectionPackage.DefInjection value)) { value = null; } if (value != null || DefInjectionUtility.ShouldCheckMissingInjection(item7, item5.fieldInfo, item5.def)) { flag2 = true; flag = true; try { if (!item7.NullOrEmpty()) { xElement.Add(new XComment(" EN: " + item7.Replace("\n", "\\n") + " ")); } } catch (Exception ex) { Log.Error("Could not add comment node in " + item3 + ": " + ex); } xElement.Add(GetDefInjectableFieldNode(suggestedPath2, value)); } } } } else { bool flag4 = false; if (englishList != null) { foreach (string item8 in englishList) { if (DefInjectionUtility.ShouldCheckMissingInjection(item8, item5.fieldInfo, item5.def)) { flag4 = true; break; } } } if (!dictionary.TryGetValue(item5.normalizedPath, out DefInjectionPackage.DefInjection value2)) { value2 = null; } if (value2 != null || flag4) { flag2 = true; flag = true; try { string text = ListToLiNodesString(englishList); if (!text.NullOrEmpty()) { xElement.Add(new XComment(" EN:\n" + text.Indented() + "\n ")); } } catch (Exception ex2) { Log.Error("Could not add comment node in " + item3 + ": " + ex2); } xElement.Add(GetDefInjectableFieldNode(item5.suggestedPath, value2)); } } } else { if (!dictionary.TryGetValue(item5.normalizedPath, out DefInjectionPackage.DefInjection value3)) { value3 = null; } string text2 = (value3 == null || !value3.injected) ? item5.curValue : value3.replacedString; if (value3 != null || DefInjectionUtility.ShouldCheckMissingInjection(text2, item5.fieldInfo, item5.def)) { flag2 = true; flag = true; try { if (!text2.NullOrEmpty()) { xElement.Add(new XComment(" EN: " + text2.Replace("\n", "\\n") + " ")); } } catch (Exception ex3) { Log.Error("Could not add comment node in " + item3 + ": " + ex3); } xElement.Add(GetDefInjectableFieldNode(item5.suggestedPath, value3)); } } } if (flag2) { xElement.Add(new XComment("NEWLINE")); } } if (enumerable2.Any()) { flag = true; xElement.Add(new XComment(" UNUSED ")); foreach (KeyValuePair <string, DefInjectionPackage.DefInjection> item9 in enumerable2) { xElement.Add(GetDefInjectableFieldNode(item9.Value.path, item9.Value)); } xElement.Add(new XComment("NEWLINE")); } } catch (Exception ex4) { Log.Error("Could not process def-injections for def " + item4 + ": " + ex4); } } } finally { if (flag) { string text3 = Path.Combine(defInjectionsFolderPath, defType.Name); Directory.CreateDirectory(text3); SaveXMLDocumentWithProcessedNewlineTags(xDocument, Path.Combine(text3, item3)); } } } catch (Exception ex5) { Log.Error("Could not process def-injections for file " + item3 + ": " + ex5); } } } }