| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2021 - 2026 * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "h2d_file.h" |
| 22 | |
| 23 | #include <array> |
| 24 | #include <cassert> |
| 25 | #include <cstdint> |
| 26 | #include <cstring> |
| 27 | |
| 28 | #include "image.h" |
| 29 | #include "zzlib.h" |
| 30 | |
| 31 | namespace |
| 32 | { |
| 33 | // 4 bytes - file identifier |
| 34 | // 4 bytes - number of files |
| 35 | // 4 bytes - file offset |
| 36 | // 4 bytes - file size |
| 37 | // 5 bytes - file name |
| 38 | const size_t minFileSize = 4 + 4 + 4 + 4 + 5 + 1; |
| 39 | |
| 40 | const uint8_t version{ 2U }; |
| 41 | const std::array<uint8_t, 4> magicSequence{ 'H', '2', 'D', version }; |
| 42 | } |
| 43 | |
| 44 | namespace fheroes2 |
| 45 | { |
| 46 | bool H2DReader::open( const std::string & path ) |
| 47 | { |
| 48 | _fileNameAndOffset.clear(); |
| 49 | _fileStream.close(); |
| 50 | |
| 51 | if ( !_fileStream.open( path, "rb" ) ) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | const size_t fileSize = _fileStream.size(); |
| 56 | if ( fileSize < minFileSize ) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | for ( const uint8_t value : magicSequence ) { |
| 61 | if ( _fileStream.get() != value ) { |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const uint32_t fileCount = _fileStream.getLE32(); |
| 67 | if ( fileCount == 0 ) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | for ( uint32_t i = 0; i < fileCount; ++i ) { |
| 72 | const uint32_t offset = _fileStream.getLE32(); |
| 73 | const uint32_t size = _fileStream.getLE32(); |
| 74 | std::string name; |
| 75 | _fileStream >> name; |
| 76 | if ( size == 0 || static_cast<size_t>( offset ) + size > fileSize || name.empty() ) { |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | _fileNameAndOffset.try_emplace( std::move( name ), std::make_pair( offset, size ) ); |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | std::vector<uint8_t> H2DReader::getFile( const std::string & fileName ) |
| 87 | { |
| 88 | const auto it = _fileNameAndOffset.find( fileName ); |
| 89 | if ( it == _fileNameAndOffset.end() ) { |
| 90 | return {}; |
| 91 | } |
| 92 | |
| 93 | _fileStream.seek( it->second.first ); |
| 94 | const auto compressedData = _fileStream.getRaw( it->second.second ); |
| 95 | |
| 96 | return Compression::unzipData( compressedData.data(), compressedData.size() ); |
| 97 | } |
| 98 | |
| 99 | std::set<std::string, std::less<>> H2DReader::getAllFileNames() const |
| 100 | { |
| 101 | std::set<std::string, std::less<>> names; |
| 102 | |
| 103 | for ( const auto & [name, offset] : _fileNameAndOffset ) { |
| 104 | names.insert( name ); |
| 105 | } |
| 106 | |
| 107 | return names; |
| 108 | } |
| 109 | |
| 110 | bool H2DWriter::write( const std::string & path ) const |
| 111 | { |
| 112 | if ( _fileData.empty() ) { |
| 113 | // Nothing to write. |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | StreamFile fileStream; |
| 118 | if ( !fileStream.open( path, "wb" ) ) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | for ( const uint8_t value : magicSequence ) { |
| 123 | fileStream.put( value ); |
| 124 | } |
| 125 | |
| 126 | fileStream.putLE32( static_cast<uint32_t>( _fileData.size() ) ); |
| 127 | |
| 128 | // Calculate file info section size. |
| 129 | size_t fileInfoSection = ( 4 + 4 ) * _fileData.size(); |
| 130 | for ( const auto & [name, data] : _fileData ) { |
| 131 | // 4 byte for string size. |
| 132 | fileInfoSection += ( name.size() + 4 ); |
| 133 | } |
| 134 | |
| 135 | size_t offset = fileInfoSection + 4 + 4; |
| 136 | for ( const auto & [name, data] : _fileData ) { |
| 137 | fileStream.putLE32( static_cast<uint32_t>( offset ) ); |
| 138 | fileStream.putLE32( static_cast<uint32_t>( data.size() ) ); |
| 139 | fileStream << name; |
| 140 | offset += data.size(); |
| 141 | } |
| 142 | |
| 143 | for ( const auto & [name, data] : _fileData ) { |
| 144 | fileStream.putRaw( data.data(), data.size() ); |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool H2DWriter::add( const std::string & name, const std::vector<uint8_t> & data ) |
| 151 | { |
| 152 | if ( name.empty() || data.empty() ) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | _fileData[name] = Compression::zipData( data.data(), data.size(), true ); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | bool H2DWriter::add( H2DReader & reader ) |
| 161 | { |
| 162 | const std::set<std::string, std::less<>> names = reader.getAllFileNames(); |
| 163 | |
| 164 | for ( const std::string & name : names ) { |
| 165 | if ( !add( name, reader.getFile( name ) ) ) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | bool readImageFromH2D( H2DReader & reader, const std::string & name, Sprite & image ) |
| 174 | { |
| 175 | const size_t imageInfoLength{ 4 + 4 + 4 + 4 + 1 }; |
| 176 | |
| 177 | const std::vector<uint8_t> & data = reader.getFile( name ); |
| 178 | if ( data.size() < imageInfoLength + 1 ) { |
| 179 | // Empty or invalid image. |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | ROStreamBuf stream( data ); |
| 184 | const int32_t width = static_cast<int32_t>( stream.getLE32() ); |
| 185 | const int32_t height = static_cast<int32_t>( stream.getLE32() ); |
| 186 | const int32_t x = static_cast<int32_t>( stream.getLE32() ); |
| 187 | const int32_t y = static_cast<int32_t>( stream.getLE32() ); |
| 188 | const bool isSingleLayer = ( stream.get() != 0 ); |
| 189 | |
| 190 | const size_t size = static_cast<size_t>( width ) * static_cast<size_t>( height ); |
| 191 | |
| 192 | if ( ( size * ( isSingleLayer ? 1 : 2 ) + imageInfoLength ) != data.size() ) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | if ( isSingleLayer ) { |
| 197 | image._disableTransformLayer(); |
| 198 | } |
| 199 | |
| 200 | image.resize( width, height ); |
| 201 | memcpy( image.image(), data.data() + imageInfoLength, size ); |
| 202 | |
| 203 | if ( !isSingleLayer ) { |
| 204 | memcpy( image.transform(), data.data() + imageInfoLength + size, size ); |
| 205 | } |
| 206 | |
| 207 | image.setPosition( x, y ); |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | bool writeImageToH2D( H2DWriter & writer, const std::string & name, const Sprite & image ) |
| 213 | { |
| 214 | assert( !image.empty() ); |
| 215 | |
| 216 | RWStreamBuf stream; |
| 217 | stream.putLE32( static_cast<uint32_t>( image.width() ) ); |
| 218 | stream.putLE32( static_cast<uint32_t>( image.height() ) ); |
| 219 | stream.putLE32( static_cast<uint32_t>( image.x() ) ); |
| 220 | stream.putLE32( static_cast<uint32_t>( image.y() ) ); |
| 221 | stream.put( image.singleLayer() ); |
| 222 | |
| 223 | const size_t imageSize = static_cast<size_t>( image.width() ) * static_cast<size_t>( image.height() ); |
| 224 | stream.putRaw( image.image(), imageSize ); |
| 225 | if ( !image.singleLayer() ) { |
| 226 | stream.putRaw( image.transform(), imageSize ); |
| 227 | } |
| 228 | |
| 229 | return writer.add( name, stream.getRaw( 0 ) ); |
| 230 | } |
| 231 | } |