summaryrefslogtreecommitdiff
path: root/ZAPD/ZArray.cpp
blob: f00d85d41dd4e62b2989467172010d2a3d02bce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "ZArray.h"
#include "Globals.h"
#include "StringHelper.h"
#include "ZFile.h"

REGISTER_ZFILENODE(Array, ZArray);

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

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 = "";

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

	ZResource* res = testFile->resources[0];
	int resSize = res->GetRawDataSize();

	if (!res->DoesSupportArray())
	{
		throw std::runtime_error(StringHelper::Sprintf(
			"Error! Resource %s does not support being wrapped in an array!\n",
			res->GetName().c_str()));
	}

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

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

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

	return "";
}

int ZArray::GetRawDataSize()
{
	return arrayCnt * testFile->resources[0]->GetRawDataSize();
}

void ZArray::ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData,
                            const int nRawDataIndex, const std::string& nRelPath)
{
	rawData = nRawData;
	rawDataIndex = nRawDataIndex;
	ParseXML(reader);
	// arr->ParseRawData();
}