2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
chore: make yuzu REUSE compliant
[REUSE] is a specification that aims at making file copyright
information consistent, so that it can be both human and machine
readable. It basically requires that all files have a header containing
copyright and licensing information. When this isn't possible, like
when dealing with binary assets, generated files or embedded third-party
dependencies, it is permitted to insert copyright information in the
`.reuse/dep5` file.
Oh, and it also requires that all the licenses used in the project are
present in the `LICENSES` folder, that's why the diff is so huge.
This can be done automatically with `reuse download --all`.
The `reuse` tool also contains a handy subcommand that analyzes the
project and tells whether or not the project is (still) compliant,
`reuse lint`.
Following REUSE has a few advantages over the current approach:
- Copyright information is easy to access for users / downstream
- Files like `dist/license.md` do not need to exist anymore, as
`.reuse/dep5` is used instead
- `reuse lint` makes it easy to ensure that copyright information of
files like binary assets / images is always accurate and up to date
To add copyright information of files that didn't have it I looked up
who committed what and when, for each file. As yuzu contributors do not
have to sign a CLA or similar I couldn't assume that copyright ownership
was of the "yuzu Emulator Project", so I used the name and/or email of
the commit author instead.
[REUSE]: https://reuse.software
Follow-up to 01cf05bc75b1e47beb08937439f3ed9339e7b254
2022-05-15 02:06:02 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-09-21 00:22:07 +02:00
|
|
|
|
|
|
|
#include <cstring>
|
2022-11-21 17:31:18 +01:00
|
|
|
#include <sstream>
|
2021-09-21 00:22:07 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include "common/fs/file.h"
|
|
|
|
#include "common/fs/fs_types.h"
|
|
|
|
#include "common/fs/path_util.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/settings.h"
|
|
|
|
#include "input_common/drivers/tas_input.h"
|
|
|
|
|
|
|
|
namespace InputCommon::TasInput {
|
|
|
|
|
2021-12-13 16:41:30 +01:00
|
|
|
enum class Tas::TasAxis : u8 {
|
2021-09-21 00:22:07 +02:00
|
|
|
StickX,
|
|
|
|
StickY,
|
|
|
|
SubstickX,
|
|
|
|
SubstickY,
|
|
|
|
Undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Supported keywords and buttons from a TAS file
|
2021-11-16 00:57:41 +01:00
|
|
|
constexpr std::array<std::pair<std::string_view, TasButton>, 18> text_to_tas_button = {
|
2021-09-21 00:22:07 +02:00
|
|
|
std::pair{"KEY_A", TasButton::BUTTON_A},
|
|
|
|
{"KEY_B", TasButton::BUTTON_B},
|
|
|
|
{"KEY_X", TasButton::BUTTON_X},
|
|
|
|
{"KEY_Y", TasButton::BUTTON_Y},
|
|
|
|
{"KEY_LSTICK", TasButton::STICK_L},
|
|
|
|
{"KEY_RSTICK", TasButton::STICK_R},
|
|
|
|
{"KEY_L", TasButton::TRIGGER_L},
|
|
|
|
{"KEY_R", TasButton::TRIGGER_R},
|
|
|
|
{"KEY_PLUS", TasButton::BUTTON_PLUS},
|
|
|
|
{"KEY_MINUS", TasButton::BUTTON_MINUS},
|
|
|
|
{"KEY_DLEFT", TasButton::BUTTON_LEFT},
|
|
|
|
{"KEY_DUP", TasButton::BUTTON_UP},
|
|
|
|
{"KEY_DRIGHT", TasButton::BUTTON_RIGHT},
|
|
|
|
{"KEY_DDOWN", TasButton::BUTTON_DOWN},
|
|
|
|
{"KEY_SL", TasButton::BUTTON_SL},
|
|
|
|
{"KEY_SR", TasButton::BUTTON_SR},
|
2021-11-16 00:57:41 +01:00
|
|
|
// These buttons are disabled to avoid TAS input from activating hotkeys
|
|
|
|
// {"KEY_CAPTURE", TasButton::BUTTON_CAPTURE},
|
|
|
|
// {"KEY_HOME", TasButton::BUTTON_HOME},
|
2021-09-21 00:22:07 +02:00
|
|
|
{"KEY_ZL", TasButton::TRIGGER_ZL},
|
|
|
|
{"KEY_ZR", TasButton::TRIGGER_ZR},
|
|
|
|
};
|
|
|
|
|
2021-12-13 15:05:23 +01:00
|
|
|
Tas::Tas(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
|
2021-09-21 00:22:07 +02:00
|
|
|
for (size_t player_index = 0; player_index < PLAYER_NUMBER; player_index++) {
|
|
|
|
PadIdentifier identifier{
|
|
|
|
.guid = Common::UUID{},
|
|
|
|
.port = player_index,
|
|
|
|
.pad = 0,
|
|
|
|
};
|
|
|
|
PreSetController(identifier);
|
|
|
|
}
|
|
|
|
ClearInput();
|
|
|
|
if (!Settings::values.tas_enable) {
|
|
|
|
needs_reset = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LoadTasFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
Tas::~Tas() {
|
|
|
|
Stop();
|
2021-12-13 17:20:23 +01:00
|
|
|
}
|
2021-09-21 00:22:07 +02:00
|
|
|
|
|
|
|
void Tas::LoadTasFiles() {
|
|
|
|
script_length = 0;
|
|
|
|
for (size_t i = 0; i < commands.size(); i++) {
|
2021-11-03 05:50:30 +01:00
|
|
|
LoadTasFile(i, 0);
|
2021-09-21 00:22:07 +02:00
|
|
|
if (commands[i].size() > script_length) {
|
|
|
|
script_length = commands[i].size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 05:50:30 +01:00
|
|
|
void Tas::LoadTasFile(size_t player_index, size_t file_index) {
|
2021-12-13 17:19:24 +01:00
|
|
|
commands[player_index].clear();
|
|
|
|
|
2021-11-03 05:50:30 +01:00
|
|
|
std::string file = Common::FS::ReadStringFromFile(
|
|
|
|
Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) /
|
|
|
|
fmt::format("script{}-{}.txt", file_index, player_index + 1),
|
|
|
|
Common::FS::FileType::BinaryFile);
|
2021-12-13 16:57:51 +01:00
|
|
|
std::istringstream command_line(file);
|
2021-09-21 00:22:07 +02:00
|
|
|
std::string line;
|
|
|
|
int frame_no = 0;
|
|
|
|
while (std::getline(command_line, line, '\n')) {
|
|
|
|
if (line.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-13 17:07:26 +01:00
|
|
|
std::vector<std::string> seg_list;
|
|
|
|
{
|
|
|
|
std::istringstream line_stream(line);
|
|
|
|
std::string segment;
|
|
|
|
while (std::getline(line_stream, segment, ' ')) {
|
|
|
|
seg_list.push_back(std::move(segment));
|
|
|
|
}
|
2021-09-21 00:22:07 +02:00
|
|
|
}
|
|
|
|
|
2021-12-13 17:07:26 +01:00
|
|
|
if (seg_list.size() < 4) {
|
2021-09-21 00:22:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-08 19:10:21 +01:00
|
|
|
try {
|
|
|
|
const auto num_frames = std::stoi(seg_list[0]);
|
|
|
|
while (frame_no < num_frames) {
|
|
|
|
commands[player_index].emplace_back();
|
|
|
|
frame_no++;
|
|
|
|
}
|
|
|
|
} catch (const std::invalid_argument&) {
|
|
|
|
LOG_ERROR(Input, "Invalid argument: '{}' at command {}", seg_list[0], frame_no);
|
|
|
|
} catch (const std::out_of_range&) {
|
|
|
|
LOG_ERROR(Input, "Out of range: '{}' at command {}", seg_list[0], frame_no);
|
2021-09-21 00:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TASCommand command = {
|
2021-12-13 17:07:26 +01:00
|
|
|
.buttons = ReadCommandButtons(seg_list[1]),
|
|
|
|
.l_axis = ReadCommandAxis(seg_list[2]),
|
|
|
|
.r_axis = ReadCommandAxis(seg_list[3]),
|
2021-09-21 00:22:07 +02:00
|
|
|
};
|
|
|
|
commands[player_index].push_back(command);
|
|
|
|
frame_no++;
|
|
|
|
}
|
|
|
|
LOG_INFO(Input, "TAS file loaded! {} frames", frame_no);
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:54:05 +01:00
|
|
|
void Tas::WriteTasFile(std::u8string_view file_name) {
|
2021-09-21 00:22:07 +02:00
|
|
|
std::string output_text;
|
|
|
|
for (size_t frame = 0; frame < record_commands.size(); frame++) {
|
|
|
|
const TASCommand& line = record_commands[frame];
|
2021-10-19 07:12:24 +02:00
|
|
|
output_text += fmt::format("{} {} {} {}\n", frame, WriteCommandButtons(line.buttons),
|
2021-09-21 00:22:07 +02:00
|
|
|
WriteCommandAxis(line.l_axis), WriteCommandAxis(line.r_axis));
|
|
|
|
}
|
2021-12-13 16:54:05 +01:00
|
|
|
|
|
|
|
const auto tas_file_name = Common::FS::GetYuzuPath(Common::FS::YuzuPath::TASDir) / file_name;
|
|
|
|
const auto bytes_written =
|
|
|
|
Common::FS::WriteStringToFile(tas_file_name, Common::FS::FileType::TextFile, output_text);
|
2021-09-21 00:22:07 +02:00
|
|
|
if (bytes_written == output_text.size()) {
|
|
|
|
LOG_INFO(Input, "TAS file written to file!");
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(Input, "Writing the TAS-file has failed! {} / {} bytes written", bytes_written,
|
|
|
|
output_text.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 02:16:10 +01:00
|
|
|
void Tas::RecordInput(u64 buttons, TasAnalog left_axis, TasAnalog right_axis) {
|
2021-09-21 00:22:07 +02:00
|
|
|
last_input = {
|
|
|
|
.buttons = buttons,
|
2021-11-03 05:50:30 +01:00
|
|
|
.l_axis = left_axis,
|
|
|
|
.r_axis = right_axis,
|
2021-09-21 00:22:07 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-21 19:21:47 +01:00
|
|
|
std::tuple<TasState, size_t, std::array<size_t, PLAYER_NUMBER>> Tas::GetStatus() const {
|
2021-09-21 00:22:07 +02:00
|
|
|
TasState state;
|
2022-01-21 19:21:47 +01:00
|
|
|
std::array<size_t, PLAYER_NUMBER> lengths{0};
|
2021-09-21 00:22:07 +02:00
|
|
|
if (is_recording) {
|
2022-01-21 19:21:47 +01:00
|
|
|
lengths[0] = record_commands.size();
|
|
|
|
return {TasState::Recording, record_commands.size(), lengths};
|
2021-09-21 00:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_running) {
|
|
|
|
state = TasState::Running;
|
|
|
|
} else {
|
|
|
|
state = TasState::Stopped;
|
|
|
|
}
|
|
|
|
|
2022-01-21 19:21:47 +01:00
|
|
|
for (size_t i = 0; i < PLAYER_NUMBER; i++) {
|
|
|
|
lengths[i] = commands[i].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {state, current_command, lengths};
|
2021-09-21 00:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tas::UpdateThread() {
|
|
|
|
if (!Settings::values.tas_enable) {
|
|
|
|
if (is_running) {
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_recording) {
|
|
|
|
record_commands.push_back(last_input);
|
|
|
|
}
|
|
|
|
if (needs_reset) {
|
|
|
|
current_command = 0;
|
|
|
|
needs_reset = false;
|
|
|
|
LoadTasFiles();
|
|
|
|
LOG_DEBUG(Input, "tas_reset done");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_running) {
|
|
|
|
ClearInput();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (current_command < script_length) {
|
|
|
|
LOG_DEBUG(Input, "Playing TAS {}/{}", current_command, script_length);
|
2021-10-31 02:16:10 +01:00
|
|
|
const size_t frame = current_command++;
|
2021-09-21 00:22:07 +02:00
|
|
|
for (size_t player_index = 0; player_index < commands.size(); player_index++) {
|
|
|
|
TASCommand command{};
|
|
|
|
if (frame < commands[player_index].size()) {
|
|
|
|
command = commands[player_index][frame];
|
|
|
|
}
|
|
|
|
|
|
|
|
PadIdentifier identifier{
|
|
|
|
.guid = Common::UUID{},
|
|
|
|
.port = player_index,
|
|
|
|
.pad = 0,
|
|
|
|
};
|
2021-10-31 02:16:10 +01:00
|
|
|
for (std::size_t i = 0; i < sizeof(command.buttons) * 8; ++i) {
|
|
|
|
const bool button_status = (command.buttons & (1LLU << i)) != 0;
|
2021-09-21 00:22:07 +02:00
|
|
|
const int button = static_cast<int>(i);
|
|
|
|
SetButton(identifier, button, button_status);
|
|
|
|
}
|
2021-12-13 16:41:30 +01:00
|
|
|
SetTasAxis(identifier, TasAxis::StickX, command.l_axis.x);
|
|
|
|
SetTasAxis(identifier, TasAxis::StickY, command.l_axis.y);
|
|
|
|
SetTasAxis(identifier, TasAxis::SubstickX, command.r_axis.x);
|
|
|
|
SetTasAxis(identifier, TasAxis::SubstickY, command.r_axis.y);
|
2021-09-21 00:22:07 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
is_running = Settings::values.tas_loop.GetValue();
|
2021-11-03 05:50:30 +01:00
|
|
|
LoadTasFiles();
|
2021-09-21 00:22:07 +02:00
|
|
|
current_command = 0;
|
|
|
|
ClearInput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tas::ClearInput() {
|
|
|
|
ResetButtonState();
|
|
|
|
ResetAnalogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
TasAnalog Tas::ReadCommandAxis(const std::string& line) const {
|
2021-12-13 17:07:26 +01:00
|
|
|
std::vector<std::string> seg_list;
|
|
|
|
{
|
|
|
|
std::istringstream line_stream(line);
|
|
|
|
std::string segment;
|
|
|
|
while (std::getline(line_stream, segment, ';')) {
|
|
|
|
seg_list.push_back(std::move(segment));
|
|
|
|
}
|
2021-09-21 00:22:07 +02:00
|
|
|
}
|
|
|
|
|
2022-01-08 19:10:21 +01:00
|
|
|
if (seg_list.size() < 2) {
|
|
|
|
LOG_ERROR(Input, "Invalid axis data: '{}'", line);
|
|
|
|
return {};
|
|
|
|
}
|
2021-09-21 00:22:07 +02:00
|
|
|
|
2022-01-08 19:10:21 +01:00
|
|
|
try {
|
|
|
|
const float x = std::stof(seg_list.at(0)) / 32767.0f;
|
|
|
|
const float y = std::stof(seg_list.at(1)) / 32767.0f;
|
|
|
|
return {x, y};
|
|
|
|
} catch (const std::invalid_argument&) {
|
|
|
|
LOG_ERROR(Input, "Invalid argument: '{}'", line);
|
|
|
|
} catch (const std::out_of_range&) {
|
|
|
|
LOG_ERROR(Input, "Out of range: '{}'", line);
|
|
|
|
}
|
|
|
|
return {};
|
2021-09-21 00:22:07 +02:00
|
|
|
}
|
|
|
|
|
2021-12-13 16:49:06 +01:00
|
|
|
u64 Tas::ReadCommandButtons(const std::string& line) const {
|
2021-12-13 17:07:26 +01:00
|
|
|
std::istringstream button_text(line);
|
2021-12-13 16:49:06 +01:00
|
|
|
std::string button_line;
|
2021-10-31 02:16:10 +01:00
|
|
|
u64 buttons = 0;
|
2021-12-13 16:49:06 +01:00
|
|
|
while (std::getline(button_text, button_line, ';')) {
|
2021-12-13 17:36:48 +01:00
|
|
|
for (const auto& [text, tas_button] : text_to_tas_button) {
|
2021-12-13 16:49:06 +01:00
|
|
|
if (text == button_line) {
|
2021-10-31 02:16:10 +01:00
|
|
|
buttons |= static_cast<u64>(tas_button);
|
2021-09-21 00:22:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buttons;
|
|
|
|
}
|
|
|
|
|
2021-10-31 02:16:10 +01:00
|
|
|
std::string Tas::WriteCommandButtons(u64 buttons) const {
|
2021-12-13 16:54:05 +01:00
|
|
|
std::string returns;
|
2021-12-13 17:36:48 +01:00
|
|
|
for (const auto& [text_button, tas_button] : text_to_tas_button) {
|
2021-10-31 02:16:10 +01:00
|
|
|
if ((buttons & static_cast<u64>(tas_button)) != 0) {
|
|
|
|
returns += fmt::format("{};", text_button);
|
|
|
|
}
|
2021-09-21 00:22:07 +02:00
|
|
|
}
|
2021-10-31 02:16:10 +01:00
|
|
|
return returns.empty() ? "NONE" : returns;
|
2021-09-21 00:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Tas::WriteCommandAxis(TasAnalog analog) const {
|
|
|
|
return fmt::format("{};{}", analog.x * 32767, analog.y * 32767);
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:41:30 +01:00
|
|
|
void Tas::SetTasAxis(const PadIdentifier& identifier, TasAxis axis, f32 value) {
|
|
|
|
SetAxis(identifier, static_cast<int>(axis), value);
|
|
|
|
}
|
|
|
|
|
2021-09-21 00:22:07 +02:00
|
|
|
void Tas::StartStop() {
|
|
|
|
if (!Settings::values.tas_enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (is_running) {
|
|
|
|
Stop();
|
|
|
|
} else {
|
|
|
|
is_running = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tas::Stop() {
|
|
|
|
is_running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tas::Reset() {
|
|
|
|
if (!Settings::values.tas_enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
needs_reset = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tas::Record() {
|
|
|
|
if (!Settings::values.tas_enable) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
is_recording = !is_recording;
|
|
|
|
return is_recording;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tas::SaveRecording(bool overwrite_file) {
|
|
|
|
if (is_recording) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (record_commands.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WriteTasFile(u8"record.txt");
|
|
|
|
if (overwrite_file) {
|
|
|
|
WriteTasFile(u8"script0-1.txt");
|
|
|
|
}
|
|
|
|
needs_reset = true;
|
|
|
|
record_commands.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace InputCommon::TasInput
|