|
6 | 6 |
|
7 | 7 | #include <map> |
8 | 8 | #include "Common/Cpp/Exceptions.h" |
| 9 | +#include "Common/Cpp/Json/JsonArray.h" |
| 10 | +#include "Common/Cpp/Json/JsonObject.h" |
9 | 11 | #include "ControllerTypeStrings.h" |
10 | 12 | #include "ControllerStateTable.h" |
11 | 13 |
|
12 | 14 | #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProControllerTable.h" |
13 | 15 | #include "NintendoSwitch/Controllers/Joycon/NintendoSwitch_JoyconTable.h" |
14 | 16 |
|
| 17 | +#include "NintendoSwitch/Options/TurboMacroTable.h" |
| 18 | + |
| 19 | +//#include <iostream> |
| 20 | +//using std::cout; |
| 21 | +//using std::endl; |
| 22 | + |
15 | 23 | namespace PokemonAutomation{ |
16 | 24 |
|
17 | 25 |
|
@@ -58,6 +66,219 @@ void ControllerCommandTable::register_controller_type( |
58 | 66 | } |
59 | 67 |
|
60 | 68 |
|
| 69 | + |
| 70 | + |
| 71 | +void ControllerCommandTable::load_json_NS_TurboMacro(const JsonValue& json){ |
| 72 | + using namespace NintendoSwitch; |
| 73 | + |
| 74 | + if (m_type != ControllerClass::NintendoSwitch_ProController){ |
| 75 | + throw FileException( |
| 76 | + nullptr, nullptr, |
| 77 | + "Incompatible controller type.", |
| 78 | + "" |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + JsonObject translated; |
| 83 | + translated["ControllerClass"] = CONTROLLER_CLASS_STRINGS().get_string(m_type); |
| 84 | + |
| 85 | + // JSON to JSON translate to the new format. |
| 86 | + JsonArray history; |
| 87 | + |
| 88 | + for (const JsonValue& item : json.to_array_throw()){ |
| 89 | + const JsonObject& obj = item.to_object_throw(); |
| 90 | + |
| 91 | + const std::string& action_name = obj.get_string_throw("Action"); |
| 92 | + const auto* entry = TurboMacroAction_Database().find_slug(action_name); |
| 93 | + if (entry == nullptr){ |
| 94 | + throw ParseException("Invalid Action: " + action_name); |
| 95 | + } |
| 96 | + |
| 97 | + const JsonValue* value; |
| 98 | + |
| 99 | + TurboMacroAction action = (TurboMacroAction)entry->enum_value; |
| 100 | + |
| 101 | + if (action == TurboMacroAction::NO_ACTION){ |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + // Wait has different timing fields. |
| 106 | + if (action == TurboMacroAction::WAIT){ |
| 107 | + value = obj.get_value("Wait"); |
| 108 | + if (value != nullptr && value->is_integer()){ |
| 109 | + JsonObject out; |
| 110 | + out["ms"] = std::to_string(value->to_integer_default() * 8) + " ms"; |
| 111 | + history.push_back(std::move(out)); |
| 112 | + } |
| 113 | + value = obj.get_value("WaitMs"); |
| 114 | + if (value != nullptr){ |
| 115 | + JsonObject out; |
| 116 | + out["ms"] = value->to_string_throw(); |
| 117 | + history.push_back(std::move(out)); |
| 118 | + } |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + JsonObject command; |
| 123 | + |
| 124 | + value = obj.get_value("Hold"); |
| 125 | + if (value != nullptr && value->is_integer()){ |
| 126 | + command["ms"] = std::to_string(value->to_integer_default() * 8) + " ms"; |
| 127 | + } |
| 128 | + value = obj.get_value("HoldMs"); |
| 129 | + if (value != nullptr){ |
| 130 | + command["ms"] = value->to_string_throw(); |
| 131 | + } |
| 132 | + if (value == nullptr){ |
| 133 | + throw ParseException("Missing Duration: " + obj.dump()); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + switch (action){ |
| 138 | + case TurboMacroAction::B: |
| 139 | + command["buttons"] = "B"; |
| 140 | + break; |
| 141 | + case TurboMacroAction::A: |
| 142 | + command["buttons"] = "A"; |
| 143 | + break; |
| 144 | + case TurboMacroAction::Y: |
| 145 | + command["buttons"] = "Y"; |
| 146 | + break; |
| 147 | + case TurboMacroAction::X: |
| 148 | + command["buttons"] = "X"; |
| 149 | + break; |
| 150 | + case TurboMacroAction::R: |
| 151 | + command["buttons"] = "R"; |
| 152 | + break; |
| 153 | + case TurboMacroAction::L: |
| 154 | + command["buttons"] = "L"; |
| 155 | + break; |
| 156 | + case TurboMacroAction::ZR: |
| 157 | + command["buttons"] = "ZR"; |
| 158 | + break; |
| 159 | + case TurboMacroAction::ZL: |
| 160 | + command["buttons"] = "ZL"; |
| 161 | + break; |
| 162 | + case TurboMacroAction::PLUS: |
| 163 | + command["buttons"] = "+"; |
| 164 | + break; |
| 165 | + case TurboMacroAction::MINUS: |
| 166 | + command["buttons"] = "-"; |
| 167 | + break; |
| 168 | + case TurboMacroAction::LEFT_JOY_CLICK: |
| 169 | + command["buttons"] = "LJ"; |
| 170 | + break; |
| 171 | + case TurboMacroAction::RIGHT_JOY_CLICK: |
| 172 | + command["buttons"] = "RJ"; |
| 173 | + break; |
| 174 | + case TurboMacroAction::DPADLEFT: |
| 175 | + command["dpad"] = "left"; |
| 176 | + break; |
| 177 | + case TurboMacroAction::DPADRIGHT: |
| 178 | + command["dpad"] = "right"; |
| 179 | + break; |
| 180 | + case TurboMacroAction::DPADUP: |
| 181 | + command["dpad"] = "up"; |
| 182 | + break; |
| 183 | + case TurboMacroAction::DPADDOWN: |
| 184 | + command["dpad"] = "down"; |
| 185 | + break; |
| 186 | + case TurboMacroAction::LEFT_JOYSTICK: |
| 187 | + value = obj.get_value("MoveDirectionX"); |
| 188 | + if (value != nullptr){ |
| 189 | + command["lx"] = value->to_integer_throw(); |
| 190 | + } |
| 191 | + value = obj.get_value("MoveDirectionY"); |
| 192 | + if (value != nullptr){ |
| 193 | + command["ly"] = value->to_integer_throw(); |
| 194 | + } |
| 195 | + break; |
| 196 | + case TurboMacroAction::RIGHT_JOYSTICK: |
| 197 | + value = obj.get_value("MoveDirectionX"); |
| 198 | + if (value != nullptr){ |
| 199 | + command["rx"] = value->to_integer_throw(); |
| 200 | + } |
| 201 | + value = obj.get_value("MoveDirectionY"); |
| 202 | + if (value != nullptr){ |
| 203 | + command["ry"] = value->to_integer_throw(); |
| 204 | + } |
| 205 | + break; |
| 206 | + default:; |
| 207 | + } |
| 208 | + |
| 209 | + history.push_back(std::move(command)); |
| 210 | + |
| 211 | + |
| 212 | + JsonObject cooldown; |
| 213 | + value = obj.get_value("Release"); |
| 214 | + if (value != nullptr && value->is_integer()){ |
| 215 | + cooldown["ms"] = std::to_string(value->to_integer_default() * 8) + " ms"; |
| 216 | + } |
| 217 | + value = obj.get_value("ReleaseMs"); |
| 218 | + if (value != nullptr){ |
| 219 | + cooldown["ms"] = value->to_string_throw(); |
| 220 | + } |
| 221 | + if (value != nullptr){ |
| 222 | + history.push_back(std::move(cooldown)); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + translated["Schedule"] = std::move(history); |
| 227 | + |
| 228 | +// cout << translated.dump() << endl; |
| 229 | + |
| 230 | + JsonValue value(std::move(translated)); |
| 231 | + load_json(value); |
| 232 | +} |
| 233 | + |
| 234 | +void ControllerCommandTable::load_json(const JsonValue& json){ |
| 235 | + clear(); |
| 236 | + |
| 237 | + if (json.is_array()){ |
| 238 | + load_json_NS_TurboMacro(json); |
| 239 | + return; |
| 240 | + } |
| 241 | + |
| 242 | + const JsonObject& obj = json.to_object_throw(); |
| 243 | + |
| 244 | + const JsonValue* value = obj.get_value("controller_class"); |
| 245 | + if (value == nullptr){ |
| 246 | + value = obj.get_value("ControllerClass"); |
| 247 | + } |
| 248 | + if (value == nullptr){ |
| 249 | + throw JsonParseException("", "ControllerClass"); |
| 250 | + } |
| 251 | + const std::string& controller = value->to_string_throw(); |
| 252 | + if (CONTROLLER_CLASS_STRINGS().get_enum(controller) != m_type){ |
| 253 | + throw FileException( |
| 254 | + nullptr, nullptr, |
| 255 | + "Incompatible controller type.", |
| 256 | + "" |
| 257 | + ); |
| 258 | + } |
| 259 | + |
| 260 | + value = obj.get_value("history"); |
| 261 | + if (value == nullptr){ |
| 262 | + value = obj.get_value("Schedule"); |
| 263 | + } |
| 264 | + if (value == nullptr){ |
| 265 | + throw JsonParseException("", "Schedule"); |
| 266 | + } |
| 267 | + |
| 268 | + EditableTableOption::load_json(*value); |
| 269 | +} |
| 270 | +JsonValue ControllerCommandTable::to_json() const{ |
| 271 | + JsonObject obj; |
| 272 | + obj["ControllerClass"] = CONTROLLER_CLASS_STRINGS().get_string(m_type); |
| 273 | + JsonArray history; |
| 274 | + run_on_all_rows<ControllerStateRow>([&](const ControllerStateRow& row){ |
| 275 | + history.push_back(row.to_json()); |
| 276 | + return false; |
| 277 | + }); |
| 278 | + obj["Schedule"] = std::move(history); |
| 279 | + return obj; |
| 280 | +} |
| 281 | + |
61 | 282 | std::vector<std::string> ControllerCommandTable::make_header() const{ |
62 | 283 | auto& map = get_controller_map(); |
63 | 284 | auto iter = map.find(m_type); |
|
0 commit comments