diff options
| author | Anghelo Carvajal <angheloalf95@gmail.com> | 2021-10-05 13:55:55 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-05 12:55:55 -0400 |
| commit | 4994e732406ffa2942f9c9ea6ff14c424cd0b896 (patch) | |
| tree | 0b40b20db17139c58f5c205643940494c20cbc43 /ZAPD | |
| parent | 17fca1e0cdda5476c4158940ca405b7eb250ec72 (diff) | |
ZResource cleanup (#178)
* Add some general use methods to ZResource
* change zarray
* adapt ztexture
* Clean up ZCollision, ZPath, ZScalar and ZVector
* cleanup background
* cleanup zmtx
* Clean up skeleton and limb
* mark GetSourceTypeName as abstract
* Remove redundant ExtractFromXML
* Reorder stuff to minimize changes in github
* remove extra ZDisplayList constructor
* Remove using namespace tinyxml2;
* run formatter
* Add some brief descriptions, and remove some methods
* Remove trivials GetSourceOutputCode and a bit of cleanup
* Add more descriptions to methods
* Fix blob build
* improve descriptions a bit
* Run formatter
* Now builds
* Add attributes and fix some warnings
* format
* fix merge issues
* fix merge issues and some clang warnings
* format
* Run formatter
* Fix merge issues
* Format
* add SEGMENTED_NULL
* Fix merge issues
* Run format
* dumb fix
* whoops
* format
* whoops
Diffstat (limited to 'ZAPD')
62 files changed, 698 insertions, 769 deletions
diff --git a/ZAPD/Declaration.h b/ZAPD/Declaration.h index e1dff59..0cbc04c 100644 --- a/ZAPD/Declaration.h +++ b/ZAPD/Declaration.h @@ -3,6 +3,12 @@ #include <string> #include <vector> +// TODO: should we drop the `_t` suffix because of UNIX compliance? +typedef uint32_t segptr_t; +typedef uint32_t offset_t; + +#define SEGMENTED_NULL ((segptr_t)0) + enum class DeclarationAlignment { Align4, @@ -35,9 +41,10 @@ public: bool isArray = false; size_t arrayItemCnt = 0; std::string arrayItemCntStr = ""; - std::vector<uint32_t> references; + std::vector<segptr_t> references; bool isUnaccounted = false; bool isPlaceholder = false; + bool declaredInXml = false; StaticConfig staticConf = StaticConfig::Global; Declaration(DeclarationAlignment nAlignment, size_t nSize, std::string nVarType, @@ -63,4 +70,4 @@ public: protected: Declaration(DeclarationAlignment nAlignment, size_t nSize, std::string nText); -};
\ No newline at end of file +}; diff --git a/ZAPD/Globals.cpp b/ZAPD/Globals.cpp index b3bc067..6812ca4 100644 --- a/ZAPD/Globals.cpp +++ b/ZAPD/Globals.cpp @@ -1,11 +1,11 @@ #include "Globals.h" + +#include <algorithm> + #include <Utils/File.h> #include <Utils/Path.h> -#include <algorithm> #include "tinyxml2.h" -using namespace tinyxml2; - Globals* Globals::Instance; Globals::Globals() @@ -35,9 +35,9 @@ std::string Globals::FindSymbolSegRef(int32_t segNumber, uint32_t symbolAddress) { if (segmentRefFiles.find(segNumber) == segmentRefFiles.end()) { - XMLDocument doc; + tinyxml2::XMLDocument doc; std::string filePath = segmentRefs[segNumber]; - XMLError eResult = doc.LoadFile(filePath.c_str()); + tinyxml2::XMLError eResult = doc.LoadFile(filePath.c_str()); if (eResult != tinyxml2::XML_SUCCESS) return "ERROR"; @@ -47,7 +47,7 @@ std::string Globals::FindSymbolSegRef(int32_t segNumber, uint32_t symbolAddress) if (root == nullptr) return "ERROR"; - for (XMLElement* child = root->FirstChildElement(); child != NULL; + for (tinyxml2::XMLElement* child = root->FirstChildElement(); child != NULL; child = child->NextSiblingElement()) { if (std::string(child->Name()) == "File") diff --git a/ZAPD/Main.cpp b/ZAPD/Main.cpp index 643f39d..4454cdd 100644 --- a/ZAPD/Main.cpp +++ b/ZAPD/Main.cpp @@ -22,8 +22,6 @@ #include <string> #include "tinyxml2.h" -using namespace tinyxml2; - extern const char gBuildHash[]; bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, ZFileMode fileMode); @@ -335,8 +333,8 @@ int main(int argc, char* argv[]) bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, ZFileMode fileMode) { - XMLDocument doc; - XMLError eResult = doc.LoadFile(xmlFilePath.string().c_str()); + tinyxml2::XMLDocument doc; + tinyxml2::XMLError eResult = doc.LoadFile(xmlFilePath.string().c_str()); if (eResult != tinyxml2::XML_SUCCESS) { @@ -344,7 +342,7 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, ZFileMode file return false; } - XMLNode* root = doc.FirstChild(); + tinyxml2::XMLNode* root = doc.FirstChild(); if (root == nullptr) { @@ -352,7 +350,7 @@ bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, ZFileMode file return false; } - for (XMLElement* child = root->FirstChildElement(); child != NULL; + for (tinyxml2::XMLElement* child = root->FirstChildElement(); child != NULL; child = child->NextSiblingElement()) { if (std::string(child->Name()) == "File") @@ -423,7 +421,7 @@ void BuildAssetBlob(const fs::path& blobFilePath, const fs::path& outPath) ZBlob* blob = ZBlob::FromFile(blobFilePath.string()); std::string name = outPath.stem().string(); // filename without extension - std::string src = blob->GetSourceOutputCode(name); + std::string src = blob->GetBodySourceCode(); File::WriteAllText(outPath.string(), src); diff --git a/ZAPD/OutputFormatter.cpp b/ZAPD/OutputFormatter.cpp index 4c92b62..393ec19 100644 --- a/ZAPD/OutputFormatter.cpp +++ b/ZAPD/OutputFormatter.cpp @@ -101,9 +101,9 @@ int (*OutputFormatter::StaticWriter())(const char* buf, int count) return &WriteStatic; } -OutputFormatter::OutputFormatter(uint32_t tabSize, uint32_t defaultIndent, uint32_t lineLimit) - : tabSize{tabSize}, lineLimit{lineLimit}, col{0}, nest{0}, nestIndent{defaultIndent}, - currentIndent{defaultIndent}, wordNests(0), wordP{word}, spaceP{space} +OutputFormatter::OutputFormatter(uint32_t tabSize, uint32_t indentation, uint32_t lineLimit) + : tabSize{tabSize}, lineLimit{lineLimit}, col{0}, nest{0}, nestIndent{indentation}, + currentIndent{indentation}, wordNests(0), wordP{word}, spaceP{space} { } diff --git a/ZAPD/OutputFormatter.h b/ZAPD/OutputFormatter.h index b024d95..28955b1 100644 --- a/ZAPD/OutputFormatter.h +++ b/ZAPD/OutputFormatter.h @@ -29,7 +29,7 @@ private: static int WriteStatic(const char* buf, int count); public: - OutputFormatter(uint32_t tabSize = 4, uint32_t defaultIndent = 4, uint32_t lineLimit = 120); + OutputFormatter(uint32_t tabSize = 4, uint32_t indentation = 4, uint32_t lineLimit = 120); int (*StaticWriter())(const char* buf, int count); // Must be `int` due to libgfxd @@ -37,4 +37,4 @@ public: int Write(const std::string& buf); std::string GetOutput(); -};
\ No newline at end of file +}; diff --git a/ZAPD/ZAnimation.cpp b/ZAPD/ZAnimation.cpp index d6645d0..ed9a58f 100644 --- a/ZAPD/ZAnimation.cpp +++ b/ZAPD/ZAnimation.cpp @@ -1,5 +1,7 @@ #include "ZAnimation.h" + #include <utility> + #include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/File.h" @@ -284,15 +286,6 @@ void ZCurveAnimation::ParseRawData() } } -void ZCurveAnimation::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - - Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align16, - GetRawDataSize(), GetSourceTypeName(), name, ""); - decl->staticConf = staticConf; -} - void ZCurveAnimation::DeclareReferences(const std::string& prefix) { if (refIndex != 0) @@ -472,15 +465,6 @@ ZLegacyAnimation::ZLegacyAnimation(ZFile* nParent) : ZAnimation(nParent) { } -void ZLegacyAnimation::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZAnimation::ExtractFromXML(reader, nRawDataIndex); - - Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, - GetRawDataSize(), GetSourceTypeName(), name, ""); - decl->staticConf = staticConf; -} - void ZLegacyAnimation::ParseRawData() { ZAnimation::ParseRawData(); diff --git a/ZAPD/ZAnimation.h b/ZAPD/ZAnimation.h index 436b4d3..b59a342 100644 --- a/ZAPD/ZAnimation.h +++ b/ZAPD/ZAnimation.h @@ -122,7 +122,6 @@ public: void ParseXML(tinyxml2::XMLElement* reader) override; void ParseRawData() override; - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; void DeclareReferences(const std::string& prefix) override; size_t GetRawDataSize() const override; @@ -158,8 +157,6 @@ class ZLegacyAnimation : public ZAnimation public: ZLegacyAnimation(ZFile* nParent); - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; - void ParseRawData() override; void DeclareReferences(const std::string& prefix) override; std::string GetBodySourceCode() const override; diff --git a/ZAPD/ZArray.cpp b/ZAPD/ZArray.cpp index f46ee8d..9c562f0 100644 --- a/ZAPD/ZArray.cpp +++ b/ZAPD/ZArray.cpp @@ -1,5 +1,7 @@ #include "ZArray.h" + #include <cassert> + #include "Globals.h" #include "Utils/StringHelper.h" #include "ZFile.h" @@ -53,30 +55,56 @@ void ZArray::ParseXML(tinyxml2::XMLElement* reader) } } -std::string ZArray::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) +Declaration* ZArray::DeclareVar(const std::string& prefix, const std::string& bodyStr) { - std::string output = ""; + std::string auxName = name; - for (size_t i = 0; i < arrayCnt; i++) - { - output += resList.at(i)->GetBodySourceCode(); - output += ","; + if (name == "") + auxName = GetDefaultName(prefix); - if (i < arrayCnt - 1) - output += "\n"; + ZResource* res = resList.at(0); + Declaration* decl; + if (res->IsExternalResource()) + { + auto filepath = Globals::Instance->outputPath / name; + std::string includePath = StringHelper::Sprintf("%s.%s.inc", filepath.c_str(), + res->GetExternalExtension().c_str()); + decl = parent->AddDeclarationIncludeArray(rawDataIndex, includePath, GetRawDataSize(), + GetSourceTypeName(), name, arrayCnt); + decl->text = bodyStr; + decl->isExternal = true; + } + else + { + decl = + parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), + GetSourceTypeName(), name, arrayCnt, bodyStr); } - Declaration* decl = - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - resList.at(0)->GetSourceTypeName(), name, arrayCnt, output); decl->staticConf = staticConf; - - return ""; + return decl; } -std::string ZArray::GetSourceTypeName() const +std::string ZArray::GetBodySourceCode() const { - return resList.at(0)->GetSourceTypeName(); + std::string output = ""; + + for (size_t i = 0; i < arrayCnt; i++) + { + const auto& res = resList[i]; + output += "\t"; + + if (res->GetResourceType() == ZResourceType::Scalar || + res->GetResourceType() == ZResourceType::Vertex) + output += resList.at(i)->GetBodySourceCode(); + else + output += StringHelper::Sprintf("{ %s }", resList.at(i)->GetBodySourceCode().c_str()); + + if (i < arrayCnt - 1 || res->IsExternalResource()) + output += ",\n"; + } + + return output; } size_t ZArray::GetRawDataSize() const @@ -87,7 +115,17 @@ size_t ZArray::GetRawDataSize() const return size; } +std::string ZArray::GetSourceTypeName() const +{ + return resList.at(0)->GetSourceTypeName(); +} + ZResourceType ZArray::GetResourceType() const { return ZResourceType::Array; } + +DeclarationAlignment ZArray::GetDeclarationAlignment() const +{ + return resList.at(0)->GetDeclarationAlignment(); +} diff --git a/ZAPD/ZArray.h b/ZAPD/ZArray.h index 257a9dc..46a04d7 100644 --- a/ZAPD/ZArray.h +++ b/ZAPD/ZArray.h @@ -14,14 +14,16 @@ public: void ParseXML(tinyxml2::XMLElement* reader) override; - std::string GetSourceOutputCode(const std::string& prefix) override; - - std::string GetSourceTypeName() const override; + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; + std::string GetBodySourceCode() const override; size_t GetRawDataSize() const override; + std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; + DeclarationAlignment GetDeclarationAlignment() const override; + protected: size_t arrayCnt; std::string childName; diff --git a/ZAPD/ZBackground.cpp b/ZAPD/ZBackground.cpp index 0e15097..938f568 100644 --- a/ZAPD/ZBackground.cpp +++ b/ZAPD/ZBackground.cpp @@ -1,4 +1,5 @@ #include "ZBackground.h" + #include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/File.h" @@ -15,16 +16,6 @@ ZBackground::ZBackground(ZFile* nParent) : ZResource(nParent) { } -ZBackground::ZBackground(const std::string& prefix, uint32_t nRawDataIndex, ZFile* nParent) - : ZResource(nParent) -{ - rawDataIndex = nRawDataIndex; - name = GetDefaultName(prefix.c_str(), rawDataIndex); - outName = name; - - ParseRawData(); -} - void ZBackground::ParseRawData() { ZResource::ParseRawData(); @@ -60,12 +51,6 @@ void ZBackground::ParseBinaryFile(const std::string& inFolder, bool appendOutNam CheckValidJpeg(filepath.generic_string()); } -void ZBackground::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - DeclareVar("", ""); -} - void ZBackground::CheckValidJpeg(const std::string& filepath) { std::string filename = outName; @@ -132,17 +117,18 @@ size_t ZBackground::GetRawDataSize() const return Globals::Instance->cfg.bgScreenHeight * Globals::Instance->cfg.bgScreenWidth * 2; } -void ZBackground::DeclareVar(const std::string& prefix, const std::string& bodyStr) const +Declaration* ZBackground::DeclareVar(const std::string& prefix, const std::string& bodyStr) { std::string auxName = name; if (name == "") - auxName = GetDefaultName(prefix, rawDataIndex); + auxName = GetDefaultName(prefix); - Declaration* decl = parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align8, + Declaration* decl = parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), GetSourceTypeName(), auxName, "SCREEN_WIDTH * SCREEN_HEIGHT / 4", bodyStr); decl->staticConf = staticConf; + return decl; } bool ZBackground::IsExternalResource() const @@ -178,23 +164,9 @@ std::string ZBackground::GetBodySourceCode() const return bodyStr; } -std::string ZBackground::GetSourceOutputCode(const std::string& prefix) +std::string ZBackground::GetDefaultName(const std::string& prefix) const { - std::string bodyStr = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(rawDataIndex); - - if (decl == nullptr) - DeclareVar(prefix, bodyStr); - else - decl->text = bodyStr; - - return ""; -} - -std::string ZBackground::GetDefaultName(const std::string& prefix, uint32_t address) -{ - return StringHelper::Sprintf("%sBackground_%06X", prefix.c_str(), address); + return StringHelper::Sprintf("%sBackground_%06X", prefix.c_str(), rawDataIndex); } std::string ZBackground::GetSourceTypeName() const @@ -206,3 +178,8 @@ ZResourceType ZBackground::GetResourceType() const { return ZResourceType::Background; } + +DeclarationAlignment ZBackground::GetDeclarationAlignment() const +{ + return DeclarationAlignment::Align8; +} diff --git a/ZAPD/ZBackground.h b/ZAPD/ZBackground.h index 8071f84..e3728bd 100644 --- a/ZAPD/ZBackground.h +++ b/ZAPD/ZBackground.h @@ -11,25 +11,24 @@ protected: public: ZBackground(ZFile* nParent); - ZBackground(const std::string& prefix, uint32_t nRawDataIndex, ZFile* nParent); - void ParseRawData() override; void ParseBinaryFile(const std::string& inFolder, bool appendOutName); - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; - - void CheckValidJpeg(const std::string& filepath); - size_t GetRawDataSize() const override; + void ParseRawData() override; - void DeclareVar(const std::string& prefix, const std::string& bodyStr) const; + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; + std::string GetBodySourceCode() const override; + std::string GetDefaultName(const std::string& prefix) const override; - bool IsExternalResource() const override; - std::string GetExternalExtension() const override; void Save(const fs::path& outFolder) override; - std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; - static std::string GetDefaultName(const std::string& prefix, uint32_t address); + bool IsExternalResource() const override; std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; + std::string GetExternalExtension() const override; + + size_t GetRawDataSize() const override; + DeclarationAlignment GetDeclarationAlignment() const override; + + void CheckValidJpeg(const std::string& filepath); }; diff --git a/ZAPD/ZBlob.cpp b/ZAPD/ZBlob.cpp index 3c18733..1cdc226 100644 --- a/ZAPD/ZBlob.cpp +++ b/ZAPD/ZBlob.cpp @@ -1,12 +1,12 @@ #include "ZBlob.h" + +#include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/File.h" #include "Utils/Path.h" #include "Utils/StringHelper.h" #include "ZFile.h" -using namespace tinyxml2; - REGISTER_ZFILENODE(Blob, ZBlob); ZBlob::ZBlob(ZFile* nParent) : ZResource(nParent) @@ -14,19 +14,6 @@ ZBlob::ZBlob(ZFile* nParent) : ZResource(nParent) RegisterRequiredAttribute("Size"); } -// Build Source File Mode -ZBlob* ZBlob::BuildFromXML(XMLElement* reader, const std::string& inFolder, bool readFile) -{ - ZBlob* blob = new ZBlob(nullptr); - - blob->ParseXML(reader); - - if (readFile) - blob->blobData = File::ReadAllBytes(inFolder + "/" + blob->name + ".bin"); - - return blob; -} - ZBlob* ZBlob::FromFile(const std::string& filePath) { ZBlob* blob = new ZBlob(nullptr); @@ -49,14 +36,34 @@ void ZBlob::ParseRawData() parent->GetRawData().begin() + rawDataIndex + blobSize); } -std::string ZBlob::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) +Declaration* ZBlob::DeclareVar(const std::string& prefix, + [[maybe_unused]] const std::string& bodyStr) +{ + std::string auxName = name; + + if (name == "") + auxName = GetDefaultName(prefix); + + std::string path = Path::GetFileNameWithoutExtension(auxName); + + std::string assetOutDir = + (Globals::Instance->outputPath / Path::GetFileNameWithoutExtension(GetOutName())).string(); + + std::string incStr = + StringHelper::Sprintf("%s.%s.inc", assetOutDir.c_str(), GetExternalExtension().c_str()); + + return parent->AddDeclarationIncludeArray(rawDataIndex, incStr, GetRawDataSize(), + GetSourceTypeName(), auxName, 0); +} + +std::string ZBlob::GetBodySourceCode() const { std::string sourceOutput = ""; for (size_t i = 0; i < blobData.size(); i += 1) { if (i % 16 == 0) - sourceOutput += " "; + sourceOutput += "\t"; sourceOutput += StringHelper::Sprintf("0x%02X, ", blobData[i]); diff --git a/ZAPD/ZBlob.h b/ZAPD/ZBlob.h index 2f8d315..86623b5 100644 --- a/ZAPD/ZBlob.h +++ b/ZAPD/ZBlob.h @@ -8,13 +8,14 @@ class ZBlob : public ZResource public: ZBlob(ZFile* nParent); - static ZBlob* BuildFromXML(tinyxml2::XMLElement* reader, const std::string& inFolder, - bool readFile); static ZBlob* FromFile(const std::string& filePath); void ParseXML(tinyxml2::XMLElement* reader) override; void ParseRawData() override; - std::string GetSourceOutputCode(const std::string& prefix) override; + + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; + std::string GetBodySourceCode() const override; + std::string GetSourceOutputHeader(const std::string& prefix) override; void Save(const fs::path& outFolder) override; diff --git a/ZAPD/ZCollision.cpp b/ZAPD/ZCollision.cpp index 17d173d..8f2a6e6 100644 --- a/ZAPD/ZCollision.cpp +++ b/ZAPD/ZCollision.cpp @@ -1,6 +1,8 @@ #include "ZCollision.h" -#include <stdint.h> + +#include <cstdint> #include <string> + #include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/StringHelper.h" @@ -13,9 +15,6 @@ ZCollisionHeader::ZCollisionHeader(ZFile* nParent) : ZResource(nParent) ZCollisionHeader::~ZCollisionHeader() { - for (WaterBoxHeader* waterBox : waterBoxes) - delete waterBox; - delete camData; } @@ -51,8 +50,16 @@ void ZCollisionHeader::ParseRawData() vertices.reserve(numVerts); polygons.reserve(numPolygons); + uint32_t currentPtr = vtxSegmentOffset; + for (uint16_t i = 0; i < numVerts; i++) - vertices.push_back(VertexEntry(rawData, vtxSegmentOffset + (i * 6))); + { + ZVector vec(parent); + vec.ExtractFromBinary(currentPtr, ZScalarType::ZSCALAR_S16, 3); + + currentPtr += vec.GetRawDataSize(); + vertices.push_back(vec); + } for (uint16_t i = 0; i < numPolygons; i++) polygons.push_back(PolygonEntry(rawData, polySegmentOffset + (i * 16))); @@ -74,30 +81,34 @@ void ZCollisionHeader::ParseRawData() polyTypeDefSegmentOffset, polygonTypes.size()); for (uint16_t i = 0; i < numWaterBoxes; i++) - waterBoxes.push_back(new WaterBoxHeader( + waterBoxes.push_back(WaterBoxHeader( rawData, waterBoxSegmentOffset + (i * (Globals::Instance->game == ZGame::OOT_SW97 ? 12 : 16)))); +} +void ZCollisionHeader::DeclareReferences(const std::string& prefix) +{ std::string declaration = ""; + std::string auxName = name; + + if (name == "") + auxName = GetDefaultName(prefix); if (waterBoxes.size() > 0) { for (size_t i = 0; i < waterBoxes.size(); i++) { - declaration += StringHelper::Sprintf("\t{ %i, %i, %i, %i, %i, 0x%08X },", - waterBoxes[i]->xMin, waterBoxes[i]->ySurface, - waterBoxes[i]->zMin, waterBoxes[i]->xLength, - waterBoxes[i]->zLength, waterBoxes[i]->properties); + declaration += + StringHelper::Sprintf("\t{ %s },", waterBoxes[i].GetBodySourceCode().c_str()); if (i + 1 < waterBoxes.size()) declaration += "\n"; } - } - if (waterBoxAddress != 0) parent->AddDeclarationArray( waterBoxSegmentOffset, DeclarationAlignment::Align4, 16 * waterBoxes.size(), "WaterBox", - StringHelper::Sprintf("%s_waterBoxes_%06X", name.c_str(), waterBoxSegmentOffset), 0, + StringHelper::Sprintf("%s_waterBoxes_%06X", auxName.c_str(), waterBoxSegmentOffset), 0, declaration); + } if (polygons.size() > 0) { @@ -113,14 +124,10 @@ void ZCollisionHeader::ParseRawData() declaration += "\n"; } - if (polyAddress != 0) - { - parent->AddDeclarationArray( - polySegmentOffset, DeclarationAlignment::Align4, polygons.size() * 16, - "CollisionPoly", - StringHelper::Sprintf("%s_polygons_%08X", name.c_str(), polySegmentOffset), - polygons.size(), declaration); - } + parent->AddDeclarationArray( + polySegmentOffset, DeclarationAlignment::Align4, polygons.size() * 16, "CollisionPoly", + StringHelper::Sprintf("%s_polygons_%08X", auxName.c_str(), polySegmentOffset), 0, + declaration); } declaration = ""; @@ -137,7 +144,7 @@ void ZCollisionHeader::ParseRawData() parent->AddDeclarationArray( polyTypeDefSegmentOffset, DeclarationAlignment::Align4, polygonTypes.size() * 8, "SurfaceType", - StringHelper::Sprintf("%s_surfaceType_%08X", name.c_str(), polyTypeDefSegmentOffset), + StringHelper::Sprintf("%s_surfaceType_%08X", auxName.c_str(), polyTypeDefSegmentOffset), polygonTypes.size(), declaration); declaration = ""; @@ -148,46 +155,55 @@ void ZCollisionHeader::ParseRawData() for (size_t i = 0; i < vertices.size(); i++) { - declaration += StringHelper::Sprintf("\t{ %6i, %6i, %6i },", vertices[i].x, - vertices[i].y, vertices[i].z); + declaration += + StringHelper::Sprintf("\t{ %s },", vertices[i].GetBodySourceCode().c_str()); if (i < vertices.size() - 1) declaration += "\n"; } + const auto& first = vertices.front(); if (vtxAddress != 0) parent->AddDeclarationArray( - vtxSegmentOffset, DeclarationAlignment::Align4, vertices.size() * 6, "Vec3s", - StringHelper::Sprintf("%s_vtx_%08X", name.c_str(), vtxSegmentOffset), 0, + vtxSegmentOffset, first.GetDeclarationAlignment(), + vertices.size() * first.GetRawDataSize(), first.GetSourceTypeName(), + StringHelper::Sprintf("%s_vtx_%08X", auxName.c_str(), vtxSegmentOffset), 0, declaration); declaration = ""; } +} - declaration = ""; - char waterBoxStr[2048]; - - if (waterBoxAddress != 0) - sprintf(waterBoxStr, "%s_waterBoxes_%06X", name.c_str(), waterBoxSegmentOffset); - else - sprintf(waterBoxStr, "NULL"); +std::string ZCollisionHeader::GetBodySourceCode() const +{ + std::string declaration = ""; declaration += "\n"; - declaration += StringHelper::Sprintf(" { %i, %i, %i },\n { %i, %i, %i },\n", absMinX, - absMinY, absMinZ, absMaxX, absMaxY, absMaxZ); + declaration += StringHelper::Sprintf("\t{ %i, %i, %i },\n", absMinX, absMinY, absMinZ); + declaration += StringHelper::Sprintf("\t{ %i, %i, %i },\n", absMaxX, absMaxY, absMaxZ); + + std::string vtxName = parent->GetDeclarationPtrName(vtxAddress); + declaration += StringHelper::Sprintf("\t%i,\n\t%s,\n", numVerts, vtxName.c_str()); + + std::string polyName = parent->GetDeclarationPtrName(polyAddress); + declaration += StringHelper::Sprintf("\t%i,\n\t%s,\n", numPolygons, polyName.c_str()); + + std::string surfaceName = parent->GetDeclarationPtrName(polyTypeDefAddress); + declaration += StringHelper::Sprintf("\t%s,\n", surfaceName.c_str()); + + std::string camName = parent->GetDeclarationPtrName(camDataAddress); + declaration += StringHelper::Sprintf("\t%s,\n", camName.c_str()); - declaration += StringHelper::Sprintf( - " %i,\n %s_vtx_%08X,\n %i,\n %s_polygons_%08X,\n %s_surfaceType_%08X,\n " - "%s_camDataList_%08X,\n %i,\n %s\n", - numVerts, name.c_str(), vtxSegmentOffset, numPolygons, name.c_str(), polySegmentOffset, - name.c_str(), polyTypeDefSegmentOffset, name.c_str(), camDataSegmentOffset, numWaterBoxes, - waterBoxStr); + std::string waterBoxName = parent->GetDeclarationPtrName(waterBoxAddress); + declaration += StringHelper::Sprintf("\t%i,\n\t%s\n", numWaterBoxes, waterBoxName.c_str()); - Declaration* decl = - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, declaration); - decl->staticConf = staticConf; + return declaration; +} + +std::string ZCollisionHeader::GetDefaultName(const std::string& prefix) const +{ + return StringHelper::Sprintf("%sCol_%06X", prefix.c_str(), rawDataIndex); } std::string ZCollisionHeader::GetSourceTypeName() const @@ -219,15 +235,6 @@ PolygonEntry::PolygonEntry(const std::vector<uint8_t>& rawData, uint32_t rawData d = BitConverter::ToUInt16BE(data, rawDataIndex + 14); } -VertexEntry::VertexEntry(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex) -{ - const uint8_t* data = rawData.data(); - - x = BitConverter::ToInt16BE(data, rawDataIndex + 0); - y = BitConverter::ToInt16BE(data, rawDataIndex + 2); - z = BitConverter::ToInt16BE(data, rawDataIndex + 4); -} - WaterBoxHeader::WaterBoxHeader(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex) { const uint8_t* data = rawData.data(); @@ -244,6 +251,12 @@ WaterBoxHeader::WaterBoxHeader(const std::vector<uint8_t>& rawData, uint32_t raw properties = BitConverter::ToInt32BE(data, rawDataIndex + 12); } +std::string WaterBoxHeader::GetBodySourceCode() const +{ + return StringHelper::Sprintf("%i, %i, %i, %i, %i, 0x%08X", xMin, ySurface, zMin, xLength, + zLength, properties); +} + CameraDataList::CameraDataList(ZFile* parent, const std::string& prefix, const std::vector<uint8_t>& rawData, uint32_t rawDataIndex, uint32_t polyTypeDefSegmentOffset, @@ -330,6 +343,15 @@ CameraDataList::CameraDataList(ZFile* parent, const std::string& prefix, } } +CameraDataList::~CameraDataList() +{ + for (auto entry : entries) + delete entry; + + for (auto camPosData : cameraPositionData) + delete camPosData; +} + CameraPositionData::CameraPositionData(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex) { x = BitConverter::ToInt16BE(rawData, rawDataIndex + 0); diff --git a/ZAPD/ZCollision.h b/ZAPD/ZCollision.h index 2942064..0c6710e 100644 --- a/ZAPD/ZCollision.h +++ b/ZAPD/ZCollision.h @@ -3,6 +3,7 @@ #include "ZFile.h" #include "ZResource.h" #include "ZRoom/ZRoom.h" +#include "ZVector.h" class PolygonEntry { @@ -14,17 +15,14 @@ public: PolygonEntry(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex); }; -class VertexEntry +class WaterBoxHeader { public: - int16_t x, y, z; + WaterBoxHeader(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex); - VertexEntry(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex); -}; + std::string GetBodySourceCode() const; -class WaterBoxHeader -{ -public: +protected: int16_t xMin; int16_t ySurface; int16_t zMin; @@ -32,8 +30,6 @@ public: int16_t zLength; int16_t pad; int32_t properties; - - WaterBoxHeader(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex); }; class CameraPositionData @@ -61,6 +57,7 @@ public: CameraDataList(ZFile* parent, const std::string& prefix, const std::vector<uint8_t>& rawData, uint32_t rawDataIndex, uint32_t polyTypeDefSegmentOffset, uint32_t polygonTypesCnt); + ~CameraDataList(); }; class ZCollisionHeader : public ZResource @@ -81,16 +78,20 @@ public: uint32_t vtxSegmentOffset, polySegmentOffset, polyTypeDefSegmentOffset, camDataSegmentOffset, waterBoxSegmentOffset; - std::vector<VertexEntry> vertices; + std::vector<ZVector> vertices; std::vector<PolygonEntry> polygons; std::vector<uint64_t> polygonTypes; - std::vector<WaterBoxHeader*> waterBoxes; + std::vector<WaterBoxHeader> waterBoxes; CameraDataList* camData; ZCollisionHeader(ZFile* nParent); ~ZCollisionHeader(); void ParseRawData() override; + void DeclareReferences(const std::string& prefix) override; + + std::string GetBodySourceCode() const override; + std::string GetDefaultName(const std::string& prefix) const override; std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; diff --git a/ZAPD/ZCutscene.cpp b/ZAPD/ZCutscene.cpp index 673a161..1157f08 100644 --- a/ZAPD/ZCutscene.cpp +++ b/ZAPD/ZCutscene.cpp @@ -104,33 +104,6 @@ std::string ZCutscene::GetBodySourceCode() const return output; } -std::string ZCutscene::GetSourceOutputCode(const std::string& prefix) -{ - std::string bodyStr = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(rawDataIndex); - - if (decl == nullptr) - DeclareVar(prefix, bodyStr); - else - decl->text = bodyStr; - - return ""; -} - -void ZCutscene::DeclareVar(const std::string& prefix, const std::string& bodyStr) const -{ - std::string auxName = name; - - if (auxName == "") - auxName = StringHelper::Sprintf("%sCutsceneData0x%06X", prefix.c_str(), rawDataIndex); - - Declaration* decl = - parent->AddDeclarationArray(getSegmentOffset(), DeclarationAlignment::Align4, - GetRawDataSize(), GetSourceTypeName(), auxName, 0, bodyStr); - decl->staticConf = staticConf; -} - size_t ZCutscene::GetRawDataSize() const { size_t size = 0; @@ -151,12 +124,6 @@ size_t ZCutscene::GetRawDataSize() const return size; } -void ZCutscene::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - DeclareVar(parent->GetName(), ""); -} - void ZCutscene::ParseRawData() { ZResource::ParseRawData(); @@ -1268,6 +1235,20 @@ ZCutsceneBase::ZCutsceneBase(ZFile* nParent) : ZResource(nParent) { } +Declaration* ZCutsceneBase::DeclareVar(const std::string& prefix, const std::string& bodyStr) +{ + std::string auxName = name; + + if (name == "") + auxName = GetDefaultName(prefix); + + Declaration* decl = + parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), + GetSourceTypeName(), auxName, 0, bodyStr); + decl->staticConf = staticConf; + return decl; +} + std::string ZCutsceneBase::GetSourceTypeName() const { return "CutsceneData"; diff --git a/ZAPD/ZCutscene.h b/ZAPD/ZCutscene.h index 19389a9..5424762 100644 --- a/ZAPD/ZCutscene.h +++ b/ZAPD/ZCutscene.h @@ -413,13 +413,9 @@ class ZCutsceneBase : public ZResource public: ZCutsceneBase(ZFile* nParent); - std::string GetBodySourceCode() const override = 0; - - virtual void DeclareVar(const std::string& prefix, const std::string& bodyStr) const = 0; + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; std::string GetSourceTypeName() const override; - - virtual uint32_t getSegmentOffset() const = 0; }; class ZCutscene : public ZCutsceneBase @@ -431,17 +427,12 @@ public: void ParseRawData() override; std::string GetBodySourceCode() const override; - void DeclareVar(const std::string& prefix, const std::string& bodyStr) const override; - std::string GetSourceOutputCode(const std::string& prefix) override; size_t GetRawDataSize() const override; - CutsceneCommands GetCommandFromID(int32_t id); - uint32_t getSegmentOffset() const override { return rawDataIndex; } ZResourceType GetResourceType() const override; - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; + CutsceneCommands GetCommandFromID(int32_t id); -protected: int32_t numCommands; int32_t endFrame; std::vector<CutsceneCommand*> commands; diff --git a/ZAPD/ZCutsceneMM.cpp b/ZAPD/ZCutsceneMM.cpp index 0ada314..52d95bc 100644 --- a/ZAPD/ZCutsceneMM.cpp +++ b/ZAPD/ZCutsceneMM.cpp @@ -1,4 +1,5 @@ #include "ZCutsceneMM.h" + #include "Utils/BitConverter.h" #include "Utils/StringHelper.h" @@ -28,47 +29,13 @@ std::string ZCutsceneMM::GetBodySourceCode() const return output; } -std::string ZCutsceneMM::GetSourceOutputCode(const std::string& prefix) -{ - std::string bodyStr = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(rawDataIndex); - - if (decl == nullptr) - DeclareVar(prefix, bodyStr); - else - decl->text = bodyStr; - - return ""; -} - -void ZCutsceneMM::DeclareVar(const std::string& prefix, const std::string& bodyStr) const -{ - std::string auxName = name; - - if (auxName == "") - auxName = StringHelper::Sprintf("%sCutsceneData0x%06X", prefix.c_str(), rawDataIndex); - - Declaration* decl = - parent->AddDeclarationArray(getSegmentOffset(), DeclarationAlignment::Align4, - GetRawDataSize(), "s32", auxName, 0, bodyStr); - decl->staticConf = staticConf; -} - size_t ZCutsceneMM::GetRawDataSize() const { return 8 + data.size() * 4; } -void ZCutsceneMM::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - DeclareVar(parent->GetName(), ""); -} - void ZCutsceneMM::ParseRawData() { - segmentOffset = rawDataIndex; const auto& rawData = parent->GetRawData(); numCommands = BitConverter::ToInt32BE(rawData, rawDataIndex + 0); diff --git a/ZAPD/ZCutsceneMM.h b/ZAPD/ZCutsceneMM.h index 91a40e8..0b8b13f 100644 --- a/ZAPD/ZCutsceneMM.h +++ b/ZAPD/ZCutsceneMM.h @@ -10,22 +10,15 @@ class ZCutsceneMM : public ZCutsceneBase { public: - uint32_t segmentOffset; - ZCutsceneMM(ZFile* nParent); virtual ~ZCutsceneMM(); std::string GetBodySourceCode() const override; - void DeclareVar(const std::string& prefix, const std::string& bodyStr) const override; - std::string GetSourceOutputCode(const std::string& prefix) override; size_t GetRawDataSize() const override; - uint32_t getSegmentOffset() const override { return segmentOffset; } void ParseRawData() override; ZResourceType GetResourceType() const override; - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; - protected: int32_t numCommands; int32_t endFrame; diff --git a/ZAPD/ZDisplayList.cpp b/ZAPD/ZDisplayList.cpp index 6bf77b1..fd38e1c 100644 --- a/ZAPD/ZDisplayList.cpp +++ b/ZAPD/ZDisplayList.cpp @@ -3,7 +3,7 @@ #include <algorithm> #include <cassert> #include <chrono> -#include <math.h> +#include <cmath> #include "Globals.h" #include "OutputFormatter.h" @@ -54,12 +54,11 @@ void ZDisplayList::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDat DeclareVar("", ""); } -ZDisplayList::ZDisplayList(uint32_t nRawDataIndex, int32_t rawDataSize, ZFile* nParent) - : ZDisplayList(nParent) +void ZDisplayList::ExtractFromBinary(uint32_t nRawDataIndex, int32_t rawDataSize) { rawDataIndex = nRawDataIndex; - name = StringHelper::Sprintf("DL_%06X", rawDataIndex); numInstructions = rawDataSize / 8; + ParseRawData(); } @@ -260,9 +259,10 @@ void ZDisplayList::ParseF3DZEX(F3DZEXOpcode opcode, uint64_t data, int32_t i, st sprintf(line, "gsSPBranchLessZraw(%sDlist0x%06X, 0x%02X, 0x%02X),", prefix.c_str(), h & 0x00FFFFFF, (a / 5) | (b / 2), z); - ZDisplayList* nList = new ZDisplayList( - h & 0x00FFFFFF, GetDListLength(parent->GetRawData(), h & 0x00FFFFFF, dListType), - parent); + ZDisplayList* nList = new ZDisplayList(parent); + nList->ExtractFromBinary( + h & 0x00FFFFFF, GetDListLength(parent->GetRawData(), h & 0x00FFFFFF, dListType)); + nList->SetName(nList->GetDefaultName(prefix)); otherDLists.push_back(nList); i++; @@ -714,9 +714,10 @@ void ZDisplayList::Opcode_G_DL(uint64_t data, std::string prefix, char* line) } else { - ZDisplayList* nList = new ZDisplayList( - GETSEGOFFSET(data), GetDListLength(parent->GetRawData(), GETSEGOFFSET(data), dListType), - parent); + ZDisplayList* nList = new ZDisplayList(parent); + nList->ExtractFromBinary(GETSEGOFFSET(data), GetDListLength(parent->GetRawData(), + GETSEGOFFSET(data), dListType)); + nList->SetName(nList->GetDefaultName(prefix)); otherDLists.push_back(nList); } @@ -870,8 +871,7 @@ void ZDisplayList::Opcode_G_VTX(uint64_t data, char* line) for (int32_t i = 0; i < nn; i++) { ZVtx vtx(parent); - vtx.SetRawDataIndex(currentPtr); - vtx.ParseRawData(); + vtx.ExtractFromFile(currentPtr); vtxList.push_back(vtx); currentPtr += 16; @@ -1629,8 +1629,7 @@ static int32_t GfxdCallback_Vtx(uint32_t seg, int32_t count) for (int32_t i = 0; i < count; i++) { ZVtx vtx(self->parent); - vtx.SetRawDataIndex(currentPtr); - vtx.ParseRawData(); + vtx.ExtractFromFile(currentPtr); vtxList.push_back(vtx); currentPtr += 16; @@ -1727,11 +1726,11 @@ static int32_t GfxdCallback_DisplayList(uint32_t seg) if ((dListSegNum <= 6) && Globals::Instance->HasSegment(dListSegNum)) { - ZDisplayList* newDList = new ZDisplayList( + ZDisplayList* newDList = new ZDisplayList(self->parent); + newDList->ExtractFromBinary( dListOffset, - self->GetDListLength(self->parent->GetRawData(), dListOffset, self->dListType), - self->parent); - newDList->parent = self->parent; + self->GetDListLength(self->parent->GetRawData(), dListOffset, self->dListType)); + newDList->SetName(newDList->GetDefaultName(self->parent->GetName())); self->otherDLists.push_back(newDList); } @@ -1759,7 +1758,10 @@ static int32_t GfxdCallback_Matrix(uint32_t seg) self->parent->GetDeclaration(Seg2Filespace(seg, self->parent->baseAddress)); if (decl == nullptr) { - ZMtx mtx(self->GetName(), Seg2Filespace(seg, self->parent->baseAddress), self->parent); + ZMtx mtx(self->parent); + mtx.SetName(mtx.GetDefaultName(self->GetName())); + mtx.ExtractFromFile(Seg2Filespace(seg, self->parent->baseAddress)); + mtx.DeclareVar(self->GetName(), ""); mtx.GetSourceOutputCode(self->GetName()); self->mtxList.push_back(mtx); @@ -2056,8 +2058,8 @@ bool ZDisplayList::TextureGenCheck(ZFile* parent, [[maybe_unused]] std::string p else { tex = new ZTexture(parent); - tex->FromBinary(texAddr, texWidth, texHeight, - TexFormatToTexType(texFmt, texSiz), texIsPalette); + tex->ExtractFromBinary(texAddr, texWidth, texHeight, + TexFormatToTexType(texFmt, texSiz), texIsPalette); parent->AddTextureResource(texAddr, tex); } @@ -2079,8 +2081,8 @@ bool ZDisplayList::TextureGenCheck(ZFile* parent, [[maybe_unused]] std::string p else { tex = new ZTexture(Globals::Instance->lastScene->parent); - tex->FromBinary(texAddr, texWidth, texHeight, - TexFormatToTexType(texFmt, texSiz), texIsPalette); + tex->ExtractFromBinary(texAddr, texWidth, texHeight, + TexFormatToTexType(texFmt, texSiz), texIsPalette); Globals::Instance->lastScene->parent->AddTextureResource(texAddr, tex); } diff --git a/ZAPD/ZDisplayList.h b/ZAPD/ZDisplayList.h index 4098b97..27d7c38 100644 --- a/ZAPD/ZDisplayList.h +++ b/ZAPD/ZDisplayList.h @@ -348,14 +348,14 @@ public: std::vector<ZMtx> mtxList; ZDisplayList(ZFile* nParent); - ZDisplayList(uint32_t rawDataIndex, int32_t rawDataSize, ZFile* nParent); ~ZDisplayList(); void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; + void ExtractFromBinary(uint32_t nRawDataIndex, int32_t rawDataSize); void ParseRawData() override; - Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr); + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; void TextureGenCheck(std::string prefix); static bool TextureGenCheck(ZFile* parent, std::string prefix, int32_t texWidth, diff --git a/ZAPD/ZFile.cpp b/ZAPD/ZFile.cpp index 8f74806..f209653 100644 --- a/ZAPD/ZFile.cpp +++ b/ZAPD/ZFile.cpp @@ -1,9 +1,11 @@ #include "ZFile.h" -#include <Utils/BinaryWriter.h> -#include <Utils/MemoryStream.h> + #include <algorithm> #include <cassert> #include <unordered_set> + +#include <Utils/BinaryWriter.h> +#include <Utils/MemoryStream.h> #include "Globals.h" #include "OutputFormatter.h" #include "Utils/Directory.h" @@ -26,8 +28,6 @@ #include "ZVector.h" #include "ZVtx.h" -using namespace tinyxml2; - ZFile::ZFile() { resources = std::vector<ZResource*>(); @@ -71,7 +71,7 @@ ZFile::~ZFile() } } -void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, +void ZFile::ParseXML(ZFileMode mode, tinyxml2::XMLElement* reader, std::string filename, [[maybe_unused]] bool placeholderMode) { if (filename == "") @@ -143,7 +143,7 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, auto nodeMap = *GetNodeMap(); uint32_t rawDataIndex = 0; - for (XMLElement* child = reader->FirstChildElement(); child != nullptr; + for (tinyxml2::XMLElement* child = reader->FirstChildElement(); child != nullptr; child = child->NextSiblingElement()) { const char* nameXml = child->Attribute("Name"); @@ -353,7 +353,7 @@ std::vector<ZResource*> ZFile::GetResourcesOfType(ZResourceType resType) return resList; } -Declaration* ZFile::AddDeclaration(uint32_t address, DeclarationAlignment alignment, size_t size, +Declaration* ZFile::AddDeclaration(offset_t address, DeclarationAlignment alignment, size_t size, std::string varType, std::string varName, std::string body) { assert(GETSEGNUM(address) == 0); @@ -376,7 +376,7 @@ Declaration* ZFile::AddDeclaration(uint32_t address, DeclarationAlignment alignm return decl; } -Declaration* ZFile::AddDeclarationArray(uint32_t address, DeclarationAlignment alignment, +Declaration* ZFile::AddDeclarationArray(offset_t address, DeclarationAlignment alignment, size_t size, std::string varType, std::string varName, size_t arrayItemCnt, std::string body) { @@ -405,7 +405,7 @@ Declaration* ZFile::AddDeclarationArray(uint32_t address, DeclarationAlignment a return decl; } -Declaration* ZFile::AddDeclarationArray(uint32_t address, DeclarationAlignment alignment, +Declaration* ZFile::AddDeclarationArray(offset_t address, DeclarationAlignment alignment, size_t size, std::string varType, std::string varName, std::string arrayItemCntStr, std::string body) { @@ -449,7 +449,7 @@ Declaration* ZFile::AddDeclarationPlaceholder(uint32_t address) return decl; } -Declaration* ZFile::AddDeclarationPlaceholder(uint32_t address, std::string varName) +Declaration* ZFile::AddDeclarationPlaceholder(offset_t address, std::string varName) { assert(GETSEGNUM(address) == 0); AddDeclarationDebugChecks(address); @@ -467,7 +467,7 @@ Declaration* ZFile::AddDeclarationPlaceholder(uint32_t address, std::string varN return decl; } -Declaration* ZFile::AddDeclarationInclude(uint32_t address, std::string includePath, size_t size, +Declaration* ZFile::AddDeclarationInclude(offset_t address, std::string includePath, size_t size, std::string varType, std::string varName) { assert(GETSEGNUM(address) == 0); @@ -489,7 +489,7 @@ Declaration* ZFile::AddDeclarationInclude(uint32_t address, std::string includeP return decl; } -Declaration* ZFile::AddDeclarationIncludeArray(uint32_t address, std::string includePath, +Declaration* ZFile::AddDeclarationIncludeArray(offset_t address, std::string includePath, size_t size, std::string varType, std::string varName, size_t arrayItemCnt) { diff --git a/ZAPD/ZFile.h b/ZAPD/ZFile.h index 00ec814..f23ab49 100644 --- a/ZAPD/ZFile.h +++ b/ZAPD/ZFile.h @@ -49,19 +49,19 @@ public: ZResource* FindResource(uint32_t rawDataIndex); std::vector<ZResource*> GetResourcesOfType(ZResourceType resType); - Declaration* AddDeclaration(uint32_t address, DeclarationAlignment alignment, size_t size, + Declaration* AddDeclaration(offset_t address, DeclarationAlignment alignment, size_t size, std::string varType, std::string varName, std::string body); - Declaration* AddDeclarationArray(uint32_t address, DeclarationAlignment alignment, size_t size, + Declaration* AddDeclarationArray(offset_t address, DeclarationAlignment alignment, size_t size, std::string varType, std::string varName, size_t arrayItemCnt, std::string body); - Declaration* AddDeclarationArray(uint32_t address, DeclarationAlignment alignment, size_t size, + Declaration* AddDeclarationArray(offset_t address, DeclarationAlignment alignment, size_t size, std::string varType, std::string varName, std::string arrayItemCntStr, std::string body); Declaration* AddDeclarationPlaceholder(uint32_t address); - Declaration* AddDeclarationPlaceholder(uint32_t address, std::string varName); - Declaration* AddDeclarationInclude(uint32_t address, std::string includePath, size_t size, + Declaration* AddDeclarationPlaceholder(offset_t address, std::string varName); + Declaration* AddDeclarationInclude(offset_t address, std::string includePath, size_t size, std::string varType, std::string varName); - Declaration* AddDeclarationIncludeArray(uint32_t address, std::string includePath, size_t size, + Declaration* AddDeclarationIncludeArray(offset_t address, std::string includePath, size_t size, std::string varType, std::string varName, size_t arrayItemCnt); std::string GetDeclarationName(uint32_t address) const; diff --git a/ZAPD/ZLimb.cpp b/ZAPD/ZLimb.cpp index 048f070..1d859a4 100644 --- a/ZAPD/ZLimb.cpp +++ b/ZAPD/ZLimb.cpp @@ -1,5 +1,7 @@ #include "ZLimb.h" + #include <cassert> + #include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/StringHelper.h" @@ -284,7 +286,8 @@ void Struct_800A5E28::DeclareReferences(const std::string& prefix) int32_t dlistLength = ZDisplayList::GetDListLength( parent->GetRawData(), unk_8_Offset, Globals::Instance->game == ZGame::OOT_SW97 ? DListType::F3DEX : DListType::F3DZEX); - unk_8_dlist = new ZDisplayList(unk_8_Offset, dlistLength, parent); + unk_8_dlist = new ZDisplayList(parent); + unk_8_dlist->ExtractFromBinary(unk_8_Offset, dlistLength); std::string dListStr = StringHelper::Sprintf("%sSkinLimbDL_%06X", prefix.c_str(), unk_8_Offset); @@ -354,27 +357,14 @@ ZLimb::ZLimb(ZFile* nParent) : ZResource(nParent) RegisterOptionalAttribute("Type"); } -ZLimb::ZLimb(ZLimbType limbType, const std::string& prefix, uint32_t nRawDataIndex, ZFile* nParent) - : ZLimb(nParent) +void ZLimb::ExtractFromBinary(uint32_t nRawDataIndex, ZLimbType nType) { rawDataIndex = nRawDataIndex; - parent = nParent; - type = limbType; - - name = StringHelper::Sprintf("%sLimb_%06X", prefix.c_str(), GetFileAddress()); + type = nType; ParseRawData(); } -void ZLimb::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - - Declaration* decl = parent->AddDeclaration(GetFileAddress(), DeclarationAlignment::Align4, - GetRawDataSize(), GetSourceTypeName(), name, ""); - decl->staticConf = staticConf; -} - void ZLimb::ParseXML(tinyxml2::XMLElement* reader) { ZResource::ParseXML(reader); @@ -480,8 +470,10 @@ void ZLimb::DeclareReferences(const std::string& prefix) uint32_t childOffset = Seg2Filespace(childPtr, parent->baseAddress); if (!parent->HasDeclaration(childOffset)) { - ZLimb* child = new ZLimb(ZLimbType::Legacy, prefix, childOffset, parent); - child->GetSourceOutputCode(prefix); + ZLimb* child = new ZLimb(parent); + child->ExtractFromBinary(childOffset, ZLimbType::Legacy); + child->DeclareVar(prefix, ""); + child->DeclareReferences(prefix); parent->AddResource(child); } } @@ -490,8 +482,10 @@ void ZLimb::DeclareReferences(const std::string& prefix) uint32_t siblingdOffset = Seg2Filespace(siblingPtr, parent->baseAddress); if (!parent->HasDeclaration(siblingdOffset)) { - ZLimb* sibling = new ZLimb(ZLimbType::Legacy, prefix, siblingdOffset, parent); - sibling->GetSourceOutputCode(prefix); + ZLimb* sibling = new ZLimb(parent); + sibling->ExtractFromBinary(siblingdOffset, ZLimbType::Legacy); + sibling->DeclareVar(prefix, ""); + sibling->DeclareReferences(prefix); parent->AddResource(sibling); } } @@ -531,12 +525,10 @@ size_t ZLimb::GetRawDataSize() const return 0x0C; } -std::string ZLimb::GetSourceOutputCode(const std::string& prefix) +std::string ZLimb::GetBodySourceCode() const { - std::string limbPrefix = type == ZLimbType::Curve ? "Curve" : ""; - std::string dListStr = GetLimbDListSourceOutputCode(prefix, limbPrefix, dListPtr); - limbPrefix = type == ZLimbType::Curve ? "Curve" : "Far"; - std::string dListStr2 = GetLimbDListSourceOutputCode(prefix, limbPrefix, dList2Ptr); + std::string dListStr = parent->GetDeclarationPtrName(dListPtr); + std::string dListStr2 = parent->GetDeclarationPtrName(dList2Ptr); std::string entryStr = "\n\t"; if (type == ZLimbType::Legacy) @@ -557,24 +549,27 @@ std::string ZLimb::GetSourceOutputCode(const std::string& prefix) { entryStr += StringHelper::Sprintf("{ %i, %i, %i }, ", transX, transY, transZ); } - entryStr += StringHelper::Sprintf("0x%02X, 0x%02X,\n", childIndex, siblingIndex); switch (type) { case ZLimbType::Standard: - entryStr += StringHelper::Sprintf(" %s\n", dListStr.c_str()); + entryStr += StringHelper::Sprintf("\t%s\n", dListStr.c_str()); break; case ZLimbType::LOD: case ZLimbType::Curve: entryStr += - StringHelper::Sprintf(" { %s, %s }\n", dListStr.c_str(), dListStr2.c_str()); + StringHelper::Sprintf("\t{ %s, %s }\n", dListStr.c_str(), dListStr2.c_str()); break; case ZLimbType::Skin: - entryStr += GetSourceOutputCodeSkin(prefix); - break; + { + std::string skinSegmentStr = parent->GetDeclarationPtrName(skinSegment); + entryStr += + StringHelper::Sprintf("\t0x%02X, %s\n", skinSegmentType, skinSegmentStr.c_str()); + } + break; case ZLimbType::Legacy: break; @@ -584,16 +579,12 @@ std::string ZLimb::GetSourceOutputCode(const std::string& prefix) } } - Declaration* decl = parent->GetDeclaration(GetFileAddress()); - - if (decl == nullptr) - decl = parent->AddDeclaration(GetFileAddress(), DeclarationAlignment::Align4, - GetRawDataSize(), GetSourceTypeName(), name, entryStr); - else - decl->text = entryStr; - decl->staticConf = staticConf; + return entryStr; +} - return ""; +std::string ZLimb::GetDefaultName(const std::string& prefix) const +{ + return StringHelper::Sprintf("%sLimb_%06X", prefix.c_str(), rawDataIndex); } std::string ZLimb::GetSourceTypeName() const @@ -665,11 +656,6 @@ ZLimbType ZLimb::GetTypeByAttributeName(const std::string& attrName) return ZLimbType::Invalid; } -uint32_t ZLimb::GetFileAddress() -{ - return Seg2Filespace(rawDataIndex, parent->baseAddress); -} - // Returns the ptrname of a dlist. Declares it if it has not been declared yet. std::string ZLimb::GetLimbDListSourceOutputCode(const std::string& prefix, const std::string& limbPrefix, segptr_t dListPtr) @@ -711,7 +697,8 @@ std::string ZLimb::GetLimbDListSourceOutputCode(const std::string& prefix, int32_t dlistLength = ZDisplayList::GetDListLength( parent->GetRawData(), dListOffset, Globals::Instance->game == ZGame::OOT_SW97 ? DListType::F3DEX : DListType::F3DZEX); - auto dList = new ZDisplayList(dListOffset, dlistLength, parent); + auto dList = new ZDisplayList(parent); + dList->ExtractFromBinary(dListOffset, dlistLength); dList->SetName(dListStr); dList->GetSourceOutputCode(prefix); return dListStr; diff --git a/ZAPD/ZLimb.h b/ZAPD/ZLimb.h index affc7b4..cd89c19 100644 --- a/ZAPD/ZLimb.h +++ b/ZAPD/ZLimb.h @@ -147,16 +147,17 @@ public: uint8_t childIndex, siblingIndex; ZLimb(ZFile* nParent); - ZLimb(ZLimbType limbType, const std::string& prefix, uint32_t nRawDataIndex, ZFile* nParent); - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; + void ExtractFromBinary(uint32_t nRawDataIndex, ZLimbType nType); void ParseXML(tinyxml2::XMLElement* reader) override; void ParseRawData() override; void DeclareReferences(const std::string& prefix) override; + std::string GetBodySourceCode() const override; + std::string GetDefaultName(const std::string& prefix) const override; + size_t GetRawDataSize() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; @@ -164,7 +165,4 @@ public: void SetLimbType(ZLimbType value); static const char* GetSourceTypeName(ZLimbType limbType); static ZLimbType GetTypeByAttributeName(const std::string& attrName); - - uint32_t GetFileAddress(); - void SetFileAddress(uint32_t nAddress); }; diff --git a/ZAPD/ZMtx.cpp b/ZAPD/ZMtx.cpp index 7febfaf..b9ca3f2 100644 --- a/ZAPD/ZMtx.cpp +++ b/ZAPD/ZMtx.cpp @@ -1,4 +1,5 @@ #include "ZMtx.h" + #include "Utils/BitConverter.h" #include "Utils/StringHelper.h" #include "ZFile.h" @@ -9,13 +10,6 @@ ZMtx::ZMtx(ZFile* nParent) : ZResource(nParent) { } -ZMtx::ZMtx(const std::string& prefix, uint32_t nRawDataIndex, ZFile* nParent) : ZResource(nParent) -{ - name = GetDefaultName(prefix.c_str(), rawDataIndex); - ExtractFromFile(nRawDataIndex); - DeclareVar("", ""); -} - void ZMtx::ParseRawData() { ZResource::ParseRawData(); @@ -26,30 +20,11 @@ void ZMtx::ParseRawData() mtx[i][j] = BitConverter::ToInt32BE(rawData, rawDataIndex + (i * 4 + j) * 4); } -void ZMtx::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - DeclareVar("", ""); -} - size_t ZMtx::GetRawDataSize() const { return 64; } -void ZMtx::DeclareVar(const std::string& prefix, const std::string& bodyStr) const -{ - std::string auxName = name; - - if (name == "") - auxName = GetDefaultName(prefix, rawDataIndex); - - Declaration* decl = - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align8, GetRawDataSize(), - GetSourceTypeName(), auxName, bodyStr); - decl->staticConf = staticConf; -} - std::string ZMtx::GetBodySourceCode() const { std::string bodyStr = "\n"; @@ -67,25 +42,6 @@ std::string ZMtx::GetBodySourceCode() const return bodyStr; } -std::string ZMtx::GetSourceOutputCode(const std::string& prefix) -{ - std::string bodyStr = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(rawDataIndex); - - if (decl == nullptr) - DeclareVar(prefix, bodyStr); - else - decl->text = bodyStr; - - return ""; -} - -std::string ZMtx::GetDefaultName(const std::string& prefix, uint32_t address) -{ - return StringHelper::Sprintf("%sMtx_%06X", prefix.c_str(), address); -} - std::string ZMtx::GetSourceTypeName() const { return "Mtx"; @@ -95,3 +51,8 @@ ZResourceType ZMtx::GetResourceType() const { return ZResourceType::Mtx; } + +DeclarationAlignment ZMtx::GetDeclarationAlignment() const +{ + return DeclarationAlignment::Align8; +} diff --git a/ZAPD/ZMtx.h b/ZAPD/ZMtx.h index 5a4d1ef..eed5023 100644 --- a/ZAPD/ZMtx.h +++ b/ZAPD/ZMtx.h @@ -8,22 +8,17 @@ class ZMtx : public ZResource { public: ZMtx(ZFile* nParent); - ZMtx(const std::string& prefix, uint32_t nRawDataIndex, ZFile* nParent); void ParseRawData() override; - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; - - size_t GetRawDataSize() const override; - - void DeclareVar(const std::string& prefix, const std::string& bodyStr) const; std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; - static std::string GetDefaultName(const std::string& prefix, uint32_t address); std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; + size_t GetRawDataSize() const override; + DeclarationAlignment GetDeclarationAlignment() const override; + protected: std::array<std::array<int32_t, 4>, 4> mtx; }; diff --git a/ZAPD/ZPath.cpp b/ZAPD/ZPath.cpp index b57b3cc..7311a4a 100644 --- a/ZAPD/ZPath.cpp +++ b/ZAPD/ZPath.cpp @@ -13,16 +13,6 @@ ZPath::ZPath(ZFile* nParent) : ZResource(nParent) RegisterOptionalAttribute("NumPaths", "1"); } -void ZPath::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - - Declaration* decl = - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, pathways.size() * 8, - GetSourceTypeName(), name, pathways.size(), ""); - decl->staticConf = staticConf; -} - void ZPath::ParseXML(tinyxml2::XMLElement* reader) { ZResource::ParseXML(reader); @@ -45,8 +35,7 @@ void ZPath::ParseRawData() for (size_t pathIndex = 0; pathIndex < numPaths; pathIndex++) { PathwayEntry path(parent); - path.SetRawDataIndex(currentPtr); - path.ParseRawData(); + path.ExtractFromFile(currentPtr); if (path.GetListAddress() == 0) break; @@ -64,6 +53,20 @@ void ZPath::DeclareReferences(const std::string& prefix) entry.DeclareReferences(prefix); } +Declaration* ZPath::DeclareVar(const std::string& prefix, const std::string& bodyStr) +{ + std::string auxName = name; + + if (name == "") + auxName = GetDefaultName(prefix); + + Declaration* decl = + parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), + GetSourceTypeName(), name, pathways.size(), bodyStr); + decl->staticConf = staticConf; + return decl; +} + std::string ZPath::GetBodySourceCode() const { std::string declaration = ""; @@ -82,22 +85,6 @@ std::string ZPath::GetBodySourceCode() const return declaration; } -std::string ZPath::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) -{ - std::string declaration = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(rawDataIndex); - if (decl == nullptr || decl->isPlaceholder) - decl = parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, - pathways.size() * 8, GetSourceTypeName(), name, - pathways.size(), declaration); - else - decl->text = declaration; - decl->staticConf = staticConf; - - return ""; -} - std::string ZPath::GetSourceTypeName() const { return "Path"; @@ -138,10 +125,7 @@ void PathwayEntry::ParseRawData() for (int32_t i = 0; i < numPoints; i++) { ZVector vec(parent); - vec.SetRawDataIndex(currentPtr); - vec.SetScalarType(ZScalarType::ZSCALAR_S16); - vec.SetDimensions(3); - vec.ParseRawData(); + vec.ExtractFromBinary(currentPtr, ZScalarType::ZSCALAR_S16, 3); currentPtr += vec.GetRawDataSize(); points.push_back(vec); @@ -154,12 +138,14 @@ void PathwayEntry::DeclareReferences(const std::string& prefix) if (points.empty()) return; + std::string pointsName = ""; + std::string declaration = ""; size_t index = 0; for (const auto& point : points) { - declaration += StringHelper::Sprintf("\t%s,", point.GetBodySourceCode().c_str()); + declaration += StringHelper::Sprintf("\t{ %s },", point.GetBodySourceCode().c_str()); if (index < points.size() - 1) declaration += "\n"; @@ -167,17 +153,11 @@ void PathwayEntry::DeclareReferences(const std::string& prefix) index++; } - Declaration* decl = parent->GetDeclaration(GETSEGOFFSET(listSegmentAddress)); - if (decl == nullptr) - { - parent->AddDeclarationArray(GETSEGOFFSET(listSegmentAddress), DeclarationAlignment::Align4, - points.size() * 6, points.at(0).GetSourceTypeName(), - StringHelper::Sprintf("%sPathwayList0x%06X", prefix.c_str(), - GETSEGOFFSET(listSegmentAddress)), - points.size(), declaration); - } - else - decl->text = declaration; + uint32_t pointsOffset = Seg2Filespace(listSegmentAddress, parent->baseAddress); + pointsName = StringHelper::Sprintf("%sPathwayList_%06X", prefix.c_str(), pointsOffset); + parent->AddDeclarationArray(pointsOffset, points.at(0).GetDeclarationAlignment(), + points.size() * 6, points.at(0).GetSourceTypeName(), pointsName, + points.size(), declaration); } std::string PathwayEntry::GetBodySourceCode() const diff --git a/ZAPD/ZPath.h b/ZAPD/ZPath.h index 21394fa..e519dec 100644 --- a/ZAPD/ZPath.h +++ b/ZAPD/ZPath.h @@ -32,14 +32,12 @@ class ZPath : public ZResource public: ZPath(ZFile* nParent); - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; - void ParseXML(tinyxml2::XMLElement* reader) override; void ParseRawData() override; void DeclareReferences(const std::string& prefix) override; + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; diff --git a/ZAPD/ZResource.cpp b/ZAPD/ZResource.cpp index 55861ef..c17f89a 100644 --- a/ZAPD/ZResource.cpp +++ b/ZAPD/ZResource.cpp @@ -2,6 +2,7 @@ #include <cassert> #include <regex> + #include "Utils/StringHelper.h" #include "ZFile.h" @@ -22,18 +23,26 @@ ZResource::ZResource(ZFile* nParent) RegisterOptionalAttribute("Static", "Global"); } -void ZResource::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) +void ZResource::ExtractFromXML(tinyxml2::XMLElement* reader, offset_t nRawDataIndex) { rawDataIndex = nRawDataIndex; + declaredInXml = true; if (reader != nullptr) ParseXML(reader); ParseRawData(); CalcHash(); + + if (!isInner) + { + Declaration* decl = DeclareVar(parent->GetName(), ""); + assert(decl != nullptr); + decl->declaredInXml = true; + } } -void ZResource::ExtractFromFile(uint32_t nRawDataIndex) +void ZResource::ExtractFromFile(offset_t nRawDataIndex) { rawDataIndex = nRawDataIndex; @@ -149,6 +158,20 @@ void ZResource::DeclareReferencesLate([[maybe_unused]] const std::string& prefix { } +Declaration* ZResource::DeclareVar(const std::string& prefix, const std::string& bodyStr) +{ + std::string auxName = name; + + if (name == "") + auxName = GetDefaultName(prefix); + + Declaration* decl = + parent->AddDeclaration(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), + GetSourceTypeName(), auxName, bodyStr); + decl->staticConf = staticConf; + return decl; +} + void ZResource::Save([[maybe_unused]] const fs::path& outFolder) { } @@ -188,28 +211,43 @@ std::string ZResource::GetExternalExtension() const return ""; } +DeclarationAlignment ZResource::GetDeclarationAlignment() const +{ + return DeclarationAlignment::Align4; +} + bool ZResource::WasDeclaredInXml() const { return declaredInXml; } -uint32_t ZResource::GetRawDataIndex() const +offset_t ZResource::GetRawDataIndex() const { return rawDataIndex; } -void ZResource::SetRawDataIndex(uint32_t value) +std::string ZResource::GetBodySourceCode() const { - rawDataIndex = value; + return "ERROR"; } -std::string ZResource::GetBodySourceCode() const +std::string ZResource::GetDefaultName(const std::string& prefix) const { - return "ERROR"; + return StringHelper::Sprintf("%s%s_%06X", prefix.c_str(), GetSourceTypeName().c_str(), + rawDataIndex); } std::string ZResource::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) { + std::string bodyStr = GetBodySourceCode(); + + Declaration* decl = parent->GetDeclaration(rawDataIndex); + if (decl == nullptr || decl->isPlaceholder) + decl = DeclareVar(prefix, bodyStr); + else + decl->text = bodyStr; + decl->staticConf = staticConf; + return ""; } @@ -249,9 +287,9 @@ void ZResource::RegisterOptionalAttribute(const std::string& attr, const std::st registeredAttributes[attr] = resAtrr; } -uint32_t Seg2Filespace(segptr_t segmentedAddress, uint32_t parentBaseAddress) +offset_t Seg2Filespace(segptr_t segmentedAddress, uint32_t parentBaseAddress) { - uint32_t currentPtr = GETSEGOFFSET(segmentedAddress); + offset_t currentPtr = GETSEGOFFSET(segmentedAddress); if (GETSEGNUM(segmentedAddress) == 0x80) // Is defined in code? { diff --git a/ZAPD/ZResource.h b/ZAPD/ZResource.h index 46306b0..633520a 100644 --- a/ZAPD/ZResource.h +++ b/ZAPD/ZResource.h @@ -21,8 +21,6 @@ #define GETSEGOFFSET(x) (x & 0x00FFFFFF) #define GETSEGNUM(x) ((x >> 24) & 0xFF) -typedef uint32_t segptr_t; - class ZFile; enum class ZResourceType @@ -70,53 +68,124 @@ public: bool outputDeclaration = true; uint32_t hash = 0; + /** + * Constructor. + * Child classes should not declare any other constructor besides this one + */ ZResource(ZFile* nParent); virtual ~ZResource() = default; // Parsing from File - virtual void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex); - virtual void ExtractFromFile(uint32_t nRawDataIndex); + virtual void ExtractFromXML(tinyxml2::XMLElement* reader, offset_t nRawDataIndex); + virtual void ExtractFromFile(offset_t nRawDataIndex); // Misc + /** + * Parses additional attributes of the XML node. + * Extra attritbutes have to be registered using `RegisterRequiredAttribute` or + * `RegisterOptionalAttribute` in the constructor of the ZResource + */ virtual void ParseXML(tinyxml2::XMLElement* reader); + /** + * Extracts data from the binary file + */ virtual void ParseRawData(); + /** + * Declares any data pointed by this resource that has not been declared already. + * For example, a Vtx referenced by a Dlist should be declared here if it wasn't + * declared previously by something else + */ virtual void DeclareReferences(const std::string& prefix); virtual void ParseRawDataLate(); virtual void DeclareReferencesLate(const std::string& prefix); - virtual std::string GetBodySourceCode() const; + + /** + * Adds this resource as a Declaration of its parent ZFile + */ + virtual Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr); + /** + * Returns the body of the variable of the extracted resource, without any side-effect + */ + [[nodiscard]] virtual std::string GetBodySourceCode() const; + /** + * Creates an automatically generated variable name for the current resource + */ + [[nodiscard]] virtual std::string GetDefaultName(const std::string& prefix) const; virtual std::string GetSourceOutputCode(const std::string& prefix); virtual std::string GetSourceOutputHeader(const std::string& prefix); virtual void CalcHash(); + /** + * Exports the resource to binary format + */ virtual void Save(const fs::path& outFolder); // Properties + /** + * Returns true if the resource will be externalized, and included back to the C file using + * `#include`s + */ virtual bool IsExternalResource() const; - virtual bool DoesSupportArray() const; // Can this type be wrapped in an <Array> node? - virtual std::string GetSourceTypeName() const = 0; - virtual ZResourceType GetResourceType() const = 0; - virtual std::string GetExternalExtension() const; + /** + * Can this type be wrapped in an <Array> node? + */ + virtual bool DoesSupportArray() const; + /** + * The type of the resource as a C struct + */ + [[nodiscard]] virtual std::string GetSourceTypeName() const = 0; + /** + * The type in the ZResource enum + */ + [[nodiscard]] virtual ZResourceType GetResourceType() const = 0; + /** + * The filename extension for assets extracted as standalone files + */ + [[nodiscard]] virtual std::string GetExternalExtension() const; // Getters/Setters - const std::string& GetName() const; + [[nodiscard]] const std::string& GetName() const; void SetName(const std::string& nName); - const std::string& GetOutName() const; + [[nodiscard]] const std::string& GetOutName() const; void SetOutName(const std::string& nName); - virtual uint32_t GetRawDataIndex() const; - virtual void SetRawDataIndex(uint32_t value); - virtual size_t GetRawDataSize() const = 0; + [[nodiscard]] offset_t GetRawDataIndex() const; + /** + * The size of the current struct being extracted, not counting data referenced by it + */ + [[nodiscard]] virtual size_t GetRawDataSize() const = 0; + /** + * The alignment of the extracted struct + */ + [[nodiscard]] virtual DeclarationAlignment GetDeclarationAlignment() const; void SetInnerNode(bool inner); - bool WasDeclaredInXml() const; + /** + * Returns `true` if this ZResource was declared using an XML node, + * `false` otherwise (for example, a Vtx extracted indirectly by a DList) + */ + [[nodiscard]] bool WasDeclaredInXml() const; protected: std::string name; std::string outName; - uint32_t rawDataIndex; + offset_t rawDataIndex; std::string sourceOutput; - bool isInner = false; // Is this resource an inner node of another resource? inside of <Array> - bool canHaveInner = false; // Can this type have an inner node? - 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 + + // Inner is used mostly for <Array> nodes + /** + * Is this resource an inner node of another resource? + * (namely inside an <Array>) + */ + bool isInner = false; + /** + * Can this type have an inner node? + */ + bool canHaveInner = false; + + /** + * 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 isCustomAsset; bool declaredInXml = false; StaticConfig staticConf = StaticConfig::Global; @@ -142,7 +211,7 @@ public: virtual void Save(ZResource* res, fs::path outPath, BinaryWriter* writer) = 0; }; -uint32_t Seg2Filespace(segptr_t segmentedAddress, uint32_t parentBaseAddress); +offset_t Seg2Filespace(segptr_t segmentedAddress, uint32_t parentBaseAddress); typedef ZResource*(ZResourceFactoryFunc)(ZFile* nParent); diff --git a/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.cpp b/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.cpp index b1b3ed0..4d588c0 100644 --- a/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.cpp +++ b/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.cpp @@ -20,8 +20,7 @@ SetAnimatedMaterialList::SetAnimatedMaterialList(ZFile* nParent) void SetAnimatedMaterialList::ParseRawData() { ZRoomCommand::ParseRawData(); - textureAnimation.SetRawDataIndex(segmentOffset); - textureAnimation.ParseRawData(); + textureAnimation.ExtractFromFile(segmentOffset); } void SetAnimatedMaterialList::DeclareReferences(const std::string& prefix) diff --git a/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.h b/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.h index fecb9f2..0f6cff3 100644 --- a/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.h +++ b/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.h @@ -1,6 +1,7 @@ #pragma once #include <memory> + #include "ZRoom/ZRoomCommand.h" #include "ZTextureAnimation.h" diff --git a/ZAPD/ZRoom/Commands/SetCollisionHeader.cpp b/ZAPD/ZRoom/Commands/SetCollisionHeader.cpp index 22273e0..96196a5 100644 --- a/ZAPD/ZRoom/Commands/SetCollisionHeader.cpp +++ b/ZAPD/ZRoom/Commands/SetCollisionHeader.cpp @@ -13,10 +13,9 @@ void SetCollisionHeader::ParseRawData() { ZRoomCommand::ParseRawData(); collisionHeader = new ZCollisionHeader(parent); - collisionHeader->SetRawDataIndex(segmentOffset); collisionHeader->SetName( StringHelper::Sprintf("%sCollisionHeader_%06X", parent->GetName().c_str(), segmentOffset)); - collisionHeader->ParseRawData(); + collisionHeader->ExtractFromFile(segmentOffset); } SetCollisionHeader::~SetCollisionHeader() diff --git a/ZAPD/ZRoom/Commands/SetCsCamera.cpp b/ZAPD/ZRoom/Commands/SetCsCamera.cpp index 1682276..72d3ce8 100644 --- a/ZAPD/ZRoom/Commands/SetCsCamera.cpp +++ b/ZAPD/ZRoom/Commands/SetCsCamera.cpp @@ -33,10 +33,7 @@ void SetCsCamera::ParseRawData() for (int32_t i = 0; i < numPoints; i++) { ZVector vec(parent); - vec.SetRawDataIndex(currentPtr); - vec.SetScalarType(ZScalarType::ZSCALAR_S16); - vec.SetDimensions(3); - vec.ParseRawData(); + vec.ExtractFromBinary(currentPtr, ZScalarType::ZSCALAR_S16, 3); currentPtr += vec.GetRawDataSize(); points.push_back(vec); diff --git a/ZAPD/ZRoom/Commands/SetCutscenes.cpp b/ZAPD/ZRoom/Commands/SetCutscenes.cpp index 34707a2..6b1c301 100644 --- a/ZAPD/ZRoom/Commands/SetCutscenes.cpp +++ b/ZAPD/ZRoom/Commands/SetCutscenes.cpp @@ -46,8 +46,9 @@ void SetCutscenes::ParseRawData() cutsceneEntries.push_back(entry); currentPtr += 8; + // TODO: don't hardcode %sCutsceneData_%06X, look up for the declared name instead declaration += StringHelper::Sprintf( - " { %sCutsceneData0x%06X, 0x%04X, 0x%02X, 0x%02X },", zRoom->GetName().c_str(), + " { %sCutsceneData_%06X, 0x%04X, 0x%02X, 0x%02X },", zRoom->GetName().c_str(), entry.segmentOffset, entry.exit, entry.entrance, entry.flag); if (i < numCutscenes - 1) @@ -67,9 +68,9 @@ void SetCutscenes::ParseRawData() for (ZCutsceneBase* cutscene : cutscenes) { - if (cutscene->getSegmentOffset() != 0) + if (cutscene->GetRawDataIndex() != 0) { - Declaration* decl = parent->GetDeclaration(cutscene->getSegmentOffset()); + Declaration* decl = parent->GetDeclaration(cutscene->GetRawDataIndex()); if (decl == nullptr) { cutscene->GetSourceOutputCode(zRoom->GetName()); diff --git a/ZAPD/ZRoom/Commands/SetEntranceList.cpp b/ZAPD/ZRoom/Commands/SetEntranceList.cpp index 6043818..0495d53 100644 --- a/ZAPD/ZRoom/Commands/SetEntranceList.cpp +++ b/ZAPD/ZRoom/Commands/SetEntranceList.cpp @@ -1,4 +1,5 @@ #include "SetEntranceList.h" + #include "SetStartPositionList.h" #include "Utils/BitConverter.h" #include "Utils/StringHelper.h" diff --git a/ZAPD/ZRoom/Commands/SetMesh.cpp b/ZAPD/ZRoom/Commands/SetMesh.cpp index 024f054..a546996 100644 --- a/ZAPD/ZRoom/Commands/SetMesh.cpp +++ b/ZAPD/ZRoom/Commands/SetMesh.cpp @@ -156,7 +156,9 @@ ZDisplayList* PolygonDlist::MakeDlist(segptr_t ptr, [[maybe_unused]] const std:: int32_t dlistLength = ZDisplayList::GetDListLength( parent->GetRawData(), dlistAddress, Globals::Instance->game == ZGame::OOT_SW97 ? DListType::F3DEX : DListType::F3DZEX); - ZDisplayList* dlist = new ZDisplayList(dlistAddress, dlistLength, parent); + ZDisplayList* dlist = new ZDisplayList(parent); + dlist->ExtractFromBinary(dlistAddress, dlistLength); + dlist->SetName(dlist->GetDefaultName(prefix)); GenDListDeclarations(zRoom, parent, dlist); return dlist; @@ -304,7 +306,13 @@ ZBackground* BgImage::MakeBackground(segptr_t ptr, const std::string& prefix) uint32_t backAddress = Seg2Filespace(ptr, parent->baseAddress); - ZBackground* background = new ZBackground(prefix, backAddress, parent); + ZBackground* background = new ZBackground(parent); + background->ExtractFromFile(backAddress); + + std::string defaultName = background->GetDefaultName(prefix); + background->SetName(defaultName); + background->SetOutName(defaultName); + background->DeclareVar(prefix, ""); parent->resources.push_back(background); diff --git a/ZAPD/ZRoom/Commands/SetPathways.cpp b/ZAPD/ZRoom/Commands/SetPathways.cpp index 0f7b4be..1649685 100644 --- a/ZAPD/ZRoom/Commands/SetPathways.cpp +++ b/ZAPD/ZRoom/Commands/SetPathways.cpp @@ -1,4 +1,5 @@ #include "SetPathways.h" + #include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/StringHelper.h" @@ -23,8 +24,7 @@ void SetPathways::ParseRawDataLate() pathwayList.SetNumPaths(numPaths); } - pathwayList.SetRawDataIndex(segmentOffset); - pathwayList.ParseRawData(); + pathwayList.ExtractFromFile(segmentOffset); } void SetPathways::DeclareReferencesLate(const std::string& prefix) diff --git a/ZAPD/ZRoom/Commands/SetPathways.h b/ZAPD/ZRoom/Commands/SetPathways.h index cc754f7..9abc93d 100644 --- a/ZAPD/ZRoom/Commands/SetPathways.h +++ b/ZAPD/ZRoom/Commands/SetPathways.h @@ -17,8 +17,6 @@ public: void ParseRawDataLate() override; void DeclareReferencesLate(const std::string& prefix) override; - void DeclareVar(const std::string& prefix, const std::string& bodyStr); - std::string GetBodySourceCode() const override; RoomCommand GetRoomCommand() const override; diff --git a/ZAPD/ZRoom/Commands/SetRoomBehavior.cpp b/ZAPD/ZRoom/Commands/SetRoomBehavior.cpp index 426804b..8c6f4bf 100644 --- a/ZAPD/ZRoom/Commands/SetRoomBehavior.cpp +++ b/ZAPD/ZRoom/Commands/SetRoomBehavior.cpp @@ -1,4 +1,5 @@ #include "SetRoomBehavior.h" + #include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/StringHelper.h" diff --git a/ZAPD/ZRoom/Commands/SetRoomList.cpp b/ZAPD/ZRoom/Commands/SetRoomList.cpp index 6d697fa..d86a511 100644 --- a/ZAPD/ZRoom/Commands/SetRoomList.cpp +++ b/ZAPD/ZRoom/Commands/SetRoomList.cpp @@ -16,9 +16,8 @@ void SetRoomList::ParseRawData() int numRooms = cmdArg1; romfile = new RomFile(parent); - romfile->SetRawDataIndex(segmentOffset); romfile->numRooms = numRooms; - romfile->ParseRawData(); + romfile->ExtractFromFile(segmentOffset); parent->resources.push_back(romfile); @@ -78,15 +77,15 @@ void RomFile::ParseRawData() } } -void RomFile::DeclareVar(const std::string& prefix, const std::string body) +Declaration* RomFile::DeclareVar(const std::string& prefix, const std::string& body) { std::string auxName = name; if (name == "") auxName = StringHelper::Sprintf("%sRoomList0x%06X", prefix.c_str(), rawDataIndex); - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, - rooms.size() * rooms.at(0).GetRawDataSize(), GetSourceTypeName(), - auxName, 0, body); + return parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, + rooms.size() * rooms.at(0).GetRawDataSize(), + GetSourceTypeName(), auxName, 0, body); } std::string RomFile::GetBodySourceCode() const diff --git a/ZAPD/ZRoom/Commands/SetRoomList.h b/ZAPD/ZRoom/Commands/SetRoomList.h index ae20351..d4edc80 100644 --- a/ZAPD/ZRoom/Commands/SetRoomList.h +++ b/ZAPD/ZRoom/Commands/SetRoomList.h @@ -22,7 +22,7 @@ public: void ParseXML(tinyxml2::XMLElement* reader) override; void ParseRawData() override; - void DeclareVar(const std::string& prefix, const std::string body); + Declaration* DeclareVar(const std::string& prefix, const std::string& body) override; std::string GetBodySourceCode() const override; std::string GetSourceOutputCode(const std::string& prefix) override; diff --git a/ZAPD/ZRoom/ZRoom.cpp b/ZAPD/ZRoom/ZRoom.cpp index 0af2670..97297ab 100644 --- a/ZAPD/ZRoom/ZRoom.cpp +++ b/ZAPD/ZRoom/ZRoom.cpp @@ -293,7 +293,7 @@ void ZRoom::DeclareReferencesLate(const std::string& prefix) cmd->DeclareReferencesLate(prefix); } -void ZRoom::DeclareVar(const std::string& prefix, const std::string body) +Declaration* ZRoom::DeclareVar(const std::string& prefix, const std::string& body) { std::string auxName = name; if (auxName == "") @@ -302,9 +302,10 @@ void ZRoom::DeclareVar(const std::string& prefix, const std::string body) auxName = StringHelper::Sprintf("%sCommands", name.c_str()); Declaration* decl = - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), + parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), GetSourceTypeName(), auxName, 0, body); decl->staticConf = staticConf; + return decl; } std::string ZRoom::GetBodySourceCode() const diff --git a/ZAPD/ZRoom/ZRoom.h b/ZAPD/ZRoom/ZRoom.h index 993baaf..6c64a57 100644 --- a/ZAPD/ZRoom/ZRoom.h +++ b/ZAPD/ZRoom/ZRoom.h @@ -33,17 +33,16 @@ public: void ParseRawDataLate() override; void DeclareReferencesLate(const std::string& prefix) override; - void DeclareVar(const std::string& prefix, const std::string body); + Declaration* DeclareVar(const std::string& prefix, const std::string& body) override; std::string GetBodySourceCode() const override; - std::string GetDefaultName(const std::string& prefix) const; + std::string GetDefaultName(const std::string& prefix) const override; size_t GetDeclarationSizeFromNeighbor(uint32_t declarationAddress); size_t GetCommandSizeFromNeighbor(ZRoomCommand* cmd); ZRoomCommand* FindCommandOfType(RoomCommand cmdType); - std::string GetSourceTypeName() const override; - size_t GetRawDataSize() const override; + std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; protected: diff --git a/ZAPD/ZRoom/ZRoomCommand.cpp b/ZAPD/ZRoom/ZRoomCommand.cpp index 3f5876e..711c86a 100644 --- a/ZAPD/ZRoom/ZRoomCommand.cpp +++ b/ZAPD/ZRoom/ZRoomCommand.cpp @@ -27,11 +27,6 @@ void ZRoomCommand::ParseRawData() segmentOffset = Seg2Filespace(cmdArg2, parent->baseAddress); } -std::string ZRoomCommand::GetSourceTypeName() const -{ - return "SCmdBase"; -} - RoomCommand ZRoomCommand::GetRoomCommand() const { return RoomCommand::Error; @@ -42,6 +37,11 @@ size_t ZRoomCommand::GetRawDataSize() const return 0x08; } +std::string ZRoomCommand::GetSourceTypeName() const +{ + return GetCommandCName(); +} + ZResourceType ZRoomCommand::GetResourceType() const { return ZResourceType::RoomCommand; diff --git a/ZAPD/ZRoom/ZRoomCommand.h b/ZAPD/ZRoom/ZRoomCommand.h index b79dc3e..e402998 100644 --- a/ZAPD/ZRoom/ZRoomCommand.h +++ b/ZAPD/ZRoom/ZRoomCommand.h @@ -57,7 +57,7 @@ public: uint32_t cmdIndex; uint32_t commandSet; RoomCommand cmdID; - uint32_t segmentOffset; + offset_t segmentOffset; ZRoomCommand(ZFile* nParent); virtual ~ZRoomCommand() = default; diff --git a/ZAPD/ZScalar.cpp b/ZAPD/ZScalar.cpp index b4c0f13..87e206a 100644 --- a/ZAPD/ZScalar.cpp +++ b/ZAPD/ZScalar.cpp @@ -1,4 +1,5 @@ #include "ZScalar.h" + #include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/File.h" @@ -14,9 +15,12 @@ ZScalar::ZScalar(ZFile* nParent) : ZResource(nParent) RegisterRequiredAttribute("Type"); } -ZScalar::ZScalar(const ZScalarType scalarType, ZFile* nParent) : ZScalar(nParent) +void ZScalar::ExtractFromBinary(uint32_t nRawDataIndex, ZScalarType nScalarType) { - this->scalarType = scalarType; + rawDataIndex = nRawDataIndex; + scalarType = nScalarType; + + ParseRawData(); } void ZScalar::ParseXML(tinyxml2::XMLElement* reader) @@ -247,16 +251,6 @@ std::string ZScalar::GetBodySourceCode() const } } -std::string ZScalar::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) -{ - Declaration* decl = - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), GetName(), GetBodySourceCode()); - decl->staticConf = staticConf; - - return ""; -} - ZResourceType ZScalar::GetResourceType() const { return ZResourceType::Scalar; @@ -266,3 +260,16 @@ bool ZScalar::DoesSupportArray() const { return true; } + +DeclarationAlignment ZScalar::GetDeclarationAlignment() const +{ + switch (scalarType) + { + case ZScalarType::ZSCALAR_S64: + case ZScalarType::ZSCALAR_U64: + case ZScalarType::ZSCALAR_F64: + return DeclarationAlignment::Align8; + default: + return DeclarationAlignment::Align4; + } +} diff --git a/ZAPD/ZScalar.h b/ZAPD/ZScalar.h index ca20827..d269995 100644 --- a/ZAPD/ZScalar.h +++ b/ZAPD/ZScalar.h @@ -44,25 +44,25 @@ class ZScalar : public ZResource friend class ZVector; public: + ZScalarData scalarData; + ZScalarType scalarType; + ZScalar(ZFile* nParent); - ZScalar(const ZScalarType scalarType, ZFile* nParent); + + void ExtractFromBinary(uint32_t nRawDataIndex, ZScalarType nScalarType); void ParseRawData() override; void ParseXML(tinyxml2::XMLElement* reader) override; std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; bool DoesSupportArray() const override; std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; size_t GetRawDataSize() const override; + DeclarationAlignment GetDeclarationAlignment() const override; static size_t MapTypeToSize(const ZScalarType scalarType); static ZScalarType MapOutputTypeToScalarType(const std::string& type); static std::string MapScalarTypeToOutputType(const ZScalarType scalarType); - -protected: - ZScalarData scalarData; - ZScalarType scalarType; }; diff --git a/ZAPD/ZSkeleton.cpp b/ZAPD/ZSkeleton.cpp index 5de5626..6eb06d9 100644 --- a/ZAPD/ZSkeleton.cpp +++ b/ZAPD/ZSkeleton.cpp @@ -1,6 +1,8 @@ #include "ZSkeleton.h" #include <cassert> + +#include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/StringHelper.h" @@ -13,15 +15,6 @@ ZSkeleton::ZSkeleton(ZFile* nParent) : ZResource(nParent), limbsTable(nParent) RegisterRequiredAttribute("LimbType"); } -void ZSkeleton::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - - Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, - GetRawDataSize(), GetSourceTypeName(), name, ""); - decl->staticConf = staticConf; -} - void ZSkeleton::ParseXML(tinyxml2::XMLElement* reader) { ZResource::ParseXML(reader); @@ -46,12 +39,9 @@ void ZSkeleton::ParseXML(tinyxml2::XMLElement* reader) limbType = ZLimb::GetTypeByAttributeName(limbTypeXml); if (limbType == ZLimbType::Invalid) { - fprintf(stderr, - "ZSkeleton::ParseXML: Warning in '%s'.\n" - "\t Invalid LimbType found: '%s'.\n" - "\t Defaulting to 'Standard'.\n", - name.c_str(), limbTypeXml.c_str()); - limbType = ZLimbType::Standard; + throw std::runtime_error(StringHelper::Sprintf("ZSkeleton::ParseXML: Error in '%s'.\n" + "\t Invalid LimbType found: '%s'.\n", + name.c_str(), limbTypeXml.c_str())); } } @@ -77,6 +67,8 @@ void ZSkeleton::DeclareReferences(const std::string& prefix) if (defaultPrefix == "") defaultPrefix = prefix; + ZResource::DeclareReferences(defaultPrefix); + if (limbsArrayAddress != 0 && GETSEGNUM(limbsArrayAddress) == parent->segment) { uint32_t ptr = Seg2Filespace(limbsArrayAddress, parent->baseAddress); @@ -122,26 +114,6 @@ size_t ZSkeleton::GetRawDataSize() const } } -std::string ZSkeleton::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) -{ - std::string headerStr = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(GetAddress()); - - if (decl == nullptr) - { - decl = parent->AddDeclaration(GetAddress(), DeclarationAlignment::Align16, GetRawDataSize(), - GetSourceTypeName(), name, headerStr); - } - else - { - decl->text = headerStr; - } - decl->staticConf = staticConf; - - return ""; -} - std::string ZSkeleton::GetSourceTypeName() const { switch (type) @@ -162,9 +134,9 @@ ZResourceType ZSkeleton::GetResourceType() const return ZResourceType::Skeleton; } -segptr_t ZSkeleton::GetAddress() +DeclarationAlignment ZSkeleton::GetDeclarationAlignment() const { - return rawDataIndex; + return DeclarationAlignment::Align16; } uint8_t ZSkeleton::GetLimbCount() @@ -180,16 +152,6 @@ ZLimbTable::ZLimbTable(ZFile* nParent) : ZResource(nParent) RegisterRequiredAttribute("Count"); } -void ZLimbTable::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - - 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) { rawDataIndex = nRawDataIndex; @@ -233,9 +195,9 @@ void ZLimbTable::ParseRawData() void ZLimbTable::DeclareReferences(const std::string& prefix) { - std::string varPrefix = prefix; - if (name != "") - varPrefix = name; + std::string varPrefix = name; + if (varPrefix == "") + varPrefix = prefix; ZResource::DeclareReferences(varPrefix); @@ -248,15 +210,31 @@ void ZLimbTable::DeclareReferences(const std::string& prefix) uint32_t limbOffset = Seg2Filespace(limbAddress, parent->baseAddress); if (!parent->HasDeclaration(limbOffset)) { - ZLimb* limb = new ZLimb(limbType, varPrefix, limbOffset, parent); + ZLimb* limb = new ZLimb(parent); + limb->ExtractFromBinary(limbOffset, limbType); + limb->SetName(limb->GetDefaultName(varPrefix)); + limb->DeclareVar(varPrefix, ""); limb->DeclareReferences(varPrefix); - limb->GetSourceOutputCode(varPrefix); parent->AddResource(limb); } } } } +Declaration* ZLimbTable::DeclareVar(const std::string& prefix, const std::string& bodyStr) +{ + std::string auxName = name; + + if (name == "") + auxName = GetDefaultName(prefix); + + Declaration* decl = + parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), + GetSourceTypeName(), auxName, limbsAddresses.size(), bodyStr); + decl->staticConf = staticConf; + return decl; +} + std::string ZLimbTable::GetBodySourceCode() const { std::string body = ""; @@ -273,22 +251,6 @@ std::string ZLimbTable::GetBodySourceCode() const return body; } -std::string ZLimbTable::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) -{ - std::string body = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(rawDataIndex); - if (decl == nullptr || decl->isPlaceholder) - decl = parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, - GetRawDataSize(), GetSourceTypeName(), name, - limbsAddresses.size(), body); - else - decl->text = body; - decl->staticConf = staticConf; - - return ""; -} - std::string ZLimbTable::GetSourceTypeName() const { switch (limbType) diff --git a/ZAPD/ZSkeleton.h b/ZAPD/ZSkeleton.h index fb65850..ed003ce 100644 --- a/ZAPD/ZSkeleton.h +++ b/ZAPD/ZSkeleton.h @@ -20,15 +20,15 @@ class ZLimbTable : public ZResource public: ZLimbTable(ZFile* nParent); - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; void ExtractFromBinary(uint32_t nRawDataIndex, ZLimbType nLimbType, size_t nCount); void ParseXML(tinyxml2::XMLElement* reader) override; void ParseRawData() override; void DeclareReferences(const std::string& prefix) override; - std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; + + std::string GetBodySourceCode() const override; std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; @@ -53,21 +53,18 @@ public: ZSkeleton(ZFile* nParent); - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; - void ParseXML(tinyxml2::XMLElement* reader) override; void ParseRawData() override; void DeclareReferences(const std::string& prefix) override; - std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; - - size_t GetRawDataSize() const override; + std::string GetBodySourceCode() const override; std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; - segptr_t GetAddress(); + size_t GetRawDataSize() const override; + DeclarationAlignment GetDeclarationAlignment() const override; + uint8_t GetLimbCount(); protected: diff --git a/ZAPD/ZString.cpp b/ZAPD/ZString.cpp index 502bb54..965d4d6 100644 --- a/ZAPD/ZString.cpp +++ b/ZAPD/ZString.cpp @@ -29,19 +29,23 @@ void ZString::ParseRawData() strData.assign(dataStart, dataStart + size); } -std::string ZString::GetBodySourceCode() const +Declaration* ZString::DeclareVar(const std::string& prefix, const std::string& bodyStr) { - return StringHelper::Sprintf("\t\"%s\"", strData.data()); -} + std::string auxName = name; + + if (name == "") + auxName = GetDefaultName(prefix); -std::string ZString::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) -{ Declaration* decl = - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), name, 0, GetBodySourceCode()); + parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), + GetSourceTypeName(), auxName, 0, bodyStr); decl->staticConf = staticConf; + return decl; +} - return ""; +std::string ZString::GetBodySourceCode() const +{ + return StringHelper::Sprintf("\t\"%s\"", strData.data()); } std::string ZString::GetSourceOutputHeader([[maybe_unused]] const std::string& prefix) diff --git a/ZAPD/ZString.h b/ZAPD/ZString.h index 19b4abb..6c58ebd 100644 --- a/ZAPD/ZString.h +++ b/ZAPD/ZString.h @@ -9,8 +9,9 @@ public: ZString(ZFile* nParent); void ParseRawData() override; + + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; std::string GetSourceOutputHeader(const std::string& prefix) override; std::string GetSourceTypeName() const override; diff --git a/ZAPD/ZSymbol.cpp b/ZAPD/ZSymbol.cpp index 3a21cbe..feea005 100644 --- a/ZAPD/ZSymbol.cpp +++ b/ZAPD/ZSymbol.cpp @@ -1,4 +1,5 @@ #include "ZSymbol.h" + #include "Utils/StringHelper.h" #include "ZFile.h" diff --git a/ZAPD/ZTexture.cpp b/ZAPD/ZTexture.cpp index 6a929c4..72014f6 100644 --- a/ZAPD/ZTexture.cpp +++ b/ZAPD/ZTexture.cpp @@ -1,6 +1,7 @@ #include "ZTexture.h" #include <cassert> + #include "CRC32.h" #include "Globals.h" #include "Utils/BitConverter.h" @@ -21,22 +22,8 @@ ZTexture::ZTexture(ZFile* nParent) : ZResource(nParent) RegisterOptionalAttribute("TlutOffset"); } -void ZTexture::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - - auto filepath = Globals::Instance->outputPath / fs::path(name).stem(); - - std::string incStr = - StringHelper::Sprintf("%s.%s.inc.c", filepath.c_str(), GetExternalExtension().c_str()); - - 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, - TextureType nType, bool nIsPalette) +void ZTexture::ExtractFromBinary(uint32_t nRawDataIndex, int32_t nWidth, int32_t nHeight, + TextureType nType, bool nIsPalette) { width = nWidth; height = nHeight; @@ -345,7 +332,7 @@ void ZTexture::DeclareReferences([[maybe_unused]] const std::string& prefix) GetExternalExtension().c_str()); tlut = new ZTexture(parent); - tlut->FromBinary(tlutOffset, tlutDim, tlutDim, TextureType::RGBA16bpp, true); + tlut->ExtractFromBinary(tlutOffset, tlutDim, tlutDim, TextureType::RGBA16bpp, true); parent->AddTextureResource(tlutOffset, tlut); parent->AddDeclarationIncludeArray(tlutOffset, incStr, tlut->GetRawDataSize(), tlut->GetSourceTypeName(), tlut->GetName(), 0); @@ -686,7 +673,7 @@ std::string ZTexture::GetIMSizFromType() } } -std::string ZTexture::GetDefaultName(const std::string& prefix) +std::string ZTexture::GetDefaultName(const std::string& prefix) const { const char* suffix = "Tex"; if (isPalette) @@ -747,6 +734,25 @@ void ZTexture::Save(const fs::path& outFolder) #endif } +Declaration* ZTexture::DeclareVar(const std::string& prefix, + [[maybe_unused]] const std::string& bodyStr) +{ + std::string auxName = name; + + if (name == "") + auxName = GetDefaultName(prefix); + + auto filepath = Globals::Instance->outputPath / fs::path(auxName).stem(); + + std::string incStr = + StringHelper::Sprintf("%s.%s.inc.c", filepath.c_str(), GetExternalExtension().c_str()); + + Declaration* decl = parent->AddDeclarationIncludeArray(rawDataIndex, incStr, GetRawDataSize(), + GetSourceTypeName(), auxName, 0); + decl->staticConf = staticConf; + return decl; +} + std::string ZTexture::GetBodySourceCode() const { std::string sourceOutput = ""; diff --git a/ZAPD/ZTexture.h b/ZAPD/ZTexture.h index 2ec4a4c..f844159 100644 --- a/ZAPD/ZTexture.h +++ b/ZAPD/ZTexture.h @@ -55,15 +55,16 @@ public: bool isPalette = false; - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; - void FromBinary(uint32_t nRawDataIndex, int32_t nWidth, int32_t nHeight, TextureType nType, - bool nIsPalette); + void ExtractFromBinary(uint32_t nRawDataIndex, int32_t nWidth, int32_t nHeight, + TextureType nType, bool nIsPalette); void FromPNG(const fs::path& pngFilePath, TextureType texType); static TextureType GetTextureTypeFromString(std::string str); void ParseXML(tinyxml2::XMLElement* reader) override; void ParseRawData() override; void DeclareReferences(const std::string& prefix) override; + + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; std::string GetBodySourceCode() const override; void CalcHash() override; void Save(const fs::path& outFolder) override; @@ -76,7 +77,7 @@ public: size_t GetRawDataSize() const override; std::string GetIMFmtFromType(); std::string GetIMSizFromType(); - std::string GetDefaultName(const std::string& prefix); + std::string GetDefaultName(const std::string& prefix) const override; uint32_t GetWidth() const; uint32_t GetHeight() const; void SetDimensions(uint32_t nWidth, uint32_t nHeight); diff --git a/ZAPD/ZTextureAnimation.cpp b/ZAPD/ZTextureAnimation.cpp index deee4e3..cd2ca1b 100644 --- a/ZAPD/ZTextureAnimation.cpp +++ b/ZAPD/ZTextureAnimation.cpp @@ -125,32 +125,6 @@ ZTextureAnimationParams::GetDefaultName([[maybe_unused]] const std::string& pref return "ShouldNotBeVIsible"; } -void ZTextureAnimationParams::DeclareVar(const std::string& prefix, - const std::string& bodyStr) const -{ - std::string auxName = name; - - if (name == "") - auxName = GetDefaultName(prefix); - - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), auxName, bodyStr); -} - -std::string ZTextureAnimationParams::GetSourceOutputCode(const std::string& prefix) -{ - std::string bodyStr = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(rawDataIndex); - - if (decl == nullptr) - DeclareVar(prefix, bodyStr); - else - decl->text = bodyStr; - - return ""; -} - ZResourceType ZTextureAnimationParams::GetResourceType() const { return ZResourceType::TextureAnimationParams; @@ -197,15 +171,16 @@ size_t TextureScrollingParams::GetRawDataSize() const /** * Overrides the parent version to declare an array of the params rather than just one entry. */ -void TextureScrollingParams::DeclareVar(const std::string& prefix, const std::string& bodyStr) const +Declaration* TextureScrollingParams::DeclareVar(const std::string& prefix, + const std::string& bodyStr) { std::string auxName = name; if (name == "") auxName = GetDefaultName(prefix); - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), auxName, count, bodyStr); + return parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), + GetSourceTypeName(), auxName, count, bodyStr); } std::string TextureScrollingParams::GetBodySourceCode() const @@ -537,12 +512,6 @@ ZTextureAnimation::ZTextureAnimation(ZFile* nParent) : ZResource(nParent) { } -void ZTextureAnimation::ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) -{ - ZResource::ExtractFromXML(reader, nRawDataIndex); - DeclareVar("", ""); -} - /** * Builds the array of params */ @@ -688,7 +657,7 @@ std::string ZTextureAnimation::GetDefaultName(const std::string& prefix) const return StringHelper::Sprintf("%sTexAnim_%06X", prefix.c_str(), rawDataIndex); } -void ZTextureAnimation::DeclareVar(const std::string& prefix, const std::string& bodyStr) const +Declaration* ZTextureAnimation::DeclareVar(const std::string& prefix, const std::string& bodyStr) { std::string auxName = name; @@ -696,9 +665,10 @@ void ZTextureAnimation::DeclareVar(const std::string& prefix, const std::string& auxName = GetDefaultName(prefix); Declaration* decl = - parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), + parent->AddDeclarationArray(rawDataIndex, GetDeclarationAlignment(), GetRawDataSize(), GetSourceTypeName(), auxName, entries.size(), bodyStr); decl->staticConf = staticConf; + return decl; } std::string ZTextureAnimation::GetBodySourceCode() const @@ -715,17 +685,3 @@ std::string ZTextureAnimation::GetBodySourceCode() const return bodyStr; } - -std::string ZTextureAnimation::GetSourceOutputCode(const std::string& prefix) -{ - std::string bodyStr = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(rawDataIndex); - - if (decl == nullptr) - DeclareVar(prefix, bodyStr); - else - decl->text = bodyStr; - - return ""; -} diff --git a/ZAPD/ZTextureAnimation.h b/ZAPD/ZTextureAnimation.h index d233071..c69fb34 100644 --- a/ZAPD/ZTextureAnimation.h +++ b/ZAPD/ZTextureAnimation.h @@ -4,8 +4,8 @@ #include <memory> #include <string> #include <vector> + #include "ZResource.h" -#include "tinyxml2.h" enum class TextureAnimationParamsType { @@ -26,10 +26,7 @@ public: void ExtractFromBinary(uint32_t nRawDataIndex); virtual void ExtractFromBinary(uint32_t nRawDataIndex, int count); - std::string GetSourceOutputCode(const std::string& prefix); - virtual std::string GetDefaultName(const std::string& prefix) const; - virtual void DeclareVar(const std::string& prefix, const std::string& bodyStr) const; ZResourceType GetResourceType() const; TextureAnimationParamsType type; @@ -55,7 +52,7 @@ public: std::string GetDefaultName(const std::string& prefix) const override; size_t GetRawDataSize() const override; - void DeclareVar(const std::string& prefix, const std::string& bodyStr) const override; + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; std::string GetBodySourceCode() const override; int count; // 1 for Single, 2 for Dual @@ -138,7 +135,6 @@ class ZTextureAnimation : public ZResource public: ZTextureAnimation(ZFile* nParent); - void ExtractFromXML(tinyxml2::XMLElement* reader, uint32_t nRawDataIndex) override; void ParseRawData() override; void DeclareReferences(const std::string& prefix) override; @@ -146,11 +142,10 @@ public: std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; size_t GetRawDataSize() const override; - std::string GetDefaultName(const std::string& prefix) const; + std::string GetDefaultName(const std::string& prefix) const override; - void DeclareVar(const std::string& prefix, const std::string& bodyStr) const; + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; private: std::vector<TextureAnimationEntry> entries; diff --git a/ZAPD/ZVector.cpp b/ZAPD/ZVector.cpp index 31d5dc4..29bf3bf 100644 --- a/ZAPD/ZVector.cpp +++ b/ZAPD/ZVector.cpp @@ -1,5 +1,7 @@ #include "ZVector.h" -#include <assert.h> + +#include <cassert> + #include "Globals.h" #include "Utils/BitConverter.h" #include "Utils/File.h" @@ -17,6 +19,16 @@ ZVector::ZVector(ZFile* nParent) : ZResource(nParent) RegisterRequiredAttribute("Dimensions"); } +void ZVector::ExtractFromBinary(uint32_t nRawDataIndex, ZScalarType nScalarType, + uint32_t nDimensions) +{ + rawDataIndex = nRawDataIndex; + scalarType = nScalarType; + dimensions = nDimensions; + + ParseRawData(); +} + void ZVector::ParseXML(tinyxml2::XMLElement* reader) { ZResource::ParseXML(reader); @@ -34,9 +46,8 @@ void ZVector::ParseRawData() for (uint32_t i = 0; i < dimensions; i++) { - ZScalar scalar(scalarType, parent); - scalar.rawDataIndex = currentRawDataIndex; - scalar.ParseRawData(); + ZScalar scalar(parent); + scalar.ExtractFromBinary(currentRawDataIndex, scalarType); currentRawDataIndex += scalar.GetRawDataSize(); scalars.push_back(scalar); @@ -86,20 +97,15 @@ std::string ZVector::GetBodySourceCode() const { std::string body = ""; - for (size_t i = 0; i < this->scalars.size(); i++) - body += StringHelper::Sprintf("%6s, ", scalars[i].GetBodySourceCode().c_str()); - - return "{ " + body + "}"; -} + for (size_t i = 0; i < scalars.size(); i++) + { + body += StringHelper::Sprintf("%6s", scalars[i].GetBodySourceCode().c_str()); -std::string ZVector::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) -{ - Declaration* decl = - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), GetName(), GetBodySourceCode()); - decl->staticConf = staticConf; + if (i + 1 < scalars.size()) + body += ", "; + } - return ""; + return body; } ZResourceType ZVector::GetResourceType() const @@ -107,12 +113,7 @@ ZResourceType ZVector::GetResourceType() const return ZResourceType::Vector; } -void ZVector::SetScalarType(ZScalarType type) -{ - scalarType = type; -} - -void ZVector::SetDimensions(uint32_t dim) +DeclarationAlignment ZVector::GetDeclarationAlignment() const { - dimensions = dim; + return scalars.at(0).GetDeclarationAlignment(); } diff --git a/ZAPD/ZVector.h b/ZAPD/ZVector.h index d700d43..d1a7389 100644 --- a/ZAPD/ZVector.h +++ b/ZAPD/ZVector.h @@ -10,24 +10,22 @@ class ZVector : public ZResource { public: + std::vector<ZScalar> scalars; + ZScalarType scalarType; + uint32_t dimensions; + ZVector(ZFile* nParent); + void ExtractFromBinary(uint32_t nRawDataIndex, ZScalarType nScalarType, uint32_t nDimensions); + void ParseXML(tinyxml2::XMLElement* reader) override; void ParseRawData() override; std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; bool DoesSupportArray() const override; std::string GetSourceTypeName() const override; ZResourceType GetResourceType() const override; size_t GetRawDataSize() const override; - - void SetScalarType(ZScalarType type); - void SetDimensions(uint32_t dim); - -protected: - std::vector<ZScalar> scalars; - ZScalarType scalarType; - uint32_t dimensions; + DeclarationAlignment GetDeclarationAlignment() const override; }; diff --git a/ZAPD/ZVtx.cpp b/ZAPD/ZVtx.cpp index 54420f0..d5214e8 100644 --- a/ZAPD/ZVtx.cpp +++ b/ZAPD/ZVtx.cpp @@ -1,4 +1,5 @@ #include "ZVtx.h" + #include "Utils/BitConverter.h" #include "Utils/StringHelper.h" #include "ZFile.h" @@ -36,22 +37,17 @@ void ZVtx::ParseRawData() a = rawData[rawDataIndex + 15]; } -std::string ZVtx::GetBodySourceCode() const +Declaration* ZVtx::DeclareVar(const std::string& prefix, const std::string& bodyStr) { - return StringHelper::Sprintf(" VTX(%i, %i, %i, %i, %i, %i, %i, %i, %i)", x, y, z, s, t, r, g, - b, a); + Declaration* decl = ZResource::DeclareVar(prefix, bodyStr); + decl->isExternal = true; + return decl; } -std::string ZVtx::GetSourceOutputCode([[maybe_unused]] const std::string& prefix) +std::string ZVtx::GetBodySourceCode() const { - std::string output = GetBodySourceCode(); - - Declaration* decl = parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align16, - GetRawDataSize(), GetSourceTypeName(), name, output); - decl->isExternal = true; - decl->staticConf = staticConf; - - return ""; + return StringHelper::Sprintf("VTX(%i, %i, %i, %i, %i, %i, %i, %i, %i)", x, y, z, s, t, r, g, b, + a); } size_t ZVtx::GetRawDataSize() const @@ -83,3 +79,8 @@ std::string ZVtx::GetExternalExtension() const { return "vtx"; } + +DeclarationAlignment ZVtx::GetDeclarationAlignment() const +{ + return DeclarationAlignment::Align16; +} diff --git a/ZAPD/ZVtx.h b/ZAPD/ZVtx.h index feed307..7bc2fdf 100644 --- a/ZAPD/ZVtx.h +++ b/ZAPD/ZVtx.h @@ -18,8 +18,9 @@ public: ZVtx(ZFile* nParent); void ParseRawData() override; + + Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override; std::string GetBodySourceCode() const override; - std::string GetSourceOutputCode(const std::string& prefix) override; bool IsExternalResource() const override; bool DoesSupportArray() const override; @@ -28,6 +29,7 @@ public: std::string GetExternalExtension() const override; size_t GetRawDataSize() const override; + DeclarationAlignment GetDeclarationAlignment() const override; protected: -};
\ No newline at end of file +}; |
