Ejemplo n.º 1
0
    /// <summary>Loads the tiles from <paramref name="Resourcepath"/>.</summary>
    /// <param name="Resourcepath">A path relative to the data folder of SuperTux.</param>
    public Tileset(string Resourcepath)
    {
        baseDir = ResourceManager.Instance.GetDirectoryName(Resourcepath);
        List TilesL = Util.Load(Resourcepath, "supertux-tiles");

        Properties TilesP = new Properties(TilesL);
        foreach(List list in TilesP.GetList("tile")) {
            try {
                Tile tile = new Tile();
                ParseTile(tile, list);
                while(tiles.Count <= tile.Id)
                    tiles.Add(null);
                tiles[tile.Id] = tile;
            } catch(Exception e) {
                Console.WriteLine("Couldn't parse a Tile: " + e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }

        foreach(List list in TilesP.GetList("tiles")) {
            try {
                ParseTiles(list);
            } catch(Exception e) {
                Console.WriteLine("Couldn't parse a tiles: " + e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }

        // construct a tilegroup with all tiles
        Tilegroup allGroup = new Tilegroup();
            allGroup.Name = "All";
            foreach(Tile tile in tiles) {
                if(tile != null)
                    allGroup.Tiles.Add(tile.Id);
            }
            tilegroups.Add(allGroup.Name, allGroup);

        LispSerializer serializer = new LispSerializer(typeof(Tilegroup));
        foreach(List list in TilesP.GetList("tilegroup")) {
            try {
                Tilegroup group = (Tilegroup) serializer.Read(list);
                for(int i = 0; i < group.Tiles.Count; ) {
                    if(!IsValid(group.Tiles[i])) {
                        Console.WriteLine("Tilegroup " + group.Name + " contains invalid TileID " + group.Tiles[i]);
                        group.Tiles.RemoveAt(i);
                        continue;
                    }
                    ++i;
                }
                tilegroups.Add(group.Name, group);
            } catch(Exception e) {
                Console.WriteLine("Couldn't parse tilegroup: " + e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
    }
Ejemplo n.º 2
0
    public void CustomLispRead(Properties props)
    {
        foreach(Type type in this.GetType().Assembly.GetTypes()) {
            SupertuxObjectAttribute objectAttribute
            = (SupertuxObjectAttribute) Attribute.GetCustomAttribute(type, typeof(SupertuxObjectAttribute));
            if(objectAttribute == null)
                continue;

            LispSerializer serializer = new LispSerializer(type);
            foreach(List list in props.GetList(objectAttribute.Name)) {
                IGameObject Object = (IGameObject) serializer.Read(list);
                GameObjects.Add(Object);
            }
        }
    }
Ejemplo n.º 3
0
    public void CustomLispWrite(Writer Writer)
    {
        Type[] types = this.GetType().Assembly.GetTypes();
        Array.Sort(types, CompareTypeNames);
        foreach(Type type in types) {
            SupertuxObjectAttribute objectAttribute = (SupertuxObjectAttribute)
                Attribute.GetCustomAttribute(type, typeof(SupertuxObjectAttribute));
            if(objectAttribute == null)
                continue;

            string name = objectAttribute.Name;
            LispSerializer serializer = new LispSerializer(type);
            foreach(object Object in GetObjects(type)) {
                serializer.Write(Writer, name, Object);
            }
        }
    }
Ejemplo n.º 4
0
        public object Read(List list)
        {
            object result = CreateObject(type);

            Properties props = new Properties(list);

            // iterate over all fields and properties
            foreach (FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type))
            {
                LispChildAttribute ChildAttrib = (LispChildAttribute)
                                                 field.GetCustomAttribute(typeof(LispChildAttribute));
                if (ChildAttrib != null)
                {
                    string Name = ChildAttrib.Name;
                    if (field.Type == typeof(int))
                    {
                        int val = 0;
                        if (!props.Get(Name, ref val))
                        {
                            if (!ChildAttrib.Optional)
                            {
                                LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
                            }
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(string))
                    {
                        string val = null;
                        if (!props.Get(Name, ref val))
                        {
                            if (!ChildAttrib.Optional)
                            {
                                LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
                            }
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(float))
                    {
                        float val = 0;
                        if (!props.Get(Name, ref val))
                        {
                            if (!ChildAttrib.Optional)
                            {
                                LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
                            }
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type.IsEnum)
                    {
                        Enum val = null;
                        if (!props.Get(Name, ref val, field.Type))
                        {
                            if (!ChildAttrib.Optional)
                            {
                                LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
                            }
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(bool))
                    {
                        bool val = false;
                        if (!props.Get(Name, ref val))
                        {
                            if (!ChildAttrib.Optional)
                            {
                                LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
                            }
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(List <int>))
                    {
                        List <int> val = new List <int>();
                        if (!props.GetIntList(Name, val))
                        {
                            if (!ChildAttrib.Optional)
                            {
                                LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
                            }
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else
                    {
                        ILispSerializer serializer = LispSerializer.GetSerializer(field.Type);
                        if (serializer == null)
                        {
                            throw new LispException("Type " + field.Type + " not supported for serialization");
                        }

                        List val = null;
                        if (!props.Get(Name, ref val))
                        {
                            if (!ChildAttrib.Optional)
                            {
                                LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
                            }
                        }
                        else
                        {
                            object oval = serializer.Read(val);
                            field.SetValue(result, oval);
                        }
                    }
                }

                foreach (LispChildsAttribute childsAttrib in
                         field.GetCustomAttributes(typeof(LispChildsAttribute)))
                {
                    object     objectList = field.GetValue(result);
                    Type       ListType   = field.Type;
                    MethodInfo AddMethod  = ListType.GetMethod(
                        "Add", new Type[] { childsAttrib.ListType }, null);
                    if (AddMethod == null)
                    {
                        throw new LispException("No Add method found for field " + field.Name);
                    }

                    ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type);
                    if (serializer == null)
                    {
                        serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type);
                    }

                    foreach (List childList in props.GetList(childsAttrib.Name))
                    {
                        object child = serializer.Read(childList);
                        AddMethod.Invoke(objectList, new object[] { child });
                    }
                }
            }

            if (result is ICustomLispSerializer)
            {
                ICustomLispSerializer custom = (ICustomLispSerializer)result;
                custom.CustomLispRead(props);
                custom.FinishRead();
            }

            return(result);
        }
Ejemplo n.º 5
0
        public void Write(Writer writer, string name, object Object)
        {
            writer.StartList(name);

            foreach (FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type))
            {
                LispChildAttribute ChildAttrib = (LispChildAttribute)
                                                 field.GetCustomAttribute(typeof(LispChildAttribute));
                if (ChildAttrib != null)
                {
                    object Value = field.GetValue(Object);
                    if (Value != null)
                    {
                        if (ChildAttrib.Translatable)
                        {
                            if (!ChildAttrib.Optional || !Value.Equals(ChildAttrib.Default))
                            {
                                writer.WriteTranslatable(ChildAttrib.Name, Value.ToString());
                            }
                        }
                        else
                        {
                            Type childType = field.Type;

                            ILispSerializer serializer = LispSerializer.GetSerializer(childType);
                            if (serializer != null)
                            {
                                serializer.Write(writer, ChildAttrib.Name, Value);
                            }
                            else
                            {
                                if (ChildAttrib.Optional && childType.IsEnum)
                                {
                                    // If it is an enum we need to convert ChildAttrib.Default
                                    // to an enum as ChildAttrib.Default is an Int32 by some (unknown) reason.
                                    Enum Defval = (Enum)Enum.ToObject(childType, ChildAttrib.Default);
                                    if (!Value.Equals(Defval))
                                    {
                                        writer.Write(ChildAttrib.Name, Value);
                                    }
                                }
                                else if (!ChildAttrib.Optional || !Value.Equals(ChildAttrib.Default))
                                {
                                    writer.Write(ChildAttrib.Name, Value);
                                }
                            }
                        }
                    }
                    else
                    {
                        LogManager.Log(LogLevel.DebugWarning, "Field '" + field.Name + "' is null");
                    }
                }

                foreach (LispChildsAttribute childsAttrib in
                         field.GetCustomAttributes(typeof(LispChildsAttribute)))
                {
                    if (childsAttrib != null)
                    {
                        object list = field.GetValue(Object);
                        if (!(list is IEnumerable))
                        {
                            throw new LispException("Field '" + field.Name + "' is not IEnumerable");
                        }

                        ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type);
                        if (serializer == null)
                        {
                            serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type);
                        }

                        IEnumerable enumerable = (IEnumerable)list;
                        foreach (object childObject in enumerable)
                        {
                            if (childsAttrib.Type.IsAssignableFrom(childObject.GetType()))
                            {
                                serializer.Write(writer, childsAttrib.Name, childObject);
                            }
                        }
                    }
                }
            }

            if (Object is ICustomLispSerializer)
            {
                ICustomLispSerializer custom = (ICustomLispSerializer)Object;
                custom.CustomLispWrite(writer);
            }

            writer.EndList(name);
        }
        public object Read(List list)
        {
            object result = CreateObject(type);

            Properties props = new Properties(list);

            // iterate over all fields and properties
            foreach (FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type))
            {
                LispChildAttribute ChildAttrib = (LispChildAttribute)
                                                 field.GetCustomAttribute(typeof(LispChildAttribute));
                if (ChildAttrib != null)
                {
                    string Name    = ChildAttrib.Name;
                    string altName = ChildAttrib.AlternativeName;
                    if (field.Type == typeof(int))
                    {
                        int val = 0;
                        if (!(props.Get(Name, ref val) || (altName != null && props.Get(altName, ref val))))
                        {
                            CheckRequired(ChildAttrib);
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(string))
                    {
                        string val = null;
                        if (!props.Get(Name, ref val))
                        {
                            CheckRequired(ChildAttrib);
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(float))
                    {
                        float val = 0;
                        if (!(props.Get(Name, ref val) || (altName != null && props.Get(altName, ref val))))
                        {
                            CheckRequired(ChildAttrib);
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type.IsEnum)
                    {
                        Enum val = null;
                        if (!props.Get(Name, ref val, field.Type))
                        {
                            CheckRequired(ChildAttrib);
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(bool))
                    {
                        bool val = false;
                        if (!props.Get(Name, ref val))
                        {
                            CheckRequired(ChildAttrib);
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(List <int>))
                    {
                        List <int> val = new List <int>();
                        if (!props.GetIntList(Name, val))
                        {
                            CheckRequired(ChildAttrib);
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(List <string>))
                    {
                        List <string> val = new List <string>();
                        if (!props.GetStringList(Name, val))
                        {
                            CheckRequired(ChildAttrib);
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else if (field.Type == typeof(Lisp.List))
                    {
                        Lisp.List val;
                        if (!props.GetLispList(Name, out val))
                        {
                            CheckRequired(ChildAttrib);
                        }
                        else
                        {
                            field.SetValue(result, val);
                        }
                    }
                    else
                    {
                        ILispSerializer serializer = LispSerializer.GetSerializer(field.Type);
                        if (serializer == null)
                        {
                            throw new LispException("Type " + field.Type + " not supported for serialization");
                        }

                        List val = null;
                        if (!props.Get(Name, ref val))
                        {
                            CheckRequired(ChildAttrib);
                        }
                        else
                        {
                            try {
                                object oval = serializer.Read(val);
                                field.SetValue(result, oval);
                            } catch (LispException) {
                                if (Name == "ambient-light" || Name == "music")
                                {
                                    // ignore errors of these for backward compatibilty
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                    }
                }

                foreach (LispChildsAttribute childsAttrib in
                         field.GetCustomAttributes(typeof(LispChildsAttribute)))
                {
                    object     objectList = field.GetValue(result);
                    Type       ListType   = field.Type;
                    MethodInfo AddMethod  = ListType.GetMethod(
                        "Add", new Type[] { childsAttrib.ListType }, null);
                    if (AddMethod == null)
                    {
                        throw new LispException("No Add method found for field " + field.Name);
                    }

                    ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type);
                    if (serializer == null)
                    {
                        serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type);
                    }

                    foreach (List childList in props.GetList(childsAttrib.Name))
                    {
                        object child = serializer.Read(childList);
                        AddMethod.Invoke(objectList, new object[] { child });
                    }
                }
            }

            if (result is ICustomLispSerializer)
            {
                ICustomLispSerializer custom = (ICustomLispSerializer)result;
                custom.CustomLispRead(props);
                custom.FinishRead();
            }

            return(result);
        }
Ejemplo n.º 7
0
    public void CustomLispWrite(Writer Writer)
    {
        System.Collections.Generic.List<Type> types = new System.Collections.Generic.List<Type>(this.GetType().Assembly.GetTypes());
        types.Sort(CompareTypeNames);
        foreach(Type type in types) {
            SupertuxObjectAttribute objectAttribute = (SupertuxObjectAttribute)
                Attribute.GetCustomAttribute(type, typeof(SupertuxObjectAttribute));
            if(objectAttribute == null)
                continue;

            string name = objectAttribute.Name;
            LispSerializer serializer = new LispSerializer(type);
            foreach(object Object in GetObjects(type)) {
                serializer.Write(Writer, name, Object);
            }
        }
    }