From 7d6712e6b60cbe5160a40bfa8ade10620207f800 Mon Sep 17 00:00:00 2001 From: Di Fan <26423504+dayfine@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:01:58 -0700 Subject: [PATCH 01/79] Lab control folder: start/stop the selected program from scripts (#76) When /LabControl/ exists, the main window polls it every 500 ms. A "start" or "stop" file does what clicking Start/Stop Program does and is then deleted; "state" holds the program state and the selected program's identifier. Nothing changes when the folder is absent. The button's handler moves into start_or_stop_program() so both paths share it. Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E Co-authored-by: Claude Opus 5.5 (cherry picked from commit f6f658f437b2c6e204c57be116d3e3a69a83cb4d) --- .../Panels/UI/PanelElements.cpp | 29 +-- .../CommonFramework/Panels/UI/PanelElements.h | 4 + .../CommonFramework/Windows/LabControl.cpp | 182 ++++++++++++++++++ .../CommonFramework/Windows/LabControl.h | 60 ++++++ .../CommonFramework/Windows/MainWindow.cpp | 11 ++ .../CommonFramework/Windows/MainWindow.h | 3 + .../ComputerPrograms_UnitTestRunner.cpp | 2 + SerialPrograms/cmake/SourceFiles.cmake | 2 + 8 files changed, 280 insertions(+), 13 deletions(-) create mode 100644 SerialPrograms/Source/CommonFramework/Windows/LabControl.cpp create mode 100644 SerialPrograms/Source/CommonFramework/Windows/LabControl.h diff --git a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.cpp b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.cpp index a61d84b49c..8b8d32b8e7 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.cpp +++ b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.cpp @@ -247,19 +247,7 @@ RunnablePanelActionBar::RunnablePanelActionBar( connect( m_start_button, &QPushButton::clicked, this, [&](bool){ - std::string error; - switch (program_session.current_state()){ - case ProgramState::STOPPED: - error = program_session.start_program(); - break; - case ProgramState::RUNNING: - error = program_session.stop_program(); - break; - default:; - } - if (!error.empty()){ - program_session.report_error(error); - } + start_or_stop_program(program_session); } ); connect( @@ -277,6 +265,21 @@ RunnablePanelActionBar::RunnablePanelActionBar( } ); } +void start_or_stop_program(ProgramSession& program_session){ + std::string error; + switch (program_session.current_state()){ + case ProgramState::STOPPED: + error = program_session.start_program(); + break; + case ProgramState::RUNNING: + error = program_session.stop_program(); + break; + default:; + } + if (!error.empty()){ + program_session.report_error(error); + } +} void RunnablePanelActionBar::set_state(ProgramState state){ m_last_known_state = state; switch (state){ diff --git a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.h b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.h index 550417e0e8..553cc4fed0 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.h +++ b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.h @@ -59,6 +59,10 @@ class StatsBar : public QLabel{ +// What clicking Start/Stop Program does: start the program if it is stopped, +// stop it if it is running, and report any error to the program's listeners. +void start_or_stop_program(ProgramSession& program_session); + class RunnablePanelActionBar : public QGroupBox{ public: RunnablePanelActionBar( diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabControl.cpp b/SerialPrograms/Source/CommonFramework/Windows/LabControl.cpp new file mode 100644 index 0000000000..718971d87e --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Windows/LabControl.cpp @@ -0,0 +1,182 @@ +/* Lab Control + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/Logging/Logger.h" +#include "CommonFramework/ProgramSession.h" +#include "CommonFramework/Panels/UI/PanelElements.h" +#include "LabControl.h" + +namespace PokemonAutomation{ + + + +LabControlAction parse_lab_control_file(const std::string& filename){ + if (filename == "start"){ + return LabControlAction::START; + } + if (filename == "stop"){ + return LabControlAction::STOP; + } + return LabControlAction::NONE; +} + +std::string lab_control_state_text( + const std::string& state, + const std::string& program, + const std::string& error +){ + std::string text = "state=" + state + "\nprogram=" + program + "\n"; + if (!error.empty()){ + text += "error=" + error + "\n"; + } + return text; +} + +std::string program_state_name(ProgramState state){ + switch (state){ + case ProgramState::NOT_READY: + return "NOT_READY"; + case ProgramState::STOPPED: + return "STOPPED"; + case ProgramState::RUNNING: + return "RUNNING"; + case ProgramState::STOPPING: + return "STOPPING"; + } + return "UNKNOWN"; +} + + +namespace{ + +// Returns an error message, or empty on success. +std::string run_action(ProgramSession* program, LabControlAction action){ + if (program == nullptr){ + return "The selected panel is not a program."; + } + ProgramState state = program->current_state(); + ProgramState from = action == LabControlAction::START + ? ProgramState::STOPPED + : ProgramState::RUNNING; + if (state != from){ + return "Program is " + program_state_name(state) + "."; + } + start_or_stop_program(*program); + return ""; +} + +void write_state_file(const Filesystem::Path& path, const std::string& text){ + Filesystem::Path temp = Filesystem::unique_temp_path(path); + { + FileIO file; + if (!file.open(temp, FileMode::WRITE | FileMode::BINARY)){ + return; + } + file.write(text); + } + std::error_code ec; + Filesystem::rename(temp, path, ec); +} + +} + + +void poll_lab_control(PanelSession* panel, LabControlStatus& status){ + const Filesystem::Path folder(USER_FILE_PATH() + "LabControl"); + if (!Filesystem::is_directory(folder)){ + status.written_state.clear(); + return; + } + + ProgramSession* program = dynamic_cast(panel); + try{ + for (const Filesystem::Path& path : Filesystem::list_directory(folder)){ + LabControlAction action = parse_lab_control_file(path.filename().string()); + if (action == LabControlAction::NONE){ + continue; + } + Filesystem::remove(path); + status.last_error = run_action(program, action); + global_logger_tagged().log( + "LabControl: " + path.filename().string() + + (status.last_error.empty() ? "" : " ignored: " + status.last_error) + ); + } + + std::string text = lab_control_state_text( + program == nullptr ? "NO_PROGRAM" : program_state_name(program->current_state()), + panel == nullptr ? "" : panel->descriptor().identifier(), + status.last_error + ); + const Filesystem::Path state_path = folder / "state"; + if (text != status.written_state || !Filesystem::exists(state_path)){ + write_state_file(state_path, text); + status.written_state = std::move(text); + } + }catch (const std::exception& e){ + global_logger_tagged().log(std::string("LabControl: ") + e.what(), COLOR_RED); + } +} + + + +class Test_LabControl_ParseFile : public UnitTest{ +public: + Test_LabControl_ParseFile() + : UnitTest("CommonFramework::LabControl - parse_lab_control_file()") + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + if (parse_lab_control_file("start") != LabControlAction::START){ + return "\"start\" should start."; + } + if (parse_lab_control_file("stop") != LabControlAction::STOP){ + return "\"stop\" should stop."; + } + // Our own output, scripts' partial writes and anything else are ignored. + for (const char* name : {"state", "start.part", "stop.part", "Start", "start.txt", ""}){ + if (parse_lab_control_file(name) != LabControlAction::NONE){ + return std::string("\"") + name + "\" should be ignored."; + } + } + return true; + } +}; + +class Test_LabControl_StateText : public UnitTest{ +public: + Test_LabControl_StateText() + : UnitTest("CommonFramework::LabControl - lab_control_state_text()") + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::string text = lab_control_state_text( + program_state_name(ProgramState::RUNNING), "PokemonBDSP:AutoStory", "" + ); + if (text != "state=RUNNING\nprogram=PokemonBDSP:AutoStory\n"){ + return "Unexpected state text: " + text; + } + text = lab_control_state_text("NO_PROGRAM", "", "The selected panel is not a program."); + if (text != "state=NO_PROGRAM\nprogram=\nerror=The selected panel is not a program.\n"){ + return "Unexpected state text: " + text; + } + return true; + } +}; + +void add_tests_LabControl(UnitTestDatabase& database){ + database.add(); + database.add(); +} + + + +} diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabControl.h b/SerialPrograms/Source/CommonFramework/Windows/LabControl.h new file mode 100644 index 0000000000..07460d9bf6 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Windows/LabControl.h @@ -0,0 +1,60 @@ +/* Lab Control + * + * From: https://github.com/PokemonAutomation/ + * + * Lets test scripts start and stop the selected program without clicking + * the window. Only active when the folder "/LabControl/" exists, + * so normal users see no change. + * + * A file named "start" or "stop" in that folder does what clicking + * Start/Stop Program does, and is then deleted. The file "state" holds the + * current program state and the selected program's identifier. + * + */ + +#ifndef PokemonAutomation_LabControl_H +#define PokemonAutomation_LabControl_H + +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/Globals.h" +#include "CommonFramework/Panels/PanelSession.h" + +namespace PokemonAutomation{ + + +enum class LabControlAction{ + NONE, + START, + STOP, +}; + +// The action requested by a file of this name in the control folder. +LabControlAction parse_lab_control_file(const std::string& filename); + +// Contents of the "state" file. "state" is NO_PROGRAM when the selected panel +// is not a program. "error" is only written after a failed start/stop. +std::string lab_control_state_text( + const std::string& state, + const std::string& program, + const std::string& error +); + +std::string program_state_name(ProgramState state); + + +struct LabControlStatus{ + std::string written_state; // Last contents written to "state". + std::string last_error; // Error from the last start/stop, if any. +}; + +// Act on any control files and refresh "state". Call from the UI thread. +// "panel" is the selected panel, or nullptr if there is none. +void poll_lab_control(PanelSession* panel, LabControlStatus& status); + +void add_tests_LabControl(UnitTestDatabase& database); + + + +} +#endif diff --git a/SerialPrograms/Source/CommonFramework/Windows/MainWindow.cpp b/SerialPrograms/Source/CommonFramework/Windows/MainWindow.cpp index c592430897..637cf96130 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/MainWindow.cpp +++ b/SerialPrograms/Source/CommonFramework/Windows/MainWindow.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "Common/Cpp/ScopeExit.h" #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Logging/MultiOutputLogger.h" @@ -288,6 +289,16 @@ MainWindow::MainWindow(QWidget* parent) GlobalSettings::instance().WALLPAPER.add_listener(*this); SystemSleepController::instance().add_listener(*this); update_wallpaper(); + + // Let lab scripts start/stop the selected program. See LabControl.h. + QTimer* lab_control_timer = new QTimer(this); + connect( + lab_control_timer, &QTimer::timeout, + this, [this]{ + poll_lab_control(m_current_panel.get(), m_lab_control_status); + } + ); + lab_control_timer->start(500); // cout << "Done constructing" << endl; } MainWindow::~MainWindow(){ diff --git a/SerialPrograms/Source/CommonFramework/Windows/MainWindow.h b/SerialPrograms/Source/CommonFramework/Windows/MainWindow.h index 8ee1a436a9..b4de2632e3 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/MainWindow.h +++ b/SerialPrograms/Source/CommonFramework/Windows/MainWindow.h @@ -13,6 +13,7 @@ #include "CommonFramework/Environment/SystemSleep.h" #include "CommonFramework/Panels/PanelTools.h" #include "CommonFramework/Panels/PanelSession.h" +#include "LabControl.h" class QVBoxLayout; class QLabel; @@ -91,6 +92,8 @@ class MainWindow bool m_pending_resize = false; bool m_pending_move = false; bool m_panel_transition = false; + + LabControlStatus m_lab_control_status; }; // returns given X value, but bounded by 0 and screen_width diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 1c19573466..61b81d1059 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -19,6 +19,7 @@ #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ProgramStats/StatsTracking.h" #include "CommonFramework/Tools/GlobalThreadPools.h" +#include "CommonFramework/Windows/LabControl.h" #include "CommonTools/VisualDetectors/BlackBorderDetector.h" #include "CommonTools/OCR/OCR_Tests.h" @@ -56,6 +57,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_FFTStreamer(ret); add_tests_SinkPipeline(ret); add_tests_AudioFileLoader(ret); + add_tests_LabControl(ret); OCR::add_tests(ret); Kernels::add_tests(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 9e4e0895bb..28f811ded0 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -766,6 +766,8 @@ file(GLOB LIBRARY_SOURCES Source/CommonFramework/Windows/ButtonDiagram.h Source/CommonFramework/Windows/DpiScaler.cpp Source/CommonFramework/Windows/DpiScaler.h + Source/CommonFramework/Windows/LabControl.cpp + Source/CommonFramework/Windows/LabControl.h Source/CommonFramework/Windows/MainWindow.cpp Source/CommonFramework/Windows/MainWindow.h Source/CommonFramework/Windows/WindowTracker.cpp From b30fb26e59610bf378d3bb8e8c36dac982e6b8ae Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 31 Oct 2026 21:10:02 -0700 Subject: [PATCH 02/79] Lab control folder: a README for the files and their protocol The folder, the start/stop files, the state file's lines and when the error line appears were described only in LabControl.h's header and in the lab scripts that use it. This says it next to the code, with the shell sequence a script uses. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E (cherry picked from commit 953ae6887596fea0d0d7aa62d752010d7de0c788) --- .../CommonFramework/Windows/LabControl.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 SerialPrograms/Source/CommonFramework/Windows/LabControl.md diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabControl.md b/SerialPrograms/Source/CommonFramework/Windows/LabControl.md new file mode 100644 index 0000000000..9111a36cb8 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Windows/LabControl.md @@ -0,0 +1,64 @@ +# Lab Control + +Lets a script start and stop the selected program of a running SerialPrograms +app, and read its state, through files in a folder: no window focus, no +click. Meant for a lab that drives one app per console from the shell. + +## Turning it on + +Create the folder `LabControl/` inside the profile's user-files folder (the +folder that holds `SerialPrograms.log`, `Screenshots/` and the settings). +While it exists the app polls it every 500 ms from the UI thread. Without it +the app behaves as before: normal users see no change. + +## Files + +| File | Direction | Meaning | +|---|---|---| +| `start` | script writes | Does what clicking Start Program does for the selected panel. The file is deleted once acted on. | +| `stop` | script writes | Does what clicking Stop Program does. Deleted once acted on. | +| `state` | app writes | The current state, rewritten whenever it changes (see below). | + +Write `start` and `stop` atomically (write `start.part`, then rename it to +`start`): only the exact names `start` and `stop` act, so a half-written +file is ignored. + +`state` holds two or three lines: + +``` +state=STOPPED +program=PokemonBDSP:AutoStory +error=The selected panel is not a program. +``` + +- `state` is `STOPPED`, `RUNNING` or `STOPPING` for a program, `NOT_READY` + while the panel loads, `NO_PROGRAM` when the selected panel is not a + program. +- `program` is the selected program's identifier (`:`), empty + for `NO_PROGRAM`. +- `error` appears only after a refused or failed `start`/`stop` and names the + reason; it clears on the next successful action. + +The selected panel is the one the window shows; Lab Control does not select +programs. A script selects a program by launching the app with a profile +whose settings remember that panel, or by its own means, then reads `program` +to confirm before writing `start`. + +## From a script + +```sh +P="$HOME/Library/Application Support/SerialPrograms/Profiles/lab-B" # the profile +mkdir -p "$P/LabControl" +sed -n 's/^program=//p' "$P/LabControl/state" # which program is selected +printf '' > "$P/LabControl/start.part" && mv "$P/LabControl/start.part" "$P/LabControl/start" +until grep -q '^state=RUNNING' "$P/LabControl/state"; do sleep 0.5; done +``` + +Read `state` to learn what the app is doing; never stop a program to find out +which one is selected. A `stop` while a program is `STOPPING` is ignored. + +## Tests + +`add_tests_LabControl()` covers the file-name parsing and the `state` file's +text (`LabControl: ...` unit tests); the unit test runner reads the same folder +when it runs under a lab profile. From bc834a8af096a9028d311c66b3dd4ee642b9f8b5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 31 Oct 2026 21:12:39 -0700 Subject: [PATCH 03/79] Command File Runner: a developer program driven by text command files Answering "does this detector fire on that screen?" or "how far does a hold move?" meant a rebuild and a full program run. The Command File Runner runs .txt files dropped into /inbox against the live console, one command per line (press, dpad, stick, wait, walk until the screen holds still, shot, shots, history, 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. Games can register their own commands and detectors. README.md next to the code documents the file format and every command; unit tests cover the parsing. Squashed from fork lab/main (fork PRs #58, #84, #92). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../NintendoSwitch/NintendoSwitch_Panels.cpp | 2 + .../NintendoSwitch_CommandFileParsing.cpp | 105 ++++ .../NintendoSwitch_CommandFileParsing.h | 54 ++ .../NintendoSwitch_CommandFileRunner.cpp | 540 ++++++++++++++++++ .../NintendoSwitch_CommandFileRunner.h | 98 ++++ ...NintendoSwitch_CommandFileRunner_Tests.cpp | 172 ++++++ .../NintendoSwitch_CommandFileRunner_Tests.h | 22 + .../Programs/CommandFileRunner/README.md | 126 ++++ SerialPrograms/cmake/SourceFiles.cmake | 6 + 10 files changed, 1127 insertions(+) create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileParsing.cpp create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileParsing.h 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/NintendoSwitch_CommandFileRunner_Tests.cpp create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner_Tests.h create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/README.md diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 61b81d1059..e1c2c26d55 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -26,6 +26,7 @@ #include "NintendoSwitch/Inference/NintendoSwitch_CheckOnlineDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_FailedToConnectDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" +#include "NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner_Tests.h" #include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h" #include "VideoGames/PokemonHome/PokemonHome_Tests.h" @@ -63,6 +64,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); + NintendoSwitch::add_tests_CommandFileRunner(ret); NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret); NintendoSwitch::PokemonFRLG::add_tests(ret); NintendoSwitch::PokemonRSE::add_tests(ret); diff --git a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_Panels.cpp b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_Panels.cpp index 2979a83f4b..16ff56933b 100644 --- a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_Panels.cpp +++ b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_Panels.cpp @@ -21,6 +21,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_CommandFileParsing.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileParsing.cpp new file mode 100644 index 0000000000..8039699fe0 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileParsing.cpp @@ -0,0 +1,105 @@ +/* Command File Parsing + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "NintendoSwitch_CommandFileParsing.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +std::vector split_command_line(const std::string& line){ + std::vector words; + std::istringstream in(line); + std::string word; + while (in >> word && word[0] != '#'){ + words.push_back(word); + } + return words; +} + +const std::string& required_arg(const std::vector& words, size_t index){ + if (index >= words.size()){ + throw ParseException(words[0] + ": missing argument " + std::to_string(index)); + } + return words[index]; +} + +double optional_number(const std::vector& words, size_t index, double fallback){ + if (index >= words.size()){ + return fallback; + } + // strtod() rather than std::stod(): on the macOS Intel CI build, the + // std::invalid_argument that std::stod() throws is not caught as a + // std::exception. + const char* begin = words[index].c_str(); + char* end = nullptr; + errno = 0; + const double value = std::strtod(begin, &end); + if (end == begin || errno == ERANGE){ + throw ParseException(words[0] + ": argument " + std::to_string(index) + " is not a number: " + words[index]); + } + return value; +} + +Milliseconds optional_duration(const std::vector& words, size_t index, Milliseconds fallback){ + return Milliseconds((int64_t)optional_number(words, index, (double)fallback.count())); +} + + +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; +} + +std::optional next_command_file(const std::string& inbox){ + std::optional next; + for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(inbox))){ + if (!Filesystem::is_regular_file(path) || path.extension().string() != ".txt"){ + continue; + } + if (!next || path.string() < next->string()){ + next = path; + } + } + return next; +} + + + +} +} diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileParsing.h b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileParsing.h new file mode 100644 index 0000000000..28ca34fd76 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileParsing.h @@ -0,0 +1,54 @@ +/* Command File Parsing + * + * From: https://github.com/PokemonAutomation/ + * + * Reading command files for the Command File Runner: the words of a line, + * button and D-pad names, and which inbox file runs next. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_CommandFileParsing_H +#define PokemonAutomation_NintendoSwitch_CommandFileParsing_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// The words of one line of a command file. A word starting with '#' starts +// a comment that runs to the end of the line. Word 0 is the command name. +std::vector split_command_line(const std::string& line); + +// Word `index` of a command line. Throws ParseException if it is missing. +const std::string& required_arg(const std::vector& words, size_t index); + +// Word `index` as a number, or `fallback` if it is missing. Throws +// ParseException if it is not a number. +double optional_number(const std::vector& words, size_t index, double fallback); + +// Word `index` as a number of milliseconds, or `fallback` if it is missing. +// Throws ParseException if it is not a number. +Milliseconds optional_duration(const std::vector& words, size_t index, Milliseconds fallback); + + +// A button name, or several joined with '+' (e.g. "L+R"). Throws +// ParseException for an unknown name. +Button parse_buttons(const std::string& text); + +// "UP", "DOWN", "LEFT" or "RIGHT". Throws ParseException otherwise. +DpadPosition parse_dpad(const std::string& text); + +// The ".txt" file in `inbox` whose name sorts first, if any. +std::optional next_command_file(const std::string& inbox); + + + +} +} +#endif 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..8ad10e6d07 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp @@ -0,0 +1,540 @@ +/* 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/ImageTools/ImageDiff.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_CommandFileParsing.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; + +// "walk": the stick is held in slices this long, and the frames between +// slices are compared (RMSD at 192x108). Two slices in a row under the +// threshold count as standing still against something. +constexpr Milliseconds WALK_SLICE = 200ms; +constexpr Milliseconds WALK_LIMIT = 5000ms; +constexpr double WALK_STILL_RMSD = 10; + +// 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{ + +// 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 std::vector& words){ + if (words[0] == "stop"){ + return false; + } + auto iter = builtins().find(words[0]); + if (iter != builtins().end()){ + (this->*iter->second.handler)(words); + return true; + } + std::optional command = find_registered_command(words[0]); + if (!command){ + throw ParseException("Unknown command: " + words[0] + " (try \"help\")"); + } + (*command)(m_session, words); + return true; + } + + // Saves the current frame as /_