Shipwright/ZAPDTR/ZAPD/ZResource.cpp

440 lines
10 KiB
C++
Raw Normal View History

#include "ZResource.h"
#include <cassert>
#include <regex>
#include "Utils/StringHelper.h"
#include "WarningHandler.h"
#include "ZFile.h"
#include <Globals.h>
#include <set>
#include <ZDisplayList.h>
#include <ZArray.h>
ZResource::ZResource(ZFile* nParent)
{
// assert(nParent != nullptr);
parent = nParent;
name = "";
outName = "";
sourceOutput = "";
rawDataIndex = 0;
outputDeclaration = true;
hash = 0;
RegisterRequiredAttribute("Name");
RegisterOptionalAttribute("OutName");
RegisterOptionalAttribute("Offset");
RegisterOptionalAttribute("Custom");
RegisterOptionalAttribute("Static", "Global");
}
void ZResource::ExtractFromXML(tinyxml2::XMLElement* reader, offset_t nRawDataIndex)
{
rawDataIndex = nRawDataIndex;
declaredInXml = true;
if (reader != nullptr)
ParseXML(reader);
// Don't parse raw data of external files
if (parent->GetMode() != ZFileMode::ExternalFile)
{
ParseRawData();
CalcHash();
}
if (!isInner)
{
Declaration* decl = DeclareVar(parent->GetName(), "");
if (decl != nullptr)
{
decl->declaredInXml = true;
decl->staticConf = staticConf;
}
}
}
void ZResource::ExtractFromFile(offset_t nRawDataIndex)
{
rawDataIndex = nRawDataIndex;
// Don't parse raw data of external files
if (parent->GetMode() == ZFileMode::ExternalFile)
return;
ParseRawData();
CalcHash();
}
void ZResource::ParseXML(tinyxml2::XMLElement* reader)
{
if (reader != nullptr)
{
// If it is an inner node, then 'Name' isn't required
if (isInner)
{
registeredAttributes.at("Name").isRequired = false;
}
auto attrs = reader->FirstAttribute();
while (attrs != nullptr)
{
std::string attrName = attrs->Name();
bool attrDeclared = false;
if (registeredAttributes.find(attrName) != registeredAttributes.end())
{
registeredAttributes[attrName].value = attrs->Value();
registeredAttributes[attrName].wasSet = true;
attrDeclared = true;
}
if (!attrDeclared)
{
HANDLE_WARNING_RESOURCE(
WarningType::UnknownAttribute, parent, this, rawDataIndex,
StringHelper::Sprintf("unexpected '%s' attribute in resource <%s>",
attrName.c_str(), reader->Name()),
"");
}
attrs = attrs->Next();
}
2022-06-10 13:37:50 -04:00
if (!Globals::Instance->otrMode)
{
2022-06-10 13:37:50 -04:00
if (!canHaveInner && !reader->NoChildren())
{
std::string errorHeader = StringHelper::Sprintf(
"resource '%s' with inner element/child detected", reader->Name());
HANDLE_ERROR_PROCESS(WarningType::InvalidXML, errorHeader, "");
}
}
for (const auto& attr : registeredAttributes)
{
if (attr.second.isRequired && attr.second.value == "")
{
std::string headerMsg =
StringHelper::Sprintf("missing required attribute '%s' in resource <%s>",
attr.first.c_str(), reader->Name());
HANDLE_ERROR_RESOURCE(WarningType::MissingAttribute, parent, this, rawDataIndex,
headerMsg, "");
}
}
name = registeredAttributes.at("Name").value;
// Disable this check for OTR file generation for now since it takes up a considerable amount of CPU time
if (!Globals::Instance->otrMode)
{
static std::regex r("[a-zA-Z_]+[a-zA-Z0-9_]*",
std::regex::icase | std::regex::optimize);
if (!isInner || (isInner && name != ""))
{
if (!std::regex_match(name, r))
{
HANDLE_ERROR_RESOURCE(WarningType::InvalidAttributeValue, parent, this,
rawDataIndex, "invalid value found for 'Name' attribute",
"");
}
}
}
outName = registeredAttributes.at("OutName").value;
if (outName == "")
outName = name;
isCustomAsset = registeredAttributes["Custom"].wasSet;
std::string& staticXml = registeredAttributes["Static"].value;
if (staticXml == "Global")
{
staticConf = StaticConfig::Global;
}
else if (staticXml == "On")
{
staticConf = StaticConfig::On;
}
else if (staticXml == "Off")
{
staticConf = StaticConfig::Off;
}
else
{
HANDLE_ERROR_RESOURCE(
WarningType::InvalidAttributeValue, parent, this, rawDataIndex,
StringHelper::Sprintf("invalid value '%s' for 'Static' attribute", staticConf), "");
}
declaredInXml = true;
}
}
void ZResource::ParseRawData()
{
}
void ZResource::DeclareReferences([[maybe_unused]] const std::string& prefix)
{
}
void ZResource::ParseRawDataLate()
{
}
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)
{
}
const std::string& ZResource::GetName() const
{
return name;
}
const std::string& ZResource::GetOutName() const
{
return outName;
}
void ZResource::SetOutName(const std::string& nName)
{
outName = nName;
}
void ZResource::SetName(const std::string& nName)
{
name = nName;
}
bool ZResource::IsExternalResource() const
{
return false;
}
bool ZResource::DoesSupportArray() const
{
return false;
}
std::string ZResource::GetExternalExtension() const
{
return "";
}
DeclarationAlignment ZResource::GetDeclarationAlignment() const
{
return DeclarationAlignment::Align4;
}
bool ZResource::WasDeclaredInXml() const
{
return declaredInXml;
}
StaticConfig ZResource::GetStaticConf() const
{
return staticConf;
}
offset_t ZResource::GetRawDataIndex() const
{
return rawDataIndex;
}
ZAPD Update (#2851) * git subrepo clone --force --branch=rebase2 C:/ZeldaStuff/ZAPDTR ZAPDTR/ subrepo: subdir: "ZAPDTR" merged: "6aa54a551" upstream: origin: "C:/ZeldaStuff/ZAPDTR" branch: "rebase2" commit: "6aa54a551" git-subrepo: version: "0.4.3" origin: "???" commit: "???" * git subrepo clone --force --branch=rebase2 C:/ZeldaStuff/ZAPDTR ZAPDTR/ subrepo: subdir: "ZAPDTR" merged: "88b012240" upstream: origin: "C:/ZeldaStuff/ZAPDTR" branch: "rebase2" commit: "88b012240" git-subrepo: version: "0.4.3" origin: "???" commit: "???" * Update (its broken) * fix the enum * git subrepo push --remote=C:/ZeldaStuff/ZAPDTR/ ZAPDTR subrepo: subdir: "ZAPDTR" merged: "b7b6e1c82" upstream: origin: "C:/ZeldaStuff/ZAPDTR/" branch: "rebase2" commit: "b7b6e1c82" git-subrepo: version: "0.4.3" origin: "???" commit: "???" * New names for LUS actions * git subrepo push --remote=C:/ZeldaStuff/ZAPDTR/ ZAPDTR subrepo: subdir: "ZAPDTR" merged: "c5cfebeee" upstream: origin: "C:/ZeldaStuff/ZAPDTR/" branch: "rebase2" commit: "c5cfebeee" git-subrepo: version: "0.4.3" origin: "???" commit: "???" * git subrepo clone (merge) --force --branch=rebase2 C:/ZeldaStuff/ZAPDTR ZAPDTR/ subrepo: subdir: "ZAPDTR" merged: "d5f4769b8" upstream: origin: "C:/ZeldaStuff/ZAPDTR" branch: "rebase2" commit: "d5f4769b8" git-subrepo: version: "0.4.3" origin: "???" commit: "???" * Fix missing commands in the exporter. * Cleanups. * git subrepo pull --force --remote=https://github.com/harbourmasters/ZAPDTR --branch=master ZAPDTR subrepo: subdir: "ZAPDTR" merged: "d4c35b90a" upstream: origin: "https://github.com/harbourmasters/ZAPDTR" branch: "master" commit: "d4c35b90a" git-subrepo: version: "0.4.3" origin: "???" commit: "???" * Add unordered_map include to fix MacOS * fix string_view * Update Main.cpp * fix string view * So close I can almost taste it * So close * Fix missed git marker. * Fix surface types and * Update ZFile.cpp * Delete Jenkinsfile --------- Co-authored-by: Christopher Leggett <chris@leggett.dev> Co-authored-by: briaguya <70942617+briaguya-ai@users.noreply.github.com>
2023-05-07 19:58:50 -04:00
void ZResource::SetRawDataIndex(offset_t nRawDataIndex)
{
rawDataIndex = nRawDataIndex;
}
std::string ZResource::GetBodySourceCode() const
{
return "ERROR";
}
std::string ZResource::GetDefaultName(const std::string& prefix) const
{
return StringHelper::Sprintf("%s%s_%06X", prefix.c_str(), GetSourceTypeName().c_str(),
rawDataIndex);
}
void ZResource::GetSourceOutputCode([[maybe_unused]] const std::string& prefix)
{
std::string bodyStr = GetBodySourceCode();
if (bodyStr != "ERROR")
{
Declaration* decl = parent->GetDeclaration(rawDataIndex);
if (decl == nullptr || decl->isPlaceholder)
decl = DeclareVar(prefix, bodyStr);
else
decl->text = bodyStr;
// OTRTODO: This is a hack and we need something more elegant in the future...
if (GetResourceType() == ZResourceType::Array)
{
ZArray* arr = (ZArray*)this;
if (arr->resList[0]->GetResourceType() == ZResourceType::Vertex)
{
for (int i = 0; i < arr->resList.size(); i++)
{
ZVtx* vtx = (ZVtx*)arr->resList[i];
decl->vertexHack.push_back(vtx);
}
}
}
if (decl != nullptr)
decl->staticConf = staticConf;
}
}
std::string ZResource::GetSourceOutputHeader([[maybe_unused]] const std::string& prefix, std::set<std::string> *nameSet)
{
if (Globals::Instance->otrMode && genOTRDef)
{
2023-03-02 09:37:47 -05:00
std::string str = "";
std::string nameStr = StringHelper::Strip(StringHelper::Strip(name, "\n"), "\r");
std::string outName = parent->GetOutName();
std::string prefix = "";
if (GetResourceType() == ZResourceType::DisplayList || GetResourceType() == ZResourceType::Texture)
{
//ZDisplayList* dList = (ZDisplayList*)this;
if (StringHelper::Contains(outName, "_room_"))
{
outName = StringHelper::Split(outName, "_room")[0] + "_scene";
}
}
std::string xmlPath = StringHelper::Replace(parent->GetXmlFilePath().string(), "\\", "/");
if (StringHelper::Contains(outName, "_room_") || StringHelper::Contains(outName, "_scene"))
Dual OTR MQ and Vanilla Support (#1694) * Changes OTR Extraction to have specific mq and nonmq paths. Also updates the game to load resources according to whether or not Master Quest or Vanilla is loaded. * Removes unneeded code from the last commit. * Fixes some weird formatting in ZRom.c * Loads oot-mq.otr and patches oot.otr on top, if both are present. If only one or the other are present, it becomes the only and main OTR. * Adds ImGui Logic for whether or an MQ Checkbox. Checkbox checked only specifies whether new saves should be MQ or not. Checkbox is disabled or force-enabled according to which OTRs are loaded. Also as a necessity includes tracking what game versions have been loaded from the OTRs. * Adds MQ settings logic for Randomizer's ImGui menu. * Writes Master Quest dungeons to the spoiler log * Loads MQ Dungeons from spoiler, persists in save, and loads when appropriate. * Adds logic to prevent loading or creating incompatible rando saves. * Fixdes some linux build issues and new rando save issues * Makes appimage create both vanilla and mq otrs If either rom is present, it makes the corresponding OTR. If both are present, it will make both. If one OTR is present but both roms are present, it will create the missing OTR. * Makes it so a randomized save file will not be marked as MQ. * Refactors to load all OTRs from MainPath or a specific list. Also adds the ability to take a std::unordered_set of hashes to validate each OTR's version file against. * Fixes a syntax error * Makes ExtractAssets output Vanilla and MQ OTRs if both roms are present * Fixes asset generation bug. * Partially working fix for dual OTR extract_assets Currently the cmake ExtractAssets target will return with a 1 if you only end up exporting one type of OTR isntead of both. Haven't found a great way to only attempt to copy a file if it exists from within cmake. It does actually correctly copy the OTR that is generated, despite the error from copying the other one. Pushing as is for now but will keep investigating. * Adds oot-mq.otr to the gitignore. * Makes ExtractAssets not fail on only one rom/OTR. * Removes PatchesPath from the constructors requiring OTRFiles vector. * Renames OOT_UNKNOWN to just UNKNOWN to remove OOT specific reference. * Removes randomizing MQ Dungeons and re-disables MQ rando. Doing this so the PR can get merged quicker with just the Dual OTR support and won't need to wait on rando logic to be updated. That will happen in another PR directly after the merge. * Update mac startup script for dual otr * Update soh/macosx/soh-macos.sh * Update soh/macosx/soh-macos.sh * Update soh/macosx/soh-macos.sh * Implements new BinaryReader to fix Linux build issue. BinaryReader itself comes from https://github.com/Moneyl/BinaryTools I added a wrapper to adapt it to the ABI from ZAPD's Binary Reader and add Endianness checking. I also had to copy a handful of other bits and pieces from ZAPD to make it all function as expected. * A few edits to the updatream BinaryReader to compile it on Linux. * Adds the Endianness to the first byte of the version file. * Fixes Jenkins * Addresses some of Kenix's comments * Renames `ReadNullTerminatedString` to `ReadCString` * Refactors Archive::LoadFile into a private method with more arguments. * Removes BitConverter and extends existing endianness.h instead. * Fixes an endianness issue with the version file. Co-authored-by: Garrett Cox <garrettjcox@gmail.com>
2022-10-16 23:07:35 -04:00
prefix = "scenes/nonmq";
else if (StringHelper::Contains(xmlPath, "objects/"))
prefix = "objects";
else if (StringHelper::Contains(xmlPath, "textures/"))
prefix = "textures";
else if (StringHelper::Contains(xmlPath, "overlays/"))
prefix = "overlays";
else if (StringHelper::Contains(xmlPath, "misc/"))
prefix = "misc";
else if (StringHelper::Contains(xmlPath, "code/"))
prefix = "code";
else if (StringHelper::Contains(xmlPath, "text/"))
prefix = "text";
Dual OTR MQ and Vanilla Support (#1694) * Changes OTR Extraction to have specific mq and nonmq paths. Also updates the game to load resources according to whether or not Master Quest or Vanilla is loaded. * Removes unneeded code from the last commit. * Fixes some weird formatting in ZRom.c * Loads oot-mq.otr and patches oot.otr on top, if both are present. If only one or the other are present, it becomes the only and main OTR. * Adds ImGui Logic for whether or an MQ Checkbox. Checkbox checked only specifies whether new saves should be MQ or not. Checkbox is disabled or force-enabled according to which OTRs are loaded. Also as a necessity includes tracking what game versions have been loaded from the OTRs. * Adds MQ settings logic for Randomizer's ImGui menu. * Writes Master Quest dungeons to the spoiler log * Loads MQ Dungeons from spoiler, persists in save, and loads when appropriate. * Adds logic to prevent loading or creating incompatible rando saves. * Fixdes some linux build issues and new rando save issues * Makes appimage create both vanilla and mq otrs If either rom is present, it makes the corresponding OTR. If both are present, it will make both. If one OTR is present but both roms are present, it will create the missing OTR. * Makes it so a randomized save file will not be marked as MQ. * Refactors to load all OTRs from MainPath or a specific list. Also adds the ability to take a std::unordered_set of hashes to validate each OTR's version file against. * Fixes a syntax error * Makes ExtractAssets output Vanilla and MQ OTRs if both roms are present * Fixes asset generation bug. * Partially working fix for dual OTR extract_assets Currently the cmake ExtractAssets target will return with a 1 if you only end up exporting one type of OTR isntead of both. Haven't found a great way to only attempt to copy a file if it exists from within cmake. It does actually correctly copy the OTR that is generated, despite the error from copying the other one. Pushing as is for now but will keep investigating. * Adds oot-mq.otr to the gitignore. * Makes ExtractAssets not fail on only one rom/OTR. * Removes PatchesPath from the constructors requiring OTRFiles vector. * Renames OOT_UNKNOWN to just UNKNOWN to remove OOT specific reference. * Removes randomizing MQ Dungeons and re-disables MQ rando. Doing this so the PR can get merged quicker with just the Dual OTR support and won't need to wait on rando logic to be updated. That will happen in another PR directly after the merge. * Update mac startup script for dual otr * Update soh/macosx/soh-macos.sh * Update soh/macosx/soh-macos.sh * Update soh/macosx/soh-macos.sh * Implements new BinaryReader to fix Linux build issue. BinaryReader itself comes from https://github.com/Moneyl/BinaryTools I added a wrapper to adapt it to the ABI from ZAPD's Binary Reader and add Endianness checking. I also had to copy a handful of other bits and pieces from ZAPD to make it all function as expected. * A few edits to the updatream BinaryReader to compile it on Linux. * Adds the Endianness to the first byte of the version file. * Fixes Jenkins * Addresses some of Kenix's comments * Renames `ReadNullTerminatedString` to `ReadCString` * Refactors Archive::LoadFile into a private method with more arguments. * Removes BitConverter and extends existing endianness.h instead. * Fixes an endianness issue with the version file. Co-authored-by: Garrett Cox <garrettjcox@gmail.com>
2022-10-16 23:07:35 -04:00
if (prefix != "") {
str += StringHelper::Sprintf("#define d%s \"__OTR__%s/%s/%s\"", name.c_str(), prefix.c_str(), outName.c_str(), nameStr.c_str());
2023-03-02 09:37:47 -05:00
}
else
str += StringHelper::Sprintf("#define d%s \"__OTR__%s/%s\"", name.c_str(), outName.c_str(), nameStr.c_str());
if (nameSet && nameSet->find(name) == nameSet->end()) {
2023-03-02 09:37:47 -05:00
str += StringHelper::Sprintf("\n");
str += StringHelper::Sprintf(R"(static const ALIGN_ASSET(2) char %s[] = d%s;)", name.c_str(), name.c_str());
if (nameSet) {
nameSet->insert(name);
}
}
if (name == "gTitleZeldaShieldLogoMQTex")
{
std::string addName = "gTitleZeldaShieldLogoTex";
nameStr = StringHelper::Strip(StringHelper::Strip(addName, "\n"), "\r");
2023-03-02 09:37:47 -05:00
str += StringHelper::Sprintf("\n\n#define d%s \"__OTR__%s/%s/%s\"", addName.c_str(), prefix.c_str(), outName.c_str(), nameStr.c_str());
if (nameSet && nameSet->find(addName) == nameSet->end())
{
2023-03-02 09:37:47 -05:00
str += StringHelper::Sprintf("\n");
str += StringHelper::Sprintf(R"(static const ALIGN_ASSET(2) char %s[] = d%s;)", addName.c_str(), addName.c_str());
if (nameSet)
{
nameSet->insert(addName);
}
}
}
return str;
}
else
return "";
}
ZResourceType ZResource::GetResourceType() const
{
return ZResourceType::Error;
}
void ZResource::CalcHash()
{
hash = 0;
}
void ZResource::SetInnerNode(bool inner)
{
isInner = inner;
}
void ZResource::RegisterRequiredAttribute(const std::string& attr)
{
ResourceAttribute resAtrr;
resAtrr.key = attr;
resAtrr.isRequired = true;
registeredAttributes[attr] = resAtrr;
}
void ZResource::RegisterOptionalAttribute(const std::string& attr, const std::string& defaultValue)
{
ResourceAttribute resAtrr;
resAtrr.key = attr;
resAtrr.value = defaultValue;
registeredAttributes[attr] = resAtrr;
}
offset_t Seg2Filespace(segptr_t segmentedAddress, uint32_t parentBaseAddress)
{
offset_t currentPtr = GETSEGOFFSET(segmentedAddress);
if (GETSEGNUM(segmentedAddress) == 0x80) // Is defined in code?
{
uint32_t parentBaseOffset = GETSEGOFFSET(parentBaseAddress);
if (parentBaseOffset > currentPtr)
{
HANDLE_ERROR(WarningType::Always,
StringHelper::Sprintf(
"resource address 0x%08X is smaller than 'BaseAddress' 0x%08X",
segmentedAddress, parentBaseAddress),
"Maybe your 'BaseAddress' is wrong?");
}
currentPtr -= parentBaseOffset;
}
return currentPtr;
}