44 *
55 */
66
7+ #include < QFile>
78#include " Common/Cpp/Json/JsonArray.h"
89#include " NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
910#include " NintendoSwitch/Controllers/NintendoSwitch_ProController.h"
@@ -47,7 +48,7 @@ RecordKeyboardController::RecordKeyboardController()
4748 LockMode::LOCK_WHILE_RUNNING,
4849 Mode::RECORD
4950 )
50- , JSON_FILE_NAME (
51+ , FILE_NAME (
5152 false ,
5253 " Name of the JSON file to read/write." ,
5354 LockMode::LOCK_WHILE_RUNNING,
@@ -56,7 +57,7 @@ RecordKeyboardController::RecordKeyboardController()
5657 )
5758{
5859 PA_ADD_OPTION (MODE);
59- PA_ADD_OPTION (JSON_FILE_NAME );
60+ PA_ADD_OPTION (FILE_NAME );
6061}
6162
6263
@@ -66,39 +67,45 @@ void RecordKeyboardController::program(SingleSwitchProgramEnvironment& env, Canc
6667 ControllerCategory controller_category = env.console .controller ().controller_category ();
6768
6869 if (MODE == Mode::RECORD){
70+ // check if given file name already exists. If it does, throw an error so we don't overwrite it.
71+ std::string output_json_filename = std::string (FILE_NAME) + " .json" ;
72+ QFile file (QString::fromStdString (output_json_filename));
73+ if (file.open (QFile::ReadOnly)){
74+ throw FileException (nullptr , PA_CURRENT_FUNCTION, " Given file name already exists. Choose a different file name." , output_json_filename);
75+ }
76+
6977 context.controller ().add_keyboard_listener (*this );
7078
7179 try {
7280 context.wait_until_cancel ();
7381 }catch (ProgramCancelledException&){
7482
75- if (MODE == Mode::RECORD){
76- JsonValue json = controller_history_to_json (env.console .logger (), controller_category);
77- json.dump (std::string (JSON_FILE_NAME) + " .json" );
78- m_controller_history.clear ();
83+ JsonValue json = controller_history_to_json (env.console .logger (), controller_category);
84+ json.dump (output_json_filename);
85+ m_controller_history.clear ();
7986
80- json_to_cpp_code (env.console .logger (),json);
87+ json_to_cpp_code (env.console .logger (), json, FILE_NAME );
8188
82- context.controller ().remove_keyboard_listener (*this );
83- }
89+ context.controller ().remove_keyboard_listener (*this );
90+
8491 throw ;
8592 }
8693
8794 }else if (MODE == Mode::REPLAY){
88- JsonValue json = load_json_file (std::string (JSON_FILE_NAME ) + " .json" );
95+ JsonValue json = load_json_file (std::string (FILE_NAME ) + " .json" );
8996 json_to_pbf_actions (env, scope, json, controller_category);
9097
9198
9299 }else if (MODE == Mode::CONVERT_JSON_TO_CODE){
93- JsonValue json = load_json_file (std::string (JSON_FILE_NAME ) + " .json" );
94- json_to_cpp_code (env.console .logger (), json);
100+ JsonValue json = load_json_file (std::string (FILE_NAME ) + " .json" );
101+ json_to_cpp_code (env.console .logger (), json, FILE_NAME );
95102
96103
97104 }
98105
99106}
100107
101- std::string json_to_cpp_code (Logger& logger, const JsonValue& json){
108+ void json_to_cpp_code (Logger& logger, const JsonValue& json, const std::string& output_file_name ){
102109 try {
103110 const JsonObject& obj = json.to_object_throw ();
104111
@@ -108,15 +115,21 @@ std::string json_to_cpp_code(Logger& logger, const JsonValue& json){
108115
109116 const JsonArray& history_json = obj.get_array_throw (" history" );
110117
118+ std::string output_text;
111119 switch (controller_category){
112120 case ControllerCategory::PRO_CONTROLLER:
113- return json_to_cpp_code_pro_controller (history_json);
121+ output_text = json_to_cpp_code_pro_controller (history_json);
122+ break ;
114123 case ControllerCategory::LEFT_JOYCON:
115124 case ControllerCategory::RIGHT_JOYCON:
116- return json_to_cpp_code_joycon (history_json);
125+ output_text = json_to_cpp_code_joycon (history_json);
126+ break ;
117127 }
118128
119- return " " ;
129+ QFile file (QString::fromStdString (output_file_name + " .txt" ));
130+ file.open (QIODevice::WriteOnly);
131+ file.write (output_text.c_str (), output_text.size ());
132+
120133 }catch (ParseException& e){
121134 logger.log (e.message () + " \n JSON parsing error. Given JSON file doesn't match the expected format." , COLOR_RED);
122135 throw ParseException (e.message () + " \n JSON parsing error. Given JSON file doesn't match the expected format." );
@@ -430,7 +443,7 @@ void json_to_pro_controller_state(
430443 right_x > STICK_MAX || right_x < STICK_MIN ||
431444 right_y > STICK_MAX || right_y < STICK_MIN)
432445 {
433- throw ParseException ();
446+ throw ParseException (" x or y values are outside of 0-255. " );
434447 }
435448
436449 NonNeutralControllerField non_neutral_field = get_non_neutral_pro_controller_field (button, dpad, left_x, left_y, right_x, right_y);
@@ -472,7 +485,7 @@ void json_to_joycon_state(
472485 if (x > STICK_MAX || x < STICK_MIN ||
473486 y > STICK_MAX || y < STICK_MIN)
474487 {
475- throw ParseException ();
488+ throw ParseException (" x or y values are outside of 0-255. " );
476489 }
477490
478491 NonNeutralControllerField non_neutral_field = get_non_neutral_joycon_controller_field (button, x, y);
0 commit comments