1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2025-08-10 08:50:23 +00:00

Update ZAPD (#1569)

* git subrepo pull --force tools/ZAPD

subrepo:
  subdir:   "tools/ZAPD"
  merged:   "094e79734"
upstream:
  origin:   "https://github.com/zeldaret/ZAPD.git"
  branch:   "master"
  commit:   "094e79734"
git-subrepo:
  version:  "0.4.6"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "110b9eb"

* Add EnumData.xml where some names are now externalized

* Remove legacy typedefs for zapd, no longer needed!
This commit is contained in:
Dragorn421 2023-10-25 03:36:10 +02:00 committed by GitHub
parent 503f6d86d5
commit 4e55168eaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
97 changed files with 4225 additions and 2328 deletions

View file

@ -181,6 +181,12 @@ void ZFile::ParseXML(tinyxml2::XMLElement* reader, const std::string& filename)
}
}
const char* segmentDefines = reader->Attribute("Defines");
if (segmentDefines != NULL)
{
makeDefines = true;
}
if (mode == ZFileMode::Extract || mode == ZFileMode::ExternalFile)
{
if (!File::Exists((basePath / name).string()))
@ -213,14 +219,12 @@ void ZFile::ParseXML(tinyxml2::XMLElement* reader, const std::string& filename)
// Check for repeated attributes.
if (offsetXml != nullptr)
{
std::string offsetStr = StringHelper::Split(offsetXml, "0x")[1];
if (!StringHelper::HasOnlyHexDigits(offsetStr))
if (!StringHelper::IsValidOffset(std::string_view(offsetXml)))
{
HANDLE_ERROR(WarningType::InvalidXML,
StringHelper::Sprintf("Invalid offset %s entered", offsetStr.c_str()),
"");
StringHelper::Sprintf("Invalid offset %s entered", offsetXml), "");
}
rawDataIndex = strtol(offsetStr.c_str(), NULL, 16);
rawDataIndex = strtol(offsetXml, NULL, 16);
if (offsetSet.find(offsetXml) != offsetSet.end())
{
@ -268,7 +272,7 @@ void ZFile::ParseXML(tinyxml2::XMLElement* reader, const std::string& filename)
ZResource* nRes = nodeMap[nodeName](this);
if (mode == ZFileMode::Extract || mode == ZFileMode::ExternalFile)
nRes->ExtractFromXML(child, rawDataIndex);
nRes->ExtractWithXML(child, rawDataIndex);
switch (nRes->GetResourceType())
{
@ -442,24 +446,25 @@ Declaration* ZFile::AddDeclaration(offset_t address, DeclarationAlignment alignm
const std::string& varType, const std::string& varName,
const std::string& body)
{
bool validOffset = AddDeclarationChecks(address, varName);
bool validOffset = DeclarationSanityChecks(address, varName);
if (!validOffset)
return nullptr;
Declaration* decl = GetDeclaration(address);
if (decl == nullptr)
{
decl = new Declaration(address, alignment, size, varType, varName, false, body);
decl = Declaration::Create(address, alignment, size, varType, varName, body);
declarations[address] = decl;
}
else
{
decl->alignment = alignment;
decl->size = size;
decl->varType = varType;
decl->varName = varName;
decl->text = body;
decl->declType = varType;
decl->declName = varName;
decl->declBody = body;
}
return decl;
}
@ -468,27 +473,29 @@ Declaration* ZFile::AddDeclarationArray(offset_t address, DeclarationAlignment a
const std::string& varName, size_t arrayItemCnt,
const std::string& body)
{
bool validOffset = AddDeclarationChecks(address, varName);
bool validOffset = DeclarationSanityChecks(address, varName);
if (!validOffset)
return nullptr;
Declaration* decl = GetDeclaration(address);
if (decl == nullptr)
{
decl =
new Declaration(address, alignment, size, varType, varName, true, arrayItemCnt, body);
decl = Declaration::CreateArray(address, alignment, size, varType, varName, body,
arrayItemCnt);
declarations[address] = decl;
}
else
{
if (decl->isPlaceholder)
decl->varName = varName;
decl->declName = varName;
decl->alignment = alignment;
decl->size = size;
decl->varType = varType;
decl->declType = varType;
decl->isArray = true;
decl->arrayItemCnt = arrayItemCnt;
decl->text = body;
decl->declBody = body;
}
return decl;
@ -499,41 +506,41 @@ Declaration* ZFile::AddDeclarationArray(offset_t address, DeclarationAlignment a
const std::string& varName,
const std::string& arrayItemCntStr, const std::string& body)
{
bool validOffset = AddDeclarationChecks(address, varName);
bool validOffset = DeclarationSanityChecks(address, varName);
if (!validOffset)
return nullptr;
Declaration* decl = GetDeclaration(address);
if (decl == nullptr)
{
decl = new Declaration(address, alignment, size, varType, varName, true, arrayItemCntStr,
body);
decl = Declaration::CreateArray(address, alignment, size, varType, varName, body,
arrayItemCntStr);
declarations[address] = decl;
}
else
{
decl->alignment = alignment;
decl->size = size;
decl->varType = varType;
decl->varName = varName;
decl->declType = varType;
decl->declName = varName;
decl->isArray = true;
decl->arrayItemCntStr = arrayItemCntStr;
decl->text = body;
decl->declBody = body;
}
return decl;
}
Declaration* ZFile::AddDeclarationPlaceholder(offset_t address, const std::string& varName)
{
bool validOffset = AddDeclarationChecks(address, varName);
bool validOffset = DeclarationSanityChecks(address, varName);
if (!validOffset)
return nullptr;
Declaration* decl;
if (declarations.find(address) == declarations.end())
{
decl = new Declaration(address, DeclarationAlignment::Align4, 0, "", varName, false, "");
decl->isPlaceholder = true;
decl = Declaration::CreatePlaceholder(address, varName);
declarations[address] = decl;
}
else
@ -546,22 +553,22 @@ Declaration* ZFile::AddDeclarationInclude(offset_t address, const std::string& i
size_t size, const std::string& varType,
const std::string& varName)
{
bool validOffset = AddDeclarationChecks(address, varName);
bool validOffset = DeclarationSanityChecks(address, varName);
if (!validOffset)
return nullptr;
Declaration* decl = GetDeclaration(address);
if (decl == nullptr)
{
decl = new Declaration(address, includePath, size, varType, varName);
decl = Declaration::CreateInclude(address, includePath, size, varType, varName);
declarations[address] = decl;
}
else
{
decl->includePath = includePath;
decl->size = size;
decl->varType = varType;
decl->varName = varName;
decl->declType = varType;
decl->declName = varName;
}
return decl;
}
@ -570,7 +577,7 @@ Declaration* ZFile::AddDeclarationIncludeArray(offset_t address, std::string& in
size_t size, const std::string& varType,
const std::string& varName, size_t arrayItemCnt)
{
bool validOffset = AddDeclarationChecks(address, varName);
bool validOffset = DeclarationSanityChecks(address, varName);
if (!validOffset)
return nullptr;
@ -582,7 +589,7 @@ Declaration* ZFile::AddDeclarationIncludeArray(offset_t address, std::string& in
Declaration* decl = GetDeclaration(address);
if (decl == nullptr)
{
decl = new Declaration(address, includePath, size, varType, varName);
decl = Declaration::CreateInclude(address, includePath, size, varType, varName);
decl->isArray = true;
decl->arrayItemCnt = arrayItemCnt;
@ -592,8 +599,8 @@ Declaration* ZFile::AddDeclarationIncludeArray(offset_t address, std::string& in
else
{
decl->includePath = includePath;
decl->varType = varType;
decl->varName = varName;
decl->declType = varType;
decl->declName = varName;
decl->size = size;
decl->isArray = true;
decl->arrayItemCnt = arrayItemCnt;
@ -601,7 +608,44 @@ Declaration* ZFile::AddDeclarationIncludeArray(offset_t address, std::string& in
return decl;
}
bool ZFile::AddDeclarationChecks(uint32_t address, const std::string& varName)
Declaration* ZFile::AddDeclarationIncludeArray(offset_t address, std::string& includePath,
size_t size, const std::string& varType,
const std::string& varName,
const std::string& defines, size_t arrayItemCnt)
{
bool validOffset = DeclarationSanityChecks(address, varName);
if (!validOffset)
return nullptr;
if (StringHelper::StartsWith(includePath, "assets/extracted/"))
includePath = "assets/" + StringHelper::Split(includePath, "assets/extracted/")[1];
if (StringHelper::StartsWith(includePath, "assets/custom/"))
includePath = "assets/" + StringHelper::Split(includePath, "assets/custom/")[1];
Declaration* decl = GetDeclaration(address);
if (decl == nullptr)
{
decl = Declaration::CreateInclude(address, includePath, size, varType, varName, defines);
decl->isArray = true;
decl->arrayItemCnt = arrayItemCnt;
declarations[address] = decl;
}
else
{
decl->includePath = includePath;
decl->declType = varType;
decl->declName = varName;
decl->defines = defines;
decl->size = size;
decl->isArray = true;
decl->arrayItemCnt = arrayItemCnt;
}
return decl;
}
bool ZFile::DeclarationSanityChecks(uint32_t address, const std::string& varName)
{
assert(GETSEGNUM(address) == 0);
assert(varName != "");
@ -645,7 +689,7 @@ bool ZFile::GetDeclarationPtrName(segptr_t segAddress, const std::string& expect
if (expectedType != "" && expectedType != "void*")
{
if (expectedType != decl->varType && "static " + expectedType != decl->varType)
if (expectedType != decl->declType && "static " + expectedType != decl->declType)
{
declName = StringHelper::Sprintf("0x%08X", segAddress);
return false;
@ -653,9 +697,9 @@ bool ZFile::GetDeclarationPtrName(segptr_t segAddress, const std::string& expect
}
if (!decl->isArray)
declName = "&" + decl->varName;
declName = "&" + decl->declName;
else
declName = decl->varName;
declName = decl->declName;
return true;
}
@ -679,7 +723,7 @@ bool ZFile::GetDeclarationArrayIndexedName(segptr_t segAddress, size_t elementSi
if (expectedType != "" && expectedType != "void*")
{
if (expectedType != decl->varType && "static " + expectedType != decl->varType)
if (expectedType != decl->declType && "static " + expectedType != decl->declType)
{
declName = StringHelper::Sprintf("0x%08X", segAddress);
return false;
@ -688,7 +732,7 @@ bool ZFile::GetDeclarationArrayIndexedName(segptr_t segAddress, size_t elementSi
if (decl->address == address)
{
declName = decl->varName;
declName = decl->declName;
return true;
}
@ -699,7 +743,7 @@ bool ZFile::GetDeclarationArrayIndexedName(segptr_t segAddress, size_t elementSi
}
uint32_t index = (address - decl->address) / elementSize;
declName = StringHelper::Sprintf("&%s[%u]", decl->varName.c_str(), index);
declName = StringHelper::Sprintf("&%s[%u]", decl->declName.c_str(), index);
return true;
}
@ -728,6 +772,20 @@ bool ZFile::HasDeclaration(offset_t address)
return declarations.find(address) != declarations.end();
}
size_t ZFile::GetDeclarationSizeFromNeighbor(uint32_t declarationAddress)
{
auto currentDecl = declarations.find(declarationAddress);
if (currentDecl == declarations.end())
return 0;
auto nextDecl = currentDecl;
std::advance(nextDecl, 1);
if (nextDecl == declarations.end())
return GetRawData().size() - currentDecl->first;
return nextDecl->first - currentDecl->first;
}
void ZFile::GenerateSourceFiles()
{
std::string sourceOutput;
@ -817,8 +875,8 @@ void ZFile::GenerateSourceHeaderFiles()
std::string ZFile::GetHeaderInclude() const
{
std::string headers = StringHelper::Sprintf("#include \"%s.h\"\n",
(outName.parent_path() / outName.stem()).c_str());
std::string headers = StringHelper::Sprintf(
"#include \"%s.h\"\n", (outName.parent_path() / outName.stem()).string().c_str());
return headers;
}
@ -843,16 +901,12 @@ std::string ZFile::GetExternalFileHeaderInclude() const
{
fs::path outputFolderPath = externalFile->GetSourceOutputFolderPath();
if (outputFolderPath == this->GetSourceOutputFolderPath())
{
outputFolderPath = externalFile->outName.stem();
}
else
{
outputFolderPath /= externalFile->outName.stem();
}
externalFilesIncludes +=
StringHelper::Sprintf("#include \"%s.h\"\n", outputFolderPath.c_str());
StringHelper::Sprintf("#include \"%s.h\"\n", outputFolderPath.string().c_str());
}
}
@ -967,46 +1021,7 @@ std::string ZFile::ProcessDeclarations()
// printf("RANGE START: 0x%06X - RANGE END: 0x%06X\n", rangeStart, rangeEnd);
// Optimization: See if there are any arrays side by side that can be merged...
std::vector<std::pair<int32_t, Declaration*>> declarationKeys(declarations.begin(),
declarations.end());
std::pair<int32_t, Declaration*> lastItem = declarationKeys.at(0);
for (size_t i = 1; i < declarationKeys.size(); i++)
{
std::pair<int32_t, Declaration*> curItem = declarationKeys[i];
if (curItem.second->isArray && lastItem.second->isArray)
{
if (curItem.second->varType == lastItem.second->varType)
{
if (!curItem.second->declaredInXml && !lastItem.second->declaredInXml)
{
// TEST: For now just do Vtx declarations...
if (lastItem.second->varType == "Vtx")
{
int32_t sizeDiff = curItem.first - (lastItem.first + lastItem.second->size);
// Make sure there isn't an unaccounted inbetween these two
if (sizeDiff == 0)
{
lastItem.second->size += curItem.second->size;
lastItem.second->arrayItemCnt += curItem.second->arrayItemCnt;
lastItem.second->text += "\n" + curItem.second->text;
declarations.erase(curItem.first);
declarationKeys.erase(declarationKeys.begin() + i);
delete curItem.second;
i--;
continue;
}
}
}
}
}
lastItem = curItem;
}
MergeNeighboringDeclarations();
for (std::pair<uint32_t, Declaration*> item : declarations)
ProcessDeclarationText(item.second);
@ -1041,20 +1056,20 @@ std::string ZFile::ProcessDeclarations()
// HACK
std::string extType;
if (item.second->varType == "Gfx")
if (item.second->declType == "Gfx")
extType = "dlist";
else if (item.second->varType == "Vtx")
else if (item.second->declType == "Vtx")
extType = "vtx";
auto filepath = outputPath / item.second->varName;
auto filepath = outputPath / item.second->declName;
File::WriteAllText(
StringHelper::Sprintf("%s.%s.inc", filepath.c_str(), extType.c_str()),
item.second->text);
StringHelper::Sprintf("%s.%s.inc", filepath.string().c_str(), extType.c_str()),
item.second->declBody);
}
output += item.second->GetExternalDeclarationStr();
}
else if (item.second->varType != "")
else if (item.second->declType != "")
{
output += item.second->GetNormalDeclarationStr();
}
@ -1063,6 +1078,50 @@ std::string ZFile::ProcessDeclarations()
return output;
}
void ZFile::MergeNeighboringDeclarations()
{
// Optimization: See if there are any arrays side by side that can be merged...
std::vector<std::pair<int32_t, Declaration*>> declarationKeys(declarations.begin(),
declarations.end());
std::pair<int32_t, Declaration*> lastItem = declarationKeys.at(0);
for (size_t i = 1; i < declarationKeys.size(); i++)
{
std::pair<int32_t, Declaration*> curItem = declarationKeys[i];
if (curItem.second->isArray && lastItem.second->isArray)
{
if (curItem.second->declType == lastItem.second->declType)
{
if (!curItem.second->declaredInXml && !lastItem.second->declaredInXml)
{
// TEST: For now just do Vtx declarations...
if (lastItem.second->declType == "Vtx")
{
int32_t sizeDiff = curItem.first - (lastItem.first + lastItem.second->size);
// Make sure there isn't an unaccounted inbetween these two
if (sizeDiff == 0)
{
lastItem.second->size += curItem.second->size;
lastItem.second->arrayItemCnt += curItem.second->arrayItemCnt;
lastItem.second->declBody += "\n" + curItem.second->declBody;
declarations.erase(curItem.first);
declarationKeys.erase(declarationKeys.begin() + i);
delete curItem.second;
i--;
continue;
}
}
}
}
}
lastItem = curItem;
}
}
void ZFile::ProcessDeclarationText(Declaration* decl)
{
size_t refIndex = 0;
@ -1070,17 +1129,17 @@ void ZFile::ProcessDeclarationText(Declaration* decl)
if (!(decl->references.size() > 0))
return;
for (size_t i = 0; i < decl->text.size() - 1; i++)
for (size_t i = 0; i < decl->declBody.size() - 1; i++)
{
char c = decl->text[i];
char c2 = decl->text[i + 1];
char c = decl->declBody[i];
char c2 = decl->declBody[i + 1];
if (c == '@' && c2 == 'r')
{
std::string vtxName;
Globals::Instance->GetSegmentedArrayIndexedName(decl->references[refIndex], 0x10, this,
"Vtx", vtxName);
decl->text.replace(i, 2, vtxName);
decl->declBody.replace(i, 2, vtxName);
refIndex++;
@ -1092,7 +1151,8 @@ void ZFile::ProcessDeclarationText(Declaration* decl)
std::string ZFile::ProcessExterns()
{
std::string output;
std::string output = "";
bool hadDefines = true; // Previous declaration included defines.
for (const auto& item : declarations)
{
@ -1101,10 +1161,21 @@ std::string ZFile::ProcessExterns()
continue;
}
std::string itemDefines = item.second->GetDefinesStr();
// Add a newline above if previous has no defines and this one does.
if (!hadDefines && (itemDefines.length() > 0))
{
output.push_back('\n');
}
output += item.second->GetExternStr();
}
output += itemDefines;
output += "\n";
// Newline below if this one has defines.
if ((hadDefines = (itemDefines.length() > 0)))
{
output.push_back('\n');
}
}
output += defines;
@ -1153,7 +1224,7 @@ std::string ZFile::ProcessTextureIntersections([[maybe_unused]] const std::strin
if (nextDecl == nullptr)
texNextName = texturesResources.at(nextOffset)->GetName();
else
texNextName = nextDecl->varName;
texNextName = nextDecl->declName;
defines += StringHelper::Sprintf("#define %s ((u32)%s + 0x%06X)\n",
texNextName.c_str(), texName.c_str(), offsetDiff);
@ -1226,8 +1297,8 @@ bool ZFile::HandleUnaccountedAddress(offset_t currentAddress, offset_t lastAddr,
std::string intersectionInfo = StringHelper::Sprintf(
"Resource from 0x%06X:0x%06X (%s) conflicts with 0x%06X (%s).", lastAddr,
lastAddr + lastSize, lastDecl->varName.c_str(), currentAddress,
currentDecl->varName.c_str());
lastAddr + lastSize, lastDecl->declName.c_str(), currentAddress,
currentDecl->declName.c_str());
HANDLE_WARNING_RESOURCE(WarningType::Intersection, this, nullptr, currentAddress,
"intersection detected", intersectionInfo);
}