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
|
|
|
|
|
|
|
namespace Retro
|
|
|
|
{
|
|
|
|
namespace DNAMP3
|
|
|
|
{
|
|
|
|
|
|
|
|
void STRG::_read(Athena::io::IStreamReader& reader)
|
|
|
|
{
|
|
|
|
atUint32 langCount = reader.readUint32();
|
|
|
|
atUint32 strCount = reader.readUint32();
|
|
|
|
|
|
|
|
atUint32 nameCount = reader.readUint32();
|
|
|
|
atUint32 nameTableSz = reader.readUint32();
|
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)
|
|
|
|
{
|
|
|
|
const char* name = (char*)(nameTableBuf.get() + HECL::SBig(nameIndex[n].nameOff));
|
|
|
|
names[name] = HECL::SBig(nameIndex[n].strIdx);
|
|
|
|
}
|
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);
|
2015-07-14 00:38:48 +00:00
|
|
|
}
|
|
|
|
std::unique_ptr<atUint32[]> strOffs(new atUint32[langCount * strCount]);
|
|
|
|
for (atUint32 l=0 ; l<langCount ; ++l)
|
|
|
|
{
|
|
|
|
reader.readUint32();
|
|
|
|
for (atUint32 s=0 ; s<strCount ; ++s)
|
|
|
|
strOffs[l*strCount+s] = reader.readUint32();
|
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)
|
|
|
|
{
|
2015-07-14 00:38:48 +00:00
|
|
|
reader.seek(strBase + strOffs[l*strCount+s], Athena::Begin);
|
2015-07-13 06:29:12 +00:00
|
|
|
atUint32 len = reader.readUint32();
|
|
|
|
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);
|
|
|
|
for (std::pair<FourCC, std::vector<std::string>>& item : langs)
|
|
|
|
langMap.emplace(item.first, &item.second);
|
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-14 00:38:48 +00:00
|
|
|
{
|
2015-07-16 01:57:34 +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
|
|
|
|
|
|
|
atUint32 version = reader.readUint32();
|
|
|
|
if (version != 3)
|
2015-07-14 00:38:48 +00:00
|
|
|
{
|
2015-07-16 01:57:34 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void STRG::write(Athena::io::IStreamWriter& writer) const
|
|
|
|
{
|
|
|
|
writer.setEndian(Athena::BigEndian);
|
|
|
|
writer.writeUint32(0x87654321);
|
|
|
|
writer.writeUint32(3);
|
|
|
|
writer.writeUint32(langs.size());
|
|
|
|
atUint32 strCount = STRG::count();
|
|
|
|
writer.writeUint32(strCount);
|
|
|
|
|
|
|
|
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;
|
|
|
|
writer.writeUint32(names.size());
|
|
|
|
writer.writeUint32(nameTableSz);
|
|
|
|
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
|
|
|
{
|
|
|
|
writer.writeUint32(offset);
|
|
|
|
writer.writeInt32(name.second);
|
|
|
|
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-07-13 06:29:12 +00:00
|
|
|
writer.writeUint32(langSz);
|
|
|
|
|
|
|
|
for (const std::string& str : lang.second)
|
|
|
|
{
|
|
|
|
writer.writeUint32(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())
|
|
|
|
{
|
|
|
|
writer.writeUint32(1);
|
|
|
|
writer.writeUByte(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::string& str = lang.second[s];
|
|
|
|
writer.writeUint32(str.size() + 1);
|
|
|
|
writer.writeString(str);
|
|
|
|
}
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 01:57:34 +00:00
|
|
|
bool STRG::readAngelScript(const AngelScript::asIScriptModule& in)
|
|
|
|
{
|
2015-07-22 19:05:18 +00:00
|
|
|
return false;
|
2015-07-16 01:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void STRG::writeAngelScript(std::ofstream& out) const
|
|
|
|
{
|
2015-07-18 04:33:38 +00:00
|
|
|
for (const std::pair<FourCC, std::vector<std::string>>& lang : langs)
|
|
|
|
{
|
|
|
|
out << "STRG::Language " << lang.first.toString() << "({";
|
|
|
|
bool comma = false;
|
|
|
|
unsigned idx = 0;
|
|
|
|
for (const std::string& str : lang.second)
|
|
|
|
{
|
|
|
|
if (comma)
|
|
|
|
out << ",";
|
|
|
|
out << "\n/* " << idx++ << " */ \"";
|
|
|
|
out << str << "\"";
|
|
|
|
comma = true;
|
|
|
|
}
|
|
|
|
out << "\n});\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
out << "STRG::Names NAMES({";
|
|
|
|
bool comma = false;
|
|
|
|
for (const std::pair<std::string, int32_t>& name : names)
|
|
|
|
{
|
|
|
|
if (comma)
|
|
|
|
out << ",";
|
|
|
|
out << "\n ";
|
|
|
|
comma = true;
|
|
|
|
out << "{\"" << name.first << "\", " << name.second << "}";
|
|
|
|
}
|
|
|
|
out << "\n});\n";
|
2015-07-16 01:57:34 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|
|
|
|
}
|