From 922f810fcffeed32a09e9f15e0f9a61afa19b6be Mon Sep 17 00:00:00 2001 From: coco875 <59367621+coco875@users.noreply.github.com> Date: Sun, 10 Aug 2025 02:00:03 +0200 Subject: [PATCH 1/6] put g_verbosity in static (#191) --- lib/libmio0/utils.c | 3 --- lib/libmio0/utils.h | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/libmio0/utils.c b/lib/libmio0/utils.c index 9ffa3c0..4ab3dc1 100644 --- a/lib/libmio0/utils.c +++ b/lib/libmio0/utils.c @@ -16,9 +16,6 @@ #include "utils.h" -// global verbosity setting -int g_verbosity = 0; - int read_s16_be(unsigned char *buf) { unsigned tmp = read_u16_be(buf); diff --git a/lib/libmio0/utils.h b/lib/libmio0/utils.h index 5576301..1004417 100644 --- a/lib/libmio0/utils.h +++ b/lib/libmio0/utils.h @@ -69,7 +69,7 @@ typedef struct } dir_list; // global verbosity setting -extern int g_verbosity; +static int g_verbosity = 0; #undef ERROR #define ERROR(...) fprintf(stderr, __VA_ARGS__) From 191b1bc467eb8dfbf3bd6d88df8752dff577afe2 Mon Sep 17 00:00:00 2001 From: coco875 <59367621+coco875@users.noreply.github.com> Date: Mon, 11 Aug 2025 04:42:57 +0200 Subject: [PATCH 2/6] Update Companion.h (#192) --- src/Companion.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Companion.h b/src/Companion.h index e427bbf..95da2a4 100644 --- a/src/Companion.h +++ b/src/Companion.h @@ -134,6 +134,9 @@ public: explicit Companion(std::filesystem::path rom, const ArchiveType otr, const bool debug, const std::string& srcDir = "", const std::string& destPath = "") : Companion(rom, otr, debug, false, srcDir, destPath) {} + + explicit Companion(std::vector rom, const ArchiveType otr, const bool debug, const std::string& srcDir = "", const std::string& destPath = "") : + Companion(rom, otr, debug, false, srcDir, destPath) {} void Init(ExportType type); From 8bf444f1e9c8d0a9e5c28d0150be435e52d80ade Mon Sep 17 00:00:00 2001 From: coco875 <59367621+coco875@users.noreply.github.com> Date: Mon, 11 Aug 2025 19:45:50 +0200 Subject: [PATCH 3/6] Update Companion.cpp (#193) --- src/Companion.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Companion.cpp b/src/Companion.cpp index eb4b2b2..1f865af 100644 --- a/src/Companion.cpp +++ b/src/Companion.cpp @@ -384,6 +384,8 @@ void Companion::ParseCurrentFileConfig(YAML::Node node) { this->gCurrentDirectory = currentDirectory; this->gCurrentExternalFiles = currentExternalFiles; this->gFileHeader.clear(); + } else { + SPDLOG_INFO("Skipping external file {} as it has already been processed", externalFileName); } } } From cb68cf1bf2b3d24562e63f78a33a6419bd0864e9 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Tue, 12 Aug 2025 01:14:33 -0600 Subject: [PATCH 4/6] Fixed torch to build on emscripten --- CMakeLists.txt | 14 +++++++++++++ src/Companion.cpp | 49 +++++++++++++++++++++++-------------------- src/lib/web.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 4 ++-- 4 files changed, 95 insertions(+), 25 deletions(-) create mode 100644 src/lib/web.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c26e0b5..05ebc20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,15 @@ option(BUILD_FZERO "Build with F-Zero X support" ON) option(BUILD_MARIO_ARTIST "Build with Mario Artist support" ON) option(BUILD_NAUDIO "Build with NAudio support" ON) +if(EMSCRIPTEN) + set(BUILD_SM64 OFF) # TODO: This is broken for some reason + add_definitions(-D__EMSCRIPTEN__) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --bind -lidbfs.js -s MODULARIZE=1 -s EXPORT_ES6=1 -s NO_DISABLE_EXCEPTION_CATCHING -s ALLOW_MEMORY_GROWTH=1 -s EXPORT_NAME=createModule -s EXPORTED_RUNTIME_METHODS='[\"FS\", \"IDBFS\"]'") + if(EMSCRIPTEN_PRELOAD) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --preload-file ${EMSCRIPTEN_PRELOAD}") + endif() +endif() + ################################################################################ # Set target arch type if empty. Visual studio solution generator provides it. ################################################################################ @@ -225,6 +234,11 @@ if(NOT USE_STANDALONE) endif() if(USE_STANDALONE) + if(EMSCRIPTEN) + set(CMAKE_CXX_FLAGS_DEBUG "-O3") + set(SPDLOG_USE_STD_FORMAT ON) + endif() + FetchContent_Declare( spdlog GIT_REPOSITORY https://github.com/gabime/spdlog.git diff --git a/src/Companion.cpp b/src/Companion.cpp index 1f865af..73e1923 100644 --- a/src/Companion.cpp +++ b/src/Companion.cpp @@ -210,8 +210,9 @@ void Companion::Init(const ExportType type) { this->RegisterFactory("NAUDIO:V1:ADPCM_BOOK", std::make_shared()); this->RegisterFactory("NAUDIO:V1:SEQUENCE", std::make_shared()); #endif - +#ifndef __EMSCRIPTEN__ // We call this manually this->Process(); +#else } void Companion::ParseEnums(std::string& header) { @@ -1038,7 +1039,7 @@ void Companion::Process() { auto hash = this->gCartridge->GetHash(); - SPDLOG_INFO("ROM decompressed to {}", hash); + SPDLOG_CRITICAL("ROM decompressed to {}", hash); if (hash != target) { throw std::runtime_error("Hash mismatch"); @@ -1204,23 +1205,25 @@ void Companion::Process() { this->ParseHash(); - SPDLOG_INFO("------------------------------------------------"); + SPDLOG_CRITICAL("------------------------------------------------"); spdlog::set_pattern(line); - SPDLOG_INFO("Starting Torch..."); + SPDLOG_CRITICAL("Starting Torch..."); if(this->gConfig.parseMode == ParseMode::Default) { - SPDLOG_INFO("Game: {}", this->gCartridge->GetGameTitle()); - SPDLOG_INFO("CRC: {}", this->gCartridge->GetCRC()); - SPDLOG_INFO("Version: {}", this->gCartridge->GetVersion()); - SPDLOG_INFO("Country: [{}]", this->gCartridge->GetCountryCode()); - SPDLOG_INFO("Hash: {}", this->gCartridge->GetHash()); - SPDLOG_INFO("Assets: {}", this->gAssetPath); + SPDLOG_CRITICAL("Game: {}", this->gCartridge->GetGameTitle()); + SPDLOG_CRITICAL("CRC: {}", this->gCartridge->GetCRC()); + SPDLOG_CRITICAL("Version: {}", this->gCartridge->GetVersion()); + SPDLOG_CRITICAL("Country: [{}]", this->gCartridge->GetCountryCode()); + SPDLOG_CRITICAL("Hash: {}", this->gCartridge->GetHash()); + SPDLOG_CRITICAL("Assets: {}", this->gAssetPath); } else { - SPDLOG_INFO("Mode: Directory"); - SPDLOG_INFO("Directory: {}", rom["folder"].as()); + SPDLOG_CRITICAL("Mode: Directory"); + SPDLOG_CRITICAL("Directory: {}", rom["folder"].as()); } + SPDLOG_CRITICAL("------------------------------------------------"); + AudioManager::Instance = new AudioManager(); BinaryWrapper* wrapper = nullptr; @@ -1278,7 +1281,7 @@ void Companion::Process() { } if(wrapper != nullptr) { - SPDLOG_INFO("Writing version file"); + SPDLOG_CRITICAL("Writing version file"); wrapper->AddFile("version", vWriter.ToVector()); vWriter.Close(); wrapper->Close(); @@ -1292,10 +1295,10 @@ void Companion::Process() { auto end = duration_cast(system_clock::now().time_since_epoch()); auto level = spdlog::get_level(); spdlog::set_level(spdlog::level::info); - SPDLOG_INFO("Done! Took {}ms", end.count() - start.count()); + SPDLOG_CRITICAL("Done! Took {}ms", end.count() - start.count()); + SPDLOG_CRITICAL("------------------------------------------------"); spdlog::set_level(level); spdlog::set_pattern(regular); - SPDLOG_INFO("------------------------------------------------"); Decompressor::ClearCache(); this->gCartridge = nullptr; @@ -1307,10 +1310,10 @@ void Companion::Pack(const std::string& folder, const std::string& output, const spdlog::set_level(spdlog::level::debug); spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] %v"); - SPDLOG_INFO("------------------------------------------------"); + SPDLOG_CRITICAL("------------------------------------------------"); - SPDLOG_INFO("Starting Torch..."); - SPDLOG_INFO("Scanning {}", folder); + SPDLOG_CRITICAL("Starting Torch..."); + SPDLOG_CRITICAL("Scanning {}", folder); auto start = duration_cast(system_clock::now().time_since_epoch()); std::unordered_map> files; @@ -1345,14 +1348,14 @@ void Companion::Pack(const std::string& folder, const std::string& output, const // Remove parent folder normalized = normalized.substr(folder.length() + 1); wrapper->AddFile(normalized, data); - SPDLOG_INFO("> Added {}", normalized); + SPDLOG_CRITICAL("> Added {}", normalized); } auto end = duration_cast(system_clock::now().time_since_epoch()); - SPDLOG_INFO("Done! Took {}ms", end.count() - start.count()); - SPDLOG_INFO("Exported to {}", output); + SPDLOG_CRITICAL("Done! Took {}ms", end.count() - start.count()); + SPDLOG_CRITICAL("Exported to {}", output); spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] %v"); - SPDLOG_INFO("------------------------------------------------"); + SPDLOG_CRITICAL("------------------------------------------------"); wrapper->Close(); } @@ -1380,7 +1383,7 @@ std::optional> Companion::RegisterAsset(cons void Companion::RegisterFactory(const std::string& type, const std::shared_ptr& factory) { this->gFactories[type] = factory; - SPDLOG_DEBUG("Registered factory for {}", type); + SPDLOG_INFO("Registered factory for {}", type); } std::optional> Companion::GetFactory(const std::string &type) { diff --git a/src/lib/web.cpp b/src/lib/web.cpp new file mode 100644 index 0000000..eed9c26 --- /dev/null +++ b/src/lib/web.cpp @@ -0,0 +1,53 @@ +#ifdef __EMSCRIPTEN__ +#include "Companion.h" + +#include +using namespace emscripten; + +Companion* Companion::Instance; + +void Bind(Companion* instance) { + Companion::Instance = instance; +} + +EMSCRIPTEN_BINDINGS(Torch) { + register_vector("VectorUInt8"); + + function("Bind", &Bind, allow_raw_pointers()); + + enum_("ExportType") + .value("Header", ExportType::Header) + .value("Code", ExportType::Code) + .value("Binary", ExportType::Binary) + .value("Modding", ExportType::Modding) + .value("XML", ExportType::XML); + + enum_("ArchiveType") + .value("None", ArchiveType::None) + .value("OTR", ArchiveType::OTR) + .value("O2R", ArchiveType::O2R); + + enum_("CountryCode") + .value("Japan", N64::CountryCode::Japan) + .value("NorthAmerica", N64::CountryCode::NorthAmerica) + .value("Europe", N64::CountryCode::Europe) + .value("Unknown", N64::CountryCode::Unknown); + + class_("Cartridge") + .function("GetGameTitle", &N64::Cartridge::GetGameTitle) + .function("GetCountryCode", &N64::Cartridge::GetCountryCode) + .function("GetCountry", &N64::Cartridge::GetCountry) + .function("GetVersion", &N64::Cartridge::GetVersion) + .function("GetHash", &N64::Cartridge::GetHash) + .function("GetCRC", &N64::Cartridge::GetCRC); + + class_("Companion") + .constructor, ArchiveType, bool, bool, std::string, std::string>() + .constructor, ArchiveType, bool, bool, std::string>() + .constructor, ArchiveType, bool, bool>() + .function("Init", &Companion::Init) + .function("GetCartridge", &Companion::GetCartridge, allow_raw_pointers()) + .function("Process", &Companion::Process) + .function("GetRomData", &Companion::GetRomData, allow_raw_pointers()); +} +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6fb356f..b5ea9fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,9 +2,9 @@ #include "CLI11.hpp" #include "Companion.h" -Companion* Companion::Instance; +#if defined(STANDALONE) && !defined(__EMSCRIPTEN__) -#ifdef STANDALONE +Companion* Companion::Instance; int main(int argc, char *argv[]) { CLI::App app{"Torch - [T]orch is [O]ur [R]esource [C]onversion [H]elper\n\ From c7977c6e355b7779c2a44aa64c769382cc0b21f5 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Tue, 12 Aug 2025 12:03:06 -0600 Subject: [PATCH 5/6] Fixed typo --- src/Companion.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Companion.cpp b/src/Companion.cpp index 73e1923..62d6493 100644 --- a/src/Companion.cpp +++ b/src/Companion.cpp @@ -212,7 +212,7 @@ void Companion::Init(const ExportType type) { #endif #ifndef __EMSCRIPTEN__ // We call this manually this->Process(); -#else +#endif } void Companion::ParseEnums(std::string& header) { From f3febeb691b7a55c190f8fcfc4d8b4dc71574d51 Mon Sep 17 00:00:00 2001 From: coco875 <59367621+coco875@users.noreply.github.com> Date: Wed, 20 Aug 2025 19:08:40 +0200 Subject: [PATCH 6/6] Update Companion.cpp (#195) --- src/Companion.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Companion.cpp b/src/Companion.cpp index 62d6493..4485f3d 100644 --- a/src/Companion.cpp +++ b/src/Companion.cpp @@ -941,6 +941,7 @@ void Companion::ProcessFile(YAML::Node root) { std::string buffer = stream.str(); if(buffer.empty()) { + SPDLOG_WARN("No data to write for {}", this->gCurrentFile); return; } @@ -951,6 +952,7 @@ void Companion::ProcessFile(YAML::Node root) { } std::ofstream file(output, std::ios::binary); + SPDLOG_INFO("Writing {} to {}", this->gCurrentFile, output); if(this->gConfig.exporterType == ExportType::Header) { fs::path entryPath = this->gCurrentFile;