summaryrefslogtreecommitdiff
path: root/src/input_common/drivers/tas_input.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2021-12-13 14:47:24 -0800
committerGravatar GitHub2021-12-13 14:47:24 -0800
commit7276aaf907fbfa80da20c3311934e135b7a7de85 (patch)
treeb8dae22038a45e49c8e318a8991e2f6e89ad752c /src/input_common/drivers/tas_input.cpp
parentMerge pull request #7574 from v1993/patch-1 (diff)
parenttas_input: Avoid minor copies in Read/WriteCommandButtons() (diff)
downloadyuzu-7276aaf907fbfa80da20c3311934e135b7a7de85.tar.gz
yuzu-7276aaf907fbfa80da20c3311934e135b7a7de85.tar.xz
yuzu-7276aaf907fbfa80da20c3311934e135b7a7de85.zip
Merge pull request #7576 from lioncash/tasenum
tas_input: Minor cleanup
Diffstat (limited to 'src/input_common/drivers/tas_input.cpp')
-rw-r--r--src/input_common/drivers/tas_input.cpp93
1 files changed, 49 insertions, 44 deletions
diff --git a/src/input_common/drivers/tas_input.cpp b/src/input_common/drivers/tas_input.cpp
index 0e01fb0d9..2094c1feb 100644
--- a/src/input_common/drivers/tas_input.cpp
+++ b/src/input_common/drivers/tas_input.cpp
@@ -3,7 +3,6 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <cstring> 5#include <cstring>
6#include <regex>
7#include <fmt/format.h> 6#include <fmt/format.h>
8 7
9#include "common/fs/file.h" 8#include "common/fs/file.h"
@@ -15,7 +14,7 @@
15 14
16namespace InputCommon::TasInput { 15namespace InputCommon::TasInput {
17 16
18enum TasAxes : u8 { 17enum class Tas::TasAxis : u8 {
19 StickX, 18 StickX,
20 StickY, 19 StickY,
21 SubstickX, 20 SubstickX,
@@ -66,7 +65,7 @@ Tas::Tas(const std::string& input_engine_) : InputCommon::InputEngine(input_engi
66 65
67Tas::~Tas() { 66Tas::~Tas() {
68 Stop(); 67 Stop();
69}; 68}
70 69
71void Tas::LoadTasFiles() { 70void Tas::LoadTasFiles() {
72 script_length = 0; 71 script_length = 0;
@@ -79,43 +78,43 @@ void Tas::LoadTasFiles() {
79} 78}
80 79
81void Tas::LoadTasFile(size_t player_index, size_t file_index) { 80void Tas::LoadTasFile(size_t player_index, size_t file_index) {
82 if (!commands[player_index].empty()) { 81 commands[player_index].clear();
83 commands[player_index].clear(); 82
84 }
85 std::string file = Common::FS::ReadStringFromFile( 83 std::string file = Common::FS::ReadStringFromFile(
86 Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) / 84 Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) /
87 fmt::format("script{}-{}.txt", file_index, player_index + 1), 85 fmt::format("script{}-{}.txt", file_index, player_index + 1),
88 Common::FS::FileType::BinaryFile); 86 Common::FS::FileType::BinaryFile);
89 std::stringstream command_line(file); 87 std::istringstream command_line(file);
90 std::string line; 88 std::string line;
91 int frame_no = 0; 89 int frame_no = 0;
92 while (std::getline(command_line, line, '\n')) { 90 while (std::getline(command_line, line, '\n')) {
93 if (line.empty()) { 91 if (line.empty()) {
94 continue; 92 continue;
95 } 93 }
96 std::smatch m;
97 94
98 std::stringstream linestream(line); 95 std::vector<std::string> seg_list;
99 std::string segment; 96 {
100 std::vector<std::string> seglist; 97 std::istringstream line_stream(line);
101 98 std::string segment;
102 while (std::getline(linestream, segment, ' ')) { 99 while (std::getline(line_stream, segment, ' ')) {
103 seglist.push_back(segment); 100 seg_list.push_back(std::move(segment));
101 }
104 } 102 }
105 103
106 if (seglist.size() < 4) { 104 if (seg_list.size() < 4) {
107 continue; 105 continue;
108 } 106 }
109 107
110 while (frame_no < std::stoi(seglist.at(0))) { 108 const auto num_frames = std::stoi(seg_list[0]);
111 commands[player_index].push_back({}); 109 while (frame_no < num_frames) {
110 commands[player_index].emplace_back();
112 frame_no++; 111 frame_no++;
113 } 112 }
114 113
115 TASCommand command = { 114 TASCommand command = {
116 .buttons = ReadCommandButtons(seglist.at(1)), 115 .buttons = ReadCommandButtons(seg_list[1]),
117 .l_axis = ReadCommandAxis(seglist.at(2)), 116 .l_axis = ReadCommandAxis(seg_list[2]),
118 .r_axis = ReadCommandAxis(seglist.at(3)), 117 .r_axis = ReadCommandAxis(seg_list[3]),
119 }; 118 };
120 commands[player_index].push_back(command); 119 commands[player_index].push_back(command);
121 frame_no++; 120 frame_no++;
@@ -123,16 +122,17 @@ void Tas::LoadTasFile(size_t player_index, size_t file_index) {
123 LOG_INFO(Input, "TAS file loaded! {} frames", frame_no); 122 LOG_INFO(Input, "TAS file loaded! {} frames", frame_no);
124} 123}
125 124
126void Tas::WriteTasFile(std::u8string file_name) { 125void Tas::WriteTasFile(std::u8string_view file_name) {
127 std::string output_text; 126 std::string output_text;
128 for (size_t frame = 0; frame < record_commands.size(); frame++) { 127 for (size_t frame = 0; frame < record_commands.size(); frame++) {
129 const TASCommand& line = record_commands[frame]; 128 const TASCommand& line = record_commands[frame];
130 output_text += fmt::format("{} {} {} {}\n", frame, WriteCommandButtons(line.buttons), 129 output_text += fmt::format("{} {} {} {}\n", frame, WriteCommandButtons(line.buttons),
131 WriteCommandAxis(line.l_axis), WriteCommandAxis(line.r_axis)); 130 WriteCommandAxis(line.l_axis), WriteCommandAxis(line.r_axis));
132 } 131 }
133 const auto bytes_written = Common::FS::WriteStringToFile( 132
134 Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) / file_name, 133 const auto tas_file_name = Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) / file_name;
135 Common::FS::FileType::TextFile, output_text); 134 const auto bytes_written =
135 Common::FS::WriteStringToFile(tas_file_name, Common::FS::FileType::TextFile, output_text);
136 if (bytes_written == output_text.size()) { 136 if (bytes_written == output_text.size()) {
137 LOG_INFO(Input, "TAS file written to file!"); 137 LOG_INFO(Input, "TAS file written to file!");
138 } else { 138 } else {
@@ -205,10 +205,10 @@ void Tas::UpdateThread() {
205 const int button = static_cast<int>(i); 205 const int button = static_cast<int>(i);
206 SetButton(identifier, button, button_status); 206 SetButton(identifier, button, button_status);
207 } 207 }
208 SetAxis(identifier, TasAxes::StickX, command.l_axis.x); 208 SetTasAxis(identifier, TasAxis::StickX, command.l_axis.x);
209 SetAxis(identifier, TasAxes::StickY, command.l_axis.y); 209 SetTasAxis(identifier, TasAxis::StickY, command.l_axis.y);
210 SetAxis(identifier, TasAxes::SubstickX, command.r_axis.x); 210 SetTasAxis(identifier, TasAxis::SubstickX, command.r_axis.x);
211 SetAxis(identifier, TasAxes::SubstickY, command.r_axis.y); 211 SetTasAxis(identifier, TasAxis::SubstickY, command.r_axis.y);
212 } 212 }
213 } else { 213 } else {
214 is_running = Settings::values.tas_loop.GetValue(); 214 is_running = Settings::values.tas_loop.GetValue();
@@ -224,27 +224,28 @@ void Tas::ClearInput() {
224} 224}
225 225
226TasAnalog Tas::ReadCommandAxis(const std::string& line) const { 226TasAnalog Tas::ReadCommandAxis(const std::string& line) const {
227 std::stringstream linestream(line); 227 std::vector<std::string> seg_list;
228 std::string segment; 228 {
229 std::vector<std::string> seglist; 229 std::istringstream line_stream(line);
230 230 std::string segment;
231 while (std::getline(linestream, segment, ';')) { 231 while (std::getline(line_stream, segment, ';')) {
232 seglist.push_back(segment); 232 seg_list.push_back(std::move(segment));
233 }
233 } 234 }
234 235
235 const float x = std::stof(seglist.at(0)) / 32767.0f; 236 const float x = std::stof(seg_list.at(0)) / 32767.0f;
236 const float y = std::stof(seglist.at(1)) / 32767.0f; 237 const float y = std::stof(seg_list.at(1)) / 32767.0f;
237 238
238 return {x, y}; 239 return {x, y};
239} 240}
240 241
241u64 Tas::ReadCommandButtons(const std::string& data) const { 242u64 Tas::ReadCommandButtons(const std::string& line) const {
242 std::stringstream button_text(data); 243 std::istringstream button_text(line);
243 std::string line; 244 std::string button_line;
244 u64 buttons = 0; 245 u64 buttons = 0;
245 while (std::getline(button_text, line, ';')) { 246 while (std::getline(button_text, button_line, ';')) {
246 for (auto [text, tas_button] : text_to_tas_button) { 247 for (const auto& [text, tas_button] : text_to_tas_button) {
247 if (text == line) { 248 if (text == button_line) {
248 buttons |= static_cast<u64>(tas_button); 249 buttons |= static_cast<u64>(tas_button);
249 break; 250 break;
250 } 251 }
@@ -254,8 +255,8 @@ u64 Tas::ReadCommandButtons(const std::string& data) const {
254} 255}
255 256
256std::string Tas::WriteCommandButtons(u64 buttons) const { 257std::string Tas::WriteCommandButtons(u64 buttons) const {
257 std::string returns = ""; 258 std::string returns;
258 for (auto [text_button, tas_button] : text_to_tas_button) { 259 for (const auto& [text_button, tas_button] : text_to_tas_button) {
259 if ((buttons & static_cast<u64>(tas_button)) != 0) { 260 if ((buttons & static_cast<u64>(tas_button)) != 0) {
260 returns += fmt::format("{};", text_button); 261 returns += fmt::format("{};", text_button);
261 } 262 }
@@ -267,6 +268,10 @@ std::string Tas::WriteCommandAxis(TasAnalog analog) const {
267 return fmt::format("{};{}", analog.x * 32767, analog.y * 32767); 268 return fmt::format("{};{}", analog.x * 32767, analog.y * 32767);
268} 269}
269 270
271void Tas::SetTasAxis(const PadIdentifier& identifier, TasAxis axis, f32 value) {
272 SetAxis(identifier, static_cast<int>(axis), value);
273}
274
270void Tas::StartStop() { 275void Tas::StartStop() {
271 if (!Settings::values.tas_enable) { 276 if (!Settings::values.tas_enable) {
272 return; 277 return;