summaryrefslogtreecommitdiff
path: root/ZAPD
diff options
context:
space:
mode:
authorAnghelo Carvajal <angheloalf95@gmail.com>2021-03-17 19:19:16 -0300
committerGitHub <noreply@github.com>2021-03-17 18:19:16 -0400
commitd698b97d02380b3bf2c898de44941d03e37daff4 (patch)
tree86a6928ad6d012cbf16c4839bd47312a7c1d207b /ZAPD
parentb25eb9a568b4dd0dbc5c2fea3d9e85ca69e21aa2 (diff)
Add new resource ZSymbol (#92)
* ZSymbol Signed-off-by: Angie <angheloalf95@gmail.com> * Add array support to ZSymbol Signed-off-by: Angie <angheloalf95@gmail.com> * Export symbol as array Signed-off-by: Angie <angheloalf95@gmail.com> * Final cleanup Signed-off-by: Angie <angheloalf95@gmail.com> * Ups, forgot the offset in a constructor Signed-off-by: Angie <angheloalf95@gmail.com>
Diffstat (limited to 'ZAPD')
-rw-r--r--ZAPD/ZArray.cpp2
-rw-r--r--ZAPD/ZFile.cpp15
-rw-r--r--ZAPD/ZResource.h3
-rw-r--r--ZAPD/ZSymbol.cpp87
-rw-r--r--ZAPD/ZSymbol.h29
5 files changed, 126 insertions, 10 deletions
diff --git a/ZAPD/ZArray.cpp b/ZAPD/ZArray.cpp
index 622aef5..4be7656 100644
--- a/ZAPD/ZArray.cpp
+++ b/ZAPD/ZArray.cpp
@@ -31,7 +31,7 @@ std::string ZArray::GetSourceOutputCode(const std::string& prefix)
if (!res->DoesSupportArray())
{
- throw StringHelper::Sprintf("Error! Resource %s does not support being wrapped in an array!\n", res->GetName().c_str());
+ 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++)
diff --git a/ZAPD/ZFile.cpp b/ZAPD/ZFile.cpp
index 5046ed1..77b99ea 100644
--- a/ZAPD/ZFile.cpp
+++ b/ZAPD/ZFile.cpp
@@ -12,6 +12,7 @@
#include "ZVtx.h"
#include "ZCutscene.h"
#include "ZArray.h"
+#include "ZSymbol.h"
#include "Path.h"
#include "File.h"
#include "Directory.h"
@@ -249,17 +250,15 @@ void ZFile::ParseXML(ZFileMode mode, XMLElement* reader, std::string filename, b
}
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;
+ if (mode == ZFileMode::Extract) {
+ symbol = ZSymbol::ExtractFromXML(child, rawData, rawDataIndex, this);
}
- resources.push_back(res);
+ if (symbol != nullptr) {
+ resources.push_back(symbol);
+ }
}
else if (string(child->Name()) == "Collision")
{
diff --git a/ZAPD/ZResource.h b/ZAPD/ZResource.h
index b34ccdc..c47d3d5 100644
--- a/ZAPD/ZResource.h
+++ b/ZAPD/ZResource.h
@@ -39,7 +39,8 @@ enum class ZResourceType
Scalar,
Vector,
Vertex,
- CollisionHeader
+ CollisionHeader,
+ Symbol,
};
class ZResource
diff --git a/ZAPD/ZSymbol.cpp b/ZAPD/ZSymbol.cpp
new file mode 100644
index 0000000..7fd6803
--- /dev/null
+++ b/ZAPD/ZSymbol.cpp
@@ -0,0 +1,87 @@
+#include "ZSymbol.h"
+#include "StringHelper.h"
+
+
+ZSymbol::ZSymbol(const std::string& nName, int nRawDataIndex, const std::string& nType, uint32_t nTypeSize, bool nIsArray, uint32_t nCount)
+ : type(nType), typeSize(nTypeSize), isArray(nIsArray), count(nCount)
+{
+ name = nName;
+ rawDataIndex = nRawDataIndex;
+}
+
+ZSymbol::ZSymbol(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData, int nRawDataIndex, ZFile* nParent)
+{
+ rawData.assign(nRawData.begin(), nRawData.end());
+ rawDataIndex = nRawDataIndex;
+ parent = nParent;
+
+ ParseXML(reader);
+}
+
+ZSymbol* ZSymbol::ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData, int nRawDataIndex, ZFile* parent)
+{
+ ZSymbol* symbol = new ZSymbol(reader, nRawData, nRawDataIndex, parent);
+
+ return symbol;
+}
+
+void ZSymbol::ParseXML(tinyxml2::XMLElement* reader)
+{
+ ZResource::ParseXML(reader);
+
+ const char* typeXml = reader->Attribute("Type");
+ if (typeXml == nullptr) {
+ fprintf(stderr, "ZSymbol::ParseXML: Warning in '%s'.\n\t Missing 'Type' attribute in xml. Defaulting to 'void*'.\n", name.c_str());
+ type = "void*";
+ }
+ else {
+ type = std::string(typeXml);
+ }
+
+ const char* typeSizeXml = reader->Attribute("TypeSize");
+ if (typeSizeXml == nullptr) {
+ fprintf(stderr, "ZSymbol::ParseXML: Warning in '%s'.\n\t Missing 'TypeSize' attribute in xml. Defaulting to '4'.\n", name.c_str());
+ typeSize = 4; // Size of a word.
+ }
+ else {
+ typeSize = std::strtoul(typeSizeXml, nullptr, 0);
+ }
+
+ const char* countXml = reader->Attribute("Count");
+ if (countXml != nullptr) {
+ isArray = true;
+ if (!std::string(countXml).empty()) {
+ count = std::strtoul(countXml, nullptr, 0);
+ }
+ }
+}
+
+int ZSymbol::GetRawDataSize()
+{
+ if (isArray) {
+ return count * typeSize;
+ }
+ return typeSize;
+}
+
+std::string ZSymbol::GetSourceOutputHeader(const std::string& prefix)
+{
+ if (isArray) {
+ if (count == 0) {
+ return StringHelper::Sprintf("extern %s %s%s[];\n", type.c_str(), prefix.c_str(), name.c_str());
+ }
+ return StringHelper::Sprintf("extern %s %s%s[%i];\n", type.c_str(), prefix.c_str(), name.c_str(), count);
+ }
+ return StringHelper::Sprintf("extern %s %s%s;\n", type.c_str(), prefix.c_str(), name.c_str());
+}
+
+std::string ZSymbol::GetSourceTypeName()
+{
+ return type;
+}
+
+ZResourceType ZSymbol::GetResourceType()
+{
+ return ZResourceType::Symbol;
+}
+
diff --git a/ZAPD/ZSymbol.h b/ZAPD/ZSymbol.h
new file mode 100644
index 0000000..e26df1c
--- /dev/null
+++ b/ZAPD/ZSymbol.h
@@ -0,0 +1,29 @@
+#pragma once
+
+#include "ZResource.h"
+#include "tinyxml2.h"
+
+class ZSymbol : public ZResource
+{
+protected:
+ std::string type;
+ uint32_t typeSize;
+ bool isArray = false;
+ uint32_t count = 0;
+
+public:
+ ZSymbol() = default;
+ ZSymbol(const std::string& nName, int nRawDataIndex, const std::string& nType, uint32_t nTypeSize, bool nIsArray, uint32_t nCount);
+ ZSymbol(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData, int nRawDataIndex, ZFile* nParent);
+
+ static ZSymbol* ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData, int nRawDataIndex, ZFile* parent);
+
+ void ParseXML(tinyxml2::XMLElement* reader) override;
+
+ int GetRawDataSize() override;
+
+ std::string GetSourceOutputHeader(const std::string& prefix) override;
+
+ std::string GetSourceTypeName() override;
+ ZResourceType GetResourceType() override;
+};