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

git subrepo pull --force tools/ZAPD (#727)

subrepo:
  subdir:   "tools/ZAPD"
  merged:   "4751db5c9"
upstream:
  origin:   "https://github.com/zeldaret/ZAPD.git"
  branch:   "master"
  commit:   "4751db5c9"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo.git"
  commit:   "2f68596"
This commit is contained in:
fig02 2021-03-20 12:02:12 -04:00 committed by GitHub
parent 77ec4d4916
commit 493bdbc3c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
115 changed files with 16370 additions and 2789 deletions

View file

@ -1,24 +1,25 @@
#include "ZFile.h"
#include "ZBlob.h"
#include "ZDisplayList.h"
#include "ZRoom/ZRoom.h"
#include "ZTexture.h"
#include "ZAnimation.h"
#include "ZLimb.h"
#include "ZSkeleton.h"
#include "ZCollision.h"
#include "ZScalar.h"
#include "ZVector.h"
#include "ZVtx.h"
#include "ZCutscene.h"
#include "ZArray.h"
#include "Path.h"
#include "File.h"
#include "Directory.h"
#include "Globals.h"
#include "HighLevel/HLModelIntermediette.h"
#include <algorithm>
#include <cassert>
#include "Directory.h"
#include "File.h"
#include "Globals.h"
#include "HighLevel/HLModelIntermediette.h"
#include "Path.h"
#include "ZAnimation.h"
#include "ZArray.h"
#include "ZBlob.h"
#include "ZCollision.h"
#include "ZCutscene.h"
#include "ZDisplayList.h"
#include "ZLimb.h"
#include "ZRoom/ZRoom.h"
#include "ZScalar.h"
#include "ZSkeleton.h"
#include "ZSymbol.h"
#include "ZTexture.h"
#include "ZVector.h"
#include "ZVtx.h"
using namespace tinyxml2;
using namespace std;
@ -28,7 +29,7 @@ ZFile::ZFile()
resources = vector<ZResource*>();
basePath = "";
outputPath = Directory::GetCurrentDirectory();
declarations = map<int32_t, Declaration*>();
declarations = map<uint32_t, Declaration*>();
defines = "";
baseAddress = 0;
rangeStart = 0x000000000;
@ -41,7 +42,9 @@ ZFile::ZFile(string nOutPath, string nName) : ZFile()
name = nName;
}
ZFile::ZFile(ZFileMode mode, XMLElement* reader, string nBasePath, string nOutPath, std::string filename, bool placeholderMode) : ZFile()
ZFile::ZFile(ZFileMode mode, XMLElement* reader, string nBasePath, string nOutPath,
std::string filename, bool placeholderMode)
: ZFile()
{
if (nBasePath == "")
basePath = Directory::GetCurrentDirectory();
@ -71,7 +74,8 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
int segment = -1;
// TODO: This should be a variable on the ZFile, but it is a large change in order to force all ZResource types to have a parent ZFile.
// TODO: This should be a variable on the ZFile, but it is a large change in order to force all
// ZResource types to have a parent ZFile.
const char* gameStr = reader->Attribute("Game");
if (reader->Attribute("Game") != nullptr)
{
@ -90,39 +94,45 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
}
if (reader->Attribute("BaseAddress") != NULL)
baseAddress = (uint32_t)strtoul(StringHelper::Split(reader->Attribute("BaseAddress"), "0x")[1].c_str(), NULL, 16);
baseAddress = (uint32_t)strtoul(
StringHelper::Split(reader->Attribute("BaseAddress"), "0x")[1].c_str(), NULL, 16);
if (reader->Attribute("RangeStart") != NULL)
rangeStart = (uint32_t)strtoul(StringHelper::Split(reader->Attribute("RangeStart"), "0x")[1].c_str(), NULL, 16);
rangeStart = (uint32_t)strtoul(
StringHelper::Split(reader->Attribute("RangeStart"), "0x")[1].c_str(), NULL, 16);
if (reader->Attribute("RangeEnd") != NULL)
rangeEnd = (uint32_t)strtoul(StringHelper::Split(reader->Attribute("RangeEnd"), "0x")[1].c_str(), NULL, 16);
rangeEnd = (uint32_t)strtoul(
StringHelper::Split(reader->Attribute("RangeEnd"), "0x")[1].c_str(), NULL, 16);
if (reader->Attribute("Segment") != NULL)
segment = strtol(reader->Attribute("Segment"), NULL, 10);
if (segment != -1)
{
//printf("Adding Segment %i\n", segment);
// printf("Adding Segment %i\n", segment);
Globals::Instance->AddSegment(segment);
}
string folderName = basePath + "/" + Path::GetFileNameWithoutExtension(name);
if (mode == ZFileMode::Extract)
{
if (!File::Exists(basePath + "/" + name))
throw StringHelper::Sprintf("Error! File %s does not exist.", (basePath + "/" + name).c_str());
throw StringHelper::Sprintf("Error! File %s does not exist.",
(basePath + "/" + name).c_str());
rawData = File::ReadAllBytes(basePath + "/" + name);
}
int rawDataIndex = 0;
for (XMLElement* child = reader->FirstChildElement(); child != NULL; child = child->NextSiblingElement())
for (XMLElement* child = reader->FirstChildElement(); child != NULL;
child = child->NextSiblingElement())
{
if (child->Attribute("Offset") != NULL)
rawDataIndex = strtol(StringHelper::Split(child->Attribute("Offset"), "0x")[1].c_str(), NULL, 16);
rawDataIndex =
strtol(StringHelper::Split(child->Attribute("Offset"), "0x")[1].c_str(), NULL, 16);
if (Globals::Instance->verbosity >= VERBOSITY_INFO)
printf("%s: 0x%06X\n", child->Attribute("Name"), rawDataIndex);
@ -163,9 +173,15 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
ZResource* dList = nullptr;
if (mode == ZFileMode::Extract)
dList = ZDisplayList::ExtractFromXML(child, rawData, rawDataIndex, ZDisplayList::GetDListLength(rawData, rawDataIndex, Globals::Instance->game == ZGame::OOT_SW97 ? DListType::F3DEX : DListType::F3DZEX), folderName);
//else
//dList = ZDisplayList::BuildFromXML(child, folderName, mode == ZFileMode::Build);
dList = ZDisplayList::ExtractFromXML(
child, rawData, rawDataIndex,
ZDisplayList::GetDListLength(rawData, rawDataIndex,
Globals::Instance->game == ZGame::OOT_SW97 ?
DListType::F3DEX :
DListType::F3DZEX),
folderName);
// else
// dList = ZDisplayList::BuildFromXML(child, folderName, mode == ZFileMode::Build);
else
dList = ZBlob::BuildFromXML(child, folderName, mode == ZFileMode::Build);
@ -180,7 +196,8 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
ZRoom* room = nullptr;
if (mode == ZFileMode::Extract)
room = ZRoom::ExtractFromXML(child, rawData, rawDataIndex, folderName, this, Globals::Instance->lastScene);
room = ZRoom::ExtractFromXML(child, rawData, rawDataIndex, folderName, this,
Globals::Instance->lastScene);
if (string(child->Name()) == "Scene")
{
@ -226,6 +243,23 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
rawDataIndex += anim->GetRawDataSize();
}
else if (string(child->Name()) == "CurveAnimation")
{
ZCurveAnimation* anim = nullptr;
if (mode == ZFileMode::Extract)
{
anim =
ZCurveAnimation::ExtractFromXML(child, rawData, rawDataIndex, folderName, this);
}
if (anim == nullptr)
{
throw std::runtime_error("Couldn't create ZCurveAnimation.");
}
resources.push_back(anim);
rawDataIndex += anim->GetRawDataSize();
}
else if (string(child->Name()) == "Skeleton")
{
ZSkeleton* skeleton = nullptr;
@ -233,6 +267,10 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
if (mode == ZFileMode::Extract)
skeleton = ZSkeleton::FromXML(child, rawData, rawDataIndex, folderName, this);
if (skeleton == nullptr)
{
throw std::runtime_error("Couldn't create ZSkeleton.");
}
resources.push_back(skeleton);
rawDataIndex += skeleton->GetRawDataSize();
}
@ -243,23 +281,27 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
if (mode == ZFileMode::Extract)
limb = ZLimb::FromXML(child, rawData, rawDataIndex, folderName, this);
if (limb == nullptr)
{
throw std::runtime_error("Couldn't create ZLimb.");
}
resources.push_back(limb);
rawDataIndex += limb->GetRawDataSize();
}
else if (string(child->Name()) == "Symbol")
{
ZResource* res = nullptr;
ZSymbol* symbol = nullptr;
if (mode == ZFileMode::Extract)
{
res = new ZResource();
res->SetName(child->Attribute("Name"));
res->SetRawDataIndex(rawDataIndex);
res->outputDeclaration = false;
symbol = ZSymbol::ExtractFromXML(child, rawData, rawDataIndex, this);
}
resources.push_back(res);
if (symbol == nullptr)
{
throw std::runtime_error("Couldn't create ZSymbol.");
}
resources.push_back(symbol);
}
else if (string(child->Name()) == "Collision")
{
@ -364,7 +406,8 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
else
{
std::cerr << "ERROR bad type\n";
printf("Encountered unknown resource type: %s on line: %d\n", child->Name(), child->GetLineNum());
printf("Encountered unknown resource type: %s on line: %d\n", child->Name(),
child->GetLineNum());
std::exit(EXIT_FAILURE);
}
}
@ -388,9 +431,9 @@ void ZFile::BuildResources()
for (ZResource* res : resources)
{
//Console.WriteLine("Building resource " + res.GetName());
// Console.WriteLine("Building resource " + res.GetName());
memcpy(file.data() + fileIndex, res->GetRawData().data(), res->GetRawData().size());
//Array.Copy(res.GetRawData(), 0, file, fileIndex, res.GetRawData().Length);
// Array.Copy(res.GetRawData(), 0, file, fileIndex, res.GetRawData().Length);
fileIndex += res->GetRawData().size();
}
@ -427,8 +470,8 @@ void ZFile::ExtractResources(string outputDir)
{
string folderName = Path::GetFileNameWithoutExtension(outputPath);
//printf("DIR CHECK: %s\n", folderName.c_str());
//printf("OUT CHECK: %s\n", outputDir.c_str());
// printf("DIR CHECK: %s\n", folderName.c_str());
// printf("OUT CHECK: %s\n", outputDir.c_str());
if (!Directory::Exists(outputPath))
Directory::CreateDirectory(outputPath);
@ -443,8 +486,8 @@ void ZFile::ExtractResources(string outputDir)
{
if (Globals::Instance->verbosity >= VERBOSITY_INFO)
printf("Saving resource %s\n", res->GetName().c_str());
res->CalcHash(); // TEST
res->CalcHash(); // TEST
res->Save(outputPath);
}
@ -457,7 +500,8 @@ void ZFile::AddResource(ZResource* res)
resources.push_back(res);
}
Declaration* ZFile::AddDeclaration(uint32_t address, DeclarationAlignment alignment, uint32_t size, std::string varType, std::string varName, std::string body)
Declaration* ZFile::AddDeclaration(uint32_t address, DeclarationAlignment alignment, uint32_t size,
std::string varType, std::string varName, std::string body)
{
#if _DEBUG
if (declarations.find(address) != declarations.end())
@ -473,7 +517,9 @@ Declaration* ZFile::AddDeclaration(uint32_t address, DeclarationAlignment alignm
return decl;
}
Declaration* ZFile::AddDeclaration(uint32_t address, DeclarationAlignment alignment, DeclarationPadding padding, uint32_t size, string varType, string varName, std::string body)
Declaration* ZFile::AddDeclaration(uint32_t address, DeclarationAlignment alignment,
DeclarationPadding padding, uint32_t size, string varType,
string varName, std::string body)
{
#if _DEBUG
if (declarations.find(address) != declarations.end())
@ -484,11 +530,14 @@ Declaration* ZFile::AddDeclaration(uint32_t address, DeclarationAlignment alignm
AddDeclarationDebugChecks(address);
declarations[address] = new Declaration(alignment, padding, size, varType, varName, false, body);
declarations[address] =
new Declaration(alignment, padding, size, varType, varName, false, body);
return declarations[address];
}
Declaration* ZFile::AddDeclarationArray(uint32_t address, DeclarationAlignment alignment, uint32_t size, std::string varType, std::string varName, int arrayItemCnt, std::string body)
Declaration* ZFile::AddDeclarationArray(uint32_t address, DeclarationAlignment alignment,
uint32_t size, std::string varType, std::string varName,
int arrayItemCnt, std::string body)
{
#if _DEBUG
if (declarations.find(address) != declarations.end())
@ -499,12 +548,14 @@ Declaration* ZFile::AddDeclarationArray(uint32_t address, DeclarationAlignment a
AddDeclarationDebugChecks(address);
declarations[address] = new Declaration(alignment, size, varType, varName, true, arrayItemCnt, body);
declarations[address] =
new Declaration(alignment, size, varType, varName, true, arrayItemCnt, body);
return declarations[address];
}
Declaration* ZFile::AddDeclarationArray(uint32_t address, DeclarationAlignment alignment, DeclarationPadding padding, uint32_t size, string varType, string varName, int arrayItemCnt, std::string body)
Declaration* ZFile::AddDeclarationArray(uint32_t address, DeclarationAlignment alignment,
DeclarationPadding padding, uint32_t size, string varType,
string varName, int arrayItemCnt, std::string body)
{
#if _DEBUG
if (declarations.find(address) != declarations.end())
@ -515,11 +566,11 @@ Declaration* ZFile::AddDeclarationArray(uint32_t address, DeclarationAlignment a
AddDeclarationDebugChecks(address);
declarations[address] = new Declaration(alignment, padding, size, varType, varName, true, arrayItemCnt, body);
declarations[address] =
new Declaration(alignment, padding, size, varType, varName, true, arrayItemCnt, body);
return declarations[address];
}
Declaration* ZFile::AddDeclarationPlaceholder(uint32_t address)
{
AddDeclarationDebugChecks(address);
@ -535,12 +586,14 @@ Declaration* ZFile::AddDeclarationPlaceholder(uint32_t address, string varName)
AddDeclarationDebugChecks(address);
if (declarations.find(address) == declarations.end())
declarations[address] = new Declaration(DeclarationAlignment::None, 0, "", varName, false, "");
declarations[address] =
new Declaration(DeclarationAlignment::None, 0, "", varName, false, "");
return declarations[address];
}
Declaration* ZFile::AddDeclarationInclude(uint32_t address, string includePath, uint32_t size, string varType, string varName)
Declaration* ZFile::AddDeclarationInclude(uint32_t address, string includePath, uint32_t size,
string varType, string varName)
{
AddDeclarationDebugChecks(address);
@ -550,7 +603,9 @@ Declaration* ZFile::AddDeclarationInclude(uint32_t address, string includePath,
return declarations[address];
}
Declaration* ZFile::AddDeclarationIncludeArray(uint32_t address, std::string includePath, uint32_t size, std::string varType, std::string varName, int arrayItemCnt)
Declaration* ZFile::AddDeclarationIncludeArray(uint32_t address, std::string includePath,
uint32_t size, std::string varType,
std::string varName, int arrayItemCnt)
{
#if _DEBUG
if (declarations.find(address) != declarations.end())
@ -582,7 +637,9 @@ void ZFile::AddDeclarationDebugChecks(uint32_t address)
std::string ZFile::GetDeclarationName(uint32_t address)
{
return GetDeclarationName(address, "ERROR_COULD_NOT_FIND_DECLARATION"); // Note: For now that default message is just for testing
return GetDeclarationName(address,
"ERROR_COULD_NOT_FIND_DECLARATION"); // Note: For now that default
// message is just for testing
}
std::string ZFile::GetDeclarationName(uint32_t address, std::string defaultResult)
@ -646,7 +703,7 @@ void ZFile::GenerateSourceFiles(string outputDir)
if (res->IsExternalResource())
{
//cout << "EXTERN\n";
// cout << "EXTERN\n";
string path = Path::GetFileNameWithoutExtension(res->GetName()).c_str();
while (StringHelper::EndsWith(outputDir, "/"))
@ -658,16 +715,19 @@ void ZFile::GenerateSourceFiles(string outputDir)
if (res->GetResourceType() != ZResourceType::Texture)
declType = "u8";
AddDeclarationIncludeArray(res->GetRawDataIndex(), StringHelper::Sprintf("%s/%s.%s.inc.c",
outputDir.c_str(), Path::GetFileNameWithoutExtension(res->GetOutName()).c_str(), res->GetExternalExtension().c_str()), res->GetRawDataSize(),
declType, res->GetName(), 0);
//File::WriteAllText("build/" + outputDir + "/" + Path::GetFileNameWithoutExtension(res->GetName()) + ".inc.c", resSrc);
AddDeclarationIncludeArray(
res->GetRawDataIndex(),
StringHelper::Sprintf("%s/%s.%s.inc.c", outputDir.c_str(),
Path::GetFileNameWithoutExtension(res->GetOutName()).c_str(),
res->GetExternalExtension().c_str()),
res->GetRawDataSize(), declType, res->GetName(), 0);
// File::WriteAllText("build/" + outputDir + "/" +
// Path::GetFileNameWithoutExtension(res->GetName()) + ".inc.c", resSrc);
}
else
{
//cout << "NOT EXTERN\n";
// cout << "NOT EXTERN\n";
sourceOutput += resSrc;
}
@ -680,18 +740,18 @@ void ZFile::GenerateSourceFiles(string outputDir)
while (StringHelper::EndsWith(outputDir, "/"))
outputDir = outputDir.substr(0, outputDir.length() - 1);
//string buildPath = "build/" + outputDir + "/" + "basefile.txt";
// string buildPath = "build/" + outputDir + "/" + "basefile.txt";
string outPath = outputDir + "/" + Path::GetFileNameWithoutExtension(name) + ".c";
//printf("WRITING %s\n", buildPath.c_str());
// printf("WRITING %s\n", buildPath.c_str());
//if (!Directory::Exists(Path::GetPath(outPath)))
//Directory::CreateDirectory(Path::GetPath(outPath));
// if (!Directory::Exists(Path::GetPath(outPath)))
// Directory::CreateDirectory(Path::GetPath(outPath));
//if (!Directory::Exists(Path::GetPath(buildPath)))
//Directory::CreateDirectory(Path::GetPath(buildPath));
// if (!Directory::Exists(Path::GetPath(buildPath)))
// Directory::CreateDirectory(Path::GetPath(buildPath));
File::WriteAllText(outPath, sourceOutput);
//File::WriteAllText(buildPath, outPath);
// File::WriteAllText(buildPath, outPath);
// Generate Header
sourceOutput = "";
@ -707,7 +767,8 @@ void ZFile::GenerateSourceFiles(string outputDir)
sourceOutput += ProcessExterns();
File::WriteAllText(outputDir + "/" + Path::GetFileNameWithoutExtension(name) + ".h", sourceOutput);
File::WriteAllText(outputDir + "/" + Path::GetFileNameWithoutExtension(name) + ".h",
sourceOutput);
}
void ZFile::GenerateHLIntermediette()
@ -727,7 +788,8 @@ void ZFile::GenerateHLIntermediette()
std::string ZFile::GetHeaderInclude()
{
return StringHelper::Sprintf("#include \"%s\"\n\n", (Path::GetFileNameWithoutExtension(name) + ".h").c_str());
return StringHelper::Sprintf("#include \"%s\"\n\n",
(Path::GetFileNameWithoutExtension(name) + ".h").c_str());
}
void ZFile::GeneratePlaceholderDeclarations()
@ -747,22 +809,16 @@ string ZFile::ProcessDeclarations()
if (declarations.size() == 0)
return output;
auto declarationKeysSorted = vector<pair<int32_t, Declaration*>>(declarations.begin(), declarations.end());
sort(declarationKeysSorted.begin(), declarationKeysSorted.end(), [](const auto& lhs, const auto& rhs)
{
return lhs.first < rhs.first;
});
// Account for padding/alignment
int lastAddr = 0;
int lastSize = 0;
uint32_t lastAddr = 0;
uint32_t lastSize = 0;
//printf("RANGE START: 0x%06X - RANGE END: 0x%06X\n", rangeStart, rangeEnd);
// 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...
//pair<int32_t, Declaration*> lastItem = declarationKeysSorted[0];
// pair<int32_t, Declaration*> lastItem = declarationKeysSorted[0];
//for (int i = 1; i < declarationKeysSorted.size(); i++)
// for (int i = 1; i < declarationKeysSorted.size(); i++)
//{
// pair<int32_t, Declaration*> curItem = declarationKeysSorted[i];
@ -789,41 +845,41 @@ string ZFile::ProcessDeclarations()
// lastItem = curItem;
//}
for (pair<int32_t, Declaration*> item : declarations)
for (pair<uint32_t, Declaration*> item : declarations)
{
ProcessDeclarationText(item.second);
}
for (pair<int32_t, Declaration*> item : declarationKeysSorted)
for (pair<int32_t, Declaration*> item : declarations)
{
while (declarations[item.first]->size % 4 != 0)
while (item.second->size % 4 != 0)
{
declarations[item.first]->size++;
item.second->size++;
}
if (lastAddr != 0)
{
if (item.second->alignment == DeclarationAlignment::Align16)
{
int lastAddrSizeTest = declarations[lastAddr]->size;
// int lastAddrSizeTest = declarations[lastAddr]->size;
int curPtr = lastAddr + declarations[lastAddr]->size;
while (curPtr % 4 != 0)
{
declarations[lastAddr]->size++;
//declarations[item.first]->size++;
// item.second->size++;
curPtr++;
}
/*while (curPtr % 16 != 0)
{
char buffer[2048];
char buffer[2048];
sprintf(buffer, "static u32 align%02X = 0;\n", curPtr);
declarations[item.first]->text = buffer + declarations[item.first]->text;
sprintf(buffer, "static u32 align%02X = 0;\n", curPtr);
item.second->text = buffer + item.second->text;
declarations[lastAddr]->size += 4;
curPtr += 4;
declarations[lastAddr]->size += 4;
curPtr += 4;
}*/
}
else if (item.second->alignment == DeclarationAlignment::Align8)
@ -833,8 +889,7 @@ string ZFile::ProcessDeclarations()
while (curPtr % 4 != 0)
{
declarations[lastAddr]->size++;
//item.second->size++;
//declarations[item.first]->size++;
// item.second->size++;
curPtr++;
}
@ -843,11 +898,10 @@ string ZFile::ProcessDeclarations()
char buffer[2048];
sprintf(buffer, "static u32 align%02X = 0;\n", curPtr);
declarations[item.first]->preText = buffer + declarations[item.first]->preText;
item.second->preText = buffer + item.second->preText;
declarations[lastAddr]->size += 4;
//item.second->size += 4;
//declarations[item.first]->size += 4;
// item.second->size += 4;
curPtr += 4;
}
}
@ -865,7 +919,7 @@ string ZFile::ProcessDeclarations()
while (curPtr % 16 != 0)
{
declarations[item.first]->postText += StringHelper::Sprintf("static u32 pad%02X = 0;\n", curPtr);
item.second->postText += StringHelper::Sprintf("static u32 pad%02X = 0;\n", curPtr);
item.second->size += 4;
curPtr += 4;
@ -878,27 +932,31 @@ string ZFile::ProcessDeclarations()
// Handle unaccounted data
lastAddr = 0;
lastSize = 0;
for (pair<int32_t, Declaration*> item : declarationKeysSorted)
for (pair<int32_t, Declaration*> item : declarations)
{
if (item.first >= rangeStart && item.first < rangeEnd)
{
if (lastAddr != 0 && declarations.find(lastAddr) != declarations.end() && lastAddr + declarations[lastAddr]->size > item.first)
if (lastAddr != 0 && declarations.find(lastAddr) != declarations.end() &&
lastAddr + declarations[lastAddr]->size > item.first)
{
printf("WARNING: Intersection detected from 0x%06X:0x%06X, conflicts with 0x%06X\n", lastAddr, lastAddr + declarations[lastAddr]->size, item.first);
printf("WARNING: Intersection detected from 0x%06X:0x%06X, conflicts with 0x%06X\n",
lastAddr, lastAddr + declarations[lastAddr]->size, item.first);
}
uint8_t* rawDataArr = rawData.data();
if (lastAddr + lastSize != item.first && lastAddr >= rangeStart && lastAddr + lastSize < rangeEnd)
if (lastAddr + lastSize != item.first && lastAddr >= rangeStart &&
lastAddr + lastSize < rangeEnd)
{
//int diff = item.first - (lastAddr + declarations[lastAddr]->size);
// int diff = item.first - (lastAddr + declarations[lastAddr]->size);
int diff = item.first - (lastAddr + lastSize);
string src = " ";
for (int i = 0; i < diff; i++)
{
//src += StringHelper::Sprintf("0x%02X, ", rawDataArr[lastAddr + declarations[lastAddr]->size + i]);
// src += StringHelper::Sprintf("0x%02X, ", rawDataArr[lastAddr +
// declarations[lastAddr]->size + i]);
src += StringHelper::Sprintf("0x%02X, ", rawDataArr[lastAddr + lastSize + i]);
if ((i % 16 == 15) && (i != (diff - 1)))
@ -909,8 +967,14 @@ string ZFile::ProcessDeclarations()
{
if (diff > 0)
{
//AddDeclarationArray(lastAddr + declarations[lastAddr]->size, DeclarationAlignment::None, diff, "static u8", StringHelper::Sprintf("unaccounted_%06X", lastAddr + declarations[lastAddr]->size), diff, src);
AddDeclarationArray(lastAddr + lastSize, DeclarationAlignment::None, diff, "static u8", StringHelper::Sprintf("unaccounted_%06X", lastAddr + lastSize), diff, src);
// AddDeclarationArray(lastAddr + declarations[lastAddr]->size,
// DeclarationAlignment::None, diff, "static u8",
// StringHelper::Sprintf("unaccounted_%06X", lastAddr +
// declarations[lastAddr]->size), diff, src);
AddDeclarationArray(
lastAddr + lastSize, DeclarationAlignment::None, diff, "static u8",
StringHelper::Sprintf("unaccounted_%06X", lastAddr + lastSize), diff,
src);
}
}
}
@ -921,7 +985,9 @@ string ZFile::ProcessDeclarations()
}
// TODO: THIS CONTAINS REDUNDANCIES. CLEAN THIS UP!
if (lastAddr + declarations[lastAddr]->size < rawData.size() && lastAddr + declarations[lastAddr]->size >= rangeStart && lastAddr + declarations[lastAddr]->size < rangeEnd)
if (lastAddr + declarations[lastAddr]->size < rawData.size() &&
lastAddr + declarations[lastAddr]->size >= rangeStart &&
lastAddr + declarations[lastAddr]->size < rangeEnd)
{
int diff = (int)(rawData.size() - (lastAddr + declarations[lastAddr]->size));
@ -929,7 +995,8 @@ string ZFile::ProcessDeclarations()
for (int i = 0; i < diff; i++)
{
src += StringHelper::Sprintf("0x%02X, ", rawData[lastAddr + declarations[lastAddr]->size + i]);
src += StringHelper::Sprintf("0x%02X, ",
rawData[lastAddr + declarations[lastAddr]->size + i]);
if (i % 16 == 15)
src += "\n ";
@ -939,34 +1006,37 @@ string ZFile::ProcessDeclarations()
{
if (diff > 0)
{
AddDeclarationArray(lastAddr + declarations[lastAddr]->size, DeclarationAlignment::None, diff, "static u8", StringHelper::Sprintf("unaccounted_%06X", lastAddr + declarations[lastAddr]->size),
diff, src);
AddDeclarationArray(lastAddr + declarations[lastAddr]->size,
DeclarationAlignment::None, diff, "static u8",
StringHelper::Sprintf("unaccounted_%06X",
lastAddr + declarations[lastAddr]->size),
diff, src);
}
}
}
// Go through include declarations
declarationKeysSorted = vector<pair<int32_t, Declaration*>>(declarations.begin(), declarations.end());
sort(declarationKeysSorted.begin(), declarationKeysSorted.end(), [](const auto& lhs, const auto& rhs)
{
return lhs.first < rhs.first;
});
// First, handle the prototypes (static only for now)
int protoCnt = 0;
for (pair<int32_t, Declaration*> item : declarationKeysSorted)
for (pair<int32_t, Declaration*> item : declarations)
{
if (item.second->includePath == "" && StringHelper::StartsWith(item.second->varType, "static ") && !StringHelper::StartsWith(item.second->varName, "unaccounted_"))
if (item.second->includePath == "" &&
StringHelper::StartsWith(item.second->varType, "static ") &&
!StringHelper::StartsWith(item.second->varName, "unaccounted_"))
{
if (item.second->isArray)
{
if (item.second->arrayItemCnt == 0)
output += StringHelper::Sprintf("%s %s[];\n", item.second->varType.c_str(), item.second->varName.c_str());
output += StringHelper::Sprintf("%s %s[];\n", item.second->varType.c_str(),
item.second->varName.c_str());
else
output += StringHelper::Sprintf("%s %s[%i];\n", item.second->varType.c_str(), item.second->varName.c_str(), item.second->arrayItemCnt);
output += StringHelper::Sprintf("%s %s[%i];\n", item.second->varType.c_str(),
item.second->varName.c_str(),
item.second->arrayItemCnt);
}
else
output += StringHelper::Sprintf("%s %s;\n", item.second->varType.c_str(), item.second->varName.c_str());
output += StringHelper::Sprintf("%s %s;\n", item.second->varType.c_str(),
item.second->varName.c_str());
protoCnt++;
}
@ -976,16 +1046,20 @@ string ZFile::ProcessDeclarations()
output += "\n";
// Next, output the actual declarations
for (pair<int32_t, Declaration*> item : declarationKeysSorted)
for (pair<int32_t, Declaration*> item : declarations)
{
if (item.first < rangeStart || item.first >= rangeEnd) {
if (item.first < rangeStart || item.first >= rangeEnd)
{
continue;
}
if (item.second->includePath != "")
{
//output += StringHelper::Sprintf("#include \"%s\"\n", item.second->includePath.c_str());
output += StringHelper::Sprintf("%s %s[] = {\n#include \"%s\"\n};\n\n", item.second->varType.c_str(), item.second->varName.c_str(), item.second->includePath.c_str());
// output += StringHelper::Sprintf("#include \"%s\"\n",
// item.second->includePath.c_str());
output += StringHelper::Sprintf(
"%s %s[] = {\n#include \"%s\"\n};\n\n", item.second->varType.c_str(),
item.second->varName.c_str(), item.second->includePath.c_str());
}
else if (item.second->varType != "")
{
@ -995,15 +1069,19 @@ string ZFile::ProcessDeclarations()
if (item.second->isArray)
{
if (item.second->arrayItemCnt == 0)
output += StringHelper::Sprintf("%s %s[] = {\n", item.second->varType.c_str(), item.second->varName.c_str());
output += StringHelper::Sprintf("%s %s[] = {\n", item.second->varType.c_str(),
item.second->varName.c_str());
else
output += StringHelper::Sprintf("%s %s[%i] = {\n", item.second->varType.c_str(), item.second->varName.c_str(), item.second->arrayItemCnt);
output += StringHelper::Sprintf("%s %s[%i] = {\n", item.second->varType.c_str(),
item.second->varName.c_str(),
item.second->arrayItemCnt);
output += item.second->text + "\n";
}
else
{
output += StringHelper::Sprintf("%s %s = { ", item.second->varType.c_str(), item.second->varName.c_str());
output += StringHelper::Sprintf("%s %s = { ", item.second->varType.c_str(),
item.second->varName.c_str());
output += item.second->text;
}
@ -1013,7 +1091,7 @@ string ZFile::ProcessDeclarations()
output += " };";
output += " " + item.second->rightText + "\n\n";
if (item.second->postText != "")
output += item.second->postText + "\n";
}
@ -1026,11 +1104,11 @@ string ZFile::ProcessDeclarations()
void ZFile::ProcessDeclarationText(Declaration* decl)
{
int refIndex = 0;
size_t refIndex = 0;
if (decl->references.size() > 0)
{
for (int i = 0; i < decl->text.size() - 1; i++)
for (size_t i = 0; i < decl->text.size() - 1; i++)
{
char c = decl->text[i];
char c2 = decl->text[i + 1];
@ -1052,11 +1130,14 @@ void ZFile::ProcessDeclarationText(Declaration* decl)
int itemSize = refDecl->size / refDecl->arrayItemCnt;
int itemIndex = (decl->references[refIndex] - refDeclAddr) / itemSize;
decl->text.replace(i, 2, StringHelper::Sprintf("&%s[%i]", refDecl->varName.c_str(), itemIndex));
decl->text.replace(i, 2,
StringHelper::Sprintf(
"&%s[%i]", refDecl->varName.c_str(), itemIndex));
}
else
{
decl->text.replace(i, 2, StringHelper::Sprintf("ERROR ARRAYITEMCNT = 0"));
decl->text.replace(i, 2,
StringHelper::Sprintf("ERROR ARRAYITEMCNT = 0"));
}
}
else
@ -1077,29 +1158,30 @@ string ZFile::ProcessExterns()
{
string output = "";
auto declarationKeysSorted = vector<pair<int32_t, Declaration*>>(declarations.begin(), declarations.end());
sort(declarationKeysSorted.begin(), declarationKeysSorted.end(), [](const auto& lhs, const auto& rhs)
for (pair<int32_t, Declaration*> item : declarations)
{
return lhs.first < rhs.first;
});
for (pair<int32_t, Declaration*> item : declarationKeysSorted)
{
if (item.first < rangeStart || item.first >= rangeEnd) {
if (item.first < rangeStart || item.first >= rangeEnd)
{
continue;
}
if (!StringHelper::StartsWith(item.second->varType, "static ") && item.second->varType != "")// && item.second->includePath == "")
if (!StringHelper::StartsWith(item.second->varType, "static ") &&
item.second->varType != "") // && item.second->includePath == "")
{
if (item.second->isArray)
{
if (item.second->arrayItemCnt == 0)
output += StringHelper::Sprintf("extern %s %s[];\n", item.second->varType.c_str(), item.second->varName.c_str());
output +=
StringHelper::Sprintf("extern %s %s[];\n", item.second->varType.c_str(),
item.second->varName.c_str());
else
output += StringHelper::Sprintf("extern %s %s[%i];\n", item.second->varType.c_str(), item.second->varName.c_str(), item.second->arrayItemCnt);
output += StringHelper::Sprintf(
"extern %s %s[%i];\n", item.second->varType.c_str(),
item.second->varName.c_str(), item.second->arrayItemCnt);
}
else
output += StringHelper::Sprintf("extern %s %s;\n", item.second->varType.c_str(), item.second->varName.c_str());
output += StringHelper::Sprintf("extern %s %s;\n", item.second->varType.c_str(),
item.second->varName.c_str());
}
}