Skip to content

Commit

Permalink
Fixed GeoLayout Processing
Browse files Browse the repository at this point in the history
  • Loading branch information
KiritoDv committed Jun 9, 2024
1 parent 521452c commit 0153dd0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/Companion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,13 +922,19 @@ void Companion::ProcessFile(YAML::Node root) {
fs::path entryPath = this->gCurrentFile;
std::string symbol = entryPath.stem().string();
std::transform(symbol.begin(), symbol.end(), symbol.begin(), toupper);
file << "#ifndef " << symbol << "_H" << std::endl;
file << "#define " << symbol << "_H" << std::endl << std::endl;
if(this->IsOTRMode()){
file << "#pragma once\n\n";
} else {
file << "#ifndef " << symbol << "_H" << std::endl;
file << "#define " << symbol << "_H" << std::endl << std::endl;
}
if(!this->gFileHeader.empty()) {
file << this->gFileHeader << std::endl;
}
file << buffer;
file << std::endl << "#endif" << std::endl;
if(!this->IsOTRMode()){
file << std::endl << "#endif" << std::endl;
}
} else {
if(!this->gFileHeader.empty()) {
file << this->gFileHeader << std::endl;
Expand Down
4 changes: 3 additions & 1 deletion src/factories/DisplayListFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ ExportResult DListBinaryExporter::Export(std::ostream &write, std::shared_ptr<IP
}

if(opcode == GBI(G_MOVEMEM)) {
// TODO: Fix this opcode
continue;
auto ptr = w1;
auto dec = Companion::Instance->GetNodeByAddr(ptr);

Expand All @@ -320,7 +322,7 @@ ExportResult DListBinaryExporter::Export(std::ostream &write, std::shared_ptr<IP

switch (gbi) {
case GBIVersion::f3d:
index = C0(0, 8);
index = C0(16, 8);
offset = 0;
break;
default:
Expand Down
66 changes: 62 additions & 4 deletions src/factories/sm64/GeoLayoutFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,60 @@
#include "geo/GeoUtils.h"
#include "utils/TorchUtils.h"

#include <regex>

std::unordered_map<uint32_t, std::string> gFunctionMap;
static std::regex pattern(R"(0x([0-9a-fA-F]+)\s{16}([^\s]+))");

uint64_t RegisterAutoGen(uint32_t ptr, std::string type) {

if (ptr != 0) {
YAML::Node node;
node["type"] = type;
node["offset"] = ptr;
Companion::Instance->AddAsset(node);
} else {
SPDLOG_WARN("RegisterAutoGen: ptr is 0 type: {}", type);
}

return ptr;
}

void StoreFunc(uint32_t vram) {
return;

if(!gFunctionMap.contains(vram)) {
return;
}

auto name = gFunctionMap[vram];
SPDLOG_INFO("Found Function: 0x{:X} Name: {}", vram, name);

std::ofstream outfile;
outfile.open("map.txt", std::ios_base::app);
outfile << "{ 0x" << std::hex << vram << ", " << name << " },\n";

gFunctionMap.erase(vram);
}

SM64::GeoLayoutFactory::GeoLayoutFactory() {
// std::ifstream file("/Users/lywx/Downloads/sm64.jp.map");
// std::string str;
// while (std::getline(file, str)) {
// if(str.find('=') != std::string::npos) {
// continue;
// }

// std::smatch match;
// if (std::regex_search(str, match, pattern)) {
// gFunctionMap[std::stoul(match[1].str(), nullptr, 16)] = match[2].str();
// }
// }
}

ExportResult SM64::GeoCodeExporter::Export(std::ostream&write, std::shared_ptr<IParsedData> data, std::string&entryName, YAML::Node&node, std::string* replacement) {
const auto cmds = std::static_pointer_cast<GeoLayout>(data)->commands;
const auto symbol = GetSafeNode(node, "symbol", entryName);
auto offset = GetSafeNode<uint32_t>(node, "offset");
size_t size = 0;
uint32_t indentCount = 1;
uint32_t cmdCount = 0;

Expand Down Expand Up @@ -258,6 +295,15 @@ ExportResult SM64::GeoBinaryExporter::Export(std::ostream&write, std::shared_ptr
}

ExportResult SM64::GeoHeaderExporter::Export(std::ostream&write, std::shared_ptr<IParsedData> data, std::string&entryName, YAML::Node&node, std::string* replacement) {
const auto symbol = GetSafeNode(node, "symbol", entryName);

if(Companion::Instance->IsOTRMode()){
write << "static const ALIGN_ASSET(2) char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
return std::nullopt;
}

write << "extern GeoLayout " << symbol << "[];\n";

return std::nullopt;
}

Expand Down Expand Up @@ -297,8 +343,10 @@ std::optional<std::shared_ptr<IParsedData>> SM64::GeoLayoutFactory::parse(std::v
cmd += 0x08 << CMD_SIZE_SHIFT;
break;
}
case GeoOpcode::Return:
case GeoOpcode::Return: {
processing = false;
break;
}
case GeoOpcode::OpenNode:
case GeoOpcode::CloseNode: {
cmd += 0x04 << CMD_SIZE_SHIFT;
Expand Down Expand Up @@ -359,6 +407,8 @@ std::optional<std::shared_ptr<IParsedData>> SM64::GeoLayoutFactory::parse(std::v
auto ptr = cur_geo_cmd_u32(0x08);
arguments.emplace_back(ptr);

StoreFunc(ptr);

cmd += 0x04 << CMD_SIZE_SHIFT;
}

Expand Down Expand Up @@ -393,6 +443,8 @@ std::optional<std::shared_ptr<IParsedData>> SM64::GeoLayoutFactory::parse(std::v
arguments.emplace_back(cs);
arguments.emplace_back(ptr);

StoreFunc(ptr);

cmd += 0x08 << CMD_SIZE_SHIFT;
break;
}
Expand All @@ -412,6 +464,8 @@ std::optional<std::shared_ptr<IParsedData>> SM64::GeoLayoutFactory::parse(std::v
arguments.emplace_back(focus);
arguments.emplace_back(ptr);

StoreFunc(ptr);

cmd += 0x14 << CMD_SIZE_SHIFT;
break;
}
Expand Down Expand Up @@ -547,7 +601,7 @@ std::optional<std::shared_ptr<IParsedData>> SM64::GeoLayoutFactory::parse(std::v

arguments.emplace_back(param);
arguments.emplace_back(ptr);
SPDLOG_INFO("Found ASM: 0x{:X}", ptr);
StoreFunc(ptr);

cmd += 0x08 << CMD_SIZE_SHIFT;
break;
Expand All @@ -559,6 +613,8 @@ std::optional<std::shared_ptr<IParsedData>> SM64::GeoLayoutFactory::parse(std::v
arguments.emplace_back(bg);
arguments.emplace_back(ptr);

StoreFunc(ptr);

cmd += 0x08 << CMD_SIZE_SHIFT;
break;
}
Expand All @@ -578,6 +634,8 @@ std::optional<std::shared_ptr<IParsedData>> SM64::GeoLayoutFactory::parse(std::v
auto ptr = cur_geo_cmd_u32(0x08);
auto player = cur_geo_cmd_u8(0x01);

StoreFunc(ptr);

arguments.emplace_back(ptr);
arguments.emplace_back(player);

Expand Down
2 changes: 2 additions & 0 deletions src/factories/sm64/GeoLayoutFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class GeoBinaryExporter : public BaseExporter {

class GeoLayoutFactory : public BaseFactory {
public:
GeoLayoutFactory();

std::optional<std::shared_ptr<IParsedData>> parse(std::vector<uint8_t>& buffer, YAML::Node& data) override;
std::unordered_map<ExportType, std::shared_ptr<BaseExporter>> GetExporters() override {
return {
Expand Down

0 comments on commit 0153dd0

Please sign in to comment.