Ejemplo n.º 1
0
        public static void BuildMappings()
        {
            Dictionary <string, string> tmpTranslationKeyToTKey = new Dictionary <string, string>();

            foreach (TKeyRef key in keys)
            {
                string normalizedTranslationKey = GetNormalizedTranslationKey(key);
                if (tKeyToNormalizedTranslationKey.TryGetValue(key.tKeyPath, out string value))
                {
                    loadErrors.Add("Duplicate TKey: " + key.tKeyPath + " -> NEW=" + normalizedTranslationKey + " | OLD" + value + " - Ignoring old");
                }
                else
                {
                    tKeyToNormalizedTranslationKey.Add(key.tKeyPath, normalizedTranslationKey);
                    tmpTranslationKeyToTKey.Add(normalizedTranslationKey, key.tKeyPath);
                }
            }
            foreach (string item in keys.Select((TKeyRef k) => k.defTypeName).Distinct())
            {
                DefInjectionUtility.ForEachPossibleDefInjection(GenTypes.GetTypeInAnyAssembly(item), delegate(string suggestedPath, string normalizedPath, bool isCollection, string currentValue, IEnumerable <string> currentValueCollection, bool translationAllowed, bool fullListTranslationAllowed, FieldInfo fieldInfo, Def def)
                {
                    if (translationAllowed && !TryGetNormalizedPath(suggestedPath, out string _) && TrySuggestTKeyPath(normalizedPath, out string tKeyPath, tmpTranslationKeyToTKey))
                    {
                        tmpTranslationKeyToTKey.Add(suggestedPath, tKeyPath);
                    }
                });
            }
            foreach (KeyValuePair <string, string> item2 in tmpTranslationKeyToTKey)
            {
                translationKeyToTKey.Add(item2.Key, item2.Value);
            }
        }
Ejemplo n.º 2
0
        public static void ForEachPossibleDefInjection(Type defType, DefInjectionUtility.PossibleDefInjectionTraverser action)
        {
            IEnumerable <Def> allDefsInDatabaseForDef = GenDefDatabase.GetAllDefsInDatabaseForDef(defType);

            foreach (Def def in allDefsInDatabaseForDef)
            {
                DefInjectionUtility.ForEachPossibleDefInjectionInDef(def, action);
            }
        }
Ejemplo n.º 3
0
        public List <string> MissingInjections(List <string> outUnnecessaryDefInjections)
        {
            List <string> missingInjections = new List <string>();
            Dictionary <string, DefInjectionPackage.DefInjection> injectionsByNormalizedPath = new Dictionary <string, DefInjectionPackage.DefInjection>();

            foreach (KeyValuePair <string, DefInjectionPackage.DefInjection> current in this.injections)
            {
                if (!injectionsByNormalizedPath.ContainsKey(current.Value.normalizedPath))
                {
                    injectionsByNormalizedPath.Add(current.Value.normalizedPath, current.Value);
                }
            }
            DefInjectionUtility.ForEachPossibleDefInjection(this.defType, delegate(string suggestedPath, string normalizedPath, bool isCollection, string str, IEnumerable <string> collection, bool translationAllowed, bool fullListTranslationAllowed, FieldInfo fieldInfo, Def def)
            {
                DefInjectionPackage.DefInjection defInjection2;
                if (!isCollection)
                {
                    bool flag   = false;
                    string text = null;
                    DefInjectionPackage.DefInjection defInjection;
                    if (injectionsByNormalizedPath.TryGetValue(normalizedPath, out defInjection) && !defInjection.IsFullListInjection)
                    {
                        if (!translationAllowed)
                        {
                            outUnnecessaryDefInjections.Add(defInjection.path + " '" + defInjection.injection.Replace("\n", "\\n") + "'");
                        }
                        else if (defInjection.isPlaceholder)
                        {
                            flag = true;
                            text = defInjection.fileSource;
                        }
                    }
                    else
                    {
                        flag = true;
                    }
                    if (flag && translationAllowed && DefInjectionUtility.ShouldCheckMissingInjection(str, fieldInfo, def))
                    {
                        missingInjections.Add(string.Concat(new string[]
                        {
                            suggestedPath,
                            " '",
                            str.Replace("\n", "\\n"),
                            "'",
                            (!text.NullOrEmpty()) ? (" (placeholder exists in " + text + ")") : string.Empty
                        }));
                    }
                }
                else if (injectionsByNormalizedPath.TryGetValue(normalizedPath, out defInjection2) && defInjection2.IsFullListInjection)
                {
                    if (!translationAllowed || !fullListTranslationAllowed)
                    {
                        outUnnecessaryDefInjections.Add(defInjection2.path + " '" + defInjection2.fullListInjection.ToStringSafeEnumerable().Replace("\n", "\\n") + "'");
                    }
                    else if (defInjection2.isPlaceholder && translationAllowed && !def.generated)
                    {
                        missingInjections.Add(suggestedPath + ((!defInjection2.fileSource.NullOrEmpty()) ? (" (placeholder exists in " + defInjection2.fileSource + ")") : string.Empty));
                    }
                }
                else
                {
                    int num = 0;
                    foreach (string current2 in collection)
                    {
                        string key   = normalizedPath + "." + num;
                        string text2 = suggestedPath + "." + num;
                        bool flag2   = false;
                        string text3 = null;
                        DefInjectionPackage.DefInjection defInjection3;
                        if (injectionsByNormalizedPath.TryGetValue(key, out defInjection3) && !defInjection3.IsFullListInjection)
                        {
                            if (!translationAllowed)
                            {
                                outUnnecessaryDefInjections.Add(defInjection3.path + " '" + defInjection3.injection.Replace("\n", "\\n") + "'");
                            }
                            else if (defInjection3.isPlaceholder)
                            {
                                flag2 = true;
                                text3 = defInjection3.fileSource;
                            }
                        }
                        else
                        {
                            flag2 = true;
                        }
                        if (flag2 && translationAllowed && DefInjectionUtility.ShouldCheckMissingInjection(current2, fieldInfo, def))
                        {
                            DefInjectionPackage.DefInjection defInjection4;
                            if (text3.NullOrEmpty() && injectionsByNormalizedPath.TryGetValue(normalizedPath, out defInjection4) && defInjection4.isPlaceholder)
                            {
                                text3 = defInjection4.fileSource;
                            }
                            missingInjections.Add(string.Concat(new string[]
                            {
                                text2,
                                " '",
                                current2.Replace("\n", "\\n"),
                                "'",
                                (!fullListTranslationAllowed) ? string.Empty : " (hint: this list allows full-list translation by using <li> nodes)",
                                (!text3.NullOrEmpty()) ? (" (placeholder exists in " + text3 + ")") : string.Empty
                            }));
                        }
                        num++;
                    }
                }
            });
            return(missingInjections);
        }
        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 <KeyValuePair <string, DefInjectionPackage.DefInjection> >();
            Dictionary <string, DefInjectionPackage.DefInjection> dictionary = new Dictionary <string, DefInjectionPackage.DefInjection>();

            foreach (KeyValuePair <string, DefInjectionPackage.DefInjection> current in list)
            {
                if (!dictionary.ContainsKey(current.Value.normalizedPath))
                {
                    dictionary.Add(current.Value.normalizedPath, current.Value);
                }
            }
            List <TranslationFilesCleaner.PossibleDefInjection> possibleDefInjections = new List <TranslationFilesCleaner.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)
                {
                    TranslationFilesCleaner.PossibleDefInjection possibleDefInjection = new TranslationFilesCleaner.PossibleDefInjection();
                    possibleDefInjection.suggestedPath              = suggestedPath;
                    possibleDefInjection.normalizedPath             = normalizedPath;
                    possibleDefInjection.isCollection               = isCollection;
                    possibleDefInjection.fullListTranslationAllowed = fullListTranslationAllowed;
                    possibleDefInjection.curValue           = str;
                    possibleDefInjection.curValueCollection = collection;
                    possibleDefInjection.fieldInfo          = fieldInfo;
                    possibleDefInjection.def = def;
                    possibleDefInjections.Add(possibleDefInjection);
                }
            });
            if (!possibleDefInjections.Any <TranslationFilesCleaner.PossibleDefInjection>() && !list.Any <KeyValuePair <string, DefInjectionPackage.DefInjection> >())
            {
                return;
            }
            List <KeyValuePair <string, DefInjectionPackage.DefInjection> > source = (from x in list
                                                                                      where !x.Value.injected
                                                                                      select x).ToList <KeyValuePair <string, DefInjectionPackage.DefInjection> >();

            foreach (string fileName in (from x in possibleDefInjections
                                         select TranslationFilesCleaner.GetSourceFile(x.def)).Concat(from x in source
                                                                                                     select x.Value.fileSource).Distinct <string>())
            {
                try
                {
                    XDocument xDocument = new XDocument();
                    bool      flag      = false;
                    try
                    {
                        XElement xElement = new XElement("LanguageData");
                        xDocument.Add(xElement);
                        xElement.Add(new XComment("NEWLINE"));
                        List <TranslationFilesCleaner.PossibleDefInjection> source2 = (from x in possibleDefInjections
                                                                                       where TranslationFilesCleaner.GetSourceFile(x.def) == fileName
                                                                                       select x).ToList <TranslationFilesCleaner.PossibleDefInjection>();
                        List <KeyValuePair <string, DefInjectionPackage.DefInjection> > source3 = (from x in source
                                                                                                   where x.Value.fileSource == fileName
                                                                                                   select x).ToList <KeyValuePair <string, DefInjectionPackage.DefInjection> >();
                        foreach (string defName in from x in (from x in source2
                                                              select x.def.defName).Concat(from x in source3
                                                                                           select x.Value.DefName).Distinct <string>()
                                 orderby x
                                 select x)
                        {
                            try
                            {
                                IEnumerable <TranslationFilesCleaner.PossibleDefInjection>             enumerable  = source2.Where((TranslationFilesCleaner.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 <TranslationFilesCleaner.PossibleDefInjection>())
                                {
                                    bool flag2 = false;
                                    foreach (TranslationFilesCleaner.PossibleDefInjection current2 in enumerable)
                                    {
                                        if (current2.isCollection)
                                        {
                                            IEnumerable <string> englishList = TranslationFilesCleaner.GetEnglishList(current2.normalizedPath, current2.curValueCollection, dictionary);
                                            bool flag3 = false;
                                            if (englishList != null)
                                            {
                                                int num = 0;
                                                foreach (string current3 in englishList)
                                                {
                                                    if (dictionary.ContainsKey(current2.normalizedPath + "." + num))
                                                    {
                                                        flag3 = true;
                                                        break;
                                                    }
                                                    num++;
                                                }
                                            }
                                            if (flag3 || !current2.fullListTranslationAllowed)
                                            {
                                                if (englishList != null)
                                                {
                                                    int num2 = -1;
                                                    foreach (string current4 in englishList)
                                                    {
                                                        num2++;
                                                        string key            = current2.normalizedPath + "." + num2;
                                                        string suggestedPath2 = current2.suggestedPath + "." + num2;
                                                        DefInjectionPackage.DefInjection defInjection;
                                                        if (!dictionary.TryGetValue(key, out defInjection))
                                                        {
                                                            defInjection = null;
                                                        }
                                                        if (defInjection != null || DefInjectionUtility.ShouldCheckMissingInjection(current4, current2.fieldInfo, current2.def))
                                                        {
                                                            flag2 = true;
                                                            flag  = true;
                                                            try
                                                            {
                                                                if (!current4.NullOrEmpty())
                                                                {
                                                                    xElement.Add(new XComment(" EN: " + current4.Replace("\n", "\\n") + " "));
                                                                }
                                                            }
                                                            catch (Exception ex)
                                                            {
                                                                Log.Error(string.Concat(new object[]
                                                                {
                                                                    "Could not add comment node in ",
                                                                    fileName,
                                                                    ": ",
                                                                    ex
                                                                }), false);
                                                            }
                                                            xElement.Add(TranslationFilesCleaner.GetDefInjectableFieldNode(suggestedPath2, defInjection));
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                bool flag4 = false;
                                                if (englishList != null)
                                                {
                                                    foreach (string current5 in englishList)
                                                    {
                                                        if (DefInjectionUtility.ShouldCheckMissingInjection(current5, current2.fieldInfo, current2.def))
                                                        {
                                                            flag4 = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                DefInjectionPackage.DefInjection defInjection2;
                                                if (!dictionary.TryGetValue(current2.normalizedPath, out defInjection2))
                                                {
                                                    defInjection2 = null;
                                                }
                                                if (defInjection2 != null || flag4)
                                                {
                                                    flag2 = true;
                                                    flag  = true;
                                                    try
                                                    {
                                                        string text = TranslationFilesCleaner.ListToLiNodesString(englishList);
                                                        if (!text.NullOrEmpty())
                                                        {
                                                            xElement.Add(new XComment(" EN:\n" + text.Indented("    ") + "\n  "));
                                                        }
                                                    }
                                                    catch (Exception ex2)
                                                    {
                                                        Log.Error(string.Concat(new object[]
                                                        {
                                                            "Could not add comment node in ",
                                                            fileName,
                                                            ": ",
                                                            ex2
                                                        }), false);
                                                    }
                                                    xElement.Add(TranslationFilesCleaner.GetDefInjectableFieldNode(current2.suggestedPath, defInjection2));
                                                }
                                            }
                                        }
                                        else
                                        {
                                            DefInjectionPackage.DefInjection defInjection3;
                                            if (!dictionary.TryGetValue(current2.normalizedPath, out defInjection3))
                                            {
                                                defInjection3 = null;
                                            }
                                            string text2 = (defInjection3 == null || !defInjection3.injected) ? current2.curValue : defInjection3.replacedString;
                                            if (defInjection3 != null || DefInjectionUtility.ShouldCheckMissingInjection(text2, current2.fieldInfo, current2.def))
                                            {
                                                flag2 = true;
                                                flag  = true;
                                                try
                                                {
                                                    if (!text2.NullOrEmpty())
                                                    {
                                                        xElement.Add(new XComment(" EN: " + text2.Replace("\n", "\\n") + " "));
                                                    }
                                                }
                                                catch (Exception ex3)
                                                {
                                                    Log.Error(string.Concat(new object[]
                                                    {
                                                        "Could not add comment node in ",
                                                        fileName,
                                                        ": ",
                                                        ex3
                                                    }), false);
                                                }
                                                xElement.Add(TranslationFilesCleaner.GetDefInjectableFieldNode(current2.suggestedPath, defInjection3));
                                            }
                                        }
                                    }
                                    if (flag2)
                                    {
                                        xElement.Add(new XComment("NEWLINE"));
                                    }
                                }
                                if (enumerable2.Any <KeyValuePair <string, DefInjectionPackage.DefInjection> >())
                                {
                                    flag = true;
                                    xElement.Add(new XComment(" UNUSED "));
                                    foreach (KeyValuePair <string, DefInjectionPackage.DefInjection> current6 in enumerable2)
                                    {
                                        xElement.Add(TranslationFilesCleaner.GetDefInjectableFieldNode(current6.Value.path, current6.Value));
                                    }
                                    xElement.Add(new XComment("NEWLINE"));
                                }
                            }
                            catch (Exception ex4)
                            {
                                Log.Error(string.Concat(new object[]
                                {
                                    "Could not process def-injections for def ",
                                    defName,
                                    ": ",
                                    ex4
                                }), false);
                            }
                        }
                    }
                    finally
                    {
                        if (flag)
                        {
                            string text3 = Path.Combine(defInjectionsFolderPath, defType.Name);
                            Directory.CreateDirectory(text3);
                            TranslationFilesCleaner.SaveXMLDocumentWithProcessedNewlineTags(xDocument, Path.Combine(text3, fileName));
                        }
                    }
                }
                catch (Exception ex5)
                {
                    Log.Error(string.Concat(new object[]
                    {
                        "Could not process def-injections for file ",
                        fileName,
                        ": ",
                        ex5
                    }), false);
                }
            }
        }
Ejemplo n.º 5
0
 private static void ForEachPossibleDefInjectionInDefRecursive(object obj, string curNormalizedPath, string curSuggestedPath, HashSet <object> visited, bool translationAllowed, Def def, DefInjectionUtility.PossibleDefInjectionTraverser action)
 {
     if (obj == null)
     {
         return;
     }
     if (visited.Contains(obj))
     {
         return;
     }
     visited.Add(obj);
     foreach (FieldInfo fieldInfo in DefInjectionUtility.FieldsInDeterministicOrder(obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)))
     {
         object value = fieldInfo.GetValue(obj);
         bool   flag  = translationAllowed && !fieldInfo.HasAttribute <NoTranslateAttribute>() && !fieldInfo.HasAttribute <UnsavedAttribute>();
         if (!(value is Def))
         {
             if (typeof(string).IsAssignableFrom(fieldInfo.FieldType))
             {
                 string currentValue   = (string)value;
                 string normalizedPath = curNormalizedPath + "." + fieldInfo.Name;
                 string suggestedPath  = curSuggestedPath + "." + fieldInfo.Name;
                 action(suggestedPath, normalizedPath, false, currentValue, null, flag, false, fieldInfo, def);
             }
             else if (value is IEnumerable <string> )
             {
                 IEnumerable <string> currentValueCollection = (IEnumerable <string>)value;
                 bool   flag2           = fieldInfo.HasAttribute <TranslationCanChangeCountAttribute>();
                 string normalizedPath2 = curNormalizedPath + "." + fieldInfo.Name;
                 string suggestedPath2  = curSuggestedPath + "." + fieldInfo.Name;
                 action(suggestedPath2, normalizedPath2, true, null, currentValueCollection, flag, flag && flag2, fieldInfo, def);
             }
             else if (value is IEnumerable)
             {
                 IEnumerable enumerable  = (IEnumerable)value;
                 int         num         = 0;
                 IEnumerator enumerator2 = enumerable.GetEnumerator();
                 try
                 {
                     while (enumerator2.MoveNext())
                     {
                         object obj2 = enumerator2.Current;
                         if (obj2 != null && !(obj2 is Def) && GenTypes.IsCustomType(obj2.GetType()))
                         {
                             string text = TranslationHandleUtility.GetBestHandleWithIndexForListElement(enumerable, obj2);
                             if (text.NullOrEmpty())
                             {
                                 text = num.ToString();
                             }
                             string curNormalizedPath2 = string.Concat(new object[]
                             {
                                 curNormalizedPath,
                                 ".",
                                 fieldInfo.Name,
                                 ".",
                                 num
                             });
                             string curSuggestedPath2 = string.Concat(new string[]
                             {
                                 curSuggestedPath,
                                 ".",
                                 fieldInfo.Name,
                                 ".",
                                 text
                             });
                             DefInjectionUtility.ForEachPossibleDefInjectionInDefRecursive(obj2, curNormalizedPath2, curSuggestedPath2, visited, flag, def, action);
                         }
                         num++;
                     }
                 }
                 finally
                 {
                     IDisposable disposable;
                     if ((disposable = (enumerator2 as IDisposable)) != null)
                     {
                         disposable.Dispose();
                     }
                 }
             }
             else if (value != null && GenTypes.IsCustomType(value.GetType()))
             {
                 string curNormalizedPath3 = curNormalizedPath + "." + fieldInfo.Name;
                 string curSuggestedPath3  = curSuggestedPath + "." + fieldInfo.Name;
                 DefInjectionUtility.ForEachPossibleDefInjectionInDefRecursive(value, curNormalizedPath3, curSuggestedPath3, visited, flag, def, action);
             }
         }
     }
 }
Ejemplo n.º 6
0
        private static void ForEachPossibleDefInjectionInDef(Def def, DefInjectionUtility.PossibleDefInjectionTraverser action)
        {
            HashSet <object> visited = new HashSet <object>();

            DefInjectionUtility.ForEachPossibleDefInjectionInDefRecursive(def, def.defName, def.defName, visited, true, def, action);
        }
Ejemplo n.º 7
0
        public List <string> MissingInjections(List <string> outUnnecessaryDefInjections)
        {
            List <string> missingInjections = new List <string>();
            Dictionary <string, DefInjection> injectionsByNormalizedPath = new Dictionary <string, DefInjection>();

            foreach (KeyValuePair <string, DefInjection> injection in injections)
            {
                if (!injectionsByNormalizedPath.ContainsKey(injection.Value.normalizedPath))
                {
                    injectionsByNormalizedPath.Add(injection.Value.normalizedPath, injection.Value);
                }
            }
            DefInjectionUtility.ForEachPossibleDefInjection(defType, delegate(string suggestedPath, string normalizedPath, bool isCollection, string str, IEnumerable <string> collection, bool translationAllowed, bool fullListTranslationAllowed, FieldInfo fieldInfo, Def def)
            {
                DefInjection value2;
                if (!isCollection)
                {
                    bool flag   = false;
                    string text = null;
                    if (injectionsByNormalizedPath.TryGetValue(normalizedPath, out DefInjection value) && !value.IsFullListInjection)
                    {
                        if (!translationAllowed)
                        {
                            outUnnecessaryDefInjections.Add(value.path + " '" + value.injection.Replace("\n", "\\n") + "'");
                        }
                        else if (value.isPlaceholder)
                        {
                            flag = true;
                            text = value.fileSource;
                        }
                    }
                    else
                    {
                        flag = true;
                    }
                    if (flag && translationAllowed && DefInjectionUtility.ShouldCheckMissingInjection(str, fieldInfo, def))
                    {
                        missingInjections.Add(suggestedPath + " '" + str.Replace("\n", "\\n") + "'" + (text.NullOrEmpty() ? "" : (" (placeholder exists in " + text + ")")));
                    }
                }
                else if (injectionsByNormalizedPath.TryGetValue(normalizedPath, out value2) && value2.IsFullListInjection)
                {
                    if (!translationAllowed || !fullListTranslationAllowed)
                    {
                        outUnnecessaryDefInjections.Add(value2.path + " '" + value2.fullListInjection.ToStringSafeEnumerable().Replace("\n", "\\n") + "'");
                    }
                    else if (value2.isPlaceholder && translationAllowed && !def.generated)
                    {
                        missingInjections.Add(suggestedPath + (value2.fileSource.NullOrEmpty() ? "" : (" (placeholder exists in " + value2.fileSource + ")")));
                    }
                }
                else
                {
                    int num = 0;
                    foreach (string item in collection)
                    {
                        string key   = normalizedPath + "." + num;
                        string text2 = suggestedPath + "." + num;
                        bool flag2   = false;
                        string text3 = null;
                        if (injectionsByNormalizedPath.TryGetValue(key, out DefInjection value3) && !value3.IsFullListInjection)
                        {
                            if (!translationAllowed)
                            {
                                outUnnecessaryDefInjections.Add(value3.path + " '" + value3.injection.Replace("\n", "\\n") + "'");
                            }
                            else if (value3.isPlaceholder)
                            {
                                flag2 = true;
                                text3 = value3.fileSource;
                            }
                        }
                        else
                        {
                            flag2 = true;
                        }
                        if (flag2 && translationAllowed && DefInjectionUtility.ShouldCheckMissingInjection(item, fieldInfo, def))
                        {
                            if (text3.NullOrEmpty() && injectionsByNormalizedPath.TryGetValue(normalizedPath, out DefInjection value4) && value4.isPlaceholder)
                            {
                                text3 = value4.fileSource;
                            }
                            missingInjections.Add(text2 + " '" + item.Replace("\n", "\\n") + "'" + (fullListTranslationAllowed ? " (hint: this list allows full-list translation by using <li> nodes)" : "") + (text3.NullOrEmpty() ? "" : (" (placeholder exists in " + text3 + ")")));
                        }
                        num++;
                    }
                }
            });
            return(missingInjections);
        }
        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);
                }
            }
        }
Ejemplo n.º 9
0
        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);
                    }
                }
            }
        }