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"
|
|
|
|
|
2017-12-29 08:08:12 +00:00
|
|
|
namespace DataSpec::DNAMP1
|
2015-07-06 02:07:57 +00:00
|
|
|
{
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void PAK::Enumerate<BigDNA::Read>(typename Read::StreamT& 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)
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Fatal, "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);
|
2018-04-07 20:55:57 +00:00
|
|
|
std::vector<Entry> entries;
|
|
|
|
entries.reserve(count);
|
2015-07-10 05:28:08 +00:00
|
|
|
for (atUint32 e=0 ; e<count ; ++e)
|
|
|
|
{
|
2018-04-07 20:55:57 +00:00
|
|
|
entries.emplace_back();
|
|
|
|
entries.back().read(reader);
|
|
|
|
}
|
|
|
|
for (atUint32 e=0 ; e<count ; ++e)
|
|
|
|
{
|
|
|
|
Entry& entry = entries[e];
|
2017-03-10 18:00:40 +00:00
|
|
|
if (entry.compressed && m_useLzo)
|
|
|
|
entry.compressed = 2;
|
|
|
|
|
|
|
|
auto search = m_entries.find(entry.id);
|
|
|
|
if (search == m_entries.end())
|
2015-10-26 02:31:09 +00:00
|
|
|
{
|
2017-03-10 18:00:40 +00:00
|
|
|
m_firstEntries.push_back(entry.id);
|
|
|
|
m_entries[entry.id] = std::move(entry);
|
2015-10-26 02:31:09 +00:00
|
|
|
}
|
2018-04-07 20:55:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Find next MREA to record which area has dupes */
|
|
|
|
for (atUint32 e2=e+1 ; e2<count ; ++e2)
|
|
|
|
{
|
|
|
|
Entry& entry2 = entries[e2];
|
|
|
|
if (entry2.type != FOURCC('MREA'))
|
|
|
|
continue;
|
|
|
|
m_dupeMREAs.insert(entry2.id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 02:31:09 +00:00
|
|
|
}
|
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)
|
2017-03-10 18:00:40 +00:00
|
|
|
m_nameMap[entry.name] = entry.id;
|
2015-07-06 02:07:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void PAK::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
|
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());
|
2017-03-10 18:00:40 +00:00
|
|
|
for (const auto& entry : m_entries)
|
2015-07-18 04:33:38 +00:00
|
|
|
{
|
2017-03-10 18:00:40 +00:00
|
|
|
Entry tmp = entry.second;
|
2015-07-18 04:33:38 +00:00
|
|
|
if (tmp.compressed)
|
|
|
|
tmp.compressed = 1;
|
|
|
|
tmp.write(writer);
|
|
|
|
}
|
2015-07-06 02:07:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void PAK::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s)
|
2015-10-18 04:08:45 +00:00
|
|
|
{
|
2018-02-22 07:24:51 +00:00
|
|
|
s += 12;
|
2015-10-18 04:08:45 +00:00
|
|
|
|
|
|
|
for (const NameEntry& entry : m_nameEntries)
|
2018-02-22 07:24:51 +00:00
|
|
|
s += 12 + entry.name.size();
|
2015-10-18 04:08:45 +00:00
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
s += m_entries.size() * 20 + 4;
|
2015-10-18 04:08:45 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 04:33:38 +00:00
|
|
|
std::unique_ptr<atUint8[]>
|
2016-03-04 23:04:53 +00:00
|
|
|
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
|
|
|
|
|
|
|
atUint32 decompSz;
|
|
|
|
strm->read(&decompSz, 4);
|
2016-03-04 23:04:53 +00:00
|
|
|
decompSz = hecl::SBig(decompSz);
|
2015-07-14 00:38:48 +00:00
|
|
|
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);
|
2016-03-04 23:04:53 +00:00
|
|
|
chunkSz = hecl::SBig(chunkSz);
|
2015-07-14 00:38:48 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 18:00:40 +00:00
|
|
|
const PAK::Entry* PAK::lookupEntry(const UniqueID32& id) const
|
|
|
|
{
|
|
|
|
auto result = m_entries.find(id);
|
|
|
|
if (result != m_entries.end())
|
|
|
|
return &result->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-13 06:19:18 +00:00
|
|
|
const PAK::Entry* PAK::lookupEntry(std::string_view name) const
|
2017-03-10 18:00:40 +00:00
|
|
|
{
|
2017-11-13 06:19:18 +00:00
|
|
|
auto result = m_nameMap.find(name.data());
|
2017-03-10 18:00:40 +00:00
|
|
|
if (result != m_nameMap.end())
|
|
|
|
{
|
|
|
|
auto result1 = m_entries.find(result->second);
|
|
|
|
if (result1 != m_entries.end())
|
|
|
|
return &result1->second;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PAK::bestEntryName(const Entry& entry, bool& named) const
|
|
|
|
{
|
|
|
|
/* Prefer named entries first */
|
|
|
|
for (const NameEntry& nentry : m_nameEntries)
|
|
|
|
if (nentry.id == entry.id)
|
|
|
|
{
|
|
|
|
named = true;
|
|
|
|
return nentry.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise return ID format string */
|
|
|
|
named = false;
|
|
|
|
return entry.type.toString() + '_' + entry.id.toString();
|
|
|
|
}
|
|
|
|
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|