#include "STRG.hpp" #include "DNAMP1.hpp" namespace DataSpec { namespace DNAMP1 { const std::vector skLanguages = { FOURCC('ENGL'), FOURCC('FREN'), FOURCC('GERM'), FOURCC('SPAN'), FOURCC('ITAL'), FOURCC('DUTC'), FOURCC('JAPN') }; static float u16stof(char16_t* str) { char cstr[16]; int i; for (i=0 ; i<15 && str[i] != u'\0' ; ++i) cstr[i] = str[i]; cstr[i] = '\0'; return strtof(cstr, nullptr); } static uint32_t ParseTag(const char16_t* str) { char parseStr[9]; int i; for (i=0 ; i<8 && str[i] ; ++i) parseStr[i] = str[i]; parseStr[i] = '\0'; return strtoul(parseStr, nullptr, 16); } static std::u16string::const_iterator SkipCommas(std::u16string& ret, const std::u16string& str, std::u16string::const_iterator it, size_t count) { for (size_t i=0 ; i( hecl::Char16ToUTF8(std::u16string(it, endIt))); ret.append(hecl::UTF8ToChar16(UniqueID32(path).toString())); it = endIt; if (*it == u';') { ret.push_back(u';'); return it + 1; } else if (*it == u',') { ret.push_back(u','); ++it; } else { break; } } /* Failsafe */ auto scpos = str.find(u';', it - str.begin()); if (scpos == std::u16string::npos) return str.end(); return str.begin() + scpos + 1; } static std::u16string UncookString(const std::u16string& str) { std::u16string ret; ret.reserve(str.size()); for (auto it = str.begin() ; it != str.end() ;) { if (*it == u'&') { ret.push_back(u'&'); ++it; if (!str.compare(it - str.begin(), 5, u"image")) { ret.append(u"image="); it += 6; if (!str.compare(it - str.begin(), 1, u"A")) { it = SkipCommas(ret, str, it, 2); it = UncookTextureList(ret, str, it); continue; } else if (!str.compare(it - str.begin(), 2, u"SA")) { it = SkipCommas(ret, str, it, 4); it = UncookTextureList(ret, str, it); continue; } else if (!str.compare(it - str.begin(), 2, u"SI")) { it = SkipCommas(ret, str, it, 3); it = UncookTextureList(ret, str, it); continue; } } else if (!str.compare(it - str.begin(), 4, u"font")) { ret.append(u"font="); it += 5; UniqueID32 id = ParseTag(&*it); hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(id, true); ret.append(hecl::UTF8ToChar16(path ? path.getRelativePathUTF8() : id.toString())); ret.push_back(u';'); auto scpos = str.find(u';', it - str.begin()); if (scpos == std::u16string::npos) it = str.end(); else it = str.begin() + scpos + 1; } else { auto scpos = str.find(u';', it - str.begin()); if (scpos == std::u16string::npos) { it = str.end(); } else { auto end = str.begin() + scpos + 1; ret.insert(ret.end(), it, end); it = end; } } } else { ret.push_back(*it); ++it; } } return ret; } static std::u16string CookString(const std::u16string& str) { std::u16string ret; ret.reserve(str.size()); for (auto it = str.begin() ; it != str.end() ;) { if (*it == u'&') { ret.push_back(u'&'); ++it; if (!str.compare(it - str.begin(), 5, u"image")) { ret.append(u"image="); it += 6; if (!str.compare(it - str.begin(), 1, u"A")) { it = SkipCommas(ret, str, it, 2); it = CookTextureList(ret, str, it); continue; } else if (!str.compare(it - str.begin(), 2, u"SA")) { it = SkipCommas(ret, str, it, 4); it = CookTextureList(ret, str, it); continue; } else if (!str.compare(it - str.begin(), 2, u"SI")) { it = SkipCommas(ret, str, it, 3); it = CookTextureList(ret, str, it); continue; } } else if (!str.compare(it - str.begin(), 4, u"font")) { ret.append(u"font="); it += 5; auto scpos = str.find(u';', it - str.begin()); if (scpos == std::u16string::npos) Log.report(logvisor::Fatal, "Missing semicolon token while pasing font tag"); hecl::ProjectPath path = UniqueIDBridge::MakePathFromString( hecl::Char16ToUTF8(std::u16string(it, str.begin() + scpos))); ret.append(hecl::UTF8ToChar16(UniqueID32(path).toString())); ret.push_back(u';'); it = str.begin() + scpos + 1; } else { auto scpos = str.find(u';', it - str.begin()); if (scpos == std::u16string::npos) { it = str.end(); } else { auto end = str.begin() + scpos + 1; ret.insert(ret.end(), it, end); it = end; } } } else { ret.push_back(*it); ++it; } } return ret; } void STRG::_read(athena::io::IStreamReader& reader) { atUint32 langCount = reader.readUint32Big(); atUint32 strCount = reader.readUint32Big(); std::vector> readLangs; readLangs.reserve(langCount); for (atUint32 l=0 ; l& lang : readLangs) { std::vector strs; reader.seek(tablesStart + lang.second, athena::SeekOrigin::Begin); reader.readUint32Big(); // table size atUint32 langStart = reader.position(); for (atUint32 s=0 ; s>& item : langs) langMap.emplace(item.first, &item.second); } void STRG::read(athena::io::IStreamReader& reader) { atUint32 magic = reader.readUint32Big(); if (magic != 0x87654321) Log.report(logvisor::Error, "invalid STRG magic"); atUint32 version = reader.readUint32Big(); if (version != 0) Log.report(logvisor::Error, "invalid STRG version"); _read(reader); } void STRG::write(athena::io::IStreamWriter& writer) const { writer.writeUint32Big(0x87654321); writer.writeUint32Big(0); writer.writeUint32Big(langs.size()); atUint32 strCount = STRG::count(); writer.writeUint32Big(strCount); std::vector strings; strings.reserve(strCount * langs.size()); atUint32 offset = 0; for (const std::pair>& lang : langs) { DNAFourCC(lang.first).write(writer); writer.writeUint32Big(offset); offset += strCount * 4 + 4; atUint32 langStrCount = lang.second.size(); for (atUint32 s=0 ; s>& lang : langs) { atUint32 langStrCount = lang.second.size(); atUint32 tableSz = strCount * 4; auto strIt = langIt; for (atUint32 s=0 ; ssize() + 1) * 2; else tableSz += 1; } writer.writeUint32Big(tableSz); offset = strCount * 4; strIt = langIt; for (atUint32 s=0 ; ssize() + 1) * 2; else offset += 1; } strIt = langIt; for (atUint32 s=0 ; s>& lang : langs) { atUint32 langStrCount = lang.second.size(); for (atUint32 s=0 ; sm_type == YAML_MAPPING_NODE) { for (const auto& lang : root->m_mapChildren) { if (lang.first == "DNAType") continue; if (lang.first.size() != 4) { Log.report(logvisor::Warning, "STRG language string '%s' must be exactly 4 characters; skipping", lang.first.c_str()); return; } if (lang.second->m_type != YAML_SEQUENCE_NODE) { Log.report(logvisor::Warning, "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) { Log.report(logvisor::Warning, "STRG language '%s' must contain all scalars; skipping", lang.first.c_str()); return; } } } } else { Log.report(logvisor::Warning, "STRG must have a mapping root node; skipping"); return; } /* Read Pass */ langs.clear(); for (const auto& lang : root->m_mapChildren) { if (lang.first == "DNAType") continue; std::vector 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); } langMap.clear(); langMap.reserve(langs.size()); for (auto& item : langs) langMap.emplace(item.first, &item.second); } void STRG::write(athena::io::YAMLDocWriter& writer) const { for (const auto& lang : langs) { writer.enterSubVector(lang.first.toString().c_str()); for (const std::u16string& str : lang.second) writer.writeU16String(nullptr, str); writer.leaveSubVector(); } } const char* STRG::DNAType() { return "urde::DNAMP1::STRG"; } } }