| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2022 - 2024 * |
| 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 <cstdint> |
| 23 | #include <cstdlib> |
| 24 | #include <filesystem> |
| 25 | #include <fstream> |
| 26 | #include <iostream> |
| 27 | #include <optional> |
| 28 | #include <string> |
| 29 | #include <system_error> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "serialize.h" |
| 33 | #include "system.h" |
| 34 | #include "tools.h" |
| 35 | |
| 36 | namespace |
| 37 | { |
| 38 | constexpr size_t correctBINSize = 821; |
| 39 | } |
| 40 | |
| 41 | int main( int argc, char ** argv ) |
| 42 | { |
| 43 | if ( argc < 3 ) { |
| 44 | const std::string toolName = System::GetFileName( argv[0] ); |
| 45 | |
| 46 | std::cerr << toolName << " extracts various data from monster animation files." << std::endl |
| 47 | << "Syntax: " << toolName << " dst_dir input_file.bin ..." << std::endl; |
| 48 | return EXIT_FAILURE; |
| 49 | } |
| 50 | |
| 51 | const char * dstDir = argv[1]; |
| 52 | |
| 53 | std::vector<std::string> inputFileNames; |
| 54 | for ( int i = 2; i < argc; ++i ) { |
| 55 | if ( System::isShellLevelGlobbingSupported() ) { |
| 56 | inputFileNames.emplace_back( argv[i] ); |
| 57 | } |
| 58 | else { |
| 59 | System::globFiles( argv[i], inputFileNames ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | std::error_code ec; |
| 64 | |
| 65 | // Using the non-throwing overloads |
| 66 | if ( !std::filesystem::exists( dstDir, ec ) && !std::filesystem::create_directories( dstDir, ec ) ) { |
| 67 | std::cerr << "Cannot create directory " << dstDir << std::endl; |
| 68 | return EXIT_FAILURE; |
| 69 | } |
| 70 | |
| 71 | uint32_t filesProcessed = 0; |
| 72 | |
| 73 | for ( const std::string & inputFileName : inputFileNames ) { |
| 74 | std::ifstream inputStream( inputFileName, std::ios_base::binary | std::ios_base::ate ); |
| 75 | if ( !inputStream ) { |
| 76 | std::cerr << "Cannot open file " << inputFileName << std::endl; |
| 77 | // A non-existent or inaccessible file is not considered a fatal error |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | const auto size = fheroes2::checkedCast<size_t>( static_cast<std::streamoff>( inputStream.tellg() ) ); |
| 82 | if ( !size ) { |
| 83 | std::cerr << "File " << inputFileName << " is too large" << std::endl; |
| 84 | // Ignore files of invalid size |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | if ( size == 0U ) { |
| 89 | std::cerr << "File " << inputFileName << " is empty" << std::endl; |
| 90 | // Ignore files of invalid size |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | if ( size != correctBINSize ) { |
| 95 | // Ignore files of invalid size |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | std::cout << "Processing " << inputFileName << "..." << std::endl; |
| 100 | |
| 101 | std::vector<char> buf( size.value() ); |
| 102 | |
| 103 | inputStream.seekg( 0, std::ios_base::beg ); |
| 104 | |
| 105 | { |
| 106 | const auto streamSize = fheroes2::checkedCast<std::streamsize>( buf.size() ); |
| 107 | if ( !streamSize ) { |
| 108 | std::cerr << "File " << inputFileName << " is too large" << std::endl; |
| 109 | return EXIT_FAILURE; |
| 110 | } |
| 111 | |
| 112 | inputStream.read( buf.data(), streamSize.value() ); |
| 113 | } |
| 114 | |
| 115 | if ( !inputStream ) { |
| 116 | std::cerr << "Error reading from file " << inputFileName << std::endl; |
| 117 | return EXIT_FAILURE; |
| 118 | } |
| 119 | |
| 120 | const std::filesystem::path outputFilePath = std::filesystem::path( dstDir ) / std::filesystem::path( inputFileName ).filename().replace_extension( "txt" ); |
| 121 | |
| 122 | std::ofstream outputStream( outputFilePath, std::ios_base::trunc ); |
| 123 | if ( !outputStream ) { |
| 124 | std::cerr << "Cannot open file " << outputFilePath << std::endl; |
| 125 | return EXIT_FAILURE; |
| 126 | } |
| 127 | |
| 128 | const char * data = buf.data(); |
| 129 | |
| 130 | outputStream << "Monster eye position: [" << fheroes2::getLEValue<int16_t>( data, 1 ) << ", " << fheroes2::getLEValue<int16_t>( data, 3 ) << "]\n\n"; |
| 131 | |
| 132 | outputStream << "Animation frame offsets:\n"; |
| 133 | for ( size_t setId = 0; setId < 7; ++setId ) { |
| 134 | outputStream << setId + 1 << " : "; |
| 135 | for ( size_t frameId = 0; frameId < 16; ++frameId ) { |
| 136 | const int frameValue = static_cast<unsigned char>( data[5 + setId * 16 + frameId] ); |
| 137 | |
| 138 | if ( frameValue < 10 ) { |
| 139 | outputStream << " " << frameValue << " "; |
| 140 | } |
| 141 | else { |
| 142 | outputStream << frameValue << " "; |
| 143 | } |
| 144 | } |
| 145 | outputStream << "\n"; |
| 146 | } |
| 147 | outputStream << "\n"; |
| 148 | |
| 149 | constexpr size_t maxIdleAnimationCount = 5; |
| 150 | |
| 151 | size_t idleAnimationCount = static_cast<unsigned char>( *( data + 117 ) ); |
| 152 | outputStream << "Number of idle animations is " << idleAnimationCount; |
| 153 | if ( idleAnimationCount > maxIdleAnimationCount ) { |
| 154 | outputStream << " (INVALID, maximum number should be " << maxIdleAnimationCount << ")"; |
| 155 | |
| 156 | idleAnimationCount = maxIdleAnimationCount; |
| 157 | } |
| 158 | outputStream << "\n\n"; |
| 159 | |
| 160 | outputStream << "Probabilities of each idle animation:\n"; |
| 161 | for ( size_t animId = 0; animId < idleAnimationCount; ++animId ) { |
| 162 | outputStream << animId + 1 << ": " << fheroes2::getLEValue<float>( data, 118, animId ) << "\n"; |
| 163 | } |
| 164 | outputStream << "\n"; |
| 165 | |
| 166 | outputStream << "Idle animation delay (?) (ms): " << fheroes2::getLEValue<uint32_t>( data, 138, 0 ) << " " << fheroes2::getLEValue<uint32_t>( data, 138, 1 ) |
| 167 | << " " << fheroes2::getLEValue<uint32_t>( data, 138, 2 ) << " " << fheroes2::getLEValue<uint32_t>( data, 138, 3 ) << " " |
| 168 | << fheroes2::getLEValue<uint32_t>( data, 138, 4 ) << "\n\n"; |
| 169 | |
| 170 | outputStream << "Idle animation delay (?) (ms): " << fheroes2::getLEValue<uint32_t>( data, 158 ) << "\n\n"; |
| 171 | outputStream << "Walking animation speed (ms): " << fheroes2::getLEValue<uint32_t>( data, 162 ) << "\n\n"; |
| 172 | outputStream << "Shooting animation speed (ms): " << fheroes2::getLEValue<uint32_t>( data, 166 ) << "\n\n"; |
| 173 | outputStream << "Flying animation speed (ms): " << fheroes2::getLEValue<uint32_t>( data, 170 ) << "\n\n"; |
| 174 | |
| 175 | outputStream << "Projectile start positions:\n"; |
| 176 | outputStream << "[" << fheroes2::getLEValue<int16_t>( data, 174, 0 ) << ", " << fheroes2::getLEValue<int16_t>( data, 174, 1 ) << "]\n"; |
| 177 | outputStream << "[" << fheroes2::getLEValue<int16_t>( data, 174, 2 ) << ", " << fheroes2::getLEValue<int16_t>( data, 174, 3 ) << "]\n"; |
| 178 | outputStream << "[" << fheroes2::getLEValue<int16_t>( data, 174, 4 ) << ", " << fheroes2::getLEValue<int16_t>( data, 174, 5 ) << "]\n"; |
| 179 | outputStream << "\n"; |
| 180 | |
| 181 | const int projectileFramesCount = static_cast<unsigned char>( *( data + 186 ) ); |
| 182 | outputStream << "Number of projectile frames is " << projectileFramesCount << "\n\n"; |
| 183 | |
| 184 | outputStream << "Projectile angles:\n"; |
| 185 | for ( size_t angleId = 0; angleId < 12; ++angleId ) { |
| 186 | outputStream << fheroes2::getLEValue<float>( data, 187, angleId ) << "\n"; |
| 187 | } |
| 188 | outputStream << "\n"; |
| 189 | |
| 190 | outputStream << "Troop count offset: [" << fheroes2::getLEValue<int32_t>( data, 235 ) << ", " << fheroes2::getLEValue<int32_t>( data, 239 ) << "]\n\n"; |
| 191 | |
| 192 | constexpr char invalidFrameId = '\xFF'; |
| 193 | |
| 194 | outputStream << "Animation sequence (frame IDs):\n"; |
| 195 | for ( size_t setId = 0; setId < 34; ++setId ) { |
| 196 | outputStream << setId + 1 << " : "; |
| 197 | |
| 198 | int frameCount = 0; |
| 199 | for ( size_t frameId = 0; frameId < 16; ++frameId ) { |
| 200 | const size_t offset = 277 + setId * 16 + frameId; |
| 201 | |
| 202 | if ( data[offset] == invalidFrameId ) { |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | const int frameValue = static_cast<unsigned char>( data[offset] ); |
| 207 | outputStream << frameValue << " "; |
| 208 | |
| 209 | ++frameCount; |
| 210 | } |
| 211 | |
| 212 | const int expectedFrameCount = static_cast<unsigned char>( data[243 + setId] ); |
| 213 | if ( frameCount != expectedFrameCount ) { |
| 214 | std::cerr << "WARNING: In " << inputFileName << " file number of animation frames for animation " << setId + 1 << " should be " << expectedFrameCount |
| 215 | << " while found number is " << frameCount << std::endl; |
| 216 | } |
| 217 | |
| 218 | outputStream << "\n"; |
| 219 | } |
| 220 | |
| 221 | if ( !outputStream ) { |
| 222 | std::cerr << "Error writing to file " << outputFilePath << std::endl; |
| 223 | return EXIT_FAILURE; |
| 224 | } |
| 225 | |
| 226 | ++filesProcessed; |
| 227 | } |
| 228 | |
| 229 | std::cout << "Total processed files: " << filesProcessed << std::endl; |
| 230 | |
| 231 | return EXIT_SUCCESS; |
| 232 | } |