Ejemplo n.º 1
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());
            }
        }
Ejemplo n.º 2
0
        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()
                }));
            }
        }