1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-25 05:57:45 +00:00

exported collision maps correctly now (i think)

This commit is contained in:
Tahir Akhlaq 2017-10-17 12:15:46 +01:00
parent c7a864335f
commit 4e2bcbfa9c
3 changed files with 210 additions and 258 deletions

View file

@ -1,18 +1,39 @@
set( CMAKE_CXX_FLAGS "-std=c++11 -m32")
cmake_minimum_required(VERSION 2.6)
cmake_policy(SET CMP0015 NEW)
project(Tool_pcb_reader2)
include_directories("../../")
set(SAPPHIRE_BOOST_VER 1.63.0)
set(SAPPHIRE_BOOST_FOLDER_NAME boost_1_63_0)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/")
include_directories("../../lib/ChaiScript-6.0.0/include/")
include_directories("../../sapphire/datReader/")
include_directories("../../sapphire/")
include_directories("../")
file(GLOB SERVER_PUBLIC_INCLUDE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*")
file(GLOB SERVER_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}*.cpp")
file(GLOB SERVER_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}*.c*")
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ".")
add_executable(pcb_parser2 ${SERVER_PUBLIC_INCLUDE_FILES} ${SERVER_SOURCE_FILES})
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin/")
add_executable(pcb_reader2 ${SERVER_PUBLIC_INCLUDE_FILES} ${SERVER_SOURCE_FILES})
set_target_properties(pcb_reader2 PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS ON
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/../bin/"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/../bin/"
RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_CURRENT_SOURCE_DIR}/../bin/"
RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_CURRENT_SOURCE_DIR}/../bin/"
)
if (UNIX)
target_link_libraries (pcb_parser2 Common xivdat pthread mysqlclient dl z)
target_link_libraries (pcb_reader2 Common xivdat pthread mysqlclient dl z)
else()
target_link_libraries (pcb_parser2 Common xivdat libmysql zlib1)
target_link_libraries (pcb_reader2 Common xivdat libmysql zlib1)
endif()
target_link_libraries(pcb_parser2 ${Boost_LIBRARIES} ${Boost_LIBRARIES})
target_link_libraries(pcb_reader2 ${Boost_LIBRARIES} ${Boost_LIBRARIES})

View file

@ -16,26 +16,6 @@
#include <iostream>
namespace fs = std::experimental::filesystem;
std::vector<std::string> loadDir(const std::string& dir)
{
std::vector<std::string> files;
auto fsDir = fs::current_path().append(fs::path(dir));
std::cout << fsDir.string() << "\n";
if (fs::exists(fsDir))
{
for (const auto& entry : fs::directory_iterator(fsDir))
{
auto filename = entry.path().filename();
if (filename.string().find("~") == -1)
files.push_back(entry.path().string());
if (filename.string().find("list") != std::string::npos)
std::cout << filename.string() << "\n";
}
}
return files;
}
int parseBlockEntry( char* data, std::vector<PCB_BLOCK_ENTRY>& entries, int gOff )
{
int offset = 0;
@ -109,46 +89,78 @@ void toFile(const std::string& fileName, const std::vector<vec3>& vertices, cons
}
for (auto i = 0; i < indices.size(); i += 3)
{
//if( indices[i]!= 0 || indices[i + 1] != 0 || indices[i+ 2] != 0)
fprintf(fp_out, "f %i %i %i\n", indices[i] + 1, indices[i + 1] + 1, indices[i + 2] + 1);
}
fclose(fp_out);
}
}
int main()
std::string zoneNameToPath( const std::string& name )
{
char teri = name[0];
char region = name[1];
char type = name[2];
char zone = name[3];
static std::map<char, std::string> teriMap
{
{ 'r', "roc" },
{ 'w', "wil" },
{ 'l', "lak" },
{ 'o', "ocn" },
{ 'f', "fst" },
{ 'a', "air" },
{ 's', "sea" },
{ 'z', "zon" }
};
xiv::dat::GameData data1( "C:\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\sqpack\\ffxiv" );
static std::map<char, std::string> typeMap
{
{ 'f', "fld" },
{ 't', "twn" },
{ 'd', "dun" },
{ 'b', "bah" },
{ 'i', "ind" },
{ 'e', "evt" },
};
std::string ret;
const auto& teriRet = teriMap[teri];
const auto& typeRet = typeMap[type];
ret += teriRet + "_";
ret += teri;
ret += region;
ret += "/" + typeRet + "/" + name;
return ret;
}
int main( int argc, char* argv[] )
{
std::string gamePath = "C:\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\sqpack\\ffxiv";
std::string zoneName = "r1f1";
if( argc > 1 )
{
gamePath = argv[1];
if( argc > 2 )
{
zoneName = argv[2];
}
}
const auto& zonePath = zoneNameToPath( zoneName );
xiv::dat::GameData data1( gamePath );
xiv::exd::ExdData eData( data1 );
const xiv::dat::Cat& test = data1.get_category( "bg" );
auto &test_file = data1.get_file( "bg/ffxiv/sea_s1/fld/s1f5/level/bg.lgb" );
auto &test_file = data1.get_file( "bg/ffxiv/" + zonePath + "/level/bg.lgb" );
auto &section = test_file->access_data_sections().at( 0 );
int32_t list_offset = *( uint32_t* ) &section[0x18];
int32_t size = *( uint32_t* ) &section[4];
std::vector<std::string> stringList;
std::vector<std::string> stringList2;
uint32_t offset = list_offset + 0x14;
for( ; ; )
{
std::string entry( &section[offset] );
offset += entry.size() + 1;
if( entry.find( "bgcommon" ) == std::string::npos && entry.find( ".mdl" ) == std::string::npos && entry.find( ".pcb" ) != std::string::npos )
{
//stringList.push_back( entry );
}
if( offset >= size )
{
break;
}
}
auto &test_file1 = data1.get_file( "bg/ffxiv/roc_r1/fld/r1f1/collision/list.pcb" );
auto &test_file1 = data1.get_file( "bg/ffxiv/" + zonePath + "/collision/list.pcb" );
auto &section1 = test_file1->access_data_sections().at( 0 );
std::string path = "bg/ffxiv/roc_r1/fld/r1f1/collision/";
int offset1 = 0x20;
@ -160,7 +172,7 @@ int main()
char someString[200];
sprintf( someString, "%str%04d.pcb", path.c_str(), trId );
stringList.push_back( std::string( someString ) );
std::cout << someString << "\n";
//std::cout << someString << "\n";
offset1 += 0x20;
if( offset1 >= section1.size() )
@ -168,6 +180,7 @@ int main()
break;
}
}
LGB_FILE bgLgb(&section[0]);
int total_vertices = 0;
int total_indices = 0;
@ -178,59 +191,87 @@ int main()
char *data;
int counter = 0;
for( auto fileName : stringList )
std::map<std::string, PCB_FILE> pcbFiles;
auto loadPcbFile = [&]( const std::string& fileName)
{
std::cout << fileName << " ";
auto file = data1.get_file( fileName );
auto sections = file->get_data_sections();
auto dataSection = sections[0];
std::cout << sections.size() << "\n";
/*if( fileName != "collision1\\s1fa_t1_hasi1.pcb" )
try
{
std::cout << fileName;
continue;
}*/
//std::cout << fileName << " ";
auto file = data1.get_file( fileName );
auto sections = file->get_data_sections();
auto dataSection = &sections.at( 0 )[0];
uint32_t offset = 0;
//std::cout << sections.size() << "\n";
PCB_FILE pcb_file;
memcpy( &pcb_file.header, &dataSection[0], sizeof( pcb_file.header ) );
offset += sizeof( pcb_file.header );
uint32_t offset = 0;
uint32_t groupCount = 0;
PCB_FILE pcb_file;
memcpy( &pcb_file.header, &dataSection[0], sizeof( pcb_file.header ) );
offset += sizeof( pcb_file.header );
std::vector<PCB_BLOCK_ENTRY> entries;
bool isgroup = true;
while( isgroup )
{
PCB_BLOCK_ENTRY block_entry;
memcpy( &block_entry.header, &dataSection[0] + offset, sizeof( block_entry.header ) );
isgroup = block_entry.header.type == 0x30 ? true : false;
//printf( "BLOCKHEADER_%X: type: %i, group_size: %i\n", offset, block_entry.header.type, block_entry.header.group_size );
//
if( isgroup )
bool isgroup = true;
while( isgroup )
{
parseBlockEntry( &dataSection[0] + offset + 0x30, pcb_file.entries, offset );
offset += block_entry.header.group_size;
}
else
{
parseBlockEntry( &dataSection[0] + offset, pcb_file.entries, offset );
}
PCB_BLOCK_ENTRY block_entry;
memcpy( &block_entry.header, &dataSection[0] + offset, sizeof( block_entry.header ) );
isgroup = block_entry.header.type == 0x30 ? true : false;
//printf( "BLOCKHEADER_%X: type: %i, group_size: %i\n", offset, block_entry.header.type, block_entry.header.group_size );
//
if( isgroup )
{
parseBlockEntry( &dataSection[0] + offset + 0x30, pcb_file.entries, offset );
offset += block_entry.header.group_size;
}
else
{
parseBlockEntry( &dataSection[0] + offset, pcb_file.entries, offset );
}
groupCount++;
}
pcbFiles.insert( std::make_pair( fileName, pcb_file ) );
}
catch( std::exception& e )
{
std::cout << "Unable to load collision mesh " << fileName << "\n\tError:\n\t" << e.what() << "\n";
}
};
auto pushVerts = [&]( const PCB_FILE& pcb_file, const vec3* scale = nullptr, const vec3* rotation = nullptr, const vec3* translation = nullptr)
{
for( auto &entry : pcb_file.entries )
{
total_vertices += entry.header.num_vertices;
total_vertices += entry.header.num_v16;
total_indices += entry.header.num_indices;
float x_base = abs( float( entry.header.x1 - entry.header.x ) );
float y_base = abs( float( entry.header.y1 - entry.header.y ) );
float z_base = abs( float( entry.header.z1 - entry.header.z ) );
auto makeTranslation = [&]( vec3& v )
{
if( scale )
{
v.x *= scale->x;
v.y *= scale->y;
v.z *= scale->z;
}
if( rotation )
{
v = v * matrix4::rotateX( rotation->x );
v = v * matrix4::rotateY( rotation->y );
v = v * matrix4::rotateZ( rotation->z );
}
if( translation )
{
v.x += translation->x;
v.y += translation->y;
v.z += translation->z;
}
};
for( auto &vertex_list : entry.data.vertices )
{
@ -238,198 +279,75 @@ int main()
v.x = vertex_list.x;
v.y = vertex_list.y;
v.z = vertex_list.z;
makeTranslation( v );
vertices.push_back( v );
}
float x_base = abs( float( entry.header.x1 - entry.header.x ) );
float y_base = abs( float( entry.header.y1 - entry.header.y ) );
float z_base = abs( float( entry.header.z1 - entry.header.z ) );
for( const auto &link : entry.data.vertices_i16 )
{
vec3 v(float( link.x ) / 0xFFFF, float( link.y ) / 0xFFFF, float( link.z ) / 0xFFFF);
vec3 v( float( link.x ) / 0xFFFF, float( link.y ) / 0xFFFF, float( link.z ) / 0xFFFF );
float x = float( link.x );
float y = float( link.y );
float z = float( link.z );
v.x = ( x / 0xFFFF ) * x_base + entry.header.x;
v.y = ( y / 0xFFFF ) * y_base + entry.header.y;
v.z = ( z / 0xFFFF ) * z_base + entry.header.z;
v.x = (x / 0xFFFF) * x_base + entry.header.x;
v.y = (y / 0xFFFF) * y_base + entry.header.y;
v.z = (z / 0xFFFF) * z_base + entry.header.z;
makeTranslation( v );
vertices.push_back( v );
}
for( const auto &index : entry.data.indices )
{
//if( index.index[0] != 0 || index.index[1] != 0 || index.index[2] != 0 )
{
indices.push_back(int(index.index[0]) + max_index );
indices.push_back(int(index.index[1]) + max_index );
indices.push_back(int(index.index[2]) + max_index );
indices.push_back( int( index.index[0] ) + max_index );
indices.push_back( int( index.index[1] ) + max_index );
indices.push_back( int( index.index[2] ) + max_index );
}
//std::cout << std::to_string( index.unknown[0] )<< " " << std::to_string( index.unknown[1] )<< " " << std::to_string( index.unknown[2]) << std::endl;
}
max_index = vertices.size();
}
//for( auto block : pcb_file.entries )
//{
// auto pMesh3 = createBoundMeshFromPcbBlock( block );
// if( pMesh3 )
// m_pMeshList3.push_back( pMesh3 );
//}
};
}
toFile("test.obj", vertices, indices);
/* char *data;
uint32_t offset = 0;
//r1f1_b1_dor00.pcb
PCB_LIST_FILE listFile;
int groupsCount = 0;
//auto list = loadDir("bg/ffxiv/roc_r1/collision");
auto lgbFiles = getLgbFiles("bg/ffxiv/roc_r1/");
std::cout << "Loaded " << lgbFiles.size() << " LGB files" << std::endl;
std::map<std::string, uint32_t> groupCounts;
std::map<std::string, PCB_FILE> pcbFiles;
int total_vertices = 0;
int total_indices = 0;
int max_index = 0;
std::vector<vec3> vertices;
std::vector<int> indices;
for (const auto& pair : lgbFiles)
for( const auto& fileName : stringList )
{
std::cout << "FILE " << pair.first << "\n";
for (const auto& group : pair.second.groups)
loadPcbFile( fileName );
pushVerts( pcbFiles[fileName] );
}
std::cout << "Writing obj file " << "\n";
std::cout << bgLgb.groups.size() << " entries " << "\n";
for( const auto& group : bgLgb.groups )
{
for( const auto pEntry : group.entries )
{
uint32_t groupEntryCount = 0;
uint32_t bgPartsCount = 0;
//std::cout << "GROUP " << group.name << "\n";
for (auto entry : group.entries)
if( !pEntry )
continue;
auto pBgParts = dynamic_cast<LGB_BGPARTS_ENTRY*>( pEntry.get() );
if( !pBgParts || pBgParts->collisionFileName.empty()
|| std::find(stringList.begin(), stringList.end(), pBgParts->collisionFileName) != stringList.end())
continue;
auto it = pcbFiles.find( pBgParts->collisionFileName );
if( it == pcbFiles.end() )
{
if (dynamic_cast<LGB_BGPARTS_ENTRY*>(entry.get()))
bgPartsCount++;
loadPcbFile( pBgParts->collisionFileName );
}
for (auto pEntry : group.entries)
if( it != pcbFiles.end() )
{
if (!pEntry)
continue;
//std::cout << pBgParts->collisionFileName << "\n";
auto pBgParts = dynamic_cast<LGB_BGPARTS_ENTRY*>(pEntry.get());
const auto* scale = &pBgParts->header.scale;
const auto* rotation = &pBgParts->header.rotation;
const auto* translation = &pBgParts->header.translation;
if (!pBgParts || pBgParts->collisionFileName.empty())
continue;
auto filename = pBgParts->collisionFileName;
if(!groupCounts[filename])
std::cout << "GROUP " << group.name << " " << pBgParts->collisionFileName << "\n";
PCB_FILE pcb_file = PCB_FILE();
if(pcbFiles.find(filename) == pcbFiles.end())
{
FILE *fp = nullptr;
fp = fopen(filename.c_str(), "rb");
if (fp == nullptr)
return 0;
fseek(fp, 0, SEEK_END);
int32_t size = ftell(fp);
data = new char[size];
rewind(fp);
fread(data, 1, size, fp);
fclose(fp);
memcpy(&pcb_file.header, data, sizeof(pcb_file.header));
auto offset = sizeof(pcb_file.header);
try
{
bool isgroup = true;
while( isgroup )
{
PCB_BLOCK_ENTRY block_entry;
memcpy(&block_entry.header, data + offset, sizeof(block_entry.header));
isgroup = block_entry.header.type == 0x30 ? true : false;
//printf( "BLOCKHEADER_%X: type: %i, group_size: %i\n", offset, block_entry.header.type, block_entry.header.group_size );
//
if( isgroup )
{
std::cout << "ISGROUP" << "\n";
parseBlockEntry(data + offset + 0x30, pcb_file.entries, offset);
offset += block_entry.header.group_size;
}
else
{
parseBlockEntry(data + offset, pcb_file.entries, offset);
}
}
}
catch(std::exception& e)
{
std::cout << "Unable to parse " << filename << " " << e.what() << "\n";
}
}
else
{
pcb_file = pcbFiles[filename];
}
for( auto &entry : pcb_file.entries )
{
total_vertices += entry.header.num_vertices;
total_vertices += entry.header.num_v16;
total_indices += entry.header.num_indices;
for( auto &vertex_list : entry.data.vertices )
{
vec3 v;
v.x = vertex_list.x;
v.y = vertex_list.y;
v.z = vertex_list.z;
vertices.push_back( v );
}
float x_base = abs( float( entry.header.x1 - entry.header.x ) );
float y_base = abs( float( entry.header.y1 - entry.header.y ) );
float z_base = abs( float( entry.header.z1 - entry.header.z ) );
for( const auto &link : entry.data.vertices_i16 )
{
vec3 v(float( link.x ) / 0xFFFF, float( link.y ) / 0xFFFF, float( link.z ) / 0xFFFF);
float x = float( link.x );
float y = float( link.y );
float z = float( link.z );
v.x = ( x / 0xFFFF ) * x_base + entry.header.x;
v.y = ( y / 0xFFFF ) * y_base + entry.header.y;
v.z = ( z / 0xFFFF ) * z_base + entry.header.z;
vertices.push_back( v );
}
for( const auto &index : entry.data.indices )
{
indices.push_back( int( index.index[0] ) + max_index );
indices.push_back( int( index.index[1] ) + max_index );
indices.push_back( int( index.index[2] ) + max_index );
//std::cout << std::to_string( index.unknown[0] )<< " " << std::to_string( index.unknown[1] )<< " " << std::to_string( index.unknown[2]) << std::endl;
}
max_index = vertices.size();
}
//toFile("test.obj", vertices, indices);
//break;
const auto& pcb_file = it->second;
pushVerts( pcb_file, scale, rotation, translation );
}
}
}
toFile("test.obj", vertices, indices);
std::cout << "vertices " << vertices.size() << " indices " << indices.size() / 3 << " expected indices " << vertices.size() << " total " << total_indices << "\n";*/
return 0;
}

View file

@ -10,19 +10,19 @@ struct matrix4
float grid[16];
matrix4()
{
memset(&grid[0], 0, sizeof(grid));
memset( &grid[0], 0, sizeof( grid ) );
}
float operator()(int row, int col) const
float operator()( int row, int col ) const
{
return grid[(row * 4) + col];
}
float& operator()(int row, int col)
float& operator()( int row, int col )
{
return grid[(row * 4) + col];
}
static matrix4 rotateX(float angle)
static matrix4 rotateX( float angle )
{
matrix4 ret = matrix4();
ret(0, 0) = 1.000000000f;
@ -34,7 +34,7 @@ struct matrix4
return ret;
}
static matrix4 rotateY(float angle)
static matrix4 rotateY( float angle )
{
matrix4 ret = matrix4();
ret(0, 0) = cos(angle);
@ -46,7 +46,7 @@ struct matrix4
return ret;
}
static matrix4 rotateZ(float angle)
static matrix4 rotateZ( float angle )
{
matrix4 ret = matrix4();
ret(0, 0) = cos(angle);
@ -58,7 +58,7 @@ struct matrix4
return ret;
}
static matrix4 scale(float x, float y, float z)
static matrix4 scale( float x, float y, float z )
{
matrix4 ret = matrix4();
ret(0, 0) = x;
@ -69,7 +69,7 @@ struct matrix4
return ret;
}
static matrix4 translate(float x, float y, float z)
static matrix4 translate( float x, float y, float z )
{
matrix4 ret = matrix4();
ret(0, 0) = 1;
@ -82,5 +82,18 @@ struct matrix4
ret(3, 2) = z;
return ret;
}
matrix4 operator *( const matrix4& rhs ) const
{
matrix4 ret;
for( unsigned int i = 0; i < 4; i++ )
{
ret( i, 0 ) = (*this)(i, 0) * rhs( 0, 0 ) + (*this)(i, 1) * rhs( 1, 0 ) + (*this)(i, 2) * rhs( 2, 0 ) + (*this)(i, 3) * rhs( 3, 0 );
ret( i, 1 ) = (*this)(i, 0) * rhs( 0, 1 ) + (*this)(i, 1) * rhs( 1, 1 ) + (*this)(i, 2) * rhs( 2, 1 ) + (*this)(i, 3) * rhs( 3, 1 );
ret( i, 2 ) = (*this)(i, 0) * rhs( 0, 2 ) + (*this)(i, 1) * rhs( 1, 2 ) + (*this)(i, 2) * rhs( 2, 2 ) + (*this)(i, 3) * rhs( 3, 2 );
ret( i, 3 ) = (*this)(i, 0) * rhs( 0, 3 ) + (*this)(i, 1) * rhs( 1, 3 ) + (*this)(i, 2) * rhs( 2, 3 ) + (*this)(i, 3) * rhs( 3, 3 );
}
return ret;
}
};
#endif