diff options
| author | Anghelo Carvajal <angheloalf95@gmail.com> | 2021-05-10 12:25:39 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-10 12:25:39 -0400 |
| commit | 769f5702a422d14bfb0a60a39319da4b4bf9ec1f (patch) | |
| tree | 11e12a20fb2880792722babdee0b5222ea86504f /ZAPD/ZRoom/Commands/SetMesh.cpp | |
| parent | a357277e412b5e8f0f2f0578c7d7d213e8744c8d (diff) | |
ZRoom refactor (#124)
* Remove redundant methods
* first pass os cleaning
* More cleaning
* Fix compilation errors
* Fix dumb crashes
* Fix dumb issues
* Removing most uses of GenerateSourceCodePass1
* more cleanup
* Move segmentOffset to ZRoomCommand
* Half of ParseRawData
* somehow broken...
* fix broken stuff
* This may or may not be broken
* More cleaning
* Remove GenerateSourceCodePass2
* More cleanup and pointer removal
* Use PolygonType2 in SetMesh
* another small cleanup
* Merge PolygonDlist and PolygonDlist2
* More code merging
* GetDeclarationPtrName
* Remove most repeated externs and a bit of redundant code
* Dumb MM fixes
* small fix
(cherry picked from commit 0813fd69538491c5aceb00d79d19bf59fd082ca7)
* Another dumb fix
* Remove GenerateExterns
* Change DeclareReferences signature
* Refactor SetAnimatedTextureList
* const std::vector<uint8_t>&
* another small bunch of changes
* last parserawdata and declarereferences separation probably
* Move parserawdata
* Remove GenerateSourceCodePass1
* Use macros for commands
* Make ZRoomCommand inherit ZResource
* shrink scenes tluts
* ProcessTextureIntersections
* small output improvements
* Small changes
* run format
* SCENE_CMD -> SCENECMD
* fix merge errors
* run format
* fix some warnings
* update macros names
* Add again different commands for each game ni SCENE_CMD_ROOM_BEHAVIOR
* macro update
* More booleans
* Move declaration to its own file
* minor changes
* Update according to latest reviews in oot
* run format
* Yet another cutscene change
* Remove special scene/room segment code
* This is crashing and I don't know why
* remove includeFilePrefix
* fix merge issues
* always pass a parent to zresource
* move path to its own zclass and properly implement parserawdata in commands
* Fix GetDeclarationPtrName uses
* Some ZPath fixes (still broken tho)
* Fix ZPath declarations
* Fix weird problem
* Add `Path` to the docs
* run format
* clean syotes code
* A bunch of changes for MM
* run format
Diffstat (limited to 'ZAPD/ZRoom/Commands/SetMesh.cpp')
| -rw-r--r-- | ZAPD/ZRoom/Commands/SetMesh.cpp | 677 |
1 files changed, 277 insertions, 400 deletions
diff --git a/ZAPD/ZRoom/Commands/SetMesh.cpp b/ZAPD/ZRoom/Commands/SetMesh.cpp index e8f522d..2635e7f 100644 --- a/ZAPD/ZRoom/Commands/SetMesh.cpp +++ b/ZAPD/ZRoom/Commands/SetMesh.cpp @@ -1,257 +1,76 @@ #include "SetMesh.h" #include <Globals.h> #include <Path.h> -#include "../../BitConverter.h" -#include "../../StringHelper.h" -#include "../../ZFile.h" -#include "../ZRoom.h" +#include "BitConverter.h" +#include "StringHelper.h" #include "ZBackground.h" +#include "ZFile.h" +#include "ZRoom/ZRoom.h" -using namespace std; +void GenDListDeclarations(ZRoom* zRoom, ZFile* parent, ZDisplayList* dList); -SetMesh::SetMesh(ZRoom* nZRoom, std::vector<uint8_t> rawData, uint32_t rawDataIndex, - int32_t segAddressOffset) - : ZRoomCommand(nZRoom, rawData, rawDataIndex) +SetMesh::SetMesh(ZFile* nParent) : ZRoomCommand(nParent) { - data = rawData[rawDataIndex + 1]; - segmentOffset = GETSEGOFFSET(BitConverter::ToInt32BE(rawData, rawDataIndex + 4)); +} - string declaration = ""; - meshHeaderType = rawData[segmentOffset + 0]; +void SetMesh::ParseRawData() +{ + ZRoomCommand::ParseRawData(); + auto& parentRawData = parent->GetRawData(); + meshHeaderType = parentRawData.at(segmentOffset); - if (meshHeaderType == 0) + switch (meshHeaderType) { - uint32_t dListStart = Seg2Filespace(BitConverter::ToInt32BE(rawData, segmentOffset + 4), - zRoom->parent->baseAddress); - uint32_t dListEnd = Seg2Filespace(BitConverter::ToInt32BE(rawData, segmentOffset + 8), - zRoom->parent->baseAddress); - - int8_t numEntries = rawData[segmentOffset + 1]; - uint32_t currentPtr = dListStart; - - // Hack for Syotes - for (int32_t i = 0; i < abs(segAddressOffset); i++) - { - rawData.erase(rawData.begin()); - segmentOffset--; - } - - if (numEntries > 0) - { - std::string polyGfxBody = ""; - std::string polyGfxType = ""; - int32_t polyGfxSize = 0; - - for (int32_t i = 0; i < numEntries; i++) - { - PolygonDlist polyGfxList(zRoom->GetName(), rawData, currentPtr, zRoom->parent, - zRoom); - if (polyGfxList.opaDList != nullptr) - { - GenDListDeclarations(rawData, polyGfxList.opaDList); - } - if (polyGfxList.xluDList != nullptr) - { - GenDListDeclarations(rawData, polyGfxList.xluDList); - } - polyGfxBody += polyGfxList.GetBodySourceCode(true); - polyGfxType = polyGfxList.GetSourceTypeName(); - polyGfxSize = polyGfxList.GetRawDataSize(); - - currentPtr += polyGfxList.GetRawDataSize(); - } - - zRoom->parent->AddDeclarationArray( - dListStart, DeclarationAlignment::Align4, numEntries * polyGfxSize, polyGfxType, - StringHelper::Sprintf("%s%s0x%06X", zRoom->GetName().c_str(), polyGfxType.c_str(), - dListStart), - numEntries, polyGfxBody); - } + case 0: + polyType = std::make_shared<PolygonType2>(parent, parentRawData, segmentOffset, zRoom); + break; - declaration = "\n "; - declaration += StringHelper::Sprintf("{ 0 }, 0x%02X, ", numEntries); - declaration += "\n "; + case 1: + polyType = std::make_shared<PolygonType1>(parent, parentRawData, segmentOffset, zRoom); + break; - std::string entriesStr = "NULL"; + case 2: + polyType = std::make_shared<PolygonType2>(parent, parentRawData, segmentOffset, zRoom); + break; - if (dListStart != 0) - { - entriesStr = zRoom->parent->GetDeclaration(dListStart)->varName; - } - declaration += StringHelper::Sprintf("%s, ", entriesStr.c_str()); - declaration += "\n "; - - if (dListEnd != 0) - declaration += StringHelper::Sprintf("%s + ARRAY_COUNT(%s), ", entriesStr.c_str(), - entriesStr.c_str()); - else - declaration += "NULL, "; - declaration += "\n"; - - zRoom->parent->AddDeclaration( - segmentOffset, DeclarationAlignment::Align16, 12, "MeshHeader0", - StringHelper::Sprintf("%sMeshHeader0x%06X", zRoom->GetName().c_str(), segmentOffset), - declaration); - - zRoom->parent->AddDeclaration(dListStart + (numEntries * 8), DeclarationAlignment::None, - DeclarationPadding::Pad16, 4, "static s32", "terminatorMaybe", - "0x01000000"); - } - else if (meshHeaderType == 1) - { - PolygonType1 polygon1(zRoom->GetName().c_str(), rawData, segmentOffset, zRoom->parent, - zRoom); - if (polygon1.polyGfxList.opaDList != nullptr) - { - GenDListDeclarations(rawData, polygon1.polyGfxList.opaDList); - } - if (polygon1.polyGfxList.xluDList != nullptr) - { - GenDListDeclarations(rawData, polygon1.polyGfxList.xluDList); - } - polygon1.DeclareAndGenerateOutputCode(); + default: + throw std::runtime_error(StringHelper::Sprintf("Error in SetMesh::ParseRawData\n" + "\t Unknown meshHeaderType: %i\n", + meshHeaderType)); } - else if (meshHeaderType == 2) - { - MeshHeader2* meshHeader2 = new MeshHeader2(); - meshHeader2->headerType = 2; - - meshHeader2->entries = vector<MeshEntry2*>(); - meshHeader2->dListStart = GETSEGOFFSET(BitConverter::ToInt32BE(rawData, segmentOffset + 4)); - meshHeader2->dListEnd = GETSEGOFFSET(BitConverter::ToInt32BE(rawData, segmentOffset + 8)); - - int8_t numEntries = rawData[segmentOffset + 1]; - uint32_t currentPtr = meshHeader2->dListStart; - - for (int32_t i = 0; i < numEntries; i++) - { - MeshEntry2* entry = new MeshEntry2(); - entry->playerXMax = BitConverter::ToInt16BE(rawData, currentPtr + 0); - entry->playerZMax = BitConverter::ToInt16BE(rawData, currentPtr + 2); - entry->playerXMin = BitConverter::ToInt16BE(rawData, currentPtr + 4); - entry->playerZMin = BitConverter::ToInt16BE(rawData, currentPtr + 6); - entry->opaqueDListAddr = GETSEGOFFSET(BitConverter::ToInt32BE(rawData, currentPtr + 8)); - entry->translucentDListAddr = - GETSEGOFFSET(BitConverter::ToInt32BE(rawData, currentPtr + 12)); - - if (entry->opaqueDListAddr != 0) - { - entry->opaqueDList = new ZDisplayList( - rawData, entry->opaqueDListAddr, - ZDisplayList::GetDListLength(rawData, entry->opaqueDListAddr, - Globals::Instance->game == ZGame::OOT_SW97 ? - DListType::F3DEX : - DListType::F3DZEX), - zRoom->parent); - entry->opaqueDList->scene = zRoom->scene; - GenDListDeclarations(rawData, entry->opaqueDList); - } - - if (entry->translucentDListAddr != 0) - { - entry->translucentDList = new ZDisplayList( - rawData, entry->translucentDListAddr, - ZDisplayList::GetDListLength(rawData, entry->translucentDListAddr, - Globals::Instance->game == ZGame::OOT_SW97 ? - DListType::F3DEX : - DListType::F3DZEX), - zRoom->parent); - entry->translucentDList->scene = zRoom->scene; - GenDListDeclarations(rawData, entry->translucentDList); - } - - meshHeader2->entries.push_back(entry); - - currentPtr += 16; - } - - declaration += StringHelper::Sprintf("{ 2 }, 0x%02lX, ", meshHeader2->entries.size()); - - if (meshHeader2->dListStart != 0) - declaration += StringHelper::Sprintf("(u32)&%sMeshDListEntry0x%06X, ", - zRoom->GetName().c_str(), meshHeader2->dListStart); - else - declaration += "0, "; - - if (meshHeader2->dListEnd != 0) - declaration += StringHelper::Sprintf( - "(u32)&(%sMeshDListEntry0x%06X) + sizeof(%sMeshDListEntry0x%06X)", - zRoom->GetName().c_str(), meshHeader2->dListStart, zRoom->GetName().c_str(), - meshHeader2->dListStart); - else - declaration += "0"; - declaration += "\n"; - - zRoom->parent->AddDeclaration( - segmentOffset, DeclarationAlignment::None, 12, "MeshHeader2", - StringHelper::Sprintf("%sMeshHeader0x%06X", zRoom->GetName().c_str(), segmentOffset), - declaration); - - declaration = ""; - - for (size_t i = 0; i < meshHeader2->entries.size(); i++) - { - declaration += StringHelper::Sprintf( - " { %i, %i, %i, %i, ", meshHeader2->entries[i]->playerXMax, - meshHeader2->entries[i]->playerZMax, meshHeader2->entries[i]->playerXMin, - meshHeader2->entries[i]->playerZMin); - - if (meshHeader2->entries[i]->opaqueDListAddr != 0) - declaration += StringHelper::Sprintf("(u32)%sDL_%06X, ", zRoom->GetName().c_str(), - meshHeader2->entries[i]->opaqueDListAddr); - else - declaration += "0, "; - - if (meshHeader2->entries[i]->translucentDListAddr != 0) - declaration += - StringHelper::Sprintf("(u32)%sDL_%06X },\n", zRoom->GetName().c_str(), - meshHeader2->entries[i]->translucentDListAddr); - else - declaration += "0 },\n"; - } - - zRoom->parent->AddDeclarationArray( - meshHeader2->dListStart, DeclarationAlignment::None, DeclarationPadding::None, - (meshHeader2->entries.size() * 16) + 0, "MeshEntry2", - StringHelper::Sprintf("%sMeshDListEntry0x%06X", zRoom->GetName().c_str(), - meshHeader2->dListStart, meshHeader2->entries.size()), - meshHeader2->entries.size(), declaration); - - zRoom->parent->AddDeclaration(meshHeader2->dListStart + (meshHeader2->entries.size() * 16), - DeclarationAlignment::None, DeclarationPadding::Pad16, 4, - "static s32", "terminatorMaybe", "0x01000000"); - - meshHeader = meshHeader2; - } + polyType->ParseRawData(); } -SetMesh::~SetMesh() +void SetMesh::DeclareReferences(const std::string& prefix) { - if (meshHeader != nullptr) - { - delete meshHeader; - meshHeader = nullptr; - } + polyType->SetName(polyType->GetDefaultName(prefix)); + polyType->DeclareReferences(prefix); + polyType->DeclareAndGenerateOutputCode(prefix); } -void SetMesh::GenDListDeclarations(std::vector<uint8_t> rawData, ZDisplayList* dList) +void GenDListDeclarations(ZRoom* zRoom, ZFile* parent, ZDisplayList* dList) { - string srcVarName = + if (dList == nullptr) + { + return; + } + std::string srcVarName = StringHelper::Sprintf("%s%s", zRoom->GetName().c_str(), dList->GetName().c_str()); dList->SetName(srcVarName); - string sourceOutput = dList->GetSourceOutputCode(zRoom->GetName()); + dList->scene = zRoom->scene; + std::string sourceOutput = dList->GetSourceOutputCode(zRoom->GetName()); for (ZDisplayList* otherDList : dList->otherDLists) - GenDListDeclarations(rawData, otherDList); + GenDListDeclarations(zRoom, parent, otherDList); - for (pair<uint32_t, string> vtxEntry : dList->vtxDeclarations) + for (const auto& vtxEntry : dList->vtxDeclarations) { - DeclarationAlignment alignment = DeclarationAlignment::Align8; + DeclarationAlignment alignment = DeclarationAlignment::Align4; if (Globals::Instance->game == ZGame::MM_RETAIL) alignment = DeclarationAlignment::None; - zRoom->parent->AddDeclarationArray( + parent->AddDeclarationArray( vtxEntry.first, alignment, dList->vertices[vtxEntry.first].size() * 16, "static Vtx", StringHelper::Sprintf("%sVtx_%06X", zRoom->GetName().c_str(), vtxEntry.first), dList->vertices[vtxEntry.first].size(), vtxEntry.second); @@ -260,13 +79,10 @@ void SetMesh::GenDListDeclarations(std::vector<uint8_t> rawData, ZDisplayList* d std::string SetMesh::GenDListExterns(ZDisplayList* dList) { - string sourceOutput = ""; + std::string sourceOutput = ""; - if (Globals::Instance->includeFilePrefix) - sourceOutput += StringHelper::Sprintf("extern Gfx %sDL_%06X[];\n", zRoom->GetName().c_str(), - dList->GetRawDataIndex()); - else - sourceOutput += StringHelper::Sprintf("extern Gfx DL_%06X[];\n", dList->GetRawDataIndex()); + sourceOutput += StringHelper::Sprintf("extern Gfx %sDL_%06X[];\n", zRoom->GetName().c_str(), + dList->GetRawDataIndex()); for (ZDisplayList* otherDList : dList->otherDLists) sourceOutput += GenDListExterns(otherDList); @@ -276,22 +92,10 @@ std::string SetMesh::GenDListExterns(ZDisplayList* dList) return sourceOutput; } -string SetMesh::GenerateSourceCodePass1(string roomName, uint32_t baseAddress) +std::string SetMesh::GetBodySourceCode() const { - string sourceOutput = ""; - - Declaration* decl = zRoom->parent->GetDeclaration(segmentOffset); - - sourceOutput += StringHelper::Sprintf( - "%s %i, &%s", ZRoomCommand::GenerateSourceCodePass1(roomName, baseAddress).c_str(), data, - decl->varName.c_str()); - - return sourceOutput; -} - -string SetMesh::GenerateExterns() const -{ - return ""; + std::string list = parent->GetDeclarationPtrName(cmdArg2); + return StringHelper::Sprintf("SCENE_CMD_MESH(%s)", list.c_str()); } size_t SetMesh::GetRawDataSize() const @@ -299,7 +103,7 @@ size_t SetMesh::GetRawDataSize() const return ZRoomCommand::GetRawDataSize(); } -string SetMesh::GetCommandCName() const +std::string SetMesh::GetCommandCName() const { return "SCmdMesh"; } @@ -315,20 +119,36 @@ PolygonDlist::PolygonDlist(const std::string& prefix, const std::vector<uint8_t> rawData.assign(nRawData.begin(), nRawData.end()); rawDataIndex = nRawDataIndex; parent = nParent; - room = nRoom; + zRoom = nRoom; name = GetDefaultName(prefix.c_str(), rawDataIndex); +} - ParseRawData(); +void PolygonDlist::ParseRawData() +{ + switch (polyType) + { + case 2: + x = BitConverter::ToInt16BE(rawData, rawDataIndex + 0); + y = BitConverter::ToInt16BE(rawData, rawDataIndex + 2); + z = BitConverter::ToInt16BE(rawData, rawDataIndex + 4); + unk_06 = BitConverter::ToInt16BE(rawData, rawDataIndex + 6); - opaDList = MakeDlist(opa, prefix); - xluDList = MakeDlist(xlu, prefix); + opa = BitConverter::ToUInt32BE(rawData, rawDataIndex + 8); + xlu = BitConverter::ToUInt32BE(rawData, rawDataIndex + 12); + break; + + default: + opa = BitConverter::ToUInt32BE(rawData, rawDataIndex); + xlu = BitConverter::ToUInt32BE(rawData, rawDataIndex + 4); + break; + } } -void PolygonDlist::ParseRawData() +void PolygonDlist::DeclareReferences(const std::string& prefix) { - opa = BitConverter::ToUInt32BE(rawData, rawDataIndex); - xlu = BitConverter::ToUInt32BE(rawData, rawDataIndex + 4); + opaDList = MakeDlist(opa, prefix); + xluDList = MakeDlist(xlu, prefix); } ZDisplayList* PolygonDlist::MakeDlist(segptr_t ptr, const std::string& prefix) @@ -344,18 +164,26 @@ ZDisplayList* PolygonDlist::MakeDlist(segptr_t ptr, const std::string& prefix) rawData, dlistAddress, Globals::Instance->game == ZGame::OOT_SW97 ? DListType::F3DEX : DListType::F3DZEX); ZDisplayList* dlist = new ZDisplayList(rawData, dlistAddress, dlistLength, parent); - - string dListStr = StringHelper::Sprintf("%sPolygonDlist_%06X", prefix.c_str(), dlistAddress); - dlist->SetName(dListStr); - dlist->scene = room->scene; - dlist->GetSourceOutputCode(prefix); + GenDListDeclarations(zRoom, parent, dlist); return dlist; } -size_t PolygonDlist::GetRawDataSize() +size_t PolygonDlist::GetRawDataSize() const { - return 0x08; + switch (polyType) + { + case 2: + return 0x10; + + default: + return 0x08; + } +} + +void PolygonDlist::SetPolyType(uint8_t nPolyType) +{ + polyType = nPolyType; } void PolygonDlist::DeclareVar(const std::string& prefix, const std::string& bodyStr) @@ -372,53 +200,32 @@ void PolygonDlist::DeclareVar(const std::string& prefix, const std::string& body std::string PolygonDlist::GetBodySourceCode(bool arrayElement) { std::string bodyStr = ""; - std::string opaStr = "NULL"; - std::string xluStr = "NULL"; + std::string opaStr = parent->GetDeclarationPtrName(opa); + std::string xluStr = parent->GetDeclarationPtrName(xlu); if (arrayElement) { - bodyStr += " { \n "; + bodyStr += " { "; } else { - bodyStr += "\n"; + bodyStr += "\n "; } - if (opa != 0) + if (polyType == 2) { - Declaration* decl = parent->GetDeclaration(Seg2Filespace(opa, parent->baseAddress)); - if (decl != nullptr) - { - opaStr = decl->varName; - } - else - { - opaStr = StringHelper::Sprintf("0x%08X", opa); - } - } - if (xlu != 0) - { - Declaration* decl = parent->GetDeclaration(Seg2Filespace(xlu, parent->baseAddress)); - if (decl != nullptr) - { - xluStr = decl->varName; - } - else - { - xluStr = StringHelper::Sprintf("0x%08X", xlu); - } + bodyStr += StringHelper::Sprintf("{ %6i, %6i, %6i }, %6i, ", x, y, z, unk_06); } - bodyStr += StringHelper::Sprintf(" %s, \n", opaStr.c_str()); + bodyStr += StringHelper::Sprintf("%s, ", opaStr.c_str()); + bodyStr += StringHelper::Sprintf("%s", xluStr.c_str()); + if (arrayElement) { - bodyStr += " "; + bodyStr += " },"; } - - bodyStr += StringHelper::Sprintf(" %s, \n", xluStr.c_str()); - - if (arrayElement) + else { - bodyStr += " },"; + bodyStr += "\n"; } return bodyStr; @@ -446,7 +253,14 @@ std::string PolygonDlist::GetDefaultName(const std::string& prefix, uint32_t add std::string PolygonDlist::GetSourceTypeName() { - return "PolygonDlist"; + switch (polyType) + { + case 2: + return "PolygonDlist2"; + + default: + return "PolygonDlist"; + } } std::string PolygonDlist::GetName() @@ -510,12 +324,12 @@ size_t BgImage::GetRawDataSize() return 0x1C; } -std::string BgImage::GetBodySourceCode(bool arrayElement) +std::string BgImage::GetBodySourceCode(bool arrayElement) const { - std::string bodyStr = ""; + std::string bodyStr = " "; if (arrayElement) { - bodyStr += " { \n "; + bodyStr += "{ \n "; } if (!isSubStruct) @@ -529,21 +343,7 @@ std::string BgImage::GetBodySourceCode(bool arrayElement) } } - std::string backgroundName = "NULL"; - if (source != 0) - { - uint32_t address = Seg2Filespace(source, parent->baseAddress); - Declaration* decl = parent->GetDeclaration(address); - - if (decl == nullptr) - { - backgroundName += StringHelper::Sprintf("0x%08X, ", source); - } - else - { - backgroundName = decl->varName; - } - } + std::string backgroundName = parent->GetDeclarationPtrName(source); bodyStr += StringHelper::Sprintf("%s, ", backgroundName.c_str()); bodyStr += "\n "; if (arrayElement) @@ -581,6 +381,10 @@ std::string BgImage::GetBodySourceCode(bool arrayElement) { bodyStr += " \n }, "; } + else + { + bodyStr += "\n"; + } return bodyStr; } @@ -600,29 +404,109 @@ std::string BgImage::GetName() return name; } -PolygonType1::PolygonType1(const std::string& prefix, const std::vector<uint8_t>& nRawData, - uint32_t nRawDataIndex, ZFile* nParent, ZRoom* nRoom) +/* PolygonType section */ + +PolygonTypeBase::PolygonTypeBase(ZFile* nParent, const std::vector<uint8_t>& nRawData, + uint32_t nRawDataIndex, ZRoom* nRoom) + : rawData{nRawData}, rawDataIndex{nRawDataIndex}, parent{nParent}, zRoom{nRoom} { - rawData.assign(nRawData.begin(), nRawData.end()); - rawDataIndex = nRawDataIndex; - parent = nParent; + type = BitConverter::ToUInt8BE(rawData, rawDataIndex); +} - name = GetDefaultName(prefix.c_str(), rawDataIndex); +void PolygonTypeBase::DeclareVar(const std::string& prefix, const std::string& bodyStr) +{ + std::string auxName = name; + if (name == "") + auxName = GetDefaultName(prefix); - ParseRawData(); + parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), + GetSourceTypeName(), auxName, bodyStr); +} + +void PolygonTypeBase::DeclareAndGenerateOutputCode(const std::string& prefix) +{ + std::string bodyStr = GetBodySourceCode(); + + Declaration* decl = parent->GetDeclaration(rawDataIndex); + if (decl == nullptr) + { + DeclareVar(prefix, bodyStr); + } + else + { + decl->text = bodyStr; + } +} + +std::string PolygonTypeBase::GetSourceTypeName() const +{ + switch (type) + { + case 2: + return "PolygonType2"; + + case 1: + return "PolygonType1"; + + default: + return "PolygonType0"; + } +} + +std::string PolygonTypeBase::GetName() const +{ + return name; +} + +void PolygonTypeBase::SetName(const std::string& newName) +{ + name = newName; +} + +std::string PolygonTypeBase::GetDefaultName(const std::string& prefix) const +{ + return StringHelper::Sprintf("%s%s_%06X", prefix.c_str(), GetSourceTypeName().c_str(), + rawDataIndex); +} + +PolygonType1::PolygonType1(ZFile* nParent, const std::vector<uint8_t>& nRawData, + uint32_t nRawDataIndex, ZRoom* nRoom) + : PolygonTypeBase(nParent, nRawData, nRawDataIndex, nRoom) +{ +} + +void PolygonType1::ParseRawData() +{ + format = BitConverter::ToUInt8BE(rawData, rawDataIndex + 0x01); + dlist = BitConverter::ToUInt32BE(rawData, rawDataIndex + 0x04); + + if (format == 2) + { + count = BitConverter::ToUInt8BE(rawData, rawDataIndex + 0x08); + list = BitConverter::ToUInt32BE(rawData, rawDataIndex + 0x0C); + } if (dlist != 0) { - polyGfxList = - PolygonDlist(prefix, rawData, Seg2Filespace(dlist, parent->baseAddress), parent, nRoom); + PolygonDlist polyGfxList(zRoom->GetName(), rawData, + Seg2Filespace(dlist, parent->baseAddress), parent, zRoom); + polyGfxList.SetPolyType(type); + polyGfxList.ParseRawData(); + polyGfxList.DeclareReferences(zRoom->GetName()); + polyDLists.push_back(polyGfxList); } +} + +void PolygonType1::DeclareReferences(const std::string& prefix) +{ + polyDLists.at(0).DeclareAndGenerateOutputCode(); uint32_t listAddress; std::string bgImageArrayBody = ""; switch (format) { case 1: - single = BgImage(true, prefix, nRawData, nRawDataIndex + 0x08, parent); + single = BgImage(true, prefix, rawData, rawDataIndex + 0x08, parent); break; case 2: @@ -659,20 +543,7 @@ PolygonType1::PolygonType1(const std::string& prefix, const std::vector<uint8_t> } } -void PolygonType1::ParseRawData() -{ - type = BitConverter::ToUInt8BE(rawData, rawDataIndex); - format = BitConverter::ToUInt8BE(rawData, rawDataIndex + 0x01); - dlist = BitConverter::ToUInt32BE(rawData, rawDataIndex + 0x04); - - if (format == 2) - { - count = BitConverter::ToUInt8BE(rawData, rawDataIndex + 0x08); - list = BitConverter::ToUInt32BE(rawData, rawDataIndex + 0x0C); - } -} - -size_t PolygonType1::GetRawDataSize() +size_t PolygonType1::GetRawDataSize() const { switch (format) { @@ -685,40 +556,14 @@ size_t PolygonType1::GetRawDataSize() return 0x20; } -void PolygonType1::DeclareVar(const std::string& prefix, const std::string& bodyStr) -{ - std::string auxName = name; - if (name == "") - { - auxName = GetDefaultName(prefix, rawDataIndex); - } - parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(), - GetSourceTypeName(), auxName, bodyStr); -} - -std::string PolygonType1::GetBodySourceCode() +std::string PolygonType1::GetBodySourceCode() const { std::string bodyStr = "\n "; bodyStr += "{ "; bodyStr += StringHelper::Sprintf("%i, %i, ", type, format); - std::string dlistStr = "NULL"; - if (dlist != 0) - { - uint32_t entryRecordAddress = Seg2Filespace(dlist, parent->baseAddress); - Declaration* decl = parent->GetDeclaration(entryRecordAddress); - - if (decl == nullptr) - { - polyGfxList.DeclareAndGenerateOutputCode(); - dlistStr = "&" + polyGfxList.GetName(); - } - else - { - dlistStr = "&" + decl->varName; - } - } + std::string dlistStr = parent->GetDeclarationPtrName(dlist); bodyStr += StringHelper::Sprintf("%s, ", dlistStr.c_str()); bodyStr += "}, \n"; @@ -727,22 +572,10 @@ std::string PolygonType1::GetBodySourceCode() switch (format) { case 1: - bodyStr += " " + single.GetBodySourceCode(false) + "\n"; + bodyStr += single.GetBodySourceCode(false); break; case 2: - if (list != 0) - { - uint32_t listAddress = Seg2Filespace(list, parent->baseAddress); - Declaration* decl = parent->GetDeclaration(listAddress); - if (decl != nullptr) - { - listStr = decl->varName; - } - else - { - listStr = StringHelper::Sprintf("0x%08X", list); - } - } + listStr = parent->GetDeclarationPtrName(list); bodyStr += StringHelper::Sprintf(" %i, %s, \n", count, listStr.c_str()); break; @@ -754,41 +587,85 @@ std::string PolygonType1::GetBodySourceCode() return bodyStr; } -void PolygonType1::DeclareAndGenerateOutputCode() +std::string PolygonType1::GetSourceTypeName() const { - std::string bodyStr = GetBodySourceCode(); - - Declaration* decl = parent->GetDeclaration(rawDataIndex); - if (decl == nullptr) - { - DeclareVar("", bodyStr); - } - else + switch (format) { - decl->text = bodyStr; + case 1: + return "MeshHeader1Single"; + + case 2: + return "MeshHeader1Multi"; } + return "ERROR"; + // return "PolygonType1"; } -std::string PolygonType1::GetDefaultName(const std::string& prefix, uint32_t address) +PolygonType2::PolygonType2(ZFile* nParent, const std::vector<uint8_t>& nRawData, + uint32_t nRawDataIndex, ZRoom* nRoom) + : PolygonTypeBase(nParent, nRawData, nRawDataIndex, nRoom) { - return StringHelper::Sprintf("%sPolygonType1_%06X", prefix.c_str(), address); } -std::string PolygonType1::GetSourceTypeName() +void PolygonType2::ParseRawData() { - switch (format) + num = BitConverter::ToUInt8BE(rawData, rawDataIndex + 0x01); + + start = BitConverter::ToUInt32BE(rawData, rawDataIndex + 0x04); + end = BitConverter::ToUInt32BE(rawData, rawDataIndex + 0x08); + + uint32_t currentPtr = GETSEGOFFSET(start); + for (size_t i = 0; i < num; i++) { - case 1: - return "MeshHeader1Single"; + PolygonDlist entry(zRoom->GetName(), rawData, currentPtr, parent, zRoom); + entry.SetPolyType(type); + entry.ParseRawData(); + entry.DeclareReferences(zRoom->GetName()); + polyDLists.push_back(entry); + currentPtr += entry.GetRawDataSize(); + } +} - case 2: - return "MeshHeader1Multi"; +void PolygonType2::DeclareReferences(const std::string& prefix) +{ + if (num > 0) + { + std::string declaration = ""; + + for (size_t i = 0; i < polyDLists.size(); i++) + { + declaration += polyDLists.at(i).GetBodySourceCode(true); + if (i + 1 < polyDLists.size()) + declaration += "\n"; + } + + std::string polyDlistType = polyDLists.at(0).GetSourceTypeName(); + std::string polyDListName = ""; + polyDListName = StringHelper::Sprintf("%s%s_%06X", prefix.c_str(), polyDlistType.c_str(), + GETSEGOFFSET(start)); + + parent->AddDeclarationArray(GETSEGOFFSET(start), DeclarationAlignment::Align4, + polyDLists.size() * polyDLists.at(0).GetRawDataSize(), + polyDlistType, polyDListName, polyDLists.size(), declaration); } - return "ERROR"; - // return "PolygonType1"; + + parent->AddDeclaration(GETSEGOFFSET(end), DeclarationAlignment::Align4, + DeclarationPadding::Pad16, 4, "static s32", "terminatorMaybe", + "0x01000000"); } -std::string PolygonType1::GetName() +std::string PolygonType2::GetBodySourceCode() const { - return name; + std::string listName = parent->GetDeclarationPtrName(start); + + std::string body = StringHelper::Sprintf("\n %i, %i,\n", type, polyDLists.size()); + body += StringHelper::Sprintf(" %s,\n", listName.c_str()); + body += + StringHelper::Sprintf(" %s + ARRAY_COUNTU(%s)\n", listName.c_str(), listName.c_str()); + return body; +} + +size_t PolygonType2::GetRawDataSize() const +{ + return 0x0C; } |
