/*************************************************************************** * fheroes2: https://github.com/ihhub/fheroes2 * * Copyright (C) 2021 - 2026 * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "h2d_file.h" #include #include #include #include #include "image.h" #include "zzlib.h" namespace { // 4 bytes - file identifier // 4 bytes - number of files // 4 bytes - file offset // 4 bytes - file size // 5 bytes - file name const size_t minFileSize = 4 + 4 + 4 + 4 + 5 + 1; const uint8_t version{ 2U }; const std::array magicSequence{ 'H', '2', 'D', version }; } namespace fheroes2 { bool H2DReader::open( const std::string & path ) { _fileNameAndOffset.clear(); _fileStream.close(); if ( !_fileStream.open( path, "rb" ) ) { return false; } const size_t fileSize = _fileStream.size(); if ( fileSize < minFileSize ) { return false; } for ( const uint8_t value : magicSequence ) { if ( _fileStream.get() != value ) { return false; } } const uint32_t fileCount = _fileStream.getLE32(); if ( fileCount == 0 ) { return false; } for ( uint32_t i = 0; i < fileCount; ++i ) { const uint32_t offset = _fileStream.getLE32(); const uint32_t size = _fileStream.getLE32(); std::string name; _fileStream >> name; if ( size == 0 || static_cast( offset ) + size > fileSize || name.empty() ) { continue; } _fileNameAndOffset.try_emplace( std::move( name ), std::make_pair( offset, size ) ); } return true; } std::vector H2DReader::getFile( const std::string & fileName ) { const auto it = _fileNameAndOffset.find( fileName ); if ( it == _fileNameAndOffset.end() ) { return {}; } _fileStream.seek( it->second.first ); const auto compressedData = _fileStream.getRaw( it->second.second ); return Compression::unzipData( compressedData.data(), compressedData.size() ); } std::set> H2DReader::getAllFileNames() const { std::set> names; for ( const auto & [name, offset] : _fileNameAndOffset ) { names.insert( name ); } return names; } bool H2DWriter::write( const std::string & path ) const { if ( _fileData.empty() ) { // Nothing to write. return false; } StreamFile fileStream; if ( !fileStream.open( path, "wb" ) ) { return false; } for ( const uint8_t value : magicSequence ) { fileStream.put( value ); } fileStream.putLE32( static_cast( _fileData.size() ) ); // Calculate file info section size. size_t fileInfoSection = ( 4 + 4 ) * _fileData.size(); for ( const auto & [name, data] : _fileData ) { // 4 byte for string size. fileInfoSection += ( name.size() + 4 ); } size_t offset = fileInfoSection + 4 + 4; for ( const auto & [name, data] : _fileData ) { fileStream.putLE32( static_cast( offset ) ); fileStream.putLE32( static_cast( data.size() ) ); fileStream << name; offset += data.size(); } for ( const auto & [name, data] : _fileData ) { fileStream.putRaw( data.data(), data.size() ); } return true; } bool H2DWriter::add( const std::string & name, const std::vector & data ) { if ( name.empty() || data.empty() ) { return false; } _fileData[name] = Compression::zipData( data.data(), data.size(), true ); return true; } bool H2DWriter::add( H2DReader & reader ) { const std::set> names = reader.getAllFileNames(); for ( const std::string & name : names ) { if ( !add( name, reader.getFile( name ) ) ) { return false; } } return true; } bool readImageFromH2D( H2DReader & reader, const std::string & name, Sprite & image ) { const size_t imageInfoLength{ 4 + 4 + 4 + 4 + 1 }; const std::vector & data = reader.getFile( name ); if ( data.size() < imageInfoLength + 1 ) { // Empty or invalid image. return false; } ROStreamBuf stream( data ); const int32_t width = static_cast( stream.getLE32() ); const int32_t height = static_cast( stream.getLE32() ); const int32_t x = static_cast( stream.getLE32() ); const int32_t y = static_cast( stream.getLE32() ); const bool isSingleLayer = ( stream.get() != 0 ); const size_t size = static_cast( width ) * static_cast( height ); if ( ( size * ( isSingleLayer ? 1 : 2 ) + imageInfoLength ) != data.size() ) { return false; } if ( isSingleLayer ) { image._disableTransformLayer(); } image.resize( width, height ); memcpy( image.image(), data.data() + imageInfoLength, size ); if ( !isSingleLayer ) { memcpy( image.transform(), data.data() + imageInfoLength + size, size ); } image.setPosition( x, y ); return true; } bool writeImageToH2D( H2DWriter & writer, const std::string & name, const Sprite & image ) { assert( !image.empty() ); RWStreamBuf stream; stream.putLE32( static_cast( image.width() ) ); stream.putLE32( static_cast( image.height() ) ); stream.putLE32( static_cast( image.x() ) ); stream.putLE32( static_cast( image.y() ) ); stream.put( image.singleLayer() ); const size_t imageSize = static_cast( image.width() ) * static_cast( image.height() ); stream.putRaw( image.image(), imageSize ); if ( !image.singleLayer() ) { stream.putRaw( image.transform(), imageSize ); } return writer.add( name, stream.getRaw( 0 ) ); } }