Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -46,7 +47,7 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 50 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=29 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
Expand All @@ -63,6 +64,7 @@
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -52,7 +53,7 @@
)
{}

std::vector<PanelEntry> PanelListFactory::make_panels() const{

Check warning on line 56 in SerialPrograms/Source/NintendoSwitch/NintendoSwitch_Panels.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PanelListFactory::make_panels() const: function_lines=48 (advisory above 25, limit 50)
std::vector<PanelEntry> ret;

ret.emplace_back("---- Settings ----");
Expand All @@ -78,6 +79,7 @@
ret.emplace_back("---- Testing ----");
ret.emplace_back(GameConsole::make_ConsolePanel<GameConsole::BoxDraw>());
ret.emplace_back(GameConsole::make_ConsoleProgram<GameConsole::SnapshotDumper>());
ret.emplace_back(make_SingleSwitchProgram<CommandFileRunner>());

if (STATIC_GLOBALS.DEVELOPER_MODE){
ret.emplace_back("---- Developer Tools ----");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* Command File Parsing
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <cerrno>
#include <cstdlib>
#include <map>
#include <sstream>
#include "Common/Cpp/Exceptions.h"
#include "NintendoSwitch_CommandFileParsing.h"

namespace PokemonAutomation{
namespace NintendoSwitch{


std::vector<std::string> split_command_line(const std::string& line){
std::vector<std::string> 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<std::string>& 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<std::string>& 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<std::string>& 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<std::string, Button> 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<std::string, DpadPosition> 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<Filesystem::Path> next_command_file(const std::string& inbox){
std::optional<Filesystem::Path> 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;
}



}
}
Original file line number Diff line number Diff line change
@@ -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 <optional>
#include <string>
#include <vector>
#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<std::string> 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<std::string>& 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<std::string>& 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<std::string>& 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<Filesystem::Path> next_command_file(const std::string& inbox);



}
}
#endif
Loading
Loading