2015-07-14 00:38:48 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#include <lzo/lzo1x.h>
|
2015-07-16 01:57:34 +00:00
|
|
|
#include "DNAMP1.hpp"
|
2015-07-06 02:07:57 +00:00
|
|
|
#include "PAK.hpp"
|
|
|
|
|
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 DNAMP1
|
2015-07-06 02:07:57 +00:00
|
|
|
{
|
|
|
|
|
2015-07-10 05:28:08 +00:00
|
|
|
void PAK::read(Athena::io::IStreamReader& reader)
|
2015-07-06 02:07:57 +00:00
|
|
|
{
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 version = reader.readUint32Big();
|
2015-07-10 05:28:08 +00:00
|
|
|
if (version != 0x00030005)
|
2015-07-16 01:57:34 +00:00
|
|
|
Log.report(LogVisor::FatalError, "unexpected PAK magic");
|
2015-08-14 03:00:51 +00:00
|
|
|
reader.readUint32Big();
|
2015-07-10 05:28:08 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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();
|
2015-07-18 04:33:38 +00:00
|
|
|
Entry& ent = m_entries.back();
|
|
|
|
ent.read(reader);
|
|
|
|
if (ent.compressed && m_useLzo)
|
|
|
|
ent.compressed = 2;
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|
2015-07-12 04:26:26 +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-12 04:26:26 +00:00
|
|
|
|
2015-07-10 05:28:08 +00:00
|
|
|
m_nameMap.clear();
|
|
|
|
m_nameMap.reserve(nameCount);
|
|
|
|
for (NameEntry& entry : m_nameEntries)
|
|
|
|
{
|
|
|
|
std::unordered_map<UniqueID32, Entry*>::iterator found = m_idMap.find(entry.id);
|
|
|
|
if (found != m_idMap.end())
|
2015-10-27 00:19:03 +00:00
|
|
|
{
|
2015-07-10 05:28:08 +00:00
|
|
|
m_nameMap[entry.name] = found->second;
|
2015-10-27 00:19:03 +00:00
|
|
|
found->second->name = entry.name;
|
|
|
|
}
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|
2015-07-06 02:07:57 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 05:28:08 +00:00
|
|
|
void PAK::write(Athena::io::IStreamWriter& writer) const
|
2015-07-06 02:07:57 +00:00
|
|
|
{
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(0x00030005);
|
|
|
|
writer.writeUint32Big(0);
|
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)
|
|
|
|
{
|
2015-10-18 04:08:45 +00:00
|
|
|
NameEntry copy = entry;
|
|
|
|
copy.nameLen = copy.name.size();
|
|
|
|
copy.write(writer);
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(m_entries.size());
|
2015-07-10 05:28:08 +00:00
|
|
|
for (const Entry& entry : m_entries)
|
2015-07-18 04:33:38 +00:00
|
|
|
{
|
|
|
|
Entry tmp = entry;
|
|
|
|
if (tmp.compressed)
|
|
|
|
tmp.compressed = 1;
|
|
|
|
tmp.write(writer);
|
|
|
|
}
|
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 += 12;
|
|
|
|
|
|
|
|
for (const NameEntry& entry : m_nameEntries)
|
|
|
|
__isz += 12 + entry.name.size();
|
|
|
|
|
|
|
|
__isz += m_entries.size() * 20 + 4;
|
|
|
|
|
|
|
|
return __isz;
|
|
|
|
}
|
|
|
|
|
2015-07-18 04:33:38 +00:00
|
|
|
std::unique_ptr<atUint8[]>
|
2015-10-04 05:08:56 +00:00
|
|
|
PAK::Entry::getBuffer(const NOD::Node& pak, atUint64& szOut) const
|
2015-07-14 00:38:48 +00:00
|
|
|
{
|
|
|
|
if (compressed)
|
|
|
|
{
|
|
|
|
std::unique_ptr<NOD::IPartReadStream> strm = pak.beginReadStream(offset);
|
|
|
|
|
|
|
|
atUint32 decompSz;
|
|
|
|
strm->read(&decompSz, 4);
|
|
|
|
decompSz = HECL::SBig(decompSz);
|
|
|
|
atUint8* buf = new atUint8[decompSz];
|
|
|
|
atUint8* bufCur = buf;
|
|
|
|
|
2015-07-18 04:33:38 +00:00
|
|
|
atUint8 compBuf[0x8000];
|
|
|
|
if (compressed == 1)
|
2015-07-14 00:38:48 +00:00
|
|
|
{
|
|
|
|
atUint32 compRem = size - 4;
|
2015-07-18 04:33:38 +00:00
|
|
|
z_stream zs = {};
|
2015-07-14 00:38:48 +00:00
|
|
|
inflateInit(&zs);
|
|
|
|
zs.avail_out = decompSz;
|
|
|
|
zs.next_out = buf;
|
|
|
|
while (zs.avail_out)
|
|
|
|
{
|
2015-09-26 03:12:08 +00:00
|
|
|
atUint64 readSz = strm->read(compBuf, std::min(compRem, atUint32(0x8000)));
|
2015-07-14 00:38:48 +00:00
|
|
|
compRem -= readSz;
|
|
|
|
zs.avail_in = readSz;
|
|
|
|
zs.next_in = compBuf;
|
|
|
|
inflate(&zs, Z_FINISH);
|
|
|
|
}
|
|
|
|
inflateEnd(&zs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
atUint32 rem = decompSz;
|
|
|
|
while (rem)
|
|
|
|
{
|
|
|
|
atUint16 chunkSz;
|
|
|
|
strm->read(&chunkSz, 2);
|
|
|
|
chunkSz = HECL::SBig(chunkSz);
|
|
|
|
strm->read(compBuf, chunkSz);
|
|
|
|
lzo_uint dsz = rem;
|
|
|
|
lzo1x_decompress(compBuf, chunkSz, bufCur, &dsz, nullptr);
|
|
|
|
bufCur += dsz;
|
|
|
|
rem -= dsz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
szOut = decompSz;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|