From 0e2e844d8c7329259b88004c114d666c5781006e Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Thu, 2 Jul 2026 23:44:18 -0600 Subject: Implemented default behavior --- src/main.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 0593ed6..4145e93 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include "CLI11.hpp" #include "Companion.h" #ifdef PM64_SUPPORT @@ -41,6 +44,55 @@ static void LaunchUI() { UI::SetBackend(nullptr); } + +// Default (no-subcommand) flow: list any ROMs in the cwd and ask the user to pick +// one or type a path. Returns the chosen path, or empty if nothing was given. +static std::string PromptForRom() { + namespace fs = std::filesystem; + std::vector found; + std::error_code ec; + for (const auto& entry : fs::directory_iterator(fs::current_path(), ec)) { + if (!entry.is_regular_file()) { + continue; + } + std::string ext = entry.path().extension().string(); + std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c) { return (char)std::tolower(c); }); + if (ext == ".z64" || ext == ".n64" || ext == ".v64") { + found.push_back(entry.path().filename().string()); + } + } + std::sort(found.begin(), found.end()); + + if (!found.empty()) { + std::cout << "ROMs in this directory:" << std::endl; + for (size_t i = 0; i < found.size(); ++i) { + std::cout << " [" << i << "] " << found[i] << std::endl; + } + std::cout << "Select a ROM by number, or type a path [0]: " << std::flush; + } else { + std::cout << "Enter the path to a ROM: " << std::flush; + } + + std::string line; + if (!std::getline(std::cin, line)) { + return {}; + } + const auto first = line.find_first_not_of(" \t\r\n"); + const auto last = line.find_last_not_of(" \t\r\n"); + line = first == std::string::npos ? std::string() : line.substr(first, last - first + 1); + + if (line.empty()) { + return found.empty() ? std::string() : found.front(); + } + // A bare number selects from the list; anything else is treated as a path. + if (!found.empty() && line.find_first_not_of("0123456789") == std::string::npos) { + const size_t idx = std::stoul(line); + if (idx < found.size()) { + return found[idx]; + } + } + return line; +} #endif int main(int argc, char* argv[]) { @@ -351,6 +403,30 @@ int main(int argc, char* argv[]) { }); #endif +#ifdef BUILD_UI + // Default behavior with no subcommand: if the cwd looks like a project (has a + // config.yml), open the viewer and prompt for a ROM instead of printing help. + if (argc == 1 && std::filesystem::exists("config.yml")) { + const std::string rom = PromptForRom(); + if (rom.empty()) { + std::cout << app.help() << std::endl; + return 0; + } + if (!std::filesystem::exists(rom)) { + std::cout << "ROM not found: " << rom << std::endl; + return 1; + } + Companion::Instance = new Companion(rom, ArchiveType::None, false, "", ""); +#ifdef PM64_SUPPORT + PM64Audio::SetPreviewAssets(true); +#endif + std::atomic assetCount{ 0 }; + Companion::Instance->Init(ExportType::Binary, assetCount, false); + LaunchUI(); + return 0; + } +#endif + try { app.parse(argc, argv); } catch (const CLI::ParseError& e) { -- cgit v1.2.3