Skip to content

Commit

Permalink
Add hex-commented-{left,right} options for --cs-float (NEstelami#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragorn421 authored Mar 22, 2024
1 parent 1300a4f commit b9f0bdd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions ZAPD/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ enum class CsFloatType
HexOnly,
FloatOnly,
HexAndFloat,
HexAndCommentedFloatLeft,
HexAndCommentedFloatRight,
};

class Globals
Expand Down
29 changes: 19 additions & 10 deletions ZAPD/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);

Expand Down Expand Up @@ -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)
Expand All @@ -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());
Expand All @@ -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;
}

Expand All @@ -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")
{
Expand Down Expand Up @@ -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]));
}
}

Expand All @@ -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;
}
Expand Down
29 changes: 9 additions & 20 deletions ZAPD/ZCutscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit b9f0bdd

Please sign in to comment.