2017-08-08 13:53:47 +02:00
|
|
|
#include <stdio.h>
|
2017-10-16 15:47:48 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
#include <experimental/filesystem>
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
#include "pcb.h"
|
2017-10-16 15:47:48 +01:00
|
|
|
#include "lgb.h"
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2017-10-16 23:54:27 +02:00
|
|
|
#include <GameData.h>
|
|
|
|
#include <File.h>
|
|
|
|
#include <DatCat.h>
|
|
|
|
#include <ExdData.h>
|
|
|
|
#include <ExdCat.h>
|
|
|
|
#include <Exd.h>
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
#include <iostream>
|
2017-10-17 21:49:01 +01:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
2017-10-16 15:47:48 +01:00
|
|
|
namespace fs = std::experimental::filesystem;
|
2017-10-17 15:14:48 +01:00
|
|
|
struct face
|
|
|
|
{
|
|
|
|
int32_t f1, f2, f3;
|
|
|
|
};
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2017-10-16 20:31:47 +01:00
|
|
|
int parseBlockEntry( char* data, std::vector<PCB_BLOCK_ENTRY>& entries, int gOff )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
bool isgroup = true;
|
|
|
|
while( isgroup )
|
|
|
|
{
|
|
|
|
PCB_BLOCK_ENTRY block_entry;
|
|
|
|
memcpy( &block_entry.header, data + offset, sizeof( block_entry.header ) );
|
2017-10-17 21:49:01 +01:00
|
|
|
isgroup = block_entry.header.type == 0x30;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
//printf( " BLOCKHEADER_%X: type: %i, group_size: %i\n", gOff + offset, block_entry.header.type, block_entry.header.group_size );
|
|
|
|
|
|
|
|
if( isgroup )
|
|
|
|
{
|
2017-10-16 20:31:47 +01:00
|
|
|
parseBlockEntry( data + offset + 0x30, entries, gOff + offset );
|
2017-08-08 13:53:47 +02:00
|
|
|
offset += block_entry.header.group_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-16 20:31:47 +01:00
|
|
|
/* printf( "\tnum_v16: %i, num_indices: %i, num_vertices: %i\n\n",
|
|
|
|
block_entry.header.num_v16, block_entry.header.num_indices, block_entry.header.num_vertices );*/
|
2017-08-08 13:53:47 +02:00
|
|
|
int doffset = sizeof( block_entry.header ) + offset;
|
|
|
|
uint16_t block_size = sizeof( block_entry.header ) +
|
|
|
|
block_entry.header.num_vertices * 3 * 4 +
|
|
|
|
block_entry.header.num_v16 * 6 +
|
|
|
|
block_entry.header.num_indices * 6;
|
|
|
|
|
|
|
|
if( block_entry.header.num_vertices != 0 )
|
|
|
|
{
|
|
|
|
block_entry.data.vertices.resize( block_entry.header.num_vertices );
|
|
|
|
|
|
|
|
int32_t size_vertexbuffer = block_entry.header.num_vertices * 3;
|
|
|
|
memcpy( &block_entry.data.vertices[0], data + doffset, size_vertexbuffer * 4 );
|
|
|
|
doffset += size_vertexbuffer * 4;
|
|
|
|
}
|
|
|
|
if( block_entry.header.num_v16 != 0 )
|
|
|
|
{
|
|
|
|
block_entry.data.vertices_i16.resize( block_entry.header.num_v16 );
|
|
|
|
int32_t size_unknownbuffer = block_entry.header.num_v16 * 6;
|
|
|
|
memcpy( &block_entry.data.vertices_i16[0], data + doffset, size_unknownbuffer );
|
|
|
|
doffset += block_entry.header.num_v16 * 6;
|
|
|
|
}
|
|
|
|
if( block_entry.header.num_indices != 0 )
|
|
|
|
{
|
|
|
|
block_entry.data.indices.resize( block_entry.header.num_indices );
|
2017-10-16 23:54:27 +02:00
|
|
|
int32_t size_indexbuffer = block_entry.header.num_indices * 12;
|
2017-08-08 13:53:47 +02:00
|
|
|
memcpy( &block_entry.data.indices[0], data + doffset, size_indexbuffer );
|
|
|
|
doffset += size_indexbuffer;
|
|
|
|
}
|
|
|
|
entries.push_back( block_entry );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:15:46 +01:00
|
|
|
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" }
|
|
|
|
};
|
|
|
|
|
|
|
|
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[] )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
auto startTime = std::chrono::system_clock::now();
|
|
|
|
|
2017-10-17 12:15:46 +01:00
|
|
|
std::string gamePath = "C:\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\sqpack\\ffxiv";
|
|
|
|
std::string zoneName = "r1f1";
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 12:15:46 +01:00
|
|
|
if( argc > 1 )
|
|
|
|
{
|
|
|
|
gamePath = argv[1];
|
|
|
|
if( argc > 2 )
|
|
|
|
{
|
|
|
|
zoneName = argv[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto& zonePath = zoneNameToPath( zoneName );
|
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
xiv::dat::GameData data1( gamePath );
|
|
|
|
xiv::exd::ExdData eData( data1 );
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
const xiv::dat::Cat& test = data1.get_category( "bg" );
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
auto &test_file = data1.get_file( "bg/ffxiv/" + zonePath + "/level/bg.lgb" );
|
|
|
|
auto §ion = test_file->access_data_sections().at( 0 );
|
|
|
|
int32_t list_offset = *(uint32_t*)§ion[0x18];
|
|
|
|
int32_t size = *(uint32_t*)§ion[4];
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
std::vector<std::string> stringList;
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
auto &test_file1 = data1.get_file( "bg/ffxiv/" + zonePath + "/collision/list.pcb" );
|
|
|
|
auto §ion1 = test_file1->access_data_sections().at( 0 );
|
|
|
|
std::string path = "bg/ffxiv/" + zonePath + "/collision/";
|
|
|
|
int offset1 = 0x20;
|
|
|
|
for( ; ; )
|
|
|
|
{
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
uint16_t trId = *(uint16_t*)§ion1[offset1];
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
char someString[200];
|
|
|
|
sprintf( someString, "%str%04d.pcb", path.c_str(), trId );
|
|
|
|
stringList.push_back( std::string( someString ) );
|
|
|
|
//std::cout << someString << "\n";
|
|
|
|
offset1 += 0x20;
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
if( offset1 >= section1.size() )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-10-16 23:54:27 +02:00
|
|
|
}
|
2017-10-17 15:14:48 +01:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
LGB_FILE bgLgb( §ion[0] );
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
int max_index = 0;
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
std::vector<std::string> vertices;
|
|
|
|
std::vector<std::string> indices;
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
char *data;
|
|
|
|
int counter = 0;
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
// dont bother if we cant write to a file
|
|
|
|
auto fp_out = fopen( (zoneName + ".obj").c_str(), "w" );
|
|
|
|
if( fp_out )
|
|
|
|
{
|
|
|
|
fprintf( fp_out, "\n" );
|
|
|
|
fclose( fp_out );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string errorMessage( "Cannot create " + zoneName + ".obj\n" +
|
|
|
|
" Check no programs have a handle to file and run as admin.\n" );
|
|
|
|
std::cout << errorMessage;
|
|
|
|
throw std::exception( errorMessage.c_str() );
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
fp_out = fopen( (zoneName + ".obj").c_str(), "ab+" );
|
|
|
|
if( fp_out )
|
2017-10-16 23:54:27 +02:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
std::map<std::string, PCB_FILE> pcbFiles;
|
|
|
|
std::map<std::string, uint32_t> objCount;
|
|
|
|
auto loadPcbFile = [&]( const std::string& fileName )
|
2017-10-17 15:14:48 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
//std::cout << fileName << " ";
|
|
|
|
auto file = data1.get_file( fileName );
|
|
|
|
auto sections = file->get_data_sections();
|
|
|
|
auto dataSection = §ions.at( 0 )[0];
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
//std::cout << sections.size() << "\n";
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
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 );
|
2017-10-17 15:14:48 +01:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
bool isgroup = true;
|
|
|
|
while( isgroup )
|
2017-10-17 15:14:48 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
PCB_BLOCK_ENTRY block_entry;
|
|
|
|
memcpy( &block_entry.header, &dataSection[0] + offset, sizeof( block_entry.header ) );
|
|
|
|
isgroup = block_entry.header.type == 0x30;
|
|
|
|
|
|
|
|
//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++;
|
2017-10-17 15:14:48 +01:00
|
|
|
}
|
2017-10-17 21:49:01 +01:00
|
|
|
pcbFiles.insert( std::make_pair( fileName, pcb_file ) );
|
2017-10-17 12:15:46 +01:00
|
|
|
}
|
2017-10-17 21:49:01 +01:00
|
|
|
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 std::string& name, const vec3* scale = nullptr, const vec3* rotation = nullptr, const vec3* translation = nullptr )
|
2017-10-17 12:15:46 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
std::string name2 = (name + "_" + std::to_string( objCount[name]++ ));
|
|
|
|
fprintf( fp_out, "o %s\n", name2.c_str() );
|
|
|
|
uint32_t groupCount = 0;
|
|
|
|
for( auto &entry : pcb_file.entries )
|
2017-10-17 12:15:46 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
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 ) );
|
2017-10-17 15:14:48 +01:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
auto makeTranslation = [&]( vec3& v )
|
|
|
|
{
|
|
|
|
if( scale )
|
|
|
|
{
|
|
|
|
v.x *= scale->x;
|
|
|
|
v.y *= scale->y;
|
|
|
|
v.z *= scale->z;
|
2017-10-17 15:14:48 +01:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
v = v * matrix4::rotateX( rotation->x );
|
|
|
|
v = v * matrix4::rotateY( rotation->y );
|
|
|
|
v = v * matrix4::rotateZ( rotation->z );
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
v.x += translation->x;
|
|
|
|
v.y += translation->y;
|
|
|
|
v.z += translation->z;
|
|
|
|
}
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
};
|
2017-10-17 12:15:46 +01:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
for( auto &vertex : entry.data.vertices )
|
|
|
|
{
|
|
|
|
vec3 v( vertex.x, vertex.y, vertex.z );
|
|
|
|
makeTranslation( v );
|
|
|
|
fprintf( fp_out, "v %f %f %f\n", v.x, v.y, v.z );
|
|
|
|
}
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
for( const auto &link : entry.data.vertices_i16 )
|
2017-10-17 15:14:48 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
vec3 v( float( link.x ) / 0xFFFF, float( link.y ) / 0xFFFF, float( link.z ) / 0xFFFF );
|
|
|
|
|
|
|
|
v.x = v.x * x_base + entry.header.x;
|
|
|
|
v.y = v.y * y_base + entry.header.y;
|
|
|
|
v.z = v.z * z_base + entry.header.z;
|
|
|
|
|
|
|
|
makeTranslation( v );
|
|
|
|
fprintf( fp_out, "v %f %f %f\n", v.x, v.y, v.z );
|
|
|
|
}
|
2017-10-17 15:14:48 +01:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
//fprintf( fp_out, "g %s_", (name2 + "_" + std::to_string( groupCount++ )).c_str() );
|
|
|
|
for( const auto &index : entry.data.indices )
|
|
|
|
{
|
|
|
|
//if( index.index[0] != 0 || index.index[1] != 0 || index.index[2] != 0 )
|
|
|
|
{
|
|
|
|
fprintf( fp_out, "f %i %i %i\n",
|
|
|
|
index.index[0] + max_index + 1,
|
|
|
|
index.index[1] + max_index + 1,
|
|
|
|
index.index[2] + max_index + 1 );
|
|
|
|
}
|
2017-10-18 13:26:06 +01:00
|
|
|
// std::cout << std::to_string( index.unknown[0] )<< " " << std::to_string( index.unknown[1] )<< " " << std::to_string( index.unknown[2]) << std::endl;
|
2017-10-17 15:14:48 +01:00
|
|
|
}
|
2017-10-17 21:49:01 +01:00
|
|
|
max_index += entry.data.vertices.size() + entry.data.vertices_i16.size();
|
2017-10-16 23:54:27 +02:00
|
|
|
}
|
2017-10-17 21:49:01 +01:00
|
|
|
};
|
2017-10-16 23:54:27 +02:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
for( const auto& fileName : stringList )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
loadPcbFile( fileName );
|
|
|
|
pushVerts( pcbFiles[fileName], fileName );
|
|
|
|
}
|
|
|
|
std::cout << "Writing obj file " << "\n";
|
2017-10-18 13:26:06 +01:00
|
|
|
std::cout << bgLgb.groups.size() << " groups " << "\n";
|
|
|
|
uint32_t totalGroups = 0;
|
|
|
|
uint32_t totalGroupEntries = 0;
|
2017-10-17 21:49:01 +01:00
|
|
|
for( const auto& group : bgLgb.groups )
|
|
|
|
{
|
2017-10-18 13:26:06 +01:00
|
|
|
//std::cout << "\t" << group.name << " Size " << group.header.entryCount << "\n";
|
|
|
|
totalGroups++;
|
2017-10-17 21:49:01 +01:00
|
|
|
for( const auto pEntry : group.entries )
|
2017-10-17 15:14:48 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
auto pBgParts = dynamic_cast<LGB_BGPARTS_ENTRY*>(pEntry.get());
|
2017-10-18 13:26:06 +01:00
|
|
|
|
|
|
|
auto& fileName = pBgParts->collisionFileName;
|
|
|
|
if( pBgParts )
|
2017-10-17 21:49:01 +01:00
|
|
|
{
|
2017-10-18 13:26:06 +01:00
|
|
|
if ( fileName.empty() )
|
|
|
|
fileName = pBgParts->modelFileName;
|
|
|
|
boost::replace_all( fileName, "bgparts", "collision" );
|
|
|
|
boost::replace_all( fileName, ".mdl", ".pcb" );
|
2017-10-17 21:49:01 +01:00
|
|
|
}
|
2017-10-18 13:26:06 +01:00
|
|
|
|
|
|
|
if( !pBgParts || fileName.empty() )
|
2017-10-17 21:49:01 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
{
|
2017-10-18 13:26:06 +01:00
|
|
|
const auto& it = pcbFiles.find( fileName );
|
|
|
|
if( it == pcbFiles.end() )
|
|
|
|
{
|
|
|
|
loadPcbFile( fileName );
|
|
|
|
//std::cout << "\t\tLoaded PCB File " << pBgParts->collisionFileName << "\n";
|
|
|
|
}
|
2017-10-17 21:49:01 +01:00
|
|
|
}
|
2017-10-18 13:26:06 +01:00
|
|
|
const auto& it = pcbFiles.find( fileName );
|
2017-10-17 21:49:01 +01:00
|
|
|
if( it != pcbFiles.end() )
|
|
|
|
{
|
2017-10-18 13:26:06 +01:00
|
|
|
totalGroupEntries++;
|
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
//std::cout << pBgParts->collisionFileName << "\n";
|
2017-10-16 20:31:47 +01:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
const auto* scale = &pBgParts->header.scale;
|
|
|
|
const auto* rotation = &pBgParts->header.rotation;
|
|
|
|
const auto* translation = &pBgParts->header.translation;
|
2017-10-16 20:31:47 +01:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
const auto& pcb_file = it->second;
|
2017-10-18 13:26:06 +01:00
|
|
|
pushVerts( pcb_file, fileName, scale, rotation, translation );
|
2017-10-17 21:49:01 +01:00
|
|
|
}
|
2017-10-17 15:14:48 +01:00
|
|
|
}
|
2017-10-16 15:47:48 +01:00
|
|
|
}
|
2017-10-18 13:26:06 +01:00
|
|
|
std::cout << "\n\nLoaded " << pcbFiles.size() << " PCB Files \n";
|
|
|
|
std::cout << "Total Groups " << totalGroups << " Total entries " << totalGroupEntries << "\n";
|
2017-10-16 15:47:48 +01:00
|
|
|
}
|
2017-10-17 21:49:01 +01:00
|
|
|
std::cout << "Finished exporting " << zoneName << " in " <<
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - startTime).count() << " seconds\n";
|
|
|
|
}
|
|
|
|
catch( std::exception& e )
|
|
|
|
{
|
|
|
|
std::cout << e.what() << std::endl;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
2017-10-16 15:47:48 +01:00
|
|
|
return 0;
|
2017-10-16 20:31:47 +01:00
|
|
|
}
|