summaryrefslogtreecommitdiff
path: root/ZAPD/ZVector.cpp
blob: 160d2aa80d3d98366cefaad2720617ac2b0c9c90 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "ZVector.h"
#include <assert.h>
#include "BitConverter.h"
#include "File.h"
#include "Globals.h"
#include "StringHelper.h"
#include "ZFile.h"

REGISTER_ZFILENODE(Vector, ZVector);

ZVector::ZVector(ZFile* nParent) : ZResource(nParent)
{
	scalars = std::vector<ZScalar*>();
	this->scalarType = ZSCALAR_NONE;
	this->dimensions = 0;
}

ZVector::~ZVector()
{
	ClearScalars();
}

void ZVector::ClearScalars()
{
	for (auto s : scalars)
		delete s;

	scalars.clear();
}

void ZVector::ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData,
                             const uint32_t nRawDataIndex, const std::string& nRelPath)
{
	ZResource::ExtractFromXML(reader, nRawData, nRawDataIndex, nRelPath);
}

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

	std::string type = reader->Attribute("Type");
	this->scalarType = ZScalar::MapOutputTypeToScalarType(type);

	std::string dimensions = reader->Attribute("Dimensions");
	this->dimensions = strtol(dimensions.c_str(), NULL, 16);
}

void ZVector::ParseRawData()
{
	int32_t currentRawDataIndex = this->rawDataIndex;

	ClearScalars();

	for (uint32_t i = 0; i < this->dimensions; i++)
	{
		ZScalar* scalar = new ZScalar(this->scalarType, this->parent);
		scalar->rawDataIndex = currentRawDataIndex;
		scalar->rawData = this->rawData;
		scalar->ParseRawData();
		currentRawDataIndex += scalar->GetRawDataSize();

		this->scalars.push_back(scalar);
	}

	// Ensure the scalars vector has the same number of elements as the vector dimension.
	assert(this->scalars.size() == this->dimensions);
}

size_t ZVector::GetRawDataSize()
{
	size_t size = 0;

	for (size_t i = 0; i < this->scalars.size(); i++)
		size += this->scalars[i]->GetRawDataSize();

	return size;
}

bool ZVector::DoesSupportArray()
{
	return true;
}

std::string ZVector::GetSourceTypeName()
{
	if (dimensions == 3 && scalarType == ZSCALAR_F32)
		return "Vec3f";
	else if (dimensions == 3 && scalarType == ZSCALAR_S16)
		return "Vec3s";
	else if (dimensions == 3 && scalarType == ZSCALAR_S32)
		return "Vec3i";
	else
	{
		std::string output = StringHelper::Sprintf(
			"Encountered unsupported vector type: %d dimensions, %s type", dimensions,
			ZScalar::MapScalarTypeToOutputType(scalarType).c_str());

		if (Globals::Instance->verbosity >= VERBOSITY_DEBUG)
			printf("%s\n", output.c_str());

		throw std::runtime_error(output);
	}
}

std::string ZVector::GetSourceValue()
{
	std::vector<std::string> strings = std::vector<std::string>();

	for (size_t i = 0; i < this->scalars.size(); i++)
		strings.push_back(scalars[i]->GetSourceValue());

	return "{ " + StringHelper::Implode(strings, ", ") + " }";
}

std::string ZVector::GetSourceOutputCode(const std::string& prefix)
{
	if (parent != nullptr)
		parent->AddDeclaration(rawDataIndex, DeclarationAlignment::None, GetRawDataSize(),
		                       GetSourceTypeName(), GetName(), GetSourceValue());

	return "";
}

ZResourceType ZVector::GetResourceType()
{
	return ZResourceType::Vector;
}