2017-10-16 15:47:48 +01:00
|
|
|
#ifndef _LGB_H
|
|
|
|
#define _LGB_H
|
|
|
|
|
2017-10-16 20:31:47 +01:00
|
|
|
#include "matrix4.h"
|
|
|
|
#include "vec3.h"
|
2017-10-16 15:47:48 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
|
|
|
|
// 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#
|
|
|
|
struct LGB_FILE;
|
|
|
|
struct LGB_FILE_HEADER;
|
|
|
|
struct LGB_GROUP;
|
|
|
|
struct LGB_GROUP_HEADER;
|
|
|
|
|
|
|
|
enum class LgbEntryType : uint32_t
|
|
|
|
{
|
|
|
|
BgParts = 1,
|
|
|
|
Light = 3,
|
|
|
|
Vfx = 4,
|
|
|
|
PositionMarker = 5,
|
|
|
|
Gimmick = 6,
|
|
|
|
SharedGroup6 = 6,// secondary variable is set to 2
|
|
|
|
Sound = 7,
|
|
|
|
EventNpc = 8,
|
|
|
|
BattleNpc = 9,
|
|
|
|
Aetheryte = 12,
|
|
|
|
EnvSpace = 13,
|
|
|
|
Gathering = 14,
|
|
|
|
SharedGroup15 = 15,// secondary variable is set to 13
|
|
|
|
Treasure = 16,
|
|
|
|
Weapon = 39,
|
|
|
|
PopRange = 40,
|
|
|
|
ExitRange = 41,
|
|
|
|
MapRange = 43,
|
|
|
|
NaviMeshRange = 44,
|
|
|
|
EventObject = 45,
|
|
|
|
EnvLocation = 47,
|
|
|
|
EventRange = 49,
|
|
|
|
QuestMarker = 51,
|
|
|
|
CollisionBox = 57,
|
|
|
|
DoorRange = 58,
|
|
|
|
LineVfx = 59,
|
|
|
|
ClientPath = 65,
|
|
|
|
ServerPath = 66,
|
|
|
|
GimmickRange = 67,
|
|
|
|
TargetMarker = 68,
|
|
|
|
ChairMarker = 69,
|
|
|
|
ClickableRange = 70,
|
|
|
|
PrefetchRange = 71,
|
|
|
|
FateRange = 72,
|
|
|
|
SphereCastRange = 75,
|
|
|
|
};
|
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
class LGB_MODEL_ENTRY
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
char* m_buf;
|
|
|
|
uint32_t m_offset;
|
|
|
|
|
|
|
|
LGB_MODEL_ENTRY()
|
|
|
|
{
|
|
|
|
m_buf = nullptr;
|
|
|
|
m_offset = 0;
|
|
|
|
};
|
|
|
|
LGB_MODEL_ENTRY( char* buf, uint32_t offset )
|
|
|
|
{
|
|
|
|
m_buf = buf;
|
|
|
|
m_offset = offset;
|
|
|
|
};
|
|
|
|
virtual ~LGB_MODEL_ENTRY() {};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-10-16 15:47:48 +01:00
|
|
|
struct LGB_BGPARTS_HEADER
|
|
|
|
{
|
|
|
|
LgbEntryType type;
|
|
|
|
uint32_t unknown2;
|
|
|
|
uint32_t nameOffset;
|
|
|
|
vec3 translation;
|
|
|
|
vec3 rotation;
|
|
|
|
vec3 scale;
|
|
|
|
uint32_t modelFileOffset;
|
|
|
|
uint32_t collisionFileOffset;
|
|
|
|
uint32_t unknown4;
|
|
|
|
uint32_t unknown5;
|
|
|
|
uint32_t unknown6;
|
|
|
|
uint32_t unknown7;
|
|
|
|
uint32_t unknown8;
|
|
|
|
uint32_t unknown9;
|
|
|
|
};
|
|
|
|
|
|
|
|
class LGB_BGPARTS_ENTRY : public LGB_MODEL_ENTRY
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LGB_BGPARTS_HEADER header;
|
|
|
|
std::string name;
|
|
|
|
std::string modelFileName;
|
|
|
|
std::string collisionFileName;
|
2017-10-17 21:49:01 +01:00
|
|
|
LGB_BGPARTS_ENTRY() {};
|
|
|
|
LGB_BGPARTS_ENTRY( char* buf, uint32_t offset )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
|
|
|
header = *reinterpret_cast<LGB_BGPARTS_HEADER*>(buf + offset);
|
2017-10-17 21:49:01 +01:00
|
|
|
name = std::string( buf + offset + header.nameOffset );
|
|
|
|
modelFileName = std::string( buf + offset + header.modelFileOffset );
|
|
|
|
collisionFileName = std::string( buf + offset + header.collisionFileOffset );
|
2017-10-16 15:47:48 +01:00
|
|
|
//std::cout << "BGPARTS_ENTRY " << name << "\n";
|
|
|
|
//std::cout << " " << modelFileName << "\n";
|
|
|
|
//std::cout << " " << collisionFileName << "\n";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LGB_GIMMICK_HEADER
|
|
|
|
{
|
|
|
|
LgbEntryType type;
|
|
|
|
uint32_t unknown;
|
|
|
|
uint32_t nameOffset;
|
|
|
|
vec3 translation;
|
|
|
|
vec3 rotation;
|
|
|
|
vec3 scale;
|
|
|
|
uint32_t gimmickFileOffset;
|
|
|
|
char unknownBytes[100];
|
|
|
|
};
|
|
|
|
|
|
|
|
class LGB_GIMMICK_ENTRY : public LGB_MODEL_ENTRY
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LGB_GIMMICK_HEADER header;
|
|
|
|
std::string name;
|
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
LGB_GIMMICK_ENTRY( char* buf, uint32_t offset )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
|
|
|
header = *reinterpret_cast<LGB_GIMMICK_HEADER*>(buf + offset);
|
2017-10-17 21:49:01 +01:00
|
|
|
name = std::string( buf + offset + header.nameOffset );
|
2017-10-16 15:47:48 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LGB_GROUP_HEADER
|
|
|
|
{
|
|
|
|
uint32_t unknown;
|
|
|
|
int32_t groupNameOffset;
|
|
|
|
int32_t entriesOffset;
|
|
|
|
int32_t entryCount;
|
|
|
|
uint32_t unknown2;
|
|
|
|
uint32_t unknown3;
|
|
|
|
uint32_t unknown4;
|
|
|
|
uint32_t unknown5;
|
|
|
|
uint32_t unknown6;
|
|
|
|
uint32_t unknown7;
|
|
|
|
uint32_t unknown8;
|
|
|
|
uint32_t unknown9;
|
|
|
|
uint32_t unknown10;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LGB_GROUP
|
|
|
|
{
|
|
|
|
LGB_FILE* parent;
|
|
|
|
LGB_GROUP_HEADER header;
|
|
|
|
std::string name;
|
|
|
|
std::vector<std::shared_ptr<LGB_MODEL_ENTRY>> entries;
|
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
LGB_GROUP( char* buf, LGB_FILE* parentStruct, uint32_t offset )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
parent = parentStruct;
|
|
|
|
header = *reinterpret_cast<LGB_GROUP_HEADER*>(buf + offset);
|
|
|
|
name = std::string( buf + offset + header.groupNameOffset );
|
2017-10-18 13:26:06 +01:00
|
|
|
//entries.resize( header.entryCount );
|
2017-10-17 21:49:01 +01:00
|
|
|
//std::cout << name << std::endl;
|
|
|
|
auto entriesOffset = offset + header.entriesOffset;
|
|
|
|
for( auto i = 0; i < header.entryCount; ++i )
|
|
|
|
{
|
|
|
|
auto entryOffset = entriesOffset + *reinterpret_cast<int32_t*>(buf + (entriesOffset + i * 4));
|
2017-10-16 15:47:48 +01:00
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
try
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
auto type = *reinterpret_cast<LgbEntryType*>(buf + entryOffset);
|
|
|
|
if( type == LgbEntryType::BgParts )
|
|
|
|
{
|
2017-10-18 13:26:06 +01:00
|
|
|
entries.push_back(std::make_shared<LGB_BGPARTS_ENTRY>( buf, entryOffset ));
|
2017-10-17 21:49:01 +01:00
|
|
|
}
|
2017-10-18 13:26:06 +01:00
|
|
|
/*
|
2017-10-17 21:49:01 +01:00
|
|
|
else if( type == LgbEntryType::Gimmick )
|
|
|
|
{
|
2017-10-18 13:26:06 +01:00
|
|
|
//entries[i] = std::make_shared<LGB_GIMMICK_ENTRY>( buf, entryOffset );
|
2017-10-17 21:49:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-18 13:26:06 +01:00
|
|
|
//entries[i] = nullptr;
|
2017-10-17 21:49:01 +01:00
|
|
|
}
|
2017-10-18 13:26:06 +01:00
|
|
|
*/
|
2017-10-16 15:47:48 +01:00
|
|
|
}
|
2017-10-17 21:49:01 +01:00
|
|
|
catch( std::exception& e )
|
2017-10-16 20:31:47 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
std::cout << name << " " << e.what() << std::endl;
|
2017-10-16 20:31:47 +01:00
|
|
|
}
|
2017-10-17 21:49:01 +01:00
|
|
|
}
|
2017-10-16 15:47:48 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LGB_FILE_HEADER
|
|
|
|
{
|
|
|
|
char magic[4]; // LGB 1
|
|
|
|
uint32_t fileSize;
|
|
|
|
uint32_t unknown;
|
|
|
|
char magic2[4]; // LGP1
|
|
|
|
uint32_t unknown2;
|
|
|
|
uint32_t unknown3;
|
|
|
|
uint32_t unknown4;
|
|
|
|
uint32_t unknown5;
|
|
|
|
int32_t groupCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LGB_FILE
|
|
|
|
{
|
|
|
|
LGB_FILE_HEADER header;
|
|
|
|
std::vector<LGB_GROUP> groups;
|
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
LGB_FILE( char* buf )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
|
|
|
header = *reinterpret_cast<LGB_FILE_HEADER*>(buf);
|
2017-10-17 21:49:01 +01:00
|
|
|
if( strncmp( &header.magic[0], "LGB1", 4 ) != 0 || strncmp( &header.magic2[0], "LGP1", 4 ) != 0 )
|
|
|
|
throw std::exception( "Invalid LGB file!" );
|
2017-10-16 15:47:48 +01:00
|
|
|
|
|
|
|
//groups.resize(header.groupCount);
|
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
auto baseOffset = sizeof( header );
|
|
|
|
for( auto i = 0; i < header.groupCount; ++i )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
|
|
|
auto groupOffset = baseOffset + *reinterpret_cast<int32_t*>(buf + (baseOffset + i * 4));
|
2017-10-17 21:49:01 +01:00
|
|
|
auto group = LGB_GROUP( buf, this, groupOffset );
|
|
|
|
groups.push_back( group );
|
2017-10-16 15:47:48 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-10-17 21:49:01 +01:00
|
|
|
std::map<std::string, LGB_FILE> getLgbFiles( const std::string& dir )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
|
|
|
namespace fs = std::experimental::filesystem;
|
|
|
|
std::map<std::string, LGB_FILE> fileMap;
|
2017-10-17 21:49:01 +01:00
|
|
|
for( const auto& path : fs::recursive_directory_iterator( dir ) )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
if( path.path().extension() == ".lgb" )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
|
|
|
auto strPath = path.path().string();
|
2017-10-17 21:49:01 +01:00
|
|
|
auto f = fopen( strPath.c_str(), "rb" );
|
|
|
|
fseek( f, 0, SEEK_END );
|
|
|
|
auto size = ftell( f );
|
|
|
|
std::vector<char> bytes( size );
|
|
|
|
rewind( f );
|
|
|
|
fread( bytes.data(), 1, size, f );
|
|
|
|
fclose( f );
|
2017-10-16 15:47:48 +01:00
|
|
|
try
|
|
|
|
{
|
2017-10-17 21:49:01 +01:00
|
|
|
LGB_FILE lgbFile( bytes.data() );
|
|
|
|
fileMap.insert( std::make_pair( strPath, lgbFile ) );
|
2017-10-16 15:47:48 +01:00
|
|
|
}
|
2017-10-17 21:49:01 +01:00
|
|
|
catch( std::exception& e )
|
2017-10-16 15:47:48 +01:00
|
|
|
{
|
|
|
|
std::cout << "Unable to load " << strPath << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fileMap;
|
|
|
|
}
|
|
|
|
#endif
|