2015-07-14 00:38:48 +00:00
|
|
|
#include <zlib.h>
|
2018-12-20 03:45:48 +00:00
|
|
|
#include <lzokay.hpp>
|
2015-07-16 01:57:34 +00:00
|
|
|
#include "DNAMP1.hpp"
|
2015-07-06 02:07:57 +00:00
|
|
|
#include "PAK.hpp"
|
2018-09-03 00:46:16 +00:00
|
|
|
#include "AGSC.hpp"
|
2015-07-06 02:07:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
namespace DataSpec::DNAMP1 {
|
2015-07-06 02:07:57 +00:00
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
2018-12-08 05:30:43 +00:00
|
|
|
void PAK::Enumerate<BigDNA::Read>(typename Read::StreamT& reader) {
|
|
|
|
atUint32 version = reader.readUint32Big();
|
|
|
|
if (version != 0x00030005)
|
2020-04-11 22:51:39 +00:00
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("unexpected PAK magic"));
|
2018-12-08 05:30:43 +00:00
|
|
|
reader.readUint32Big();
|
|
|
|
|
|
|
|
atUint32 nameCount = reader.readUint32Big();
|
|
|
|
m_nameEntries.clear();
|
|
|
|
m_nameEntries.reserve(nameCount);
|
|
|
|
for (atUint32 n = 0; n < nameCount; ++n) {
|
|
|
|
m_nameEntries.emplace_back();
|
|
|
|
m_nameEntries.back().read(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
atUint32 count = reader.readUint32Big();
|
|
|
|
m_entries.clear();
|
|
|
|
m_entries.reserve(count);
|
|
|
|
m_firstEntries.clear();
|
|
|
|
m_firstEntries.reserve(count);
|
|
|
|
std::vector<Entry> entries;
|
|
|
|
entries.reserve(count);
|
|
|
|
for (atUint32 e = 0; e < count; ++e) {
|
|
|
|
entries.emplace_back();
|
|
|
|
entries.back().read(reader);
|
|
|
|
}
|
|
|
|
for (atUint32 e = 0; e < count; ++e) {
|
|
|
|
Entry& entry = entries[e];
|
|
|
|
if (entry.compressed && m_useLzo)
|
|
|
|
entry.compressed = 2;
|
|
|
|
|
|
|
|
auto search = m_entries.find(entry.id);
|
|
|
|
if (search == m_entries.end()) {
|
|
|
|
m_firstEntries.push_back(entry.id);
|
|
|
|
m_entries[entry.id] = std::move(entry);
|
|
|
|
} 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-07-10 05:28:08 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2015-07-10 05:28:08 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
m_nameMap.clear();
|
|
|
|
m_nameMap.reserve(nameCount);
|
|
|
|
for (NameEntry& entry : m_nameEntries)
|
|
|
|
m_nameMap[entry.name] = entry.id;
|
2015-07-06 02:07:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
2018-12-08 05:30:43 +00:00
|
|
|
void PAK::Enumerate<BigDNA::Write>(typename Write::StreamT& writer) {
|
|
|
|
writer.writeUint32Big(0x00030005);
|
|
|
|
writer.writeUint32Big(0);
|
|
|
|
|
|
|
|
writer.writeUint32Big((atUint32)m_nameEntries.size());
|
|
|
|
for (const NameEntry& entry : m_nameEntries) {
|
|
|
|
NameEntry copy = entry;
|
|
|
|
copy.nameLen = copy.name.size();
|
|
|
|
copy.write(writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.writeUint32Big(m_entries.size());
|
|
|
|
for (const auto& entry : m_entries) {
|
|
|
|
Entry tmp = entry.second;
|
|
|
|
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 <>
|
2018-12-08 05:30:43 +00:00
|
|
|
void PAK::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s) {
|
|
|
|
s += 12;
|
2015-10-18 04:08:45 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
for (const NameEntry& entry : m_nameEntries)
|
|
|
|
s += 12 + entry.name.size();
|
2015-10-18 04:08:45 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
s += m_entries.size() * 20 + 4;
|
2015-10-18 04:08:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
std::unique_ptr<atUint8[]> PAK::Entry::getBuffer(const nod::Node& pak, atUint64& szOut) const {
|
|
|
|
if (compressed) {
|
|
|
|
std::unique_ptr<nod::IPartReadStream> strm = pak.beginReadStream(offset);
|
|
|
|
|
|
|
|
atUint32 decompSz;
|
|
|
|
strm->read(&decompSz, 4);
|
|
|
|
decompSz = hecl::SBig(decompSz);
|
2020-04-05 14:16:07 +00:00
|
|
|
std::unique_ptr<atUint8[]> buf{new atUint8[decompSz]};
|
|
|
|
atUint8* bufCur = buf.get();
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
atUint8 compBuf[0x8000];
|
|
|
|
if (compressed == 1) {
|
|
|
|
atUint32 compRem = size - 4;
|
|
|
|
z_stream zs = {};
|
|
|
|
inflateInit(&zs);
|
|
|
|
zs.avail_out = decompSz;
|
2020-04-05 14:16:07 +00:00
|
|
|
zs.next_out = buf.get();
|
2018-12-08 05:30:43 +00:00
|
|
|
while (zs.avail_out) {
|
|
|
|
atUint64 readSz = strm->read(compBuf, std::min(compRem, atUint32(0x8000)));
|
|
|
|
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);
|
2019-06-15 00:39:20 +00:00
|
|
|
size_t dsz;
|
|
|
|
lzokay::decompress(compBuf, chunkSz, bufCur, rem, dsz);
|
2018-12-08 05:30:43 +00:00
|
|
|
bufCur += dsz;
|
|
|
|
rem -= dsz;
|
|
|
|
}
|
2015-07-14 00:38:48 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
szOut = decompSz;
|
2020-04-05 14:16:07 +00:00
|
|
|
return buf;
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2020-04-05 14:16:07 +00:00
|
|
|
std::unique_ptr<atUint8[]> buf{new atUint8[size]};
|
|
|
|
pak.beginReadStream(offset)->read(buf.get(), size);
|
2018-12-08 05:30:43 +00:00
|
|
|
szOut = size;
|
2020-04-05 14:16:07 +00:00
|
|
|
return buf;
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2015-07-14 00:38:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +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-03-10 18:00:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
const PAK::Entry* PAK::lookupEntry(std::string_view name) const {
|
2020-04-11 22:51:39 +00:00
|
|
|
// TODO: Heterogeneous lookup when C++20 available
|
2018-12-08 05:30:43 +00:00
|
|
|
auto result = m_nameMap.find(name.data());
|
|
|
|
if (result != m_nameMap.end()) {
|
|
|
|
auto result1 = m_entries.find(result->second);
|
|
|
|
if (result1 != m_entries.end())
|
|
|
|
return &result1->second;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2017-03-10 18:00:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 07:38:03 +00:00
|
|
|
std::string PAK::bestEntryName(const nod::Node& pakNode, const Entry& entry, std::string& catalogueName) const {
|
2018-12-08 05:30:43 +00:00
|
|
|
std::unordered_map<UniqueID32, Entry>::const_iterator search;
|
|
|
|
if (entry.type == FOURCC('AGSC') && (search = m_entries.find(entry.id)) != m_entries.cend()) {
|
|
|
|
/* Use internal AGSC name for entry */
|
|
|
|
auto rs = search->second.beginReadStream(pakNode);
|
|
|
|
AGSC::Header header;
|
|
|
|
header.read(rs);
|
2019-10-01 07:38:03 +00:00
|
|
|
catalogueName = header.groupName;
|
2020-04-11 22:51:39 +00:00
|
|
|
return fmt::format(FMT_STRING("{}_{}"), header.groupName, entry.id);
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Prefer named entries first */
|
2019-10-01 07:38:03 +00:00
|
|
|
for (const NameEntry& nentry : m_nameEntries) {
|
2018-12-08 05:30:43 +00:00
|
|
|
if (nentry.id == entry.id) {
|
2019-10-01 07:38:03 +00:00
|
|
|
catalogueName = nentry.name;
|
2020-04-11 22:51:39 +00:00
|
|
|
return fmt::format(FMT_STRING("{}_{}"), nentry.name, entry.id);
|
2018-09-03 00:46:16 +00:00
|
|
|
}
|
2019-10-01 07:38:03 +00:00
|
|
|
}
|
2018-09-03 00:46:16 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
/* Otherwise return ID format string */
|
2020-04-11 22:51:39 +00:00
|
|
|
return fmt::format(FMT_STRING("{}_{}"), entry.type, entry.id);
|
2017-03-10 18:00:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
} // namespace DataSpec::DNAMP1
|