示例#1
0
    public void CustomLispRead(Properties Props)
    {
        int Width = 0;
        int Height = 0;
        Props.Get("width", ref Width);
        Props.Get("height", ref Height);
        if(Width == 0 || Height == 0) throw new LispException("Width or Height of TileBlock invalid");

        List<int> Tiles = new List<int>();
        Props.GetIntList("tiles", Tiles);
        if(Tiles.Count != (int) (Width * Height)) throw new LispException("TileCount != Width*Height: " + Tiles.Count + " != " + (int)Width + "*" + (int)Height);

        field.Assign(Tiles, Width, Height);
    }
示例#2
0
    /// <summary>Parse a list of tiles (from a tiles block in the tileset file)</summary>
    /// <exception cref="System.Exception">
    /// Thrown when width and/or height of ids/attributes of a tiles block is wrong.
    /// </exception>
    private void ParseTiles(List list)
    {
        Properties props = new Properties(list);

        int width = 0;
        int height = 0;
        string image = "";
        props.Get("width", ref width);
        props.Get("height", ref height);
        props.Get("image", ref image);
        if(width == 0 || height == 0)
            throw new ApplicationException("Width and Height of tiles block must be > 0");

        List<int> ids = new List<int> ();
        List<uint> attributes = new List<uint> ();
        props.GetIntList("ids", ids);
        props.GetUIntList("attributes", attributes);
        if(ids.Count != width * height)
            throw new ApplicationException("Must have width*height ids in tiles block");
        if(attributes.Count != width * height)
            throw new ApplicationException("Must have width*height attributes in tiles block");

        int id = 0;
        for(int y = 0; y < height; ++y) {
            for(int x = 0; x < width; ++x) {
                Tile tile = new Tile();

                Tile.ImageResource res = new Tile.ImageResource();
                res.Filename = image;
                res.x = x * TILE_WIDTH;
                res.y = y * TILE_HEIGHT;
                res.w = TILE_WIDTH;
                res.h = TILE_HEIGHT;
                tile.Images = new List<Tile.ImageResource>();
                tile.Images.Add(res);
                tile.Id = ids[id];
                tile.Attributes = (Tile.Attribute) attributes[id];

                while(tiles.Count <= tile.Id)
                    tiles.Add(null);
                tiles[tile.Id] = tile;

                id++;
            }
        }
    }
        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)
                                Console.WriteLine("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)
                                Console.WriteLine("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)
                                Console.WriteLine("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)
                                Console.WriteLine("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)
                                Console.WriteLine("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)
                                Console.WriteLine("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)
                                Console.WriteLine("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;
        }
示例#4
0
    /// <summary>Parse a list of tiles (from a tiles block in the tileset file)</summary>
    /// <exception cref="System.Exception">
    /// Thrown when width and/or height of ids/attributes of a tiles block is wrong.
    /// </exception>
    private void ParseTiles(List list)
    {
        Properties props = new Properties(list);

        int width = 0;
        int height = 0;
        string image = String.Empty;
        string editor_images = String.Empty;
        props.Get("width", ref width);
        props.Get("height", ref height);
        props.Get("image", ref image);
        if (image.Equals (String.Empty))
            props.Get("images", ref image);
        props.Get("editor-images", ref editor_images);
        if(width == 0 || height == 0)
            throw new ApplicationException("Width and Height of tiles block must be > 0");

        List<int> ids = new List<int> ();
        List<uint> attributes = new List<uint> ();
        List<uint> datas = new List<uint> ();
        props.GetIntList("ids", ids);
        props.GetUIntList("attributes", attributes);
        props.GetUIntList("datas", datas);
        if(ids.Count != width * height)
            throw new ApplicationException("Must have width*height ids in tiles block");
        if((attributes.Count != width * height) && attributes.Count > 0)	//missing atributes == all-are-0-attributes
            throw new ApplicationException("Must have width*height attributes in tiles block");
        if((datas.Count != width * height) && datas.Count > 0)	//missing DATAs == all-are-0-DATAs
            throw new ApplicationException("Must have width*height DATAs in tiles block");

        int id = 0;
        for(int y = 0; y < height; ++y) {
            for(int x = 0; x < width; ++x) {
                if (ids[id] != 0) {
                    Tile tile = new Tile();

                    if (!image.Equals (String.Empty)) {
                        Tile.ImageResource res = new Tile.ImageResource();
                        res.Filename = image;
                        res.x = x * TILE_WIDTH;
                        res.y = y * TILE_HEIGHT;
                        res.w = TILE_WIDTH;
                        res.h = TILE_HEIGHT;
                        tile.Images = new List<Tile.ImageResource>();
                        tile.Images.Add(res);
                    }
                    if (!editor_images.Equals (String.Empty)) {
                        Tile.ImageResource eres = new Tile.ImageResource();
                        eres.Filename = editor_images;
                        eres.x = x * TILE_WIDTH;
                        eres.y = y * TILE_HEIGHT;
                        eres.w = TILE_WIDTH;
                        eres.h = TILE_HEIGHT;
                        tile.EditorImages = new List<Tile.ImageResource>();
                        tile.EditorImages.Add(eres);
                    }
                    tile.Id = ids[id];
                    tile.Attributes = (attributes.Count > 0)?(Tile.Attribute) attributes[id]:0;	//missing atributes == all-are-0-attributes
                    tile.Data = (datas.Count > 0)?(int) datas[id]:0;	//missing DATAs == all-are-0-DATAs

                    while(tiles.Count <= tile.Id)
                        tiles.Add(null);

                    tiles[tile.Id] = tile;
                }

                id++;
            }
        }
    }