Skip to content

Commit

Permalink
Implemented GetSafeNodeByAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
KiritoDv committed Jan 30, 2025
1 parent f3d6841 commit 7b83e15
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/Companion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,24 @@ std::optional<std::tuple<std::string, YAML::Node>> Companion::GetNodeByAddr(cons
return this->gAddrMap[this->gCurrentFile][addr];
}

std::optional<std::tuple<std::string, YAML::Node>> Companion::GetSafeNodeByAddr(const uint32_t addr, std::string type) {
auto node = this->GetNodeByAddr(addr);

if(!node.has_value()) {
return std::nullopt;
}

auto [name, n] = node.value();
auto n_type = GetSafeNode<std::string>(n, "type");

if(n_type != type) {
throw std::runtime_error("Requested node type does not match with the target node type at " + Torch::to_hex(addr, false));
}

return node;

}

std::optional<ParseResultData> Companion::GetParseDataByAddr(uint32_t addr) {
if(!this->gParseResults.contains(this->gCurrentFile)){
for (auto &file : this->gCurrentExternalFiles) {
Expand Down
3 changes: 2 additions & 1 deletion src/Companion.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ class Companion {
std::optional<ParseResultData> GetParseDataBySymbol(const std::string& symbol);

std::optional<std::uint32_t> GetFileOffsetFromSegmentedAddr(uint8_t segment) const;
std::optional<std::tuple<std::string, YAML::Node>> GetNodeByAddr(uint32_t addr);
std::optional<std::shared_ptr<BaseFactory>> GetFactory(const std::string& type);
std::optional<std::tuple<std::string, YAML::Node>> GetNodeByAddr(uint32_t addr);
std::optional<std::tuple<std::string, YAML::Node>> GetSafeNodeByAddr(const uint32_t addr, std::string type);
std::optional<std::vector<std::tuple<std::string, YAML::Node>>> GetNodesByType(const std::string& type);

std::optional<std::uint32_t> GetFileOffset(void) const { return this->gCurrentFileOffset; };
Expand Down
18 changes: 9 additions & 9 deletions src/factories/DisplayListOverrides.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int Vtx(uint32_t ptr, int32_t num) {
return 1;
}

auto dec = Companion::Instance->GetNodeByAddr(ptr);
auto dec = Companion::Instance->GetSafeNodeByAddr(ptr, "VTX");

if(dec.has_value()){
auto node = std::get<1>(dec.value());
Expand All @@ -86,7 +86,7 @@ int Vtx(uint32_t ptr, int32_t num) {
}

int Texture(uint32_t ptr, int32_t fmt, int32_t siz, int32_t width, int32_t height, int32_t pal) {
auto dec = Companion::Instance->GetNodeByAddr(ptr);
auto dec = Companion::Instance->GetSafeNodeByAddr(ptr, "TEXTURE");

if(dec.has_value()){
auto node = std::get<1>(dec.value());
Expand All @@ -101,7 +101,7 @@ int Texture(uint32_t ptr, int32_t fmt, int32_t siz, int32_t width, int32_t heigh
}

int Palette(uint32_t ptr, int32_t idx, int32_t count) {
auto dec = Companion::Instance->GetNodeByAddr(ptr);
auto dec = Companion::Instance->GetSafeNodeByAddr(ptr, "TEXTURE");

if(dec.has_value()){
auto node = std::get<1>(dec.value());
Expand All @@ -116,7 +116,7 @@ int Palette(uint32_t ptr, int32_t idx, int32_t count) {
}

int Lights(uint32_t ptr, int32_t count) {
auto dec = Companion::Instance->GetNodeByAddr(ptr);
auto dec = Companion::Instance->GetSafeNodeByAddr(ptr, "LIGHTS");

if(dec.has_value()){
auto node = std::get<1>(dec.value());
Expand All @@ -131,7 +131,7 @@ int Lights(uint32_t ptr, int32_t count) {
}

int Light(uint32_t ptr) {
auto res = Companion::Instance->GetNodeByAddr(ptr);
auto res = Companion::Instance->GetSafeNodeByAddr(ptr, "LIGHTS");

if(res.has_value()){
auto node = std::get<1>(res.value());
Expand All @@ -141,7 +141,7 @@ int Light(uint32_t ptr) {
return 1;
}

res = Companion::Instance->GetNodeByAddr(ptr - 0x8);
res = Companion::Instance->GetSafeNodeByAddr(ptr - 0x8, "LIGHTS");

if(res.has_value()){
auto node = std::get<1>(res.value());
Expand All @@ -156,7 +156,7 @@ int Light(uint32_t ptr) {
}

int DisplayList(uint32_t ptr) {
auto dec = Companion::Instance->GetNodeByAddr(ptr);
auto dec = Companion::Instance->GetSafeNodeByAddr(ptr, "GFX");

if(dec.has_value()){
auto node = std::get<1>(dec.value());
Expand All @@ -171,7 +171,7 @@ int DisplayList(uint32_t ptr) {
}

int Viewport(uint32_t ptr) {
auto dec = Companion::Instance->GetNodeByAddr(ptr);
auto dec = Companion::Instance->GetSafeNodeByAddr(ptr, "VP");

if(dec.has_value()){
auto node = std::get<1>(dec.value());
Expand All @@ -186,7 +186,7 @@ int Viewport(uint32_t ptr) {
}

int Matrix(uint32_t ptr) {
auto dec = Companion::Instance->GetNodeByAddr(ptr);
auto dec = Companion::Instance->GetSafeNodeByAddr(ptr, "MTX");

if(dec.has_value()){
auto node = std::get<1>(dec.value());
Expand Down

0 comments on commit 7b83e15

Please sign in to comment.