summaryrefslogtreecommitdiff
path: root/src/input_common/tas/tas_input.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/tas/tas_input.cpp')
-rw-r--r--src/input_common/tas/tas_input.cpp340
1 files changed, 340 insertions, 0 deletions
diff --git a/src/input_common/tas/tas_input.cpp b/src/input_common/tas/tas_input.cpp
new file mode 100644
index 000000000..343641945
--- /dev/null
+++ b/src/input_common/tas/tas_input.cpp
@@ -0,0 +1,340 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#include <chrono>
6#include <cstring>
7#include <functional>
8#include <random>
9#include <regex>
10#include <thread>
11#include <boost/asio.hpp>
12
13#include "common/fs/file.h"
14#include "common/fs/fs_types.h"
15#include "common/fs/path_util.h"
16#include "common/logging/log.h"
17#include "common/settings.h"
18#include "input_common/tas/tas_input.h"
19
20namespace TasInput {
21
22Tas::Tas() {
23 LoadTasFiles();
24}
25
26Tas::~Tas() {
27 update_thread_running = false;
28}
29
30void Tas::RefreshTasFile() {
31 refresh_tas_fle = true;
32}
33void Tas::LoadTasFiles() {
34 scriptLength = 0;
35 for (int i = 0; i < PLAYER_NUMBER; i++) {
36 LoadTasFile(i);
37 if (newCommands[i].size() > scriptLength)
38 scriptLength = newCommands[i].size();
39 }
40}
41void Tas::LoadTasFile(int playerIndex) {
42 LOG_DEBUG(Input, "LoadTasFile()");
43 if (!newCommands[playerIndex].empty()) {
44 newCommands[playerIndex].clear();
45 }
46 std::string file = Common::FS::ReadStringFromFile(
47 Common::FS::GetYuzuPathString(Common::FS::YuzuPath::TASFile) + "script0-" +
48 std::to_string(playerIndex + 1) + ".txt",
49 Common::FS::FileType::BinaryFile);
50 std::stringstream command_line(file);
51 std::string line;
52 int frameNo = 0;
53 TASCommand empty = {.buttons = 0, .l_axis = {0.f, 0.f}, .r_axis = {0.f, 0.f}};
54 while (std::getline(command_line, line, '\n')) {
55 if (line.empty())
56 continue;
57 LOG_DEBUG(Input, "Loading line: {}", line);
58 std::smatch m;
59
60 std::stringstream linestream(line);
61 std::string segment;
62 std::vector<std::string> seglist;
63
64 while (std::getline(linestream, segment, ' ')) {
65 seglist.push_back(segment);
66 }
67
68 if (seglist.size() < 4)
69 continue;
70
71 while (frameNo < std::stoi(seglist.at(0))) {
72 newCommands[playerIndex].push_back(empty);
73 frameNo++;
74 }
75
76 TASCommand command = {
77 .buttons = ReadCommandButtons(seglist.at(1)),
78 .l_axis = ReadCommandAxis(seglist.at(2)),
79 .r_axis = ReadCommandAxis(seglist.at(3)),
80 };
81 newCommands[playerIndex].push_back(command);
82 frameNo++;
83 }
84 LOG_INFO(Input, "TAS file loaded! {} frames", frameNo);
85}
86
87void Tas::WriteTasFile() {
88 LOG_DEBUG(Input, "WriteTasFile()");
89 std::string output_text = "";
90 for (int frame = 0; frame < (signed)recordCommands.size(); frame++) {
91 if (!output_text.empty())
92 output_text += "\n";
93 TASCommand line = recordCommands.at(frame);
94 output_text += std::to_string(frame) + " " + WriteCommandButtons(line.buttons) + " " +
95 WriteCommandAxis(line.l_axis) + " " + WriteCommandAxis(line.r_axis);
96 }
97 size_t bytesWritten = Common::FS::WriteStringToFile(
98 Common::FS::GetYuzuPathString(Common::FS::YuzuPath::TASFile) + "record.txt",
99 Common::FS::FileType::TextFile, output_text);
100 if (bytesWritten == output_text.size())
101 LOG_INFO(Input, "TAS file written to file!");
102 else
103 LOG_ERROR(Input, "Writing the TAS-file has failed! {} / {} bytes written", bytesWritten,
104 output_text.size());
105}
106
107void Tas::RecordInput(u32 buttons, std::array<std::pair<float, float>, 2> axes) {
108 lastInput = {buttons, flipY(axes[0]), flipY(axes[1])};
109}
110
111std::pair<float, float> Tas::flipY(std::pair<float, float> old) const {
112 auto [x, y] = old;
113 return {x, -y};
114}
115
116std::string Tas::GetStatusDescription() {
117 if (Settings::values.tas_record) {
118 return "Recording TAS: " + std::to_string(recordCommands.size());
119 }
120 if (Settings::values.tas_enable) {
121 return "Playing TAS: " + std::to_string(current_command) + "/" +
122 std::to_string(scriptLength);
123 }
124 return "TAS not running: " + std::to_string(current_command) + "/" +
125 std::to_string(scriptLength);
126}
127
128std::string debugButtons(u32 buttons) {
129 return "{ " + TasInput::Tas::buttonsToString(buttons) + " }";
130}
131
132std::string debugJoystick(float x, float y) {
133 return "[ " + std::to_string(x) + "," + std::to_string(y) + " ]";
134}
135
136std::string debugInput(TasData data) {
137 return "{ " + debugButtons(data.buttons) + " , " + debugJoystick(data.axis[0], data.axis[1]) +
138 " , " + debugJoystick(data.axis[2], data.axis[3]) + " }";
139}
140
141std::string debugInputs(std::array<TasData, PLAYER_NUMBER> arr) {
142 std::string returns = "[ ";
143 for (size_t i = 0; i < arr.size(); i++) {
144 returns += debugInput(arr[i]);
145 if (i != arr.size() - 1)
146 returns += " , ";
147 }
148 return returns + "]";
149}
150
151void Tas::UpdateThread() {
152 if (update_thread_running) {
153 if (Settings::values.pauseTasOnLoad && Settings::values.cpuBoosted) {
154 for (int i = 0; i < PLAYER_NUMBER; i++) {
155 tas_data[i].buttons = 0;
156 tas_data[i].axis = {};
157 }
158 }
159
160 if (Settings::values.tas_record) {
161 recordCommands.push_back(lastInput);
162 }
163 if (!Settings::values.tas_record && !recordCommands.empty()) {
164 WriteTasFile();
165 Settings::values.tas_reset = true;
166 refresh_tas_fle = true;
167 recordCommands.clear();
168 }
169 if (Settings::values.tas_reset) {
170 current_command = 0;
171 if (refresh_tas_fle) {
172 LoadTasFiles();
173 refresh_tas_fle = false;
174 }
175 Settings::values.tas_reset = false;
176 LoadTasFiles();
177 LOG_DEBUG(Input, "tas_reset done");
178 }
179 if (Settings::values.tas_enable) {
180 if ((signed)current_command < scriptLength) {
181 LOG_INFO(Input, "Playing TAS {}/{}", current_command, scriptLength);
182 size_t frame = current_command++;
183 for (int i = 0; i < PLAYER_NUMBER; i++) {
184 if (frame < newCommands[i].size()) {
185 TASCommand command = newCommands[i][frame];
186 tas_data[i].buttons = command.buttons;
187 auto [l_axis_x, l_axis_y] = command.l_axis;
188 tas_data[i].axis[0] = l_axis_x;
189 tas_data[i].axis[1] = l_axis_y;
190 auto [r_axis_x, r_axis_y] = command.r_axis;
191 tas_data[i].axis[2] = r_axis_x;
192 tas_data[i].axis[3] = r_axis_y;
193 } else {
194 tas_data[i].buttons = 0;
195 tas_data[i].axis = {};
196 }
197 }
198 } else {
199 Settings::values.tas_enable = false;
200 current_command = 0;
201 for (int i = 0; i < PLAYER_NUMBER; i++) {
202 tas_data[i].buttons = 0;
203 tas_data[i].axis = {};
204 }
205 }
206 } else {
207 for (int i = 0; i < PLAYER_NUMBER; i++) {
208 tas_data[i].buttons = 0;
209 tas_data[i].axis = {};
210 }
211 }
212 }
213 LOG_DEBUG(Input, "TAS inputs: {}", debugInputs(tas_data));
214}
215
216TasAnalog Tas::ReadCommandAxis(const std::string line) const {
217 std::stringstream linestream(line);
218 std::string segment;
219 std::vector<std::string> seglist;
220
221 while (std::getline(linestream, segment, ';')) {
222 seglist.push_back(segment);
223 }
224
225 const float x = std::stof(seglist.at(0)) / 32767.f;
226 const float y = std::stof(seglist.at(1)) / 32767.f;
227
228 return {x, y};
229}
230
231u32 Tas::ReadCommandButtons(const std::string data) const {
232 std::stringstream button_text(data);
233 std::string line;
234 u32 buttons = 0;
235 while (std::getline(button_text, line, ';')) {
236 for (auto [text, tas_button] : text_to_tas_button) {
237 if (text == line) {
238 buttons |= static_cast<u32>(tas_button);
239 break;
240 }
241 }
242 }
243 return buttons;
244}
245
246std::string Tas::WriteCommandAxis(TasAnalog data) const {
247 auto [x, y] = data;
248 std::string line;
249 line += std::to_string(static_cast<int>(x * 32767));
250 line += ";";
251 line += std::to_string(static_cast<int>(y * 32767));
252 return line;
253}
254
255std::string Tas::WriteCommandButtons(u32 data) const {
256 if (data == 0)
257 return "NONE";
258
259 std::string line;
260 u32 index = 0;
261 while (data > 0) {
262 if ((data & 1) == 1) {
263 for (auto [text, tas_button] : text_to_tas_button) {
264 if (tas_button == static_cast<TasButton>(1 << index)) {
265 if (line.size() > 0)
266 line += ";";
267 line += text;
268 break;
269 }
270 }
271 }
272 index++;
273 data >>= 1;
274 }
275 return line;
276}
277
278InputCommon::ButtonMapping Tas::GetButtonMappingForDevice(
279 const Common::ParamPackage& params) const {
280 // This list is missing ZL/ZR since those are not considered buttons.
281 // We will add those afterwards
282 // This list also excludes any button that can't be really mapped
283 static constexpr std::array<std::pair<Settings::NativeButton::Values, TasButton>, 20>
284 switch_to_tas_button = {
285 std::pair{Settings::NativeButton::A, TasButton::BUTTON_A},
286 {Settings::NativeButton::B, TasButton::BUTTON_B},
287 {Settings::NativeButton::X, TasButton::BUTTON_X},
288 {Settings::NativeButton::Y, TasButton::BUTTON_Y},
289 {Settings::NativeButton::LStick, TasButton::STICK_L},
290 {Settings::NativeButton::RStick, TasButton::STICK_R},
291 {Settings::NativeButton::L, TasButton::TRIGGER_L},
292 {Settings::NativeButton::R, TasButton::TRIGGER_R},
293 {Settings::NativeButton::Plus, TasButton::BUTTON_PLUS},
294 {Settings::NativeButton::Minus, TasButton::BUTTON_MINUS},
295 {Settings::NativeButton::DLeft, TasButton::BUTTON_LEFT},
296 {Settings::NativeButton::DUp, TasButton::BUTTON_UP},
297 {Settings::NativeButton::DRight, TasButton::BUTTON_RIGHT},
298 {Settings::NativeButton::DDown, TasButton::BUTTON_DOWN},
299 {Settings::NativeButton::SL, TasButton::BUTTON_SL},
300 {Settings::NativeButton::SR, TasButton::BUTTON_SR},
301 {Settings::NativeButton::Screenshot, TasButton::BUTTON_CAPTURE},
302 {Settings::NativeButton::Home, TasButton::BUTTON_HOME},
303 {Settings::NativeButton::ZL, TasButton::TRIGGER_ZL},
304 {Settings::NativeButton::ZR, TasButton::TRIGGER_ZR},
305 };
306
307 InputCommon::ButtonMapping mapping{};
308 for (const auto& [switch_button, tas_button] : switch_to_tas_button) {
309 Common::ParamPackage button_params({{"engine", "tas"}});
310 button_params.Set("pad", params.Get("pad", 0));
311 button_params.Set("button", static_cast<int>(tas_button));
312 mapping.insert_or_assign(switch_button, std::move(button_params));
313 }
314
315 return mapping;
316}
317
318InputCommon::AnalogMapping Tas::GetAnalogMappingForDevice(
319 const Common::ParamPackage& params) const {
320
321 InputCommon::AnalogMapping mapping = {};
322 Common::ParamPackage left_analog_params;
323 left_analog_params.Set("engine", "tas");
324 left_analog_params.Set("pad", params.Get("pad", 0));
325 left_analog_params.Set("axis_x", static_cast<int>(TasAxes::StickX));
326 left_analog_params.Set("axis_y", static_cast<int>(TasAxes::StickY));
327 mapping.insert_or_assign(Settings::NativeAnalog::LStick, std::move(left_analog_params));
328 Common::ParamPackage right_analog_params;
329 right_analog_params.Set("engine", "tas");
330 right_analog_params.Set("pad", params.Get("pad", 0));
331 right_analog_params.Set("axis_x", static_cast<int>(TasAxes::SubstickX));
332 right_analog_params.Set("axis_y", static_cast<int>(TasAxes::SubstickY));
333 mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params));
334 return mapping;
335}
336
337const TasData& Tas::GetTasState(std::size_t pad) const {
338 return tas_data[pad];
339}
340} // namespace TasInput