Path-substitution integrated into MP1 STRG cook/uncook

This commit is contained in:
Jack Andersen 2017-01-25 22:09:22 -10:00
parent da91c921cb
commit d6482874d6
7 changed files with 280 additions and 23 deletions

View File

@ -41,6 +41,7 @@ std::unique_ptr<ISTRG> LoadSTRG(athena::io::IStreamReader& reader)
newStrg->_read(reader);
return std::unique_ptr<ISTRG>(newStrg);
}
default: break;
}
return std::unique_ptr<ISTRG>();
}

View File

@ -17,6 +17,262 @@ const std::vector<FourCC> skLanguages =
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 strtol(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<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;
}
static std::u16string::const_iterator UncookTextureList(std::u16string& ret,
const std::u16string& str,
std::u16string::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;
}
static std::u16string::const_iterator CookTextureList(std::u16string& ret,
const std::u16string& str,
std::u16string::const_iterator it)
{
while (true)
{
auto end = str.find(u',', it - str.begin());
if (end == std::u16string::npos)
Log.report(logvisor::Fatal, "Missing comma token while pasing font tag");
auto endIt = str.begin() + end + 1;
hecl::ProjectPath path =
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;
}
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 end = str.find(u',', it - str.begin());
if (end == std::u16string::npos)
Log.report(logvisor::Fatal, "Missing comma token while pasing font tag");
hecl::ProjectPath path =
UniqueIDBridge::MakePathFromString<UniqueID32>(
hecl::Char16ToUTF8(std::u16string(it, str.begin() + end + 1)));
ret.append(hecl::UTF8ToChar16(UniqueID32(path).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;
}
void STRG::_read(athena::io::IStreamReader& reader)
{
atUint32 langCount = reader.readUint32Big();
@ -47,7 +303,7 @@ void STRG::_read(athena::io::IStreamReader& reader)
atUint32 strOffset = reader.readUint32Big();
atUint32 tmpOffset = reader.position();
reader.seek(langStart + strOffset, athena::SeekOrigin::Begin);
strs.emplace_back(reader.readU16StringBig());
strs.emplace_back(UncookString(reader.readU16StringBig()));
reader.seek(tmpOffset, athena::SeekOrigin::Begin);
}
langs.emplace_back(lang.first, strs);
@ -123,7 +379,7 @@ void STRG::write(athena::io::IStreamWriter& writer) const
for (atUint32 s=0 ; s<strCount ; ++s)
{
if (s < langStrCount)
writer.writeU16StringBig(lang.second[s]);
writer.writeU16StringBig(CookString(lang.second[s]));
else
writer.writeUByte(0);
}

View File

@ -19,9 +19,9 @@ struct STRG : ISTRG
std::vector<std::pair<FourCC, std::vector<std::u16string>>> langs;
std::unordered_map<FourCC, std::vector<std::u16string>*> langMap;
inline int32_t lookupIdx(const std::string& name) const {return -1;}
int32_t lookupIdx(const std::string& name) const {return -1;}
inline size_t count() const
size_t count() const
{
size_t retval = 0;
for (const auto& item : langs)
@ -32,21 +32,21 @@ struct STRG : ISTRG
}
return retval;
}
inline std::string getUTF8(const FourCC& lang, size_t idx) const
std::string getUTF8(const FourCC& lang, size_t idx) const
{
auto search = langMap.find(lang);
if (search != langMap.end())
return hecl::Char16ToUTF8(search->second->at(idx));
return std::string();
}
inline std::u16string getUTF16(const FourCC& lang, size_t idx) const
std::u16string getUTF16(const FourCC& lang, size_t idx) const
{
auto search = langMap.find(lang);
if (search != langMap.end())
return search->second->at(idx);
return std::u16string();
}
inline hecl::SystemString getSystemString(const FourCC& lang, size_t idx) const
hecl::SystemString getSystemString(const FourCC& lang, size_t idx) const
{
auto search = langMap.find(lang);
if (search != langMap.end())

View File

@ -19,7 +19,7 @@ struct STRG : ISTRG
std::unordered_map<FourCC, std::vector<std::u16string>*> langMap;
std::map<std::string, int32_t> names;
inline int32_t lookupIdx(const std::string& name) const
int32_t lookupIdx(const std::string& name) const
{
auto search = names.find(name);
if (search == names.end())
@ -27,7 +27,7 @@ struct STRG : ISTRG
return search->second;
}
inline size_t count() const
size_t count() const
{
size_t retval = 0;
for (const auto& item : langs)
@ -38,21 +38,21 @@ struct STRG : ISTRG
}
return retval;
}
inline std::string getUTF8(const FourCC& lang, size_t idx) const
std::string getUTF8(const FourCC& lang, size_t idx) const
{
auto search = langMap.find(lang);
if (search != langMap.end())
return hecl::Char16ToUTF8(search->second->at(idx));
return std::string();
}
inline std::u16string getUTF16(const FourCC& lang, size_t idx) const
std::u16string getUTF16(const FourCC& lang, size_t idx) const
{
auto search = langMap.find(lang);
if (search != langMap.end())
return search->second->at(idx);
return std::u16string();
}
inline hecl::SystemString getSystemString(const FourCC& lang, size_t idx) const
hecl::SystemString getSystemString(const FourCC& lang, size_t idx) const
{
auto search = langMap.find(lang);
if (search != langMap.end())

View File

@ -19,7 +19,7 @@ struct STRG : ISTRG
std::unordered_map<DNAFourCC, std::vector<std::string>*> langMap;
std::map<std::string, int32_t> names;
inline int32_t lookupIdx(const std::string& name) const
int32_t lookupIdx(const std::string& name) const
{
auto search = names.find(name);
if (search == names.end())
@ -27,7 +27,7 @@ struct STRG : ISTRG
return search->second;
}
inline size_t count() const
size_t count() const
{
size_t retval = 0;
for (const auto& item : langs)
@ -38,21 +38,21 @@ struct STRG : ISTRG
}
return retval;
}
inline std::string getUTF8(const FourCC& lang, size_t idx) const
std::string getUTF8(const FourCC& lang, size_t idx) const
{
auto search = langMap.find(lang);
if (search != langMap.end())
return search->second->at(idx);
return std::string();
}
inline std::u16string getUTF16(const FourCC& lang, size_t idx) const
std::u16string getUTF16(const FourCC& lang, size_t idx) const
{
auto search = langMap.find(lang);
if (search != langMap.end())
return hecl::UTF8ToChar16(search->second->at(idx));
return std::u16string();
}
inline hecl::SystemString getSystemString(const FourCC& lang, size_t idx) const
hecl::SystemString getSystemString(const FourCC& lang, size_t idx) const
{
auto search = langMap.find(lang);
if (search != langMap.end())

View File

@ -7,12 +7,12 @@ namespace urde
static float u16stof(char16_t* str)
{
wchar_t wstr[16];
char cstr[16];
int i;
for (i=0 ; i<15 && str[i] != u'\0' ; ++i)
wstr[i] = str[i];
wstr[i+1] = L'\0';
return std::wcstof(wstr, nullptr);
cstr[i] = str[i];
cstr[i] = '\0';
return strtof(cstr, nullptr);
}
CTextColor CTextParser::ParseColor(const char16_t* str, int len)
@ -325,7 +325,7 @@ void CTextParser::ParseText(CTextExecuteBuffer& out, const char16_t* str, int le
while (str[e] && (len == -1 || e < len) && str[e] != u';')
++e;
ParseTag(out, str + e, e - b);
ParseTag(out, str + b, e - b);
b = e + 1;
}
else

2
hecl

@ -1 +1 @@
Subproject commit 5d797e53ecffbbf2e612bc0e21eed2c495985ca0
Subproject commit 1a079c247e07d1b3c4ebc384db27274f56a5e1a2