metaforce/hecl/lib/Blender/SDNARead.cpp

191 lines
5.4 KiB
C++
Raw Normal View History

2018-10-22 08:14:32 +00:00
#include "hecl/Blender/SDNARead.hpp"
#include <cstring>
#include <string>
#include "hecl/hecl.hpp"
#include <athena/FileReader.hpp>
#include <athena/MemoryReader.hpp>
2018-10-22 08:14:32 +00:00
#include <zlib.h>
2018-12-08 05:18:42 +00:00
namespace hecl::blender {
void SDNABlock::SDNAStruct::computeOffsets(const SDNABlock& block) {
atUint32 offset = 0;
for (SDNAField& f : fields) {
const auto& name = block.names[f.name];
f.offset = offset;
if (name.front() == '*') {
offset += 8;
} else {
atUint32 length = block.tlens[f.type];
auto bracket = name.find('[');
if (bracket != std::string::npos)
length *= strtoul(name.data() + bracket + 1, nullptr, 10);
offset += length;
2018-10-22 08:14:32 +00:00
}
2018-12-08 05:18:42 +00:00
}
2018-10-22 08:14:32 +00:00
}
2018-12-08 05:18:42 +00:00
const SDNABlock::SDNAStruct::SDNAField* SDNABlock::SDNAStruct::lookupField(const SDNABlock& block,
const char* n) const {
for (const SDNAField& field : fields) {
const auto& name = block.names[field.name];
auto bracket = name.find('[');
if (bracket != std::string::npos) {
if (!name.compare(0, bracket, n))
return &field;
2019-10-01 07:23:35 +00:00
} else if (name == n)
2018-12-08 05:18:42 +00:00
return &field;
}
return nullptr;
2018-10-22 08:14:32 +00:00
}
const SDNABlock::SDNAStruct* SDNABlock::lookupStruct(const char* n, atUint32& idx) const {
2018-12-08 05:18:42 +00:00
idx = 0;
for (const SDNAStruct& strc : strcs) {
const auto& name = types[strc.type];
2019-10-01 07:23:35 +00:00
if (name == n)
2018-12-08 05:18:42 +00:00
return &strc;
++idx;
}
return nullptr;
2018-10-22 08:14:32 +00:00
}
2018-12-08 05:18:42 +00:00
void SDNARead::enumerate(const std::function<bool(const FileBlock& block, athena::io::MemoryReader& r)>& func) const {
athena::io::MemoryReader r(m_data.data(), m_data.size());
r.seek(12);
while (r.position() < r.length()) {
FileBlock block;
block.read(r);
if (block.type == FOURCC('ENDB'))
break;
athena::io::MemoryReader r2(m_data.data() + r.position(), block.size);
if (!func(block, r2))
break;
r.seek(block.size);
}
2018-10-22 08:14:32 +00:00
}
2018-12-08 05:18:42 +00:00
SDNARead::SDNARead(SystemStringView path) {
athena::io::FileReader r(path);
if (r.hasError())
return;
atUint64 length = r.length();
char magicBuf[7];
r.readUBytesToBuf(magicBuf, 7);
r.seek(0, athena::SeekOrigin::Begin);
2018-12-08 05:18:42 +00:00
if (strncmp(magicBuf, "BLENDER", 7)) {
/* Try gzip decompression */
std::unique_ptr<uint8_t[]> compBuf(new uint8_t[4096]);
m_data.resize((length * 2 + 4095) & ~4095);
z_stream zstrm = {};
inflateInit2(&zstrm, 16 + MAX_WBITS);
zstrm.next_out = (Bytef*)m_data.data();
zstrm.avail_out = m_data.size();
zstrm.total_out = 0;
atUint64 rs;
while ((rs = r.readUBytesToBuf(compBuf.get(), 4096))) {
int inflateRet;
zstrm.next_in = compBuf.get();
zstrm.avail_in = rs;
while (zstrm.avail_in) {
if (!zstrm.avail_out) {
zstrm.avail_out = m_data.size();
m_data.resize(zstrm.avail_out * 2);
zstrm.next_out = (Bytef*)m_data.data() + zstrm.avail_out;
}
inflateRet = inflate(&zstrm, Z_NO_FLUSH);
if (inflateRet == Z_STREAM_END)
break;
if (inflateRet != Z_OK) {
inflateEnd(&zstrm);
m_data = std::vector<uint8_t>();
return;
2018-10-22 08:14:32 +00:00
}
2018-12-08 05:18:42 +00:00
}
if (inflateRet == Z_STREAM_END)
break;
}
2018-10-22 08:14:32 +00:00
2018-12-08 05:18:42 +00:00
inflateEnd(&zstrm);
2018-10-22 08:14:32 +00:00
2018-12-08 05:18:42 +00:00
if (strncmp((char*)m_data.data(), "BLENDER", 7)) {
m_data = std::vector<uint8_t>();
return;
2018-10-22 08:14:32 +00:00
}
2018-12-08 05:18:42 +00:00
} else {
m_data.resize(length);
r.readUBytesToBuf(m_data.data(), length);
}
enumerate([this](const FileBlock& block, athena::io::MemoryReader& r) {
if (block.type == FOURCC('DNA1')) {
m_sdnaBlock.read(r);
for (SDNABlock::SDNAStruct& s : m_sdnaBlock.strcs)
s.computeOffsets(m_sdnaBlock);
return false;
2018-10-22 08:14:32 +00:00
}
2018-12-08 05:18:42 +00:00
return true;
});
2018-10-22 08:14:32 +00:00
}
2018-12-08 05:18:42 +00:00
BlendType GetBlendType(SystemStringView path) {
SDNARead r(path);
if (!r)
return BlendType::None;
atUint32 idPropIdx;
2018-12-08 05:18:42 +00:00
const auto* idPropStruct = r.sdnaBlock().lookupStruct("IDProperty", idPropIdx);
if (!idPropStruct)
return BlendType::None;
const auto* typeField = idPropStruct->lookupField(r.sdnaBlock(), "type");
if (!typeField)
return BlendType::None;
atUint32 typeOffset = typeField->offset;
const auto* nameField = idPropStruct->lookupField(r.sdnaBlock(), "name");
if (!nameField)
return BlendType::None;
atUint32 nameOffset = nameField->offset;
const auto* dataField = idPropStruct->lookupField(r.sdnaBlock(), "data");
if (!dataField)
return BlendType::None;
atUint32 dataOffset = dataField->offset;
atUint32 idPropDataIdx;
2018-12-08 05:18:42 +00:00
const auto* idPropDataStruct = r.sdnaBlock().lookupStruct("IDPropertyData", idPropDataIdx);
if (!idPropDataStruct)
return BlendType::None;
const auto* valField = idPropDataStruct->lookupField(r.sdnaBlock(), "val");
if (!valField)
return BlendType::None;
atUint32 valOffset = dataOffset + valField->offset;
BlendType ret = BlendType::None;
r.enumerate(
[idPropIdx, typeOffset, nameOffset, valOffset, &ret](const FileBlock& block, athena::io::MemoryReader& r) {
if (block.type == FOURCC('DATA') && block.sdnaIdx == idPropIdx) {
r.seek(typeOffset, athena::SeekOrigin::Begin);
2018-12-08 05:18:42 +00:00
if (r.readUByte() != 1)
return true;
r.seek(nameOffset, athena::SeekOrigin::Begin);
2018-12-08 05:18:42 +00:00
if (r.readString() != "hecl_type")
return true;
r.seek(valOffset, athena::SeekOrigin::Begin);
2018-12-08 05:18:42 +00:00
ret = BlendType(r.readUint32Little());
return false;
2018-10-22 08:14:32 +00:00
}
return true;
2018-12-08 05:18:42 +00:00
});
2018-10-22 08:14:32 +00:00
2018-12-08 05:18:42 +00:00
return ret;
2018-10-22 08:14:32 +00:00
}
2018-12-08 05:18:42 +00:00
} // namespace hecl::blender