tas_input.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  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, size_t> Tas::GetStatus() const {
  142. TasState state;
  143. if (is_recording) {
  144. return {TasState::Recording, 0, record_commands.size()};
  145. }
  146. if (is_running) {
  147. state = TasState::Running;
  148. } else {
  149. state = TasState::Stopped;
  150. }
  151. return {state, current_command, script_length};
  152. }
  153. void Tas::UpdateThread() {
  154. if (!Settings::values.tas_enable) {
  155. if (is_running) {
  156. Stop();
  157. }
  158. return;
  159. }
  160. if (is_recording) {
  161. record_commands.push_back(last_input);
  162. }
  163. if (needs_reset) {
  164. current_command = 0;
  165. needs_reset = false;
  166. LoadTasFiles();
  167. LOG_DEBUG(Input, "tas_reset done");
  168. }
  169. if (!is_running) {
  170. ClearInput();
  171. return;
  172. }
  173. if (current_command < script_length) {
  174. LOG_DEBUG(Input, "Playing TAS {}/{}", current_command, script_length);
  175. const size_t frame = current_command++;
  176. for (size_t player_index = 0; player_index < commands.size(); player_index++) {
  177. TASCommand command{};
  178. if (frame < commands[player_index].size()) {
  179. command = commands[player_index][frame];
  180. }
  181. PadIdentifier identifier{
  182. .guid = Common::UUID{},
  183. .port = player_index,
  184. .pad = 0,
  185. };
  186. for (std::size_t i = 0; i < sizeof(command.buttons) * 8; ++i) {
  187. const bool button_status = (command.buttons & (1LLU << i)) != 0;
  188. const int button = static_cast<int>(i);
  189. SetButton(identifier, button, button_status);
  190. }
  191. SetTasAxis(identifier, TasAxis::StickX, command.l_axis.x);
  192. SetTasAxis(identifier, TasAxis::StickY, command.l_axis.y);
  193. SetTasAxis(identifier, TasAxis::SubstickX, command.r_axis.x);
  194. SetTasAxis(identifier, TasAxis::SubstickY, command.r_axis.y);
  195. }
  196. } else {
  197. is_running = Settings::values.tas_loop.GetValue();
  198. LoadTasFiles();
  199. current_command = 0;
  200. ClearInput();
  201. }
  202. }
  203. void Tas::ClearInput() {
  204. ResetButtonState();
  205. ResetAnalogState();
  206. }
  207. TasAnalog Tas::ReadCommandAxis(const std::string& line) const {
  208. std::vector<std::string> seg_list;
  209. {
  210. std::istringstream line_stream(line);
  211. std::string segment;
  212. while (std::getline(line_stream, segment, ';')) {
  213. seg_list.push_back(std::move(segment));
  214. }
  215. }
  216. if (seg_list.size() < 2) {
  217. LOG_ERROR(Input, "Invalid axis data: '{}'", line);
  218. return {};
  219. }
  220. try {
  221. const float x = std::stof(seg_list.at(0)) / 32767.0f;
  222. const float y = std::stof(seg_list.at(1)) / 32767.0f;
  223. return {x, y};
  224. } catch (const std::invalid_argument&) {
  225. LOG_ERROR(Input, "Invalid argument: '{}'", line);
  226. } catch (const std::out_of_range&) {
  227. LOG_ERROR(Input, "Out of range: '{}'", line);
  228. }
  229. return {};
  230. }
  231. u64 Tas::ReadCommandButtons(const std::string& line) const {
  232. std::istringstream button_text(line);
  233. std::string button_line;
  234. u64 buttons = 0;
  235. while (std::getline(button_text, button_line, ';')) {
  236. for (const auto& [text, tas_button] : text_to_tas_button) {
  237. if (text == button_line) {
  238. buttons |= static_cast<u64>(tas_button);
  239. break;
  240. }
  241. }
  242. }
  243. return buttons;
  244. }
  245. std::string Tas::WriteCommandButtons(u64 buttons) const {
  246. std::string returns;
  247. for (const auto& [text_button, tas_button] : text_to_tas_button) {
  248. if ((buttons & static_cast<u64>(tas_button)) != 0) {
  249. returns += fmt::format("{};", text_button);
  250. }
  251. }
  252. return returns.empty() ? "NONE" : returns;
  253. }
  254. std::string Tas::WriteCommandAxis(TasAnalog analog) const {
  255. return fmt::format("{};{}", analog.x * 32767, analog.y * 32767);
  256. }
  257. void Tas::SetTasAxis(const PadIdentifier& identifier, TasAxis axis, f32 value) {
  258. SetAxis(identifier, static_cast<int>(axis), value);
  259. }
  260. void Tas::StartStop() {
  261. if (!Settings::values.tas_enable) {
  262. return;
  263. }
  264. if (is_running) {
  265. Stop();
  266. } else {
  267. is_running = true;
  268. }
  269. }
  270. void Tas::Stop() {
  271. is_running = false;
  272. }
  273. void Tas::Reset() {
  274. if (!Settings::values.tas_enable) {
  275. return;
  276. }
  277. needs_reset = true;
  278. }
  279. bool Tas::Record() {
  280. if (!Settings::values.tas_enable) {
  281. return true;
  282. }
  283. is_recording = !is_recording;
  284. return is_recording;
  285. }
  286. void Tas::SaveRecording(bool overwrite_file) {
  287. if (is_recording) {
  288. return;
  289. }
  290. if (record_commands.empty()) {
  291. return;
  292. }
  293. WriteTasFile(u8"record.txt");
  294. if (overwrite_file) {
  295. WriteTasFile(u8"script0-1.txt");
  296. }
  297. needs_reset = true;
  298. record_commands.clear();
  299. }
  300. } // namespace InputCommon::TasInput