diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/input_common/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | src/input_common/tas/tas_input.cpp | 455 | ||||
| -rw-r--r-- | src/input_common/tas/tas_input.h | 237 | ||||
| -rw-r--r-- | src/input_common/tas/tas_poller.cpp | 101 | ||||
| -rw-r--r-- | src/input_common/tas/tas_poller.h | 43 |
5 files changed, 2 insertions, 840 deletions
diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt index c8871513c..19270dc84 100644 --- a/src/input_common/CMakeLists.txt +++ b/src/input_common/CMakeLists.txt | |||
| @@ -5,6 +5,8 @@ add_library(input_common STATIC | |||
| 5 | drivers/keyboard.h | 5 | drivers/keyboard.h |
| 6 | drivers/mouse.cpp | 6 | drivers/mouse.cpp |
| 7 | drivers/mouse.h | 7 | drivers/mouse.h |
| 8 | drivers/tas_input.cpp | ||
| 9 | drivers/tas_input.h | ||
| 8 | drivers/touch_screen.cpp | 10 | drivers/touch_screen.cpp |
| 9 | drivers/touch_screen.h | 11 | drivers/touch_screen.h |
| 10 | helpers/stick_from_buttons.cpp | 12 | helpers/stick_from_buttons.cpp |
| @@ -27,10 +29,6 @@ add_library(input_common STATIC | |||
| 27 | motion_input.h | 29 | motion_input.h |
| 28 | sdl/sdl.cpp | 30 | sdl/sdl.cpp |
| 29 | sdl/sdl.h | 31 | sdl/sdl.h |
| 30 | tas/tas_input.cpp | ||
| 31 | tas/tas_input.h | ||
| 32 | tas/tas_poller.cpp | ||
| 33 | tas/tas_poller.h | ||
| 34 | udp/client.cpp | 32 | udp/client.cpp |
| 35 | udp/client.h | 33 | udp/client.h |
| 36 | udp/udp.cpp | 34 | udp/udp.cpp |
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 | ||
diff --git a/src/input_common/tas/tas_input.h b/src/input_common/tas/tas_input.h deleted file mode 100644 index 3e2db8f00..000000000 --- a/src/input_common/tas/tas_input.h +++ /dev/null | |||
| @@ -1,237 +0,0 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | |||
| 9 | #include "common/common_types.h" | ||
| 10 | #include "common/settings_input.h" | ||
| 11 | #include "core/frontend/input.h" | ||
| 12 | #include "input_common/main.h" | ||
| 13 | |||
| 14 | /* | ||
| 15 | To play back TAS scripts on Yuzu, select the folder with scripts in the configuration menu below | ||
| 16 | Tools -> Configure TAS. The file itself has normal text format and has to be called script0-1.txt | ||
| 17 | for controller 1, script0-2.txt for controller 2 and so forth (with max. 8 players). | ||
| 18 | |||
| 19 | A script file has the same format as TAS-nx uses, so final files will look like this: | ||
| 20 | |||
| 21 | 1 KEY_B 0;0 0;0 | ||
| 22 | 6 KEY_ZL 0;0 0;0 | ||
| 23 | 41 KEY_ZL;KEY_Y 0;0 0;0 | ||
| 24 | 43 KEY_X;KEY_A 32767;0 0;0 | ||
| 25 | 44 KEY_A 32767;0 0;0 | ||
| 26 | 45 KEY_A 32767;0 0;0 | ||
| 27 | 46 KEY_A 32767;0 0;0 | ||
| 28 | 47 KEY_A 32767;0 0;0 | ||
| 29 | |||
| 30 | After placing the file at the correct location, it can be read into Yuzu with the (default) hotkey | ||
| 31 | CTRL+F6 (refresh). In the bottom left corner, it will display the amount of frames the script file | ||
| 32 | has. Playback can be started or stopped using CTRL+F5. | ||
| 33 | |||
| 34 | However, for playback to actually work, the correct input device has to be selected: In the Controls | ||
| 35 | menu, select TAS from the device list for the controller that the script should be played on. | ||
| 36 | |||
| 37 | Recording a new script file is really simple: Just make sure that the proper device (not TAS) is | ||
| 38 | connected on P1, and press CTRL+F7 to start recording. When done, just press the same keystroke | ||
| 39 | again (CTRL+F7). The new script will be saved at the location previously selected, as the filename | ||
| 40 | record.txt. | ||
| 41 | |||
| 42 | For debugging purposes, the common controller debugger can be used (View -> Debugging -> Controller | ||
| 43 | P1). | ||
| 44 | */ | ||
| 45 | |||
| 46 | namespace TasInput { | ||
| 47 | |||
| 48 | constexpr size_t PLAYER_NUMBER = 8; | ||
| 49 | |||
| 50 | using TasAnalog = std::pair<float, float>; | ||
| 51 | |||
| 52 | enum class TasState { | ||
| 53 | Running, | ||
| 54 | Recording, | ||
| 55 | Stopped, | ||
| 56 | }; | ||
| 57 | |||
| 58 | enum class TasButton : u32 { | ||
| 59 | BUTTON_A = 1U << 0, | ||
| 60 | BUTTON_B = 1U << 1, | ||
| 61 | BUTTON_X = 1U << 2, | ||
| 62 | BUTTON_Y = 1U << 3, | ||
| 63 | STICK_L = 1U << 4, | ||
| 64 | STICK_R = 1U << 5, | ||
| 65 | TRIGGER_L = 1U << 6, | ||
| 66 | TRIGGER_R = 1U << 7, | ||
| 67 | TRIGGER_ZL = 1U << 8, | ||
| 68 | TRIGGER_ZR = 1U << 9, | ||
| 69 | BUTTON_PLUS = 1U << 10, | ||
| 70 | BUTTON_MINUS = 1U << 11, | ||
| 71 | BUTTON_LEFT = 1U << 12, | ||
| 72 | BUTTON_UP = 1U << 13, | ||
| 73 | BUTTON_RIGHT = 1U << 14, | ||
| 74 | BUTTON_DOWN = 1U << 15, | ||
| 75 | BUTTON_SL = 1U << 16, | ||
| 76 | BUTTON_SR = 1U << 17, | ||
| 77 | BUTTON_HOME = 1U << 18, | ||
| 78 | BUTTON_CAPTURE = 1U << 19, | ||
| 79 | }; | ||
| 80 | |||
| 81 | enum class TasAxes : u8 { | ||
| 82 | StickX, | ||
| 83 | StickY, | ||
| 84 | SubstickX, | ||
| 85 | SubstickY, | ||
| 86 | Undefined, | ||
| 87 | }; | ||
| 88 | |||
| 89 | struct TasData { | ||
| 90 | u32 buttons{}; | ||
| 91 | std::array<float, 4> axis{}; | ||
| 92 | }; | ||
| 93 | |||
| 94 | class Tas { | ||
| 95 | public: | ||
| 96 | Tas(); | ||
| 97 | ~Tas(); | ||
| 98 | |||
| 99 | // Changes the input status that will be stored in each frame | ||
| 100 | void RecordInput(u32 buttons, const std::array<std::pair<float, float>, 2>& axes); | ||
| 101 | |||
| 102 | // Main loop that records or executes input | ||
| 103 | void UpdateThread(); | ||
| 104 | |||
| 105 | // Sets the flag to start or stop the TAS command excecution and swaps controllers profiles | ||
| 106 | void StartStop(); | ||
| 107 | |||
| 108 | // Stop the TAS and reverts any controller profile | ||
| 109 | void Stop(); | ||
| 110 | |||
| 111 | // Sets the flag to reload the file and start from the begining in the next update | ||
| 112 | void Reset(); | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Sets the flag to enable or disable recording of inputs | ||
| 116 | * @return Returns true if the current recording status is enabled | ||
| 117 | */ | ||
| 118 | bool Record(); | ||
| 119 | |||
| 120 | // Saves contents of record_commands on a file if overwrite is enabled player 1 will be | ||
| 121 | // overwritten with the recorded commands | ||
| 122 | void SaveRecording(bool overwrite_file); | ||
| 123 | |||
| 124 | /** | ||
| 125 | * Returns the current status values of TAS playback/recording | ||
| 126 | * @return Tuple of | ||
| 127 | * TasState indicating the current state out of Running, Recording or Stopped ; | ||
| 128 | * Current playback progress or amount of frames (so far) for Recording ; | ||
| 129 | * Total length of script file currently loaded or amount of frames (so far) for Recording | ||
| 130 | */ | ||
| 131 | std::tuple<TasState, size_t, size_t> GetStatus() const; | ||
| 132 | |||
| 133 | // Retuns an array of the default button mappings | ||
| 134 | InputCommon::ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) const; | ||
| 135 | |||
| 136 | // Retuns an array of the default analog mappings | ||
| 137 | InputCommon::AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) const; | ||
| 138 | [[nodiscard]] const TasData& GetTasState(std::size_t pad) const; | ||
| 139 | |||
| 140 | private: | ||
| 141 | struct TASCommand { | ||
| 142 | u32 buttons{}; | ||
| 143 | TasAnalog l_axis{}; | ||
| 144 | TasAnalog r_axis{}; | ||
| 145 | }; | ||
| 146 | |||
| 147 | // Loads TAS files from all players | ||
| 148 | void LoadTasFiles(); | ||
| 149 | |||
| 150 | // Loads TAS file from the specified player | ||
| 151 | void LoadTasFile(size_t player_index); | ||
| 152 | |||
| 153 | // Writes a TAS file from the recorded commands | ||
| 154 | void WriteTasFile(std::u8string file_name); | ||
| 155 | |||
| 156 | /** | ||
| 157 | * Parses a string containing the axis values with the following format "x;y" | ||
| 158 | * X and Y have a range from -32767 to 32767 | ||
| 159 | * @return Returns a TAS analog object with axis values with range from -1.0 to 1.0 | ||
| 160 | */ | ||
| 161 | TasAnalog ReadCommandAxis(const std::string& line) const; | ||
| 162 | |||
| 163 | /** | ||
| 164 | * Parses a string containing the button values with the following format "a;b;c;d..." | ||
| 165 | * Each button is represented by it's text format specified in text_to_tas_button array | ||
| 166 | * @return Returns a u32 with each bit representing the status of a button | ||
| 167 | */ | ||
| 168 | u32 ReadCommandButtons(const std::string& line) const; | ||
| 169 | |||
| 170 | /** | ||
| 171 | * Converts an u32 containing the button status into the text equivalent | ||
| 172 | * @return Returns a string with the name of the buttons to be written to the file | ||
| 173 | */ | ||
| 174 | std::string WriteCommandButtons(u32 data) const; | ||
| 175 | |||
| 176 | /** | ||
| 177 | * Converts an TAS analog object containing the axis status into the text equivalent | ||
| 178 | * @return Returns a string with the value of the axis to be written to the file | ||
| 179 | */ | ||
| 180 | std::string WriteCommandAxis(TasAnalog data) const; | ||
| 181 | |||
| 182 | // Inverts the Y axis polarity | ||
| 183 | std::pair<float, float> FlipAxisY(std::pair<float, float> old); | ||
| 184 | |||
| 185 | /** | ||
| 186 | * Converts an u32 containing the button status into the text equivalent | ||
| 187 | * @return Returns a string with the name of the buttons to be printed on console | ||
| 188 | */ | ||
| 189 | std::string DebugButtons(u32 buttons) const; | ||
| 190 | |||
| 191 | /** | ||
| 192 | * Converts an TAS analog object containing the axis status into the text equivalent | ||
| 193 | * @return Returns a string with the value of the axis to be printed on console | ||
| 194 | */ | ||
| 195 | std::string DebugJoystick(float x, float y) const; | ||
| 196 | |||
| 197 | /** | ||
| 198 | * Converts the given TAS status into the text equivalent | ||
| 199 | * @return Returns a string with the value of the TAS status to be printed on console | ||
| 200 | */ | ||
| 201 | std::string DebugInput(const TasData& data) const; | ||
| 202 | |||
| 203 | /** | ||
| 204 | * Converts the given TAS status of multiple players into the text equivalent | ||
| 205 | * @return Returns a string with the value of the status of all TAS players to be printed on | ||
| 206 | * console | ||
| 207 | */ | ||
| 208 | std::string DebugInputs(const std::array<TasData, PLAYER_NUMBER>& arr) const; | ||
| 209 | |||
| 210 | /** | ||
| 211 | * Converts an u32 containing the button status into the text equivalent | ||
| 212 | * @return Returns a string with the name of the buttons | ||
| 213 | */ | ||
| 214 | std::string ButtonsToString(u32 button) const; | ||
| 215 | |||
| 216 | // Stores current controller configuration and sets a TAS controller for every active controller | ||
| 217 | // to the current config | ||
| 218 | void SwapToTasController(); | ||
| 219 | |||
| 220 | // Sets the stored controller configuration to the current config | ||
| 221 | void SwapToStoredController(); | ||
| 222 | |||
| 223 | size_t script_length{0}; | ||
| 224 | std::array<TasData, PLAYER_NUMBER> tas_data; | ||
| 225 | bool is_old_input_saved{false}; | ||
| 226 | bool is_recording{false}; | ||
| 227 | bool is_running{false}; | ||
| 228 | bool needs_reset{false}; | ||
| 229 | std::array<std::vector<TASCommand>, PLAYER_NUMBER> commands{}; | ||
| 230 | std::vector<TASCommand> record_commands{}; | ||
| 231 | size_t current_command{0}; | ||
| 232 | TASCommand last_input{}; // only used for recording | ||
| 233 | |||
| 234 | // Old settings for swapping controllers | ||
| 235 | std::array<Settings::PlayerInput, 10> player_mappings; | ||
| 236 | }; | ||
| 237 | } // namespace TasInput | ||
diff --git a/src/input_common/tas/tas_poller.cpp b/src/input_common/tas/tas_poller.cpp deleted file mode 100644 index 15810d6b0..000000000 --- a/src/input_common/tas/tas_poller.cpp +++ /dev/null | |||
| @@ -1,101 +0,0 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <mutex> | ||
| 6 | #include <utility> | ||
| 7 | |||
| 8 | #include "common/settings.h" | ||
| 9 | #include "common/threadsafe_queue.h" | ||
| 10 | #include "input_common/tas/tas_input.h" | ||
| 11 | #include "input_common/tas/tas_poller.h" | ||
| 12 | |||
| 13 | namespace InputCommon { | ||
| 14 | |||
| 15 | class TasButton final : public Input::ButtonDevice { | ||
| 16 | public: | ||
| 17 | explicit TasButton(u32 button_, u32 pad_, const TasInput::Tas* tas_input_) | ||
| 18 | : button(button_), pad(pad_), tas_input(tas_input_) {} | ||
| 19 | |||
| 20 | bool GetStatus() const override { | ||
| 21 | return (tas_input->GetTasState(pad).buttons & button) != 0; | ||
| 22 | } | ||
| 23 | |||
| 24 | private: | ||
| 25 | const u32 button; | ||
| 26 | const u32 pad; | ||
| 27 | const TasInput::Tas* tas_input; | ||
| 28 | }; | ||
| 29 | |||
| 30 | TasButtonFactory::TasButtonFactory(std::shared_ptr<TasInput::Tas> tas_input_) | ||
| 31 | : tas_input(std::move(tas_input_)) {} | ||
| 32 | |||
| 33 | std::unique_ptr<Input::ButtonDevice> TasButtonFactory::Create(const Common::ParamPackage& params) { | ||
| 34 | const auto button_id = params.Get("button", 0); | ||
| 35 | const auto pad = params.Get("pad", 0); | ||
| 36 | |||
| 37 | return std::make_unique<TasButton>(button_id, pad, tas_input.get()); | ||
| 38 | } | ||
| 39 | |||
| 40 | class TasAnalog final : public Input::AnalogDevice { | ||
| 41 | public: | ||
| 42 | explicit TasAnalog(u32 pad_, u32 axis_x_, u32 axis_y_, const TasInput::Tas* tas_input_) | ||
| 43 | : pad(pad_), axis_x(axis_x_), axis_y(axis_y_), tas_input(tas_input_) {} | ||
| 44 | |||
| 45 | float GetAxis(u32 axis) const { | ||
| 46 | std::lock_guard lock{mutex}; | ||
| 47 | return tas_input->GetTasState(pad).axis.at(axis); | ||
| 48 | } | ||
| 49 | |||
| 50 | std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { | ||
| 51 | float x = GetAxis(analog_axis_x); | ||
| 52 | float y = GetAxis(analog_axis_y); | ||
| 53 | |||
| 54 | // Make sure the coordinates are in the unit circle, | ||
| 55 | // otherwise normalize it. | ||
| 56 | float r = x * x + y * y; | ||
| 57 | if (r > 1.0f) { | ||
| 58 | r = std::sqrt(r); | ||
| 59 | x /= r; | ||
| 60 | y /= r; | ||
| 61 | } | ||
| 62 | |||
| 63 | return {x, y}; | ||
| 64 | } | ||
| 65 | |||
| 66 | std::tuple<float, float> GetStatus() const override { | ||
| 67 | return GetAnalog(axis_x, axis_y); | ||
| 68 | } | ||
| 69 | |||
| 70 | Input::AnalogProperties GetAnalogProperties() const override { | ||
| 71 | return {0.0f, 1.0f, 0.5f}; | ||
| 72 | } | ||
| 73 | |||
| 74 | private: | ||
| 75 | const u32 pad; | ||
| 76 | const u32 axis_x; | ||
| 77 | const u32 axis_y; | ||
| 78 | const TasInput::Tas* tas_input; | ||
| 79 | mutable std::mutex mutex; | ||
| 80 | }; | ||
| 81 | |||
| 82 | /// An analog device factory that creates analog devices from GC Adapter | ||
| 83 | TasAnalogFactory::TasAnalogFactory(std::shared_ptr<TasInput::Tas> tas_input_) | ||
| 84 | : tas_input(std::move(tas_input_)) {} | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Creates analog device from joystick axes | ||
| 88 | * @param params contains parameters for creating the device: | ||
| 89 | * - "port": the nth gcpad on the adapter | ||
| 90 | * - "axis_x": the index of the axis to be bind as x-axis | ||
| 91 | * - "axis_y": the index of the axis to be bind as y-axis | ||
| 92 | */ | ||
| 93 | std::unique_ptr<Input::AnalogDevice> TasAnalogFactory::Create(const Common::ParamPackage& params) { | ||
| 94 | const auto pad = static_cast<u32>(params.Get("pad", 0)); | ||
| 95 | const auto axis_x = static_cast<u32>(params.Get("axis_x", 0)); | ||
| 96 | const auto axis_y = static_cast<u32>(params.Get("axis_y", 1)); | ||
| 97 | |||
| 98 | return std::make_unique<TasAnalog>(pad, axis_x, axis_y, tas_input.get()); | ||
| 99 | } | ||
| 100 | |||
| 101 | } // namespace InputCommon | ||
diff --git a/src/input_common/tas/tas_poller.h b/src/input_common/tas/tas_poller.h deleted file mode 100644 index 09e426cef..000000000 --- a/src/input_common/tas/tas_poller.h +++ /dev/null | |||
| @@ -1,43 +0,0 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <memory> | ||
| 8 | #include "core/frontend/input.h" | ||
| 9 | #include "input_common/tas/tas_input.h" | ||
| 10 | |||
| 11 | namespace InputCommon { | ||
| 12 | |||
| 13 | /** | ||
| 14 | * A button device factory representing a tas bot. It receives tas events and forward them | ||
| 15 | * to all button devices it created. | ||
| 16 | */ | ||
| 17 | class TasButtonFactory final : public Input::Factory<Input::ButtonDevice> { | ||
| 18 | public: | ||
| 19 | explicit TasButtonFactory(std::shared_ptr<TasInput::Tas> tas_input_); | ||
| 20 | |||
| 21 | /** | ||
| 22 | * Creates a button device from a button press | ||
| 23 | * @param params contains parameters for creating the device: | ||
| 24 | * - "code": the code of the key to bind with the button | ||
| 25 | */ | ||
| 26 | std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override; | ||
| 27 | |||
| 28 | private: | ||
| 29 | std::shared_ptr<TasInput::Tas> tas_input; | ||
| 30 | }; | ||
| 31 | |||
| 32 | /// An analog device factory that creates analog devices from tas | ||
| 33 | class TasAnalogFactory final : public Input::Factory<Input::AnalogDevice> { | ||
| 34 | public: | ||
| 35 | explicit TasAnalogFactory(std::shared_ptr<TasInput::Tas> tas_input_); | ||
| 36 | |||
| 37 | std::unique_ptr<Input::AnalogDevice> Create(const Common::ParamPackage& params) override; | ||
| 38 | |||
| 39 | private: | ||
| 40 | std::shared_ptr<TasInput::Tas> tas_input; | ||
| 41 | }; | ||
| 42 | |||
| 43 | } // namespace InputCommon | ||