From 6b06266eb8d35a7865aa1f942ecbe393e7cde740 Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Fri, 1 Oct 2021 22:19:47 -0300 Subject: Add `--static` flag (#193) * remove static from every extracted asset * fix makefile * Move a bunch of code to Declaration * make static enablalble * fix texture static * clean up * minor fixes * Format * add small docs * fix * add table * add note in the readme * typo * maybe_unused * Add warning to ZSymbol * format * fix --- ZAPD/ZFile.cpp | 131 +++++++++------------------------------------------------ 1 file changed, 21 insertions(+), 110 deletions(-) (limited to 'ZAPD/ZFile.cpp') diff --git a/ZAPD/ZFile.cpp b/ZAPD/ZFile.cpp index 6a039a1..8f74806 100644 --- a/ZAPD/ZFile.cpp +++ b/ZAPD/ZFile.cpp @@ -109,7 +109,7 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, if (reader->Attribute("RangeEnd") != nullptr) rangeEnd = StringHelper::StrToL(reader->Attribute("RangeEnd"), 16); - if( rangeStart > rangeEnd ) + if (rangeStart > rangeEnd) throw std::runtime_error("Error: RangeStart must be before than RangeEnd."); // Commented until ZArray doesn't use a ZFile to parse it's contents anymore. @@ -504,7 +504,7 @@ Declaration* ZFile::AddDeclarationIncludeArray(uint32_t address, std::string inc Declaration* decl = GetDeclaration(address); if (decl == nullptr) { - Declaration* decl = new Declaration(includePath, size, varType, varName); + decl = new Declaration(includePath, size, varType, varName); decl->isArray = true; decl->arrayItemCnt = arrayItemCnt; @@ -529,7 +529,7 @@ void ZFile::AddDeclarationDebugChecks(uint32_t address) #ifdef _DEBUG if (address == 0x0000) { - int32_t bp = 0; + [[maybe_unused]] int32_t bp = 0; } #endif } @@ -716,8 +716,16 @@ void ZFile::GeneratePlaceholderDeclarations() // Generate placeholder declarations for (ZResource* res : resources) { - if (GetDeclaration(res->GetRawDataIndex()) == nullptr) - AddDeclarationPlaceholder(res->GetRawDataIndex(), res->GetName()); + if (GetDeclaration(res->GetRawDataIndex()) != nullptr) + { + continue; + } + + Declaration* decl = AddDeclarationPlaceholder(res->GetRawDataIndex(), res->GetName()); + if (res->GetResourceType() == ZResourceType::Symbol) + { + decl->staticConf = StaticConfig::Off; + } } } @@ -856,7 +864,7 @@ std::string ZFile::ProcessDeclarations() { char buffer[2048]; - sprintf(buffer, "static u32 align%02X = 0;\n", curPtr); + sprintf(buffer, "u32 %s_align%02X = 0;\n", name.c_str(), curPtr); item.second->preText = buffer + item.second->preText; declarations[lastAddr]->size += 4; @@ -962,8 +970,8 @@ std::string ZFile::ProcessDeclarations() } Declaration* decl = AddDeclarationArray( - unaccountedAddress, DeclarationAlignment::Align4, diff, "static u8", - StringHelper::Sprintf("%s_%06X", unaccountedPrefix.c_str(), + unaccountedAddress, DeclarationAlignment::Align4, diff, "u8", + StringHelper::Sprintf("%s_%s_%06X", name.c_str(), unaccountedPrefix.c_str(), unaccountedAddress), diff, src); decl->isUnaccounted = true; @@ -998,42 +1006,12 @@ std::string ZFile::ProcessDeclarations() // Go through include declarations // First, handle the prototypes (static only for now) - int32_t protoCnt = 0; for (std::pair item : declarations) { - if (StringHelper::StartsWith(item.second->varType, "static ") && - !item.second->isUnaccounted) - { - if (item.second->isArray) - { - if (item.second->arrayItemCntStr != "") - { - output += StringHelper::Sprintf("%s %s[%s];\n", item.second->varType.c_str(), - item.second->varName.c_str(), - item.second->arrayItemCntStr.c_str()); - } - else if (item.second->arrayItemCnt == 0) - { - output += StringHelper::Sprintf("%s %s[];\n", item.second->varType.c_str(), - item.second->varName.c_str()); - } - else - { - output += StringHelper::Sprintf("%s %s[%i];\n", item.second->varType.c_str(), - item.second->varName.c_str(), - item.second->arrayItemCnt); - } - } - else - output += StringHelper::Sprintf("%s %s;\n", item.second->varType.c_str(), - item.second->varName.c_str()); - - protoCnt++; - } + output += item.second->GetStaticForwardDeclarationStr(); } - if (protoCnt > 0) - output += "\n"; + output += "\n"; // Next, output the actual declarations for (std::pair item : declarations) @@ -1059,61 +1037,11 @@ std::string ZFile::ProcessDeclarations() item.second->text); } - if (item.second->arrayItemCntStr != "") - output += StringHelper::Sprintf( - "%s %s[%s] = {\n #include \"%s\"\n};\n\n", item.second->varType.c_str(), - item.second->varName.c_str(), item.second->arrayItemCntStr.c_str(), - item.second->includePath.c_str()); - else - output += StringHelper::Sprintf( - "%s %s[] = {\n #include \"%s\"\n};\n\n", item.second->varType.c_str(), - item.second->varName.c_str(), item.second->includePath.c_str()); + output += item.second->GetExternalDeclarationStr(); } else if (item.second->varType != "") { - if (item.second->preText != "") - output += item.second->preText + "\n"; - - { - if (item.second->isArray) - { - if (item.second->arrayItemCntStr != "") - { - output += StringHelper::Sprintf( - "%s %s[%s];\n", item.second->varType.c_str(), - item.second->varName.c_str(), item.second->arrayItemCntStr.c_str()); - } - else - { - if (item.second->arrayItemCnt == 0) - output += - StringHelper::Sprintf("%s %s[] = {\n", item.second->varType.c_str(), - item.second->varName.c_str()); - else - output += StringHelper::Sprintf( - "%s %s[%i] = {\n", item.second->varType.c_str(), - item.second->varName.c_str(), item.second->arrayItemCnt); - } - - output += item.second->text + "\n"; - } - else - { - output += StringHelper::Sprintf("%s %s = { ", item.second->varType.c_str(), - item.second->varName.c_str()); - output += item.second->text; - } - - if (output.back() == '\n') - output += "};"; - else - output += " };"; - } - - output += " " + item.second->rightText + "\n\n"; - - if (item.second->postText != "") - output += item.second->postText + "\n"; + output += item.second->GetNormalDeclarationStr(); } } @@ -1186,24 +1114,7 @@ std::string ZFile::ProcessExterns() continue; } - if (!StringHelper::StartsWith(item.second->varType, "static ") && - item.second->varType != "") // && item.second->includePath == "") - { - if (item.second->isArray) - { - if (item.second->arrayItemCnt == 0) - output += - StringHelper::Sprintf("extern %s %s[];\n", item.second->varType.c_str(), - item.second->varName.c_str()); - else - output += StringHelper::Sprintf( - "extern %s %s[%i];\n", item.second->varType.c_str(), - item.second->varName.c_str(), item.second->arrayItemCnt); - } - else - output += StringHelper::Sprintf("extern %s %s;\n", item.second->varType.c_str(), - item.second->varName.c_str()); - } + output += item.second->GetExternStr(); } output += "\n"; -- cgit v1.2.3