2015-07-13 06:29:12 +00:00
|
|
|
#include "STRG.hpp"
|
2015-07-16 01:57:34 +00:00
|
|
|
#include "DNAMP3.hpp"
|
2015-07-13 06:29:12 +00:00
|
|
|
|
2017-12-29 08:08:12 +00:00
|
|
|
namespace DataSpec::DNAMP3
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
|
|
|
|
2016-03-04 23:04:53 +00:00
|
|
|
void STRG::_read(athena::io::IStreamReader& reader)
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 langCount = reader.readUint32Big();
|
|
|
|
atUint32 strCount = reader.readUint32Big();
|
2015-07-13 06:29:12 +00:00
|
|
|
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 nameCount = reader.readUint32Big();
|
|
|
|
atUint32 nameTableSz = reader.readUint32Big();
|
2015-07-14 00:38:48 +00:00
|
|
|
if (nameTableSz)
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
2015-07-14 00:38:48 +00:00
|
|
|
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)
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
const char* name = (char*)(nameTableBuf.get() + hecl::SBig(nameIndex[n].nameOff));
|
|
|
|
names[name] = hecl::SBig(nameIndex[n].strIdx);
|
2015-07-14 00:38:48 +00:00
|
|
|
}
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 06:42:29 +00:00
|
|
|
std::vector<DNAFourCC> readLangs;
|
2015-07-13 06:29:12 +00:00
|
|
|
readLangs.reserve(langCount);
|
|
|
|
for (atUint32 l=0 ; l<langCount ; ++l)
|
|
|
|
{
|
2015-08-23 06:42:29 +00:00
|
|
|
DNAFourCC lang;
|
2015-07-13 06:29:12 +00:00
|
|
|
lang.read(reader);
|
|
|
|
readLangs.emplace_back(lang);
|
2015-07-14 00:38:48 +00:00
|
|
|
}
|
|
|
|
std::unique_ptr<atUint32[]> strOffs(new atUint32[langCount * strCount]);
|
|
|
|
for (atUint32 l=0 ; l<langCount ; ++l)
|
|
|
|
{
|
2015-08-14 03:00:51 +00:00
|
|
|
reader.readUint32Big();
|
2015-07-14 00:38:48 +00:00
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
2015-08-14 03:00:51 +00:00
|
|
|
strOffs[l*strCount+s] = reader.readUint32Big();
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 00:38:48 +00:00
|
|
|
atUint64 strBase = reader.position();
|
2015-07-13 06:29:12 +00:00
|
|
|
langs.clear();
|
|
|
|
langs.reserve(langCount);
|
2015-07-14 00:38:48 +00:00
|
|
|
for (atUint32 l=0 ; l<langCount ; ++l)
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> strs;
|
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
reader.seek(strBase + strOffs[l*strCount+s], athena::Begin);
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 len = reader.readUint32Big();
|
2015-07-13 06:29:12 +00:00
|
|
|
strs.emplace_back(reader.readString(len));
|
|
|
|
}
|
2015-07-18 04:33:38 +00:00
|
|
|
langs.emplace_back(readLangs[l], strs);
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|
2015-07-18 04:33:38 +00:00
|
|
|
|
|
|
|
langMap.clear();
|
|
|
|
langMap.reserve(langCount);
|
2015-08-23 06:42:29 +00:00
|
|
|
for (std::pair<DNAFourCC, std::vector<std::string>>& item : langs)
|
2015-07-18 04:33:38 +00:00
|
|
|
langMap.emplace(item.first, &item.second);
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void STRG::Enumerate<BigDNA::Read>(athena::io::IStreamReader& reader)
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 magic = reader.readUint32Big();
|
2015-07-13 06:29:12 +00:00
|
|
|
if (magic != 0x87654321)
|
2015-07-14 00:38:48 +00:00
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Error, "invalid STRG magic");
|
2015-07-14 00:38:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-13 06:29:12 +00:00
|
|
|
|
2015-08-14 03:00:51 +00:00
|
|
|
atUint32 version = reader.readUint32Big();
|
2015-07-13 06:29:12 +00:00
|
|
|
if (version != 3)
|
2015-07-14 00:38:48 +00:00
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Error, "invalid STRG version");
|
2015-07-14 00:38:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-13 06:29:12 +00:00
|
|
|
|
|
|
|
_read(reader);
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void STRG::Enumerate<BigDNA::ReadYaml>(athena::io::YAMLDocReader& reader)
|
2015-08-09 03:55:11 +00:00
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
const athena::io::YAMLNode* root = reader.getRootNode();
|
2015-08-09 03:55:11 +00:00
|
|
|
|
|
|
|
/* Validate Pass */
|
|
|
|
if (root->m_type == YAML_MAPPING_NODE)
|
|
|
|
{
|
|
|
|
for (const auto& lang : root->m_mapChildren)
|
|
|
|
{
|
|
|
|
if (!lang.first.compare("names"))
|
|
|
|
continue;
|
|
|
|
if (lang.first.size() != 4)
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Warning, "STRG language string '%s' must be exactly 4 characters; skipping", lang.first.c_str());
|
2015-08-09 03:55:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (lang.second->m_type != YAML_SEQUENCE_NODE)
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Warning,
|
2015-08-09 03:55:11 +00:00
|
|
|
"STRG language string '%s' must contain a sequence; skipping", lang.first.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const auto& str : lang.second->m_seqChildren)
|
|
|
|
{
|
|
|
|
if (str->m_type != YAML_SCALAR_NODE)
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Warning, "STRG language '%s' must contain all scalars; skipping", lang.first.c_str());
|
2015-08-09 03:55:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Warning, "STRG must have a mapping root node; skipping");
|
2015-08-09 03:55:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:04:53 +00:00
|
|
|
const athena::io::YAMLNode* nameYAML = root->findMapChild("names");
|
2015-08-09 03:55:11 +00:00
|
|
|
names.clear();
|
|
|
|
if (nameYAML && nameYAML->m_type == YAML_MAPPING_NODE)
|
|
|
|
for (const auto& item : nameYAML->m_mapChildren)
|
|
|
|
if (item.second->m_type == YAML_SCALAR_NODE)
|
2016-03-04 23:04:53 +00:00
|
|
|
names[item.first] = athena::io::NodeToVal<atInt32>(item.second.get());
|
2015-08-09 03:55:11 +00:00
|
|
|
|
|
|
|
langs.clear();
|
|
|
|
langs.reserve(root->m_mapChildren.size());
|
|
|
|
for (const auto& item : root->m_mapChildren)
|
|
|
|
{
|
|
|
|
if (!item.first.compare("names") || item.first.size() != 4 ||
|
|
|
|
item.second->m_type != YAML_SEQUENCE_NODE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<std::string> strs;
|
|
|
|
for (const auto& node : item.second->m_seqChildren)
|
|
|
|
if (node->m_type == YAML_SCALAR_NODE)
|
|
|
|
strs.emplace_back(node->m_scalarString);
|
2015-08-23 06:42:29 +00:00
|
|
|
langs.emplace_back(std::make_pair(DNAFourCC(item.first.c_str()), std::move(strs)));
|
2015-08-09 03:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
langMap.clear();
|
|
|
|
langMap.reserve(langs.size());
|
2015-08-23 06:42:29 +00:00
|
|
|
for (std::pair<DNAFourCC, std::vector<std::string>>& item : langs)
|
2015-08-09 03:55:11 +00:00
|
|
|
langMap.emplace(item.first, &item.second);
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void STRG::Enumerate<BigDNA::Write>(athena::io::IStreamWriter& writer)
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(0x87654321);
|
|
|
|
writer.writeUint32Big(3);
|
|
|
|
writer.writeUint32Big(langs.size());
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 strCount = STRG::count();
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(strCount);
|
2015-07-13 06:29:12 +00:00
|
|
|
|
|
|
|
atUint32 nameTableSz = names.size() * 8;
|
2015-07-14 00:38:48 +00:00
|
|
|
for (const auto& name : names)
|
2015-07-13 06:29:12 +00:00
|
|
|
nameTableSz += name.first.size() + 1;
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(names.size());
|
|
|
|
writer.writeUint32Big(nameTableSz);
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 offset = names.size() * 8;
|
2015-07-14 00:38:48 +00:00
|
|
|
for (const auto& name : names)
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(offset);
|
|
|
|
writer.writeInt32Big(name.second);
|
2015-07-13 06:29:12 +00:00
|
|
|
offset += name.first.size() + 1;
|
|
|
|
}
|
2015-07-14 00:38:48 +00:00
|
|
|
for (const auto& name : names)
|
2015-07-13 06:29:12 +00:00
|
|
|
writer.writeString(name.first);
|
|
|
|
|
2015-07-14 00:38:48 +00:00
|
|
|
for (const auto& lang : langs)
|
2015-07-13 06:29:12 +00:00
|
|
|
lang.first.write(writer);
|
|
|
|
|
2015-07-14 00:38:48 +00:00
|
|
|
offset = 0;
|
|
|
|
for (const auto& lang : langs)
|
|
|
|
{
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 langSz = 0;
|
|
|
|
for (const std::string& str : lang.second)
|
2015-07-14 00:38:48 +00:00
|
|
|
langSz += str.size() + 5;
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(langSz);
|
2015-07-13 06:29:12 +00:00
|
|
|
|
|
|
|
for (const std::string& str : lang.second)
|
|
|
|
{
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(offset);
|
2015-07-14 00:38:48 +00:00
|
|
|
offset += str.size() + 5;
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 00:38:48 +00:00
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
2015-07-14 00:38:48 +00:00
|
|
|
for (const auto& lang : langs)
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
2015-07-14 00:38:48 +00:00
|
|
|
if (s >= lang.second.size())
|
|
|
|
{
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(1);
|
2015-07-14 00:38:48 +00:00
|
|
|
writer.writeUByte(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::string& str = lang.second[s];
|
2015-08-14 03:00:51 +00:00
|
|
|
writer.writeUint32Big(str.size() + 1);
|
2015-07-14 00:38:48 +00:00
|
|
|
writer.writeString(str);
|
|
|
|
}
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void STRG::Enumerate<BigDNA::BinarySize>(size_t& __isz)
|
2015-10-18 04:08:45 +00:00
|
|
|
{
|
|
|
|
__isz += 24;
|
|
|
|
__isz += names.size() * 8;
|
|
|
|
for (const auto& name : names)
|
|
|
|
__isz += name.first.size() + 1;
|
|
|
|
|
|
|
|
__isz += langs.size() * 4;
|
|
|
|
|
|
|
|
for (const auto& lang : langs)
|
|
|
|
__isz += 4 + lang.second.size() * 4;
|
|
|
|
|
|
|
|
size_t strCount = STRG::count();
|
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
|
|
|
{
|
|
|
|
for (const auto& lang : langs)
|
|
|
|
{
|
|
|
|
if (s >= lang.second.size())
|
|
|
|
{
|
|
|
|
__isz += 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::string& str = lang.second[s];
|
|
|
|
__isz += str.size() + 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void STRG::Enumerate<BigDNA::WriteYaml>(athena::io::YAMLDocWriter& writer)
|
2015-08-09 03:55:11 +00:00
|
|
|
{
|
|
|
|
for (const auto& item : langs)
|
|
|
|
{
|
2017-02-12 23:56:03 +00:00
|
|
|
if (auto v = writer.enterSubVector(item.first.toString().c_str()))
|
|
|
|
for (const std::string& str : item.second)
|
|
|
|
writer.writeString(nullptr, str);
|
2015-08-09 03:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (names.size())
|
|
|
|
{
|
2017-02-12 23:56:03 +00:00
|
|
|
if (auto rec = writer.enterSubRecord("names"))
|
|
|
|
for (const auto& item : names)
|
|
|
|
if (auto rec = writer.enterSubRecord(item.first.c_str()))
|
|
|
|
writer.writeInt32(nullptr, item.second);
|
2015-08-09 03:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 00:40:21 +00:00
|
|
|
const char* STRG::DNAType()
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
return "urde::DNAMP3::STRG";
|
2015-10-01 00:40:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|