summaryrefslogtreecommitdiff
path: root/ZAPD/ZSymbol.cpp
blob: 603d4d889cb6b9598d3a11019b3b02cb2cbe8a1a (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
#include "ZSymbol.h"

#include <ship/utils/StringHelper.h>
#include "WarningHandler.h"
#include "ZFile.h"

REGISTER_ZFILENODE(Symbol, ZSymbol);

ZSymbol::ZSymbol(ZFile* nParent) : ZResource(nParent)
{
	RegisterOptionalAttribute("Type");
	RegisterOptionalAttribute("TypeSize");
	RegisterOptionalAttribute("Count");
}

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

	std::string typeXml = registeredAttributes.at("Type").value;

	if (typeXml == "")
	{
		HANDLE_WARNING_RESOURCE(WarningType::MissingAttribute, parent, this, rawDataIndex,
		                        "missing 'Type' attribute in <Symbol>", "Defaulting to 'void*'.");
		type = "void*";
	}
	else
	{
		type = typeXml;
	}

	std::string typeSizeXml = registeredAttributes.at("TypeSize").value;
	if (typeSizeXml == "")
	{
		HANDLE_WARNING_RESOURCE(WarningType::MissingAttribute, parent, this, rawDataIndex,
		                        "missing 'TypeSize' attribute in <Symbol>", "Defaulting to '4'.");
		typeSize = 4;  // Size of a word.
	}
	else
	{
		typeSize = StringHelper::StrToL(typeSizeXml, 0);
	}

	if (registeredAttributes.at("Count").wasSet)
	{
		isArray = true;

		std::string countXml = registeredAttributes.at("Count").value;
		if (countXml != "")
			count = StringHelper::StrToL(countXml, 0);
	}

	if (registeredAttributes.at("Static").value == "On")
	{
		HANDLE_WARNING_RESOURCE(WarningType::InvalidAttributeValue, parent, this, rawDataIndex,
		                        "a <Symbol> cannot be marked as static",
		                        "Disabling static for this resource.");
	}
	staticConf = StaticConfig::Off;
}

Declaration* ZSymbol::DeclareVar([[maybe_unused]] const std::string& prefix,
                                 [[maybe_unused]] const std::string& bodyStr)
{
	return nullptr;
}

size_t ZSymbol::GetRawDataSize() const
{
	if (isArray)
		return count * typeSize;

	return typeSize;
}

std::string ZSymbol::GetSourceOutputHeader([[maybe_unused]] const std::string& prefix, std::set<std::string> *nameSet)
{
	if (isArray)
	{
		if (count == 0)
			return StringHelper::Sprintf("extern %s %s[];\n", type.c_str(), name.c_str());
		else
			return StringHelper::Sprintf("extern %s %s[%i];\n", type.c_str(), name.c_str(), count);
	}

	return StringHelper::Sprintf("extern %s %s;\n", type.c_str(), name.c_str());
}

std::string ZSymbol::GetSourceTypeName() const
{
	return type;
}

ZResourceType ZSymbol::GetResourceType() const
{
	return ZResourceType::Symbol;
}