コード例 #1
0
        public bool ReadFromBuffer(GR.Memory.ByteBuffer ProjectFile)
        {
            if (ProjectFile == null)
            {
                return(false);
            }

            GR.IO.MemoryReader memReader = new GR.IO.MemoryReader(ProjectFile);

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

            string importedCharSet = "";

            while (chunk.ReadFromStream(memReader))
            {
                GR.IO.MemoryReader chunkReader = chunk.MemoryReader();
                switch (chunk.Type)
                {
                case FileChunkConstants.MAP_PROJECT_INFO:
                {
                    uint version = chunkReader.ReadUInt32();
                    importedCharSet = chunkReader.ReadString();

                    ShowGrid = (chunkReader.ReadInt32() == 1);
                }
                break;

                case FileChunkConstants.MAP_CHARSET:
                {
                    GR.Memory.ByteBuffer data = new GR.Memory.ByteBuffer();
                    chunkReader.ReadBlock(data, (uint)(chunkReader.Size - chunkReader.Position));

                    Charset.ReadFromBuffer(data);
                }
                break;

                case FileChunkConstants.MAP_PROJECT_DATA:
                {
                    GR.IO.FileChunk chunkData = new GR.IO.FileChunk();

                    while (chunkData.ReadFromStream(chunkReader))
                    {
                        GR.IO.MemoryReader subChunkReader = chunkData.MemoryReader();
                        switch (chunkData.Type)
                        {
                        case FileChunkConstants.MULTICOLOR_DATA:
                            Mode            = (TextMode)subChunkReader.ReadUInt8();
                            BackgroundColor = subChunkReader.ReadUInt8();
                            MultiColor1     = subChunkReader.ReadUInt8();
                            MultiColor2     = subChunkReader.ReadUInt8();
                            BGColor4        = subChunkReader.ReadUInt8();
                            break;

                        case FileChunkConstants.MAP_TILE:
                        {
                            Tile tile = new Tile();
                            tile.Name = subChunkReader.ReadString();

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

                            tile.Chars.Resize(w, h);
                            for (int j = 0; j < tile.Chars.Height; ++j)
                            {
                                for (int i = 0; i < tile.Chars.Width; ++i)
                                {
                                    tile.Chars[i, j].Character = subChunkReader.ReadUInt8();
                                    tile.Chars[i, j].Color     = subChunkReader.ReadUInt8();
                                }
                            }
                            Tiles.Add(tile);
                            tile.Index = Tiles.Count - 1;
                        }
                        break;

                        case FileChunkConstants.MAP:
                        {
                            GR.IO.FileChunk mapChunk = new GR.IO.FileChunk();

                            Map map = new Map();

                            while (mapChunk.ReadFromStream(subChunkReader))
                            {
                                GR.IO.MemoryReader mapChunkReader = mapChunk.MemoryReader();
                                switch (mapChunk.Type)
                                {
                                case FileChunkConstants.MAP_INFO:
                                    map.Name                       = mapChunkReader.ReadString();
                                    map.TileSpacingX               = mapChunkReader.ReadInt32();
                                    map.TileSpacingY               = mapChunkReader.ReadInt32();
                                    map.AlternativeMultiColor1     = mapChunkReader.ReadInt32() - 1;
                                    map.AlternativeMultiColor2     = mapChunkReader.ReadInt32() - 1;
                                    map.AlternativeBackgroundColor = mapChunkReader.ReadInt32() - 1;
                                    map.AlternativeBGColor4        = mapChunkReader.ReadInt32() - 1;
                                    map.AlternativeMode            = (TextCharMode)(mapChunkReader.ReadInt32() - 1);
                                    break;

                                case FileChunkConstants.MAP_DATA:
                                {
                                    int w = mapChunkReader.ReadInt32();
                                    int h = mapChunkReader.ReadInt32();

                                    map.Tiles.Resize(w, h);
                                    for (int j = 0; j < map.Tiles.Height; ++j)
                                    {
                                        for (int i = 0; i < map.Tiles.Width; ++i)
                                        {
                                            map.Tiles[i, j] = mapChunkReader.ReadInt32();
                                        }
                                    }
                                }
                                break;

                                case FileChunkConstants.MAP_EXTRA_DATA:
                                {
                                    uint len = mapChunkReader.ReadUInt32();

                                    mapChunkReader.ReadBlock(map.ExtraDataOld, len);

                                    map.ExtraDataText = map.ExtraDataOld.ToString();
                                    map.ExtraDataOld.Clear();
                                }
                                break;

                                case FileChunkConstants.MAP_EXTRA_DATA_TEXT:
                                {
                                    map.ExtraDataText = mapChunkReader.ReadString();
                                }
                                break;
                                }
                            }

                            Maps.Add(map);
                        }
                        break;
                        }
                    }
                }
                break;
                }
            }
            memReader.Close();


            Charset.Colors.MultiColor1 = MultiColor1;
            Charset.Colors.MultiColor2 = MultiColor2;
            Charset.Colors.BGColor4    = BGColor4;
            return(true);
        }
コード例 #2
0
        public override bool HandleImport(CharsetProject Charset, CharsetEditor Editor)
        {
            string filename;

            //Clear();
            if (Editor.OpenFile("Open charset", RetroDevStudio.Types.Constants.FILEFILTER_CHARSET + RetroDevStudio.Types.Constants.FILEFILTER_CHARSET_CHARPAD + RetroDevStudio.Types.Constants.FILEFILTER_ALL, out filename))
            {
                if (System.IO.Path.GetExtension(filename).ToUpper() == ".CHARSETPROJECT")
                {
                    // a project
                    GR.Memory.ByteBuffer projectFile = GR.IO.File.ReadAllBytes(filename);

                    RetroDevStudio.Formats.CharsetProject project = new RetroDevStudio.Formats.CharsetProject();
                    if (!project.ReadFromBuffer(projectFile))
                    {
                        return(false);
                    }
                    Charset.Colors = new ColorSettings(project.Colors);
                    Charset.ExportNumCharacters = project.ExportNumCharacters;
                    Charset.ShowGrid            = project.ShowGrid;

                    for (int i = 0; i < Charset.TotalNumberOfCharacters; ++i)
                    {
                        Charset.Characters[i].Tile.CustomColor = project.Characters[i].Tile.CustomColor;
                        Charset.Characters[i].Tile.Data        = new GR.Memory.ByteBuffer(project.Characters[i].Tile.Data);
                    }

                    Editor.CharsetWasImported();

                    Editor.SetModified();
                    return(true);
                }
                else if (System.IO.Path.GetExtension(filename).ToUpper() == ".CTM")
                {
                    // a charpad project file
                    GR.Memory.ByteBuffer projectFile = GR.IO.File.ReadAllBytes(filename);

                    Formats.CharpadProject cpProject = new RetroDevStudio.Formats.CharpadProject();
                    if (!cpProject.LoadFromFile(projectFile))
                    {
                        return(false);
                    }

                    Charset.Colors.BackgroundColor = cpProject.BackgroundColor;
                    Charset.Colors.MultiColor1     = cpProject.MultiColor1;
                    Charset.Colors.MultiColor2     = cpProject.MultiColor2;

                    Charset.ExportNumCharacters = cpProject.NumChars;
                    if (Charset.ExportNumCharacters > 256)
                    {
                        Charset.ExportNumCharacters = 256;
                    }
                    for (int charIndex = 0; charIndex < Charset.ExportNumCharacters; ++charIndex)
                    {
                        Charset.Characters[charIndex].Tile.Data        = cpProject.Characters[charIndex].Data;
                        Charset.Characters[charIndex].Tile.CustomColor = cpProject.Characters[charIndex].Color;
                    }

                    Editor.CharsetWasImported();
                    Editor.SetModified();
                    return(true);
                }

                // treat as binary .chr file
                GR.Memory.ByteBuffer charData = GR.IO.File.ReadAllBytes(filename);

                Editor.ImportFromData(charData);
            }
            return(true);
        }