STRG YAML for MP2/3

This commit is contained in:
Jack Andersen 2015-08-08 17:55:11 -10:00
parent db46dbdfdb
commit ca49972974
10 changed files with 209 additions and 23 deletions

View File

@ -10,7 +10,7 @@
namespace Retro namespace Retro
{ {
struct ISTRG struct ISTRG : BigYAML
{ {
virtual ~ISTRG() {} virtual ~ISTRG() {}

View File

@ -148,10 +148,6 @@ struct ANCS : BigYAML
struct IMetaAnim : BigYAML struct IMetaAnim : BigYAML
{ {
Delete expl; Delete expl;
void read(Athena::io::IStreamReader&) {}
void write(Athena::io::IStreamWriter&) const {}
void fromYAML(Athena::io::YAMLDocReader&) {}
void toYAML(Athena::io::YAMLDocWriter&) const {}
enum Type enum Type
{ {
MAPrimitive = 0, MAPrimitive = 0,
@ -230,10 +226,6 @@ struct ANCS : BigYAML
struct IMetaTrans : BigYAML struct IMetaTrans : BigYAML
{ {
Delete expl; Delete expl;
void read(Athena::io::IStreamReader&) {}
void write(Athena::io::IStreamWriter&) const {}
void fromYAML(Athena::io::YAMLDocReader&) {}
void toYAML(Athena::io::YAMLDocWriter&) const {}
enum Type enum Type
{ {
MTMetaAnim = 0, MTMetaAnim = 0,

View File

@ -159,13 +159,13 @@ void STRG::fromYAML(Athena::io::YAMLDocReader& reader)
langMap.clear(); langMap.clear();
langMap.reserve(langs.size()); langMap.reserve(langs.size());
for (std::pair<FourCC, std::vector<std::wstring>>& item : langs) for (auto& item : langs)
langMap.emplace(item.first, &item.second); langMap.emplace(item.first, &item.second);
} }
void STRG::toYAML(Athena::io::YAMLDocWriter& writer) const void STRG::toYAML(Athena::io::YAMLDocWriter& writer) const
{ {
for (const std::pair<FourCC, std::vector<std::wstring>>& lang : langs) for (const auto& lang : langs)
{ {
writer.enterSubVector(lang.first.toString().c_str()); writer.enterSubVector(lang.first.toString().c_str());
for (const std::wstring& str : lang.second) for (const std::wstring& str : lang.second)

View File

@ -11,10 +11,10 @@ namespace Retro
namespace DNAMP1 namespace DNAMP1
{ {
struct STRG : ISTRG, BigYAML struct STRG : ISTRG
{ {
DECL_EXPLICIT_DNA DECL_YAML
DECL_EXPLICIT_YAML Delete expl;
void _read(Athena::io::IStreamReader& reader); void _read(Athena::io::IStreamReader& reader);
std::vector<std::pair<FourCC, std::vector<std::wstring>>> langs; std::vector<std::pair<FourCC, std::vector<std::wstring>>> langs;
std::unordered_map<FourCC, std::vector<std::wstring>*> langMap; std::unordered_map<FourCC, std::vector<std::wstring>*> langMap;
@ -58,7 +58,7 @@ struct STRG : ISTRG, BigYAML
return HECL::SystemString(); return HECL::SystemString();
} }
static bool Extract(const SpecBase& dataspec, PAKEntryReadStream& rs, const HECL::ProjectPath& outPath) static bool Extract(const SpecBase&, PAKEntryReadStream& rs, const HECL::ProjectPath& outPath)
{ {
STRG strg; STRG strg;
strg.read(rs); strg.read(rs);

View File

@ -183,7 +183,7 @@ ResExtractor<PAKBridge> PAKBridge::LookupExtractor(const DNAMP1::PAK::Entry& ent
switch (entry.type.toUint32()) switch (entry.type.toUint32())
{ {
case SBIG('STRG'): case SBIG('STRG'):
return {STRG::Extract, nullptr, ".as"}; return {STRG::Extract, nullptr, ".yaml"};
case SBIG('TXTR'): case SBIG('TXTR'):
return {TXTR::Extract, nullptr, ".png"}; return {TXTR::Extract, nullptr, ".png"};
} }

View File

@ -139,5 +139,87 @@ void STRG::write(Athena::io::IStreamWriter& writer) const
} }
} }
void STRG::fromYAML(Athena::io::YAMLDocReader& reader)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> wconv;
const Athena::io::YAMLNode* root = reader.getRootNode();
/* Validate Pass */
if (root->m_type == YAML_MAPPING_NODE)
{
for (const auto& lang : root->m_mapChildren)
{
if (!lang.first.compare("names"))
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)
{
std::vector<std::wstring> strs;
for (const auto& str : lang.second->m_seqChildren)
strs.emplace_back(wconv.from_bytes(str->m_scalarString));
langs.emplace_back(FourCC(lang.first.c_str()), strs);
}
names.clear();
const Athena::io::YAMLNode* namesNode = root->findMapChild("names");
if (namesNode)
for (const auto& item : namesNode->m_mapChildren)
names[item.first] = Athena::io::NodeToVal<atInt32>(item.second.get());
langMap.clear();
langMap.reserve(langs.size());
for (std::pair<FourCC, std::vector<std::wstring>>& item : langs)
langMap.emplace(item.first, &item.second);
}
void STRG::toYAML(Athena::io::YAMLDocWriter& writer) const
{
for (const auto& lang : langs)
{
writer.enterSubVector(lang.first.toString().c_str());
for (const std::wstring& str : lang.second)
writer.writeWString(nullptr, str);
writer.leaveSubVector();
}
if (names.size())
{
writer.enterSubRecord("names");
for (const auto& name : names)
{
writer.enterSubRecord(name.first.c_str());
writer.writeInt32(nullptr, name.second);
writer.leaveSubRecord();
}
writer.leaveSubRecord();
}
}
} }
} }

View File

@ -10,9 +10,10 @@ namespace Retro
namespace DNAMP2 namespace DNAMP2
{ {
struct STRG : ISTRG, BigDNA struct STRG : ISTRG
{ {
DECL_EXPLICIT_DNA DECL_YAML
Delete expl;
void _read(Athena::io::IStreamReader& reader); void _read(Athena::io::IStreamReader& reader);
std::vector<std::pair<FourCC, std::vector<std::wstring>>> langs; std::vector<std::pair<FourCC, std::vector<std::wstring>>> langs;
std::unordered_map<FourCC, std::vector<std::wstring>*> langMap; std::unordered_map<FourCC, std::vector<std::wstring>*> langMap;
@ -63,13 +64,24 @@ struct STRG : ISTRG, BigDNA
return HECL::SystemString(); return HECL::SystemString();
} }
static bool Extract(const SpecBase& dataspec, PAKEntryReadStream& rs, const HECL::ProjectPath& outPath) static bool Extract(const SpecBase&, PAKEntryReadStream& rs, const HECL::ProjectPath& outPath)
{ {
STRG strg;
strg.read(rs);
FILE* fp = HECL::Fopen(outPath.getAbsolutePath().c_str(), _S("wb"));
strg.toYAMLFile(fp);
fclose(fp);
return true; return true;
} }
static bool Cook(const HECL::ProjectPath& inPath, const HECL::ProjectPath& outPath) static bool Cook(const HECL::ProjectPath& inPath, const HECL::ProjectPath& outPath)
{ {
STRG strg;
FILE* fp = HECL::Fopen(inPath.getAbsolutePath().c_str(), _S("rb"));
strg.fromYAMLFile(fp);
fclose(fp);
Athena::io::FileWriter ws(outPath.getAbsolutePath());
strg.write(ws);
return true; return true;
} }
}; };

View File

@ -57,7 +57,7 @@ ResExtractor<PAKBridge> PAKBridge::LookupExtractor(const PAK::Entry& entry)
switch (entry.type.toUint32()) switch (entry.type.toUint32())
{ {
case SBIG('STRG'): case SBIG('STRG'):
return {STRG::Extract, nullptr, ".as"}; return {STRG::Extract, nullptr, ".yaml"};
case SBIG('TXTR'): case SBIG('TXTR'):
return {TXTR::Extract, nullptr, ".png"}; return {TXTR::Extract, nullptr, ".png"};
} }

View File

@ -86,6 +86,72 @@ void STRG::read(Athena::io::IStreamReader& reader)
_read(reader); _read(reader);
} }
void STRG::fromYAML(Athena::io::YAMLDocReader& reader)
{
const Athena::io::YAMLNode* root = reader.getRootNode();
/* Validate Pass */
if (root->m_type == YAML_MAPPING_NODE)
{
for (const auto& lang : root->m_mapChildren)
{
if (!lang.first.compare("names"))
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;
}
const Athena::io::YAMLNode* nameYAML = root->findMapChild("names");
names.clear();
if (nameYAML && nameYAML->m_type == YAML_MAPPING_NODE)
for (const auto& item : nameYAML->m_mapChildren)
if (item.second->m_type == YAML_SCALAR_NODE)
names[item.first] = Athena::io::NodeToVal<atInt32>(item.second.get());
langs.clear();
langs.reserve(root->m_mapChildren.size());
for (const auto& item : root->m_mapChildren)
{
if (!item.first.compare("names") || item.first.size() != 4 ||
item.second->m_type != YAML_SEQUENCE_NODE)
continue;
std::vector<std::string> strs;
for (const auto& node : item.second->m_seqChildren)
if (node->m_type == YAML_SCALAR_NODE)
strs.emplace_back(node->m_scalarString);
langs.emplace_back(std::make_pair(FourCC(item.first.c_str()), std::move(strs)));
}
langMap.clear();
langMap.reserve(langs.size());
for (std::pair<FourCC, std::vector<std::string>>& item : langs)
langMap.emplace(item.first, &item.second);
}
void STRG::write(Athena::io::IStreamWriter& writer) const void STRG::write(Athena::io::IStreamWriter& writer) const
{ {
writer.setEndian(Athena::BigEndian); writer.setEndian(Athena::BigEndian);
@ -147,5 +213,28 @@ void STRG::write(Athena::io::IStreamWriter& writer) const
} }
} }
void STRG::toYAML(Athena::io::YAMLDocWriter& writer) const
{
for (const auto& item : langs)
{
writer.enterSubVector(item.first.toString().c_str());
for (const std::string& str : item.second)
writer.writeString(nullptr, str);
writer.leaveSubVector();
}
if (names.size())
{
writer.enterSubRecord("names");
for (const auto& item : names)
{
writer.enterSubRecord(item.first.c_str());
writer.writeInt32(nullptr, item.second);
writer.leaveSubRecord();
}
writer.leaveSubRecord();
}
}
} }
} }

View File

@ -10,9 +10,10 @@ namespace Retro
namespace DNAMP3 namespace DNAMP3
{ {
struct STRG : ISTRG, BigDNA struct STRG : ISTRG
{ {
DECL_EXPLICIT_DNA DECL_YAML
Delete expl;
void _read(Athena::io::IStreamReader& reader); void _read(Athena::io::IStreamReader& reader);
std::vector<std::pair<FourCC, std::vector<std::string>>> langs; std::vector<std::pair<FourCC, std::vector<std::string>>> langs;
std::unordered_map<FourCC, std::vector<std::string>*> langMap; std::unordered_map<FourCC, std::vector<std::string>*> langMap;
@ -63,13 +64,23 @@ struct STRG : ISTRG, BigDNA
return HECL::SystemString(); return HECL::SystemString();
} }
static bool Extract(const SpecBase& dataspec, PAKEntryReadStream& rs, const HECL::ProjectPath& outPath) static bool Extract(const SpecBase&, PAKEntryReadStream& rs, const HECL::ProjectPath& outPath)
{ {
std::unique_ptr<ISTRG> strg = LoadSTRG(rs);
FILE* fp = HECL::Fopen(outPath.getAbsolutePath().c_str(), _S("wb"));
strg->toYAMLFile(fp);
fclose(fp);
return true; return true;
} }
static bool Cook(const HECL::ProjectPath& inPath, const HECL::ProjectPath& outPath) static bool Cook(const HECL::ProjectPath& inPath, const HECL::ProjectPath& outPath)
{ {
STRG strg;
FILE* fp = HECL::Fopen(inPath.getAbsolutePath().c_str(), _S("rb"));
strg.fromYAMLFile(fp);
fclose(fp);
Athena::io::FileWriter ws(outPath.getAbsolutePath());
strg.write(ws);
return true; return true;
} }
}; };