mirror of
https://github.com/izzy2lost/Torch.git
synced 2026-07-06 00:18:35 -07:00
MacroObject Factory
This commit is contained in:
@@ -34,6 +34,7 @@
|
||||
#include "factories/sm64/DictionaryFactory.h"
|
||||
#include "factories/sm64/TextFactory.h"
|
||||
#include "factories/sm64/GeoLayoutFactory.h"
|
||||
#include "factories/sm64/MacroFactory.h"
|
||||
#include "factories/sm64/MovtexFactory.h"
|
||||
#include "factories/sm64/MovtexQuadFactory.h"
|
||||
|
||||
@@ -93,6 +94,7 @@ void Companion::Init(const ExportType type) {
|
||||
this->RegisterFactory("SM64:ANIM", std::make_shared<SM64::AnimationFactory>());
|
||||
this->RegisterFactory("SM64:COLLISION", std::make_shared<SM64::CollisionFactory>());
|
||||
this->RegisterFactory("SM64:GEO_LAYOUT", std::make_shared<SM64::GeoLayoutFactory>());
|
||||
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>());
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ enum class ResourceType {
|
||||
Dictionary = 0x44494354, // DICT
|
||||
GeoLayout = 0x47454F20, // GEO
|
||||
Collision = 0x434F4C20, // COL
|
||||
MacroObject = 0x4D41434F, // MACO
|
||||
Movtex = 0x4D4F5654, // MOVT
|
||||
MovtexQuad = 0x4D4F5651, // MOVQ
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
#include "MacroFactory.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
#include "Companion.h"
|
||||
#include "utils/Decompressor.h"
|
||||
#include "utils/TorchUtils.h"
|
||||
#include <regex>
|
||||
|
||||
ExportResult SM64::MacroHeaderExporter::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 MacroObject " << symbol << "[];\n";
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
ExportResult SM64::MacroCodeExporter::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 macro = std::static_pointer_cast<SM64::MacroData>(raw);
|
||||
|
||||
write << "const MacroObject " << symbol << "[] = {\n";
|
||||
|
||||
for (auto &object : macro->mMacroData) {
|
||||
write << fourSpaceTab;
|
||||
|
||||
if (object.behParam == 0) {
|
||||
write << "MACRO_OBJECT(";
|
||||
} else {
|
||||
write << "MACRO_OBJECT_WITH_BEH_PARAM(";
|
||||
}
|
||||
|
||||
write << object.preset << ", ";
|
||||
write << object.yaw << ", ";
|
||||
write << object.posX << ", ";
|
||||
write << object.posY << ", ";
|
||||
write << object.posZ;
|
||||
|
||||
if (object.behParam != 0) {
|
||||
write << ", " << object.behParam;
|
||||
}
|
||||
write << "),\n";
|
||||
}
|
||||
|
||||
write << fourSpaceTab << "MACRO_OBJECT_END(),";
|
||||
|
||||
write << "\n};\n";
|
||||
|
||||
size_t size = (macro->mMacroData.size()) * sizeof(MacroObject) + 1;
|
||||
|
||||
return offset + size;
|
||||
}
|
||||
|
||||
ExportResult SM64::MacroBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
|
||||
auto writer = LUS::BinaryWriter();
|
||||
auto macro = std::static_pointer_cast<SM64::MacroData>(raw);
|
||||
|
||||
WriteHeader(writer, LUS::ResourceType::MacroObject, 0);
|
||||
|
||||
writer.Write((uint32_t)macro->mMacroData.size());
|
||||
|
||||
for (auto &object : macro->mMacroData) {
|
||||
writer.Write(static_cast<int16_t>(object.preset));
|
||||
writer.Write(object.yaw);
|
||||
writer.Write(object.posX);
|
||||
writer.Write(object.posY);
|
||||
writer.Write(object.posZ);
|
||||
writer.Write(object.behParam);
|
||||
}
|
||||
|
||||
writer.Finish(write);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<IParsedData>> SM64::MacroFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) {
|
||||
std::vector<MacroObject> macroData;
|
||||
auto [_, segment] = Decompressor::AutoDecode(node, buffer);
|
||||
LUS::BinaryReader reader(segment.data, segment.size);
|
||||
reader.SetEndianness(LUS::Endianness::Big);
|
||||
|
||||
while (true) {
|
||||
auto presetYaw = reader.ReadInt16();
|
||||
if (presetYaw == 0x1E) {
|
||||
break;
|
||||
}
|
||||
|
||||
int16_t yaw = ((presetYaw >> 9) * 45) / 0x10;
|
||||
int16_t preset = (presetYaw - ((yaw * 0x10 / 45) << 9)) - 0x1F;
|
||||
auto posX = reader.ReadInt16();
|
||||
auto posY = reader.ReadInt16();
|
||||
auto posZ = reader.ReadInt16();
|
||||
auto behParam = reader.ReadInt16();
|
||||
macroData.emplace_back(static_cast<MacroPresets>(preset), yaw, posX, posY, posZ, behParam);
|
||||
}
|
||||
|
||||
return std::make_shared<SM64::MacroData>(macroData);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include "factories/BaseFactory.h"
|
||||
#include "types/Vec3D.h"
|
||||
#include "macro/MacroPresets.h"
|
||||
|
||||
namespace SM64 {
|
||||
|
||||
struct MacroObject {
|
||||
MacroPresets preset;
|
||||
int16_t yaw;
|
||||
int16_t posX;
|
||||
int16_t posY;
|
||||
int16_t posZ;
|
||||
int16_t behParam;
|
||||
MacroObject(MacroPresets preset, int16_t yaw, int16_t posX, int16_t posY, int16_t posZ, int16_t behParam) : preset(preset), yaw(yaw), posX(posX), posY(posY), posZ(posZ), behParam(behParam) {}
|
||||
};
|
||||
|
||||
class MacroData : public IParsedData {
|
||||
public:
|
||||
std::vector<MacroObject> mMacroData;
|
||||
|
||||
MacroData(std::vector<MacroObject> macroData) : mMacroData(std::move(macroData)) {}
|
||||
};
|
||||
|
||||
class MacroHeaderExporter : public BaseExporter {
|
||||
ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override;
|
||||
};
|
||||
|
||||
class MacroBinaryExporter : public BaseExporter {
|
||||
ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override;
|
||||
};
|
||||
|
||||
class MacroCodeExporter : public BaseExporter {
|
||||
ExportResult Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override;
|
||||
};
|
||||
|
||||
class MacroFactory : 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, MacroCodeExporter)
|
||||
REGISTER(Header, MacroHeaderExporter)
|
||||
REGISTER(Binary, MacroBinaryExporter)
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -24,7 +24,7 @@ ExportResult SM64::MovtexCodeExporter::Export(std::ostream &write, std::shared_p
|
||||
|
||||
auto movtex = std::static_pointer_cast<SM64::MovtexData>(raw);
|
||||
|
||||
write << "static Movtex " << symbol << "[] = {";
|
||||
write << "static Movtex " << symbol << "[] = {\n";
|
||||
|
||||
if (movtex->mIsQuad) {
|
||||
auto numLists = movtex->mMovtexData.at(0);
|
||||
|
||||
@@ -24,7 +24,7 @@ ExportResult SM64::MovtexQuadCodeExporter::Export(std::ostream &write, std::shar
|
||||
|
||||
auto quadData = std::static_pointer_cast<SM64::MovtexQuadData>(raw);
|
||||
|
||||
write << "const struct MovtexQuadCollection " << symbol << "[] = {";
|
||||
write << "const struct MovtexQuadCollection " << symbol << "[] = {\n";
|
||||
|
||||
for (auto &quad: quadData->mMovtexQuads) {
|
||||
write << "{" << quad.first << ", ";
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user