2015-07-06 02:07:57 +00:00
|
|
|
#include "PAK.hpp"
|
2015-07-16 01:57:34 +00:00
|
|
|
#include "DNAMP3.hpp"
|
2015-07-06 02:07:57 +00:00
|
|
|
|
2016-02-13 09:02:47 +00:00
|
|
|
namespace DataSpec
|
2015-07-06 02:07:57 +00:00
|
|
|
{
|
2015-07-10 05:28:08 +00:00
|
|
|
namespace DNAMP3
|
2015-07-06 02:07:57 +00:00
|
|
|
{
|
|
|
|
|
2016-03-04 23:04:53 +00:00
|
|
|
const hecl::FourCC CMPD("CMPD");
|
2015-07-14 00:38:48 +00:00
|
|
|
|
2016-03-04 23:04:53 +00:00
|
|
|
void PAK::read(athena::io::IStreamReader& reader)
|
2015-07-06 02:07:57 +00:00
|
|
|
{
|
2015-07-10 05:28:08 +00:00
|
|
|
m_header.read(reader);
|
|
|
|
if (m_header.version != 2)
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Fatal, "unexpected PAK magic");
|
2015-07-06 02:07:57 +00:00
|
|
|
|
2016-03-04 23:04:53 +00:00
|
|
|
reader.seek(8, athena::Current);
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 strgSz = reader.readUint32Big();
|
2016-03-04 23:04:53 +00:00
|
|
|
reader.seek(4, athena::Current);
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 rshdSz = reader.readUint32Big();
|
2016-03-04 23:04:53 +00:00
|
|
|
reader.seek(44, athena::Current);
|
2015-07-14 00:38:48 +00:00
|
|
|
atUint32 dataOffset = 128 + strgSz + rshdSz;
|
2015-07-06 02:07:57 +00:00
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint64 strgBase = reader.position();
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 nameCount = reader.readUint32Big();
|
2015-07-10 05:28:08 +00:00
|
|
|
m_nameEntries.clear();
|
|
|
|
m_nameEntries.reserve(nameCount);
|
|
|
|
for (atUint32 n=0 ; n<nameCount ; ++n)
|
|
|
|
{
|
|
|
|
m_nameEntries.emplace_back();
|
|
|
|
m_nameEntries.back().read(reader);
|
|
|
|
}
|
2016-03-04 23:04:53 +00:00
|
|
|
reader.seek(strgBase + strgSz, athena::Begin);
|
2015-07-06 02:07:57 +00:00
|
|
|
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 count = reader.readUint32Big();
|
2015-07-10 05:28:08 +00:00
|
|
|
m_entries.clear();
|
|
|
|
m_entries.reserve(count);
|
2015-10-26 02:31:09 +00:00
|
|
|
m_firstEntries.clear();
|
|
|
|
m_firstEntries.reserve(count);
|
2015-07-10 05:28:08 +00:00
|
|
|
m_idMap.clear();
|
|
|
|
m_idMap.reserve(count);
|
|
|
|
for (atUint32 e=0 ; e<count ; ++e)
|
|
|
|
{
|
|
|
|
m_entries.emplace_back();
|
|
|
|
m_entries.back().read(reader);
|
2015-07-14 00:38:48 +00:00
|
|
|
m_entries.back().offset += dataOffset;
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|
2015-07-13 06:29:12 +00:00
|
|
|
for (Entry& entry : m_entries)
|
2015-10-26 02:31:09 +00:00
|
|
|
{
|
|
|
|
auto search = m_idMap.find(entry.id);
|
|
|
|
if (search == m_idMap.end())
|
|
|
|
{
|
|
|
|
m_firstEntries.push_back(&entry);
|
|
|
|
m_idMap[entry.id] = &entry;
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 05:28:08 +00:00
|
|
|
|
|
|
|
m_nameMap.clear();
|
|
|
|
m_nameMap.reserve(nameCount);
|
|
|
|
for (NameEntry& entry : m_nameEntries)
|
|
|
|
{
|
2015-07-13 06:29:12 +00:00
|
|
|
auto search = m_idMap.find(entry.id);
|
|
|
|
if (search != m_idMap.end())
|
2015-10-27 00:19:03 +00:00
|
|
|
{
|
2015-07-13 06:29:12 +00:00
|
|
|
m_nameMap[entry.name] = search->second;
|
2015-10-27 00:19:03 +00:00
|
|
|
search->second->name = entry.name;
|
|
|
|
}
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-04 23:04:53 +00:00
|
|
|
void PAK::write(athena::io::IStreamWriter& writer) const
|
2015-07-06 02:07:57 +00:00
|
|
|
{
|
2015-07-10 05:28:08 +00:00
|
|
|
m_header.write(writer);
|
|
|
|
|
2015-08-23 06:42:29 +00:00
|
|
|
DNAFourCC("STRG").write(writer);
|
2015-07-10 05:28:08 +00:00
|
|
|
atUint32 strgSz = 4;
|
|
|
|
for (const NameEntry& entry : m_nameEntries)
|
2015-07-22 19:05:18 +00:00
|
|
|
strgSz += (atUint32)entry.name.size() + 13;
|
2015-07-10 05:28:08 +00:00
|
|
|
atUint32 strgPad = ((strgSz + 63) & ~63) - strgSz;
|
|
|
|
strgSz += strgPad;
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(strgSz);
|
2015-07-10 05:28:08 +00:00
|
|
|
|
2015-08-23 06:42:29 +00:00
|
|
|
DNAFourCC("RSHD").write(writer);
|
2015-07-10 05:28:08 +00:00
|
|
|
atUint32 rshdSz = 4 + 24 * m_entries.size();
|
|
|
|
atUint32 rshdPad = ((rshdSz + 63) & ~63) - rshdSz;
|
|
|
|
rshdSz += rshdPad;
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(rshdSz);
|
2015-07-14 00:38:48 +00:00
|
|
|
atUint32 dataOffset = 128 + strgSz + rshdSz;
|
2015-07-10 05:28:08 +00:00
|
|
|
|
2015-08-23 06:42:29 +00:00
|
|
|
DNAFourCC("DATA").write(writer);
|
2015-07-10 05:28:08 +00:00
|
|
|
atUint32 dataSz = 0;
|
|
|
|
for (const Entry& entry : m_entries)
|
|
|
|
dataSz += (entry.size + 63) & ~63;
|
|
|
|
atUint32 dataPad = ((dataSz + 63) & ~63) - dataSz;
|
|
|
|
dataSz += dataPad;
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(dataSz);
|
2016-03-04 23:04:53 +00:00
|
|
|
writer.seek(36, athena::Current);
|
2015-07-10 05:28:08 +00:00
|
|
|
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big((atUint32)m_nameEntries.size());
|
2015-07-10 05:28:08 +00:00
|
|
|
for (const NameEntry& entry : m_nameEntries)
|
|
|
|
entry.write(writer);
|
2016-03-04 23:04:53 +00:00
|
|
|
writer.seek(strgPad, athena::Current);
|
2015-07-10 05:28:08 +00:00
|
|
|
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big((atUint32)m_entries.size());
|
2015-07-10 05:28:08 +00:00
|
|
|
for (const Entry& entry : m_entries)
|
2015-07-14 00:38:48 +00:00
|
|
|
{
|
|
|
|
Entry copy = entry;
|
|
|
|
copy.offset -= dataOffset;
|
|
|
|
copy.write(writer);
|
|
|
|
}
|
2016-03-04 23:04:53 +00:00
|
|
|
writer.seek(rshdPad, athena::Current);
|
2015-07-06 02:07:57 +00:00
|
|
|
}
|
2015-10-18 04:08:45 +00:00
|
|
|
size_t PAK::binarySize(size_t __isz) const
|
|
|
|
{
|
|
|
|
__isz = m_header.binarySize(__isz);
|
|
|
|
|
|
|
|
size_t strgSz = 4;
|
|
|
|
for (const NameEntry& entry : m_nameEntries)
|
|
|
|
strgSz += entry.name.size() + 13;
|
|
|
|
size_t strgPad = ((strgSz + 63) & ~63) - strgSz;
|
|
|
|
|
|
|
|
size_t rshdSz = 4 + 24 * m_entries.size();
|
|
|
|
size_t rshdPad = ((rshdSz + 63) & ~63) - rshdSz;
|
|
|
|
|
|
|
|
__isz += 60;
|
|
|
|
|
|
|
|
__isz += 4;
|
|
|
|
for (const NameEntry& entry : m_nameEntries)
|
|
|
|
__isz = entry.binarySize(__isz);
|
|
|
|
__isz += strgPad;
|
|
|
|
|
|
|
|
__isz += 4;
|
|
|
|
for (const Entry& entry : m_entries)
|
|
|
|
__isz = entry.binarySize(__isz);
|
|
|
|
__isz += rshdPad;
|
|
|
|
|
|
|
|
return __isz;
|
|
|
|
}
|
2015-07-06 02:07:57 +00:00
|
|
|
|
2016-03-04 23:04:53 +00:00
|
|
|
std::unique_ptr<atUint8[]> PAK::Entry::getBuffer(const nod::Node& pak, atUint64& szOut) const
|
2015-07-14 00:38:48 +00:00
|
|
|
{
|
|
|
|
if (compressed)
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
std::unique_ptr<nod::IPartReadStream> strm = pak.beginReadStream(offset);
|
2015-07-14 00:38:48 +00:00
|
|
|
struct
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
hecl::FourCC magic;
|
2015-07-14 00:38:48 +00:00
|
|
|
atUint32 blockCount;
|
|
|
|
} head;
|
|
|
|
strm->read(&head, 8);
|
|
|
|
if (head.magic != CMPD)
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Error, "invalid CMPD block");
|
2015-07-14 00:38:48 +00:00
|
|
|
return std::unique_ptr<atUint8[]>();
|
|
|
|
}
|
2016-03-04 23:04:53 +00:00
|
|
|
head.blockCount = hecl::SBig(head.blockCount);
|
2015-07-14 00:38:48 +00:00
|
|
|
|
|
|
|
struct Block
|
|
|
|
{
|
|
|
|
atUint32 compSz;
|
|
|
|
atUint32 decompSz;
|
2015-07-22 19:05:18 +00:00
|
|
|
};
|
|
|
|
std::unique_ptr<Block[]> blocks(new Block[head.blockCount]);
|
|
|
|
strm->read(blocks.get(), 8 * head.blockCount);
|
2015-07-14 00:38:48 +00:00
|
|
|
|
|
|
|
atUint64 maxBlockSz = 0;
|
|
|
|
atUint64 totalDecompSz = 0;
|
|
|
|
for (atUint32 b=0 ; b<head.blockCount ; ++b)
|
|
|
|
{
|
|
|
|
Block& block = blocks[b];
|
2016-03-04 23:04:53 +00:00
|
|
|
block.compSz = hecl::SBig(block.compSz) & 0xffffff;
|
|
|
|
block.decompSz = hecl::SBig(block.decompSz);
|
2015-07-14 00:38:48 +00:00
|
|
|
if (block.compSz > maxBlockSz)
|
|
|
|
maxBlockSz = block.compSz;
|
|
|
|
totalDecompSz += block.decompSz;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<atUint8[]> compBuf(new atUint8[maxBlockSz]);
|
|
|
|
atUint8* buf = new atUint8[totalDecompSz];
|
|
|
|
atUint8* bufCur = buf;
|
|
|
|
for (atUint32 b=0 ; b<head.blockCount ; ++b)
|
|
|
|
{
|
|
|
|
Block& block = blocks[b];
|
|
|
|
atUint8* compBufCur = compBuf.get();
|
|
|
|
strm->read(compBufCur, block.compSz);
|
|
|
|
if (block.compSz == block.decompSz)
|
|
|
|
{
|
|
|
|
memcpy(bufCur, compBufCur, block.decompSz);
|
|
|
|
bufCur += block.decompSz;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
atUint32 rem = block.decompSz;
|
|
|
|
while (rem)
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
atUint16 chunkSz = hecl::SBig(*(atUint16*)compBufCur);
|
2015-07-14 00:38:48 +00:00
|
|
|
compBufCur += 2;
|
|
|
|
lzo_uint dsz = rem;
|
|
|
|
lzo1x_decompress(compBufCur, chunkSz, bufCur, &dsz, nullptr);
|
|
|
|
compBufCur += chunkSz;
|
|
|
|
bufCur += dsz;
|
|
|
|
rem -= dsz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
szOut = totalDecompSz;
|
|
|
|
return std::unique_ptr<atUint8[]>(buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
atUint8* buf = new atUint8[size];
|
|
|
|
pak.beginReadStream(offset)->read(buf, size);
|
|
|
|
szOut = size;
|
|
|
|
return std::unique_ptr<atUint8[]>(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|
|
|
|
}
|