diff options
Diffstat (limited to 'src/input_common/tas/tas_input.cpp')
| -rw-r--r-- | src/input_common/tas/tas_input.cpp | 455 |
1 files changed, 0 insertions, 455 deletions
diff --git a/src/input_common/tas/tas_input.cpp b/src/input_common/tas/tas_input.cpp deleted file mode 100644 index 1598092b6..000000000 --- a/src/input_common/tas/tas_input.cpp +++ /dev/null | |||
| @@ -1,455 +0,0 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2+ | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cstring> | ||
| 6 | #include <regex> | ||
| 7 | |||
| 8 | #include "common/fs/file.h" | ||
| 9 | #include "common/fs/fs_types.h" | ||
| 10 | #include "common/fs/path_util.h" | ||
| 11 | #include "common/logging/log.h" | ||
| 12 | #include "common/settings.h" | ||
| 13 | #include "input_common/tas/tas_input.h" | ||
| 14 | |||
| 15 | namespace TasInput { | ||
| 16 | |||
| 17 | // Supported keywords and buttons from a TAS file | ||
| 18 | constexpr std::array<std::pair<std::string_view, TasButton>, 20> text_to_tas_button = { | ||
| 19 | std::pair{"KEY_A", TasButton::BUTTON_A}, | ||
| 20 | {"KEY_B", TasButton::BUTTON_B}, | ||
| 21 | {"KEY_X", TasButton::BUTTON_X}, | ||
| 22 | {"KEY_Y", TasButton::BUTTON_Y}, | ||
| 23 | {"KEY_LSTICK", TasButton::STICK_L}, | ||
| 24 | {"KEY_RSTICK", TasButton::STICK_R}, | ||
| 25 | {"KEY_L", TasButton::TRIGGER_L}, | ||
| 26 | {"KEY_R", TasButton::TRIGGER_R}, | ||
| 27 | {"KEY_PLUS", TasButton::BUTTON_PLUS}, | ||
| 28 | {"KEY_MINUS", TasButton::BUTTON_MINUS}, | ||
| 29 | {"KEY_DLEFT", TasButton::BUTTON_LEFT}, | ||
| 30 | {"KEY_DUP", TasButton::BUTTON_UP}, | ||
| 31 | {"KEY_DRIGHT", TasButton::BUTTON_RIGHT}, | ||
| 32 | {"KEY_DDOWN", TasButton::BUTTON_DOWN}, | ||
| 33 | {"KEY_SL", TasButton::BUTTON_SL}, | ||
| 34 | {"KEY_SR", TasButton::BUTTON_SR}, | ||
| 35 | {"KEY_CAPTURE", TasButton::BUTTON_CAPTURE}, | ||
| 36 | {"KEY_HOME", TasButton::BUTTON_HOME}, | ||
| 37 | {"KEY_ZL", TasButton::TRIGGER_ZL}, | ||
| 38 | {"KEY_ZR", TasButton::TRIGGER_ZR}, | ||
| 39 | }; | ||
| 40 | |||
| 41 | Tas::Tas() { | ||
| 42 | if (!Settings::values.tas_enable) { | ||
| 43 | needs_reset = true; | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | LoadTasFiles(); | ||
| 47 | } | ||
| 48 | |||
| 49 | Tas::~Tas() { | ||
| 50 | Stop(); | ||
| 51 | }; | ||
| 52 | |||
| 53 | void Tas::LoadTasFiles() { | ||
| 54 | script_length = 0; | ||
| 55 | for (size_t i = 0; i < commands.size(); i++) { | ||
| 56 | LoadTasFile(i); | ||
| 57 | if (commands[i].size() > script_length) { | ||
| 58 | script_length = commands[i].size(); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | void Tas::LoadTasFile(size_t player_index) { | ||
| 64 | if (!commands[player_index].empty()) { | ||
| 65 | commands[player_index].clear(); | ||
| 66 | } | ||
| 67 | std::string file = | ||
| 68 | Common::FS::ReadStringFromFile(Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) / | ||
| 69 | fmt::format("script0-{}.txt", player_index + 1), | ||
| 70 | Common::FS::FileType::BinaryFile); | ||
| 71 | std::stringstream command_line(file); | ||
| 72 | std::string line; | ||
| 73 | int frame_no = 0; | ||
| 74 | while (std::getline(command_line, line, '\n')) { | ||
| 75 | if (line.empty()) { | ||
| 76 | continue; | ||
| 77 | } | ||
| 78 | LOG_DEBUG(Input, "Loading line: {}", line); | ||
| 79 | std::smatch m; | ||
| 80 | |||
| 81 | std::stringstream linestream(line); | ||
| 82 | std::string segment; | ||
| 83 | std::vector<std::string> seglist; | ||
| 84 | |||
| 85 | while (std::getline(linestream, segment, ' ')) { | ||
| 86 | seglist.push_back(segment); | ||
| 87 | } | ||
| 88 | |||
| 89 | if (seglist.size() < 4) { | ||
| 90 | continue; | ||
| 91 | } | ||
| 92 | |||
| 93 | while (frame_no < std::stoi(seglist.at(0))) { | ||
| 94 | commands[player_index].push_back({}); | ||
| 95 | frame_no++; | ||
| 96 | } | ||
| 97 | |||
| 98 | TASCommand command = { | ||
| 99 | .buttons = ReadCommandButtons(seglist.at(1)), | ||
| 100 | .l_axis = ReadCommandAxis(seglist.at(2)), | ||
| 101 | .r_axis = ReadCommandAxis(seglist.at(3)), | ||
| 102 | }; | ||
| 103 | commands[player_index].push_back(command); | ||
| 104 | frame_no++; | ||
| 105 | } | ||
| 106 | LOG_INFO(Input, "TAS file loaded! {} frames", frame_no); | ||
| 107 | } | ||
| 108 | |||
| 109 | void Tas::WriteTasFile(std::u8string file_name) { | ||
| 110 | std::string output_text; | ||
| 111 | for (size_t frame = 0; frame < record_commands.size(); frame++) { | ||
| 112 | if (!output_text.empty()) { | ||
| 113 | output_text += "\n"; | ||
| 114 | } | ||
| 115 | const TASCommand& line = record_commands[frame]; | ||
| 116 | output_text += std::to_string(frame) + " " + WriteCommandButtons(line.buttons) + " " + | ||
| 117 | WriteCommandAxis(line.l_axis) + " " + WriteCommandAxis(line.r_axis); | ||
| 118 | } | ||
| 119 | const auto bytes_written = Common::FS::WriteStringToFile( | ||
| 120 | Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) / file_name, | ||
| 121 | Common::FS::FileType::TextFile, output_text); | ||
| 122 | if (bytes_written == output_text.size()) { | ||
| 123 | LOG_INFO(Input, "TAS file written to file!"); | ||
| 124 | } else { | ||
| 125 | LOG_ERROR(Input, "Writing the TAS-file has failed! {} / {} bytes written", bytes_written, | ||
| 126 | output_text.size()); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | std::pair<float, float> Tas::FlipAxisY(std::pair<float, float> old) { | ||
| 131 | auto [x, y] = old; | ||
| 132 | return {x, -y}; | ||
| 133 | } | ||
| 134 | |||
| 135 | void Tas::RecordInput(u32 buttons, const std::array<std::pair<float, float>, 2>& axes) { | ||
| 136 | last_input = {buttons, FlipAxisY(axes[0]), FlipAxisY(axes[1])}; | ||
| 137 | } | ||
| 138 | |||
| 139 | std::tuple<TasState, size_t, size_t> Tas::GetStatus() const { | ||
| 140 | TasState state; | ||
| 141 | if (is_recording) { | ||
| 142 | return {TasState::Recording, 0, record_commands.size()}; | ||
| 143 | } | ||
| 144 | |||
| 145 | if (is_running) { | ||
| 146 | state = TasState::Running; | ||
| 147 | } else { | ||
| 148 | state = TasState::Stopped; | ||
| 149 | } | ||
| 150 | |||
| 151 | return {state, current_command, script_length}; | ||
| 152 | } | ||
| 153 | |||
| 154 | std::string Tas::DebugButtons(u32 buttons) const { | ||
| 155 | return fmt::format("{{ {} }}", TasInput::Tas::ButtonsToString(buttons)); | ||
| 156 | } | ||
| 157 | |||
| 158 | std::string Tas::DebugJoystick(float x, float y) const { | ||
| 159 | return fmt::format("[ {} , {} ]", std::to_string(x), std::to_string(y)); | ||
| 160 | } | ||
| 161 | |||
| 162 | std::string Tas::DebugInput(const TasData& data) const { | ||
| 163 | return fmt::format("{{ {} , {} , {} }}", DebugButtons(data.buttons), | ||
| 164 | DebugJoystick(data.axis[0], data.axis[1]), | ||
| 165 | DebugJoystick(data.axis[2], data.axis[3])); | ||
| 166 | } | ||
| 167 | |||
| 168 | std::string Tas::DebugInputs(const std::array<TasData, PLAYER_NUMBER>& arr) const { | ||
| 169 | std::string returns = "[ "; | ||
| 170 | for (size_t i = 0; i < arr.size(); i++) { | ||
| 171 | returns += DebugInput(arr[i]); | ||
| 172 | if (i != arr.size() - 1) { | ||
| 173 | returns += " , "; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | return returns + "]"; | ||
| 177 | } | ||
| 178 | |||
| 179 | std::string Tas::ButtonsToString(u32 button) const { | ||
| 180 | std::string returns; | ||
| 181 | for (auto [text_button, tas_button] : text_to_tas_button) { | ||
| 182 | if ((button & static_cast<u32>(tas_button)) != 0) | ||
| 183 | returns += fmt::format(", {}", text_button.substr(4)); | ||
| 184 | } | ||
| 185 | return returns.empty() ? "" : returns.substr(2); | ||
| 186 | } | ||
| 187 | |||
| 188 | void Tas::UpdateThread() { | ||
| 189 | if (!Settings::values.tas_enable) { | ||
| 190 | if (is_running) { | ||
| 191 | Stop(); | ||
| 192 | } | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | if (is_recording) { | ||
| 197 | record_commands.push_back(last_input); | ||
| 198 | } | ||
| 199 | if (needs_reset) { | ||
| 200 | current_command = 0; | ||
| 201 | needs_reset = false; | ||
| 202 | LoadTasFiles(); | ||
| 203 | LOG_DEBUG(Input, "tas_reset done"); | ||
| 204 | } | ||
| 205 | |||
| 206 | if (!is_running) { | ||
| 207 | tas_data.fill({}); | ||
| 208 | return; | ||
| 209 | } | ||
| 210 | if (current_command < script_length) { | ||
| 211 | LOG_DEBUG(Input, "Playing TAS {}/{}", current_command, script_length); | ||
| 212 | size_t frame = current_command++; | ||
| 213 | for (size_t i = 0; i < commands.size(); i++) { | ||
| 214 | if (frame < commands[i].size()) { | ||
| 215 | TASCommand command = commands[i][frame]; | ||
| 216 | tas_data[i].buttons = command.buttons; | ||
| 217 | auto [l_axis_x, l_axis_y] = command.l_axis; | ||
| 218 | tas_data[i].axis[0] = l_axis_x; | ||
| 219 | tas_data[i].axis[1] = l_axis_y; | ||
| 220 | auto [r_axis_x, r_axis_y] = command.r_axis; | ||
| 221 | tas_data[i].axis[2] = r_axis_x; | ||
| 222 | tas_data[i].axis[3] = r_axis_y; | ||
| 223 | } else { | ||
| 224 | tas_data[i] = {}; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | } else { | ||
| 228 | is_running = Settings::values.tas_loop.GetValue(); | ||
| 229 | current_command = 0; | ||
| 230 | tas_data.fill({}); | ||
| 231 | if (!is_running) { | ||
| 232 | SwapToStoredController(); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | LOG_DEBUG(Input, "TAS inputs: {}", DebugInputs(tas_data)); | ||
| 236 | } | ||
| 237 | |||
| 238 | TasAnalog Tas::ReadCommandAxis(const std::string& line) const { | ||
| 239 | std::stringstream linestream(line); | ||
| 240 | std::string segment; | ||
| 241 | std::vector<std::string> seglist; | ||
| 242 | |||
| 243 | while (std::getline(linestream, segment, ';')) { | ||
| 244 | seglist.push_back(segment); | ||
| 245 | } | ||
| 246 | |||
| 247 | const float x = std::stof(seglist.at(0)) / 32767.0f; | ||
| 248 | const float y = std::stof(seglist.at(1)) / 32767.0f; | ||
| 249 | |||
| 250 | return {x, y}; | ||
| 251 | } | ||
| 252 | |||
| 253 | u32 Tas::ReadCommandButtons(const std::string& data) const { | ||
| 254 | std::stringstream button_text(data); | ||
| 255 | std::string line; | ||
| 256 | u32 buttons = 0; | ||
| 257 | while (std::getline(button_text, line, ';')) { | ||
| 258 | for (auto [text, tas_button] : text_to_tas_button) { | ||
| 259 | if (text == line) { | ||
| 260 | buttons |= static_cast<u32>(tas_button); | ||
| 261 | break; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | return buttons; | ||
| 266 | } | ||
| 267 | |||
| 268 | std::string Tas::WriteCommandAxis(TasAnalog data) const { | ||
| 269 | auto [x, y] = data; | ||
| 270 | std::string line; | ||
| 271 | line += std::to_string(static_cast<int>(x * 32767)); | ||
| 272 | line += ";"; | ||
| 273 | line += std::to_string(static_cast<int>(y * 32767)); | ||
| 274 | return line; | ||
| 275 | } | ||
| 276 | |||
| 277 | std::string Tas::WriteCommandButtons(u32 data) const { | ||
| 278 | if (data == 0) { | ||
| 279 | return "NONE"; | ||
| 280 | } | ||
| 281 | |||
| 282 | std::string line; | ||
| 283 | u32 index = 0; | ||
| 284 | while (data > 0) { | ||
| 285 | if ((data & 1) == 1) { | ||
| 286 | for (auto [text, tas_button] : text_to_tas_button) { | ||
| 287 | if (tas_button == static_cast<TasButton>(1 << index)) { | ||
| 288 | if (line.size() > 0) { | ||
| 289 | line += ";"; | ||
| 290 | } | ||
| 291 | line += text; | ||
| 292 | break; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | } | ||
| 296 | index++; | ||
| 297 | data >>= 1; | ||
| 298 | } | ||
| 299 | return line; | ||
| 300 | } | ||
| 301 | |||
| 302 | void Tas::StartStop() { | ||
| 303 | if (!Settings::values.tas_enable) { | ||
| 304 | return; | ||
| 305 | } | ||
| 306 | if (is_running) { | ||
| 307 | Stop(); | ||
| 308 | } else { | ||
| 309 | is_running = true; | ||
| 310 | SwapToTasController(); | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | void Tas::Stop() { | ||
| 315 | is_running = false; | ||
| 316 | SwapToStoredController(); | ||
| 317 | } | ||
| 318 | |||
| 319 | void Tas::SwapToTasController() { | ||
| 320 | if (!Settings::values.tas_swap_controllers) { | ||
| 321 | return; | ||
| 322 | } | ||
| 323 | auto& players = Settings::values.players.GetValue(); | ||
| 324 | for (std::size_t index = 0; index < players.size(); index++) { | ||
| 325 | auto& player = players[index]; | ||
| 326 | player_mappings[index] = player; | ||
| 327 | |||
| 328 | // Only swap active controllers | ||
| 329 | if (!player.connected) { | ||
| 330 | continue; | ||
| 331 | } | ||
| 332 | |||
| 333 | Common::ParamPackage tas_param; | ||
| 334 | tas_param.Set("pad", static_cast<u8>(index)); | ||
| 335 | auto button_mapping = GetButtonMappingForDevice(tas_param); | ||
| 336 | auto analog_mapping = GetAnalogMappingForDevice(tas_param); | ||
| 337 | auto& buttons = player.buttons; | ||
| 338 | auto& analogs = player.analogs; | ||
| 339 | |||
| 340 | for (std::size_t i = 0; i < buttons.size(); ++i) { | ||
| 341 | buttons[i] = button_mapping[static_cast<Settings::NativeButton::Values>(i)].Serialize(); | ||
| 342 | } | ||
| 343 | for (std::size_t i = 0; i < analogs.size(); ++i) { | ||
| 344 | analogs[i] = analog_mapping[static_cast<Settings::NativeAnalog::Values>(i)].Serialize(); | ||
| 345 | } | ||
| 346 | } | ||
| 347 | is_old_input_saved = true; | ||
| 348 | Settings::values.is_device_reload_pending.store(true); | ||
| 349 | } | ||
| 350 | |||
| 351 | void Tas::SwapToStoredController() { | ||
| 352 | if (!is_old_input_saved) { | ||
| 353 | return; | ||
| 354 | } | ||
| 355 | auto& players = Settings::values.players.GetValue(); | ||
| 356 | for (std::size_t index = 0; index < players.size(); index++) { | ||
| 357 | players[index] = player_mappings[index]; | ||
| 358 | } | ||
| 359 | is_old_input_saved = false; | ||
| 360 | Settings::values.is_device_reload_pending.store(true); | ||
| 361 | } | ||
| 362 | |||
| 363 | void Tas::Reset() { | ||
| 364 | if (!Settings::values.tas_enable) { | ||
| 365 | return; | ||
| 366 | } | ||
| 367 | needs_reset = true; | ||
| 368 | } | ||
| 369 | |||
| 370 | bool Tas::Record() { | ||
| 371 | if (!Settings::values.tas_enable) { | ||
| 372 | return true; | ||
| 373 | } | ||
| 374 | is_recording = !is_recording; | ||
| 375 | return is_recording; | ||
| 376 | } | ||
| 377 | |||
| 378 | void Tas::SaveRecording(bool overwrite_file) { | ||
| 379 | if (is_recording) { | ||
| 380 | return; | ||
| 381 | } | ||
| 382 | if (record_commands.empty()) { | ||
| 383 | return; | ||
| 384 | } | ||
| 385 | WriteTasFile(u8"record.txt"); | ||
| 386 | if (overwrite_file) { | ||
| 387 | WriteTasFile(u8"script0-1.txt"); | ||
| 388 | } | ||
| 389 | needs_reset = true; | ||
| 390 | record_commands.clear(); | ||
| 391 | } | ||
| 392 | |||
| 393 | InputCommon::ButtonMapping Tas::GetButtonMappingForDevice( | ||
| 394 | const Common::ParamPackage& params) const { | ||
| 395 | // This list is missing ZL/ZR since those are not considered buttons. | ||
| 396 | // We will add those afterwards | ||
| 397 | // This list also excludes any button that can't be really mapped | ||
| 398 | static constexpr std::array<std::pair<Settings::NativeButton::Values, TasButton>, 20> | ||
| 399 | switch_to_tas_button = { | ||
| 400 | std::pair{Settings::NativeButton::A, TasButton::BUTTON_A}, | ||
| 401 | {Settings::NativeButton::B, TasButton::BUTTON_B}, | ||
| 402 | {Settings::NativeButton::X, TasButton::BUTTON_X}, | ||
| 403 | {Settings::NativeButton::Y, TasButton::BUTTON_Y}, | ||
| 404 | {Settings::NativeButton::LStick, TasButton::STICK_L}, | ||
| 405 | {Settings::NativeButton::RStick, TasButton::STICK_R}, | ||
| 406 | {Settings::NativeButton::L, TasButton::TRIGGER_L}, | ||
| 407 | {Settings::NativeButton::R, TasButton::TRIGGER_R}, | ||
| 408 | {Settings::NativeButton::Plus, TasButton::BUTTON_PLUS}, | ||
| 409 | {Settings::NativeButton::Minus, TasButton::BUTTON_MINUS}, | ||
| 410 | {Settings::NativeButton::DLeft, TasButton::BUTTON_LEFT}, | ||
| 411 | {Settings::NativeButton::DUp, TasButton::BUTTON_UP}, | ||
| 412 | {Settings::NativeButton::DRight, TasButton::BUTTON_RIGHT}, | ||
| 413 | {Settings::NativeButton::DDown, TasButton::BUTTON_DOWN}, | ||
| 414 | {Settings::NativeButton::SL, TasButton::BUTTON_SL}, | ||
| 415 | {Settings::NativeButton::SR, TasButton::BUTTON_SR}, | ||
| 416 | {Settings::NativeButton::Screenshot, TasButton::BUTTON_CAPTURE}, | ||
| 417 | {Settings::NativeButton::Home, TasButton::BUTTON_HOME}, | ||
| 418 | {Settings::NativeButton::ZL, TasButton::TRIGGER_ZL}, | ||
| 419 | {Settings::NativeButton::ZR, TasButton::TRIGGER_ZR}, | ||
| 420 | }; | ||
| 421 | |||
| 422 | InputCommon::ButtonMapping mapping{}; | ||
| 423 | for (const auto& [switch_button, tas_button] : switch_to_tas_button) { | ||
| 424 | Common::ParamPackage button_params({{"engine", "tas"}}); | ||
| 425 | button_params.Set("pad", params.Get("pad", 0)); | ||
| 426 | button_params.Set("button", static_cast<int>(tas_button)); | ||
| 427 | mapping.insert_or_assign(switch_button, std::move(button_params)); | ||
| 428 | } | ||
| 429 | |||
| 430 | return mapping; | ||
| 431 | } | ||
| 432 | |||
| 433 | InputCommon::AnalogMapping Tas::GetAnalogMappingForDevice( | ||
| 434 | const Common::ParamPackage& params) const { | ||
| 435 | |||
| 436 | InputCommon::AnalogMapping mapping = {}; | ||
| 437 | Common::ParamPackage left_analog_params; | ||
| 438 | left_analog_params.Set("engine", "tas"); | ||
| 439 | left_analog_params.Set("pad", params.Get("pad", 0)); | ||
| 440 | left_analog_params.Set("axis_x", static_cast<int>(TasAxes::StickX)); | ||
| 441 | left_analog_params.Set("axis_y", static_cast<int>(TasAxes::StickY)); | ||
| 442 | mapping.insert_or_assign(Settings::NativeAnalog::LStick, std::move(left_analog_params)); | ||
| 443 | Common::ParamPackage right_analog_params; | ||
| 444 | right_analog_params.Set("engine", "tas"); | ||
| 445 | right_analog_params.Set("pad", params.Get("pad", 0)); | ||
| 446 | right_analog_params.Set("axis_x", static_cast<int>(TasAxes::SubstickX)); | ||
| 447 | right_analog_params.Set("axis_y", static_cast<int>(TasAxes::SubstickY)); | ||
| 448 | mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params)); | ||
| 449 | return mapping; | ||
| 450 | } | ||
| 451 | |||
| 452 | const TasData& Tas::GetTasState(std::size_t pad) const { | ||
| 453 | return tas_data[pad]; | ||
| 454 | } | ||
| 455 | } // namespace TasInput | ||