mirror of
https://github.com/izzy2lost/Torch.git
synced 2026-07-06 00:18:35 -07:00
Implemented ObjectInit factory and added an enum system
This commit is contained in:
@@ -34,6 +34,8 @@
|
||||
#include "factories/sf64/AnimFactory.h"
|
||||
#include "factories/sf64/ScriptFactory.h"
|
||||
#include "factories/sf64/HitboxFactory.h"
|
||||
#include "factories/sf64/ObjInitFactory.h"
|
||||
#include <regex>
|
||||
|
||||
using namespace std::chrono;
|
||||
namespace fs = std::filesystem;
|
||||
@@ -77,11 +79,56 @@ void Companion::Init(const ExportType type) {
|
||||
this->RegisterFactory("SF64:MSG_TABLE", std::make_shared<SF64::MessageLookupFactory>());
|
||||
this->RegisterFactory("SF64:SCRIPT", std::make_shared<SF64::ScriptFactory>());
|
||||
this->RegisterFactory("SF64:HITBOX", std::make_shared<SF64::HitboxFactory>());
|
||||
this->RegisterFactory("SF64:OBJECT_INIT", std::make_shared<SF64::ObjInitFactory>());
|
||||
|
||||
|
||||
this->Process();
|
||||
}
|
||||
|
||||
void Companion::ParseEnums(std::string& header) {
|
||||
std::ifstream file(header);
|
||||
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("Failed to open file");
|
||||
}
|
||||
|
||||
std::regex enumRegex(R"(enum\s+(\w+)\s*(?:\s*:\s*(\w+))?[\s\n\r]*\{)");
|
||||
|
||||
std::string line;
|
||||
std::smatch match;
|
||||
std::string enumName;
|
||||
|
||||
bool inEnum = false;
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
if (!inEnum && std::regex_search(line, match, enumRegex) && match.size() > 1) {
|
||||
enumName = match.str(1);
|
||||
inEnum = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!inEnum) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(line.find("}") != std::string::npos) {
|
||||
inEnum = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Remove any comments and non-alphanumeric characters
|
||||
line = std::regex_replace(line, std::regex(R"((/\*.*?\*/)|([^a-zA-Z0-9=_\-\.]))"), "");
|
||||
|
||||
if(line.find("=") != std::string::npos) {
|
||||
auto name = line.substr(0, line.find("="));
|
||||
auto value = line.substr(line.find("=") + 1);
|
||||
this->gEnums[enumName][value.starts_with("-") ? -std::stoi(value.substr(1)) : std::stoi(value)] = name;
|
||||
} else {
|
||||
this->gEnums[enumName][this->gEnums[enumName].size()] = line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Companion::ExtractNode(YAML::Node& node, std::string& name, SWrapper* binary) {
|
||||
std::ostringstream stream;
|
||||
|
||||
@@ -409,6 +456,13 @@ void Companion::Process() {
|
||||
}
|
||||
}
|
||||
|
||||
if(cfg["enums"]) {
|
||||
auto enums = GetSafeNode<std::vector<std::string>>(cfg, "enums");
|
||||
for (auto& file : enums) {
|
||||
this->ParseEnums(file);
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INFO("------------------------------------------------");
|
||||
spdlog::set_pattern(line);
|
||||
|
||||
@@ -806,6 +860,18 @@ std::optional<Table> Companion::SearchTable(uint32_t addr){
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::string> Companion::GetEnumFromValue(const std::string& key, int32_t id) {
|
||||
if(!this->gEnums.contains(key)){
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if(!this->gEnums[key].contains(id)){
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return this->gEnums[key][id];
|
||||
}
|
||||
|
||||
std::optional<std::uint32_t> Companion::GetFileOffsetFromSegmentedAddr(const uint8_t segment) const {
|
||||
|
||||
auto segments = this->gConfig.segment;
|
||||
|
||||
+4
-1
@@ -89,6 +89,7 @@ public:
|
||||
|
||||
GBIVersion GetGBIVersion() const { return this->gConfig.gbi.version; }
|
||||
GBIMinorVersion GetGBIMinorVersion() const { return this->gConfig.gbi.subversion; }
|
||||
std::optional<std::string> GetEnumFromValue(const std::string& key, int id);
|
||||
|
||||
std::optional<std::uint32_t> GetFileOffsetFromSegmentedAddr(uint8_t segment) const;
|
||||
std::optional<std::tuple<std::string, YAML::Node>> GetNodeByAddr(uint32_t addr);
|
||||
@@ -116,6 +117,7 @@ private:
|
||||
std::vector<uint8_t> gRomData;
|
||||
std::filesystem::path gRomPath;
|
||||
std::shared_ptr<N64::Cartridge> gCartridge;
|
||||
std::unordered_map<std::string, std::unordered_map<int32_t, std::string>> gEnums;
|
||||
|
||||
// Temporal Variables
|
||||
std::string gCurrentFile;
|
||||
@@ -133,8 +135,9 @@ private:
|
||||
std::unordered_map<std::string, std::map<std::string, std::vector<std::pair<uint32_t, std::string>>>> gWriteMap;
|
||||
std::unordered_map<std::string, std::unordered_map<uint32_t, std::tuple<std::string, YAML::Node>>> gAddrMap;
|
||||
|
||||
void ParseCurrentFileConfig(YAML::Node node);
|
||||
void ParseEnums(std::string& file);
|
||||
void ParseModdingConfig();
|
||||
void ParseCurrentFileConfig(YAML::Node node);
|
||||
void RegisterFactory(const std::string& type, const std::shared_ptr<BaseFactory>& factory);
|
||||
void ExtractNode(YAML::Node& node, std::string& name, SWrapper* binary);
|
||||
};
|
||||
|
||||
@@ -39,5 +39,6 @@ enum class ResourceType {
|
||||
Skeleton = 0x534B454C, // SKEL
|
||||
Script = 0x53435250, // SCRP
|
||||
Hitbox = 0x48544258, // HTBX
|
||||
ObjectInit = 0x4F42494E, // OBIN
|
||||
};
|
||||
} // namespace LUS
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "ObjInitFactory.h"
|
||||
#include "utils/Decompressor.h"
|
||||
#include "Companion.h"
|
||||
|
||||
#define NUM(x, w) std::dec << std::setfill(' ') << std::setw(w) << x
|
||||
|
||||
void SF64::ObjInitCodeExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
|
||||
auto symbol = GetSafeNode(node, "symbol", entryName);
|
||||
auto objs = std::static_pointer_cast<ObjInitData>(raw)->mObjInit;
|
||||
|
||||
write << "ObjectInit " << symbol << "[] = {\n";
|
||||
for(auto& obj : objs) {
|
||||
auto enumName = Companion::Instance->GetEnumFromValue("ObjectId", obj.id).value_or(std::to_string(obj.id));
|
||||
write << fourSpaceTab << "{ ";
|
||||
write << NUM(obj.zPos1, 8) << "f, ";
|
||||
write << NUM(obj.zPos2, 7) << ", ";
|
||||
write << NUM(obj.xPos, 7) << ", ";
|
||||
write << NUM(obj.yPos, 7) << ", ";
|
||||
write << "{ " << NUM(obj.rot.x, 3) << ", " << NUM(obj.rot.y, 3) << ", " << NUM(obj.rot.z, 3) << " }, ";
|
||||
write << enumName << " },\n";
|
||||
}
|
||||
write << "};\n\n";
|
||||
}
|
||||
|
||||
void SF64::ObjInitBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
|
||||
auto writer = LUS::BinaryWriter();
|
||||
auto data = std::static_pointer_cast<ObjInitData>(raw)->mObjInit;
|
||||
|
||||
WriteHeader(writer, LUS::ResourceType::ObjectInit, 0);
|
||||
writer.Write((uint32_t) data.size());
|
||||
for(auto& obj : data) {
|
||||
writer.Write(obj.zPos1);
|
||||
writer.Write(obj.zPos2);
|
||||
writer.Write(obj.xPos);
|
||||
writer.Write(obj.yPos);
|
||||
writer.Write(obj.rot.x);
|
||||
writer.Write(obj.rot.y);
|
||||
writer.Write(obj.rot.z);
|
||||
writer.Write(obj.id);
|
||||
}
|
||||
writer.Finish(write);
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<IParsedData>> SF64::ObjInitFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) {
|
||||
auto [_, segment] = Decompressor::AutoDecode(node, buffer);
|
||||
|
||||
LUS::BinaryReader reader(segment.data, segment.size);
|
||||
reader.SetEndianness(LUS::Endianness::Big);
|
||||
std::vector<ObjectInit> objects;
|
||||
|
||||
bool processing = true;
|
||||
while(processing) {
|
||||
float zPos1 = reader.ReadFloat();
|
||||
int16_t zPos2 = reader.ReadInt16();
|
||||
int16_t xPos = reader.ReadInt16();
|
||||
int16_t yPos = reader.ReadInt16();
|
||||
int16_t rotX = reader.ReadInt16();
|
||||
int16_t rotY = reader.ReadInt16();
|
||||
int16_t rotZ = reader.ReadInt16();
|
||||
int16_t id = reader.ReadInt16();
|
||||
reader.ReadInt16();
|
||||
|
||||
processing = id != -1;
|
||||
|
||||
objects.push_back({ zPos1, zPos2, xPos, yPos, {rotX, rotY, rotZ}, id});
|
||||
}
|
||||
|
||||
return std::make_shared<ObjInitData>(objects);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include "../BaseFactory.h"
|
||||
|
||||
namespace SF64 {
|
||||
struct Vec3s {
|
||||
/* 0x0 */ int16_t x;
|
||||
/* 0x2 */ int16_t y;
|
||||
/* 0x4 */ int16_t z;
|
||||
}; // size = 0x6;
|
||||
|
||||
struct ObjectInit {
|
||||
/* 0x00 */ float zPos1;
|
||||
/* 0x04 */ int16_t zPos2;
|
||||
/* 0x06 */ int16_t xPos;
|
||||
/* 0x08 */ int16_t yPos;
|
||||
/* 0x0A */ Vec3s rot;
|
||||
/* 0x10 */ int16_t id;
|
||||
};
|
||||
|
||||
class ObjInitData : public IParsedData {
|
||||
public:
|
||||
std::vector<ObjectInit> mObjInit;
|
||||
|
||||
explicit ObjInitData(std::vector<ObjectInit> objInit) : mObjInit(objInit) {}
|
||||
};
|
||||
|
||||
class ObjInitBinaryExporter : public BaseExporter {
|
||||
void Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override;
|
||||
};
|
||||
|
||||
class ObjInitCodeExporter : public BaseExporter {
|
||||
void Export(std::ostream& write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node& node, std::string* replacement) override;
|
||||
};
|
||||
|
||||
class ObjInitFactory : public BaseFactory {
|
||||
public:
|
||||
std::optional<std::shared_ptr<IParsedData>> parse(std::vector<uint8_t>& buffer, YAML::Node& data) override;
|
||||
std::optional<std::shared_ptr<IParsedData>> parse_modding(std::vector<uint8_t>& buffer, YAML::Node& data) override {
|
||||
return std::nullopt;
|
||||
}
|
||||
inline std::unordered_map<ExportType, std::shared_ptr<BaseExporter>> GetExporters() override {
|
||||
return {
|
||||
REGISTER(Binary, ObjInitBinaryExporter)
|
||||
REGISTER(Code, ObjInitCodeExporter)
|
||||
};
|
||||
}
|
||||
bool SupportModdedAssets() override { return false; }
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user