| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2022 - 2024 * |
| 4 | * * |
| 5 | * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 * |
| 6 | * Copyright (C) 2009 by Andrey Afletdinov <[email protected]> * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <cstdint> |
| 26 | #include <cstdlib> |
| 27 | #include <filesystem> |
| 28 | #include <fstream> |
| 29 | #include <functional> |
| 30 | #include <iostream> |
| 31 | #include <map> |
| 32 | #include <optional> |
| 33 | #include <string> |
| 34 | #include <system_error> |
| 35 | #include <type_traits> |
| 36 | #include <utility> |
| 37 | #include <vector> |
| 38 | |
| 39 | #include "agg_file.h" |
| 40 | #include "serialize.h" |
| 41 | #include "system.h" |
| 42 | #include "tools.h" |
| 43 | |
| 44 | namespace |
| 45 | { |
| 46 | constexpr size_t AGGItemNameLen = 15; |
| 47 | |
| 48 | struct AGGItemInfo |
| 49 | { |
| 50 | // Hash of this item's name, see fheroes2::calculateAggFilenameHash() for details |
| 51 | uint32_t hash; |
| 52 | uint32_t offset; |
| 53 | uint32_t size; |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | int main( int argc, char ** argv ) |
| 58 | { |
| 59 | if ( argc < 3 ) { |
| 60 | const std::string toolName = System::GetFileName( argv[0] ); |
| 61 | |
| 62 | std::cerr << toolName << " extracts the contents of the specified AGG file(s)." << std::endl |
| 63 | << "Syntax: " << toolName << " dst_dir input_file.agg ..." << std::endl; |
| 64 | return EXIT_FAILURE; |
| 65 | } |
| 66 | |
| 67 | const char * dstDir = argv[1]; |
| 68 | |
| 69 | std::vector<std::string> inputFileNames; |
| 70 | for ( int i = 2; i < argc; ++i ) { |
| 71 | if ( System::isShellLevelGlobbingSupported() ) { |
| 72 | inputFileNames.emplace_back( argv[i] ); |
| 73 | } |
| 74 | else { |
| 75 | System::globFiles( argv[i], inputFileNames ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | uint32_t itemsExtracted = 0; |
| 80 | uint32_t itemsFailed = 0; |
| 81 | |
| 82 | for ( const std::string & inputFileName : inputFileNames ) { |
| 83 | std::cout << "Processing " << inputFileName << "..." << std::endl; |
| 84 | |
| 85 | StreamFile inputStream; |
| 86 | if ( !inputStream.open( inputFileName, "rb" ) ) { |
| 87 | std::cerr << "Cannot open file " << inputFileName << std::endl; |
| 88 | // A non-existent or inaccessible file is not considered a fatal error |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | const std::filesystem::path prefixPath = std::filesystem::path( dstDir ) / std::filesystem::path( inputFileName ).stem(); |
| 93 | |
| 94 | std::error_code ec; |
| 95 | |
| 96 | // Using the non-throwing overloads |
| 97 | if ( !std::filesystem::exists( prefixPath, ec ) && !std::filesystem::create_directories( prefixPath, ec ) ) { |
| 98 | std::cerr << "Cannot create directory " << prefixPath << std::endl; |
| 99 | return EXIT_FAILURE; |
| 100 | } |
| 101 | |
| 102 | const size_t inputStreamSize = inputStream.size(); |
| 103 | const uint16_t itemsCount = inputStream.getLE16(); |
| 104 | |
| 105 | ROStreamBuf itemsStream = inputStream.getStreamBuf( static_cast<size_t>( itemsCount ) * 4 * 3 /* hash, offset, size */ ); |
| 106 | inputStream.seek( inputStreamSize - AGGItemNameLen * itemsCount ); |
| 107 | ROStreamBuf namesStream = inputStream.getStreamBuf( AGGItemNameLen * itemsCount ); |
| 108 | |
| 109 | std::map<std::string, AGGItemInfo, std::less<>> aggItemsMap; |
| 110 | |
| 111 | for ( uint16_t i = 0; i < itemsCount; ++i ) { |
| 112 | AGGItemInfo & info = aggItemsMap[StringLower( namesStream.getString( AGGItemNameLen ) )]; |
| 113 | |
| 114 | info.hash = itemsStream.getLE32(); |
| 115 | info.offset = itemsStream.getLE32(); |
| 116 | info.size = itemsStream.getLE32(); |
| 117 | } |
| 118 | |
| 119 | for ( const auto & item : aggItemsMap ) { |
| 120 | const auto & [name, info] = item; |
| 121 | |
| 122 | if ( info.size == 0 ) { |
| 123 | ++itemsFailed; |
| 124 | |
| 125 | std::cerr << inputFileName << ": item " << name << " is empty" << std::endl; |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | const uint32_t hash = fheroes2::calculateAggFilenameHash( name ); |
| 130 | if ( hash != info.hash ) { |
| 131 | ++itemsFailed; |
| 132 | |
| 133 | std::cerr << inputFileName << ": invalid hash for item " << name << ": expected " << GetHexString( info.hash ) << ", got " << GetHexString( hash ) |
| 134 | << std::endl; |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | inputStream.seek( info.offset ); |
| 139 | |
| 140 | static_assert( std::is_same_v<uint8_t, unsigned char> ); |
| 141 | |
| 142 | const std::vector<uint8_t> buf = inputStream.getRaw( info.size ); |
| 143 | if ( buf.size() != info.size ) { |
| 144 | ++itemsFailed; |
| 145 | |
| 146 | std::cerr << inputFileName << ": item " << name << " has an invalid size of " << info.size << std::endl; |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | const std::filesystem::path outputFilePath = prefixPath / std::filesystem::path( name ); |
| 151 | |
| 152 | std::ofstream outputStream( outputFilePath, std::ios_base::binary | std::ios_base::trunc ); |
| 153 | if ( !outputStream ) { |
| 154 | std::cerr << "Cannot open file " << outputFilePath << std::endl; |
| 155 | return EXIT_FAILURE; |
| 156 | } |
| 157 | |
| 158 | { |
| 159 | const auto streamSize = fheroes2::checkedCast<std::streamsize>( buf.size() ); |
| 160 | if ( !streamSize ) { |
| 161 | std::cerr << inputFileName << ": item " << name << " is too large" << std::endl; |
| 162 | return EXIT_FAILURE; |
| 163 | } |
| 164 | |
| 165 | outputStream.write( reinterpret_cast<const char *>( buf.data() ), streamSize.value() ); |
| 166 | } |
| 167 | |
| 168 | if ( !outputStream ) { |
| 169 | std::cerr << "Error writing to file " << outputFilePath << std::endl; |
| 170 | return EXIT_FAILURE; |
| 171 | } |
| 172 | |
| 173 | ++itemsExtracted; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | std::cout << "Total extracted items: " << itemsExtracted << ", failed items: " << itemsFailed << std::endl; |
| 178 | |
| 179 | return ( itemsFailed == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 180 | } |