tas_input.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <cstring>
  4. #include <fmt/format.h>
  5. #include "common/fs/file.h"
  6. #include "common/fs/fs_types.h"
  7. #include "common/fs/path_util.h"
  8. #include "common/logging/log.h"
  9. #include "common/settings.h"
  10. #include "input_common/drivers/tas_input.h"
  11. namespace InputCommon::TasInput {
  12. enum class Tas::TasAxis : u8 {
  13. StickX,
  14. StickY,
  15. SubstickX,
  16. SubstickY,
  17. Undefined,
  18. };
  19. // Supported keywords and buttons from a TAS file
  20. constexpr std::array<std::pair<std::string_view, TasButton>, 18> text_to_tas_button = {
  21. std::pair{"KEY_A", TasButton::BUTTON_A},
  22. {"KEY_B", TasButton::BUTTON_B},
  23. {"KEY_X", TasButton::BUTTON_X},
  24. {"KEY_Y", TasButton::BUTTON_Y},
  25. {"KEY_LSTICK", TasButton::STICK_L},
  26. {"KEY_RSTICK", TasButton::STICK_R},
  27. {"KEY_L", TasButton::TRIGGER_L},
  28. {"KEY_R", TasButton::TRIGGER_R},
  29. {"KEY_PLUS", TasButton::BUTTON_PLUS},
  30. {"KEY_MINUS", TasButton::BUTTON_MINUS},
  31. {"KEY_DLEFT", TasButton::BUTTON_LEFT},
  32. {"KEY_DUP", TasButton::BUTTON_UP},
  33. {"KEY_DRIGHT", TasButton::BUTTON_RIGHT},
  34. {"KEY_DDOWN", TasButton::BUTTON_DOWN},
  35. {"KEY_SL", TasButton::BUTTON_SL},
  36. {"KEY_SR", TasButton::BUTTON_SR},
  37. // These buttons are disabled to avoid TAS input from activating hotkeys
  38. // {"KEY_CAPTURE", TasButton::BUTTON_CAPTURE},
  39. // {"KEY_HOME", TasButton::BUTTON_HOME},
  40. {"KEY_ZL", TasButton::TRIGGER_ZL},
  41. {"KEY_ZR", TasButton::TRIGGER_ZR},
  42. };
  43. Tas::Tas(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
  44. for (size_t player_index = 0; player_index < PLAYER_NUMBER; player_index++) {
  45. PadIdentifier identifier{
  46. .guid = Common::UUID{},
  47. .port = player_index,
  48. .pad = 0,
  49. };
  50. PreSetController(identifier);
  51. }
  52. ClearInput();
  53. if (!Settings::values.tas_enable) {
  54. needs_reset = true;
  55. return;
  56. }
  57. LoadTasFiles();
  58. }
  59. Tas::~Tas() {
  60. Stop();
  61. }
  62. void Tas::LoadTasFiles() {
  63. script_length = 0;
  64. for (size_t i = 0; i < commands.size(); i++) {
  65. LoadTasFile(i, 0);
  66. if (commands[i].size() > script_length) {
  67. script_length = commands[i].size();
  68. }
  69. }
  70. }
  71. void Tas::LoadTasFile(size_t player_index, size_t file_index) {
  72. commands[player_index].clear();
  73. std::string file = Common::FS::ReadStringFromFile(
  74. Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) /
  75. fmt::format("script{}-{}.txt", file_index, player_index + 1),
  76. Common::FS::FileType::BinaryFile);
  77. std::istringstream command_line(file);
  78. std::string line;
  79. int frame_no = 0;
  80. while (std::getline(command_line, line, '\n')) {
  81. if (line.empty()) {
  82. continue;
  83. }
  84. std::vector<std::string> seg_list;
  85. {
  86. std::istringstream line_stream(line);
  87. std::string segment;
  88. while (std::getline(line_stream, segment, ' ')) {
  89. seg_list.push_back(std::move(segment));
  90. }
  91. }
  92. if (seg_list.size() < 4) {
  93. continue;
  94. }
  95. try {
  96. const auto num_frames = std::stoi(seg_list[0]);
  97. while (frame_no < num_frames) {
  98. commands[player_index].emplace_back();
  99. frame_no++;
  100. }
  101. } catch (const std::invalid_argument&) {
  102. LOG_ERROR(Input, "Invalid argument: '{}' at command {}", seg_list[0], frame_no);
  103. } catch (const std::out_of_range&) {
  104. LOG_ERROR(Input, "Out of range: '{}' at command {}", seg_list[0], frame_no);
  105. }
  106. TASCommand command = {
  107. .buttons = ReadCommandButtons(seg_list[1]),
  108. .l_axis = ReadCommandAxis(seg_list[2]),
  109. .r_axis = ReadCommandAxis(seg_list[3]),
  110. };
  111. commands[player_index].push_back(command);
  112. frame_no++;
  113. }
  114. LOG_INFO(Input, "TAS file loaded! {} frames", frame_no);
  115. }
  116. void Tas::WriteTasFile(std::u8string_view file_name) {
  117. std::string output_text;
  118. for (size_t frame = 0; frame < record_commands.size(); frame++) {
  119. const TASCommand& line = record_commands[frame];
  120. output_text += fmt::format("{} {} {} {}\n", frame, WriteCommandButtons(line.buttons),
  121. WriteCommandAxis(line.l_axis), WriteCommandAxis(line.r_axis));
  122. }
  123. const auto tas_file_name = Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) / file_name;
  124. const auto bytes_written =
  125. Common::FS::WriteStringToFile(tas_file_name, Common::FS::FileType::TextFile, output_text);
  126. if (bytes_written == output_text.size()) {
  127. LOG_INFO(Input, "TAS file written to file!");
  128. } else {
  129. LOG_ERROR(Input, "Writing the TAS-file has failed! {} / {} bytes written", bytes_written,
  130. output_text.size());
  131. }
  132. }
  133. void Tas::RecordInput(u64 buttons, TasAnalog left_axis, TasAnalog right_axis) {
  134. last_input = {
  135. .buttons = buttons,
  136. .l_axis = left_axis,
  137. .r_axis = right_axis,
  138. };
  139. }
  140. std::tuple<TasState, size_t, size_t> Tas::GetStatus() const {
  141. TasState state;
  142. if (is_recording) {
  143. return {TasState::Recording, 0, record_commands.size()};
  144. }
  145. if (is_running) {
  146. state = TasState::Running;
  147. } else {
  148. state = TasState::Stopped;
  149. }
  150. return {state, current_command, script_length};
  151. }
  152. void Tas::UpdateThread() {
  153. if (!Settings::values.tas_enable) {
  154. if (is_running) {
  155. Stop();
  156. }
  157. return;
  158. }
  159. if (is_recording) {
  160. record_commands.push_back(last_input);
  161. }
  162. if (needs_reset) {
  163. current_command = 0;
  164. needs_reset = false;
  165. LoadTasFiles();
  166. LOG_DEBUG(Input, "tas_reset done");
  167. }
  168. if (!is_running) {
  169. ClearInput();
  170. return;
  171. }
  172. if (current_command < script_length) {
  173. LOG_DEBUG(Input, "Playing TAS {}/{}", current_command, script_length);
  174. const size_t frame = current_command++;
  175. for (size_t player_index = 0; player_index < commands.size(); player_index++) {
  176. TASCommand command{};
  177. if (frame < commands[player_index].size()) {
  178. command = commands[player_index][frame];
  179. }
  180. PadIdentifier identifier{
  181. .guid = Common::UUID{},
  182. .port = player_index,
  183. .pad = 0,
  184. };
  185. for (std::size_t i = 0; i < sizeof(command.buttons) * 8; ++i) {
  186. const bool button_status = (command.buttons & (1LLU << i)) != 0;
  187. const int button = static_cast<int>(i);
  188. SetButton(identifier, button, button_status);
  189. }
  190. SetTasAxis(identifier, TasAxis::StickX, command.l_axis.x);
  191. SetTasAxis(identifier, TasAxis::StickY, command.l_axis.y);
  192. SetTasAxis(identifier, TasAxis::SubstickX, command.r_axis.x);
  193. SetTasAxis(identifier, TasAxis::SubstickY, command.r_axis.y);
  194. }
  195. } else {
  196. is_running = Settings::values.tas_loop.GetValue();
  197. LoadTasFiles();
  198. current_command = 0;
  199. ClearInput();
  200. }
  201. }
  202. void Tas::ClearInput() {
  203. ResetButtonState();
  204. ResetAnalogState();
  205. }
  206. TasAnalog Tas::ReadCommandAxis(const std::string& line) const {
  207. std::vector<std::string> seg_list;
  208. {
  209. std::istringstream line_stream(line);
  210. std::string segment;
  211. while (std::getline(line_stream, segment, ';')) {
  212. seg_list.push_back(std::move(segment));
  213. }
  214. }
  215. if (seg_list.size() < 2) {
  216. LOG_ERROR(Input, "Invalid axis data: '{}'", line);
  217. return {};
  218. }
  219. try {
  220. const float x = std::stof(seg_list.at(0)) / 32767.0f;
  221. const float y = std::stof(seg_list.at(1)) / 32767.0f;
  222. return {x, y};
  223. } catch (const std::invalid_argument&) {
  224. LOG_ERROR(Input, "Invalid argument: '{}'", line);
  225. } catch (const std::out_of_range&) {
  226. LOG_ERROR(Input, "Out of range: '{}'", line);
  227. }
  228. return {};
  229. }
  230. u64 Tas::ReadCommandButtons(const std::string& line) const {
  231. std::istringstream button_text(line);
  232. std::string button_line;
  233. u64 buttons = 0;
  234. while (std::getline(button_text, button_line, ';')) {
  235. for (const auto& [text, tas_button] : text_to_tas_button) {
  236. if (text == button_line) {
  237. buttons |= static_cast<u64>(tas_button);
  238. break;
  239. }
  240. }
  241. }
  242. return buttons;
  243. }
  244. std::string Tas::WriteCommandButtons(u64 buttons) const {
  245. std::string returns;
  246. for (const auto& [text_button, tas_button] : text_to_tas_button) {
  247. if ((buttons & static_cast<u64>(tas_button)) != 0) {
  248. returns += fmt::format("{};", text_button);
  249. }
  250. }
  251. return returns.empty() ? "NONE" : returns;
  252. }
  253. std::string Tas::WriteCommandAxis(TasAnalog analog) const {
  254. return fmt::format("{};{}", analog.x * 32767, analog.y * 32767);
  255. }
  256. void Tas::SetTasAxis(const PadIdentifier& identifier, TasAxis axis, f32 value) {
  257. SetAxis(identifier, static_cast<int>(axis), value);
  258. }
  259. void Tas::StartStop() {
  260. if (!Settings::values.tas_enable) {
  261. return;
  262. }
  263. if (is_running) {
  264. Stop();
  265. } else {
  266. is_running = true;
  267. }
  268. }
  269. void Tas::Stop() {
  270. is_running = false;
  271. }
  272. void Tas::Reset() {
  273. if (!Settings::values.tas_enable) {
  274. return;
  275. }
  276. needs_reset = true;
  277. }
  278. bool Tas::Record() {
  279. if (!Settings::values.tas_enable) {
  280. return true;
  281. }
  282. is_recording = !is_recording;
  283. return is_recording;
  284. }
  285. void Tas::SaveRecording(bool overwrite_file) {
  286. if (is_recording) {
  287. return;
  288. }
  289. if (record_commands.empty()) {
  290. return;
  291. }
  292. WriteTasFile(u8"record.txt");
  293. if (overwrite_file) {
  294. WriteTasFile(u8"script0-1.txt");
  295. }
  296. needs_reset = true;
  297. record_commands.clear();
  298. }
  299. } // namespace InputCommon::TasInput