tas_input.cpp 10.0 KB

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