Implemented ObjectInit factory and added an enum system

This commit is contained in:
KiritoDv
2024-03-12 21:44:19 -06:00
committed by Lywx
parent f1781973bc
commit b56eefab94
5 changed files with 190 additions and 1 deletions
+66
View File
@@ -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;