| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2023 - 2025 * |
| 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 <algorithm> |
| 22 | #include <cassert> |
| 23 | #include <cstdint> |
| 24 | #include <cstdlib> |
| 25 | #include <cstring> |
| 26 | #include <filesystem> |
| 27 | #include <fstream> |
| 28 | #include <functional> |
| 29 | #include <iostream> |
| 30 | #include <optional> |
| 31 | #include <set> |
| 32 | #include <string> |
| 33 | #include <string_view> |
| 34 | #include <system_error> |
| 35 | #include <type_traits> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include "h2d_file.h" |
| 39 | #include "image.h" |
| 40 | #include "image_palette.h" |
| 41 | #include "image_tool.h" |
| 42 | #include "serialize.h" |
| 43 | #include "system.h" |
| 44 | #include "tools.h" |
| 45 | |
| 46 | namespace |
| 47 | { |
| 48 | constexpr size_t validPaletteSize = 768; |
| 49 | constexpr uint8_t imageBackground = 142; |
| 50 | |
| 51 | bool isH2DImageItem( const std::string_view name ) |
| 52 | { |
| 53 | return std::filesystem::path( name ).extension() == ".image"; |
| 54 | } |
| 55 | |
| 56 | bool isImageFile( const std::string_view fileName ) |
| 57 | { |
| 58 | const std::string extension = StringLower( std::filesystem::path( fileName ).extension().string() ); |
| 59 | |
| 60 | return ( extension == ".bmp" || extension == ".png" ); |
| 61 | } |
| 62 | |
| 63 | void printUsage( char ** argv ) |
| 64 | { |
| 65 | const std::string toolName = System::GetFileName( argv[0] ); |
| 66 | |
| 67 | std::cerr << toolName << " manages the contents of the specified H2D file(s)." << std::endl |
| 68 | << "Syntax: " << toolName << " extract dst_dir palette_file.pal input_file.h2d ..." << std::endl |
| 69 | << " " << toolName << " combine target_file.h2d palette_file.pal input_file ..." << std::endl; |
| 70 | } |
| 71 | |
| 72 | bool loadPalette( const char * paletteFileName ) |
| 73 | { |
| 74 | StreamFile paletteStream; |
| 75 | if ( !paletteStream.open( paletteFileName, "rb" ) ) { |
| 76 | std::cerr << "Cannot open file " << paletteFileName << std::endl; |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | const std::vector<uint8_t> palette = paletteStream.getRaw( 0 ); |
| 81 | if ( palette.size() != validPaletteSize ) { |
| 82 | std::cerr << "Invalid palette size of " << palette.size() << " instead of " << validPaletteSize << std::endl; |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | fheroes2::setGamePalette( palette ); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | int extractH2D( const int argc, char ** argv ) |
| 91 | { |
| 92 | assert( argc >= 5 ); |
| 93 | |
| 94 | const char * dstDir = argv[2]; |
| 95 | |
| 96 | if ( !loadPalette( argv[3] ) ) { |
| 97 | return EXIT_FAILURE; |
| 98 | } |
| 99 | |
| 100 | std::vector<std::string> inputFileNames; |
| 101 | for ( int i = 4; i < argc; ++i ) { |
| 102 | if ( System::isShellLevelGlobbingSupported() ) { |
| 103 | inputFileNames.emplace_back( argv[i] ); |
| 104 | } |
| 105 | else { |
| 106 | System::globFiles( argv[i], inputFileNames ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | uint32_t itemsExtracted = 0; |
| 111 | |
| 112 | for ( const std::string & inputFileName : inputFileNames ) { |
| 113 | std::cout << "Processing " << inputFileName << "..." << std::endl; |
| 114 | |
| 115 | fheroes2::H2DReader reader; |
| 116 | if ( !reader.open( inputFileName ) ) { |
| 117 | std::cerr << "Cannot open file " << inputFileName << std::endl; |
| 118 | // A non-existent or inaccessible file is not considered a fatal error |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | const std::filesystem::path prefixPath = std::filesystem::path( dstDir ) / std::filesystem::path( inputFileName ).stem(); |
| 123 | |
| 124 | std::error_code ec; |
| 125 | |
| 126 | // Using the non-throwing overloads |
| 127 | if ( !std::filesystem::exists( prefixPath, ec ) && !std::filesystem::create_directories( prefixPath, ec ) ) { |
| 128 | std::cerr << "Cannot create directory " << prefixPath << std::endl; |
| 129 | return EXIT_FAILURE; |
| 130 | } |
| 131 | |
| 132 | for ( const std::string & name : reader.getAllFileNames() ) { |
| 133 | // Image items need special processing |
| 134 | if ( isH2DImageItem( name ) ) { |
| 135 | fheroes2::Sprite image; |
| 136 | |
| 137 | if ( !fheroes2::readImageFromH2D( reader, name, image ) ) { |
| 138 | std::cerr << inputFileName << ": item " << name << " contains an invalid image" << std::endl; |
| 139 | return EXIT_FAILURE; |
| 140 | } |
| 141 | |
| 142 | std::string outputFileName = ( prefixPath / std::filesystem::path( name ).stem() ).string(); |
| 143 | |
| 144 | if ( fheroes2::isPNGFormatSupported() ) { |
| 145 | outputFileName += ".png"; |
| 146 | } |
| 147 | else { |
| 148 | outputFileName += ".bmp"; |
| 149 | } |
| 150 | |
| 151 | if ( !fheroes2::Save( image, outputFileName, imageBackground ) ) { |
| 152 | std::cerr << inputFileName << ": error saving image " << name << std::endl; |
| 153 | return EXIT_FAILURE; |
| 154 | } |
| 155 | } |
| 156 | else { |
| 157 | static_assert( std::is_same_v<uint8_t, unsigned char> ); |
| 158 | |
| 159 | const std::vector<uint8_t> buf = reader.getFile( name ); |
| 160 | |
| 161 | const std::filesystem::path outputFilePath = prefixPath / std::filesystem::path( name ); |
| 162 | |
| 163 | std::ofstream outputStream( outputFilePath, std::ios_base::binary | std::ios_base::trunc ); |
| 164 | if ( !outputStream ) { |
| 165 | std::cerr << "Cannot open file " << outputFilePath << std::endl; |
| 166 | return EXIT_FAILURE; |
| 167 | } |
| 168 | |
| 169 | const auto streamSize = fheroes2::checkedCast<std::streamsize>( buf.size() ); |
| 170 | if ( !streamSize ) { |
| 171 | std::cerr << inputFileName << ": item " << name << " is too large" << std::endl; |
| 172 | return EXIT_FAILURE; |
| 173 | } |
| 174 | |
| 175 | outputStream.write( reinterpret_cast<const char *>( buf.data() ), streamSize.value() ); |
| 176 | if ( !outputStream ) { |
| 177 | std::cerr << "Error writing to file " << outputFilePath << std::endl; |
| 178 | return EXIT_FAILURE; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | ++itemsExtracted; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | std::cout << "Total extracted items: " << itemsExtracted << std::endl; |
| 187 | |
| 188 | return EXIT_SUCCESS; |
| 189 | } |
| 190 | |
| 191 | int combineH2D( const int argc, char ** argv ) |
| 192 | { |
| 193 | assert( argc >= 5 ); |
| 194 | |
| 195 | const char * h2dFileName = argv[2]; |
| 196 | |
| 197 | if ( !loadPalette( argv[3] ) ) { |
| 198 | return EXIT_FAILURE; |
| 199 | } |
| 200 | |
| 201 | std::vector<std::string> inputFileNames; |
| 202 | for ( int i = 4; i < argc; ++i ) { |
| 203 | if ( System::isShellLevelGlobbingSupported() ) { |
| 204 | inputFileNames.emplace_back( argv[i] ); |
| 205 | } |
| 206 | else { |
| 207 | System::globFiles( argv[i], inputFileNames ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | fheroes2::H2DWriter writer; |
| 212 | |
| 213 | std::error_code ec; |
| 214 | |
| 215 | // Using the non-throwing overload |
| 216 | if ( std::filesystem::exists( h2dFileName, ec ) ) { |
| 217 | const std::filesystem::path h2dFileBackupPath = std::filesystem::path( h2dFileName ).replace_extension( "bak" ); |
| 218 | |
| 219 | // Using the non-throwing overload |
| 220 | if ( !std::filesystem::copy_file( h2dFileName, h2dFileBackupPath, std::filesystem::copy_options::overwrite_existing, ec ) ) { |
| 221 | std::cerr << "Cannot create backup file " << h2dFileBackupPath << std::endl; |
| 222 | return EXIT_FAILURE; |
| 223 | } |
| 224 | |
| 225 | fheroes2::H2DReader reader; |
| 226 | if ( !reader.open( h2dFileName ) ) { |
| 227 | std::cerr << "Cannot open file " << h2dFileName << std::endl; |
| 228 | return EXIT_FAILURE; |
| 229 | } |
| 230 | |
| 231 | if ( !writer.add( reader ) ) { |
| 232 | std::cerr << "Error reading from file " << h2dFileName << std::endl; |
| 233 | return EXIT_FAILURE; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | uint32_t itemsAdded = 0; |
| 238 | |
| 239 | for ( const std::string & inputFileName : inputFileNames ) { |
| 240 | std::cout << "Processing " << inputFileName << "..." << std::endl; |
| 241 | |
| 242 | // Image files need special processing |
| 243 | if ( isImageFile( inputFileName ) ) { |
| 244 | fheroes2::Sprite image; |
| 245 | |
| 246 | if ( !fheroes2::Load( inputFileName, image ) ) { |
| 247 | std::cerr << "Cannot open file " << inputFileName << std::endl; |
| 248 | // A non-existent or inaccessible file is not considered a fatal error |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | if ( image.empty() ) { |
| 253 | std::cerr << "File " << inputFileName << " contains an empty image" << std::endl; |
| 254 | return EXIT_FAILURE; |
| 255 | } |
| 256 | |
| 257 | // TODO: Add ability to set sprite position and ability to determine colors in original image |
| 258 | // that are used for transparency and shadows. |
| 259 | if ( !fheroes2::writeImageToH2D( writer, std::filesystem::path( inputFileName ).filename().replace_extension( "image" ).string(), image ) ) { |
| 260 | std::cerr << "Error adding file " << inputFileName << std::endl; |
| 261 | return EXIT_FAILURE; |
| 262 | } |
| 263 | } |
| 264 | else { |
| 265 | std::ifstream inputStream( inputFileName, std::ios_base::binary | std::ios_base::ate ); |
| 266 | if ( !inputStream ) { |
| 267 | std::cerr << "Cannot open file " << inputFileName << std::endl; |
| 268 | // A non-existent or inaccessible file is not considered a fatal error |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | const auto size = fheroes2::checkedCast<size_t>( static_cast<std::streamoff>( inputStream.tellg() ) ); |
| 273 | if ( !size ) { |
| 274 | std::cerr << "File " << inputFileName << " is too large" << std::endl; |
| 275 | return EXIT_FAILURE; |
| 276 | } |
| 277 | |
| 278 | if ( size == 0U ) { |
| 279 | std::cerr << "File " << inputFileName << " is empty" << std::endl; |
| 280 | return EXIT_FAILURE; |
| 281 | } |
| 282 | |
| 283 | static_assert( std::is_same_v<uint8_t, unsigned char> ); |
| 284 | |
| 285 | std::vector<uint8_t> buf( size.value() ); |
| 286 | |
| 287 | inputStream.seekg( 0, std::ios_base::beg ); |
| 288 | |
| 289 | const auto streamSize = fheroes2::checkedCast<std::streamsize>( buf.size() ); |
| 290 | if ( !streamSize ) { |
| 291 | std::cerr << "File " << inputFileName << " is too large" << std::endl; |
| 292 | return EXIT_FAILURE; |
| 293 | } |
| 294 | |
| 295 | inputStream.read( reinterpret_cast<char *>( buf.data() ), streamSize.value() ); |
| 296 | if ( !inputStream ) { |
| 297 | std::cerr << "Error reading from file " << inputFileName << std::endl; |
| 298 | return EXIT_FAILURE; |
| 299 | } |
| 300 | |
| 301 | if ( !writer.add( std::filesystem::path( inputFileName ).filename().string(), buf ) ) { |
| 302 | std::cerr << "Error adding file " << inputFileName << std::endl; |
| 303 | return EXIT_FAILURE; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | ++itemsAdded; |
| 308 | } |
| 309 | |
| 310 | if ( !writer.write( h2dFileName ) ) { |
| 311 | std::cerr << "Error writing to file " << h2dFileName << std::endl; |
| 312 | return EXIT_FAILURE; |
| 313 | } |
| 314 | |
| 315 | std::cout << "Total added items: " << itemsAdded << std::endl; |
| 316 | |
| 317 | return EXIT_SUCCESS; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | int main( int argc, char ** argv ) |
| 322 | { |
| 323 | if ( argc >= 5 && strcmp( argv[1], "extract" ) == 0 ) { |
| 324 | return extractH2D( argc, argv ); |
| 325 | } |
| 326 | |
| 327 | if ( argc >= 5 && strcmp( argv[1], "combine" ) == 0 ) { |
| 328 | return combineH2D( argc, argv ); |
| 329 | } |
| 330 | |
| 331 | printUsage( argv ); |
| 332 | |
| 333 | return EXIT_FAILURE; |
| 334 | } |