Skip to content

Commit

Permalink
[librii] g3d: Add back warning messages
Browse files Browse the repository at this point in the history
  • Loading branch information
riidefi committed Jun 13, 2024
1 parent 29e0642 commit b559134
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
2 changes: 1 addition & 1 deletion source/brres/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion source/brres/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "brres"
license = "MIT"
version = "0.1.0"
version = "0.0.0"
edition = "2021"
description = "gctex is a Rust crate designed for encoding and decoding texture formats used in the Nintendo GameCube and Wii games. The library provides C bindings, making it useful in both Rust and C/C++ based projects."
homepage = "https://github.com/riidefi/RiiStudio/tree/master/source/brres"
Expand Down
44 changes: 42 additions & 2 deletions source/brres/lib/brres-sys/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

#include "./librii/g3d/data/Archive.hpp"

#include <vendor/json_struct.h>

#include <LibBadUIFramework/Plugins.hpp>

bool gTestMode __attribute__((weak)) = false;

namespace librii::g3d {
Expand Down Expand Up @@ -55,19 +59,55 @@ static void SetCResult(CResult& result, DumpResult&& dumped) {
};
}

struct Warning {
JS_OBJ(mclass, domain, body);
int mclass;
std::string domain;
std::string body;
};

struct ArchiveExtensions {
JS_OBJ(warnings);
std::vector<Warning> warnings;
};

extern "C" {

WASM_EXPORT u32 imp_brres_read_from_bytes(CResult* result, const void* buf,
u32 len) {
std::span<const u8> buf_span(reinterpret_cast<const u8*>(buf),
reinterpret_cast<const u8*>(buf) + len);
auto arc =
librii::g3d::Archive::fromMemory(buf_span, "brres_read_from_bytes");

ArchiveExtensions ext;

kpi::LightIOTransaction trans;
trans.callback = [&](kpi::IOMessageClass message_class,
const std::string_view domain,
const std::string_view message_body) {
ext.warnings.push_back(Warning{
static_cast<int>(message_class),
std::string(domain),
std::string(message_body),
});
};
auto arc = librii::g3d::Archive::fromMemory(buf_span, "brres_read_from_bytes",
trans);

DumpResult dumped;
bool ok = true;
if (arc.has_value()) {
dumped = librii::g3d::DumpJson(*arc);

auto extJson = JS::serializeStruct(ext);

// Assumes final character is a "}"
assert(dumped.jsonData.size() >= 1);
assert(dumped.jsonData[dumped.jsonData.size() - 1] == '}');
dumped.jsonData.resize(dumped.jsonData.size() - 1);
// Assumes inital character is a "{"
assert(extJson.size() >= 1);
assert(extJson[0] == '{');
dumped.jsonData = dumped.jsonData + "," + extJson.substr(1);
} else {
dumped.jsonData = arc.error();
ok = false;
Expand Down
22 changes: 21 additions & 1 deletion source/librii/g3d/io/ArchiveIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <brres/lib/brres-sys/include/brres_sys.h>
#include <rsl/WriteFile.hpp>

#include <vendor/nlohmann/json.hpp>

namespace librii::g3d {
void TestJson(const librii::g3d::Archive& archive);

Expand Down Expand Up @@ -50,7 +52,7 @@ Result<std::vector<u8>> Archive::write() const {
Result<Archive> Archive::fromFile(std::string path,
kpi::LightIOTransaction& transaction) {
auto reader = TRY(oishii::BinaryReader::FromFilePath(path, std::endian::big));
return fromMemory(reader.mBuf, path);
return fromMemory(reader.mBuf, path, transaction);
}
Result<Archive> Archive::fromFile(std::string path) {
kpi::LightIOTransaction trans;
Expand All @@ -71,6 +73,24 @@ Result<Archive> Archive::fromMemory(std::span<const u8> buf, std::string path,
kpi::LightIOTransaction& trans) {
auto res = TRY(brres::read_from_bytes(buf));
auto arc = TRY(ReadJsonArc(res.jsonData, res.collatedBuffer));
auto j = nlohmann::json::parse(res.jsonData);
if (j.contains("warnings") && j["warnings"].is_array()) {
for (auto& w : j["warnings"]) {
if (!j.contains("mclass") || !j["mclass"].is_number_unsigned()) {
continue;
}
if (!j.contains("domain") || !j["domain"].is_string()) {
continue;
}
if (!j.contains("body") || !j["body"].is_string()) {
continue;
}
int mclass = j["mclass"];
std::string domain = j["domain"];
std::string body = j["body"];
trans.callback(static_cast<kpi::IOMessageClass>(mclass), domain, body);
}
}
return arc;
}
Result<Archive> Archive::fromMemory(std::span<const u8> buf, std::string path) {
Expand Down
4 changes: 2 additions & 2 deletions source/vendor/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16454,7 +16454,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
}
}

throw std::out_of_range("key not found");
JSON_THROW(std::out_of_range("key not found"));
}

const T& at(const Key& key) const
Expand All @@ -16467,7 +16467,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
}
}

throw std::out_of_range("key not found");
JSON_THROW(std::out_of_range("key not found"));
}

size_type erase(const Key& key)
Expand Down

0 comments on commit b559134

Please sign in to comment.