metaforce/DataSpec/DNAMP1/STRG.cpp

572 lines
18 KiB
C++
Raw Normal View History

2015-07-10 05:28:08 +00:00
#include "STRG.hpp"
2015-07-16 01:57:34 +00:00
#include "DNAMP1.hpp"
2015-07-10 05:28:08 +00:00
2017-12-29 08:08:12 +00:00
namespace DataSpec::DNAMP1
2015-07-10 05:28:08 +00:00
{
2017-01-31 11:21:45 +00:00
const std::vector<FourCC> skLanguages = {FOURCC('ENGL'), FOURCC('FREN'), FOURCC('GERM'), FOURCC('SPAN'),
FOURCC('ITAL'), FOURCC('DUTC'), FOURCC('JAPN')};
2016-08-17 05:40:25 +00:00
static float u16stof(char16_t* str)
{
char cstr[16];
int i;
2017-01-31 11:21:45 +00:00
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;
2017-01-31 11:21:45 +00:00
for (i = 0; i < 8 && str[i]; ++i)
parseStr[i] = str[i];
parseStr[i] = '\0';
2017-01-29 07:27:48 +00:00
return strtoul(parseStr, nullptr, 16);
}
2017-11-13 06:19:18 +00:00
static std::u16string_view::const_iterator SkipCommas(std::u16string& ret, std::u16string_view str,
std::u16string_view::const_iterator it, size_t count)
{
2017-01-31 11:21:45 +00:00
for (size_t i = 0; i < count; ++i)
{
auto cpos = str.find(u',', it - str.begin());
if (cpos == std::u16string::npos)
return str.end();
auto end = str.begin() + cpos + 1;
ret.insert(ret.end(), it, end);
it = end;
}
return it;
}
2017-11-13 06:19:18 +00:00
static std::u16string_view::const_iterator UncookTextureList(std::u16string& ret, std::u16string_view str,
std::u16string_view::const_iterator it)
{
while (true)
{
UniqueID32 id = ParseTag(&*it);
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(id);
ret.append(hecl::UTF8ToChar16(path ? path.getRelativePathUTF8() : id.toString()));
it += 8;
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;
}
2017-11-13 06:19:18 +00:00
static std::u16string_view::const_iterator CookTextureList(std::u16string& ret, std::u16string_view str,
std::u16string_view::const_iterator it)
{
while (true)
{
2017-01-29 03:58:16 +00:00
auto end = str.find_first_of(u",;", it - str.begin());
if (end == std::u16string::npos)
2017-01-31 11:21:45 +00:00
Log.report(logvisor::Fatal, "Missing comma/semicolon token while pasing font tag");
2017-01-29 03:58:16 +00:00
auto endIt = str.begin() + end;
hecl::ProjectPath path =
2017-01-31 11:21:45 +00:00
UniqueIDBridge::MakePathFromString<UniqueID32>(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;
}
2017-11-13 06:19:18 +00:00
static std::u16string_view::const_iterator GatherTextureList(std::vector<hecl::ProjectPath>& pathsOut,
std::u16string_view str, std::u16string_view::const_iterator it)
2017-01-31 11:21:45 +00:00
{
while (true)
{
auto end = str.find_first_of(u",;", it - str.begin());
if (end == std::u16string::npos)
Log.report(logvisor::Fatal, "Missing comma/semicolon token while pasing font tag");
auto endIt = str.begin() + end;
hecl::ProjectPath path =
UniqueIDBridge::MakePathFromString<UniqueID32>(hecl::Char16ToUTF8(std::u16string(it, endIt)));
if (path)
pathsOut.push_back(path);
it = endIt;
if (*it == u';')
{
return it + 1;
}
else if (*it == 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;
}
2017-11-13 06:19:18 +00:00
static std::u16string UncookString(std::u16string_view str)
{
std::u16string ret;
ret.reserve(str.size());
2017-01-31 11:21:45 +00:00
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;
}
2017-11-13 06:19:18 +00:00
static std::u16string CookString(std::u16string_view str)
{
std::u16string ret;
ret.reserve(str.size());
2017-01-31 11:21:45 +00:00
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;
2017-01-27 02:22:52 +00:00
auto scpos = str.find(u';', it - str.begin());
if (scpos == std::u16string::npos)
Log.report(logvisor::Fatal, "Missing semicolon token while pasing font tag");
2017-01-31 11:21:45 +00:00
hecl::ProjectPath path = UniqueIDBridge::MakePathFromString<UniqueID32>(
hecl::Char16ToUTF8(std::u16string(it, str.begin() + scpos)));
ret.append(hecl::UTF8ToChar16(UniqueID32(path).toString()));
ret.push_back(u';');
2017-01-27 02:22:52 +00:00
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;
}
2017-01-31 11:21:45 +00:00
void STRG::gatherDependencies(std::vector<hecl::ProjectPath>& pathsOut) const
{
std::u16string skip;
for (const auto& lang : langs)
{
for (const std::u16string& str : lang.second)
{
2017-11-13 06:19:18 +00:00
std::u16string_view strView(str);
for (auto it = strView.begin(); it != strView.end();)
2017-01-31 11:21:45 +00:00
{
if (*it == u'&')
{
++it;
2017-11-13 06:19:18 +00:00
if (!str.compare(it - strView.begin(), 5, u"image"))
2017-01-31 11:21:45 +00:00
{
it += 6;
2017-11-13 06:19:18 +00:00
if (!str.compare(it - strView.begin(), 1, u"A"))
2017-01-31 11:21:45 +00:00
{
it = SkipCommas(skip, str, it, 2);
it = GatherTextureList(pathsOut, str, it);
continue;
}
2017-11-13 06:19:18 +00:00
else if (!str.compare(it - strView.begin(), 2, u"SA"))
2017-01-31 11:21:45 +00:00
{
it = SkipCommas(skip, str, it, 4);
it = GatherTextureList(pathsOut, str, it);
continue;
}
2017-11-13 06:19:18 +00:00
else if (!str.compare(it - strView.begin(), 2, u"SI"))
2017-01-31 11:21:45 +00:00
{
it = SkipCommas(skip, str, it, 3);
it = GatherTextureList(pathsOut, str, it);
continue;
}
}
2017-11-13 06:19:18 +00:00
else if (!str.compare(it - strView.begin(), 4, u"font"))
2017-01-31 11:21:45 +00:00
{
it += 5;
2017-11-13 06:19:18 +00:00
auto scpos = str.find(u';', it - strView.begin());
2017-01-31 11:21:45 +00:00
if (scpos == std::u16string::npos)
Log.report(logvisor::Fatal, "Missing semicolon token while pasing font tag");
hecl::ProjectPath path = UniqueIDBridge::MakePathFromString<UniqueID32>(
2017-11-13 06:19:18 +00:00
hecl::Char16ToUTF8(std::u16string(it, strView.begin() + scpos)));
2017-01-31 11:21:45 +00:00
if (path)
pathsOut.push_back(path);
2017-11-13 06:19:18 +00:00
it = strView.begin() + scpos + 1;
2017-01-31 11:21:45 +00:00
}
else
{
2017-11-13 06:19:18 +00:00
auto scpos = str.find(u';', it - strView.begin());
2017-01-31 11:21:45 +00:00
if (scpos == std::u16string::npos)
2017-11-13 06:19:18 +00:00
it = strView.end();
2017-01-31 11:21:45 +00:00
else
2017-11-13 06:19:18 +00:00
it = strView.begin() + scpos + 1;
2017-01-31 11:21:45 +00:00
}
}
else
++it;
}
}
}
}
2016-03-04 23:04:53 +00:00
void STRG::_read(athena::io::IStreamReader& reader)
2015-07-10 05:28:08 +00:00
{
atUint32 langCount = reader.readUint32Big();
atUint32 strCount = reader.readUint32Big();
2015-07-10 05:28:08 +00:00
2016-08-17 05:40:25 +00:00
std::vector<std::pair<FourCC, atUint32>> readLangs;
readLangs.reserve(langCount);
2017-01-31 11:21:45 +00:00
for (atUint32 l = 0; l < langCount; ++l)
{
2015-08-23 06:42:29 +00:00
DNAFourCC lang;
lang.read(reader);
2016-08-17 05:40:25 +00:00
atUint32 off = reader.readUint32Big();
readLangs.emplace_back(lang, off);
}
2015-07-10 05:28:08 +00:00
2016-08-17 05:40:25 +00:00
atUint32 tablesStart = reader.position();
2015-07-10 05:28:08 +00:00
langs.clear();
2016-08-17 05:40:25 +00:00
langs.reserve(skLanguages.size());
for (const std::pair<FourCC, atUint32>& lang : readLangs)
2015-07-10 05:28:08 +00:00
{
2017-01-24 07:41:33 +00:00
std::vector<std::u16string> strs;
2016-08-17 05:40:25 +00:00
reader.seek(tablesStart + lang.second, athena::SeekOrigin::Begin);
reader.readUint32Big(); // table size
atUint32 langStart = reader.position();
2017-01-31 11:21:45 +00:00
for (atUint32 s = 0; s < strCount; ++s)
2016-08-17 05:40:25 +00:00
{
atUint32 strOffset = reader.readUint32Big();
atUint32 tmpOffset = reader.position();
reader.seek(langStart + strOffset, athena::SeekOrigin::Begin);
strs.emplace_back(UncookString(reader.readU16StringBig()));
2016-08-17 05:40:25 +00:00
reader.seek(tmpOffset, athena::SeekOrigin::Begin);
}
langs.emplace_back(lang.first, strs);
2015-07-10 05:28:08 +00:00
}
langMap.clear();
langMap.reserve(langCount);
2017-01-24 07:41:33 +00:00
for (std::pair<FourCC, std::vector<std::u16string>>& item : langs)
langMap.emplace(item.first, &item.second);
}
2015-07-10 05:28:08 +00:00
2016-03-04 23:04:53 +00:00
void STRG::read(athena::io::IStreamReader& reader)
{
atUint32 magic = reader.readUint32Big();
if (magic != 0x87654321)
2016-03-04 23:04:53 +00:00
Log.report(logvisor::Error, "invalid STRG magic");
atUint32 version = reader.readUint32Big();
if (version != 0)
2016-03-04 23:04:53 +00:00
Log.report(logvisor::Error, "invalid STRG version");
_read(reader);
2015-07-10 05:28:08 +00:00
}
2016-03-04 23:04:53 +00:00
void STRG::write(athena::io::IStreamWriter& writer) const
2015-07-10 05:28:08 +00:00
{
writer.writeUint32Big(0x87654321);
writer.writeUint32Big(0);
writer.writeUint32Big(langs.size());
atUint32 strCount = STRG::count();
writer.writeUint32Big(strCount);
2015-07-10 05:28:08 +00:00
2017-01-27 02:22:52 +00:00
std::vector<std::u16string> strings;
strings.reserve(strCount * langs.size());
atUint32 offset = 0;
2017-01-24 07:41:33 +00:00
for (const std::pair<FourCC, std::vector<std::u16string>>& lang : langs)
2015-07-10 05:28:08 +00:00
{
2015-08-23 06:42:29 +00:00
DNAFourCC(lang.first).write(writer);
writer.writeUint32Big(offset);
2015-07-10 05:28:08 +00:00
offset += strCount * 4 + 4;
atUint32 langStrCount = lang.second.size();
2017-01-31 11:21:45 +00:00
for (atUint32 s = 0; s < strCount; ++s)
2015-07-10 05:28:08 +00:00
{
2017-01-27 02:22:52 +00:00
std::u16string str = CookString(lang.second[s]);
atUint32 chCount = str.size();
if (s < langStrCount)
2016-08-17 05:40:25 +00:00
offset += (chCount + 1) * 2;
2015-07-10 05:28:08 +00:00
else
offset += 1;
2017-01-27 02:22:52 +00:00
strings.push_back(std::move(str));
2015-07-10 05:28:08 +00:00
}
}
2017-01-27 02:22:52 +00:00
auto langIt = strings.cbegin();
2017-01-24 07:41:33 +00:00
for (const std::pair<FourCC, std::vector<std::u16string>>& lang : langs)
2015-07-10 05:28:08 +00:00
{
atUint32 langStrCount = lang.second.size();
atUint32 tableSz = strCount * 4;
2017-01-27 02:22:52 +00:00
auto strIt = langIt;
2017-01-31 11:21:45 +00:00
for (atUint32 s = 0; s < strCount; ++s)
{
if (s < langStrCount)
2017-01-27 02:22:52 +00:00
tableSz += ((strIt++)->size() + 1) * 2;
else
tableSz += 1;
}
writer.writeUint32Big(tableSz);
2015-07-10 05:28:08 +00:00
offset = strCount * 4;
2017-01-27 02:22:52 +00:00
strIt = langIt;
2017-01-31 11:21:45 +00:00
for (atUint32 s = 0; s < strCount; ++s)
2015-07-10 05:28:08 +00:00
{
writer.writeUint32Big(offset);
if (s < langStrCount)
2017-01-27 02:22:52 +00:00
offset += ((strIt++)->size() + 1) * 2;
2015-07-10 05:28:08 +00:00
else
offset += 1;
}
2017-01-27 02:22:52 +00:00
strIt = langIt;
2017-01-31 11:21:45 +00:00
for (atUint32 s = 0; s < strCount; ++s)
2015-07-10 05:28:08 +00:00
{
if (s < langStrCount)
2017-01-27 02:22:52 +00:00
writer.writeU16StringBig(*strIt++);
2015-07-10 05:28:08 +00:00
else
writer.writeUByte(0);
}
2017-01-27 02:22:52 +00:00
langIt = strIt;
2015-07-10 05:28:08 +00:00
}
}
size_t STRG::binarySize(size_t __isz) const
{
__isz += 16;
__isz += langs.size() * 12;
size_t strCount = STRG::count();
__isz += langs.size() * strCount * 4;
2017-01-24 07:41:33 +00:00
for (const std::pair<FourCC, std::vector<std::u16string>>& lang : langs)
{
atUint32 langStrCount = lang.second.size();
2017-01-31 11:21:45 +00:00
for (atUint32 s = 0; s < strCount; ++s)
{
if (s < langStrCount)
2017-01-27 02:22:52 +00:00
__isz += (CookString(lang.second[s]).size() + 1) * 2;
else
__isz += 1;
}
}
return __isz;
}
2016-03-04 23:04:53 +00:00
void STRG::read(athena::io::YAMLDocReader& reader)
2015-08-01 00:38:35 +00:00
{
2016-03-04 23:04:53 +00:00
const athena::io::YAMLNode* root = reader.getRootNode();
2015-08-01 00:38:35 +00:00
/* Validate Pass */
if (root->m_type == YAML_MAPPING_NODE)
{
for (const auto& lang : root->m_mapChildren)
{
2016-08-17 05:40:25 +00:00
if (lang.first == "DNAType")
continue;
2015-08-01 00:38:35 +00:00
if (lang.first.size() != 4)
{
2017-01-31 11:21:45 +00:00
Log.report(logvisor::Warning, "STRG language string '%s' must be exactly 4 characters; skipping",
lang.first.c_str());
2015-08-01 00:38:35 +00:00
return;
}
if (lang.second->m_type != YAML_SEQUENCE_NODE)
{
2017-01-31 11:21:45 +00:00
Log.report(logvisor::Warning, "STRG language string '%s' must contain a sequence; skipping",
lang.first.c_str());
2015-08-01 00:38:35 +00:00
return;
}
for (const auto& str : lang.second->m_seqChildren)
{
if (str->m_type != YAML_SCALAR_NODE)
{
2017-01-31 11:21:45 +00:00
Log.report(logvisor::Warning, "STRG language '%s' must contain all scalars; skipping",
lang.first.c_str());
2015-08-01 00:38:35 +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-01 00:38:35 +00:00
return;
}
/* Read Pass */
langs.clear();
for (const auto& lang : root->m_mapChildren)
{
2016-08-17 05:40:25 +00:00
if (lang.first == "DNAType")
continue;
2017-01-24 07:41:33 +00:00
std::vector<std::u16string> strs;
2015-08-01 00:38:35 +00:00
for (const auto& str : lang.second->m_seqChildren)
2017-01-24 07:41:33 +00:00
strs.emplace_back(hecl::UTF8ToChar16(str->m_scalarString));
2015-08-01 00:38:35 +00:00
langs.emplace_back(FourCC(lang.first.c_str()), strs);
}
langMap.clear();
langMap.reserve(langs.size());
2015-08-09 03:55:11 +00:00
for (auto& item : langs)
2015-08-01 00:38:35 +00:00
langMap.emplace(item.first, &item.second);
}
2016-03-04 23:04:53 +00:00
void STRG::write(athena::io::YAMLDocWriter& writer) const
2015-08-01 00:38:35 +00:00
{
2015-08-09 03:55:11 +00:00
for (const auto& lang : langs)
2015-08-01 00:38:35 +00:00
{
2017-02-12 23:56:03 +00:00
if (auto v = writer.enterSubVector(lang.first.toString().c_str()))
for (const std::u16string& str : lang.second)
writer.writeU16String(nullptr, str);
2015-08-01 00:38:35 +00:00
}
}
2017-01-31 11:21:45 +00:00
const char* STRG::DNAType() { return "urde::DNAMP1::STRG"; }
2015-07-10 05:28:08 +00:00
}