From b9f0bdd8ef3ec0ad143ee637f14241d61ab9d8df Mon Sep 17 00:00:00 2001 From: Dragorn421 Date: Fri, 22 Mar 2024 23:09:39 +0100 Subject: [PATCH] Add hex-commented-{left,right} options for --cs-float (#318) --- README.md | 8 +++++--- ZAPD/Globals.h | 2 ++ ZAPD/Main.cpp | 29 +++++++++++++++++++---------- ZAPD/ZCutscene.cpp | 29 +++++++++-------------------- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index d51dd055..c7a9c284 100644 --- a/README.md +++ b/README.md @@ -119,9 +119,11 @@ ZAPD also accepts the following list of extra parameters: - This behaviour can be overridden per asset using `Static=` in the respective XML node. - `--cs-float` : How cutscene floats should be extracted. - Valid values: - - `hex` - - `float` - - `both` + - `hex`: `0x42280000` + - `float`: `42.0f` + - `both`: `CS_FLOAT(0x42280000, 42.0f)` + - `hex-commented-left`: `/* 42.0f */ 0x42280000` + - `hex-commented-right`: `0x42280000 /* 42.0f */` - `-W...`: warning flags, see below Additionally, you can pass the flag `--version` to see the current ZAPD version. If that flag is passed, ZAPD will ignore any other parameter passed. diff --git a/ZAPD/Globals.h b/ZAPD/Globals.h index 14bd0658..6f603e7a 100644 --- a/ZAPD/Globals.h +++ b/ZAPD/Globals.h @@ -21,6 +21,8 @@ enum class CsFloatType HexOnly, FloatOnly, HexAndFloat, + HexAndCommentedFloatLeft, + HexAndCommentedFloatRight, }; class Globals diff --git a/ZAPD/Main.cpp b/ZAPD/Main.cpp index eb1737d6..75529119 100644 --- a/ZAPD/Main.cpp +++ b/ZAPD/Main.cpp @@ -41,7 +41,7 @@ void Arg_CsFloatMode(int& i, char* argv[]); int main(int argc, char* argv[]); bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path& outPath, - ZFileMode fileMode); + ZFileMode fileMode); void ParseArgs(int& argc, char* argv[]); @@ -115,7 +115,7 @@ int main(int argc, char* argv[]) returnCode = HandleExtract(fileMode, exporterSet); else if (fileMode == ZFileMode::BuildTexture) BuildAssetTexture(Globals::Instance->inputPath, Globals::Instance->texType, - Globals::Instance->outputPath); + Globals::Instance->outputPath); else if (fileMode == ZFileMode::BuildBackground) BuildAssetBackground(Globals::Instance->inputPath, Globals::Instance->outputPath); else if (fileMode == ZFileMode::BuildBlob) @@ -126,7 +126,7 @@ int main(int argc, char* argv[]) } bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path& outPath, - ZFileMode fileMode) + ZFileMode fileMode) { tinyxml2::XMLDocument doc; tinyxml2::XMLError eResult = doc.LoadFile(xmlFilePath.string().c_str()); @@ -135,7 +135,7 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path { // TODO: use XMLDocument::ErrorIDToName to get more specific error messages here HANDLE_ERROR(WarningType::InvalidXML, - StringHelper::Sprintf("invalid XML file: '%s'", xmlFilePath.c_str()), ""); + StringHelper::Sprintf("invalid XML file: '%s'", xmlFilePath.c_str()), ""); return false; } @@ -150,7 +150,7 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path } for (tinyxml2::XMLElement* child = root->FirstChildElement(); child != NULL; - child = child->NextSiblingElement()) + child = child->NextSiblingElement()) { if (std::string_view(child->Name()) == "File") { @@ -409,14 +409,23 @@ void Arg_CsFloatMode([[maybe_unused]] int& i, [[maybe_unused]] char* argv[]) { Globals::Instance->floatType = CsFloatType::HexAndFloat; } + else if (std::strcmp(argv[i], "hex-commented-left") == 0) + { + Globals::Instance->floatType = CsFloatType::HexAndCommentedFloatLeft; + } + else if (std::strcmp(argv[i], "hex-commented-right") == 0) + { + Globals::Instance->floatType = CsFloatType::HexAndCommentedFloatRight; + } else { Globals::Instance->floatType = CsFloatType::FloatOnly; HANDLE_WARNING( WarningType::Always, "Invalid CS Float Type", - StringHelper::Sprintf("Invalid CS float type entered. Expected \"hex\", \"float\", or " - "\"both\". Got %s.\n Defaulting to \"float\".", - argv[i])); + StringHelper::Sprintf("Invalid CS float type entered. Expected \"hex\", \"float\", " + "\"both\", \"hex-commented-left\" or \"hex-commented-right\". " + "Got %s.\n Defaulting to \"float\".", + argv[i])); } } @@ -440,14 +449,14 @@ int HandleExtract(ZFileMode fileMode, ExporterSet* exporterSet) printf("Parsing external file from config: '%s'\n", externalXmlFilePath.c_str()); parseSuccessful = Parse(externalXmlFilePath, Globals::Instance->baseRomPath, - extFile.outPath, ZFileMode::ExternalFile); + extFile.outPath, ZFileMode::ExternalFile); if (!parseSuccessful) return 1; } parseSuccessful = Parse(Globals::Instance->inputPath, Globals::Instance->baseRomPath, - Globals::Instance->outputPath, fileMode); + Globals::Instance->outputPath, fileMode); if (!parseSuccessful) return 1; } diff --git a/ZAPD/ZCutscene.cpp b/ZAPD/ZCutscene.cpp index 0bf0363d..7ae431eb 100644 --- a/ZAPD/ZCutscene.cpp +++ b/ZAPD/ZCutscene.cpp @@ -375,33 +375,22 @@ ZResourceType ZCutscene::GetResourceType() const std::string ZCutscene::GetCsEncodedFloat(float f, CsFloatType type, bool useSciNotation) { + uint32_t i; + std::memcpy(&i, &f, sizeof(i)); + switch (type) { default: // This default case will NEVER be reached, but GCC still gives a warning. case CsFloatType::HexOnly: - { - uint32_t i; - std::memcpy(&i, &f, sizeof(i)); return StringHelper::Sprintf("0x%08X", i); - } case CsFloatType::FloatOnly: - { - if (useSciNotation) - { - return StringHelper::Sprintf("%.8ef", f); - } - return StringHelper::Sprintf("%ff", f); - } + return StringHelper::Sprintf(useSciNotation ? "%.8ef" : "%ff", f); case CsFloatType::HexAndFloat: - { - uint32_t i; - std::memcpy(&i, &f, sizeof(i)); - if (useSciNotation) - { - return StringHelper::Sprintf("CS_FLOAT(0x%08X, %.8ef)", i, f); - } - return StringHelper::Sprintf("CS_FLOAT(0x%08X, %ff)", i, f); - } + return StringHelper::Sprintf(useSciNotation ? "CS_FLOAT(0x%08X, %.8ef)" : "CS_FLOAT(0x%08X, %ff)", i, f); + case CsFloatType::HexAndCommentedFloatLeft: + return StringHelper::Sprintf(useSciNotation ? "/* %.8ef */ 0x%08X" : "/* %ff */ 0x%08X", f, i); + case CsFloatType::HexAndCommentedFloatRight: + return StringHelper::Sprintf(useSciNotation ? "0x%08X /* %.8ef */" : "0x%08X /* %ff */", i, f); } }