public bool ReadFromBuffer(GR.Memory.ByteBuffer DataIn)
        {
            if (DataIn == null)
            {
                return(false);
            }

            ValueTable.Values.Clear();

            GR.IO.MemoryReader memIn = DataIn.MemoryReader();

            uint version = memIn.ReadUInt32();

            Name = memIn.ReadString();

            int numEntries = memIn.ReadInt32();

            for (int i = 0; i < numEntries; ++i)
            {
                ValueTable.Values.Add(memIn.ReadString());
            }
            ValueTable.Formula    = memIn.ReadString();
            ValueTable.StartValue = memIn.ReadString();
            ValueTable.EndValue   = memIn.ReadString();
            ValueTable.StepValue  = memIn.ReadString();
            return(true);
        }
        public void FromBuffer(GR.Memory.ByteBuffer Data)
        {
            if (Data == null)
            {
                return;
            }
            GR.IO.MemoryReader memIn = Data.MemoryReader();

            int w = memIn.ReadInt32();
            int h = memIn.ReadInt32();

            System.Drawing.Imaging.PixelFormat pf = (System.Drawing.Imaging.PixelFormat)memIn.ReadUInt32();

            Create(w, h, pf);

            int numEntries = memIn.ReadInt32();

            for (int i = 0; i < numEntries; ++i)
            {
                byte r = memIn.ReadUInt8();
                byte g = memIn.ReadUInt8();
                byte b = memIn.ReadUInt8();

                SetPaletteColor(i, r, g, b);
            }
        }
Beispiel #3
0
    public override void ApplyDisplayDetails( GR.Memory.ByteBuffer Buffer )
    {
      GR.IO.MemoryReader binReader = Buffer.MemoryReader();

      foreach ( ColumnHeader column in listWatch.Columns )
      {
        int     width = binReader.ReadInt32();
        if ( width != 0 )
        {
          column.Width = width;
        }
      }
    }
Beispiel #4
0
        public override void ApplyDisplayDetails(GR.Memory.ByteBuffer Buffer)
        {
            GR.IO.MemoryReader memIn = Buffer.MemoryReader();

            int numColumns = memIn.ReadInt32();

            for (int i = 0; i < numColumns; ++i)
            {
                if (i < listResults.Columns.Count)
                {
                    listResults.Columns[i].Width = memIn.ReadInt32();
                }
            }

            listResultsSortColumn = memIn.ReadInt32();
            listResults.Sorting   = (SortOrder)memIn.ReadInt32();
        }
        public bool ReadFromBuffer(GR.Memory.ByteBuffer DataIn)
        {
            if (DataIn == null)
            {
                return(false);
            }
            GR.IO.MemoryReader memIn = DataIn.MemoryReader();

            uint version = memIn.ReadUInt32();

            if (version == 1)
            {
                TotalNumberOfCharacters = 256;

                string name            = memIn.ReadString();
                string charsetFilename = memIn.ReadString();
                for (int i = 0; i < TotalNumberOfCharacters; ++i)
                {
                    Characters[i].Tile.CustomColor = memIn.ReadInt32();
                }

                bool hasAnyMC = false;
                for (int i = 0; i < TotalNumberOfCharacters; ++i)
                {
                    var mode = (TextCharMode)memIn.ReadUInt8();

                    if ((mode == TextCharMode.COMMODORE_MULTICOLOR) &&
                        (Characters[i].Tile.CustomColor < 8))
                    {
                        mode = TextCharMode.COMMODORE_HIRES;
                    }
                    if (mode == TextCharMode.COMMODORE_MULTICOLOR)
                    {
                        hasAnyMC = true;
                    }
                }
                Colors.BackgroundColor = memIn.ReadInt32();
                Colors.MultiColor1     = memIn.ReadInt32();
                Colors.MultiColor2     = memIn.ReadInt32();


                for (int i = 0; i < TotalNumberOfCharacters; ++i)
                {
                    int tileColor1 = memIn.ReadInt32();
                    int tileColor2 = memIn.ReadInt32();
                    int tileColor3 = memIn.ReadInt32();
                    int tileColor4 = memIn.ReadInt32();
                    int tileChar1  = memIn.ReadInt32();
                    int tileChar2  = memIn.ReadInt32();
                    int tileChar3  = memIn.ReadInt32();
                    int tileChar4  = memIn.ReadInt32();
                }

                bool genericMulticolor       = (memIn.ReadInt32() != 0);
                GR.Memory.ByteBuffer testbed = new GR.Memory.ByteBuffer();
                memIn.ReadBlock(testbed, 64);

                GR.Memory.ByteBuffer charsetData = new GR.Memory.ByteBuffer();
                memIn.ReadBlock(charsetData, (uint)(TotalNumberOfCharacters * 8));

                for (int i = 0; i < TotalNumberOfCharacters; ++i)
                {
                    Characters[i].Tile.Data = charsetData.SubBuffer(i * 8, 8);
                }

                UsedTiles = memIn.ReadUInt32();

                ExportFilename = memIn.ReadString();
                string exportPathBlockTable  = memIn.ReadString();
                string exportPathCharset     = memIn.ReadString();
                string exportPathEditorTiles = memIn.ReadString();

                // categories
                Categories.Clear();
                int categoryCount = memIn.ReadInt32();
                for (int i = 0; i < categoryCount; ++i)
                {
                    int    catKey  = memIn.ReadInt32();
                    string catName = memIn.ReadString();

                    Categories.Add(catName);
                }
                if (Categories.Count == 0)
                {
                    // add default category
                    Categories.Add("Uncategorized");
                }
                for (int i = 0; i < TotalNumberOfCharacters; ++i)
                {
                    Characters[i].Category = memIn.ReadInt32();
                    if ((Characters[i].Category < 0) ||
                        (Characters[i].Category >= Categories.Count))
                    {
                        Characters[i].Category = 0;
                    }
                }
                ExportNumCharacters = memIn.ReadInt32();
                if (ExportNumCharacters < TotalNumberOfCharacters)
                {
                    ExportNumCharacters = TotalNumberOfCharacters;
                }
                ShowGrid             = (memIn.ReadInt32() != 0);
                ExportStartCharacter = memIn.ReadInt32();
                Colors.BGColor4      = memIn.ReadInt32();

                // playground
                int w = memIn.ReadInt32();
                int h = memIn.ReadInt32();
                if (w * h < 256)
                {
                    w = 16;
                    h = 16;
                }
                PlaygroundChars = new List <uint>(w * h);
                for (int i = 0; i < w * h; ++i)
                {
                    ushort charInfo = memIn.ReadUInt16();
                    PlaygroundChars.Add((uint)((charInfo & 0xff) | ((charInfo & 0xff00) << 8)));
                }

                Mode = (TextCharMode)memIn.ReadInt32();
                if (hasAnyMC)
                {
                    Mode = TextCharMode.COMMODORE_MULTICOLOR;
                }
            }
            else if (version == 2)
            {
                Characters.Clear();
                Categories.Clear();
                Colors.Palettes.Clear();
                TotalNumberOfCharacters = 256;
                Mode = TextCharMode.COMMODORE_HIRES;

                var chunk = new GR.IO.FileChunk();

                while (chunk.ReadFromStream(memIn))
                {
                    if (chunk.Type == FileChunkConstants.CHARSET_PROJECT)
                    {
                        var chunkIn = chunk.MemoryReader();

                        var subChunk = new GR.IO.FileChunk();

                        while (subChunk.ReadFromStream(chunkIn))
                        {
                            var subMemIn = subChunk.MemoryReader();
                            switch (subChunk.Type)
                            {
                            case FileChunkConstants.CHARSET_INFO:
                                Mode = (TextCharMode)subMemIn.ReadInt32();
                                TotalNumberOfCharacters = subMemIn.ReadInt32();
                                ShowGrid = ((subMemIn.ReadInt32() & 1) == 1);
                                break;

                            case FileChunkConstants.CHARSET_COLOR_SETTINGS:
                                Colors.BackgroundColor = subMemIn.ReadInt32();
                                Colors.MultiColor1     = subMemIn.ReadInt32();
                                Colors.MultiColor2     = subMemIn.ReadInt32();
                                Colors.BGColor4        = subMemIn.ReadInt32();
                                Colors.ActivePalette   = subMemIn.ReadInt32();
                                break;

                            case FileChunkConstants.PALETTE:
                                Colors.Palettes.Add(Palette.Read(subMemIn));
                                break;

                            case FileChunkConstants.CHARSET_EXPORT:
                                ExportStartCharacter = subMemIn.ReadInt32();
                                ExportNumCharacters  = subMemIn.ReadInt32();
                                ExportFilename       = subMemIn.ReadString();
                                break;

                            case FileChunkConstants.CHARSET_CHAR:
                            {
                                var charData = new CharData();

                                subMemIn.ReadInt32(); // was TextCharMode
                                charData.Tile.CustomColor = subMemIn.ReadInt32();
                                charData.Tile.Mode        = Lookup.GraphicTileModeFromTextCharMode(Mode, charData.Tile.CustomColor);

                                charData.Category = subMemIn.ReadInt32();

                                int dataLength = subMemIn.ReadInt32();
                                charData.Tile.Data = new GR.Memory.ByteBuffer();
                                subMemIn.ReadBlock(charData.Tile.Data, (uint)dataLength);

                                Characters.Add(charData);
                            }
                            break;

                            case FileChunkConstants.CHARSET_CATEGORY:
                                Categories.Add(subMemIn.ReadString());
                                break;

                            case FileChunkConstants.CHARSET_PLAYGROUND:
                                PlaygroundWidth  = subMemIn.ReadInt32();
                                PlaygroundHeight = subMemIn.ReadInt32();

                                PlaygroundChars = new List <uint>(PlaygroundWidth * PlaygroundHeight);

                                for (int i = 0; i < PlaygroundWidth * PlaygroundHeight; ++i)
                                {
                                    // 16 bit index, 16 bit color
                                    PlaygroundChars.Add(subMemIn.ReadUInt32());
                                }
                                break;
                            }
                        }
                    }
                }
                if (Colors.Palettes.Count == 0)
                {
                    Colors.Palettes.Add(PaletteManager.PaletteFromNumColors(Lookup.NumberOfColorsInCharacter(Mode)));
                }
            }
            else
            {
                return(false);
            }
            return(true);
        }
        public bool ReadFromBuffer(GR.Memory.ByteBuffer DataIn)
        {
            GR.IO.MemoryReader memIn = DataIn.MemoryReader();

            uint   version         = memIn.ReadUInt32();
            string name            = memIn.ReadString();
            string charsetFilename = memIn.ReadString();

            for (int i = 0; i < 256; ++i)
            {
                Characters[i].Color = memIn.ReadInt32();
            }
            for (int i = 0; i < 256; ++i)
            {
                Characters[i].Multicolor = (memIn.ReadUInt8() != 0);
            }
            BackgroundColor = memIn.ReadInt32();
            MultiColor1     = memIn.ReadInt32();
            MultiColor2     = memIn.ReadInt32();


            for (int i = 0; i < 256; ++i)
            {
                int tileColor1 = memIn.ReadInt32();
                int tileColor2 = memIn.ReadInt32();
                int tileColor3 = memIn.ReadInt32();
                int tileColor4 = memIn.ReadInt32();
                int tileChar1  = memIn.ReadInt32();
                int tileChar2  = memIn.ReadInt32();
                int tileChar3  = memIn.ReadInt32();
                int tileChar4  = memIn.ReadInt32();
            }

            bool genericMulticolor = (memIn.ReadInt32() != 0);

            GR.Memory.ByteBuffer testbed = new GR.Memory.ByteBuffer();
            memIn.ReadBlock(testbed, 64);

            GR.Memory.ByteBuffer charsetData = new GR.Memory.ByteBuffer();
            memIn.ReadBlock(charsetData, 2048);

            for (int i = 0; i < 256; ++i)
            {
                Characters[i].Data = charsetData.SubBuffer(i * 8, 8);
            }

            UsedTiles = memIn.ReadUInt32();

            ExportFilename = memIn.ReadString();
            string exportPathBlockTable  = memIn.ReadString();
            string exportPathCharset     = memIn.ReadString();
            string exportPathEditorTiles = memIn.ReadString();

            // categories
            Categories.Clear();
            int categoryCount = memIn.ReadInt32();

            for (int i = 0; i < categoryCount; ++i)
            {
                int    catKey  = memIn.ReadInt32();
                string catName = memIn.ReadString();

                Categories[catKey] = catName;
            }
            if (Categories.Count == 0)
            {
                // add default category
                Categories[0] = "Uncategorized";
            }
            for (int i = 0; i < 256; ++i)
            {
                Characters[i].Category = memIn.ReadInt32();
            }
            return(true);
        }
Beispiel #7
0
        public bool ReadFromBuffer(GR.Memory.ByteBuffer DataIn)
        {
            if (DataIn == null)
            {
                return(false);
            }
            SpriteLayers.Clear();

            GR.IO.MemoryReader memIn = DataIn.MemoryReader();

            uint Version    = memIn.ReadUInt32();
            int  numSprites = 256;

            if (Version >= 1)
            {
                numSprites = memIn.ReadInt32();
            }
            Sprites = new List <SpriteData>();
            for (int i = 0; i < numSprites; ++i)
            {
                Sprites.Add(new SpriteData());
                CustomRenderer.PaletteManager.ApplyPalette(Sprites[i].Image);
            }

            string name = memIn.ReadString();

            for (int i = 0; i < numSprites; ++i)
            {
                Sprites[i].Color = memIn.ReadInt32();
            }
            for (int i = 0; i < numSprites; ++i)
            {
                Sprites[i].Multicolor = (memIn.ReadUInt8() != 0);
            }
            BackgroundColor = memIn.ReadInt32();
            MultiColor1     = memIn.ReadInt32();
            MultiColor2     = memIn.ReadInt32();

            bool genericMultiColor = (memIn.ReadUInt32() != 0);

            for (int i = 0; i < numSprites; ++i)
            {
                GR.Memory.ByteBuffer tempBuffer = new GR.Memory.ByteBuffer();

                memIn.ReadBlock(tempBuffer, 64);
                tempBuffer.CopyTo(Sprites[i].Data, 0, 63);
            }

            UsedSprites = memIn.ReadUInt32();

            ExportFilename = memIn.ReadString();
            string exportPathSpriteFile = memIn.ReadString();

            for (int i = 0; i < numSprites; ++i)
            {
                string desc = memIn.ReadString();
            }
            int spriteTestCount = memIn.ReadInt32();

            for (int i = 0; i < spriteTestCount; ++i)
            {
                int  spriteIndex      = memIn.ReadInt32();
                byte spriteColor      = memIn.ReadUInt8();
                bool spriteMultiColor = (memIn.ReadUInt8() != 0);
                int  spriteX          = memIn.ReadInt32();
                int  spriteY          = memIn.ReadInt32();
            }

            GR.IO.FileChunk chunk = new GR.IO.FileChunk();

            while (chunk.ReadFromStream(memIn))
            {
                switch (chunk.Type)
                {
                case Types.FileChunk.SPRITESET_LAYER:
                {
                    Layer layer = new Layer();

                    SpriteLayers.Add(layer);

                    var chunkReader = chunk.MemoryReader();

                    GR.IO.FileChunk subChunk = new GR.IO.FileChunk();

                    while (subChunk.ReadFromStream(chunkReader))
                    {
                        var subChunkReader = subChunk.MemoryReader();

                        if (subChunk.Type == Types.FileChunk.SPRITESET_LAYER_ENTRY)
                        {
                            LayerSprite sprite = new LayerSprite();

                            sprite.Index   = subChunkReader.ReadInt32();
                            sprite.Color   = subChunkReader.ReadUInt8();
                            sprite.X       = subChunkReader.ReadInt32();
                            sprite.Y       = subChunkReader.ReadInt32();
                            sprite.ExpandX = (subChunkReader.ReadUInt8() != 0);
                            sprite.ExpandY = (subChunkReader.ReadUInt8() != 0);

                            layer.Sprites.Add(sprite);
                        }
                        else if (subChunk.Type == Types.FileChunk.SPRITESET_LAYER_INFO)
                        {
                            layer.Name            = subChunkReader.ReadString();
                            layer.BackgroundColor = subChunkReader.ReadUInt8();
                        }
                    }
                }
                break;
                }
            }
            return(true);
        }
        public bool ReadFromBuffer(GR.Memory.ByteBuffer DataIn)
        {
            if (DataIn == null)
            {
                return(false);
            }
            Data.Clear();
            JumpedAtAddresses.Clear();
            NamedLabels.Clear();
            Description = "";

            GR.IO.MemoryReader memIn = DataIn.MemoryReader();

            GR.IO.FileChunk chunk = new GR.IO.FileChunk();

            while (chunk.ReadFromStream(memIn))
            {
                var chunkReader = chunk.MemoryReader();
                switch (chunk.Type)
                {
                case FileChunkConstants.DISASSEMBLY_INFO:
                    Description = chunkReader.ReadString();
                    break;

                case FileChunkConstants.DISASSEMBLY_DATA:
                {
                    DataStartAddress = chunkReader.ReadInt32();
                    uint dataLength = chunkReader.ReadUInt32();

                    chunkReader.ReadBlock(Data, dataLength);
                }
                break;

                case FileChunkConstants.DISASSEMBLY_JUMP_ADDRESSES:
                {
                    int numEntries = chunkReader.ReadInt32();

                    for (int i = 0; i < numEntries; ++i)
                    {
                        int value = chunkReader.ReadInt32();
                        JumpedAtAddresses.Add(value);
                    }
                }
                break;

                case FileChunkConstants.DISASSEMBLY_NAMED_LABELS:
                {
                    int numEntries = chunkReader.ReadInt32();

                    for (int i = 0; i < numEntries; ++i)
                    {
                        int    address = chunkReader.ReadInt32();
                        string name    = chunkReader.ReadString();

                        NamedLabels[address] = name;
                    }
                }
                break;
                }
            }
            return(true);
        }
Beispiel #9
0
        private bool LoadVersion7(GR.Memory.ByteBuffer Data)
        {
            BackgroundColor  = Data.ByteAt(4);
            MultiColor1      = Data.ByteAt(5);
            MultiColor2      = Data.ByteAt(6);
            BackgroundColor4 = Data.ByteAt(7);
            CustomColor      = Data.ByteAt(8);
            TileColorMode    = (ColorMode)Data.ByteAt(9);

            CharsetMode = (Types.CharsetMode)Data.ByteAt(10);

            byte flags          = Data.ByteAt(11);
            bool tileSysEnabled = ((flags & 0x01) != 0);


            ushort charDataBlockID      = 0xdab0;
            ushort charAttributeBlockID = 0xdab1;
            ushort mapDataBlockID       = 0xdab2;


            if (!tileSysEnabled)
            {
                // fake tiles (one per char)
                TileWidth  = 1;
                TileHeight = 1;
                NumTiles   = 256;

                for (int i = 0; i < NumTiles; ++i)
                {
                    Tile tile = new Tile();

                    tile.CharData.Resize((uint)(TileWidth * TileHeight * 2));
                    tile.CharData.SetU16At(0, (ushort)i);

                    tile.ColorData.Resize((uint)(TileWidth * TileHeight));
                    tile.ColorData.SetU8At(0, (byte)CustomColor);

                    Tiles.Add(tile);
                }
            }

            var reader = Data.MemoryReader();

            reader.Skip(12);

            while (reader.DataAvailable)
            {
                ushort blockID = reader.ReadUInt16NetworkOrder();

                if (blockID == mapDataBlockID)
                {
                    if (tileSysEnabled)
                    {
                        // Tile data block

                        // TILECNT: Tile count minus one(16 - bit, LSBF).
                        NumTiles = reader.ReadUInt16() + 1;

                        // TILEWID: Tile width( byte).
                        TileWidth = reader.ReadUInt8();
                        // TILEHEI: Tile height( byte).
                        TileHeight = reader.ReadUInt8();

                        // TILEDAT: Tile data, 16 bits per tile cell( LSBF) for TILEWID* TILEHEI cells * TILECNT items, cells are in LRTB order.
                        for (int i = 0; i < NumTiles; ++i)
                        {
                            Tile tile = new Tile();

                            tile.CharData.Resize((uint)(TileWidth * TileHeight * 2));
                            tile.ColorData.Resize((uint)(TileWidth * TileHeight));

                            Tiles.Add(tile);
                        }
                        if (NumChars < 256)
                        {
                            // add all chars for safety reasons
                            for (int i = NumChars; i < 256; ++i)
                            {
                                SingleChar newChar = new SingleChar();
                                newChar.Data = new GR.Memory.ByteBuffer(8);
                                Characters.Add(newChar);
                            }
                        }
                        for (int i = 0; i < NumTiles; ++i)
                        {
                            for (int j = 0; j < TileWidth * TileHeight; ++j)
                            {
                                Tiles[i].CharData.SetU16At(j * 2, reader.ReadUInt16());
                            }
                        }
                    }
                    else
                    {
                        // BLKMARK : Block marker (0xDA, 0xBn).
                        // MAPWID: Map Width(16 - bit, LSBF).
                        // MAPHEI: Map height(16 - bit, LSBF).
                        // MAPDAT: Map data, 16 bits per cell( LSBF ) for MAPWID* MAPHEI cells, cells are in LRTB order.
                        MapWidth  = reader.ReadUInt16();
                        MapHeight = reader.ReadUInt16();

                        MapData = new GR.Memory.ByteBuffer((uint)(MapWidth * MapHeight));

                        for (int i = 0; i < MapHeight; ++i)
                        {
                            for (int j = 0; j < MapWidth; ++j)
                            {
                                MapData.SetU8At(i * MapWidth + j, (byte)reader.ReadUInt16());
                            }
                        }
                    }
                }

                if (blockID == charDataBlockID)
                {
                    // Character data block

                    // CHARCNT: Character image count minus one( 16 - bit, LSBF ).
                    NumChars = reader.ReadUInt16() + 1;

                    // CHARDAT : Character image data( eight bytes / rows per image for CHARCNT images, rows are in TB order ).
                    for (int charIndex = 0; charIndex < NumChars; ++charIndex)
                    {
                        SingleChar newChar = new SingleChar();
                        newChar.Data  = new GR.Memory.ByteBuffer();
                        newChar.Color = CustomColor;
                        reader.ReadBlock(newChar.Data, 8);
                        Characters.Add(newChar);
                    }

                    if (!tileSysEnabled)
                    {
                        if (NumChars < 256)
                        {
                            // add all chars for safety reasons
                            for (int i = NumChars; i < 256; ++i)
                            {
                                SingleChar newChar = new SingleChar();
                                newChar.Data  = new GR.Memory.ByteBuffer(8);
                                newChar.Color = CustomColor;

                                Characters.Add(newChar);
                            }
                        }
                    }
                }
                else if (blockID == charAttributeBlockID)
                {
                    // char attributes
                    // BLKMARK: Block marker(0xDA, 0xB1).
                    // CHARATTS: Char attribute data, one byte per char image for CHARCNT images, low nybble = colour, high nybble = material.
                    //           nb.colours are only stored when the colouring mode is "per character".
                    for (int charIndex = 0; charIndex < NumChars; ++charIndex)
                    {
                        if (TileColorMode == ColorMode.PER_TILE_CELL)
                        {
                            Characters[charIndex].Color = reader.ReadUInt8() & 0x0f;
                            if (!tileSysEnabled)
                            {
                                Tiles[charIndex].ColorData.SetU8At(0, (byte)Characters[charIndex].Color);
                            }
                        }
                    }
                }
            }


            /*
             *
             *
             * int headerSize = 20;
             * int offsetToCharData = headerSize;
             * int offsetToCharAttribs = offsetToCharData + NumChars * 8;
             * int offsetToTileData = offsetToCharAttribs + NumChars;
             * int offsetToTileColors = offsetToTileData + NumTiles * TileWidth * TileHeight * 2;
             * if ( ( tileSysEnabled )
             || ( noTiles ) )
             ||{
             ||offsetToTileColors = offsetToTileData;
             ||}
             ||int offsetToMapData = offsetToTileColors + NumTiles;
             ||if ( ( TileColorMode != ColorMode.PER_TILE )
             || ( noTiles ) )
             ||{
             ||offsetToMapData = offsetToTileColors;
             ||}
             ||
             ||// tile_data
             ||if ( noTiles )
             ||{
             ||for ( int i = 0; i < NumTiles; ++i )
             ||{
             || Tile tile = new Tile();
             ||
             || tile.CharData.Resize( (uint)( TileWidth * TileHeight * 2 ) );
             || tile.CharData.SetU16At( 0, (ushort)i );
             ||
             || tile.ColorData.Resize( (uint)( TileWidth * TileHeight ) );
             || tile.ColorData.SetU8At( 0, (byte)CustomColor );
             ||
             || Tiles.Add( tile );
             ||}
             ||if ( NumChars < 256 )
             ||{
             || // add all chars for safety reasons
             || for ( int i = NumChars; i < 256; ++i )
             || {
             ||   SingleChar    newChar = new SingleChar();
             ||   newChar.Data = new GR.Memory.ByteBuffer( 8 );
             ||   if ( TileColorMode == ColorMode.PER_TILE_CELL )
             ||   {
             ||     newChar.Color = Data.ByteAt( offsetToCharAttribs + i ) & 0x0f;
             ||   }
             ||   else
             ||   {
             ||     newChar.Color = CustomColor;
             ||   }
             ||
             ||   Characters.Add( newChar );
             || }
             ||}
             ||}
             ||else
             ||{
             ||for ( int i = 0; i < NumTiles; ++i )
             ||{
             || Tile tile = new Tile();
             ||
             || tile.CharData.Resize( (uint)( TileWidth * TileHeight * 2 ) );
             || tile.ColorData.Resize( (uint)( TileWidth * TileHeight ) );
             ||
             || Tiles.Add( tile );
             ||}
             ||
             ||if ( tileSysEnabled )
             ||{
             || byte curCharIndex = 0;
             || for ( int i = 0; i < NumTiles; ++i )
             || {
             ||   for ( int j = 0; j < TileWidth * TileHeight; ++j )
             ||   {
             ||     Tiles[i].CharData.SetU16At( j * 2, curCharIndex );
             ++curCharIndex;
             ||   }
             || }
             ||}
             ||else
             ||{
             || // tile_data.      Size = NUM_TILES * TILE_WIDTH * TILE_HEIGHT bytes * 2 bytes. (only exists if CHAR_DATA is not "Expanded")
             || for ( int i = 0; i < NumTiles; ++i )
             || {
             ||   for ( int j = 0; j < TileWidth * TileHeight; ++j )
             ||   {
             ||     Tiles[i].CharData.SetU16At( j * 2, Data.UInt16At( offsetToTileData + i * TileWidth * TileHeight * 2 + j * 2 ) );
             ||   }
             || }
             ||}
             ||}
             ||
             ||// TILE_COLOURS.   Size = NUM_TILES bytes (1 byte per tile = "RAM colour". only exists if COLOR_MODE = 1 (Per Tile)
             ||if ( TileColorMode == ColorMode.PER_TILE )
             ||{
             ||for ( int i = 0; i < NumTiles; ++i )
             ||{
             || for ( int y = 0; y < TileHeight; ++y )
             || {
             ||   for ( int x = 0; x < TileWidth; ++x )
             ||   {
             ||     Tiles[i].ColorData.SetU8At( x + y * TileWidth, (byte)( Data.ByteAt( offsetToTileColors + i ) & 0x0f ) );
             ||   }
             || }
             ||}
             ||}
             ||else if ( TileColorMode == ColorMode.PER_TILE_CELL )
             ||{
             ||// with V5 this actually means per character
             ||for ( int i = 0; i < NumTiles; ++i )
             ||{
             || for ( int y = 0; y < TileHeight; ++y )
             || {
             ||   for ( int x = 0; x < TileWidth; ++x )
             ||   {
             ||     byte    charColor = (byte)Characters[Tiles[i].CharData.ByteAt( 2 * ( x + y * TileWidth ) )].Color;
             ||     Tiles[i].ColorData.SetU8At( x + y * TileWidth, charColor );
             ||   }
             || }
             ||}
             ||}
             ||else if ( TileColorMode == ColorMode.GLOBAL )
             ||{
             ||for ( int i = 0; i < NumTiles; ++i )
             ||{
             || for ( int y = 0; y < TileHeight; ++y )
             || {
             ||   for ( int x = 0; x < TileWidth; ++x )
             ||   {
             ||     Tiles[i].ColorData.SetU8At( x + y * TileWidth, (byte)CustomColor );
             ||   }
             || }
             ||}
             ||}
             ||else if ( TileColorMode == ColorMode.PER_TILE_CELL )
             ||{
             ||for ( int i = 0; i < NumTiles; ++i )
             ||{
             || for ( int y = 0; y < TileHeight; ++y )
             || {
             ||   for ( int x = 0; x < TileWidth; ++x )
             ||   {
             ||     Tiles[i].ColorData.SetU8At( x + y * TileWidth, (byte)( Data.ByteAt( offsetCellAttribs + i * TileWidth * TileHeight + x + y * TileHeight ) & 0x0f ) );
             ||   }
             || }
             ||}
             ||}
             ||
             */
            return(true);
        }
        public bool ReadFromBuffer(GR.Memory.ByteBuffer DataIn)
        {
            if (DataIn == null)
            {
                return(false);
            }
            SpriteLayers.Clear();

            GR.IO.MemoryReader memIn = DataIn.MemoryReader();

            GR.Memory.ByteBuffer header = new GR.Memory.ByteBuffer();

            if (memIn.ReadBlock(header, 9) != 9)
            {
                return(false);
            }
            if ((header.ByteAt(0) != 0x53) ||
                (header.ByteAt(1) != 0x50) ||
                (header.ByteAt(2) != 0x44))
            {
                // no SPD
                return(false);
            }

            NumSprites = header.ByteAt(4) + 1;
            int numAnims = header.ByteAt(5) + 1;

            BackgroundColor = header.ByteAt(6);
            MultiColor1     = header.ByteAt(7);
            MultiColor2     = header.ByteAt(8);

            Sprites = new List <SpriteData>();

            GR.Memory.ByteBuffer tempData = new GR.Memory.ByteBuffer();
            for (int i = 0; i < NumSprites; ++i)
            {
                Sprites.Add(new SpriteData());
                PaletteManager.ApplyPalette(Sprites[i].Image);

                tempData.Clear();
                memIn.ReadBlock(tempData, 63);
                tempData.CopyTo(Sprites[i].Data, 0, 63);

                Sprites[i].Color      = memIn.ReadUInt8();
                Sprites[i].Multicolor = (((Sprites[i].Color) & 0x80) != 0);
                Sprites[i].Color     &= 0x0f;
            }

            if (numAnims > 0)
            {
                GR.Memory.ByteBuffer animFrom       = new GR.Memory.ByteBuffer();
                GR.Memory.ByteBuffer animTo         = new GR.Memory.ByteBuffer();
                GR.Memory.ByteBuffer animNumFrames  = new GR.Memory.ByteBuffer();
                GR.Memory.ByteBuffer animAttributes = new GR.Memory.ByteBuffer();

                memIn.ReadBlock(animFrom, (uint)numAnims);
                memIn.ReadBlock(animTo, (uint)numAnims);
                memIn.ReadBlock(animNumFrames, (uint)numAnims);
                memIn.ReadBlock(animAttributes, (uint)numAnims);
            }
            UsedSprites = (uint)NumSprites;
            return(true);
        }
Beispiel #11
0
        public bool ReadFromBuffer(GR.Memory.ByteBuffer DataIn)
        {
            if (DataIn == null)
            {
                return(false);
            }
            GR.IO.MemoryReader memIn = DataIn.MemoryReader();

            uint   version         = memIn.ReadUInt32();
            string name            = memIn.ReadString();
            string charsetFilename = memIn.ReadString();

            for (int i = 0; i < 256; ++i)
            {
                Characters[i].Color = memIn.ReadInt32();
            }
            for (int i = 0; i < 256; ++i)
            {
                Characters[i].Mode = (Types.CharsetMode)memIn.ReadUInt8();
            }
            BackgroundColor = memIn.ReadInt32();
            MultiColor1     = memIn.ReadInt32();
            MultiColor2     = memIn.ReadInt32();


            for (int i = 0; i < 256; ++i)
            {
                int tileColor1 = memIn.ReadInt32();
                int tileColor2 = memIn.ReadInt32();
                int tileColor3 = memIn.ReadInt32();
                int tileColor4 = memIn.ReadInt32();
                int tileChar1  = memIn.ReadInt32();
                int tileChar2  = memIn.ReadInt32();
                int tileChar3  = memIn.ReadInt32();
                int tileChar4  = memIn.ReadInt32();
            }

            bool genericMulticolor = (memIn.ReadInt32() != 0);

            GR.Memory.ByteBuffer testbed = new GR.Memory.ByteBuffer();
            memIn.ReadBlock(testbed, 64);

            GR.Memory.ByteBuffer charsetData = new GR.Memory.ByteBuffer();
            memIn.ReadBlock(charsetData, 2048);

            for (int i = 0; i < 256; ++i)
            {
                Characters[i].Data = charsetData.SubBuffer(i * 8, 8);
            }

            UsedTiles = memIn.ReadUInt32();

            ExportFilename = memIn.ReadString();
            string exportPathBlockTable  = memIn.ReadString();
            string exportPathCharset     = memIn.ReadString();
            string exportPathEditorTiles = memIn.ReadString();

            // categories
            Categories.Clear();
            int categoryCount = memIn.ReadInt32();

            for (int i = 0; i < categoryCount; ++i)
            {
                int    catKey  = memIn.ReadInt32();
                string catName = memIn.ReadString();

                Categories.Add(catName);
            }
            if (Categories.Count == 0)
            {
                // add default category
                Categories.Add("Uncategorized");
            }
            for (int i = 0; i < 256; ++i)
            {
                Characters[i].Category = memIn.ReadInt32();
                if ((Characters[i].Category < 0) ||
                    (Characters[i].Category >= Categories.Count))
                {
                    Characters[i].Category = 0;
                }
            }
            NumCharacters = memIn.ReadInt32();
            if (NumCharacters == 0)
            {
                NumCharacters = 256;
            }
            ShowGrid       = (memIn.ReadInt32() != 0);
            StartCharacter = memIn.ReadInt32();
            BGColor4       = memIn.ReadInt32();

            // playground
            int w = memIn.ReadInt32();
            int h = memIn.ReadInt32();

            if (w * h < 256)
            {
                w = 16;
                h = 16;
            }
            PlaygroundChars = new List <ushort>(w * h);
            for (int i = 0; i < w * h; ++i)
            {
                PlaygroundChars.Add(memIn.ReadUInt16());
            }
            return(true);
        }
Beispiel #12
0
        public bool GetFromClipboard()
        {
            IDataObject dataObj = Clipboard.GetDataObject();

            if (dataObj == null)
            {
                return(false);
            }
            if (!dataObj.GetDataPresent("C64Studio.ImageList"))
            {
                return(false);
            }
            System.IO.MemoryStream ms = (System.IO.MemoryStream)dataObj.GetData("C64Studio.ImageList");

            GR.Memory.ByteBuffer spriteData = new GR.Memory.ByteBuffer((uint)ms.Length);
            ms.Read(spriteData.Data(), 0, (int)ms.Length);
            GR.IO.MemoryReader memIn = spriteData.MemoryReader();

            int numEntries = memIn.ReadInt32();

            ColumnBased = (memIn.ReadInt32() > 0) ? true : false;

            var incomingColorSettings = new ColorSettings();

            incomingColorSettings.BackgroundColor = memIn.ReadInt32();
            incomingColorSettings.MultiColor1     = memIn.ReadInt32();
            incomingColorSettings.MultiColor2     = memIn.ReadInt32();
            incomingColorSettings.BGColor4        = memIn.ReadInt32();

            incomingColorSettings.Palettes.Clear();
            int numPalettes = memIn.ReadInt32();

            for (int j = 0; j < numPalettes; ++j)
            {
                int numPaletteEntries = memIn.ReadInt32();
                var pal = new Palette(numPaletteEntries);
                for (int i = 0; i < numPaletteEntries; ++i)
                {
                    pal.ColorValues[i] = memIn.ReadUInt32();
                }
                pal.CreateBrushes();
                incomingColorSettings.Palettes.Add(pal);
            }

            for (int i = 0; i < numEntries; ++i)
            {
                var entry = new Entry();

                entry.Index = memIn.ReadInt32();

                entry.Tile.Mode        = (GraphicTileMode)memIn.ReadInt32();
                entry.Tile.CustomColor = memIn.ReadInt32();
                int palIndex = memIn.ReadInt32();
                entry.Tile.Width  = memIn.ReadInt32();
                entry.Tile.Height = memIn.ReadInt32();
                uint dataLength = memIn.ReadUInt32();
                entry.Tile.Data = new GR.Memory.ByteBuffer();
                memIn.ReadBlock(entry.Tile.Data, dataLength);

                entry.Tile.Colors = new ColorSettings(incomingColorSettings);
                entry.Tile.Colors.ActivePalette = palIndex;

                int originalIndex = memIn.ReadInt32();

                Entries.Add(entry);
            }
            return(true);
        }
Beispiel #13
0
        public override void ApplyDisplayDetails(GR.Memory.ByteBuffer Buffer)
        {
            GR.IO.MemoryReader binReader = Buffer.MemoryReader();

            SetCursorToLine(binReader.ReadInt32(), true);
        }
        private bool LoadVersion8(GR.Memory.ByteBuffer Data)
        {
            DisplayModeFile = (DisplayMode)Data.ByteAt(4);
            TileColorMode   = (ColorMode)Data.ByteAt(5);

            byte flags = Data.ByteAt(6);

            BackgroundColor  = Data.ByteAt(7);
            MultiColor1      = Data.ByteAt(8);
            MultiColor2      = Data.ByteAt(9);
            BackgroundColor4 = Data.ByteAt(10);

            BaseCellColorColorMatrix = Data.ByteAt(11);
            BaseCellColorScreenLo    = Data.ByteAt(12);
            BaseCellColorScreenHi    = Data.ByteAt(13);

            bool tileSysEnabled = ((flags & 0x01) != 0);


            ushort charDataBlockID      = 0xdab0;
            ushort charAttributeBlockID = 0xdab1;
            ushort charSetColorBlockID  = 0;
            ushort tileSetDataBlockID   = 0;
            ushort tileSetColorBlockID  = 0;
            ushort tileSetTagBlockID    = 0;
            ushort tileSetNameBlockID   = 0;
            ushort mapDataBlockID       = 0;

            int curBlockID = 0xdab2;

            if (TileColorMode == ColorMode.PER_CHAR)
            {
                charSetColorBlockID = (ushort)curBlockID++;
            }
            if (tileSysEnabled)
            {
                tileSetDataBlockID = (ushort)curBlockID++;
                if (TileColorMode == ColorMode.PER_TILE)
                {
                    tileSetColorBlockID = (ushort)curBlockID++;
                }
                tileSetTagBlockID  = (ushort)curBlockID++;
                tileSetNameBlockID = (ushort)curBlockID++;
            }
            mapDataBlockID = (ushort)curBlockID++;

            if (!tileSysEnabled)
            {
                // fake tiles (one per char)
                TileWidth  = 1;
                TileHeight = 1;
                NumTiles   = 256;

                for (int i = 0; i < NumTiles; ++i)
                {
                    Tile tile = new Tile();

                    tile.CharData.Resize((uint)(TileWidth * TileHeight * 2));
                    tile.CharData.SetU16At(0, (ushort)i);

                    tile.ColorData.Resize((uint)(TileWidth * TileHeight));
                    tile.ColorData.SetU8At(0, (byte)CustomColor);

                    Tiles.Add(tile);
                }
            }

            var reader = Data.MemoryReader();

            reader.Skip(14);

            while (reader.DataAvailable)
            {
                ushort blockID = reader.ReadUInt16NetworkOrder();

                if (blockID == charDataBlockID)
                {
                    // Character data block

                    // CHARCNT: Character image count minus one( 16 - bit, LSBF ).
                    NumChars = reader.ReadUInt16() + 1;

                    // CHARDAT : Character image data( eight bytes / rows per image for CHARCNT images, rows are in TB order ).
                    for (int charIndex = 0; charIndex < NumChars; ++charIndex)
                    {
                        SingleChar newChar = new SingleChar();
                        newChar.Data  = new GR.Memory.ByteBuffer();
                        newChar.Color = CustomColor;
                        reader.ReadBlock(newChar.Data, 8);
                        Characters.Add(newChar);
                    }

                    if (!tileSysEnabled)
                    {
                        if (NumChars < 256)
                        {
                            // add all chars for safety reasons
                            for (int i = NumChars; i < 256; ++i)
                            {
                                SingleChar newChar = new SingleChar();
                                newChar.Data  = new GR.Memory.ByteBuffer(8);
                                newChar.Color = CustomColor;

                                Characters.Add(newChar);
                            }
                        }
                    }
                }
                else if (blockID == charAttributeBlockID)
                {
                    // char attributes
                    // BLKMARK: Block marker(0xDA, 0xB1).
                    // CHARATTS: Char attribute data, one byte per char image for CHARCNT images, low nybble = colour, high nybble = material.
                    //           nb.colours are only stored when the colouring mode is "per character".
                    for (int charIndex = 0; charIndex < NumChars; ++charIndex)
                    {
                        if (TileColorMode == ColorMode.PER_CHAR)
                        {
                            Characters[charIndex].Color = reader.ReadUInt8() & 0x0f;
                            if (!tileSysEnabled)
                            {
                                Tiles[charIndex].ColorData.SetU8At(0, (byte)Characters[charIndex].Color);
                            }
                        }
                    }
                }
                else if (blockID == charSetColorBlockID)
                {
                    // Character set colours block (only present if the project uses per-char colouring)...
                    //
                    // BLKMARK         : Block marker (0xDA, 0xBn).
                    // MTRXCOLRS_CHARS : Char colour data, 1-3 bytes per char image for CHARCNT images...
                    //
                    //    Colour_CmLo : Colour Matrix Low nybble (0-15) (not present if DISP_MODE is Bitmap_HR).
                    //    Colour_SmLo : Screen Matrix Low nybble (0-15) (only present if DISP_MODE is Bitmap_HR or Bitmap_MC).
                    //    Colour_SmHi : Screen Matrix High nybble (0-15) (only present if DISP_MODE is Bitmap_HR or Bitmap_MC).
                    //
                    //    Notes:-
                    //    - The colours in this block are intended for transfer to the C64 colour RAM cells and/or screen RAM cells.
                    //    - The usage / usefulness of a colour will depend on the display mode.
                    //    - Only the low nybbles of each byte are currently used, each provides a colour 0-15.

                    for (int i = 0; i < NumChars; ++i)
                    {
                        if (DisplayModeFile != DisplayMode.BITMAP_HIRES)
                        {
                            Characters[i].Color = reader.ReadUInt8();
                        }
                        if ((DisplayModeFile == DisplayMode.BITMAP_MC) ||
                            (DisplayModeFile == DisplayMode.BITMAP_HIRES))
                        {
                            // screen color lo
                            reader.ReadUInt8();
                            // screen color hi
                            reader.ReadUInt8();
                        }
                        if (!tileSysEnabled)
                        {
                            // use the charset color for our faked tiles
                            Tiles[i].ColorData.SetU8At(0, (byte)Characters[i].Color);
                        }
                    }
                }
                else if (blockID == tileSetDataBlockID)
                {
                    // Tile data block

                    // TILECNT: Tile count minus one(16 - bit, LSBF).
                    NumTiles = reader.ReadUInt16() + 1;

                    // TILEWID: Tile width( byte).
                    TileWidth = reader.ReadUInt8();
                    // TILEHEI: Tile height( byte).
                    TileHeight = reader.ReadUInt8();

                    // TILEDAT: Tile data, 16 bits per tile cell( LSBF) for TILEWID* TILEHEI cells * TILECNT items, cells are in LRTB order.
                    for (int i = 0; i < NumTiles; ++i)
                    {
                        Tile tile = new Tile();

                        tile.CharData.Resize((uint)(TileWidth * TileHeight * 2));
                        tile.ColorData.Resize((uint)(TileWidth * TileHeight));

                        Tiles.Add(tile);
                    }
                    if (NumChars < 256)
                    {
                        // add all chars for safety reasons
                        for (int i = NumChars; i < 256; ++i)
                        {
                            SingleChar newChar = new SingleChar();
                            newChar.Data = new GR.Memory.ByteBuffer(8);
                            Characters.Add(newChar);
                        }
                    }
                    for (int i = 0; i < NumTiles; ++i)
                    {
                        for (int j = 0; j < TileWidth * TileHeight; ++j)
                        {
                            Tiles[i].CharData.SetU16At(j * 2, reader.ReadUInt16());
                        }
                    }
                }
                else if (blockID == tileSetColorBlockID)
                {
                    // Tile color block

                    // BLKMARK         : Block marker (0xDA, 0xBn).
                    // MTRXCOLRS_TILES : Tile colour data, 1-3 bytes per tile for TILECNT tiles...
                    //
                    //    Colour_CmLo : Colour Matrix Low nybble (0-15) (not present if DISP_MODE is Bitmap_HR).
                    //    Colour_SmLo : Screen Matrix Low nybble (0-15) (only present if DISP_MODE is Bitmap_HR or Bitmap_MC).
                    //    Colour_SmHi : Screen Matrix High nybble (0-15) (only present if DISP_MODE is Bitmap_HR or Bitmap_MC).
                    //
                    //    Notes:-
                    //    - The colours in this block are intended for transfer to the C64 colour RAM cells and/or screen RAM cells.
                    //    - The usage / usefulness of a colour will depend on the display mode.
                    //    - Only the low nybbles of each byte are currently used, each provides a colour 0-15.

                    for (int i = 0; i < NumTiles; ++i)
                    {
                        if (DisplayModeFile != DisplayMode.BITMAP_HIRES)
                        {
                            // tile generic color
                            reader.ReadUInt8();
                        }
                        if ((DisplayModeFile == DisplayMode.BITMAP_MC) ||
                            (DisplayModeFile == DisplayMode.BITMAP_HIRES))
                        {
                            // screen color lo
                            reader.ReadUInt8();
                            // screen color hi
                            reader.ReadUInt8();
                        }
                    }
                }
                else if (blockID == tileSetTagBlockID)
                {
                    // BLKMARK  : Block marker (0xDA, 0xBn).
                    // TILETAGS : Tile tag values, one byte per tile for TILECNT items.
                    for (int i = 0; i < NumTiles; ++i)
                    {
                        byte tileTag = reader.ReadUInt8();
                    }
                }
                else if (blockID == tileSetNameBlockID)
                {
                    // BLKMARK  : Block marker (0xDA, 0xBn).
                    // TILETAGS : Tile tag values, one byte per tile for TILECNT items.
                    for (int i = 0; i < NumTiles; ++i)
                    {
                        // zero terminated strings (urgh)
                        string name = "";
                        char   c    = (char)0;
                        do
                        {
                            c = (char)reader.ReadUInt8();
                            if (c != 0)
                            {
                                name += c;
                            }
                        }while (c != 0);
                        Tiles[i].Name = name;
                    }
                }
                else if (blockID == mapDataBlockID)
                {
                    // BLKMARK : Block marker (0xDA, 0xBn).
                    // MAPWID: Map Width(16 - bit, LSBF).
                    // MAPHEI: Map height(16 - bit, LSBF).
                    // MAPDAT: Map data, 16 bits per cell( LSBF ) for MAPWID* MAPHEI cells, cells are in LRTB order.
                    MapWidth  = reader.ReadUInt16();
                    MapHeight = reader.ReadUInt16();

                    MapData = new GR.Memory.ByteBuffer((uint)(MapWidth * MapHeight));
                    if (!tileSysEnabled)
                    {
                        // map has color data
                        MapColorData = new GR.Memory.ByteBuffer((uint)(MapWidth * MapHeight));
                    }

                    for (int i = 0; i < MapHeight; ++i)
                    {
                        for (int j = 0; j < MapWidth; ++j)
                        {
                            ushort mapData = reader.ReadUInt16();

                            // we only support 8 bytes per char
                            MapData.SetU8At(i * MapWidth + j, (byte)mapData); // do we?? Mega65!

                            if (TileColorMode == ColorMode.PER_CHAR)
                            {
                                MapColorData.SetU8At(i * MapWidth + j, (byte)Characters[(byte)mapData].Color);
                            }
                        }
                    }
                }
                else
                {
                    Debug.Log("Unexpected block ID: " + blockID.ToString("X"));
                    return(false);
                }
            }
            return(true);
        }
        private bool LoadVersion7(GR.Memory.ByteBuffer Data)
        {
            BackgroundColor  = Data.ByteAt(4);
            MultiColor1      = Data.ByteAt(5);
            MultiColor2      = Data.ByteAt(6);
            BackgroundColor4 = Data.ByteAt(7);
            CustomColor      = Data.ByteAt(8);
            TileColorMode    = (ColorMode)Data.ByteAt(9);

            // only uses values 0 to 2, which maps fine
            DisplayModeFile = (DisplayMode)Data.ByteAt(10);

            byte flags          = Data.ByteAt(11);
            bool tileSysEnabled = ((flags & 0x01) != 0);


            ushort charDataBlockID      = 0xdab0;
            ushort charAttributeBlockID = 0xdab1;
            ushort mapDataBlockID       = 0xdab2;


            if (!tileSysEnabled)
            {
                // fake tiles (one per char)
                TileWidth  = 1;
                TileHeight = 1;
                NumTiles   = 256;

                for (int i = 0; i < NumTiles; ++i)
                {
                    Tile tile = new Tile();

                    tile.CharData.Resize((uint)(TileWidth * TileHeight * 2));
                    tile.CharData.SetU16At(0, (ushort)i);

                    tile.ColorData.Resize((uint)(TileWidth * TileHeight));
                    tile.ColorData.SetU8At(0, (byte)CustomColor);

                    Tiles.Add(tile);
                }
            }

            var reader = Data.MemoryReader();

            reader.Skip(12);

            while (reader.DataAvailable)
            {
                ushort blockID = reader.ReadUInt16NetworkOrder();

                if (blockID == mapDataBlockID)
                {
                    if (tileSysEnabled)
                    {
                        // Tile data block

                        // TILECNT: Tile count minus one(16 - bit, LSBF).
                        NumTiles = reader.ReadUInt16() + 1;

                        // TILEWID: Tile width( byte).
                        TileWidth = reader.ReadUInt8();
                        // TILEHEI: Tile height( byte).
                        TileHeight = reader.ReadUInt8();

                        // TILEDAT: Tile data, 16 bits per tile cell( LSBF) for TILEWID* TILEHEI cells * TILECNT items, cells are in LRTB order.
                        for (int i = 0; i < NumTiles; ++i)
                        {
                            Tile tile = new Tile();

                            tile.CharData.Resize((uint)(TileWidth * TileHeight * 2));
                            tile.ColorData.Resize((uint)(TileWidth * TileHeight));

                            Tiles.Add(tile);
                        }
                        if (NumChars < 256)
                        {
                            // add all chars for safety reasons
                            for (int i = NumChars; i < 256; ++i)
                            {
                                SingleChar newChar = new SingleChar();
                                newChar.Data = new GR.Memory.ByteBuffer(8);
                                Characters.Add(newChar);
                            }
                        }
                        for (int i = 0; i < NumTiles; ++i)
                        {
                            for (int j = 0; j < TileWidth * TileHeight; ++j)
                            {
                                Tiles[i].CharData.SetU16At(j * 2, reader.ReadUInt16());
                            }
                        }
                    }
                    else
                    {
                        // BLKMARK : Block marker (0xDA, 0xBn).
                        // MAPWID: Map Width(16 - bit, LSBF).
                        // MAPHEI: Map height(16 - bit, LSBF).
                        // MAPDAT: Map data, 16 bits per cell( LSBF ) for MAPWID* MAPHEI cells, cells are in LRTB order.
                        MapWidth  = reader.ReadUInt16();
                        MapHeight = reader.ReadUInt16();

                        MapData = new GR.Memory.ByteBuffer((uint)(MapWidth * MapHeight));

                        for (int i = 0; i < MapHeight; ++i)
                        {
                            for (int j = 0; j < MapWidth; ++j)
                            {
                                MapData.SetU8At(i * MapWidth + j, (byte)reader.ReadUInt16());
                            }
                        }
                    }
                }

                if (blockID == charDataBlockID)
                {
                    // Character data block

                    // CHARCNT: Character image count minus one( 16 - bit, LSBF ).
                    NumChars = reader.ReadUInt16() + 1;

                    // CHARDAT : Character image data( eight bytes / rows per image for CHARCNT images, rows are in TB order ).
                    for (int charIndex = 0; charIndex < NumChars; ++charIndex)
                    {
                        SingleChar newChar = new SingleChar();
                        newChar.Data  = new GR.Memory.ByteBuffer();
                        newChar.Color = CustomColor;
                        reader.ReadBlock(newChar.Data, 8);
                        Characters.Add(newChar);
                    }

                    if (!tileSysEnabled)
                    {
                        if (NumChars < 256)
                        {
                            // add all chars for safety reasons
                            for (int i = NumChars; i < 256; ++i)
                            {
                                SingleChar newChar = new SingleChar();
                                newChar.Data  = new GR.Memory.ByteBuffer(8);
                                newChar.Color = CustomColor;

                                Characters.Add(newChar);
                            }
                        }
                    }
                }
                else if (blockID == charAttributeBlockID)
                {
                    // char attributes
                    // BLKMARK: Block marker(0xDA, 0xB1).
                    // CHARATTS: Char attribute data, one byte per char image for CHARCNT images, low nybble = colour, high nybble = material.
                    //           nb.colours are only stored when the colouring mode is "per character".
                    for (int charIndex = 0; charIndex < NumChars; ++charIndex)
                    {
                        if (TileColorMode == ColorMode.PER_CHAR)
                        {
                            Characters[charIndex].Color = reader.ReadUInt8() & 0x0f;
                            if (!tileSysEnabled)
                            {
                                Tiles[charIndex].ColorData.SetU8At(0, (byte)Characters[charIndex].Color);
                            }
                        }
                    }
                }
            }
            return(true);
        }
        public bool LoadFromFile(string Filename)
        {
            Elements.Clear();
            ObjectTemplates.Clear();
            this.Screens.Clear();
            Regions.Clear();

            GR.Memory.ByteBuffer editorFile = GR.IO.File.ReadAllBytes(Filename);
            if (editorFile == null)
            {
                return(false);
            }

            GR.IO.MemoryReader memIn = editorFile.MemoryReader();

            GR.IO.FileChunk chunk = new GR.IO.FileChunk();

            while (chunk.ReadFromStream(memIn))
            {
                GR.IO.MemoryReader memReader = chunk.MemoryReader();
                switch (chunk.Type)
                {
                case (ushort)ChunkType.GENERAL:
                    // general
                {
                    OldCharsetProjectFilename = memReader.ReadString();
                    ExportFilename            = memReader.ReadString();
                    ExportPrefix          = memReader.ReadString();
                    ExportConstantOffset  = memReader.ReadInt32();
                    SpriteProjectFilename = memReader.ReadString();
                    ProjectType           = memReader.ReadString();
                    EmptyChar             = memReader.ReadUInt8();
                    EmptyColor            = memReader.ReadUInt8();
                }
                break;

                case (ushort)ChunkType.CHARSET_INFO:
                {
                    Types.CharsetProjectInfo info = new ElementEditor.Types.CharsetProjectInfo();

                    info.Filename   = memReader.ReadString();
                    info.Multicolor = (memReader.ReadUInt8() == 1);
                    CharsetProjects.Add(info);
                }
                break;

                case (ushort)ChunkType.ELEMENT:
                    // element
                {
                    Element element = new Element();

                    element.Name  = memReader.ReadString();
                    element.Index = memReader.ReadInt32();

                    int w = memReader.ReadInt32();
                    int h = memReader.ReadInt32();

                    element.Characters.Resize(w, h);
                    for (int i = 0; i < w; ++i)
                    {
                        for (int j = 0; j < h; ++j)
                        {
                            element.Characters[i, j].Char  = memReader.ReadUInt8();
                            element.Characters[i, j].Color = memReader.ReadUInt8();
                        }
                    }
                    element.CharsetIndex = memReader.ReadInt32();
                    //element.CharsetIndex = 0;
                    if ((CharsetProjects.Count == 0) &&
                        (element.CharsetIndex != 0))
                    {
                        element.CharsetIndex = 0;
                    }
                    else if (element.CharsetIndex >= CharsetProjects.Count)
                    {
                        element.CharsetIndex = 0;
                    }
                    Elements.Add(element);
                }
                break;

                case (ushort)ChunkType.OLD_SCREEN:
                    // screen
                {
                    Screen screen = new Screen();

                    screen.Name = memReader.ReadString();
                    int numElements = memReader.ReadInt32();

                    for (int i = 0; i < numElements; ++i)
                    {
                        ScreenElement element = new ScreenElement();
                        element.Type = (ScreenElementType)memReader.ReadInt32();
                        int elementIndex = memReader.ReadInt32();
                        if (elementIndex >= Elements.Count)
                        {
                            elementIndex = 0;
                        }

                        element.Index             = elementIndex;
                        element.X                 = memReader.ReadInt32();
                        element.Y                 = memReader.ReadInt32();
                        element.Repeats           = memReader.ReadInt32();
                        element.Char              = memReader.ReadInt32();
                        element.Color             = memReader.ReadInt32();
                        element.SearchObjectIndex = memReader.ReadInt32();

                        screen.DisplayedElements.Add(element);
                    }
                    // fetch extra object data
                    foreach (ScreenElement element in screen.DisplayedElements)
                    {
                        if (element.Type == ScreenElementType.LD_OBJECT)
                        {
                            GR.IO.FileChunk chunkObject = new GR.IO.FileChunk();
                            if (!chunkObject.ReadFromStream(memReader))
                            {
                                return(false);
                            }
                            element.Object = new GameObject();

                            GR.IO.MemoryReader memObjectReader = chunkObject.MemoryReader();

                            element.Object.TemplateIndex    = memObjectReader.ReadInt32();
                            element.Object.X                = memObjectReader.ReadInt32();
                            element.Object.Y                = memObjectReader.ReadInt32();
                            element.Object.Color            = memObjectReader.ReadInt32();
                            element.Object.Speed            = memObjectReader.ReadInt32();
                            element.Object.Behaviour        = memObjectReader.ReadInt32();
                            element.Object.MoveBorderLeft   = memObjectReader.ReadInt32();
                            element.Object.MoveBorderTop    = memObjectReader.ReadInt32();
                            element.Object.MoveBorderRight  = memObjectReader.ReadInt32();
                            element.Object.MoveBorderBottom = memObjectReader.ReadInt32();
                            element.Object.Data             = memObjectReader.ReadInt32();
                        }
                        else if (element.Type == ScreenElementType.LD_SPAWN_SPOT)
                        {
                            GR.IO.FileChunk chunkSpawn = new GR.IO.FileChunk();
                            if (!chunkSpawn.ReadFromStream(memReader))
                            {
                                return(false);
                            }
                            element.Object = new GameObject();

                            GR.IO.MemoryReader memObjectReader = chunkSpawn.MemoryReader();

                            element.Object.TemplateIndex = memObjectReader.ReadInt32();
                            element.Object.X             = memObjectReader.ReadInt32();
                            element.Object.Y             = memObjectReader.ReadInt32();
                            element.Repeats = memObjectReader.ReadInt32();
                        }
                    }
                    GR.IO.FileChunk chunkMoreData = new GR.IO.FileChunk();
                    while (chunkMoreData.ReadFromStream(memReader))
                    {
                        GR.IO.MemoryReader moreDataReader = chunkMoreData.MemoryReader();

                        switch (chunkMoreData.Type)
                        {
                        case (ushort)ChunkType.OLD_SCREEN_MORE_DATA:
                            screen.Width = moreDataReader.ReadInt32();
                            break;
                        }
                    }

                    Screens.Add(screen);
                }
                break;

                case (ushort)ChunkType.MAP_REGION:
                {
                    Region region = new Region();

                    GR.IO.FileChunk subChunk = new GR.IO.FileChunk();

                    while (subChunk.ReadFromStream(memReader))
                    {
                        GR.IO.MemoryReader subReader = subChunk.MemoryReader();
                        switch (subChunk.Type)
                        {
                        case (ushort)ChunkType.MAP_REGION_INFO:
                            region.DisplayX = subReader.ReadInt32();
                            region.DisplayY = subReader.ReadInt32();
                            region.Vertical = (subReader.ReadInt32() != 0);
                            {
                                uint numExtraDataBytes = subReader.ReadUInt32();
                                region.ExtraData = new GR.Memory.ByteBuffer();
                                subReader.ReadBlock(region.ExtraData, numExtraDataBytes);
                            }
                            break;

                        case (ushort)ChunkType.MAP_REGION_SCREEN:
                        {
                            RegionScreenInfo screenInfo = new RegionScreenInfo();

                            screenInfo.ScreenIndex = subReader.ReadInt32();
                            screenInfo.ExitN       = (subReader.ReadInt32() != 0);
                            screenInfo.ExitS       = (subReader.ReadInt32() != 0);
                            screenInfo.ExitW       = (subReader.ReadInt32() != 0);
                            screenInfo.ExitE       = (subReader.ReadInt32() != 0);

                            region.Screens.Add(screenInfo);
                        }
                        break;
                        }
                    }
                    Regions.Add(region);
                }
                break;

                case (ushort)ChunkType.SCREEN:
                    // screen
                {
                    Screen screen = new Screen();

                    GR.IO.FileChunk subChunk = new GR.IO.FileChunk();

                    while (subChunk.ReadFromStream(memReader))
                    {
                        GR.IO.MemoryReader subReader = subChunk.MemoryReader();
                        switch (subChunk.Type)
                        {
                        case (ushort)ChunkType.SCREEN_INFO:
                            screen.Name   = subReader.ReadString();
                            screen.Width  = subReader.ReadInt32();
                            screen.Height = subReader.ReadInt32();
                            if (screen.Height == 0)
                            {
                                screen.Height = 25;
                            }
                            screen.ConfigByte   = subReader.ReadUInt8();
                            screen.CharsetIndex = subReader.ReadInt32();
                            screen.WLConfigByte = subReader.ReadUInt8();
                            screen.ExtraData    = subReader.ReadString();
                            screen.OverrideMC1  = subReader.ReadInt32() - 1;
                            screen.OverrideMC2  = subReader.ReadInt32() - 1;
                            break;

                        case (ushort)ChunkType.SCREEN_ELEMENT:
                        {
                            ScreenElement element = new ScreenElement();
                            element.Type = (ScreenElementType)subReader.ReadInt32();
                            int elementIndex = subReader.ReadInt32();
                            if (elementIndex >= Elements.Count)
                            {
                                elementIndex = 0;
                            }
                            element.Index             = elementIndex;
                            element.X                 = subReader.ReadInt32();
                            element.Y                 = subReader.ReadInt32();
                            element.Repeats           = subReader.ReadInt32();
                            element.Repeats2          = subReader.ReadInt32();
                            element.Char              = subReader.ReadInt32();
                            element.Color             = subReader.ReadInt32();
                            element.SearchObjectIndex = subReader.ReadInt32();
                            element.TargetX           = subReader.ReadInt32();
                            element.TargetY           = subReader.ReadInt32();
                            element.TargetLevel       = subReader.ReadInt32();
                            element.Flags             = subReader.ReadInt32();

                            if ((element.Type == ScreenElementType.LD_OBJECT) ||
                                (element.Type == ScreenElementType.LD_SPAWN_SPOT))
                            {
                                element.Object = new GameObject();
                            }

                            screen.DisplayedElements.Add(element);
                        }
                        break;

                        case (ushort)ChunkType.ELEMENT_OBJECT_DATA:
                        {
                            ScreenElement screenElement = screen.DisplayedElements[subReader.ReadInt32()];
                            if (screenElement.Type == ScreenElementType.LD_OBJECT)
                            {
                                screenElement.Object.TemplateIndex    = subReader.ReadInt32();
                                screenElement.Object.X                = subReader.ReadInt32();
                                screenElement.Object.Y                = subReader.ReadInt32();
                                screenElement.Object.Color            = subReader.ReadInt32();
                                screenElement.Object.Speed            = subReader.ReadInt32();
                                screenElement.Object.Behaviour        = subReader.ReadInt32();
                                screenElement.Object.MoveBorderLeft   = subReader.ReadInt32();
                                screenElement.Object.MoveBorderTop    = subReader.ReadInt32();
                                screenElement.Object.MoveBorderRight  = subReader.ReadInt32();
                                screenElement.Object.MoveBorderBottom = subReader.ReadInt32();
                                screenElement.Object.Data             = subReader.ReadInt32();
                                screenElement.Object.OptionalValue    = subReader.ReadInt32();
                                screenElement.Object.Optional         = (GameObject.OptionalType)subReader.ReadInt32();
                            }
                        }
                        break;

                        case (ushort)ChunkType.ELEMENT_SPAWN_SPOT_DATA:
                        {
                            ScreenElement screenElement = screen.DisplayedElements[subReader.ReadInt32()];

                            screenElement.Object.TemplateIndex = subReader.ReadInt32();
                            screenElement.Object.X             = subReader.ReadInt32();
                            screenElement.Object.Y             = subReader.ReadInt32();
                            screenElement.Repeats = subReader.ReadInt32();
                        }
                        break;
                        }
                    }
                    // fetch extra object data
                    Screens.Add(screen);
                }
                break;

                case (ushort)ChunkType.OBJECT_TEMPLATE:
                {
                    ObjectTemplate obj = new ObjectTemplate();

                    obj.Name        = memReader.ReadString();
                    obj.StartSprite = memReader.ReadInt32();

                    int countBehaviours = memReader.ReadInt32();
                    for (int i = 0; i < countBehaviours; ++i)
                    {
                        int    behaviourIndex = memReader.ReadInt32();
                        string behaviourName  = memReader.ReadString();

                        int newIndex = obj.Behaviours.Count;
                        obj.Behaviours[newIndex]       = new Behaviour();
                        obj.Behaviours[newIndex].Name  = behaviourName;
                        obj.Behaviours[newIndex].Value = behaviourIndex;
                    }

                    ObjectTemplates.Add(obj);
                }
                break;
                }
            }
            return(true);
        }