Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check XML resource attributes #126

Merged
merged 29 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9654396
register attibutes
AngheloAlf Apr 28, 2021
c3f8496
fix array problems
AngheloAlf Apr 28, 2021
71d210c
cutscene problems
AngheloAlf Apr 28, 2021
0336311
Set inner
AngheloAlf Apr 28, 2021
a52d76e
Merge remote-tracking branch 'upstream/master' into attributes_check
AngheloAlf Apr 28, 2021
55036ef
fix merge
AngheloAlf Apr 28, 2021
28cc68f
ups
AngheloAlf Apr 28, 2021
b13d525
Run format
AngheloAlf Apr 28, 2021
3c0f4d2
small cleaning
AngheloAlf Apr 28, 2021
01906d5
A small blob cleanup
AngheloAlf Apr 29, 2021
37d16fc
Simplify the logic a bit
AngheloAlf May 5, 2021
daecd87
Merge remote-tracking branch 'upstream/master' into attributes_check
AngheloAlf May 5, 2021
84be3fb
ups
AngheloAlf May 5, 2021
30ec67f
Merge remote-tracking branch 'upstream/master' into attributes_check
AngheloAlf May 6, 2021
ca758b4
Fix merge problem
AngheloAlf May 6, 2021
fb6389b
Merge remote-tracking branch 'upstream/master' into attributes_check
AngheloAlf May 7, 2021
ef47cb1
Fix merge issues
AngheloAlf May 8, 2021
09c75d5
Merge remote-tracking branch 'upstream/master' into attributes_check
AngheloAlf May 10, 2021
895ed2c
Fix merge issues
AngheloAlf May 10, 2021
d513b11
fix merge-produced type
AngheloAlf May 10, 2021
88f3b4b
Merge remote-tracking branch 'upstream/master' into attributes_check
AngheloAlf May 10, 2021
fac7d51
Fix merge issues
AngheloAlf May 10, 2021
bb905d2
run format and update easteregg
AngheloAlf May 10, 2021
364f07e
register ZPath attributes
AngheloAlf May 10, 2021
51b7783
dumb problems
AngheloAlf May 10, 2021
ea525c2
How dumb can I be?
AngheloAlf May 10, 2021
2331b40
future proof change
AngheloAlf May 11, 2021
aa0ef8c
empty commit
AngheloAlf May 15, 2021
3a905a5
Check if Width and Height attributes of ZTexture has only decimal digits
AngheloAlf May 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ZAPD/HighLevel/HLAnimationIntermediette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ HLAnimationIntermediette* HLAnimationIntermediette::FromZAnimation(ZAnimation* z

ZAnimation* HLAnimationIntermediette::ToZAnimation()
{
ZAnimation* zAnim = new ZAnimation(nullptr);
ZAnimation* zAnim = new ZNormalAnimation(nullptr);

return zAnim;
}
Expand Down
17 changes: 12 additions & 5 deletions ZAPD/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

#if !defined(_MSC_VER) && !defined(__CYGWIN__)
#include <csignal>
#include <cstdlib>
#include <cxxabi.h> // for __cxa_demangle
#include <dlfcn.h> // for dladdr
#include <execinfo.h>
#include <time.h>
#include <unistd.h>
#endif

Expand All @@ -35,6 +37,7 @@ void BuildAssetModelIntermediette(const fs::path& outPath);
void BuildAssetAnimationIntermediette(const fs::path& animPath, const fs::path& outPath);

#if !defined(_MSC_VER) && !defined(__CYGWIN__)
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
void ErrorHandler(int sig)
{
void* array[4096];
Expand All @@ -44,11 +47,15 @@ void ErrorHandler(int sig)

fprintf(stderr, "\nZAPD crashed. (Signal: %i)\n", sig);

fprintf(stderr, "\n\t\tYou've met with a terrible fate, haven't you?\n\n");
/**
* Other possible options:
* - SEA BEARS FOAM. SLEEP BEARS DREAMS. \n BOTH END IN THE SAME WAY: CRASSSH!
*/
const char* crashEasterEgg[] = {
"\tYou've met with a terrible fate, haven't you?",
"\tSEA BEARS FOAM. SLEEP BEARS DREAMS. \n\tBOTH END IN THE SAME WAY: CRASSSH!",
};

srand(time(nullptr));
auto easterIndex = rand() % ARRAY_COUNT(crashEasterEgg);

fprintf(stderr, "\n%s\n\n", crashEasterEgg[easterIndex]);

fprintf(stderr, "Traceback:\n");
for (size_t i = 1; i < size; i++)
Expand Down
41 changes: 9 additions & 32 deletions ZAPD/ZAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ void ZAnimation::Save(const fs::path& outFolder)
}
}

void ZAnimation::ParseXML(tinyxml2::XMLElement* reader)
{
ZResource::ParseXML(reader);
}

std::string ZAnimation::GetSourceOutputCode(const std::string& prefix)
{
return "";
Expand Down Expand Up @@ -120,16 +115,6 @@ std::string ZNormalAnimation::GetSourceTypeName() const
return "AnimationHeader";
}

void ZNormalAnimation::ExtractFromXML(tinyxml2::XMLElement* reader,
const std::vector<uint8_t>& nRawData,
const uint32_t nRawDataIndex)
{
rawData = std::move(nRawData);
rawDataIndex = nRawDataIndex;
ParseXML(reader);
ParseRawData();
}

void ZNormalAnimation::ParseRawData()
{
ZAnimation::ParseRawData();
Expand Down Expand Up @@ -196,16 +181,6 @@ std::string ZLinkAnimation::GetSourceTypeName() const
return "LinkAnimationHeader";
}

void ZLinkAnimation::ExtractFromXML(tinyxml2::XMLElement* reader,
const std::vector<uint8_t>& nRawData,
const uint32_t nRawDataIndex)
{
rawData = std::move(nRawData);
rawDataIndex = nRawDataIndex;
ParseXML(reader);
ParseRawData();
}

void ZLinkAnimation::ParseRawData()
{
ZAnimation::ParseRawData();
Expand Down Expand Up @@ -251,6 +226,7 @@ std::string TransformData::GetSourceTypeName()

ZCurveAnimation::ZCurveAnimation(ZFile* nParent) : ZAnimation(nParent)
{
RegisterOptionalAttribute("SkelOffset");
}

ZCurveAnimation::~ZCurveAnimation()
Expand All @@ -262,15 +238,16 @@ void ZCurveAnimation::ParseXML(tinyxml2::XMLElement* reader)
{
ZAnimation::ParseXML(reader);

const char* skelOffsetXml = reader->Attribute("SkelOffset");
if (skelOffsetXml == nullptr)
std::string skelOffsetXml = registeredAttributes.at("SkelOffset").value;
if (skelOffsetXml == "")
{
throw std::runtime_error(StringHelper::Sprintf(
"ZCurveAnimation::ParseXML: Fatal error in '%s'. Missing 'SkelOffset' attribute in "
"ZCurveAnimation. You need to provide the offset of the curve skeleton.",
name.c_str()));
throw std::runtime_error(
StringHelper::Sprintf("ZCurveAnimation::ParseXML: Fatal error in '%s'.\n"
"\t Missing 'SkelOffset' attribute in ZCurveAnimation.\n"
"\t You need to provide the offset of the curve skeleton.",
name.c_str()));
}
skelOffset = std::strtoul(skelOffsetXml, nullptr, 0);
skelOffset = StringHelper::StrToL(skelOffsetXml, 0);
}

void ZCurveAnimation::ParseRawData()
Expand Down
7 changes: 0 additions & 7 deletions ZAPD/ZAnimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ZAnimation : public ZResource
protected:
void ParseRawData() override;
void Save(const fs::path& outFolder) override;
void ParseXML(tinyxml2::XMLElement* reader) override;
};

class ZNormalAnimation : public ZAnimation
Expand All @@ -52,9 +51,6 @@ class ZNormalAnimation : public ZAnimation
size_t GetRawDataSize() const override;
std::string GetSourceTypeName() const override;

void ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData,
const uint32_t nRawDataIndex) override;

protected:
virtual void ParseRawData() override;
};
Expand All @@ -70,9 +66,6 @@ class ZLinkAnimation : public ZAnimation
size_t GetRawDataSize() const override;
std::string GetSourceTypeName() const override;

void ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData,
const uint32_t nRawDataIndex) override;

protected:
virtual void ParseRawData() override;
};
Expand Down
70 changes: 39 additions & 31 deletions ZAPD/ZArray.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ZArray.h"
#include <cassert>
#include "Globals.h"
#include "StringHelper.h"
#include "ZFile.h"
Expand All @@ -8,71 +9,78 @@ REGISTER_ZFILENODE(Array, ZArray);
ZArray::ZArray(ZFile* nParent) : ZResource(nParent)
{
canHaveInner = true;
RegisterRequiredAttribute("Count");
}

ZArray::~ZArray()
{
delete testFile;
for (auto res : resList)
delete res;
}

void ZArray::ParseXML(tinyxml2::XMLElement* reader)
{
ZResource::ParseXML(reader);

arrayCnt = reader->IntAttribute("Count", 0);
testFile = new ZFile(ZFileMode::Extract, reader, Globals::Instance->baseRomPath, "",
parent->GetName(), "ZArray subfile", true);
}

// TODO: This is a bit hacky, but until we refactor how ZFile parses the XML, it'll have to do.
std::string ZArray::GetSourceOutputCode(const std::string& prefix)
{
std::string output = "";
arrayCnt = StringHelper::StrToL(registeredAttributes.at("Count").value, 0);
// TODO: do a better check.
assert(arrayCnt > 0);

if (testFile->resources.size() <= 0)
tinyxml2::XMLElement* child = reader->FirstChildElement();
if (child == nullptr)
throw std::runtime_error(
StringHelper::Sprintf("Error! Array needs at least one sub-element.\n"));

ZResource* res = testFile->resources[0];
size_t resSize = res->GetRawDataSize();
childName = child->Name();

if (!res->DoesSupportArray())
auto nodeMap = ZFile::GetNodeMap();
size_t childIndex = rawDataIndex;
for (size_t i = 0; i < arrayCnt; i++)
{
throw std::runtime_error(StringHelper::Sprintf(
"Error! Resource %s does not support being wrapped in an array!\n",
res->GetName().c_str()));
ZResource* res = nodeMap->at(childName)(parent);
if (!res->DoesSupportArray())
{
throw std::runtime_error(StringHelper::Sprintf(
"Error! Resource %s does not support being wrapped in an array!\n",
childName.c_str()));
}
res->parent = parent;
res->SetInnerNode(true);
res->ExtractFromXML(child, rawData, childIndex);

childIndex += res->GetRawDataSize();
resList.push_back(res);
}
}

std::string ZArray::GetSourceOutputCode(const std::string& prefix)
{
std::string output = "";

for (size_t i = 0; i < arrayCnt; i++)
{
size_t childIndex = rawDataIndex + (i * resSize);
res->SetRawDataIndex(childIndex);
res->ParseRawData();
std::string test = res->GetSourceOutputCode("");
// output += " { " + testFile->declarations[childIndex]->text + " }";
output += testFile->declarations[childIndex]->text;
output += resList.at(i)->GetBodySourceCode();

if (i < arrayCnt - 1)
output += ",\n";
}

if (parent != nullptr)
parent->AddDeclarationArray(rawDataIndex, DeclarationAlignment::None, GetRawDataSize(),
res->GetSourceTypeName(), name, arrayCnt, output);
resList.at(0)->GetSourceTypeName(), name, arrayCnt, output);

return "";
}

size_t ZArray::GetRawDataSize() const
{
return arrayCnt * testFile->resources[0]->GetRawDataSize();
size_t size = 0;
for (auto res : resList)
size += res->GetRawDataSize();
return size;
}

void ZArray::ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData,
const uint32_t nRawDataIndex)
ZResourceType ZArray::GetResourceType() const
{
rawData = nRawData;
rawDataIndex = nRawDataIndex;
ParseXML(reader);
// arr->ParseRawData();
return ZResourceType::Array;
}
9 changes: 5 additions & 4 deletions ZAPD/ZArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class ZArray : public ZResource
~ZArray();

void ParseXML(tinyxml2::XMLElement* reader) override;

std::string GetSourceOutputCode(const std::string& prefix) override;
size_t GetRawDataSize() const override;

void ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData,
const uint32_t nRawDataIndex) override;
ZResourceType GetResourceType() const override;

protected:
size_t arrayCnt;
ZFile* testFile;
};
std::string childName;
std::vector<ZResource*> resList;
};
49 changes: 23 additions & 26 deletions ZAPD/ZBlob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,7 @@ REGISTER_ZFILENODE(Blob, ZBlob);

ZBlob::ZBlob(ZFile* nParent) : ZResource(nParent)
{
}

ZBlob::ZBlob(const std::vector<uint8_t>& nRawData, uint32_t nRawDataIndex, size_t size,
std::string nName, ZFile* nParent)
: ZBlob(nParent)
{
rawDataIndex = nRawDataIndex;
rawData =
std::vector<uint8_t>(nRawData.data() + rawDataIndex, nRawData.data() + rawDataIndex + size);
name = std::move(nName);
}

void ZBlob::ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData,
const uint32_t nRawDataIndex)
{
rawDataIndex = nRawDataIndex;

ParseXML(reader);
long size = strtol(reader->Attribute("Size"), NULL, 16);
rawData =
std::vector<uint8_t>(nRawData.data() + rawDataIndex, nRawData.data() + rawDataIndex + size);
RegisterRequiredAttribute("Size");
}

// Build Source File Mode
Expand All @@ -42,7 +22,7 @@ ZBlob* ZBlob::BuildFromXML(XMLElement* reader, const std::string& inFolder, bool
blob->ParseXML(reader);

if (readFile)
blob->rawData = File::ReadAllBytes(inFolder + "/" + blob->name + ".bin");
blob->blobData = File::ReadAllBytes(inFolder + "/" + blob->name + ".bin");

return blob;
}
Expand All @@ -51,21 +31,33 @@ ZBlob* ZBlob::FromFile(const std::string& filePath)
{
ZBlob* blob = new ZBlob(nullptr);
blob->name = StringHelper::Split(Path::GetFileNameWithoutExtension(filePath), ".")[0];
blob->rawData = File::ReadAllBytes(filePath);
blob->blobData = File::ReadAllBytes(filePath);

return blob;
}

void ZBlob::ParseXML(tinyxml2::XMLElement* reader)
{
ZResource::ParseXML(reader);

blobSize = StringHelper::StrToL(registeredAttributes.at("Size").value, 16);
}

void ZBlob::ParseRawData()
{
blobData.assign(rawData.data() + rawDataIndex, rawData.data() + rawDataIndex + blobSize);
}

std::string ZBlob::GetSourceOutputCode(const std::string& prefix)
{
sourceOutput = "";

for (size_t i = 0; i < rawData.size(); i += 1)
for (size_t i = 0; i < blobData.size(); i += 1)
{
if (i % 16 == 0)
sourceOutput += " ";

sourceOutput += StringHelper::Sprintf("0x%02X, ", rawData[i]);
sourceOutput += StringHelper::Sprintf("0x%02X, ", blobData[i]);

if (i % 16 == 15)
sourceOutput += "\n";
Expand All @@ -86,7 +78,7 @@ std::string ZBlob::GetSourceOutputHeader(const std::string& prefix)

void ZBlob::Save(const fs::path& outFolder)
{
File::WriteAllBytes(outFolder / (name + ".bin"), rawData);
File::WriteAllBytes(outFolder / (name + ".bin"), blobData);
}

bool ZBlob::IsExternalResource() const
Expand All @@ -108,3 +100,8 @@ ZResourceType ZBlob::GetResourceType() const
{
return ZResourceType::Blob;
}

size_t ZBlob::GetRawDataSize() const
{
return blobSize;
}
Loading