summaryrefslogtreecommitdiff
path: root/ZAPD
diff options
context:
space:
mode:
authorNicholas Estelami <NEstelami@users.noreply.github.com>2023-05-30 22:07:11 -0400
committerGitHub <noreply@github.com>2023-05-30 22:07:11 -0400
commitbd87285d719e738a8baee8f11d4d6ffce0ab24a0 (patch)
tree1e4956187985e860261c364eca86d9bd0710e19c /ZAPD
parent55bb75eaa6ebf95858931e1557ed90105a150d80 (diff)
Some cleanup and refactoring (#295)
* Cleaned up Main and improved documentation * Moved ZRoom::GetDeclarationSizeFromNeighbor into ZFile * Fixed compile issue
Diffstat (limited to 'ZAPD')
-rw-r--r--ZAPD/Main.cpp124
-rw-r--r--ZAPD/ZArray.cpp2
-rw-r--r--ZAPD/ZDisplayList.cpp2
-rw-r--r--ZAPD/ZDisplayList.h2
-rw-r--r--ZAPD/ZFile.cpp21
-rw-r--r--ZAPD/ZFile.h1
-rw-r--r--ZAPD/ZResource.cpp2
-rw-r--r--ZAPD/ZResource.h14
-rw-r--r--ZAPD/ZRoom/Commands/SetAlternateHeaders.cpp2
-rw-r--r--ZAPD/ZRoom/Commands/SetEntranceList.cpp2
-rw-r--r--ZAPD/ZRoom/Commands/SetExitList.cpp2
-rw-r--r--ZAPD/ZRoom/Commands/SetPathways.cpp2
-rw-r--r--ZAPD/ZRoom/ZRoom.cpp18
-rw-r--r--ZAPD/ZRoom/ZRoom.h3
14 files changed, 104 insertions, 93 deletions
diff --git a/ZAPD/Main.cpp b/ZAPD/Main.cpp
index 6a7e6c1..28b280f 100644
--- a/ZAPD/Main.cpp
+++ b/ZAPD/Main.cpp
@@ -48,12 +48,14 @@ void ParseArgs(int& argc, char* argv[]);
void BuildAssetTexture(const fs::path& pngFilePath, TextureType texType, const fs::path& outPath);
void BuildAssetBackground(const fs::path& imageFilePath, const fs::path& outPath);
void BuildAssetBlob(const fs::path& blobFilePath, const fs::path& outPath);
+ZFileMode ParseFileMode(const std::string& buildMode, ExporterSet* exporterSet);
+int HandleExtract(ZFileMode fileMode, ExporterSet* exporterSet);
extern const char gBuildHash[];
int main(int argc, char* argv[])
{
- // Syntax: ZAPD.out [mode (btex/bovl/e)] (Arbritrary Number of Arguments)
+ int returnCode = 0;
if (argc < 2)
{
@@ -87,20 +89,7 @@ int main(int argc, char* argv[])
// Parse File Mode
ExporterSet* exporterSet = Globals::Instance->GetExporterSet();
std::string buildMode = argv[1];
- ZFileMode fileMode = ZFileMode::Invalid;
-
- if (buildMode == "btex")
- fileMode = ZFileMode::BuildTexture;
- else if (buildMode == "bren")
- fileMode = ZFileMode::BuildBackground;
- else if (buildMode == "bsf")
- fileMode = ZFileMode::BuildSourceFile;
- else if (buildMode == "bblb")
- fileMode = ZFileMode::BuildBlob;
- else if (buildMode == "e")
- fileMode = ZFileMode::Extract;
- else if (exporterSet != nullptr && exporterSet->parseFileModeFunc != nullptr)
- exporterSet->parseFileModeFunc(buildMode, fileMode);
+ ZFileMode fileMode = ParseFileMode(buildMode, exporterSet);
if (fileMode == ZFileMode::Invalid)
{
@@ -110,7 +99,6 @@ int main(int argc, char* argv[])
// We've parsed through our commands once. If an exporter exists, it's been set by now.
// Now we'll parse through them again but pass them on to our exporter if one is available.
-
if (exporterSet != nullptr && exporterSet->parseArgsFunc != nullptr)
{
for (int32_t i = 2; i < argc; i++)
@@ -123,58 +111,18 @@ int main(int argc, char* argv[])
if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_DEBUG)
WarningHandler::PrintWarningsDebugInfo();
- // TODO: switch
if (fileMode == ZFileMode::Extract || fileMode == ZFileMode::BuildSourceFile)
- {
- bool procFileModeSuccess = false;
-
- if (exporterSet != nullptr && exporterSet->processFileModeFunc != nullptr)
- procFileModeSuccess = exporterSet->processFileModeFunc(fileMode);
-
- if (!procFileModeSuccess)
- {
- bool parseSuccessful;
-
- for (auto& extFile : Globals::Instance->cfg.externalFiles)
- {
- fs::path externalXmlFilePath =
- Globals::Instance->cfg.externalXmlFolder / extFile.xmlPath;
-
- if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_INFO)
- {
- printf("Parsing external file from config: '%s'\n",
- externalXmlFilePath.c_str());
- }
-
- parseSuccessful = Parse(externalXmlFilePath, Globals::Instance->baseRomPath,
- extFile.outPath, ZFileMode::ExternalFile);
-
- if (!parseSuccessful)
- return 1;
- }
-
- parseSuccessful = Parse(Globals::Instance->inputPath, Globals::Instance->baseRomPath,
- Globals::Instance->outputPath, fileMode);
- if (!parseSuccessful)
- return 1;
- }
- }
+ returnCode = HandleExtract(fileMode, exporterSet);
else if (fileMode == ZFileMode::BuildTexture)
- {
- TextureType texType = Globals::Instance->texType;
- BuildAssetTexture(Globals::Instance->inputPath, texType, Globals::Instance->outputPath);
- }
+ BuildAssetTexture(Globals::Instance->inputPath, Globals::Instance->texType,
+ Globals::Instance->outputPath);
else if (fileMode == ZFileMode::BuildBackground)
- {
BuildAssetBackground(Globals::Instance->inputPath, Globals::Instance->outputPath);
- }
else if (fileMode == ZFileMode::BuildBlob)
- {
BuildAssetBlob(Globals::Instance->inputPath, Globals::Instance->outputPath);
- }
delete g;
- return 0;
+ return returnCode;
}
bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path& outPath,
@@ -322,6 +270,27 @@ void ParseArgs(int& argc, char* argv[])
}
}
+
+ZFileMode ParseFileMode(const std::string& buildMode, ExporterSet* exporterSet)
+{
+ ZFileMode fileMode = ZFileMode::Invalid;
+
+ if (buildMode == "btex")
+ fileMode = ZFileMode::BuildTexture;
+ else if (buildMode == "bren")
+ fileMode = ZFileMode::BuildBackground;
+ else if (buildMode == "bsf")
+ fileMode = ZFileMode::BuildSourceFile;
+ else if (buildMode == "bblb")
+ fileMode = ZFileMode::BuildBlob;
+ else if (buildMode == "e")
+ fileMode = ZFileMode::Extract;
+ else if (exporterSet != nullptr && exporterSet->parseFileModeFunc != nullptr)
+ exporterSet->parseFileModeFunc(buildMode, fileMode);
+
+ return fileMode;
+}
+
void Arg_SetOutputPath(int& i, [[maybe_unused]] char* argv[])
{
Globals::Instance->outputPath = argv[++i];
@@ -419,6 +388,41 @@ void Arg_ForceUnaccountedStatic([[maybe_unused]] int& i, [[maybe_unused]] char*
Globals::Instance->forceUnaccountedStatic = true;
}
+int HandleExtract(ZFileMode fileMode, ExporterSet* exporterSet)
+{
+ bool procFileModeSuccess = false;
+
+ if (exporterSet != nullptr && exporterSet->processFileModeFunc != nullptr)
+ procFileModeSuccess = exporterSet->processFileModeFunc(fileMode);
+
+ if (!procFileModeSuccess)
+ {
+ bool parseSuccessful;
+
+ for (auto& extFile : Globals::Instance->cfg.externalFiles)
+ {
+ fs::path externalXmlFilePath =
+ Globals::Instance->cfg.externalXmlFolder / extFile.xmlPath;
+
+ if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_INFO)
+ printf("Parsing external file from config: '%s'\n", externalXmlFilePath.c_str());
+
+ parseSuccessful = Parse(externalXmlFilePath, Globals::Instance->baseRomPath,
+ extFile.outPath, ZFileMode::ExternalFile);
+
+ if (!parseSuccessful)
+ return 1;
+ }
+
+ parseSuccessful = Parse(Globals::Instance->inputPath, Globals::Instance->baseRomPath,
+ Globals::Instance->outputPath, fileMode);
+ if (!parseSuccessful)
+ return 1;
+ }
+
+ return 0;
+}
+
void BuildAssetTexture(const fs::path& pngFilePath, TextureType texType, const fs::path& outPath)
{
std::string name = outPath.stem().string();
diff --git a/ZAPD/ZArray.cpp b/ZAPD/ZArray.cpp
index afca71a..62720bc 100644
--- a/ZAPD/ZArray.cpp
+++ b/ZAPD/ZArray.cpp
@@ -56,7 +56,7 @@ void ZArray::ParseXML(tinyxml2::XMLElement* reader)
}
res->parent = parent;
res->SetInnerNode(true);
- res->ExtractFromXML(child, childIndex);
+ res->ExtractWithXML(child, childIndex);
childIndex += res->GetRawDataSize();
resList.push_back(res);
diff --git a/ZAPD/ZDisplayList.cpp b/ZAPD/ZDisplayList.cpp
index d4cad7b..3d34370 100644
--- a/ZAPD/ZDisplayList.cpp
+++ b/ZAPD/ZDisplayList.cpp
@@ -40,7 +40,7 @@ ZDisplayList::~ZDisplayList()
}
// EXTRACT MODE
-void ZDisplayList::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex)
+void ZDisplayList::ExtractWithXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex)
{
rawDataIndex = nRawDataIndex;
ParseXML(reader);
diff --git a/ZAPD/ZDisplayList.h b/ZAPD/ZDisplayList.h
index c68713c..356e89a 100644
--- a/ZAPD/ZDisplayList.h
+++ b/ZAPD/ZDisplayList.h
@@ -346,7 +346,7 @@ public:
ZDisplayList(ZFile* nParent);
~ZDisplayList();
- void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override;
+ void ExtractWithXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override;
void ExtractFromBinary(uint32_t nRawDataIndex, int32_t rawDataSize);
void ParseRawData() override;
diff --git a/ZAPD/ZFile.cpp b/ZAPD/ZFile.cpp
index 5087a24..8501509 100644
--- a/ZAPD/ZFile.cpp
+++ b/ZAPD/ZFile.cpp
@@ -272,7 +272,7 @@ void ZFile::ParseXML(tinyxml2::XMLElement* reader, const std::string& filename)
ZResource* nRes = nodeMap[nodeName](this);
if (mode == ZFileMode::Extract || mode == ZFileMode::ExternalFile)
- nRes->ExtractFromXML(child, rawDataIndex);
+ nRes->ExtractWithXML(child, rawDataIndex);
switch (nRes->GetResourceType())
{
@@ -772,6 +772,21 @@ bool ZFile::HasDeclaration(offset_t address)
return declarations.find(address) != declarations.end();
}
+
+size_t ZFile::GetDeclarationSizeFromNeighbor(uint32_t declarationAddress)
+{
+ auto currentDecl = declarations.find(declarationAddress);
+ if (currentDecl == declarations.end())
+ return 0;
+
+ auto nextDecl = currentDecl;
+ std::advance(nextDecl, 1);
+ if (nextDecl == declarations.end())
+ return GetRawData().size() - currentDecl->first;
+
+ return nextDecl->first - currentDecl->first;
+}
+
void ZFile::GenerateSourceFiles()
{
std::string sourceOutput;
@@ -887,13 +902,9 @@ std::string ZFile::GetExternalFileHeaderInclude() const
{
fs::path outputFolderPath = externalFile->GetSourceOutputFolderPath();
if (outputFolderPath == this->GetSourceOutputFolderPath())
- {
outputFolderPath = externalFile->outName.stem();
- }
else
- {
outputFolderPath /= externalFile->outName.stem();
- }
externalFilesIncludes +=
StringHelper::Sprintf("#include \"%s.h\"\n", outputFolderPath.string().c_str());
diff --git a/ZAPD/ZFile.h b/ZAPD/ZFile.h
index d529c1c..65b1cc4 100644
--- a/ZAPD/ZFile.h
+++ b/ZAPD/ZFile.h
@@ -89,6 +89,7 @@ public:
Declaration* GetDeclaration(offset_t address) const;
Declaration* GetDeclarationRanged(offset_t address) const;
bool HasDeclaration(offset_t address);
+ size_t GetDeclarationSizeFromNeighbor(uint32_t declarationAddress);
std::string GetHeaderInclude() const;
std::string GetZRoomHeaderInclude() const;
diff --git a/ZAPD/ZResource.cpp b/ZAPD/ZResource.cpp
index 3aedec9..d991c46 100644
--- a/ZAPD/ZResource.cpp
+++ b/ZAPD/ZResource.cpp
@@ -24,7 +24,7 @@ ZResource::ZResource(ZFile* nParent)
RegisterOptionalAttribute("Static", "Global");
}
-void ZResource::ExtractFromXML(tinyxml2::XMLElement* reader, offset_t nRawDataIndex)
+void ZResource::ExtractWithXML(tinyxml2::XMLElement* reader, offset_t nRawDataIndex)
{
rawDataIndex = nRawDataIndex;
declaredInXml = true;
diff --git a/ZAPD/ZResource.h b/ZAPD/ZResource.h
index 7c271a6..171dcdc 100644
--- a/ZAPD/ZResource.h
+++ b/ZAPD/ZResource.h
@@ -80,8 +80,18 @@ public:
ZResource(ZFile* nParent);
virtual ~ZResource() = default;
- // Parsing from File
- virtual void ExtractFromXML(tinyxml2::XMLElement* reader, offset_t nRawDataIndex);
+
+ /// <summary>
+ /// Extracts/Parsees data from binary file using an XML to provide the needed metadata.
+ /// </summary>
+ /// <param name="reader">XML Node we wish to parse from.</param>
+ /// <param name="nRawDataIndex">The offset within the binary file we are going to parse from as indicated by the "Offset" parameter in the XML.</param>
+ virtual void ExtractWithXML(tinyxml2::XMLElement* reader, offset_t nRawDataIndex);
+
+ /// <summary>
+ /// Extracts/Parses the needed data straight from a binary without the use of an XML.
+ /// </summary>
+ /// <param name="nRawDataIndex">The offset within the binary file we wish to parse from.</param>
virtual void ExtractFromFile(offset_t nRawDataIndex);
// Misc
diff --git a/ZAPD/ZRoom/Commands/SetAlternateHeaders.cpp b/ZAPD/ZRoom/Commands/SetAlternateHeaders.cpp
index 1687b54..e64b855 100644
--- a/ZAPD/ZRoom/Commands/SetAlternateHeaders.cpp
+++ b/ZAPD/ZRoom/Commands/SetAlternateHeaders.cpp
@@ -21,7 +21,7 @@ void SetAlternateHeaders::DeclareReferences([[maybe_unused]] const std::string&
void SetAlternateHeaders::ParseRawDataLate()
{
- size_t numHeaders = zRoom->GetDeclarationSizeFromNeighbor(segmentOffset) / 4;
+ size_t numHeaders = zRoom->parent->GetDeclarationSizeFromNeighbor(segmentOffset) / 4;
headers.reserve(numHeaders);
for (uint32_t i = 0; i < numHeaders; i++)
diff --git a/ZAPD/ZRoom/Commands/SetEntranceList.cpp b/ZAPD/ZRoom/Commands/SetEntranceList.cpp
index c92f56c..79c87ba 100644
--- a/ZAPD/ZRoom/Commands/SetEntranceList.cpp
+++ b/ZAPD/ZRoom/Commands/SetEntranceList.cpp
@@ -24,7 +24,7 @@ void SetEntranceList::DeclareReferences([[maybe_unused]] const std::string& pref
void SetEntranceList::ParseRawDataLate()
{
// Parse Entrances and Generate Declaration
- uint32_t numEntrances = zRoom->GetDeclarationSizeFromNeighbor(segmentOffset) / 2;
+ uint32_t numEntrances = zRoom->parent->GetDeclarationSizeFromNeighbor(segmentOffset) / 2;
uint32_t currentPtr = segmentOffset;
entrances.reserve(numEntrances);
diff --git a/ZAPD/ZRoom/Commands/SetExitList.cpp b/ZAPD/ZRoom/Commands/SetExitList.cpp
index 80ccc6b..78bbaa8 100644
--- a/ZAPD/ZRoom/Commands/SetExitList.cpp
+++ b/ZAPD/ZRoom/Commands/SetExitList.cpp
@@ -24,7 +24,7 @@ void SetExitList::DeclareReferences([[maybe_unused]] const std::string& prefix)
void SetExitList::ParseRawDataLate()
{
// Parse Entrances and Generate Declaration
- uint32_t numEntrances = zRoom->GetDeclarationSizeFromNeighbor(segmentOffset) / 2;
+ uint32_t numEntrances = zRoom->parent->GetDeclarationSizeFromNeighbor(segmentOffset) / 2;
uint32_t currentPtr = segmentOffset;
exits.reserve(numEntrances);
diff --git a/ZAPD/ZRoom/Commands/SetPathways.cpp b/ZAPD/ZRoom/Commands/SetPathways.cpp
index 52d400a..967d10b 100644
--- a/ZAPD/ZRoom/Commands/SetPathways.cpp
+++ b/ZAPD/ZRoom/Commands/SetPathways.cpp
@@ -24,7 +24,7 @@ void SetPathways::ParseRawDataLate()
{
if (Globals::Instance->game == ZGame::MM_RETAIL)
{
- auto numPaths = zRoom->GetDeclarationSizeFromNeighbor(segmentOffset) / 8;
+ auto numPaths = zRoom->parent->GetDeclarationSizeFromNeighbor(segmentOffset) / 8;
pathwayList.SetNumPaths(numPaths);
}
diff --git a/ZAPD/ZRoom/ZRoom.cpp b/ZAPD/ZRoom/ZRoom.cpp
index de9ee61..a28ea7b 100644
--- a/ZAPD/ZRoom/ZRoom.cpp
+++ b/ZAPD/ZRoom/ZRoom.cpp
@@ -64,9 +64,9 @@ ZRoom::~ZRoom()
delete cmd;
}
-void ZRoom::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex)
+void ZRoom::ExtractWithXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex)
{
- ZResource::ExtractFromXML(reader, nRawDataIndex);
+ ZResource::ExtractWithXML(reader, nRawDataIndex);
if (hackMode == "syotes_room")
SyotesRoomFix();
@@ -356,20 +356,6 @@ ZRoomCommand* ZRoom::FindCommandOfType(RoomCommand cmdType)
return nullptr;
}
-size_t ZRoom::GetDeclarationSizeFromNeighbor(uint32_t declarationAddress)
-{
- auto currentDecl = parent->declarations.find(declarationAddress);
- if (currentDecl == parent->declarations.end())
- return 0;
-
- auto nextDecl = currentDecl;
- std::advance(nextDecl, 1);
- if (nextDecl == parent->declarations.end())
- return parent->GetRawData().size() - currentDecl->first;
-
- return nextDecl->first - currentDecl->first;
-}
-
size_t ZRoom::GetCommandSizeFromNeighbor(ZRoomCommand* cmd)
{
int32_t cmdIndex = -1;
diff --git a/ZAPD/ZRoom/ZRoom.h b/ZAPD/ZRoom/ZRoom.h
index 4e9026f..950dbbb 100644
--- a/ZAPD/ZRoom/ZRoom.h
+++ b/ZAPD/ZRoom/ZRoom.h
@@ -22,7 +22,7 @@ public:
ZRoom(ZFile* nParent);
virtual ~ZRoom();
- void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override;
+ void ExtractWithXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override;
void ExtractFromBinary(uint32_t nRawDataIndex, ZResourceType parentType);
void ParseXML(tinyxml2::XMLElement* reader) override;
@@ -37,7 +37,6 @@ public:
void GetSourceOutputCode(const std::string& prefix) override;
std::string GetDefaultName(const std::string& prefix) const override;
- size_t GetDeclarationSizeFromNeighbor(uint32_t declarationAddress);
size_t GetCommandSizeFromNeighbor(ZRoomCommand* cmd);
ZRoomCommand* FindCommandOfType(RoomCommand cmdType);