metaforce/DataSpec/DNAMP2/STRG.cpp

219 lines
6.4 KiB
C++
Raw Normal View History

#include "STRG.hpp"
2015-07-16 01:57:34 +00:00
#include "DNAMP2.hpp"
2018-12-08 05:30:43 +00:00
namespace DataSpec::DNAMP2 {
void STRG::_read(athena::io::IStreamReader& reader) {
atUint32 langCount = reader.readUint32Big();
atUint32 strCount = reader.readUint32Big();
std::vector<FourCC> readLangs;
readLangs.reserve(langCount);
for (atUint32 l = 0; l < langCount; ++l) {
DNAFourCC lang;
lang.read(reader);
readLangs.emplace_back(lang);
reader.seek(8);
}
atUint32 nameCount = reader.readUint32Big();
atUint32 nameTableSz = reader.readUint32Big();
std::unique_ptr<uint8_t[]> nameTableBuf(new uint8_t[nameTableSz]);
reader.readUBytesToBuf(nameTableBuf.get(), nameTableSz);
struct NameIdxEntry {
atUint32 nameOff;
atUint32 strIdx;
}* nameIndex = (NameIdxEntry*)nameTableBuf.get();
for (atUint32 n = 0; n < nameCount; ++n) {
const char* name = (char*)(nameTableBuf.get() + hecl::SBig(nameIndex[n].nameOff));
names[name] = hecl::SBig(nameIndex[n].strIdx);
}
langs.clear();
langs.reserve(langCount);
for (FourCC& lang : readLangs) {
std::vector<std::u16string> strs;
reader.seek(strCount * 4);
for (atUint32 s = 0; s < strCount; ++s)
strs.emplace_back(reader.readU16StringBig());
langs.emplace_back(lang, strs);
}
langMap.clear();
langMap.reserve(langCount);
for (std::pair<FourCC, std::vector<std::u16string>>& item : langs)
langMap.emplace(item.first, &item.second);
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void STRG::Enumerate<BigDNA::Read>(athena::io::IStreamReader& reader) {
atUint32 magic = reader.readUint32Big();
if (magic != 0x87654321)
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Error, FMT_STRING("invalid STRG magic"));
2018-12-08 05:30:43 +00:00
atUint32 version = reader.readUint32Big();
if (version != 1)
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Error, FMT_STRING("invalid STRG version"));
2018-12-08 05:30:43 +00:00
_read(reader);
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void STRG::Enumerate<BigDNA::Write>(athena::io::IStreamWriter& writer) {
writer.writeUint32Big(0x87654321);
writer.writeUint32Big(1);
writer.writeUint32Big(langs.size());
atUint32 strCount = STRG::count();
writer.writeUint32Big(strCount);
atUint32 offset = 0;
for (const auto& lang : langs) {
DNAFourCC{lang.first}.write(writer);
2018-12-08 05:30:43 +00:00
writer.writeUint32Big(offset);
offset += strCount * 4 + 4;
atUint32 langStrCount = lang.second.size();
atUint32 tableSz = strCount * 4;
for (atUint32 s = 0; s < strCount; ++s) {
atUint32 chCount = lang.second[s].size();
if (s < langStrCount) {
offset += chCount * 2 + 1;
tableSz += chCount * 2 + 1;
} else {
offset += 1;
tableSz += 1;
}
}
2018-12-08 05:30:43 +00:00
writer.writeUint32Big(tableSz);
}
atUint32 nameTableSz = names.size() * 8;
for (const auto& name : names) {
2018-12-08 05:30:43 +00:00
nameTableSz += name.first.size() + 1;
}
2018-12-08 05:30:43 +00:00
writer.writeUint32Big(names.size());
writer.writeUint32Big(nameTableSz);
offset = names.size() * 8;
for (const auto& name : names) {
2018-12-08 05:30:43 +00:00
writer.writeUint32Big(offset);
writer.writeInt32Big(name.second);
offset += name.first.size() + 1;
}
for (const auto& name : names) {
2018-12-08 05:30:43 +00:00
writer.writeString(name.first);
}
2018-12-08 05:30:43 +00:00
for (const auto& lang : langs) {
2018-12-08 05:30:43 +00:00
offset = strCount * 4;
atUint32 langStrCount = lang.second.size();
for (atUint32 s = 0; s < strCount; ++s) {
writer.writeUint32Big(offset);
if (s < langStrCount)
offset += lang.second[s].size() * 2 + 1;
else
offset += 1;
}
2018-12-08 05:30:43 +00:00
for (atUint32 s = 0; s < strCount; ++s) {
if (s < langStrCount)
writer.writeU16StringBig(lang.second[s]);
else
writer.writeUByte(0);
}
2018-12-08 05:30:43 +00:00
}
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void STRG::Enumerate<BigDNA::BinarySize>(size_t& __isz) {
__isz += 16;
__isz += langs.size() * 12;
__isz += 8;
__isz += names.size() * 8;
for (const auto& name : names) {
2018-12-08 05:30:43 +00:00
__isz += name.first.size() + 1;
}
2018-12-08 05:30:43 +00:00
size_t strCount = STRG::count();
for (const auto& lang : langs) {
2018-12-08 05:30:43 +00:00
atUint32 langStrCount = lang.second.size();
__isz += strCount * 4;
for (atUint32 s = 0; s < strCount; ++s) {
if (s < langStrCount)
__isz += (lang.second[s].size() + 1) * 2;
else
__isz += 1;
}
2018-12-08 05:30:43 +00:00
}
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void STRG::Enumerate<BigDNA::ReadYaml>(athena::io::YAMLDocReader& reader) {
const athena::io::YAMLNode* root = reader.getRootNode();
/* Validate Pass */
if (root->m_type == YAML_MAPPING_NODE) {
for (const auto& lang : root->m_mapChildren) {
2019-10-01 07:38:03 +00:00
if (lang.first == "names")
2018-12-08 05:30:43 +00:00
continue;
if (lang.first.size() != 4) {
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Warning, FMT_STRING("STRG language string '{}' must be exactly 4 characters; skipping"), lang.first);
2015-08-09 03:55:11 +00:00
return;
2018-12-08 05:30:43 +00:00
}
if (lang.second->m_type != YAML_SEQUENCE_NODE) {
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Warning, FMT_STRING("STRG language string '{}' must contain a sequence; skipping"), lang.first);
2018-12-08 05:30:43 +00:00
return;
}
for (const auto& str : lang.second->m_seqChildren) {
if (str->m_type != YAML_SCALAR_NODE) {
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Warning, FMT_STRING("STRG language '{}' must contain all scalars; skipping"), lang.first);
2018-12-08 05:30:43 +00:00
return;
}
}
2015-08-09 03:55:11 +00:00
}
2018-12-08 05:30:43 +00:00
} else {
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Warning, FMT_STRING("STRG must have a mapping root node; skipping"));
2018-12-08 05:30:43 +00:00
return;
}
/* Read Pass */
langs.clear();
for (const auto& lang : root->m_mapChildren) {
std::vector<std::u16string> strs;
for (const auto& str : lang.second->m_seqChildren)
strs.emplace_back(hecl::UTF8ToChar16(str->m_scalarString));
langs.emplace_back(FourCC(lang.first.c_str()), strs);
}
names.clear();
const athena::io::YAMLNode* namesNode = root->findMapChild("names");
if (namesNode)
for (const auto& item : namesNode->m_mapChildren)
names[item.first] = athena::io::NodeToVal<atInt32>(item.second.get());
langMap.clear();
langMap.reserve(langs.size());
for (std::pair<FourCC, std::vector<std::u16string>>& item : langs)
langMap.emplace(item.first, &item.second);
2015-08-09 03:55:11 +00:00
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void STRG::Enumerate<BigDNA::WriteYaml>(athena::io::YAMLDocWriter& writer) {
for (const auto& lang : langs) {
2019-10-01 07:38:03 +00:00
if (auto v = writer.enterSubVector(lang.first.toString()))
2018-12-08 05:30:43 +00:00
for (const std::u16string& str : lang.second)
2019-10-01 07:38:03 +00:00
writer.writeU16String(str);
2018-12-08 05:30:43 +00:00
}
if (names.size()) {
if (auto rec = writer.enterSubRecord("names"))
for (const auto& name : names)
2019-10-01 07:38:03 +00:00
if (auto rec = writer.enterSubRecord(name.first))
writer.writeInt32(name.second);
2018-12-08 05:30:43 +00:00
}
2015-08-09 03:55:11 +00:00
}
2019-10-01 07:38:03 +00:00
std::string_view STRG::DNAType() { return "urde::DNAMP2::STRG"sv; }
2015-10-01 00:40:21 +00:00
2018-12-08 05:30:43 +00:00
} // namespace DataSpec::DNAMP2