diff options
41 files changed, 552 insertions, 355 deletions
diff --git a/ExporterTest/CollisionExporter.cpp b/ExporterTest/CollisionExporter.cpp index 81a9b54..b7b2852 100644 --- a/ExporterTest/CollisionExporter.cpp +++ b/ExporterTest/CollisionExporter.cpp @@ -1,6 +1,7 @@ #include "CollisionExporter.h" -void ExporterExample_Collision::Save(ZResource* res, [[maybe_unused]]fs::path outPath, BinaryWriter* writer) +void ExporterExample_Collision::Save(ZResource* res, [[maybe_unused]] fs::path outPath, + BinaryWriter* writer) { ZCollisionHeader* col = (ZCollisionHeader*)res; diff --git a/ExporterTest/CollisionExporter.h b/ExporterTest/CollisionExporter.h index 2828995..5f48e65 100644 --- a/ExporterTest/CollisionExporter.h +++ b/ExporterTest/CollisionExporter.h @@ -1,7 +1,7 @@ #pragma once -#include "ZResource.h" #include "ZCollision.h" +#include "ZResource.h" class ExporterExample_Collision : public ZResourceExporter { diff --git a/ExporterTest/Main.cpp b/ExporterTest/Main.cpp index 1fb6bea..ee47cbf 100644 --- a/ExporterTest/Main.cpp +++ b/ExporterTest/Main.cpp @@ -1,7 +1,7 @@ -#include <TextureExporter.h> -#include <RoomExporter.h> #include <CollisionExporter.h> #include <Globals.h> +#include <RoomExporter.h> +#include <TextureExporter.h> enum class ExporterFileMode { @@ -20,17 +20,15 @@ static void ExporterParseFileMode(std::string buildMode, ZFileMode& fileMode) fileMode = (ZFileMode)ExporterFileMode::ModeExample3; } -static void ExporterParseArgs([[maybe_unused]]int argc, char* argv[], int& i) +static void ExporterParseArgs([[maybe_unused]] int argc, char* argv[], int& i) { std::string arg = argv[i]; if (arg == "--do-x") { - } else if (arg == "--do-y") { - } } @@ -61,7 +59,8 @@ static void ExporterFileEnd(ZFile* file) static void ImportExporters() { // In this example we set up a new exporter called "EXAMPLE". - // By running ZAPD with the argument -se EXAMPLE, we tell it that we want to use this exporter for our resources. + // By running ZAPD with the argument -se EXAMPLE, we tell it that we want to use this exporter + // for our resources. ExporterSet* exporterSet = new ExporterSet(); exporterSet->processFileModeFunc = ExporterProcessFileMode; exporterSet->parseFileModeFunc = ExporterParseFileMode; @@ -75,5 +74,6 @@ static void ImportExporters() Globals::AddExporter("EXAMPLE", exporterSet); } -// When ZAPD starts up, it will automatically call the below function, which in turn sets up our exporters. +// When ZAPD starts up, it will automatically call the below function, which in turn sets up our +// exporters. REGISTER_EXPORTER(ImportExporters)
\ No newline at end of file diff --git a/ExporterTest/RoomExporter.cpp b/ExporterTest/RoomExporter.cpp index dc8c823..b3c8d6c 100644 --- a/ExporterTest/RoomExporter.cpp +++ b/ExporterTest/RoomExporter.cpp @@ -1,31 +1,31 @@ #include "RoomExporter.h" +#include <CollisionExporter.h> #include <Utils/BinaryWriter.h> -#include <Utils/MemoryStream.h> #include <Utils/File.h> -#include <ZRoom/Commands/SetWind.h> -#include <ZRoom/Commands/SetTimeSettings.h> -#include <ZRoom/Commands/SetSkyboxModifier.h> -#include <ZRoom/Commands/SetSoundSettings.h> +#include <Utils/MemoryStream.h> #include <ZRoom/Commands/SetCameraSettings.h> +#include <ZRoom/Commands/SetCollisionHeader.h> +#include <ZRoom/Commands/SetCsCamera.h> +#include <ZRoom/Commands/SetEchoSettings.h> +#include <ZRoom/Commands/SetEntranceList.h> +#include <ZRoom/Commands/SetLightingSettings.h> #include <ZRoom/Commands/SetMesh.h> #include <ZRoom/Commands/SetRoomBehavior.h> -#include <ZRoom/Commands/SetCsCamera.h> #include <ZRoom/Commands/SetRoomList.h> -#include <ZRoom/Commands/SetCollisionHeader.h> -#include <ZRoom/Commands/SetEntranceList.h> +#include <ZRoom/Commands/SetSkyboxModifier.h> +#include <ZRoom/Commands/SetSkyboxSettings.h> +#include <ZRoom/Commands/SetSoundSettings.h> #include <ZRoom/Commands/SetSpecialObjects.h> #include <ZRoom/Commands/SetStartPositionList.h> -#include <ZRoom/Commands/SetSkyboxSettings.h> -#include <ZRoom/Commands/SetLightingSettings.h> -#include <ZRoom/Commands/SetEchoSettings.h> -#include <CollisionExporter.h> +#include <ZRoom/Commands/SetTimeSettings.h> +#include <ZRoom/Commands/SetWind.h> void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* writer) { ZRoom* room = (ZRoom*)res; - //MemoryStream* memStream = new MemoryStream(); - //BinaryWriter* writer = new BinaryWriter(memStream); + // MemoryStream* memStream = new MemoryStream(); + // BinaryWriter* writer = new BinaryWriter(memStream); for (uint i = 0; i < room->commands.size() * 8; i++) writer->Write((uint8_t)0); @@ -33,9 +33,9 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* for (uint i = 0; i < room->commands.size(); i++) { ZRoomCommand* cmd = room->commands[i]; - + writer->Seek(i * 8, SeekOffsetType::Start); - + writer->Write((uint8_t)cmd->cmdID); switch (cmd->cmdID) @@ -44,115 +44,115 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* { SetWind* cmdSetWind = (SetWind*)cmd; - writer->Write((uint8_t)0); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)0); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 - writer->Write(cmdSetWind->windWest); // 0x04 - writer->Write(cmdSetWind->windVertical); // 0x05 - writer->Write(cmdSetWind->windSouth); // 0x06 - writer->Write(cmdSetWind->clothFlappingStrength); // 0x07 + writer->Write(cmdSetWind->windWest); // 0x04 + writer->Write(cmdSetWind->windVertical); // 0x05 + writer->Write(cmdSetWind->windSouth); // 0x06 + writer->Write(cmdSetWind->clothFlappingStrength); // 0x07 } - break; + break; case RoomCommand::SetTimeSettings: { SetTimeSettings* cmdTime = (SetTimeSettings*)cmd; - writer->Write((uint8_t)0); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)0); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 - writer->Write(cmdTime->hour); // 0x04 - writer->Write(cmdTime->min); // 0x05 - writer->Write(cmdTime->unk); // 0x06 - writer->Write((uint8_t)0); // 0x07 + writer->Write(cmdTime->hour); // 0x04 + writer->Write(cmdTime->min); // 0x05 + writer->Write(cmdTime->unk); // 0x06 + writer->Write((uint8_t)0); // 0x07 } break; case RoomCommand::SetSkyboxModifier: { SetSkyboxModifier* cmdSkybox = (SetSkyboxModifier*)cmd; - writer->Write((uint8_t)0); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)0); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 - writer->Write(cmdSkybox->disableSky); // 0x04 - writer->Write(cmdSkybox->disableSunMoon); // 0x05 - writer->Write((uint8_t)0); // 0x06 - writer->Write((uint8_t)0); // 0x07 + writer->Write(cmdSkybox->disableSky); // 0x04 + writer->Write(cmdSkybox->disableSunMoon); // 0x05 + writer->Write((uint8_t)0); // 0x06 + writer->Write((uint8_t)0); // 0x07 } break; case RoomCommand::SetEchoSettings: { SetEchoSettings* cmdEcho = (SetEchoSettings*)cmd; - writer->Write((uint8_t)0); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write((uint8_t)0); // 0x04 - writer->Write((uint8_t)0); // 0x05 - writer->Write((uint8_t)0); // 0x06 - writer->Write((uint8_t)cmdEcho->echo); // 0x07 + writer->Write((uint8_t)0); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)0); // 0x04 + writer->Write((uint8_t)0); // 0x05 + writer->Write((uint8_t)0); // 0x06 + writer->Write((uint8_t)cmdEcho->echo); // 0x07 } break; case RoomCommand::SetSoundSettings: { SetSoundSettings* cmdSound = (SetSoundSettings*)cmd; - writer->Write((uint8_t)cmdSound->reverb); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write((uint8_t)0); // 0x04 - writer->Write((uint8_t)0); // 0x05 + writer->Write((uint8_t)cmdSound->reverb); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)0); // 0x04 + writer->Write((uint8_t)0); // 0x05 - writer->Write(cmdSound->nightTimeSFX); // 0x06 - writer->Write(cmdSound->musicSequence); // 0x07 + writer->Write(cmdSound->nightTimeSFX); // 0x06 + writer->Write(cmdSound->musicSequence); // 0x07 } break; case RoomCommand::SetSkyboxSettings: { SetSkyboxSettings* cmdSkybox = (SetSkyboxSettings*)cmd; - writer->Write((uint8_t)cmdSkybox->unk1); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write((uint8_t)cmdSkybox->skyboxNumber); // 0x04 - writer->Write((uint8_t)cmdSkybox->cloudsType); // 0x05 - writer->Write((uint8_t)cmdSkybox->isIndoors); // 0x06 + writer->Write((uint8_t)cmdSkybox->unk1); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)cmdSkybox->skyboxNumber); // 0x04 + writer->Write((uint8_t)cmdSkybox->cloudsType); // 0x05 + writer->Write((uint8_t)cmdSkybox->isIndoors); // 0x06 } break; case RoomCommand::SetRoomBehavior: { SetRoomBehavior* cmdRoom = (SetRoomBehavior*)cmd; - writer->Write((uint8_t)cmdRoom->gameplayFlags); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write(cmdRoom->gameplayFlags2); // 0x04 + writer->Write((uint8_t)cmdRoom->gameplayFlags); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write(cmdRoom->gameplayFlags2); // 0x04 } break; case RoomCommand::SetCsCamera: { SetCsCamera* cmdCsCam = (SetCsCamera*)cmd; - writer->Write((uint8_t)cmdCsCam->cameras.size()); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)cmdCsCam->cameras.size()); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 - writer->Write(cmdCsCam->segmentOffset); // 0x04 + writer->Write(cmdCsCam->segmentOffset); // 0x04 } break; case RoomCommand::SetMesh: { SetMesh* cmdMesh = (SetMesh*)cmd; - + int baseStreamEnd = writer->GetStream().get()->GetLength(); - writer->Write((uint8_t)cmdMesh->data); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)cmdMesh->data); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 - writer->Write(baseStreamEnd); // 0x04 + writer->Write(baseStreamEnd); // 0x04 uint32_t oldOffset = writer->GetBaseAddress(); writer->Seek(baseStreamEnd, SeekOffsetType::Start); @@ -163,15 +163,13 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* if (cmdMesh->meshHeaderType == 0) { - //writer->Write(cmdMesh->) + // writer->Write(cmdMesh->) } else if (cmdMesh->meshHeaderType == 1) { - } else if (cmdMesh->meshHeaderType == 2) { - } writer->Seek(oldOffset, SeekOffsetType::Start); @@ -181,20 +179,20 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* { SetCameraSettings* cmdCam = (SetCameraSettings*)cmd; - writer->Write((uint8_t)cmdCam->cameraMovement); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write(cmdCam->mapHighlight); // 0x04 + writer->Write((uint8_t)cmdCam->cameraMovement); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write(cmdCam->mapHighlight); // 0x04 } break; case RoomCommand::SetLightingSettings: { SetLightingSettings* cmdLight = (SetLightingSettings*)cmd; - writer->Write((uint8_t)cmdLight->settings.size()); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write(cmdLight->segmentOffset); // 0x04 + writer->Write((uint8_t)cmdLight->settings.size()); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write(cmdLight->segmentOffset); // 0x04 uint32_t oldOffset = writer->GetBaseAddress(); writer->Seek(cmdLight->segmentOffset, SeekOffsetType::Start); @@ -236,12 +234,12 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* { SetRoomList* cmdRoom = (SetRoomList*)cmd; - writer->Write((uint8_t)cmdRoom->rooms.size()); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)cmdRoom->rooms.size()); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 int baseStreamEnd = writer->GetLength(); - writer->Write(baseStreamEnd); // 0x04 + writer->Write(baseStreamEnd); // 0x04 uint32_t oldOffset = writer->GetBaseAddress(); writer->Seek(baseStreamEnd, SeekOffsetType::Start); @@ -261,10 +259,10 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* int streamEnd = writer->GetStream().get()->GetLength(); - writer->Write((uint8_t)0); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write(streamEnd); // 0x04 + writer->Write((uint8_t)0); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write(streamEnd); // 0x04 // TODO: NOT DONE @@ -284,10 +282,10 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* uint32_t baseStreamEnd = writer->GetStream().get()->GetLength(); - writer->Write((uint8_t)0); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write(baseStreamEnd); // 0x04 + writer->Write((uint8_t)0); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write(baseStreamEnd); // 0x04 uint32_t oldOffset = writer->GetBaseAddress(); writer->Seek(baseStreamEnd, SeekOffsetType::Start); @@ -305,12 +303,12 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* { SetSpecialObjects* cmdSpecObj = (SetSpecialObjects*)cmd; - writer->Write((uint8_t)cmdSpecObj->elfMessage); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write((uint8_t)0); // 0x04 - writer->Write((uint8_t)0); // 0x05 - writer->Write((uint16_t)cmdSpecObj->globalObject); // 0x06 + writer->Write((uint8_t)cmdSpecObj->elfMessage); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)0); // 0x04 + writer->Write((uint8_t)0); // 0x05 + writer->Write((uint16_t)cmdSpecObj->globalObject); // 0x06 } break; case RoomCommand::SetStartPositionList: @@ -319,10 +317,10 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* uint32_t baseStreamEnd = writer->GetStream().get()->GetLength(); - writer->Write((uint8_t)cmdStartPos->actors.size()); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write(baseStreamEnd); // 0x04 + writer->Write((uint8_t)cmdStartPos->actors.size()); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write(baseStreamEnd); // 0x04 uint32_t oldOffset = writer->GetBaseAddress(); writer->Seek(baseStreamEnd, SeekOffsetType::Start); @@ -344,30 +342,31 @@ void ExporterExample_Room::Save(ZResource* res, fs::path outPath, BinaryWriter* break; case RoomCommand::EndMarker: { - writer->Write((uint8_t)0); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write((uint8_t)0); // 0x04 - writer->Write((uint8_t)0); // 0x05 - writer->Write((uint8_t)0); // 0x06 - writer->Write((uint8_t)0); // 0x07 + writer->Write((uint8_t)0); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)0); // 0x04 + writer->Write((uint8_t)0); // 0x05 + writer->Write((uint8_t)0); // 0x06 + writer->Write((uint8_t)0); // 0x07 } break; default: printf("UNIMPLEMENTED COMMAND: %i\n", (int)cmd->cmdID); - writer->Write((uint8_t)0); // 0x01 - writer->Write((uint8_t)0); // 0x02 - writer->Write((uint8_t)0); // 0x03 - writer->Write((uint8_t)0); // 0x04 - writer->Write((uint8_t)0); // 0x05 - writer->Write((uint8_t)0); // 0x06 - writer->Write((uint8_t)0); // 0x07 + writer->Write((uint8_t)0); // 0x01 + writer->Write((uint8_t)0); // 0x02 + writer->Write((uint8_t)0); // 0x03 + writer->Write((uint8_t)0); // 0x04 + writer->Write((uint8_t)0); // 0x05 + writer->Write((uint8_t)0); // 0x06 + writer->Write((uint8_t)0); // 0x07 break; } } - //writer->Close(); - //File::WriteAllBytes(StringHelper::Sprintf("%s", res->GetName().c_str()), memStream->ToVector()); + // writer->Close(); + // File::WriteAllBytes(StringHelper::Sprintf("%s", res->GetName().c_str()), + // memStream->ToVector()); } diff --git a/ExporterTest/TextureExporter.cpp b/ExporterTest/TextureExporter.cpp index 95d84b8..c236d12 100644 --- a/ExporterTest/TextureExporter.cpp +++ b/ExporterTest/TextureExporter.cpp @@ -1,7 +1,8 @@ #include "TextureExporter.h" #include "../ZAPD/ZFile.h" -void ExporterExample_Texture::Save(ZResource* res, [[maybe_unused]]fs::path outPath, BinaryWriter* writer) +void ExporterExample_Texture::Save(ZResource* res, [[maybe_unused]] fs::path outPath, + BinaryWriter* writer) { ZTexture* tex = (ZTexture*)res; diff --git a/ExporterTest/TextureExporter.h b/ExporterTest/TextureExporter.h index fcee8be..ffe6001 100644 --- a/ExporterTest/TextureExporter.h +++ b/ExporterTest/TextureExporter.h @@ -1,8 +1,8 @@ #pragma once +#include <Utils/BinaryWriter.h> #include "ZResource.h" #include "ZTexture.h" -#include <Utils/BinaryWriter.h> class ExporterExample_Texture : public ZResourceExporter { @@ -110,8 +110,10 @@ ZAPD also accepts the following list of extra parameters: - Can be used only in `e` or `bsf` modes. - `-tm MODE`: Test Mode (enables certain experimental features). To enable it, set `MODE` to `1`. - `-wno` / `--warn-no-offsets` : Enable warnings for nodes that dont have offsets specified. Takes priority over `-eno`/ `--error-no-offsets`. -- `-eno`/ `--error-no-offsets` : Enable errors for nodes that dont have offsets specified. +- `-eno` / `--error-no-offsets` : Enable errors for nodes that dont have offsets specified. - `-se` / `--set-exporter` : Sets which exporter to use. - `--gcc-compat` : Enables GCC compatible mode. Slower. +- `-s` / `--static` : Mark every asset as `static`. + - This behaviour can be overridden per asset using `Static=` in the respective XML node. 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/Declaration.cpp b/ZAPD/Declaration.cpp index dc4954e..230540e 100644 --- a/ZAPD/Declaration.cpp +++ b/ZAPD/Declaration.cpp @@ -1,5 +1,8 @@ #include "Declaration.h" +#include "Globals.h" +#include "Utils/StringHelper.h" + Declaration::Declaration(DeclarationAlignment nAlignment, size_t nSize, std::string nText) { alignment = nAlignment; @@ -54,3 +57,163 @@ Declaration::Declaration(std::string nIncludePath, size_t nSize, std::string nVa varType = nVarType; varName = nVarName; } + +bool Declaration::IsStatic() const +{ + switch (staticConf) + { + case StaticConfig::Off: + return false; + + case StaticConfig::Global: + return Globals::Instance->forceStatic; + + case StaticConfig::On: + return true; + } + + return false; +} + +std::string Declaration::GetNormalDeclarationStr() const +{ + std::string output; + + if (preText != "") + output += preText + "\n"; + + if (IsStatic()) + { + output += "static "; + } + + if (isArray) + { + if (arrayItemCntStr != "") + { + output += StringHelper::Sprintf("%s %s[%s];\n", varType.c_str(), varName.c_str(), + arrayItemCntStr.c_str()); + } + else if (arrayItemCnt == 0) + { + output += StringHelper::Sprintf("%s %s[] = {\n", varType.c_str(), varName.c_str()); + } + else + { + output += StringHelper::Sprintf("%s %s[%i] = {\n", varType.c_str(), varName.c_str(), + arrayItemCnt); + } + + output += text + "\n"; + } + else + { + output += StringHelper::Sprintf("%s %s = { ", varType.c_str(), varName.c_str()); + output += text; + } + + if (output.back() == '\n') + output += "};"; + else + output += " };"; + + if (rightText != "") + output += " " + rightText + ""; + + output += "\n"; + + if (postText != "") + output += postText + "\n"; + + output += "\n"; + + return output; +} + +std::string Declaration::GetExternalDeclarationStr() const +{ + std::string output; + + if (preText != "") + output += preText + "\n"; + + if (IsStatic()) + { + output += "static "; + } + + if (arrayItemCntStr != "") + output += + StringHelper::Sprintf("%s %s[%s] = {\n#include \"%s\"\n};", varType.c_str(), + varName.c_str(), arrayItemCntStr.c_str(), includePath.c_str()); + else if (arrayItemCnt != 0) + output += StringHelper::Sprintf("%s %s[%i] = {\n#include \"%s\"\n};", varType.c_str(), + varName.c_str(), arrayItemCnt, includePath.c_str()); + else + output += StringHelper::Sprintf("%s %s[] = {\n#include \"%s\"\n};", varType.c_str(), + varName.c_str(), includePath.c_str()); + + if (rightText != "") + output += " " + rightText + ""; + + output += "\n"; + + if (postText != "") + output += postText + "\n"; + + output += "\n"; + + return output; +} + +std::string Declaration::GetExternStr() const +{ + if (IsStatic() || varType == "") + { + return ""; + } + + if (isArray) + { + if (arrayItemCntStr != "") + { + return StringHelper::Sprintf("extern %s %s[%s];\n", varType.c_str(), varName.c_str(), + arrayItemCntStr.c_str()); + } + else if (arrayItemCnt != 0) + return StringHelper::Sprintf("extern %s %s[%i];\n", varType.c_str(), varName.c_str(), + arrayItemCnt); + else + return StringHelper::Sprintf("extern %s %s[];\n", varType.c_str(), varName.c_str()); + } + + return StringHelper::Sprintf("extern %s %s;\n", varType.c_str(), varName.c_str()); +} + +std::string Declaration::GetStaticForwardDeclarationStr() const +{ + if (!IsStatic() || isUnaccounted) + return ""; + + if (isArray) + { + if (arrayItemCntStr == "" && arrayItemCnt == 0) + { + // Forward declaring static arrays without specifying the size is not allowed. + return ""; + } + + if (arrayItemCntStr != "") + { + return StringHelper::Sprintf("static %s %s[%s];\n", varType.c_str(), varName.c_str(), + arrayItemCntStr.c_str()); + } + else + { + return StringHelper::Sprintf("static %s %s[%i];\n", varType.c_str(), varName.c_str(), + arrayItemCnt); + } + } + + return StringHelper::Sprintf("static %s %s;\n", varType.c_str(), varName.c_str()); +} diff --git a/ZAPD/Declaration.h b/ZAPD/Declaration.h index a835ce3..e1dff59 100644 --- a/ZAPD/Declaration.h +++ b/ZAPD/Declaration.h @@ -10,6 +10,13 @@ enum class DeclarationAlignment Align16 }; +enum class StaticConfig +{ + Off, + Global, + On +}; + class Declaration { public: @@ -31,6 +38,7 @@ public: std::vector<uint32_t> references; bool isUnaccounted = false; bool isPlaceholder = false; + StaticConfig staticConf = StaticConfig::Global; Declaration(DeclarationAlignment nAlignment, size_t nSize, std::string nVarType, std::string nVarName, bool nIsArray, std::string nText); @@ -44,6 +52,15 @@ public: bool nIsExternal); Declaration(std::string nIncludePath, size_t nSize, std::string nVarType, std::string nVarName); + bool IsStatic() const; + + std::string GetNormalDeclarationStr() const; + std::string GetExternalDeclarationStr() const; + + std::string GetExternStr() const; + + std::string GetStaticForwardDeclarationStr() const; + protected: Declaration(DeclarationAlignment nAlignment, size_t nSize, std::string nText); };
\ No newline at end of file diff --git a/ZAPD/Globals.h b/ZAPD/Globals.h index 60c79db..70cecbb 100644 --- a/ZAPD/Globals.h +++ b/ZAPD/Globals.h @@ -76,6 +76,7 @@ public: bool errorNoOffset = false; bool verboseUnaccounted = false; bool gccCompat = false; + bool forceStatic = false; std::vector<ZFile*> files; std::vector<int32_t> segments; diff --git a/ZAPD/Main.cpp b/ZAPD/Main.cpp index e7fc6fc..643f39d 100644 --- a/ZAPD/Main.cpp +++ b/ZAPD/Main.cpp @@ -218,6 +218,10 @@ int main(int argc, char* argv[]) { Globals::Instance->gccCompat = true; } + else if (arg == "-s" || arg == "--static") + { + Globals::Instance->forceStatic = true; + } } // Parse File Mode diff --git a/ZAPD/ZAnimation.cpp b/ZAPD/ZAnimation.cpp index 63a87ce..d6645d0 100644 --- a/ZAPD/ZAnimation.cpp +++ b/ZAPD/ZAnimation.cpp @@ -51,9 +51,10 @@ std::string ZNormalAnimation::GetSourceOutputCode([[maybe_unused]] const std::st headerStr += StringHelper::Sprintf("\t%sFrameData,\n", defaultPrefix.c_str()); headerStr += StringHelper::Sprintf("\t%sJointIndices,\n", defaultPrefix.c_str()); headerStr += StringHelper::Sprintf("\t%i\n", limit); - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), StringHelper::Sprintf("%s", name.c_str()), - headerStr); + Declaration* decl = parent->AddDeclaration( + rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), GetSourceTypeName(), + StringHelper::Sprintf("%s", name.c_str()), headerStr); + decl->staticConf = staticConf; std::string indicesStr = ""; std::string valuesStr = " "; @@ -79,12 +80,12 @@ std::string ZNormalAnimation::GetSourceOutputCode([[maybe_unused]] const std::st } parent->AddDeclarationArray(rotationValuesOffset, DeclarationAlignment::Align16, - rotationValues.size() * 2, "static s16", + rotationValues.size() * 2, "s16", StringHelper::Sprintf("%sFrameData", defaultPrefix.c_str()), rotationValues.size(), valuesStr); parent->AddDeclarationArray(rotationIndicesOffset, DeclarationAlignment::Align16, - rotationIndices.size() * 6, "static JointIndex", + rotationIndices.size() * 6, "JointIndex", StringHelper::Sprintf("%sJointIndices", defaultPrefix.c_str()), rotationIndices.size(), indicesStr); } @@ -287,8 +288,9 @@ void ZCurveAnimation::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRaw { ZResource::ExtractFromXML(reader, nRawDataIndex); - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align16, GetRawDataSize(), - GetSourceTypeName(), name, ""); + Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align16, + GetRawDataSize(), GetSourceTypeName(), name, ""); + decl->staticConf = staticConf; } void ZCurveAnimation::DeclareReferences(const std::string& prefix) @@ -447,13 +449,14 @@ std::string ZCurveAnimation::GetSourceOutputCode(const std::string& prefix) Declaration* decl = parent->GetDeclaration(address); if (decl == nullptr) { - parent->AddDeclaration(address, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, bodyStr); + decl = parent->AddDeclaration(address, DeclarationAlignment::Align4, GetRawDataSize(), + GetSourceTypeName(), name, bodyStr); } else { decl->text = bodyStr; } + decl->staticConf = staticConf; return ""; } @@ -473,8 +476,9 @@ void ZLegacyAnimation::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRa { ZAnimation::ExtractFromXML(reader, nRawDataIndex); - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, ""); + Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, + GetRawDataSize(), GetSourceTypeName(), name, ""); + decl->staticConf = staticConf; } void ZLegacyAnimation::ParseRawData() @@ -586,10 +590,11 @@ std::string ZLegacyAnimation::GetSourceOutputCode([[maybe_unused]] const std::st Declaration* decl = parent->GetDeclaration(rawDataIndex); if (decl == nullptr || decl->isPlaceholder) - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, body); + decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), + GetSourceTypeName(), name, body); else decl->text = body; + decl->staticConf = staticConf; return ""; } diff --git a/ZAPD/ZArray.cpp b/ZAPD/ZArray.cpp index 1eb28c0..f46ee8d 100644 --- a/ZAPD/ZArray.cpp +++ b/ZAPD/ZArray.cpp @@ -60,14 +60,16 @@ std::string ZArray::GetSourceOutputCode([[maybe_unused]] const std::string& pref for (size_t i = 0; i < arrayCnt; i++) { output += resList.at(i)->GetBodySourceCode(); + output += ","; if (i < arrayCnt - 1) - output += ",\n"; + output += "\n"; } - if (parent != nullptr) + Declaration* decl = parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - resList.at(0)->GetSourceTypeName(), name, arrayCnt, output); + resList.at(0)->GetSourceTypeName(), name, arrayCnt, output); + decl->staticConf = staticConf; return ""; } diff --git a/ZAPD/ZBackground.cpp b/ZAPD/ZBackground.cpp index ed2968c..0e15097 100644 --- a/ZAPD/ZBackground.cpp +++ b/ZAPD/ZBackground.cpp @@ -139,9 +139,10 @@ void ZBackground::DeclareVar(const std::string& prefix, const std::string& bodyS if (name == "") auxName = GetDefaultName(prefix, rawDataIndex); - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align8, GetRawDataSize(), - GetSourceTypeName(), auxName, "SCREEN_WIDTH * SCREEN_HEIGHT / 4", - bodyStr); + Declaration* decl = parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align8, + GetRawDataSize(), GetSourceTypeName(), auxName, + "SCREEN_WIDTH * SCREEN_HEIGHT / 4", bodyStr); + decl->staticConf = staticConf; } bool ZBackground::IsExternalResource() const diff --git a/ZAPD/ZCollision.cpp b/ZAPD/ZCollision.cpp index 6e8debb..17d173d 100644 --- a/ZAPD/ZCollision.cpp +++ b/ZAPD/ZCollision.cpp @@ -118,8 +118,8 @@ void ZCollisionHeader::ParseRawData() parent->AddDeclarationArray( polySegmentOffset, DeclarationAlignment::Align4, polygons.size() * 16, "CollisionPoly", - StringHelper::Sprintf("%s_polygons_%08X", name.c_str(), polySegmentOffset), 0, - declaration); + StringHelper::Sprintf("%s_polygons_%08X", name.c_str(), polySegmentOffset), + polygons.size(), declaration); } } @@ -137,8 +137,8 @@ void ZCollisionHeader::ParseRawData() parent->AddDeclarationArray( polyTypeDefSegmentOffset, DeclarationAlignment::Align4, polygonTypes.size() * 8, "SurfaceType", - StringHelper::Sprintf("%s_surfaceType_%08X", name.c_str(), polyTypeDefSegmentOffset), 0, - declaration); + StringHelper::Sprintf("%s_surfaceType_%08X", name.c_str(), polyTypeDefSegmentOffset), + polygonTypes.size(), declaration); declaration = ""; @@ -184,8 +184,10 @@ void ZCollisionHeader::ParseRawData() name.c_str(), polyTypeDefSegmentOffset, name.c_str(), camDataSegmentOffset, numWaterBoxes, waterBoxStr); - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, 44, GetSourceTypeName(), - name, declaration); + Declaration* decl = + parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), + GetSourceTypeName(), name, declaration); + decl->staticConf = staticConf; } std::string ZCollisionHeader::GetSourceTypeName() const diff --git a/ZAPD/ZCutscene.cpp b/ZAPD/ZCutscene.cpp index 4ccca8d..c8b0fc0 100644 --- a/ZAPD/ZCutscene.cpp +++ b/ZAPD/ZCutscene.cpp @@ -127,8 +127,10 @@ void ZCutscene::DeclareVar(const std::string& prefix, const std::string& bodyStr if (auxName == "") auxName = StringHelper::Sprintf("%sCutsceneData0x%06X", prefix.c_str(), rawDataIndex); - parent->AddDeclarationArray(getSegmentOffset(), DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), auxName, 0, bodyStr); + Declaration* decl = + parent->AddDeclarationArray(getSegmentOffset(), DeclarationAlignment::Align4, + GetRawDataSize(), GetSourceTypeName(), auxName, 0, bodyStr); + decl->staticConf = staticConf; } size_t ZCutscene::GetRawDataSize() const diff --git a/ZAPD/ZCutsceneMM.cpp b/ZAPD/ZCutsceneMM.cpp index 4d3c2af..0ada314 100644 --- a/ZAPD/ZCutsceneMM.cpp +++ b/ZAPD/ZCutsceneMM.cpp @@ -49,8 +49,10 @@ void ZCutsceneMM::DeclareVar(const std::string& prefix, const std::string& bodyS if (auxName == "") auxName = StringHelper::Sprintf("%sCutsceneData0x%06X", prefix.c_str(), rawDataIndex); - parent->AddDeclarationArray(getSegmentOffset(), DeclarationAlignment::Align4, GetRawDataSize(), - "s32", auxName, 0, bodyStr); + Declaration* decl = + parent->AddDeclarationArray(getSegmentOffset(), DeclarationAlignment::Align4, + GetRawDataSize(), "s32", auxName, 0, bodyStr); + decl->staticConf = staticConf; } size_t ZCutsceneMM::GetRawDataSize() const diff --git a/ZAPD/ZDisplayList.cpp b/ZAPD/ZDisplayList.cpp index d426e40..6831436 100644 --- a/ZAPD/ZDisplayList.cpp +++ b/ZAPD/ZDisplayList.cpp @@ -81,8 +81,9 @@ Declaration* ZDisplayList::DeclareVar([[maybe_unused]] const std::string& prefix { Declaration* decl = parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align8, GetRawDataSize(), - GetSourceTypeName(), name, 0, bodyStr); + GetSourceTypeName(), name, numInstructions, bodyStr); decl->isExternal = true; + decl->staticConf = staticConf; return decl; } @@ -1848,14 +1849,10 @@ std::string ZDisplayList::GetSourceOutputCode(const std::string& prefix) vtxDeclarations[item.first] = declaration; - if (parent != nullptr) - { - parent->AddDeclarationArray(item.first, DeclarationAlignment::Align16, - item.second.size() * 16, "static Vtx", - StringHelper::Sprintf("%sVtx_%06X", prefix.c_str(), - item.first, item.second.size()), - item.second.size(), declaration); - } + parent->AddDeclarationArray( + item.first, DeclarationAlignment::Align16, item.second.size() * 16, "Vtx", + StringHelper::Sprintf("%sVtx_%06X", prefix.c_str(), item.first, item.second.size()), + item.second.size(), declaration); } } @@ -1946,11 +1943,11 @@ std::string ZDisplayList::GetSourceOutputCode(const std::string& prefix) std::string incStr = StringHelper::Sprintf("%s.%s.inc", filepath.c_str(), "vtx"); parent->AddDeclarationArray(vtxKeys[i], DeclarationAlignment::Align16, - item.size() * 16, "static Vtx", vtxName, item.size(), + item.size() * 16, "Vtx", vtxName, item.size(), declaration); Declaration* vtxDecl = parent->AddDeclarationIncludeArray( - vtxKeys[i], incStr, item.size() * 16, "static Vtx", vtxName, item.size()); + vtxKeys[i], incStr, item.size() * 16, "Vtx", vtxName, item.size()); vtxDecl->isExternal = true; } } @@ -2120,7 +2117,7 @@ bool ZDisplayList::TextureGenCheck(ZFile* parent, [[maybe_unused]] std::string p tex->GetExternalExtension().c_str()); Globals::Instance->lastScene->parent->AddDeclarationIncludeArray( texAddr, filename, tex->GetRawDataSize(), tex->GetSourceTypeName(), - tex->GetName(), 0); + tex->GetName(), tex->GetRawDataSize() / 8); } return true; } 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<uint32_t, Declaration*> 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<uint32_t, Declaration*> 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"; diff --git a/ZAPD/ZLimb.cpp b/ZAPD/ZLimb.cpp index 9099ac1..048f070 100644 --- a/ZAPD/ZLimb.cpp +++ b/ZAPD/ZLimb.cpp @@ -370,8 +370,9 @@ void ZLimb::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) { ZResource::ExtractFromXML(reader, nRawDataIndex); - parent->AddDeclaration(GetFileAddress(), DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, ""); + Declaration* decl = parent->AddDeclaration(GetFileAddress(), DeclarationAlignment::Align4, + GetRawDataSize(), GetSourceTypeName(), name, ""); + decl->staticConf = staticConf; } void ZLimb::ParseXML(tinyxml2::XMLElement* reader) @@ -586,10 +587,11 @@ std::string ZLimb::GetSourceOutputCode(const std::string& prefix) Declaration* decl = parent->GetDeclaration(GetFileAddress()); if (decl == nullptr) - parent->AddDeclaration(GetFileAddress(), DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, entryStr); + decl = parent->AddDeclaration(GetFileAddress(), DeclarationAlignment::Align4, + GetRawDataSize(), GetSourceTypeName(), name, entryStr); else decl->text = entryStr; + decl->staticConf = staticConf; return ""; } diff --git a/ZAPD/ZMtx.cpp b/ZAPD/ZMtx.cpp index 87918b8..7febfaf 100644 --- a/ZAPD/ZMtx.cpp +++ b/ZAPD/ZMtx.cpp @@ -44,8 +44,10 @@ void ZMtx::DeclareVar(const std::string& prefix, const std::string& bodyStr) con if (name == "") auxName = GetDefaultName(prefix, rawDataIndex); - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align8, GetRawDataSize(), - GetSourceTypeName(), auxName, bodyStr); + Declaration* decl = + parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align8, GetRawDataSize(), + GetSourceTypeName(), auxName, bodyStr); + decl->staticConf = staticConf; } std::string ZMtx::GetBodySourceCode() const diff --git a/ZAPD/ZPath.cpp b/ZAPD/ZPath.cpp index 97bb733..b57b3cc 100644 --- a/ZAPD/ZPath.cpp +++ b/ZAPD/ZPath.cpp @@ -17,8 +17,10 @@ void ZPath::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) { ZResource::ExtractFromXML(reader, nRawDataIndex); - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, pathways.size() * 8, - GetSourceTypeName(), name, pathways.size(), ""); + Declaration* decl = + parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, pathways.size() * 8, + GetSourceTypeName(), name, pathways.size(), ""); + decl->staticConf = staticConf; } void ZPath::ParseXML(tinyxml2::XMLElement* reader) @@ -86,10 +88,12 @@ std::string ZPath::GetSourceOutputCode([[maybe_unused]] const std::string& prefi Declaration* decl = parent->GetDeclaration(rawDataIndex); if (decl == nullptr || decl->isPlaceholder) - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, pathways.size() * 8, - GetSourceTypeName(), name, pathways.size(), declaration); + decl = parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, + pathways.size() * 8, GetSourceTypeName(), name, + pathways.size(), declaration); else decl->text = declaration; + decl->staticConf = staticConf; return ""; } diff --git a/ZAPD/ZResource.cpp b/ZAPD/ZResource.cpp index a28204f..55861ef 100644 --- a/ZAPD/ZResource.cpp +++ b/ZAPD/ZResource.cpp @@ -19,6 +19,7 @@ ZResource::ZResource(ZFile* nParent) RegisterOptionalAttribute("OutName"); RegisterOptionalAttribute("Offset"); RegisterOptionalAttribute("Custom"); + RegisterOptionalAttribute("Static", "Global"); } void ZResource::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) @@ -110,6 +111,24 @@ void ZResource::ParseXML(tinyxml2::XMLElement* reader) isCustomAsset = registeredAttributes["Custom"].wasSet; + std::string& staticXml = registeredAttributes["Static"].value; + if (staticXml == "Global") + { + staticConf = StaticConfig::Global; + } + else if (staticXml == "On") + { + staticConf = StaticConfig::On; + } + else if (staticXml == "Off") + { + staticConf = StaticConfig::Off; + } + else + { + throw std::runtime_error("Invalid value for 'Static' attribute."); + } + declaredInXml = true; } } @@ -237,13 +256,14 @@ uint32_t Seg2Filespace(segptr_t segmentedAddress, uint32_t parentBaseAddress) if (GETSEGNUM(segmentedAddress) == 0x80) // Is defined in code? { uint32_t parentBaseOffset = GETSEGOFFSET(parentBaseAddress); - if (parentBaseOffset > currentPtr) + if (parentBaseOffset > currentPtr) { - throw std::runtime_error(StringHelper::Sprintf( - "\nSeg2Filespace: Segmented address is smaller than 'BaseAddress'. Maybe your 'BaseAddress' is wrong?\n" - "\t SegmentedAddress: 0x%08X\n" - "\t BaseAddress: 0x%08X\n", - segmentedAddress, parentBaseAddress)); + throw std::runtime_error( + StringHelper::Sprintf("\nSeg2Filespace: Segmented address is smaller than " + "'BaseAddress'. Maybe your 'BaseAddress' is wrong?\n" + "\t SegmentedAddress: 0x%08X\n" + "\t BaseAddress: 0x%08X\n", + segmentedAddress, parentBaseAddress)); } currentPtr -= parentBaseOffset; } diff --git a/ZAPD/ZResource.h b/ZAPD/ZResource.h index 71d91ee..46306b0 100644 --- a/ZAPD/ZResource.h +++ b/ZAPD/ZResource.h @@ -118,6 +118,7 @@ protected: bool isCustomAsset; // If set to true, create a reference for the asset in the file, but don't // actually try to extract it from the file bool declaredInXml = false; + StaticConfig staticConf = StaticConfig::Global; // Reading from this XMLs attributes should be performed in the overrided `ParseXML` method. std::map<std::string, ResourceAttribute> registeredAttributes; diff --git a/ZAPD/ZRoom/Commands/SetMesh.cpp b/ZAPD/ZRoom/Commands/SetMesh.cpp index c334082..024f054 100644 --- a/ZAPD/ZRoom/Commands/SetMesh.cpp +++ b/ZAPD/ZRoom/Commands/SetMesh.cpp @@ -68,7 +68,7 @@ void GenDListDeclarations(ZRoom* zRoom, ZFile* parent, ZDisplayList* dList) { parent->AddDeclarationArray( vtxEntry.first, DeclarationAlignment::Align16, - dList->vertices[vtxEntry.first].size() * 16, "static Vtx", + dList->vertices[vtxEntry.first].size() * 16, "Vtx", StringHelper::Sprintf("%sVtx_%06X", zRoom->GetName().c_str(), vtxEntry.first), dList->vertices[vtxEntry.first].size(), vtxEntry.second); } @@ -646,8 +646,10 @@ void PolygonType2::DeclareReferences(const std::string& prefix) polyDlistType, polyDListName, polyDLists.size(), declaration); } - parent->AddDeclaration(GETSEGOFFSET(end), DeclarationAlignment::Align4, 4, "static s32", - "terminatorMaybe", "0x01000000"); + parent->AddDeclaration(GETSEGOFFSET(end), DeclarationAlignment::Align4, 4, "s32", + StringHelper::Sprintf("%s_terminatorMaybe_%06X", + parent->GetName().c_str(), GETSEGOFFSET(end)), + "0x01000000"); } std::string PolygonType2::GetBodySourceCode() const diff --git a/ZAPD/ZRoom/ZRoom.cpp b/ZAPD/ZRoom/ZRoom.cpp index 0ef3724..0af2670 100644 --- a/ZAPD/ZRoom/ZRoom.cpp +++ b/ZAPD/ZRoom/ZRoom.cpp @@ -301,8 +301,10 @@ void ZRoom::DeclareVar(const std::string& prefix, const std::string body) if (zroomType == ZResourceType::Scene || zroomType == ZResourceType::Room) auxName = StringHelper::Sprintf("%sCommands", name.c_str()); - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), auxName, 0, body); + Declaration* decl = + parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), + GetSourceTypeName(), auxName, 0, body); + decl->staticConf = staticConf; } std::string ZRoom::GetBodySourceCode() const diff --git a/ZAPD/ZScalar.cpp b/ZAPD/ZScalar.cpp index 9d68f39..b4c0f13 100644 --- a/ZAPD/ZScalar.cpp +++ b/ZAPD/ZScalar.cpp @@ -249,9 +249,10 @@ std::string ZScalar::GetBodySourceCode() const std::string ZScalar::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) { - if (parent != nullptr) + Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), GetName(), GetBodySourceCode()); + GetSourceTypeName(), GetName(), GetBodySourceCode()); + decl->staticConf = staticConf; return ""; } diff --git a/ZAPD/ZSkeleton.cpp b/ZAPD/ZSkeleton.cpp index 6912f2e..5de5626 100644 --- a/ZAPD/ZSkeleton.cpp +++ b/ZAPD/ZSkeleton.cpp @@ -17,8 +17,9 @@ void ZSkeleton::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIn { ZResource::ExtractFromXML(reader, nRawDataIndex); - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, ""); + Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, + GetRawDataSize(), GetSourceTypeName(), name, ""); + decl->staticConf = staticConf; } void ZSkeleton::ParseXML(tinyxml2::XMLElement* reader) @@ -129,13 +130,14 @@ std::string ZSkeleton::GetSourceOutputCode([[maybe_unused]] const std::string& p if (decl == nullptr) { - parent->AddDeclaration(GetAddress(), DeclarationAlignment::Align16, GetRawDataSize(), - GetSourceTypeName(), name, headerStr); + decl = parent->AddDeclaration(GetAddress(), DeclarationAlignment::Align16, GetRawDataSize(), + GetSourceTypeName(), name, headerStr); } else { decl->text = headerStr; } + decl->staticConf = staticConf; return ""; } @@ -182,8 +184,10 @@ void ZLimbTable::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataI { ZResource::ExtractFromXML(reader, nRawDataIndex); - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, limbsAddresses.size(), ""); + Declaration* decl = + parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), + GetSourceTypeName(), name, limbsAddresses.size(), ""); + decl->staticConf = staticConf; } void ZLimbTable::ExtractFromBinary(uint32_t nRawDataIndex, ZLimbType nLimbType, size_t nCount) @@ -275,10 +279,12 @@ std::string ZLimbTable::GetSourceOutputCode([[maybe_unused]] const std::string& Declaration* decl = parent->GetDeclaration(rawDataIndex); if (decl == nullptr || decl->isPlaceholder) - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, limbsAddresses.size(), body); + decl = parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, + GetRawDataSize(), GetSourceTypeName(), name, + limbsAddresses.size(), body); else decl->text = body; + decl->staticConf = staticConf; return ""; } diff --git a/ZAPD/ZString.cpp b/ZAPD/ZString.cpp index cfea21a..502bb54 100644 --- a/ZAPD/ZString.cpp +++ b/ZAPD/ZString.cpp @@ -36,8 +36,10 @@ std::string ZString::GetBodySourceCode() const std::string ZString::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) { - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, 0, GetBodySourceCode()); + Declaration* decl = + parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), + GetSourceTypeName(), name, 0, GetBodySourceCode()); + decl->staticConf = staticConf; return ""; } diff --git a/ZAPD/ZSymbol.cpp b/ZAPD/ZSymbol.cpp index 278c80d..3a21cbe 100644 --- a/ZAPD/ZSymbol.cpp +++ b/ZAPD/ZSymbol.cpp @@ -54,6 +54,12 @@ void ZSymbol::ParseXML(tinyxml2::XMLElement* reader) if (countXml != "") count = StringHelper::StrToL(countXml, 0); } + + if (registeredAttributes.at("Static").value == "On") + { + fprintf(stderr, "A <Symbol> can't be marked as static.\n\t Disabling static\n"); + } + staticConf = StaticConfig::Off; } size_t ZSymbol::GetRawDataSize() const diff --git a/ZAPD/ZTexture.cpp b/ZAPD/ZTexture.cpp index 45a6a7c..6a929c4 100644 --- a/ZAPD/ZTexture.cpp +++ b/ZAPD/ZTexture.cpp @@ -30,8 +30,9 @@ void ZTexture::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataInd std::string incStr = StringHelper::Sprintf("%s.%s.inc.c", filepath.c_str(), GetExternalExtension().c_str()); - parent->AddDeclarationIncludeArray(rawDataIndex, incStr, GetRawDataSize(), GetSourceTypeName(), - name, 0); + Declaration* decl = parent->AddDeclarationIncludeArray( + rawDataIndex, incStr, GetRawDataSize(), GetSourceTypeName(), name, GetRawDataSize() / 8); + decl->staticConf = staticConf; } void ZTexture::FromBinary(uint32_t nRawDataIndex, int32_t nWidth, int32_t nHeight, diff --git a/ZAPD/ZTextureAnimation.cpp b/ZAPD/ZTextureAnimation.cpp index e8236f1..deee4e3 100644 --- a/ZAPD/ZTextureAnimation.cpp +++ b/ZAPD/ZTextureAnimation.cpp @@ -695,8 +695,10 @@ void ZTextureAnimation::DeclareVar(const std::string& prefix, const std::string& if (name == "") auxName = GetDefaultName(prefix); - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), auxName, entries.size(), bodyStr); + Declaration* decl = + parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), + GetSourceTypeName(), auxName, entries.size(), bodyStr); + decl->staticConf = staticConf; } std::string ZTextureAnimation::GetBodySourceCode() const diff --git a/ZAPD/ZVector.cpp b/ZAPD/ZVector.cpp index 69171a8..31d5dc4 100644 --- a/ZAPD/ZVector.cpp +++ b/ZAPD/ZVector.cpp @@ -94,9 +94,10 @@ std::string ZVector::GetBodySourceCode() const std::string ZVector::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) { - if (parent != nullptr) + Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), GetName(), GetBodySourceCode()); + GetSourceTypeName(), GetName(), GetBodySourceCode()); + decl->staticConf = staticConf; return ""; } diff --git a/ZAPD/ZVtx.cpp b/ZAPD/ZVtx.cpp index 06762bb..54420f0 100644 --- a/ZAPD/ZVtx.cpp +++ b/ZAPD/ZVtx.cpp @@ -38,21 +38,18 @@ void ZVtx::ParseRawData() std::string ZVtx::GetBodySourceCode() const { - return StringHelper::Sprintf(" VTX(%i, %i, %i, %i, %i, %i, %i, %i, %i)", x, y, z, s, t, r, g, b, - a); + return StringHelper::Sprintf(" VTX(%i, %i, %i, %i, %i, %i, %i, %i, %i)", x, y, z, s, t, r, g, + b, a); } std::string ZVtx::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) { std::string output = GetBodySourceCode(); - if (parent != nullptr) - { - Declaration* decl = - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align16, GetRawDataSize(), - GetSourceTypeName(), name, output); - decl->isExternal = true; - } + Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align16, + GetRawDataSize(), GetSourceTypeName(), name, output); + decl->isExternal = true; + decl->staticConf = staticConf; return ""; } diff --git a/ZAPDUtils/Color3b.h b/ZAPDUtils/Color3b.h index d241297..7e59f6b 100644 --- a/ZAPDUtils/Color3b.h +++ b/ZAPDUtils/Color3b.h @@ -6,6 +6,16 @@ struct Color3b { uint8_t r, g, b; - Color3b() { r = 0; g = 0; b = 0; }; - Color3b(uint8_t nR, uint8_t nG, uint8_t nB) { r = nR; g = nG; b = nB; }; + Color3b() + { + r = 0; + g = 0; + b = 0; + }; + Color3b(uint8_t nR, uint8_t nG, uint8_t nB) + { + r = nR; + g = nG; + b = nB; + }; };
\ No newline at end of file diff --git a/ZAPDUtils/StrHash.h b/ZAPDUtils/StrHash.h index 858c517..68d22b9 100644 --- a/ZAPDUtils/StrHash.h +++ b/ZAPDUtils/StrHash.h @@ -6,8 +6,7 @@ typedef uint32_t strhash; -[[maybe_unused]] -static strhash CRC32B(unsigned char* message, int32_t size) +[[maybe_unused]] static strhash CRC32B(unsigned char* message, int32_t size) { int32_t byte = 0, crc = 0; int32_t mask = 0; @@ -29,8 +28,7 @@ static strhash CRC32B(unsigned char* message, int32_t size) return ~(uint32_t)(crc); } -[[maybe_unused]] -constexpr static strhash CRC32BCT(const char* message, int32_t size) +[[maybe_unused]] constexpr static strhash CRC32BCT(const char* message, int32_t size) { int32_t byte = 0, crc = 0; int32_t mask = 0; diff --git a/ZAPDUtils/Utils/BinaryReader.cpp b/ZAPDUtils/Utils/BinaryReader.cpp index b051888..374ebe4 100644 --- a/ZAPDUtils/Utils/BinaryReader.cpp +++ b/ZAPDUtils/Utils/BinaryReader.cpp @@ -1,7 +1,7 @@ #include "BinaryReader.h" -#include "Stream.h" #include <math.h> #include <stdexcept> +#include "Stream.h" BinaryReader::BinaryReader(Stream* nStream) { @@ -23,7 +23,7 @@ uint32_t BinaryReader::GetBaseAddress() return stream->GetBaseAddress(); } -void BinaryReader::Read([[maybe_unused]]char* buffer, int32_t length) +void BinaryReader::Read([[maybe_unused]] char* buffer, int32_t length) { stream->Read(length); } @@ -135,7 +135,7 @@ std::string BinaryReader::ReadString() { std::string res = ""; char c; - + do { c = ReadChar(); @@ -144,6 +144,5 @@ std::string BinaryReader::ReadString() res += c; } while (c != 0); - return res; }
\ No newline at end of file diff --git a/ZAPDUtils/Utils/BinaryReader.h b/ZAPDUtils/Utils/BinaryReader.h index 9e28718..cf0eeb4 100644 --- a/ZAPDUtils/Utils/BinaryReader.h +++ b/ZAPDUtils/Utils/BinaryReader.h @@ -1,14 +1,14 @@ #pragma once -#include "Stream.h" +#include <array> +#include <memory> +#include <string> +#include <vector> +#include "../Color3b.h" #include "../Vec2f.h" #include "../Vec3f.h" #include "../Vec3s.h" -#include "../Color3b.h" -#include <memory> -#include <array> -#include <vector> -#include <string> +#include "Stream.h" class BinaryReader { @@ -16,7 +16,7 @@ public: BinaryReader(Stream* nStream); void Close(); - + void Seek(uint32_t offset, SeekOffsetType seekType); uint32_t GetBaseAddress(); @@ -37,7 +37,7 @@ public: Vec2f ReadVec2f(); Color3b ReadColor3b(); std::string ReadString(); - + protected: std::shared_ptr<Stream> stream; };
\ No newline at end of file diff --git a/ZAPDUtils/Vec2f.h b/ZAPDUtils/Vec2f.h index e9c5059..9d4beeb 100644 --- a/ZAPDUtils/Vec2f.h +++ b/ZAPDUtils/Vec2f.h @@ -6,6 +6,14 @@ struct Vec2f { float x, y; - Vec2f() { x = 0; y = 0; }; - Vec2f(float nX, float nY) { x = nX; y = nY; }; + Vec2f() + { + x = 0; + y = 0; + }; + Vec2f(float nX, float nY) + { + x = nX; + y = nY; + }; };
\ No newline at end of file diff --git a/ZAPDUtils/Vec3f.h b/ZAPDUtils/Vec3f.h index cde5202..4bfbb3c 100644 --- a/ZAPDUtils/Vec3f.h +++ b/ZAPDUtils/Vec3f.h @@ -6,6 +6,16 @@ struct Vec3f { float x, y, z; - Vec3f() { x = 0; y = 0; z = 0; }; - Vec3f(float nX, float nY, float nZ) { x = nX; y = nY; z = nZ; }; + Vec3f() + { + x = 0; + y = 0; + z = 0; + }; + Vec3f(float nX, float nY, float nZ) + { + x = nX; + y = nY; + z = nZ; + }; };
\ No newline at end of file diff --git a/docs/zapd_extraction_xml_reference.md b/docs/zapd_extraction_xml_reference.md index d07ef98..03fb4f1 100644 --- a/docs/zapd_extraction_xml_reference.md +++ b/docs/zapd_extraction_xml_reference.md @@ -69,6 +69,20 @@ For most resources inside a `<File>` tag **you should also set an `Offset` attri It's worth noting that every tag expects a `Name="gNameOfTheAsset"`. This is will be the name of the extracted variable in the output C code. Every asset must be prefixed with `g` and the suffix should represent the type of the variable. +Every tag can accept a `Static` attribute to specify if the asset should be marked as `static` or not. +There are 3 valid values (defaults to `Global`): + +- `Global`: Mark static if the flag `--static` was used. +- `On`: Override the global config and **always mark** as `static`. +- `Off`: Override the global config and **don't mark** as `static`. + +This table summarizes if the asset will be marked `static` (✅) or not (❌) +| `Static=""` attribute in XML | Without `--static` flag | With `--static` flag | +| ---------------------------- | ----------------------- | -------------------- | +| `On` | ✅ | ✅ | +| `Global` (default) | ❌ | ✅ | +| `Off` | ❌ | ❌ | + ------------------------- ### File @@ -402,6 +416,7 @@ extern u8 gJsjutanShadowTex[2048]; - `Type`: The type of the declared variable. If missing, it will default to `void*`. - `TypeSize`: The size in bytes of the type. If missing, it will default to `4` (the size of a word and a pointer). Integer or hex value. - `Count`: Optional. If it is present, the variable will be declared as an array instead of a plain variable. The value of this attribute specifies the length of the array. If `Count` is present but it has no value (`Count=""`), then the length of the array will not be specified either in the declared variable. Integer or hex value. + - `Static`: This attribute can't be enabled on a Symbol node. A warning will be showed in this case. ------------------------- @@ -570,8 +585,6 @@ Currently, only [`Scalar`](#scalar), [`Vector`](#vector) and [`Vtx`](#vtx) suppo ------------------------- -------------------------- - ### Path - Example: |
