diff --git a/src/tools/pcb_reader/lgb.h b/src/tools/pcb_reader/lgb.h index 94fbfa20..7a9b7bdc 100644 --- a/src/tools/pcb_reader/lgb.h +++ b/src/tools/pcb_reader/lgb.h @@ -13,6 +13,9 @@ #include "vec3.h" #include "sgb.h" +// garbage to skip model loading +extern bool ignoreModels; + // all credit to // https://github.com/ufx/SaintCoinach/blob/master/SaintCoinach/Graphics/Lgb/ // this is simply their work ported to c++ since we dont c# @@ -70,23 +73,30 @@ struct LGB_ENTRY_HEADER vec3 scale; }; -class LGB_MODEL_ENTRY +class LGB_ENTRY { public: char* m_buf; uint32_t m_offset; + LGB_ENTRY_HEADER header; - LGB_MODEL_ENTRY() + LGB_ENTRY() { m_buf = nullptr; m_offset = 0; + memset( &header, 0, sizeof( header ) ); }; - LGB_MODEL_ENTRY( char* buf, uint32_t offset ) + LGB_ENTRY( char* buf, uint32_t offset ) { m_buf = buf; m_offset = offset; + header = *reinterpret_cast< LGB_ENTRY_HEADER* >( buf + offset ); }; - virtual ~LGB_MODEL_ENTRY() {}; + const LgbEntryType getType() const + { + return header.type; + }; + virtual ~LGB_ENTRY() {}; }; @@ -102,7 +112,7 @@ struct LGB_BGPARTS_HEADER : public LGB_ENTRY_HEADER uint32_t unknown9; }; -class LGB_BGPARTS_ENTRY : public LGB_MODEL_ENTRY +class LGB_BGPARTS_ENTRY : public LGB_ENTRY { public: LGB_BGPARTS_HEADER header; @@ -110,7 +120,7 @@ public: std::string modelFileName; std::string collisionFileName; LGB_BGPARTS_ENTRY() {}; - LGB_BGPARTS_ENTRY( char* buf, uint32_t offset ) + LGB_BGPARTS_ENTRY( char* buf, uint32_t offset ) : LGB_ENTRY( buf, offset ) { header = *reinterpret_cast( buf + offset ); name = std::string( buf + offset + header.nameOffset ); @@ -125,14 +135,14 @@ struct LGB_GIMMICK_HEADER : public LGB_ENTRY_HEADER char unknownBytes[100]; }; -class LGB_GIMMICK_ENTRY : public LGB_MODEL_ENTRY +class LGB_GIMMICK_ENTRY : public LGB_ENTRY { public: LGB_GIMMICK_HEADER header; std::string name; std::string gimmickFileName; - LGB_GIMMICK_ENTRY( char* buf, uint32_t offset ) + LGB_GIMMICK_ENTRY( char* buf, uint32_t offset ) : LGB_ENTRY( buf, offset ) { header = *reinterpret_cast( buf + offset ); name = std::string( buf + offset + header.nameOffset ); @@ -147,13 +157,13 @@ struct LGB_ENPC_HEADER : public LGB_ENTRY_HEADER uint8_t unknown1[0x24]; }; -class LGB_ENPC_ENTRY : public LGB_MODEL_ENTRY +class LGB_ENPC_ENTRY : public LGB_ENTRY { public: LGB_ENPC_HEADER header; std::string name; - LGB_ENPC_ENTRY( char* buf, uint32_t offset ) + LGB_ENPC_ENTRY( char* buf, uint32_t offset ) : LGB_ENTRY( buf, offset ) { header = *reinterpret_cast< LGB_ENPC_HEADER* >( buf + offset ); name = std::string( buf + offset + header.nameOffset ); @@ -167,13 +177,13 @@ struct LGB_EOBJ_HEADER : public LGB_ENTRY_HEADER uint8_t unknown1[0x10]; }; -class LGB_EOBJ_ENTRY : public LGB_MODEL_ENTRY +class LGB_EOBJ_ENTRY : public LGB_ENTRY { public: LGB_EOBJ_HEADER header; std::string name; - LGB_EOBJ_ENTRY( char* buf, uint32_t offset ) + LGB_EOBJ_ENTRY( char* buf, uint32_t offset ) : LGB_ENTRY( buf, offset ) { header = *reinterpret_cast< LGB_EOBJ_HEADER* >( buf + offset ); //std::cout << "\t " << header.eobjId << " " << name << " unknown: " << header.unknown << "\n"; @@ -181,6 +191,26 @@ public: }; }; +struct LGB_MAPRANGE_HEADER : public LGB_ENTRY_HEADER +{ + uint32_t type; + uint32_t unknown2; + uint8_t unknown3[0x10]; +}; + +struct LGB_MAPRANGE_ENTRY : public LGB_ENTRY +{ +public: + LGB_MAPRANGE_HEADER header; + std::string name; + + LGB_MAPRANGE_ENTRY( char* buf, uint32_t offset ) : LGB_ENTRY( buf, offset ) + { + header = *reinterpret_cast< LGB_MAPRANGE_HEADER* >( buf + offset ); + name = std::string( buf + offset + header.nameOffset ); + }; +}; + struct LGB_GROUP_HEADER { uint32_t unknown; @@ -203,7 +233,7 @@ struct LGB_GROUP LGB_FILE* parent; LGB_GROUP_HEADER header; std::string name; - std::vector< std::shared_ptr< LGB_MODEL_ENTRY > > entries; + std::vector< std::shared_ptr< LGB_ENTRY > > entries; LGB_GROUP( char* buf, LGB_FILE* parentStruct, uint32_t offset ) { @@ -220,11 +250,12 @@ struct LGB_GROUP try { const auto type = *reinterpret_cast( buf + entryOffset ); - if( type == LgbEntryType::BgParts ) + // garbage to skip model loading + if( !ignoreModels && type == LgbEntryType::BgParts ) { entries.push_back( std::make_shared< LGB_BGPARTS_ENTRY >( buf, entryOffset ) ); } - else if( type == LgbEntryType::Gimmick ) + else if( !ignoreModels && type == LgbEntryType::Gimmick ) { entries.push_back( std::make_shared< LGB_GIMMICK_ENTRY >( buf, entryOffset ) ); } @@ -236,12 +267,17 @@ struct LGB_GROUP { entries.push_back( std::make_shared< LGB_EOBJ_ENTRY >( buf, entryOffset ) ); } + else if( type == LgbEntryType::MapRange ) + { + entries.push_back( std::make_shared< LGB_MAPRANGE_ENTRY >( buf, entryOffset ) ); + } /* else { - //entries[i] = nullptr; + entries[i] = nullptr; } */ + } catch( std::exception& e ) { @@ -268,8 +304,9 @@ struct LGB_FILE { LGB_FILE_HEADER header; std::vector< LGB_GROUP > groups; + std::string name; - LGB_FILE( char* buf ) + LGB_FILE( char* buf, const std::string& name ) { header = *reinterpret_cast< LGB_FILE_HEADER* >( buf ); if( strncmp( &header.magic[0], "LGB1", 4 ) != 0 || strncmp( &header.magic2[0], "LGP1", 4 ) != 0 ) diff --git a/src/tools/pcb_reader/main.cpp b/src/tools/pcb_reader/main.cpp index 2a5d5c61..c6704856 100644 --- a/src/tools/pcb_reader/main.cpp +++ b/src/tools/pcb_reader/main.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include "pcb.h" #include "lgb.h" @@ -21,8 +23,16 @@ #include #endif -std::string gamePath("C:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\sqpack\\ffxiv"); +// garbage to ignore models +bool ignoreModels = false; + +std::string gamePath( "C:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\sqpack\\ffxiv" ); std::unordered_map< uint32_t, std::string > eobjNameMap; +std::unordered_map< uint16_t, std::string > zoneNameMap; +uint32_t zoneId; + +std::set< std::string > zoneDumpList; + xiv::dat::GameData* data1 = nullptr; xiv::exd::ExdData* eData = nullptr; @@ -103,8 +113,8 @@ int parseBlockEntry( char* data, std::vector& entries, int gOff void dumpLevelExdEntries( uint32_t zoneId, const std::string& name = std::string() ) { - auto& cat = eData->get_category( "Level" ); - auto exd = static_cast< xiv::exd::Exd >( cat.get_data_ln( xiv::exd::Language::none ) ); + static auto& cat = eData->get_category( "Level" ); + static auto exd = static_cast< xiv::exd::Exd >( cat.get_data_ln( xiv::exd::Language::none ) ); std::string fileName( name + "_" + std::to_string( zoneId ) + "_Level" + ".csv" ); std::ofstream outfile( fileName, std::ios::trunc ); @@ -113,8 +123,8 @@ void dumpLevelExdEntries( uint32_t zoneId, const std::string& name = std::string { outfile.close(); outfile.open( fileName, std::ios::app ); - - for( auto& row : exd.get_rows() ) + static auto rows = exd.get_rows(); + for( auto& row : rows ) { auto id = row.first; auto& fields = row.second; @@ -143,7 +153,8 @@ void dumpLevelExdEntries( uint32_t zoneId, const std::string& name = std::string std::string zoneNameToPath( const std::string& name ) { std::string path; - uint32_t id; + bool found = false; + #ifdef STANDALONE auto inFile = std::ifstream( "territorytype.exh.csv" ); if( inFile.good() ) @@ -153,22 +164,23 @@ std::string zoneNameToPath( const std::string& name ) while( std::getline( inFile, line ) ) { std::smatch match; - if( std::regex_match( line, match, re ) ) + if( std::regex_match( line, match, re ) { - if( name == match[2].str() ) + auto tmpId = std::stoul( match[1].str() ); + if( !found && name == match[2].str() ) { - id = match[1].str(); + zoneId = tmpId; path = match[3].str(); - break; + found = true; } + zoneNameMap[tmpId] = match[2].str(); } } inFile.close(); } #else - xiv::dat::GameData dat( gamePath ); - xiv::exd::ExdData eData( dat ); - auto& cat = eData.get_category( "TerritoryType" ); + + auto& cat = eData->get_category( "TerritoryType" ); auto exd = static_cast< xiv::exd::Exd >( cat.get_data_ln( xiv::exd::Language::none ) ); for( auto& row : exd.get_rows() ) { @@ -177,16 +189,17 @@ std::string zoneNameToPath( const std::string& name ) if( teriName.empty() ) continue; auto teriPath = *boost::get< std::string >( &fields.at( static_cast< size_t >( TerritoryTypeExdIndexes::Path ) ) ); - id = row.first; - if( boost::iequals( name, teriName ) ) + if( !found && boost::iequals( name, teriName ) ) { path = teriPath; - break; + found = true; + zoneId = row.first; } + zoneNameMap[row.first] = teriName; } #endif - if( !path.empty() ) + if( found ) { //path = path.substr( path.find_first_of( "/" ) + 1, path.size() - path.find_first_of( "/" )); //path = std::string( "ffxiv/" ) + path; @@ -198,7 +211,7 @@ std::string zoneNameToPath( const std::string& name ) throw std::runtime_error( "Unable to find path for " + name + ".\n\tPlease double check spelling or open 0a0000.win32.index with FFXIV Explorer and extract territorytype.exh as CSV\n\tand copy territorytype.exh.csv into pcb_reader.exe directory if using standalone" ); } - dumpLevelExdEntries( id, name ); + return path; } @@ -215,15 +228,62 @@ void loadEobjNames() } } -void writeEobjEntry( std::ofstream& out, LGB_EOBJ_ENTRY* pEobj, const std::string& name ) +void writeEobjEntry( std::ofstream& out, LGB_ENTRY* pObj ) { + static std::string mapRangeStr( "\"MapRange\", " ); + static std::string eobjStr( "\"EObj\", " ); + + uint32_t id; + std::string name; + std::string typeStr; + + if( pObj->getType() == LgbEntryType::EventObject ) + { + auto pEobj = reinterpret_cast< LGB_EOBJ_ENTRY* >( pObj ); + id = pEobj->header.eobjId; + name = eobjNameMap[id]; + typeStr = eobjStr; + } + else if( pObj->getType() == LgbEntryType::MapRange ) + { + auto pMapRange = reinterpret_cast< LGB_MAPRANGE_ENTRY* >( pObj ); + id = pMapRange->header.unknown; + typeStr = mapRangeStr; + } + std::string outStr( - std::to_string( pEobj->header.eobjId ) + ", \"" + name + "\", " + - std::to_string( pEobj->header.translation.x ) + ", " + std::to_string( pEobj->header.translation.y ) + ", " + std::to_string( pEobj->header.translation.z ) + "\n" + std::to_string( id ) + ", " + typeStr + "\"" + name + "\", " + + std::to_string( pObj->header.translation.x ) + ", " + std::to_string( pObj->header.translation.y ) + ", " + std::to_string( pObj->header.translation.z ) + "\n" ); out.write( outStr.c_str(), outStr.size() ); } +void loadAllInstanceContentEntries() +{ + auto& catInstance = eData->get_category( "InstanceContent" ); + auto exdInstance = static_cast< xiv::exd::Exd >( catInstance.get_data_ln( xiv::exd::Language::en ) ); + + if( zoneNameMap.size() == 0 ) + { + zoneNameToPath( "f1d1" ); + } + + for( auto& row : exdInstance.get_rows() ) + { + auto id = row.first; + auto& fields = row.second; + + auto name = *boost::get< std::string >( &fields.at( 3 ) ); + if( name.empty() ) + continue; + auto teri = *boost::get< uint32_t >( &fields.at( 7 ) ); + auto i = 0; + while( ( i = name.find( ' ' ) ) != std::string::npos ) + name = name.replace( name.begin() + i, name.begin() + i + 1, { '_' } ); + zoneDumpList.emplace( zoneNameMap[teri] ); + } +} + void readFileToBuffer( const std::string& path, std::vector< char >& buf ) { auto inFile = std::ifstream( path, std::ios::binary ); @@ -245,22 +305,47 @@ void readFileToBuffer( const std::string& path, std::vector< char >& buf ) int main( int argc, char* argv[] ) { auto startTime = std::chrono::system_clock::now(); + auto entryStartTime = std::chrono::system_clock::now(); + std::vector< std::string > argVec( argv + 1, argv + argc ); // todo: support expansions std::string zoneName = "r1f1"; + bool dumpInstances = ignoreModels = std::remove_if( argVec.begin(), argVec.end(), []( auto arg ){ return arg == "--instance-dump"; } ) != argVec.end(); + if( argc > 1 ) { zoneName = argv[1]; if( argc > 2 ) { - gamePath = argv[2]; + std::string tmpPath( argv[2] ); + if( !( tmpPath.empty() || tmpPath.find( '/' ) == std::string::npos ) ) + gamePath = argv[2]; } } initExd( gamePath ); + if( dumpInstances ) + { + loadAllInstanceContentEntries(); + } + else + { + zoneDumpList.emplace( zoneName ); + } + +LABEL_DUMP: + entryStartTime = std::chrono::system_clock::now(); + zoneName = *zoneDumpList.begin(); try { const auto& zonePath = zoneNameToPath( zoneName ); + + if( zonePath.find( "ex1/" ) != std::string::npos || zonePath.find( "ex2" ) != std::string::npos ) + { + std::cout << "[Error] Expansions are currently not supported " << zonePath << "\n"; + goto LABEL_NEXT_ZONE_ENTRY; + } + std::string listPcbPath( zonePath + "/collision/list.pcb" ); std::string bgLgbPath( zonePath + "/level/bg.lgb" ); std::string planmapLgbPath( zonePath + "/level/planmap.lgb" ); @@ -292,7 +377,8 @@ int main( int argc, char* argv[] ) uint32_t offset1 = 0x20; loadEobjNames(); - std::string eobjFileName( zoneName + "eobj.csv" ); + dumpLevelExdEntries( zoneId, zoneName ); + std::string eobjFileName( zoneName + "_eobj.csv" ); std::ofstream eobjOut( eobjFileName, std::ios::trunc ); if( !eobjOut.good() ) throw std::string( "Unable to create " + zoneName + "_eobj.csv for eobj entries. Run as admin or check there isnt already a handle on the file." ).c_str(); @@ -300,6 +386,9 @@ int main( int argc, char* argv[] ) eobjOut.close(); eobjOut.open( eobjFileName, std::ios::app ); + if( !eobjOut.good() ) + throw std::string( "Unable to create " + zoneName + "_eobj.csv for eobj entries. Run as admin or check there isnt already a handle on the file." ).c_str(); + for( ; ; ) { @@ -317,20 +406,20 @@ int main( int argc, char* argv[] ) } } - LGB_FILE bgLgb( §ion[0] ); - LGB_FILE planmapLgb( §ion2[0] ); + LGB_FILE bgLgb( §ion[0], "bg" ); + LGB_FILE planmapLgb( §ion2[0], "planmap" ); std::vector< LGB_FILE > lgbList { bgLgb, planmapLgb }; uint32_t max_index = 0; // dont bother if we cant write to a file - auto fp_out = fopen( ( zoneName + ".obj" ).c_str(), "w" ); + auto fp_out = ignoreModels ? ( FILE* )nullptr : fopen( ( zoneName + ".obj" ).c_str(), "w" ); if( fp_out ) { fprintf( fp_out, "\n" ); fclose( fp_out ); } - else + else if( !ignoreModels ) { std::string errorMessage( "Cannot create " + zoneName + ".obj\n" + " Check no programs have a handle to file and run as admin.\n" ); @@ -339,19 +428,20 @@ int main( int argc, char* argv[] ) return 0; } - fp_out = fopen( ( zoneName + ".obj" ).c_str(), "ab+" ); - if( fp_out ) + if( ignoreModels || ( fp_out = fopen( ( zoneName + ".obj" ).c_str(), "ab+" ) ) ) { std::map< std::string, PCB_FILE > pcbFiles; std::map< std::string, SGB_FILE > sgbFiles; std::map< std::string, uint32_t > objCount; auto loadPcbFile = [&]( const std::string& fileName ) -> bool { + if( ignoreModels ) + return false; try { if( fileName.find( '.' ) == std::string::npos ) return false; - else if( fileName.substr(fileName.find_last_of('.')) != ".pcb" ) + else if( fileName.substr( fileName.find_last_of( '.' ) ) != ".pcb" ) throw std::runtime_error( "Not a PCB file." ); char* dataSection = nullptr; @@ -434,6 +524,8 @@ int main( int argc, char* argv[] ) const vec3* translation = nullptr, const SGB_MODEL_ENTRY* pSgbEntry = nullptr) { + if( ignoreModels ) + return; char name2[0x100]; memset( name2, 0, 0x100 ); sprintf( &name2[0], "%s_%u", &name[0], objCount[name]++ ); @@ -516,14 +608,14 @@ int main( int argc, char* argv[] ) loadPcbFile( fileName ); pushVerts( pcbFiles[fileName], fileName ); } - std::cout << "[Info] " << "Writing obj file " << "\n"; - std::cout << "[Info] " << bgLgb.groups.size() << " groups " << "\n"; + + std::cout << "[Info] " << ( ignoreModels ? "Dumping MapRange and EObj" : "Writing obj file " ) << "\n"; uint32_t totalGroups = 0; uint32_t totalGroupEntries = 0; for( const auto& lgb : lgbList ) { - for( const auto& group : bgLgb.groups ) + for( const auto& group : lgb.groups ) { //std::cout << "\t" << group.name << " Size " << group.header.entryCount << "\n"; totalGroups++; @@ -531,7 +623,6 @@ int main( int argc, char* argv[] ) { auto pGimmick = dynamic_cast< LGB_GIMMICK_ENTRY* >( pEntry.get() ); auto pBgParts = dynamic_cast< LGB_BGPARTS_ENTRY* >( pEntry.get() ); - auto pEventObj = dynamic_cast< LGB_EOBJ_ENTRY* >( pEntry.get() ); std::string fileName( "" ); fileName.resize( 256 ); @@ -591,20 +682,19 @@ int main( int argc, char* argv[] ) } } - if( pEventObj ) + if( pEntry->getType() == LgbEntryType::EventObject || pEntry->getType() == LgbEntryType::MapRange ) { - fileName = pEventObj->name.empty() ? eobjNameMap[pEventObj->header.eobjId] : pEventObj->name; - writeEobjEntry( eobjOut, pEventObj, fileName ); + writeEobjEntry( eobjOut, pEntry.get() ); //writeOutput( fileName, &pEventObj->header.scale, &pEventObj->header.rotation, &pEventObj->header.translation ); } } } } - std::cout << "\n[Info] " << "Loaded " << pcbFiles.size() << " PCB Files \n"; + std::cout << "[Info] " << "Loaded " << pcbFiles.size() << " PCB Files \n"; std::cout << "[Info] " << "Total Groups " << totalGroups << " Total entries " << totalGroupEntries << "\n"; } - std::cout << "[Success] " << "Finished exporting " << zoneName << " in " << - std::chrono::duration_cast< std::chrono::seconds >( std::chrono::system_clock::now() - startTime ).count() << " seconds\n"; + std::cout << "[Success] " << "Exported " << zoneName << " in " << + std::chrono::duration_cast< std::chrono::seconds >( std::chrono::system_clock::now() - entryStartTime ).count() << " seconds\n"; } catch( std::exception& e ) { @@ -613,6 +703,14 @@ int main( int argc, char* argv[] ) std::cout << std::endl; std::cout << "[Info] " << "Usage: pcb_reader2 territory \"path/to/game/sqpack/ffxiv\" " << std::endl; } + std::cout << "\n\n\n"; + LABEL_NEXT_ZONE_ENTRY: + zoneDumpList.erase( zoneName ); + if( !zoneDumpList.empty() ) + goto LABEL_DUMP; + + std::cout << "\n\n\n[Success] Finished all tasks in " << + std::chrono::duration_cast< std::chrono::seconds >( std::chrono::system_clock::now() - startTime ).count() << " seconds\n"; if( eData ) delete eData; diff --git a/src/tools/pcb_reader/sgb.h b/src/tools/pcb_reader/sgb.h index edd6899a..2aa675aa 100644 --- a/src/tools/pcb_reader/sgb.h +++ b/src/tools/pcb_reader/sgb.h @@ -11,6 +11,9 @@ #include "vec3.h" +// garbage to skip model loading +extern bool ignoreModels; + // // ported from https://github.com/ufx/SaintCoinach/blob/master/SaintCoinach/Graphics/Sgb/SgbDataType.cs @@ -132,7 +135,7 @@ struct SGB_GROUP if( entryOffset > fileSize ) throw std::runtime_error( "SGB_GROUP entry offset was larger than SGB file size!" ); auto type = *reinterpret_cast< uint32_t* >( buf + entryOffset ); - if( type == SgbGroupEntryType::Model ) + if( type == SgbGroupEntryType::Model && !ignoreModels ) { entries.push_back( std::make_shared< SGB_MODEL_ENTRY >( buf, entryOffset ) ); }