diff options
| author | Kenix3 <kenixwhisperwind@gmail.com> | 2020-12-30 19:25:06 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-30 19:25:06 -0500 |
| commit | 27c17b20d38a72b7a8016852fa85311dcdc40f11 (patch) | |
| tree | cc62e053f3ff368e1affbd809a34e2e99ce1c74a /ZAPD/ZBlob.cpp | |
| parent | fd533747ddfa6645a53f6b1356b5b68d7f2f0bed (diff) | |
ZAP2 project renamed to ZAPD (#27)
Diffstat (limited to 'ZAPD/ZBlob.cpp')
| -rw-r--r-- | ZAPD/ZBlob.cpp | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/ZAPD/ZBlob.cpp b/ZAPD/ZBlob.cpp new file mode 100644 index 0000000..edbc9e0 --- /dev/null +++ b/ZAPD/ZBlob.cpp @@ -0,0 +1,104 @@ +#include "ZBlob.h" +#include "File.h" +#include "ZFile.h" +#include "BitConverter.h" +#include "Path.h" +#include "StringHelper.h" + +using namespace tinyxml2; +using namespace std; + +ZBlob::ZBlob() : ZResource() +{ + +} + +ZBlob::ZBlob(std::vector<uint8_t> nRawData, int nRawDataIndex, int size, std::string nName) : ZBlob() +{ + rawDataIndex = nRawDataIndex; + rawData = vector<uint8_t>(nRawData.data() + rawDataIndex, nRawData.data() + rawDataIndex + size); + name = nName; +} + +ZBlob* ZBlob::ExtractFromXML(XMLElement* reader, vector<uint8_t> nRawData, int nRawDataIndex, string nRelPath) +{ + ZBlob* blob = new ZBlob(); + + blob->rawDataIndex = nRawDataIndex; + + blob->ParseXML(reader); + int size = strtol(reader->Attribute("Size"), NULL, 16); + blob->rawData = vector<uint8_t>(nRawData.data() + blob->rawDataIndex, nRawData.data() + blob->rawDataIndex + size); + blob->relativePath = nRelPath; + + return blob; +} + +ZBlob* ZBlob::BuildFromXML(XMLElement* reader, string inFolder, bool readFile) +{ + ZBlob* blob = new ZBlob(); + + blob->ParseXML(reader); + + if (readFile) + blob->rawData = File::ReadAllBytes(inFolder + "/" + blob->name + ".bin"); + + return blob; +} + +ZBlob* ZBlob::FromFile(string filePath) +{ + int comp; + ZBlob* blob = new ZBlob(); + blob->name = StringHelper::Split(Path::GetFileNameWithoutExtension(filePath), ".")[0]; + blob->rawData = File::ReadAllBytes(filePath); + + return blob; +} + +string ZBlob::GetSourceOutputCode(std::string prefix) +{ + sourceOutput = ""; + //sourceOutput += StringHelper::Sprintf("u8 _%s_%s[] = \n{\n", prefix.c_str(), name.c_str()); + + for (int i = 0; i < rawData.size(); i += 1) + { + if (i % 16 == 0) + sourceOutput += "\t"; + + sourceOutput += StringHelper::Sprintf("0x%02X, ", rawData[i]); + + if (i % 16 == 15) + sourceOutput += "\n"; + } + + //sourceOutput += "};\n"; + + return sourceOutput; +} + +string ZBlob::GetSourceOutputHeader(std::string prefix) +{ + return StringHelper::Sprintf("extern u8 %s[];\n", name.c_str()); +} + +void ZBlob::Save(string outFolder) +{ + //printf("NAME = %s\n", name.c_str()); + File::WriteAllBytes(outFolder + "/" + name + ".bin", rawData); +} + +bool ZBlob::IsExternalResource() +{ + return true; +} + +string ZBlob::GetExternalExtension() +{ + return "bin"; +} + +ZResourceType ZBlob::GetResourceType() +{ + return ZResourceType::Blob; +} |
