public static void DefLabels()
        {
            List <DebugMenuOption> list = new List <DebugMenuOption>();

            foreach (Type type in from def in GenDefDatabase.AllDefTypesWithDatabases()
                     orderby def.Name
                     select def)
            {
                DebugMenuOption item = new DebugMenuOption(type.Name, DebugMenuOptionMode.Action, delegate
                {
                    IEnumerable source          = (IEnumerable)GenGeneric.GetStaticPropertyOnGenericType(typeof(DefDatabase <>), type, "AllDefs");
                    int num                     = 0;
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (Def item2 in source.Cast <Def>())
                    {
                        stringBuilder.AppendLine(item2.label);
                        num++;
                        if (num >= 500)
                        {
                            Log.Message(stringBuilder.ToString());
                            stringBuilder = new StringBuilder();
                            num           = 0;
                        }
                    }
                    Log.Message(stringBuilder.ToString());
                });
                list.Add(item);
            }
            Find.WindowStack.Add(new Dialog_DebugOptionListLister(list));
        }
        public static void DefNamesAll()
        {
            StringBuilder stringBuilder = new StringBuilder();
            int           num           = 0;

            foreach (Type item in from def in GenDefDatabase.AllDefTypesWithDatabases()
                     orderby def.Name
                     select def)
            {
                IEnumerable source = (IEnumerable)GenGeneric.GetStaticPropertyOnGenericType(typeof(DefDatabase <>), item, "AllDefs");
                stringBuilder.AppendLine("--    " + item.ToString());
                foreach (Def item2 in from Def def in source
                         orderby def.defName
                         select def)
                {
                    stringBuilder.AppendLine(item2.defName);
                    num++;
                    if (num >= 500)
                    {
                        Log.Message(stringBuilder.ToString());
                        stringBuilder = new StringBuilder();
                        num           = 0;
                    }
                }
                stringBuilder.AppendLine();
                stringBuilder.AppendLine();
            }
            Log.Message(stringBuilder.ToString());
        }
 public void InjectIntoDefs()
 {
     foreach (KeyValuePair <string, string> current in this.injections)
     {
         string[] array = current.Key.Split(new char[]
         {
             '.'
         });
         string text = array[0];
         text = BackCompatibility.BackCompatibleDefName(this.defType, text);
         if (GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), this.defType, "GetNamedSilentFail", new object[]
         {
             text
         }) == null)
         {
             Log.Warning(string.Concat(new object[]
             {
                 "Def-linked translation error: Found no ",
                 this.defType,
                 " named ",
                 text,
                 " to match ",
                 current.Key
             }));
         }
         else
         {
             this.SetDefFieldAtPath(this.defType, current.Key, current.Value);
         }
     }
     GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), this.defType, "ClearCachedData");
 }
Exemple #4
0
 public static T DefFromNodeUnsafe <T>(XmlNode subNode)
 {
     return((T)((object)GenGeneric.InvokeStaticGenericMethod(typeof(ScribeExtractor), typeof(T), "DefFromNode", new object[]
     {
         subNode
     })));
 }
 public static Def GetDefSilentFail(Type type, string targetDefName)
 {
     if (type == typeof(SoundDef))
     {
         return(SoundDef.Named(targetDefName));
     }
     return((Def)GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), type, "GetNamedSilentFail", targetDefName));
 }
 public static Def GetDef(Type defType, string defName, bool errorOnFail = true)
 {
     return((Def)GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), defType, "GetNamed", new object[]
     {
         defName,
         errorOnFail
     }));
 }
Exemple #7
0
        public static Graphic Get(Type graphicClass, string path, Shader shader, Vector2 drawSize, Color color, Color colorTwo, GraphicData data)
        {
            GraphicRequest graphicRequest = new GraphicRequest(graphicClass, path, shader, drawSize, color, colorTwo, data, 0);

            if (graphicRequest.graphicClass == typeof(Graphic_Single))
            {
                return(GraphicDatabase.GetInner <Graphic_Single>(graphicRequest));
            }
            if (graphicRequest.graphicClass == typeof(Graphic_Terrain))
            {
                return(GraphicDatabase.GetInner <Graphic_Terrain>(graphicRequest));
            }
            if (graphicRequest.graphicClass == typeof(Graphic_Multi))
            {
                return(GraphicDatabase.GetInner <Graphic_Multi>(graphicRequest));
            }
            if (graphicRequest.graphicClass == typeof(Graphic_Mote))
            {
                return(GraphicDatabase.GetInner <Graphic_Mote>(graphicRequest));
            }
            if (graphicRequest.graphicClass == typeof(Graphic_Random))
            {
                return(GraphicDatabase.GetInner <Graphic_Random>(graphicRequest));
            }
            if (graphicRequest.graphicClass == typeof(Graphic_Flicker))
            {
                return(GraphicDatabase.GetInner <Graphic_Flicker>(graphicRequest));
            }
            if (graphicRequest.graphicClass == typeof(Graphic_Appearances))
            {
                return(GraphicDatabase.GetInner <Graphic_Appearances>(graphicRequest));
            }
            if (graphicRequest.graphicClass == typeof(Graphic_StackCount))
            {
                return(GraphicDatabase.GetInner <Graphic_StackCount>(graphicRequest));
            }
            try
            {
                return((Graphic)GenGeneric.InvokeStaticGenericMethod(typeof(GraphicDatabase), graphicRequest.graphicClass, "GetInner", new object[]
                {
                    graphicRequest
                }));
            }
            catch (Exception ex)
            {
                Log.Error(string.Concat(new object[]
                {
                    "Exception getting ",
                    graphicClass,
                    " at ",
                    path,
                    ": ",
                    ex.ToString()
                }));
            }
            return(BaseContent.BadGraphic);
        }
 public static Def GetDefSilentFail(Type type, string targetDefName, bool specialCaseForSoundDefs = true)
 {
     if (specialCaseForSoundDefs && type == typeof(SoundDef))
     {
         return(SoundDef.Named(targetDefName));
     }
     return((Def)GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), type, "GetNamedSilentFail", new object[]
     {
         targetDefName
     }));
 }
 public WeatherWorker(WeatherDef def)
 {
     this.def = def;
     foreach (Type current in def.overlayClasses)
     {
         SkyOverlay item = (SkyOverlay)GenGeneric.InvokeStaticGenericMethod(typeof(WeatherPartPool), current, "GetInstanceOf");
         this.overlays.Add(item);
     }
     this.skyTargets[0] = new WeatherWorker.SkyThreshold(def.skyColorsNightMid, 0f);
     this.skyTargets[1] = new WeatherWorker.SkyThreshold(def.skyColorsNightEdge, 0.1f);
     this.skyTargets[2] = new WeatherWorker.SkyThreshold(def.skyColorsDusk, 0.6f);
     this.skyTargets[3] = new WeatherWorker.SkyThreshold(def.skyColorsDay, 1f);
 }
 public static void ClearAllPlayData()
 {
     LanguageDatabase.Clear();
     LoadedModManager.ClearDestroy();
     foreach (Type item in typeof(Def).AllSubclasses())
     {
         GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), item, "Clear");
     }
     ThingCategoryNodeDatabase.Clear();
     BackstoryDatabase.Clear();
     SolidBioDatabase.Clear();
     Current.Game             = null;
     PlayDataLoader.loadedInt = false;
 }
Exemple #11
0
 public void InjectIntoDefs()
 {
     foreach (KeyValuePair <string, string> injection in this.injections)
     {
         string[] array   = injection.Key.Split('.');
         string   defName = array[0];
         defName = BackCompatibility.BackCompatibleDefName(this.defType, defName);
         object obj = GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), this.defType, "GetNamedSilentFail", defName);
         if (obj == null)
         {
             Log.Warning("Def-linked translation error: Found no " + this.defType + " named " + defName + " to match " + injection.Key);
         }
         else
         {
             this.SetDefFieldAtPath(this.defType, injection.Key, injection.Value);
         }
     }
     GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), this.defType, "ClearCachedData");
 }
Exemple #12
0
 public void InjectIntoDefs(bool errorOnDefNotFound)
 {
     foreach (KeyValuePair <string, DefInjection> injection in injections)
     {
         if (!injection.Value.injected)
         {
             string normalizedPath;
             string suggestedPath;
             if (injection.Value.IsFullListInjection)
             {
                 injection.Value.injected = SetDefFieldAtPath(defType, injection.Key, injection.Value.fullListInjection, typeof(List <string>), errorOnDefNotFound, injection.Value.fileSource, injection.Value.isPlaceholder, out normalizedPath, out suggestedPath, out injection.Value.replacedString, out injection.Value.replacedList);
             }
             else
             {
                 injection.Value.injected = SetDefFieldAtPath(defType, injection.Key, injection.Value.injection, typeof(string), errorOnDefNotFound, injection.Value.fileSource, injection.Value.isPlaceholder, out normalizedPath, out suggestedPath, out injection.Value.replacedString, out injection.Value.replacedList);
             }
             injection.Value.normalizedPath = normalizedPath;
             injection.Value.suggestedPath  = suggestedPath;
         }
     }
     GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), defType, "ClearCachedData");
 }
 public void InjectIntoDefs(bool errorOnDefNotFound)
 {
     foreach (KeyValuePair <string, DefInjectionPackage.DefInjection> current in this.injections)
     {
         if (!current.Value.injected)
         {
             string normalizedPath;
             string suggestedPath;
             if (current.Value.IsFullListInjection)
             {
                 current.Value.injected = this.SetDefFieldAtPath(this.defType, current.Key, current.Value.fullListInjection, typeof(List <string>), errorOnDefNotFound, current.Value.fileSource, current.Value.isPlaceholder, out normalizedPath, out suggestedPath, out current.Value.replacedString, out current.Value.replacedList);
             }
             else
             {
                 current.Value.injected = this.SetDefFieldAtPath(this.defType, current.Key, current.Value.injection, typeof(string), errorOnDefNotFound, current.Value.fileSource, current.Value.isPlaceholder, out normalizedPath, out suggestedPath, out current.Value.replacedString, out current.Value.replacedList);
             }
             current.Value.normalizedPath = normalizedPath;
             current.Value.suggestedPath  = suggestedPath;
         }
     }
     GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), this.defType, "ClearCachedData");
 }
 public static IEnumerable <Def> GetAllDefsInDatabaseForDef(Type defType)
 {
     return(((IEnumerable)GenGeneric.GetStaticPropertyOnGenericType(typeof(DefDatabase <>), defType, "AllDefs")).Cast <Def>());
 }
        private void SetDefFieldAtPath(Type defType, string path, string value)
        {
            path = BackCompatibility.BackCompatibleModifiedTranslationPath(defType, path);
            try
            {
                List <string> list = path.Split(new char[]
                {
                    '.'
                }).ToList <string>();
                object obj = GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), defType, "GetNamedSilentFail", new object[]
                {
                    list[0]
                });
                if (obj == null)
                {
                    throw new InvalidOperationException("Def named " + list[0] + " not found.");
                }
                list.RemoveAt(0);
                DefInjectionPathPartKind defInjectionPathPartKind;
                string text;
                int    num;
                while (true)
                {
                    defInjectionPathPartKind = DefInjectionPathPartKind.Field;
                    text = list[0];
                    num  = -1;
                    if (text.Contains('['))
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.FieldWithListIndex;
                        string[] array = text.Split(new char[]
                        {
                            '['
                        });
                        string text2 = array[1];
                        text2 = text2.Substring(0, text2.Length - 1);
                        num   = (int)ParseHelper.FromString(text2, typeof(int));
                        text  = array[0];
                    }
                    else if (int.TryParse(text, out num))
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.ListIndex;
                    }
                    if (list.Count == 1)
                    {
                        break;
                    }
                    if (defInjectionPathPartKind == DefInjectionPathPartKind.ListIndex)
                    {
                        PropertyInfo property = obj.GetType().GetProperty("Item");
                        if (property == null)
                        {
                            goto Block_17;
                        }
                        obj = property.GetValue(obj, new object[]
                        {
                            num
                        });
                    }
                    else
                    {
                        FieldInfo field = obj.GetType().GetField(text, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                        if (field == null)
                        {
                            goto Block_18;
                        }
                        if (defInjectionPathPartKind == DefInjectionPathPartKind.Field)
                        {
                            obj = field.GetValue(obj);
                        }
                        else
                        {
                            object       value2    = field.GetValue(obj);
                            PropertyInfo property2 = value2.GetType().GetProperty("Item");
                            if (property2 == null)
                            {
                                goto Block_20;
                            }
                            obj = property2.GetValue(value2, new object[]
                            {
                                num
                            });
                        }
                    }
                    list.RemoveAt(0);
                }
                if (defInjectionPathPartKind == DefInjectionPathPartKind.Field)
                {
                    FieldInfo fieldNamed = this.GetFieldNamed(obj.GetType(), text);
                    if (fieldNamed == null)
                    {
                        throw new InvalidOperationException(string.Concat(new object[]
                        {
                            "Field ",
                            text,
                            " does not exist in type ",
                            obj.GetType(),
                            "."
                        }));
                    }
                    if (fieldNamed.HasAttribute <NoTranslateAttribute>())
                    {
                        Log.Error(string.Concat(new object[]
                        {
                            "Translated untranslateable field ",
                            fieldNamed.Name,
                            " of type ",
                            fieldNamed.FieldType,
                            " at path ",
                            path,
                            ". Translating this field will break the game."
                        }));
                    }
                    else if (fieldNamed.FieldType != typeof(string))
                    {
                        Log.Error(string.Concat(new object[]
                        {
                            "Translated non-string field ",
                            fieldNamed.Name,
                            " of type ",
                            fieldNamed.FieldType,
                            " at path ",
                            path,
                            ". Only string fields should be translated."
                        }));
                    }
                    else
                    {
                        fieldNamed.SetValue(obj, value);
                    }
                }
                else
                {
                    object obj2;
                    if (defInjectionPathPartKind == DefInjectionPathPartKind.FieldWithListIndex)
                    {
                        FieldInfo field2 = obj.GetType().GetField(text, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                        if (field2 == null)
                        {
                            throw new InvalidOperationException("Field " + text + " does not exist.");
                        }
                        obj2 = field2.GetValue(obj);
                    }
                    else
                    {
                        obj2 = obj;
                    }
                    Type         type      = obj2.GetType();
                    PropertyInfo property3 = type.GetProperty("Count");
                    if (property3 == null)
                    {
                        throw new InvalidOperationException("Tried to use index on non-list (missing 'Count' property).");
                    }
                    int num2 = (int)property3.GetValue(obj2, null);
                    if (num >= num2)
                    {
                        throw new InvalidOperationException(string.Concat(new object[]
                        {
                            "Trying to translate ",
                            defType,
                            ".",
                            path,
                            " at index ",
                            num,
                            " but the original list only has ",
                            num2,
                            " entries (so the max index is ",
                            (num2 - 1).ToString(),
                            ")."
                        }));
                    }
                    PropertyInfo property4 = type.GetProperty("Item");
                    if (property4 == null)
                    {
                        throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
                    }
                    property4.SetValue(obj2, value, new object[]
                    {
                        num
                    });
                }
                return;

Block_17:
                throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
Block_18:
                throw new InvalidOperationException("Field " + text + " does not exist.");
Block_20:
                throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
            }
            catch (Exception ex)
            {
                Log.Warning(string.Concat(new object[]
                {
                    "Def-linked translation error: Exception getting field at path ",
                    path,
                    " in ",
                    defType,
                    ": ",
                    ex.ToString()
                }));
            }
        }
Exemple #16
0
 public static object InvokeStaticMethodOnGenericType(Type genericBase, Type genericParam, string methodName)
 {
     return(GenGeneric.MethodOnGenericType(genericBase, genericParam, methodName).Invoke(null, null));
 }
        private bool SetDefFieldAtPath(Type defType, string path, object value, Type ensureFieldType, bool errorOnDefNotFound, string fileSource, bool isPlaceholder, out string normalizedPath, out string suggestedPath, out string replacedString, out IEnumerable <string> replacedStringsList)
        {
            replacedString      = null;
            replacedStringsList = null;
            normalizedPath      = path;
            suggestedPath       = path;
            string text = path.Split(new char[]
            {
                '.'
            })[0];

            text = BackCompatibility.BackCompatibleDefName(defType, text, true);
            if (GenDefDatabase.GetDefSilentFail(defType, text, false) == null)
            {
                if (errorOnDefNotFound)
                {
                    this.loadErrors.Add(string.Concat(new object[]
                    {
                        "Found no ",
                        defType,
                        " named ",
                        text,
                        " to match ",
                        path,
                        " (",
                        fileSource,
                        ")"
                    }));
                }
                return(false);
            }
            bool flag = false;
            int  num  = 0;
            bool result;

            try
            {
                List <string> list = path.Split(new char[]
                {
                    '.'
                }).ToList <string>();
                object obj = GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), defType, "GetNamedSilentFail", new object[]
                {
                    list[0]
                });
                if (obj == null)
                {
                    throw new InvalidOperationException("Def named " + list[0] + " not found.");
                }
                list.RemoveAt(0);
                num++;
                string text2;
                int    num2;
                DefInjectionPathPartKind defInjectionPathPartKind;
                FieldInfo field;
                int       num3;
                int       num4;
                while (true)
                {
                    text2 = list[0];
                    num2  = -1;
                    if (int.TryParse(text2, out num2))
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.ListIndex;
                    }
                    else if (this.GetFieldNamed(obj.GetType(), text2) != null)
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.Field;
                    }
                    else if (obj.GetType().GetProperty("Count") != null)
                    {
                        if (text2.Contains('-'))
                        {
                            defInjectionPathPartKind = DefInjectionPathPartKind.ListHandleWithIndex;
                            string[] array = text2.Split(new char[]
                            {
                                '-'
                            });
                            text2 = array[0];
                            num2  = (int)ParseHelper.FromString(array[1], typeof(int));
                        }
                        else
                        {
                            defInjectionPathPartKind = DefInjectionPathPartKind.ListHandle;
                        }
                    }
                    else
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.Field;
                    }
                    if (list.Count == 1)
                    {
                        break;
                    }
                    if (defInjectionPathPartKind == DefInjectionPathPartKind.Field)
                    {
                        field = obj.GetType().GetField(text2, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                        if (field == null)
                        {
                            goto Block_41;
                        }
                        if (field.HasAttribute <NoTranslateAttribute>())
                        {
                            goto Block_42;
                        }
                        if (field.HasAttribute <UnsavedAttribute>())
                        {
                            goto Block_43;
                        }
                        if (field.HasAttribute <TranslationCanChangeCountAttribute>())
                        {
                            flag = true;
                        }
                        if (defInjectionPathPartKind == DefInjectionPathPartKind.Field)
                        {
                            obj = field.GetValue(obj);
                        }
                        else
                        {
                            object       value2   = field.GetValue(obj);
                            PropertyInfo property = value2.GetType().GetProperty("Item");
                            if (property == null)
                            {
                                goto Block_46;
                            }
                            num3 = (int)value2.GetType().GetProperty("Count").GetValue(value2, null);
                            if (num2 < 0 || num2 >= num3)
                            {
                                goto IL_ADC;
                            }
                            obj = property.GetValue(value2, new object[]
                            {
                                num2
                            });
                        }
                    }
                    else if (defInjectionPathPartKind == DefInjectionPathPartKind.ListIndex || defInjectionPathPartKind == DefInjectionPathPartKind.ListHandle || defInjectionPathPartKind == DefInjectionPathPartKind.ListHandleWithIndex)
                    {
                        object       obj2      = obj;
                        PropertyInfo property2 = obj2.GetType().GetProperty("Item");
                        if (property2 == null)
                        {
                            goto Block_50;
                        }
                        bool flag2;
                        if (defInjectionPathPartKind == DefInjectionPathPartKind.ListHandle || defInjectionPathPartKind == DefInjectionPathPartKind.ListHandleWithIndex)
                        {
                            num2 = TranslationHandleUtility.GetElementIndexByHandle(obj2, text2, num2);
                            defInjectionPathPartKind = DefInjectionPathPartKind.ListIndex;
                            flag2 = true;
                        }
                        else
                        {
                            flag2 = false;
                        }
                        num4 = (int)obj2.GetType().GetProperty("Count").GetValue(obj2, null);
                        if (num2 < 0 || num2 >= num4)
                        {
                            goto IL_BB7;
                        }
                        obj = property2.GetValue(obj2, new object[]
                        {
                            num2
                        });
                        if (flag2)
                        {
                            string[] array2 = normalizedPath.Split(new char[]
                            {
                                '.'
                            });
                            array2[num]    = num2.ToString();
                            normalizedPath = string.Join(".", array2);
                        }
                        else
                        {
                            string bestHandleWithIndexForListElement = TranslationHandleUtility.GetBestHandleWithIndexForListElement(obj2, obj);
                            if (!bestHandleWithIndexForListElement.NullOrEmpty())
                            {
                                string[] array3 = suggestedPath.Split(new char[]
                                {
                                    '.'
                                });
                                array3[num]   = bestHandleWithIndexForListElement;
                                suggestedPath = string.Join(".", array3);
                            }
                        }
                    }
                    else
                    {
                        this.loadErrors.Add(string.Concat(new object[]
                        {
                            "Can't enter node ",
                            text2,
                            " at path ",
                            path,
                            ", element kind is ",
                            defInjectionPathPartKind,
                            ". (",
                            fileSource,
                            ")"
                        }));
                    }
                    list.RemoveAt(0);
                    num++;
                }
                bool flag3 = false;
                foreach (KeyValuePair <string, DefInjectionPackage.DefInjection> current in this.injections)
                {
                    if (!(current.Key == path))
                    {
                        if (current.Value.injected && current.Value.normalizedPath == normalizedPath)
                        {
                            string text3 = string.Concat(new string[]
                            {
                                "Duplicate def-injected translation key. Both ",
                                current.Value.path,
                                " and ",
                                path,
                                " refer to the same field (",
                                suggestedPath,
                                ")"
                            });
                            if (current.Value.path != current.Value.nonBackCompatiblePath)
                            {
                                string text4 = text3;
                                text3 = string.Concat(new string[]
                                {
                                    text4,
                                    " (",
                                    current.Value.nonBackCompatiblePath,
                                    " was auto-renamed to ",
                                    current.Value.path,
                                    ")"
                                });
                            }
                            text3 = text3 + " (" + current.Value.fileSource + ")";
                            this.loadErrors.Add(text3);
                            flag3 = true;
                            break;
                        }
                    }
                }
                bool flag4 = false;
                if (!flag3)
                {
                    if (defInjectionPathPartKind == DefInjectionPathPartKind.Field)
                    {
                        FieldInfo fieldNamed = this.GetFieldNamed(obj.GetType(), text2);
                        if (fieldNamed == null)
                        {
                            throw new InvalidOperationException(string.Concat(new object[]
                            {
                                "Field ",
                                text2,
                                " does not exist in type ",
                                obj.GetType(),
                                "."
                            }));
                        }
                        if (fieldNamed.HasAttribute <NoTranslateAttribute>())
                        {
                            this.loadErrors.Add(string.Concat(new object[]
                            {
                                "Translated untranslatable field ",
                                fieldNamed.Name,
                                " of type ",
                                fieldNamed.FieldType,
                                " at path ",
                                path,
                                ". Translating this field will break the game. (",
                                fileSource,
                                ")"
                            }));
                        }
                        else if (fieldNamed.HasAttribute <UnsavedAttribute>())
                        {
                            this.loadErrors.Add(string.Concat(new object[]
                            {
                                "Translated untranslatable field (UnsavedAttribute) ",
                                fieldNamed.Name,
                                " of type ",
                                fieldNamed.FieldType,
                                " at path ",
                                path,
                                ". Translating this field will break the game. (",
                                fileSource,
                                ")"
                            }));
                        }
                        else if (!isPlaceholder && fieldNamed.FieldType != ensureFieldType)
                        {
                            this.loadErrors.Add(string.Concat(new object[]
                            {
                                "Translated non-",
                                ensureFieldType,
                                " field ",
                                fieldNamed.Name,
                                " of type ",
                                fieldNamed.FieldType,
                                " at path ",
                                path,
                                ". Expected ",
                                ensureFieldType,
                                ". (",
                                fileSource,
                                ")"
                            }));
                        }
                        else if (!isPlaceholder && ensureFieldType != typeof(string) && !fieldNamed.HasAttribute <TranslationCanChangeCountAttribute>())
                        {
                            this.loadErrors.Add(string.Concat(new object[]
                            {
                                "Tried to translate field ",
                                fieldNamed.Name,
                                " of type ",
                                fieldNamed.FieldType,
                                " at path ",
                                path,
                                ", but this field doesn't have [TranslationCanChangeCount] attribute so it doesn't allow this type of translation. (",
                                fileSource,
                                ")"
                            }));
                        }
                        else if (!isPlaceholder)
                        {
                            if (ensureFieldType == typeof(string))
                            {
                                replacedString = (string)fieldNamed.GetValue(obj);
                            }
                            else
                            {
                                replacedStringsList = (fieldNamed.GetValue(obj) as IEnumerable <string>);
                            }
                            fieldNamed.SetValue(obj, value);
                            flag4 = true;
                        }
                    }
                    else if (defInjectionPathPartKind == DefInjectionPathPartKind.ListIndex || defInjectionPathPartKind == DefInjectionPathPartKind.ListHandle || defInjectionPathPartKind == DefInjectionPathPartKind.ListHandleWithIndex)
                    {
                        object obj3 = obj;
                        if (obj3 == null)
                        {
                            throw new InvalidOperationException("Tried to use index on null list at " + path);
                        }
                        Type         type      = obj3.GetType();
                        PropertyInfo property3 = type.GetProperty("Count");
                        if (property3 == null)
                        {
                            throw new InvalidOperationException("Tried to use index on non-list (missing 'Count' property).");
                        }
                        if (defInjectionPathPartKind == DefInjectionPathPartKind.ListHandle || defInjectionPathPartKind == DefInjectionPathPartKind.ListHandleWithIndex)
                        {
                            num2 = TranslationHandleUtility.GetElementIndexByHandle(obj3, text2, num2);
                            defInjectionPathPartKind = DefInjectionPathPartKind.ListIndex;
                        }
                        int num5 = (int)property3.GetValue(obj3, null);
                        if (num2 >= num5)
                        {
                            throw new InvalidOperationException(string.Concat(new object[]
                            {
                                "Trying to translate ",
                                defType,
                                ".",
                                path,
                                " at index ",
                                num2,
                                " but the list only has ",
                                num5,
                                " entries (so max index is ",
                                (num5 - 1).ToString(),
                                ")."
                            }));
                        }
                        PropertyInfo property4 = type.GetProperty("Item");
                        if (property4 == null)
                        {
                            throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
                        }
                        Type propertyType = property4.PropertyType;
                        if (!isPlaceholder && propertyType != ensureFieldType)
                        {
                            this.loadErrors.Add(string.Concat(new object[]
                            {
                                "Translated non-",
                                ensureFieldType,
                                " list item of type ",
                                propertyType,
                                " at path ",
                                path,
                                ". Expected ",
                                ensureFieldType,
                                ". (",
                                fileSource,
                                ")"
                            }));
                        }
                        else if (!isPlaceholder && ensureFieldType != typeof(string) && !flag)
                        {
                            this.loadErrors.Add(string.Concat(new object[]
                            {
                                "Tried to translate field of type ",
                                propertyType,
                                " at path ",
                                path,
                                ", but this field doesn't have [TranslationCanChangeCount] attribute so it doesn't allow this type of translation. (",
                                fileSource,
                                ")"
                            }));
                        }
                        else if (num2 < 0 || num2 >= (int)type.GetProperty("Count").GetValue(obj3, null))
                        {
                            this.loadErrors.Add("Index out of bounds (max index is " + ((int)type.GetProperty("Count").GetValue(obj3, null) - 1) + ")");
                        }
                        else if (!isPlaceholder)
                        {
                            replacedString = (string)property4.GetValue(obj3, new object[]
                            {
                                num2
                            });
                            property4.SetValue(obj3, value, new object[]
                            {
                                num2
                            });
                            flag4 = true;
                        }
                    }
                    else
                    {
                        this.loadErrors.Add(string.Concat(new object[]
                        {
                            "Translated ",
                            text2,
                            " at path ",
                            path,
                            " but it's not a field, it's ",
                            defInjectionPathPartKind,
                            ". (",
                            fileSource,
                            ")"
                        }));
                    }
                }
                if (path != suggestedPath)
                {
                    IList <string> list2 = value as IList <string>;
                    string         text5;
                    if (list2 != null)
                    {
                        text5 = list2.ToStringSafeEnumerable();
                    }
                    else
                    {
                        text5 = value.ToString();
                    }
                    this.loadSyntaxSuggestions.Add(string.Concat(new string[]
                    {
                        "Consider using ",
                        suggestedPath,
                        " instead of ",
                        path,
                        " for translation '",
                        text5,
                        "' (",
                        fileSource,
                        ")"
                    }));
                }
                result = flag4;
                return(result);

Block_41:
                throw new InvalidOperationException("Field " + text2 + " does not exist.");
Block_42:
                throw new InvalidOperationException(string.Concat(new object[]
                {
                    "Translated untranslatable field ",
                    field.Name,
                    " of type ",
                    field.FieldType,
                    " at path ",
                    path,
                    ". Translating this field will break the game."
                }));
Block_43:
                throw new InvalidOperationException(string.Concat(new object[]
                {
                    "Translated untranslatable field ([Unsaved] attribute) ",
                    field.Name,
                    " of type ",
                    field.FieldType,
                    " at path ",
                    path,
                    ". Translating this field will break the game."
                }));
Block_46:
                throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
IL_ADC:
                throw new InvalidOperationException("Index out of bounds (max index is " + (num3 - 1) + ")");
Block_50:
                throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
IL_BB7:
                throw new InvalidOperationException("Index out of bounds (max index is " + (num4 - 1) + ")");
            }
            catch (Exception ex)
            {
                string text6 = string.Concat(new object[]
                {
                    "Couldn't inject ",
                    path,
                    " into ",
                    defType,
                    " (",
                    fileSource,
                    "): ",
                    ex.Message
                });
                if (ex.InnerException != null)
                {
                    text6 = text6 + " -> " + ex.InnerException.Message;
                }
                this.loadErrors.Add(text6);
                result = false;
            }
            return(result);
        }
 private static void DoPlayLoad()
 {
     GraphicDatabase.Clear();
     DeepProfiler.Start("Load all active mods.");
     try
     {
         LoadedModManager.LoadAllActiveMods();
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Load language metadata.");
     try
     {
         LanguageDatabase.LoadAllMetadata();
     }
     finally
     {
         DeepProfiler.End();
     }
     LongEventHandler.SetCurrentEventText("LoadingDefs".Translate());
     DeepProfiler.Start("Copy all Defs from mods to global databases.");
     try
     {
         foreach (Type item in typeof(Def).AllSubclasses())
         {
             GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), item, "AddAllInMods");
         }
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Resolve cross-references between non-implied Defs.");
     try
     {
         DirectXmlCrossRefLoader.ResolveAllWantedCrossReferences(FailMode.Silent);
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Rebind defs (early).");
     try
     {
         DefOfHelper.RebindAllDefOfs(true);
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Generate implied Defs (pre-resolve).");
     try
     {
         DefGenerator.GenerateImpliedDefs_PreResolve();
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Resolve cross-references between Defs made by the implied defs.");
     try
     {
         DirectXmlCrossRefLoader.ResolveAllWantedCrossReferences(FailMode.LogErrors);
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Rebind DefOfs (final).");
     try
     {
         DefOfHelper.RebindAllDefOfs(false);
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Other def binding, resetting and global operations.");
     try
     {
         PlayerKnowledgeDatabase.ReloadAndRebind();
         LessonAutoActivator.Reset();
         CostListCalculator.Reset();
         PawnApparelGenerator.Reset();
         RestUtility.Reset();
         ThoughtUtility.Reset();
         PawnWeaponGenerator.Reset();
         ThinkTreeKeyAssigner.Reset();
         ThingCategoryNodeDatabase.FinalizeInit();
         TrainableUtility.Reset();
         HaulAIUtility.Reset();
         GenConstruct.Reset();
         WorkGiver_FillFermentingBarrel.Reset();
         WorkGiver_DoBill.Reset();
         Pawn.Reset();
         WorkGiver_InteractAnimal.Reset();
         WorkGiver_Warden_DoExecution.Reset();
         WorkGiver_GrowerSow.Reset();
         WorkGiver_Miner.Reset();
         MedicalCareUtility.Reset();
         InspectPaneUtility.Reset();
         GraphicDatabaseHeadRecords.Reset();
         DateReadout.Reset();
         ResearchProjectDef.GenerateNonOverlappingCoordinates();
         WorkGiver_FixBrokenDownBuilding.CacheTranslations();
         ItemCollectionGeneratorUtility.Reset();
         BaseGen.Reset();
         HealthUtility.Reset();
         ResourceCounter.ResetDefs();
         WildSpawner.Reset();
         ApparelProperties.Reset();
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Resolve references.");
     try
     {
         foreach (Type item2 in typeof(Def).AllSubclasses())
         {
             if (item2 != typeof(ThingDef))
             {
                 GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), item2, "ResolveAllReferences", true);
             }
         }
         DefDatabase <ThingDef> .ResolveAllReferences(true);
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Generate implied Defs (post-resolve).");
     try
     {
         DefGenerator.GenerateImpliedDefs_PostResolve();
     }
     finally
     {
         DeepProfiler.End();
     }
     if (Prefs.DevMode)
     {
         DeepProfiler.Start("Error check all defs.");
         try
         {
             foreach (Type item3 in typeof(Def).AllSubclasses())
             {
                 GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), item3, "ErrorCheckAllDefs");
             }
         }
         finally
         {
             DeepProfiler.End();
         }
     }
     LongEventHandler.SetCurrentEventText("Initializing".Translate());
     DeepProfiler.Start("Load keyboard preferences.");
     try
     {
         KeyPrefs.Init();
     }
     finally
     {
         DeepProfiler.End();
     }
     DeepProfiler.Start("Short hash giving.");
     try
     {
         ShortHashGiver.GiveAllShortHashes();
     }
     finally
     {
         DeepProfiler.End();
     }
     LongEventHandler.ExecuteWhenFinished(delegate
     {
         DeepProfiler.Start("Load backstories.");
         try
         {
             BackstoryDatabase.ReloadAllBackstories();
         }
         finally
         {
             DeepProfiler.End();
         }
     });
     LongEventHandler.ExecuteWhenFinished(delegate
     {
         DeepProfiler.Start("Inject selected language data into game data.");
         try
         {
             LanguageDatabase.activeLanguage.InjectIntoData();
             GenLabel.ClearCache();
         }
         finally
         {
             DeepProfiler.End();
         }
     });
     LongEventHandler.ExecuteWhenFinished(delegate
     {
         StaticConstructorOnStartupUtility.CallAll();
         if (Prefs.DevMode)
         {
             StaticConstructorOnStartupUtility.ReportProbablyMissingAttributes();
         }
     });
 }
Exemple #19
0
 public static object GetStaticPropertyOnGenericType(Type genericBase, Type genericParam, string propertyName)
 {
     return(GenGeneric.PropertyOnGenericType(genericBase, genericParam, propertyName).GetGetMethod().Invoke(null, null));
 }
Exemple #20
0
        private bool SetDefFieldAtPath(Type defType, string path, object value, Type ensureFieldType, bool errorOnDefNotFound, string fileSource, bool isPlaceholder, out string normalizedPath, out string suggestedPath, out string replacedString, out IEnumerable <string> replacedStringsList)
        {
            replacedString      = null;
            replacedStringsList = null;
            string b    = path;
            string text = path;
            bool   flag = TKeySystem.TryGetNormalizedPath(path, out normalizedPath);

            if (flag)
            {
                text          = text + " (" + normalizedPath + ")";
                suggestedPath = path;
                path          = normalizedPath;
            }
            else
            {
                normalizedPath = path;
                suggestedPath  = path;
            }
            string defName = path.Split('.')[0];

            defName = BackCompatibility.BackCompatibleDefName(defType, defName, forDefInjections: true);
            if (GenDefDatabase.GetDefSilentFail(defType, defName, specialCaseForSoundDefs: false) == null)
            {
                if (errorOnDefNotFound)
                {
                    loadErrors.Add("Found no " + defType + " named " + defName + " to match " + text + " (" + fileSource + ")");
                }
                return(false);
            }
            bool          flag2 = false;
            int           num   = 0;
            List <object> list  = new List <object>();

            try
            {
                List <string> list2 = path.Split('.').ToList();
                object        obj   = GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), defType, "GetNamedSilentFail", list2[0]);
                if (obj == null)
                {
                    throw new InvalidOperationException("Def named " + list2[0] + " not found.");
                }
                num++;
                string text2;
                int    result;
                DefInjectionPathPartKind defInjectionPathPartKind;
                while (true)
                {
                    text2 = list2[num];
                    list.Add(obj);
                    result = -1;
                    if (int.TryParse(text2, out result))
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.ListIndex;
                    }
                    else if (GetFieldNamed(obj.GetType(), text2) != null)
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.Field;
                    }
                    else if (obj.GetType().GetProperty("Count") != null)
                    {
                        if (text2.Contains('-'))
                        {
                            defInjectionPathPartKind = DefInjectionPathPartKind.ListHandleWithIndex;
                            string[] array = text2.Split('-');
                            text2  = array[0];
                            result = ParseHelper.FromString <int>(array[1]);
                        }
                        else
                        {
                            defInjectionPathPartKind = DefInjectionPathPartKind.ListHandle;
                        }
                    }
                    else
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.Field;
                    }
                    if (num == list2.Count - 1)
                    {
                        break;
                    }
                    switch (defInjectionPathPartKind)
                    {
                    case DefInjectionPathPartKind.Field:
                    {
                        FieldInfo field = obj.GetType().GetField(text2, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                        if (field == null)
                        {
                            throw new InvalidOperationException("Field or TKey " + text2 + " does not exist.");
                        }
                        if (field.HasAttribute <NoTranslateAttribute>())
                        {
                            throw new InvalidOperationException("Translated untranslatable field " + field.Name + " of type " + field.FieldType + " at path " + text + ". Translating this field will break the game.");
                        }
                        if (field.HasAttribute <UnsavedAttribute>())
                        {
                            throw new InvalidOperationException("Translated untranslatable field ([Unsaved] attribute) " + field.Name + " of type " + field.FieldType + " at path " + text + ". Translating this field will break the game.");
                        }
                        if (field.HasAttribute <TranslationCanChangeCountAttribute>())
                        {
                            flag2 = true;
                        }
                        if (defInjectionPathPartKind == DefInjectionPathPartKind.Field)
                        {
                            obj = field.GetValue(obj);
                        }
                        else
                        {
                            object       value2    = field.GetValue(obj);
                            PropertyInfo property2 = value2.GetType().GetProperty("Item");
                            if (property2 == null)
                            {
                                throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
                            }
                            int num3 = (int)value2.GetType().GetProperty("Count").GetValue(value2, null);
                            if (result < 0 || result >= num3)
                            {
                                throw new InvalidOperationException("Index out of bounds (max index is " + (num3 - 1) + ")");
                            }
                            obj = property2.GetValue(value2, new object[1]
                                {
                                    result
                                });
                        }
                        break;
                    }

                    case DefInjectionPathPartKind.ListIndex:
                    case DefInjectionPathPartKind.ListHandle:
                    case DefInjectionPathPartKind.ListHandleWithIndex:
                    {
                        object       obj2     = obj;
                        PropertyInfo property = obj2.GetType().GetProperty("Item");
                        if (property == null)
                        {
                            throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
                        }
                        bool flag3;
                        if (defInjectionPathPartKind == DefInjectionPathPartKind.ListHandle || defInjectionPathPartKind == DefInjectionPathPartKind.ListHandleWithIndex)
                        {
                            result = TranslationHandleUtility.GetElementIndexByHandle(obj2, text2, result);
                            defInjectionPathPartKind = DefInjectionPathPartKind.ListIndex;
                            flag3 = true;
                        }
                        else
                        {
                            flag3 = false;
                        }
                        int num2 = (int)obj2.GetType().GetProperty("Count").GetValue(obj2, null);
                        if (result < 0 || result >= num2)
                        {
                            throw new InvalidOperationException("Index out of bounds (max index is " + (num2 - 1) + ")");
                        }
                        obj = property.GetValue(obj2, new object[1]
                            {
                                result
                            });
                        if (flag3)
                        {
                            string[] array2 = normalizedPath.Split('.');
                            array2[num]    = result.ToString();
                            normalizedPath = string.Join(".", array2);
                        }
                        else if (!flag)
                        {
                            string bestHandleWithIndexForListElement = TranslationHandleUtility.GetBestHandleWithIndexForListElement(obj2, obj);
                            if (!bestHandleWithIndexForListElement.NullOrEmpty())
                            {
                                string[] array3 = suggestedPath.Split('.');
                                array3[num]   = bestHandleWithIndexForListElement;
                                suggestedPath = string.Join(".", array3);
                            }
                        }
                        break;
                    }

                    default:
                        loadErrors.Add("Can't enter node " + text2 + " at path " + text + ", element kind is " + defInjectionPathPartKind + ". (" + fileSource + ")");
                        break;
                    }
                    num++;
                }
                bool flag4 = false;
                foreach (KeyValuePair <string, DefInjection> injection in injections)
                {
                    if (!(injection.Key == b) && injection.Value.injected && injection.Value.normalizedPath == normalizedPath)
                    {
                        string text3 = "Duplicate def-injected translation key. Both " + injection.Value.path + " and " + text + " refer to the same field (" + suggestedPath + ")";
                        if (injection.Value.path != injection.Value.nonBackCompatiblePath)
                        {
                            text3 = text3 + " (" + injection.Value.nonBackCompatiblePath + " was auto-renamed to " + injection.Value.path + ")";
                        }
                        text3 = text3 + " (" + injection.Value.fileSource + ")";
                        loadErrors.Add(text3);
                        flag4 = true;
                        break;
                    }
                }
                bool result2 = false;
                if (!flag4)
                {
                    switch (defInjectionPathPartKind)
                    {
                    case DefInjectionPathPartKind.Field:
                    {
                        FieldInfo fieldNamed = GetFieldNamed(obj.GetType(), text2);
                        if (fieldNamed == null)
                        {
                            throw new InvalidOperationException("Field " + text2 + " does not exist in type " + obj.GetType() + ".");
                        }
                        if (fieldNamed.HasAttribute <NoTranslateAttribute>())
                        {
                            loadErrors.Add("Translated untranslatable field " + fieldNamed.Name + " of type " + fieldNamed.FieldType + " at path " + text + ". Translating this field will break the game. (" + fileSource + ")");
                        }
                        else if (fieldNamed.HasAttribute <UnsavedAttribute>())
                        {
                            loadErrors.Add("Translated untranslatable field (UnsavedAttribute) " + fieldNamed.Name + " of type " + fieldNamed.FieldType + " at path " + text + ". Translating this field will break the game. (" + fileSource + ")");
                        }
                        else if (!isPlaceholder && fieldNamed.FieldType != ensureFieldType)
                        {
                            loadErrors.Add("Translated non-" + ensureFieldType + " field " + fieldNamed.Name + " of type " + fieldNamed.FieldType + " at path " + text + ". Expected " + ensureFieldType + ". (" + fileSource + ")");
                        }
                        else if (!isPlaceholder && ensureFieldType != typeof(string) && !fieldNamed.HasAttribute <TranslationCanChangeCountAttribute>())
                        {
                            loadErrors.Add("Tried to translate field " + fieldNamed.Name + " of type " + fieldNamed.FieldType + " at path " + text + ", but this field doesn't have [TranslationCanChangeCount] attribute so it doesn't allow this type of translation. (" + fileSource + ")");
                        }
                        else if (!isPlaceholder)
                        {
                            if (ensureFieldType == typeof(string))
                            {
                                replacedString = (string)fieldNamed.GetValue(obj);
                            }
                            else
                            {
                                replacedStringsList = (fieldNamed.GetValue(obj) as IEnumerable <string>);
                            }
                            fieldNamed.SetValue(obj, value);
                            result2 = true;
                        }
                        break;
                    }

                    case DefInjectionPathPartKind.ListIndex:
                    case DefInjectionPathPartKind.ListHandle:
                    case DefInjectionPathPartKind.ListHandleWithIndex:
                    {
                        object obj3 = obj;
                        if (obj3 == null)
                        {
                            throw new InvalidOperationException("Tried to use index on null list at " + text);
                        }
                        Type         type      = obj3.GetType();
                        PropertyInfo property3 = type.GetProperty("Count");
                        if (property3 == null)
                        {
                            throw new InvalidOperationException("Tried to use index on non-list (missing 'Count' property).");
                        }
                        if (defInjectionPathPartKind == DefInjectionPathPartKind.ListHandle || defInjectionPathPartKind == DefInjectionPathPartKind.ListHandleWithIndex)
                        {
                            result = TranslationHandleUtility.GetElementIndexByHandle(obj3, text2, result);
                            defInjectionPathPartKind = DefInjectionPathPartKind.ListIndex;
                        }
                        int num4 = (int)property3.GetValue(obj3, null);
                        if (result >= num4)
                        {
                            throw new InvalidOperationException("Trying to translate " + defType + "." + text + " at index " + result + " but the list only has " + num4 + " entries (so max index is " + (num4 - 1).ToString() + ").");
                        }
                        PropertyInfo property4 = type.GetProperty("Item");
                        if (property4 == null)
                        {
                            throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
                        }
                        Type propertyType = property4.PropertyType;
                        if (!isPlaceholder && propertyType != ensureFieldType)
                        {
                            loadErrors.Add("Translated non-" + ensureFieldType + " list item of type " + propertyType + " at path " + text + ". Expected " + ensureFieldType + ". (" + fileSource + ")");
                        }
                        else if (!isPlaceholder && ensureFieldType != typeof(string) && !flag2)
                        {
                            loadErrors.Add("Tried to translate field of type " + propertyType + " at path " + text + ", but this field doesn't have [TranslationCanChangeCount] attribute so it doesn't allow this type of translation. (" + fileSource + ")");
                        }
                        else if (result < 0 || result >= (int)type.GetProperty("Count").GetValue(obj3, null))
                        {
                            loadErrors.Add("Index out of bounds (max index is " + ((int)type.GetProperty("Count").GetValue(obj3, null) - 1) + ")");
                        }
                        else if (!isPlaceholder)
                        {
                            replacedString = (string)property4.GetValue(obj3, new object[1]
                                {
                                    result
                                });
                            property4.SetValue(obj3, value, new object[1]
                                {
                                    result
                                });
                            result2 = true;
                        }
                        break;
                    }

                    default:
                        loadErrors.Add("Translated " + text2 + " at path " + text + " but it's not a field, it's " + defInjectionPathPartKind + ". (" + fileSource + ")");
                        break;
                    }
                }
                for (int num5 = list.Count - 1; num5 > 0; num5--)
                {
                    if (list[num5].GetType().IsValueType&& !list[num5].GetType().IsPrimitive)
                    {
                        FieldInfo fieldNamed2 = GetFieldNamed(list[num5 - 1].GetType(), list2[num5]);
                        if (fieldNamed2 != null)
                        {
                            fieldNamed2.SetValue(list[num5 - 1], list[num5]);
                        }
                    }
                }
                string tKeyPath;
                if (flag)
                {
                    path = suggestedPath;
                }
                else if (TKeySystem.TrySuggestTKeyPath(path, out tKeyPath))
                {
                    suggestedPath = tKeyPath;
                }
                if (path != suggestedPath)
                {
                    IList <string> list3 = value as IList <string>;
                    string         text4 = (list3 == null) ? value.ToString() : list3.ToStringSafeEnumerable();
                    loadSyntaxSuggestions.Add("Consider using " + suggestedPath + " instead of " + text + " for translation '" + text4 + "' (" + fileSource + ")");
                }
                return(result2);
            }
            catch (Exception ex)
            {
                string text5 = "Couldn't inject " + text + " into " + defType + " (" + fileSource + "): " + ex.Message;
                if (ex.InnerException != null)
                {
                    text5 = text5 + " -> " + ex.InnerException.Message;
                }
                loadErrors.Add(text5);
                return(false);
            }
        }
Exemple #21
0
        private void SetDefFieldAtPath(Type defType, string path, string value)
        {
            path = BackCompatibility.BackCompatibleModifiedTranslationPath(defType, path);
            try
            {
                List <string> list = path.Split('.').ToList();
                object        obj  = GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), defType, "GetNamedSilentFail", list[0]);
                if (obj == null)
                {
                    throw new InvalidOperationException("Def named " + list[0] + " not found.");
                }
                list.RemoveAt(0);
                while (true)
                {
                    DefInjectionPathPartKind defInjectionPathPartKind = DefInjectionPathPartKind.Field;
                    string text = list[0];
                    int    num  = -1;
                    if (text.Contains('['))
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.FieldWithListIndex;
                        string[] array = text.Split('[');
                        string   text2 = array[1];
                        text2 = text2.Substring(0, text2.Length - 1);
                        num   = (int)ParseHelper.FromString(text2, typeof(int));
                        text  = array[0];
                    }
                    else if (int.TryParse(text, out num))
                    {
                        defInjectionPathPartKind = DefInjectionPathPartKind.ListIndex;
                    }
                    if (list.Count == 1)
                    {
                        object obj2;
                        switch (defInjectionPathPartKind)
                        {
                        case DefInjectionPathPartKind.Field:
                        {
                            FieldInfo fieldNamed = this.GetFieldNamed(obj.GetType(), text);
                            if (fieldNamed == null)
                            {
                                throw new InvalidOperationException("Field " + text + " does not exist in type " + obj.GetType() + ".");
                            }
                            if (fieldNamed.HasAttribute <NoTranslateAttribute>())
                            {
                                Log.Error("Translated untranslateable field " + fieldNamed.Name + " of type " + fieldNamed.FieldType + " at path " + path + ". Translating this field will break the game.");
                            }
                            else if (fieldNamed.FieldType != typeof(string))
                            {
                                Log.Error("Translated non-string field " + fieldNamed.Name + " of type " + fieldNamed.FieldType + " at path " + path + ". Only string fields should be translated.");
                            }
                            else
                            {
                                fieldNamed.SetValue(obj, value);
                            }
                            return;
                        }

                        case DefInjectionPathPartKind.FieldWithListIndex:
                        {
                            FieldInfo field = obj.GetType().GetField(text, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                            if (field == null)
                            {
                                throw new InvalidOperationException("Field " + text + " does not exist.");
                            }
                            obj2 = field.GetValue(obj);
                            break;
                        }

                        default:
                            obj2 = obj;
                            break;
                        }
                        Type         type     = obj2.GetType();
                        PropertyInfo property = type.GetProperty("Count");
                        if (property == null)
                        {
                            throw new InvalidOperationException("Tried to use index on non-list (missing 'Count' property).");
                        }
                        int num2 = (int)property.GetValue(obj2, null);
                        if (num >= num2)
                        {
                            throw new InvalidOperationException("Trying to translate " + defType + "." + path + " at index " + num + " but the original list only has " + num2 + " entries (so the max index is " + (num2 - 1).ToString() + ").");
                        }
                        PropertyInfo property2 = type.GetProperty("Item");
                        if (property2 == null)
                        {
                            throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
                        }
                        property2.SetValue(obj2, value, new object[1]
                        {
                            num
                        });
                        return;
                    }
                    if (defInjectionPathPartKind == DefInjectionPathPartKind.ListIndex)
                    {
                        PropertyInfo property3 = obj.GetType().GetProperty("Item");
                        if (property3 == null)
                        {
                            throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
                        }
                        obj = property3.GetValue(obj, new object[1]
                        {
                            num
                        });
                        goto IL_0421;
                    }
                    FieldInfo field2 = obj.GetType().GetField(text, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                    if (field2 == null)
                    {
                        throw new InvalidOperationException("Field " + text + " does not exist.");
                    }
                    if (defInjectionPathPartKind == DefInjectionPathPartKind.Field)
                    {
                        obj = field2.GetValue(obj);
                        goto IL_0421;
                    }
                    object       value2    = field2.GetValue(obj);
                    PropertyInfo property4 = value2.GetType().GetProperty("Item");
                    if (property4 != null)
                    {
                        obj = property4.GetValue(value2, new object[1]
                        {
                            num
                        });
                        goto IL_0421;
                    }
                    break;
IL_0421:
                    list.RemoveAt(0);
                }
                throw new InvalidOperationException("Tried to use index on non-list (missing 'Item' property).");
            }
            catch (Exception ex)
            {
                Log.Warning("Def-linked translation error: Exception getting field at path " + path + " in " + defType + ": " + ex.ToString());
            }
        }
Exemple #22
0
        private static void DoPlayLoad()
        {
            DeepProfiler.Start("GraphicDatabase.Clear()");
            try
            {
                GraphicDatabase.Clear();
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Load all active mods.");
            try
            {
                LoadedModManager.LoadAllActiveMods();
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Load language metadata.");
            try
            {
                LanguageDatabase.InitAllMetadata();
            }
            finally
            {
                DeepProfiler.End();
            }
            LongEventHandler.SetCurrentEventText("LoadingDefs".Translate());
            DeepProfiler.Start("Copy all Defs from mods to global databases.");
            try
            {
                foreach (Type item in typeof(Def).AllSubclasses())
                {
                    GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), item, "AddAllInMods");
                }
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Resolve cross-references between non-implied Defs.");
            try
            {
                DirectXmlCrossRefLoader.ResolveAllWantedCrossReferences(FailMode.Silent);
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Rebind defs (early).");
            try
            {
                DefOfHelper.RebindAllDefOfs(earlyTryMode: true);
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("TKeySystem.BuildMappings()");
            try
            {
                TKeySystem.BuildMappings();
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Inject selected language data into game data (early pass).");
            try
            {
                LanguageDatabase.activeLanguage.InjectIntoData_BeforeImpliedDefs();
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Generate implied Defs (pre-resolve).");
            try
            {
                DefGenerator.GenerateImpliedDefs_PreResolve();
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Resolve cross-references between Defs made by the implied defs.");
            try
            {
                DirectXmlCrossRefLoader.ResolveAllWantedCrossReferences(FailMode.LogErrors);
            }
            finally
            {
                DirectXmlCrossRefLoader.Clear();
                DeepProfiler.End();
            }
            DeepProfiler.Start("Rebind DefOfs (final).");
            try
            {
                DefOfHelper.RebindAllDefOfs(earlyTryMode: false);
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Other def binding, resetting and global operations (pre-resolve).");
            try
            {
                PlayerKnowledgeDatabase.ReloadAndRebind();
                LessonAutoActivator.Reset();
                CostListCalculator.Reset();
                Pawn.ResetStaticData();
                PawnApparelGenerator.Reset();
                RestUtility.Reset();
                ThoughtUtility.Reset();
                ThinkTreeKeyAssigner.Reset();
                ThingCategoryNodeDatabase.FinalizeInit();
                TrainableUtility.Reset();
                HaulAIUtility.Reset();
                GenConstruct.Reset();
                MedicalCareUtility.Reset();
                InspectPaneUtility.Reset();
                GraphicDatabaseHeadRecords.Reset();
                DateReadout.Reset();
                ResearchProjectDef.GenerateNonOverlappingCoordinates();
                BaseGen.Reset();
                ResourceCounter.ResetDefs();
                ApparelProperties.ResetStaticData();
                WildPlantSpawner.ResetStaticData();
                PawnGenerator.Reset();
                TunnelHiveSpawner.ResetStaticData();
                Hive.ResetStaticData();
                ExpectationsUtility.Reset();
                WealthWatcher.ResetStaticData();
                SkillUI.Reset();
                QuestNode_GetThingPlayerCanProduce.ResetStaticData();
                Pawn_PsychicEntropyTracker.ResetStaticData();
                ColoredText.ResetStaticData();
                QuestNode_GetRandomNegativeGameCondition.ResetStaticData();
                RoyalTitleUtility.ResetStaticData();
                RewardsGenerator.ResetStaticData();
                WorkGiver_FillFermentingBarrel.ResetStaticData();
                WorkGiver_DoBill.ResetStaticData();
                WorkGiver_InteractAnimal.ResetStaticData();
                WorkGiver_Warden_DoExecution.ResetStaticData();
                WorkGiver_GrowerSow.ResetStaticData();
                WorkGiver_Miner.ResetStaticData();
                WorkGiver_FixBrokenDownBuilding.ResetStaticData();
                WorkGiver_ConstructDeliverResources.ResetStaticData();
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Resolve references.");
            try
            {
                DeepProfiler.Start("ThingCategoryDef resolver");
                try
                {
                    DefDatabase <ThingCategoryDef> .ResolveAllReferences();
                }
                finally
                {
                    DeepProfiler.End();
                }
                DeepProfiler.Start("RecipeDef resolver");
                try
                {
                    DeepProfiler.enabled = false;
                    DefDatabase <RecipeDef> .ResolveAllReferences(onlyExactlyMyType : true, parallel : true);

                    DeepProfiler.enabled = true;
                }
                finally
                {
                    DeepProfiler.End();
                }
                DeepProfiler.Start("Static resolver calls");
                try
                {
                    foreach (Type item2 in typeof(Def).AllSubclasses())
                    {
                        if (!(item2 == typeof(ThingDef)) && !(item2 == typeof(ThingCategoryDef)) && !(item2 == typeof(RecipeDef)))
                        {
                            GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), item2, "ResolveAllReferences", true, false);
                        }
                    }
                }
                finally
                {
                    DeepProfiler.End();
                }
                DeepProfiler.Start("ThingDef resolver");
                try
                {
                    DefDatabase <ThingDef> .ResolveAllReferences();
                }
                finally
                {
                    DeepProfiler.End();
                }
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Generate implied Defs (post-resolve).");
            try
            {
                DefGenerator.GenerateImpliedDefs_PostResolve();
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Other def binding, resetting and global operations (post-resolve).");
            try
            {
                PawnWeaponGenerator.Reset();
                BuildingProperties.FinalizeInit();
                ThingSetMakerUtility.Reset();
            }
            finally
            {
                DeepProfiler.End();
            }
            if (Prefs.DevMode)
            {
                DeepProfiler.Start("Error check all defs.");
                try
                {
                    foreach (Type item3 in typeof(Def).AllSubclasses())
                    {
                        GenGeneric.InvokeStaticMethodOnGenericType(typeof(DefDatabase <>), item3, "ErrorCheckAllDefs");
                    }
                }
                finally
                {
                    DeepProfiler.End();
                }
            }
            LongEventHandler.SetCurrentEventText("Initializing".Translate());
            DeepProfiler.Start("Load keyboard preferences.");
            try
            {
                KeyPrefs.Init();
            }
            finally
            {
                DeepProfiler.End();
            }
            DeepProfiler.Start("Short hash giving.");
            try
            {
                ShortHashGiver.GiveAllShortHashes();
            }
            finally
            {
                DeepProfiler.End();
            }
            LongEventHandler.ExecuteWhenFinished(delegate
            {
                DeepProfiler.Start("Load backstories.");
                try
                {
                    BackstoryDatabase.ReloadAllBackstories();
                }
                finally
                {
                    DeepProfiler.End();
                }
            });
            LongEventHandler.ExecuteWhenFinished(delegate
            {
                DeepProfiler.Start("Inject selected language data into game data.");
                try
                {
                    LanguageDatabase.activeLanguage.InjectIntoData_AfterImpliedDefs();
                    GenLabel.ClearCache();
                }
                finally
                {
                    DeepProfiler.End();
                }
            });
            LongEventHandler.ExecuteWhenFinished(delegate
            {
                DeepProfiler.Start("Static constructor calls");
                try
                {
                    StaticConstructorOnStartupUtility.CallAll();
                    if (Prefs.DevMode)
                    {
                        StaticConstructorOnStartupUtility.ReportProbablyMissingAttributes();
                    }
                }
                finally
                {
                    DeepProfiler.End();
                }
                DeepProfiler.Start("Garbage Collection");
                try
                {
                    AbstractFilesystem.ClearAllCache();
                    GC.Collect(int.MaxValue, GCCollectionMode.Forced);
                }
                finally
                {
                    DeepProfiler.End();
                }
            });
        }