From 45cb3c20287601ea7cfb693f5e7f57d533b24860 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 29 Oct 2026 13:29:04 -0700 Subject: [PATCH 1/6] Add Command File Runner, a developer program driven by text command files Answering "does this detector fire on that screen?" meant a rebuild and a full program run. Command File Runner runs .txt files dropped into /inbox against the live console, one command per line (press, dpad, stick, wait, shot/shots, detect, wait_for/expect, state, close_game, help, stop), and writes a timed result file with screenshots to /outbox, stopping a file at its first failing line. Options set the command folder and whether to stop once the inbox is empty. Games can register their own commands and detectors. README.md next to the code documents the file format and every command; like the other testing programs it has no page on the documentation site. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../NintendoSwitch/NintendoSwitch_Panels.cpp | 2 + .../NintendoSwitch_CommandFileRunner.cpp | 572 ++++++++++++++++++ .../NintendoSwitch_CommandFileRunner.h | 98 +++ .../Programs/CommandFileRunner/README.md | 124 ++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 5 files changed, 798 insertions(+) create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.h create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/README.md diff --git a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_Panels.cpp b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_Panels.cpp index aa933aeb63..86f3b1f0c2 100644 --- a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_Panels.cpp +++ b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_Panels.cpp @@ -22,6 +22,7 @@ #include "Programs/NintendoSwitch_TurboMacro.h" #include "Programs/NintendoSwitch_PushJoySticks.h" #include "Programs/NintendoSwitch_PreventSleep.h" +#include "Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.h" #include "Programs/NintendoSwitch_FriendCodeAdder.h" #include "Programs/NintendoSwitch_FriendDelete.h" #include "Programs/NintendoSwitch_RecordKeyboardController.h" @@ -78,6 +79,7 @@ std::vector PanelListFactory::make_panels() const{ ret.emplace_back("---- Testing ----"); ret.emplace_back(GameConsole::make_ConsolePanel()); ret.emplace_back(GameConsole::make_ConsoleProgram()); + ret.emplace_back(make_SingleSwitchProgram()); if (STATIC_GLOBALS.DEVELOPER_MODE){ ret.emplace_back("---- Developer Tools ----"); diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp new file mode 100644 index 0000000000..4f54363b26 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp @@ -0,0 +1,572 @@ +/* Command File Runner + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Filesystem/FileIO.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "NintendoSwitch_CommandFileRunner.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +using namespace std::chrono_literals; + + +namespace{ + +// Defaults for timings a command line leaves out. +constexpr Milliseconds BUTTON_HOLD = 160ms; // Long enough for every game to see the press. +constexpr Milliseconds BUTTON_RELEASE = 160ms; +constexpr Milliseconds DPAD_HOLD = 100ms; // One menu step without auto-repeat. +constexpr Milliseconds DPAD_RELEASE = 200ms; +constexpr Milliseconds STICK_HOLD = 160ms; +constexpr Milliseconds STICK_RELEASE = 160ms; +constexpr Milliseconds SHOTS_INTERVAL = 500ms; +constexpr Milliseconds WAIT_FOR_TIMEOUT = 5000ms; + +// How often "wait_for" and "expect" check a new frame. +constexpr Milliseconds DETECTOR_POLL_INTERVAL = 100ms; + +// How often an empty inbox is checked for a new file. +constexpr Milliseconds INBOX_POLL_INTERVAL = 500ms; + + + +struct Registry{ + Mutex lock; + std::map detectors; + std::map> commands; // name -> usage, command +}; +Registry& registry(){ + static Registry registry; + return registry; +} + +// Detectors every game can use. +void register_builtin_detectors(){ + static const bool registered = []{ + register_command_file_detector("black", [](ConsoleHandle&){ return std::make_unique(); }); + register_command_file_detector("white", [](ConsoleHandle&){ return std::make_unique(); }); + register_command_file_detector("home", [](ConsoleHandle& console){ return std::make_unique(console, COLOR_RED); }); + register_command_file_detector("user_select", [](ConsoleHandle& console){ return std::make_unique(console); }); + return true; + }(); + (void)registered; +} + +std::unique_ptr make_detector(const std::string& name, ConsoleHandle& console){ + Registry& r = registry(); + LockGuard lg(r.lock); + auto iter = r.detectors.find(name); + if (iter == r.detectors.end()){ + throw ParseException("Unknown detector: " + name + " (try \"help\")"); + } + return iter->second(console); +} + +std::optional find_registered_command(const std::string& name){ + Registry& r = registry(); + LockGuard lg(r.lock); + auto iter = r.commands.find(name); + if (iter == r.commands.end()){ + return std::nullopt; + } + return iter->second.second; +} + +} + + + +void register_command_file_command(const std::string& name, const std::string& usage, CommandFileCommand command){ + Registry& r = registry(); + LockGuard lg(r.lock); + r.commands[name] = {usage, std::move(command)}; +} +void register_command_file_detector(const std::string& name, CommandFileDetectorFactory factory){ + Registry& r = registry(); + LockGuard lg(r.lock); + r.detectors[name] = std::move(factory); +} + + + +namespace{ + +// One line of a command file, split into words. +class CommandLine{ +public: + explicit CommandLine(const std::string& line){ + std::istringstream in(line); + std::string word; + while (in >> word && word[0] != '#'){ + m_words.push_back(word); + } + } + + bool empty() const{ return m_words.empty(); } + const std::string& name() const{ return m_words[0]; } + const std::vector& words() const{ return m_words; } + bool has(size_t index) const{ return index < m_words.size(); } + + const std::string& arg(size_t index) const{ + if (!has(index)){ + throw ParseException(name() + ": missing argument " + std::to_string(index)); + } + return m_words[index]; + } + double number(size_t index, double fallback) const{ + if (!has(index)){ + return fallback; + } + try{ + return std::stod(m_words[index]); + }catch (std::exception&){ + throw ParseException(name() + ": argument " + std::to_string(index) + " is not a number: " + m_words[index]); + } + } + Milliseconds duration(size_t index, Milliseconds fallback) const{ + return Milliseconds((int64_t)number(index, (double)fallback.count())); + } + +private: + std::vector m_words; +}; + + +Button parse_buttons(const std::string& text){ + static const std::map NAMES{ + {"A", BUTTON_A}, {"B", BUTTON_B}, {"X", BUTTON_X}, {"Y", BUTTON_Y}, + {"L", BUTTON_L}, {"R", BUTTON_R}, {"ZL", BUTTON_ZL}, {"ZR", BUTTON_ZR}, + {"PLUS", BUTTON_PLUS}, {"MINUS", BUTTON_MINUS}, + {"LCLICK", BUTTON_LCLICK}, {"RCLICK", BUTTON_RCLICK}, + {"HOME", BUTTON_HOME}, {"CAPTURE", BUTTON_CAPTURE}, + }; + Button buttons = BUTTON_NONE; + std::istringstream in(text); + std::string name; + while (std::getline(in, name, '+')){ + auto iter = NAMES.find(name); + if (iter == NAMES.end()){ + throw ParseException("Unknown button: " + name); + } + buttons = (Button)(buttons | iter->second); + } + return buttons; +} + +DpadPosition parse_dpad(const std::string& text){ + static const std::map NAMES{ + {"UP", DPAD_UP}, {"DOWN", DPAD_DOWN}, {"LEFT", DPAD_LEFT}, {"RIGHT", DPAD_RIGHT}, + }; + auto iter = NAMES.find(text); + if (iter == NAMES.end()){ + throw ParseException("Unknown D-pad direction: " + text); + } + return iter->second; +} + + + +// Runs the lines of one command file. +class LineRunner{ +public: + LineRunner(CommandFileSession& session, std::string name) + : m_session(session) + , m_name(std::move(name)) + {} + + // Runs one line. Returns false for "stop". Throws to fail the line. + bool run(const CommandLine& line){ + if (line.name() == "stop"){ + return false; + } + auto iter = builtins().find(line.name()); + if (iter != builtins().end()){ + (this->*iter->second.handler)(line); + return true; + } + std::optional command = find_registered_command(line.name()); + if (!command){ + throw ParseException("Unknown command: " + line.name() + " (try \"help\")"); + } + (*command)(m_session, line.words()); + return true; + } + + // Saves the current frame as /_