metaforce/DataSpec/DNAMP3/PAK.cpp

94 lines
2.6 KiB
C++
Raw Normal View History

2015-07-06 02:07:57 +00:00
#include "PAK.hpp"
2015-07-10 05:28:08 +00:00
namespace Retro
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
{
2015-07-10 05:28:08 +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
reader.setEndian(Athena::BigEndian);
m_header.read(reader);
if (m_header.version != 2)
LogModule.report(LogVisor::FatalError, "unexpected PAK magic");
2015-07-06 02:07:57 +00:00
2015-07-10 05:28:08 +00:00
reader.seek(8, Athena::Current);
atUint32 strgSz = reader.readUint32();
reader.seek(4, Athena::Current);
atUint32 rshdSz = reader.readUint32();
reader.seek(44, Athena::Current);
m_dataOffset = 128 + strgSz + rshdSz;
2015-07-06 02:07:57 +00:00
2015-07-10 05:28:08 +00:00
atUint32 nameCount = reader.readUint32();
m_nameEntries.clear();
m_nameEntries.reserve(nameCount);
for (atUint32 n=0 ; n<nameCount ; ++n)
{
m_nameEntries.emplace_back();
m_nameEntries.back().read(reader);
}
reader.seek((reader.position() + 63) & ~63, Athena::Begin);
2015-07-06 02:07:57 +00:00
2015-07-10 05:28:08 +00:00
atUint32 count = reader.readUint32();
m_entries.clear();
m_entries.reserve(count);
m_idMap.clear();
m_idMap.reserve(count);
for (atUint32 e=0 ; e<count ; ++e)
{
m_entries.emplace_back();
m_entries.back().read(reader);
m_idMap[m_entries.back().id] = &m_entries.back();
}
m_nameMap.clear();
m_nameMap.reserve(nameCount);
for (NameEntry& entry : m_nameEntries)
{
std::unordered_map<UniqueID64, Entry*>::iterator found = m_idMap.find(entry.id);
if (found != m_idMap.end())
m_nameMap[entry.name] = found->second;
}
}
void PAK::write(Athena::io::IStreamWriter& writer) const
2015-07-06 02:07:57 +00:00
{
2015-07-10 05:28:08 +00:00
writer.setEndian(Athena::BigEndian);
m_header.write(writer);
HECL::FourCC("STRG").write(writer);
atUint32 strgSz = 4;
for (const NameEntry& entry : m_nameEntries)
strgSz += entry.name.size() + 13;
atUint32 strgPad = ((strgSz + 63) & ~63) - strgSz;
strgSz += strgPad;
writer.writeUint32(strgSz);
HECL::FourCC("RSHD").write(writer);
atUint32 rshdSz = 4 + 24 * m_entries.size();
atUint32 rshdPad = ((rshdSz + 63) & ~63) - rshdSz;
rshdSz += rshdPad;
writer.writeUint32(rshdSz);
HECL::FourCC("DATA").write(writer);
atUint32 dataSz = 0;
for (const Entry& entry : m_entries)
dataSz += (entry.size + 63) & ~63;
atUint32 dataPad = ((dataSz + 63) & ~63) - dataSz;
dataSz += dataPad;
writer.writeUint32(dataSz);
writer.seek(36, Athena::Current);
writer.writeUint32(m_nameEntries.size());
for (const NameEntry& entry : m_nameEntries)
entry.write(writer);
writer.seek(strgPad, Athena::Current);
writer.writeUint32(m_entries.size());
for (const Entry& entry : m_entries)
entry.write(writer);
writer.seek(rshdPad, Athena::Current);
2015-07-06 02:07:57 +00:00
}
2015-07-10 05:28:08 +00:00
}
}