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
|
|
|
|
|
|
|
namespace Retro
|
|
|
|
{
|
|
|
|
namespace DNAMP1
|
|
|
|
{
|
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
void STRG::_read(Athena::io::IStreamReader& reader)
|
2015-07-10 05:28:08 +00:00
|
|
|
{
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 langCount = reader.readUint32();
|
|
|
|
atUint32 strCount = reader.readUint32();
|
2015-07-10 05:28:08 +00:00
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
std::vector<FourCC> readLangs;
|
|
|
|
readLangs.reserve(langCount);
|
|
|
|
for (atUint32 l=0 ; l<langCount ; ++l)
|
|
|
|
{
|
|
|
|
FourCC lang;
|
|
|
|
lang.read(reader);
|
|
|
|
readLangs.emplace_back(lang);
|
|
|
|
reader.seek(4);
|
|
|
|
}
|
2015-07-10 05:28:08 +00:00
|
|
|
|
|
|
|
langs.clear();
|
|
|
|
langs.reserve(langCount);
|
2015-07-13 06:29:12 +00:00
|
|
|
for (FourCC& lang : readLangs)
|
2015-07-10 05:28:08 +00:00
|
|
|
{
|
2015-07-13 06:29:12 +00:00
|
|
|
std::vector<std::wstring> strs;
|
|
|
|
reader.seek(strCount * 4 + 4);
|
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
|
|
|
strs.emplace_back(reader.readWString());
|
2015-07-18 04:33:38 +00:00
|
|
|
langs.emplace_back(lang, strs);
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|
2015-07-18 04:33:38 +00:00
|
|
|
|
|
|
|
langMap.clear();
|
|
|
|
langMap.reserve(langCount);
|
|
|
|
for (std::pair<FourCC, std::vector<std::wstring>>& item : langs)
|
|
|
|
langMap.emplace(item.first, &item.second);
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|
2015-07-10 05:28:08 +00:00
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
void STRG::read(Athena::io::IStreamReader& reader)
|
|
|
|
{
|
|
|
|
reader.setEndian(Athena::BigEndian);
|
|
|
|
atUint32 magic = reader.readUint32();
|
|
|
|
if (magic != 0x87654321)
|
2015-07-16 01:57:34 +00:00
|
|
|
Log.report(LogVisor::Error, "invalid STRG magic");
|
2015-07-13 06:29:12 +00:00
|
|
|
|
|
|
|
atUint32 version = reader.readUint32();
|
|
|
|
if (version != 0)
|
2015-07-16 01:57:34 +00:00
|
|
|
Log.report(LogVisor::Error, "invalid STRG version");
|
2015-07-13 06:29:12 +00:00
|
|
|
|
|
|
|
_read(reader);
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void STRG::write(Athena::io::IStreamWriter& writer) const
|
|
|
|
{
|
2015-07-12 04:26:26 +00:00
|
|
|
writer.setEndian(Athena::BigEndian);
|
2015-07-10 05:28:08 +00:00
|
|
|
writer.writeUint32(0x87654321);
|
2015-07-13 06:29:12 +00:00
|
|
|
writer.writeUint32(0);
|
2015-07-10 05:28:08 +00:00
|
|
|
writer.writeUint32(langs.size());
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 strCount = STRG::count();
|
2015-07-10 05:28:08 +00:00
|
|
|
writer.writeUint32(strCount);
|
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 offset = 0;
|
|
|
|
for (const std::pair<FourCC, std::vector<std::wstring>>& lang : langs)
|
2015-07-10 05:28:08 +00:00
|
|
|
{
|
2015-07-13 06:29:12 +00:00
|
|
|
lang.first.write(writer);
|
2015-07-10 05:28:08 +00:00
|
|
|
writer.writeUint32(offset);
|
|
|
|
offset += strCount * 4 + 4;
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 langStrCount = lang.second.size();
|
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
2015-07-10 05:28:08 +00:00
|
|
|
{
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 chCount = lang.second[s].size();
|
|
|
|
if (s < langStrCount)
|
|
|
|
offset += chCount * 2 + 1;
|
2015-07-10 05:28:08 +00:00
|
|
|
else
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
for (const std::pair<FourCC, std::vector<std::wstring>>& lang : langs)
|
2015-07-10 05:28:08 +00:00
|
|
|
{
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 langStrCount = lang.second.size();
|
|
|
|
atUint32 tableSz = strCount * 4;
|
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
|
|
|
{
|
|
|
|
if (s < langStrCount)
|
|
|
|
tableSz += lang.second[s].size() * 2 + 1;
|
|
|
|
else
|
|
|
|
tableSz += 1;
|
|
|
|
}
|
|
|
|
writer.writeUint32(tableSz);
|
|
|
|
|
2015-07-10 05:28:08 +00:00
|
|
|
offset = strCount * 4;
|
2015-07-13 06:29:12 +00:00
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
2015-07-10 05:28:08 +00:00
|
|
|
{
|
|
|
|
writer.writeUint32(offset);
|
2015-07-13 06:29:12 +00:00
|
|
|
if (s < langStrCount)
|
|
|
|
offset += lang.second[s].size() * 2 + 1;
|
2015-07-10 05:28:08 +00:00
|
|
|
else
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
2015-07-10 05:28:08 +00:00
|
|
|
{
|
2015-07-13 06:29:12 +00:00
|
|
|
if (s < langStrCount)
|
|
|
|
writer.writeWString(lang.second[s]);
|
2015-07-10 05:28:08 +00:00
|
|
|
else
|
|
|
|
writer.writeUByte(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 01:57:34 +00:00
|
|
|
bool STRG::readAngelScript(const AngelScript::asIScriptModule& in)
|
|
|
|
{
|
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> wconv;
|
|
|
|
|
|
|
|
/* Validate pass */
|
|
|
|
for (AngelScript::asUINT i=0 ; i<in.GetGlobalVarCount() ; ++i)
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
int typeId;
|
|
|
|
if (in.GetGlobalVar(i, &name, 0, &typeId) < 0)
|
|
|
|
continue;
|
|
|
|
if (typeId == ASTYPE_STRGLanguage.getTypeID())
|
|
|
|
{
|
|
|
|
if (strlen(name) != 4)
|
|
|
|
{
|
|
|
|
Log.report(LogVisor::Error, "STRG language string '%s' from %s must be exactly 4 characters", name, in.GetName());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read pass */
|
2015-07-18 04:33:38 +00:00
|
|
|
langs.clear();
|
2015-07-16 01:57:34 +00:00
|
|
|
for (AngelScript::asUINT i=0 ; i<in.GetGlobalVarCount() ; ++i)
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
int typeId;
|
|
|
|
if (in.GetGlobalVar(i, &name, 0, &typeId) < 0)
|
|
|
|
continue;
|
|
|
|
if (typeId == ASTYPE_STRGLanguage.getTypeID())
|
|
|
|
{
|
|
|
|
const std::vector<std::string*>& strsin = ASTYPE_STRGLanguage.vectorCast(in.GetAddressOfGlobalVar(i));
|
|
|
|
std::vector<std::wstring> strs;
|
|
|
|
for (const std::string* str : strsin)
|
|
|
|
strs.emplace_back(wconv.from_bytes(*str));
|
2015-07-18 04:33:38 +00:00
|
|
|
langs.emplace_back(FourCC(name), strs);
|
2015-07-16 01:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-18 04:33:38 +00:00
|
|
|
langMap.clear();
|
|
|
|
langMap.reserve(langs.size());
|
|
|
|
for (std::pair<FourCC, std::vector<std::wstring>>& item : langs)
|
|
|
|
langMap.emplace(item.first, &item.second);
|
|
|
|
|
2015-07-16 01:57:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void STRG::writeAngelScript(std::ofstream& out) const
|
|
|
|
{
|
2015-07-18 04:33:38 +00:00
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> wconv;
|
2015-07-16 01:57:34 +00:00
|
|
|
for (const std::pair<FourCC, std::vector<std::wstring>>& lang : langs)
|
|
|
|
{
|
|
|
|
out << "STRG::Language " << lang.first.toString() << "({";
|
|
|
|
bool comma = false;
|
2015-07-18 04:33:38 +00:00
|
|
|
unsigned idx = 0;
|
2015-07-16 01:57:34 +00:00
|
|
|
for (const std::wstring& str : lang.second)
|
|
|
|
{
|
2015-07-18 04:33:38 +00:00
|
|
|
if (comma)
|
|
|
|
out << ",";
|
|
|
|
out << "\n/* " << idx++ << " */ \"";
|
|
|
|
out << wconv.to_bytes(str);
|
2015-07-16 01:57:34 +00:00
|
|
|
out << "\"";
|
|
|
|
comma = true;
|
|
|
|
}
|
2015-07-18 04:33:38 +00:00
|
|
|
out << "\n});\n";
|
2015-07-16 01:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 05:28:08 +00:00
|
|
|
}
|
|
|
|
}
|