diff options
| author | inspectredc <78732756+inspectredc@users.noreply.github.com> | 2024-04-23 17:38:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-23 10:38:58 -0600 |
| commit | 91e834d819c82f0d64cf4f71ea9d1788c9135bda (patch) | |
| tree | 33dfd5dfbdf280cf764b9421176aa20cd6d5cf03 /src | |
| parent | 686ca4115a85c8e13db62af61b3dafd73c55a51f (diff) | |
Painting Mapping Factory (#104)
* Painting Factory
* update factory naming
---------
Co-authored-by: Lywx <kiritodev01@gmail.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/Companion.cpp | 2 | ||||
| -rw-r--r-- | src/factories/ResourceType.h | 1 | ||||
| -rw-r--r-- | src/factories/sm64/PaintingMapFactory.cpp | 104 | ||||
| -rw-r--r-- | src/factories/sm64/PaintingMapFactory.h | 46 |
4 files changed, 153 insertions, 0 deletions
diff --git a/src/Companion.cpp b/src/Companion.cpp index 87b3810..317fe64 100644 --- a/src/Companion.cpp +++ b/src/Companion.cpp @@ -37,6 +37,7 @@ #include "factories/sm64/MacroFactory.h" #include "factories/sm64/MovtexFactory.h" #include "factories/sm64/MovtexQuadFactory.h" +#include "factories/sm64/PaintingMapFactory.h" #include "factories/sm64/TrajectoryFactory.h" #include "factories/mk64/CourseVtx.h" @@ -98,6 +99,7 @@ void Companion::Init(const ExportType type) { this->RegisterFactory("SM64:MACRO", std::make_shared<SM64::MacroFactory>()); this->RegisterFactory("SM64:MOVTEX_QUAD", std::make_shared<SM64::MovtexQuadFactory>()); this->RegisterFactory("SM64:MOVTEX", std::make_shared<SM64::MovtexFactory>()); + this->RegisterFactory("SM64:PAINTING_MAP", std::make_shared<SM64::PaintingMapFactory>()); this->RegisterFactory("SM64:TRAJECTORY", std::make_shared<SM64::TrajectoryFactory>()); // MK64 specific diff --git a/src/factories/ResourceType.h b/src/factories/ResourceType.h index a25c558..0076b7b 100644 --- a/src/factories/ResourceType.h +++ b/src/factories/ResourceType.h @@ -32,6 +32,7 @@ enum class ResourceType { MacroObject = 0x4D41434F, // MACO Movtex = 0x4D4F5654, // MOVT MovtexQuad = 0x4D4F5651, // MOVQ + PaintingData = 0x5041494E, // PAIN Trajectory = 0x5452414A, // TRAJ // MK64 diff --git a/src/factories/sm64/PaintingMapFactory.cpp b/src/factories/sm64/PaintingMapFactory.cpp new file mode 100644 index 0000000..b17108b --- /dev/null +++ b/src/factories/sm64/PaintingMapFactory.cpp @@ -0,0 +1,104 @@ +#include "PaintingMapFactory.h" +#include "spdlog/spdlog.h" + +#include "Companion.h" +#include "utils/Decompressor.h" +#include "utils/TorchUtils.h" +#include <regex> + +#define FORMAT_INT(x, w) std::dec << std::setfill(' ') << std::setw(w) << x + +ExportResult SM64::PaintingMapHeaderExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement) { + const auto symbol = GetSafeNode(node, "symbol", entryName); + + if(Companion::Instance->IsOTRMode()){ + write << "static const char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n"; + return std::nullopt; + } + + write << "extern PaintingData " << symbol << "[];\n"; + return std::nullopt; +} + +ExportResult SM64::PaintingMapCodeExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) { + const auto symbol = GetSafeNode(node, "symbol", entryName); + const auto offset = GetSafeNode<uint32_t>(node, "offset"); + + auto paintingData = std::static_pointer_cast<SM64::PaintingData>(raw); + + write << "static const PaintingData " << symbol << "[] = {\n"; + + write << fourSpaceTab << paintingData->mPaintingMappings.size() << ",\n"; + + for (auto &mapping : paintingData->mPaintingMappings) { + write << fourSpaceTab; + write << FORMAT_INT(mapping.vtxId << "," << mapping.texX << "," << mapping.texY, 6) << ",\n"; + } + + write << fourSpaceTab << paintingData->mPaintingMappings.size() << ",\n"; + + for (auto &group : paintingData->mPaintingGroups) { + write << fourSpaceTab; + write << FORMAT_INT(group, 4) << ",\n"; + } + + write << "};\n"; + + size_t size = (paintingData->mPaintingMappings.size() + paintingData->mPaintingGroups.size()) * 3 * sizeof(int16_t) + 2; + + return offset + size; +} + +ExportResult SM64::PaintingMapBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) { + auto writer = LUS::BinaryWriter(); + auto paintingData = std::static_pointer_cast<SM64::PaintingData>(raw); + + WriteHeader(writer, LUS::ResourceType::PaintingData, 0); + + writer.Write((int16_t)paintingData->mPaintingMappings.size()); + + for (auto &mapping : paintingData->mPaintingMappings) { + writer.Write(mapping.vtxId); + writer.Write(mapping.texX); + writer.Write(mapping.texY); + } + + writer.Write((int16_t)paintingData->mPaintingGroups.size()); + + for (auto &group : paintingData->mPaintingGroups) { + writer.Write(group.x); + writer.Write(group.y); + writer.Write(group.z); + } + + writer.Finish(write); + return std::nullopt; +} + +std::optional<std::shared_ptr<IParsedData>> SM64::PaintingMapFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) { + std::vector<PaintingMapping> paintingMappings; + std::vector<Vec3s> paintingGroups; + auto [_, segment] = Decompressor::AutoDecode(node, buffer); + LUS::BinaryReader reader(segment.data, segment.size); + reader.SetEndianness(LUS::Endianness::Big); + + auto mappingsSize = reader.ReadInt16(); + + for (int16_t i = 0; i < mappingsSize; ++i) { + auto vtxId = reader.ReadInt16(); + auto texX = reader.ReadInt16(); + auto texY = reader.ReadInt16(); + paintingMappings.emplace_back(vtxId, texX, texY); + } + + auto groupSize = reader.ReadInt16(); + + for (int16_t i = 0; i < groupSize; ++i) { + auto x = reader.ReadInt16(); + auto y = reader.ReadInt16(); + auto z = reader.ReadInt16(); + paintingGroups.emplace_back(x, y, z); + } + + return std::make_shared<SM64::PaintingData>(paintingMappings, paintingGroups); +} diff --git a/src/factories/sm64/PaintingMapFactory.h b/src/factories/sm64/PaintingMapFactory.h new file mode 100644 index 0000000..d73a651 --- /dev/null +++ b/src/factories/sm64/PaintingMapFactory.h @@ -0,0 +1,46 @@ +#pragma once + +#include "factories/BaseFactory.h" +#include "types/Vec3D.h" + +namespace SM64 { + +struct PaintingMapping { + int16_t vtxId; + int16_t texX; + int16_t texY; + PaintingMapping(int16_t vtxId, int16_t texX, int16_t texY) : vtxId(vtxId), texX(texX), texY(texY) {} +}; + +class PaintingData : public IParsedData { +public: + std::vector<PaintingMapping> mPaintingMappings; + std::vector<Vec3s> mPaintingGroups; + + PaintingData(std::vector<PaintingMapping> paintingMappings, std::vector<Vec3s> paintingGroups) : mPaintingMappings(std::move(paintingMappings)), mPaintingGroups(std::move(paintingGroups)) {} +}; + +class PaintingMapHeaderExporter : public BaseExporter { + ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override; +}; + +class PaintingMapBinaryExporter : public BaseExporter { + ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override; +}; + +class PaintingMapCodeExporter : public BaseExporter { + ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override; +}; + +class PaintingMapFactory : public BaseFactory { +public: + std::optional<std::shared_ptr<IParsedData>> parse(std::vector<uint8_t>& buffer, YAML::Node& data) override; + inline std::unordered_map<ExportType, std::shared_ptr<BaseExporter>> GetExporters() override { + return { + REGISTER(Code, PaintingMapCodeExporter) + REGISTER(Header, PaintingMapHeaderExporter) + REGISTER(Binary, PaintingMapBinaryExporter) + }; + } +}; +} |
