From 378575792b9f0eeddae6e9fa53e1412d129a89a5 Mon Sep 17 00:00:00 2001 From: inspectredc Date: Wed, 3 Apr 2024 21:54:26 +0100 Subject: fix generic array factory for u8/s8 and add binary exporter --- src/factories/GenericArrayFactory.cpp | 107 ++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 4 deletions(-) (limited to 'src/factories/GenericArrayFactory.cpp') diff --git a/src/factories/GenericArrayFactory.cpp b/src/factories/GenericArrayFactory.cpp index 87affed..81144f5 100644 --- a/src/factories/GenericArrayFactory.cpp +++ b/src/factories/GenericArrayFactory.cpp @@ -83,11 +83,11 @@ GenericArray::GenericArray(std::vector data) : mData(std::move(data) for (auto &datum : mData) { switch (static_cast(datum.index())) { case ArrayType::u8: { - mMaxWidth = std::max(mMaxWidth, GET_MAG_U(std::get(datum))); + mMaxWidth = std::max(mMaxWidth, GET_MAG_U((uint32_t)std::get(datum))); break; } case ArrayType::s8: { - mMaxWidth = std::max(mMaxWidth, GET_MAG(std::get(datum))); + mMaxWidth = std::max(mMaxWidth, GET_MAG((int32_t)std::get(datum))); break; } case ArrayType::u16: { @@ -192,10 +192,10 @@ ExportResult ArrayCodeExporter::Export(std::ostream &write, std::shared_ptr(datum.index())) { case ArrayType::u8: - write << FORMAT_INT(std::get(datum), array->mMaxWidth) << ", "; + write << FORMAT_INT((uint32_t)std::get(datum), array->mMaxWidth) << ", "; break; case ArrayType::s8: - write << FORMAT_INT(std::get(datum), array->mMaxWidth) << ", "; + write << FORMAT_INT((int32_t)std::get(datum), array->mMaxWidth) << ", "; break; case ArrayType::u16: write << FORMAT_INT(std::get(datum), array->mMaxWidth) << ", "; @@ -249,6 +249,105 @@ ExportResult ArrayCodeExporter::Export(std::ostream &write, std::shared_ptr raw, std::string& entryName, YAML::Node &node, std::string* replacement ) { + auto writer = LUS::BinaryWriter(); + const auto type = GetSafeNode(node, "array_type"); + auto array = std::static_pointer_cast(raw); + + if (arrayTypeMap.find(type) == arrayTypeMap.end()) { + SPDLOG_ERROR("Unknown Generic Array type '{}'", type); + throw std::runtime_error("Unknown Generic Array type " + type); + } + + ArrayType arrayType = arrayTypeMap.at(type); + + WriteHeader(writer, LUS::ResourceType::Blob, 0); + writer.Write((uint32_t) array->mData.size()); + + for (auto &datum : array->mData) { + switch (static_cast(datum.index())) { + case ArrayType::u8: { + writer.Write(std::get(datum)); + break; + } + case ArrayType::s8: { + writer.Write(std::get(datum)); + break; + } + case ArrayType::u16: { + writer.Write(std::get(datum)); + break; + } + case ArrayType::s16: { + writer.Write(std::get(datum)); + break; + } + case ArrayType::u32: { + writer.Write(std::get(datum)); + break; + } + case ArrayType::s32: { + writer.Write(std::get(datum)); + break; + } + case ArrayType::u64: { + writer.Write(std::get(datum)); + break; + } + case ArrayType::f32: { + writer.Write(std::get(datum)); + break; + } + case ArrayType::f64: { + writer.Write(std::get(datum)); + break; + } + case ArrayType::Vec2f: { + auto [x, y] = std::get(datum); + writer.Write(x); + writer.Write(y); + break; + } + case ArrayType::Vec3f: { + auto [x, y, z] = std::get(datum); + writer.Write(x); + writer.Write(y); + writer.Write(z); + break; + } + case ArrayType::Vec3s: { + auto [x, y, z] = std::get(datum); + writer.Write(x); + writer.Write(y); + writer.Write(z); + break; + } + case ArrayType::Vec3i: { + auto [x, y, z] = std::get(datum); + writer.Write(x); + writer.Write(y); + writer.Write(z); + break; + } + case ArrayType::Vec4f: { + auto [x, y, z, w] = std::get(datum); + writer.Write(x); + writer.Write(y); + writer.Write(z); + writer.Write(w); + break; + } + case ArrayType::Vec4s: { + auto [x, y, z, w] = std::get(datum); + writer.Write(x); + writer.Write(y); + writer.Write(z); + writer.Write(w); + break; + } + } + } + + writer.Finish(write); return std::nullopt; } -- cgit v1.2.3