2022-09-25 00:46:49 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include "common/fs/file.h"
|
|
|
|
#include "common/fs/fs.h"
|
|
|
|
#include "common/fs/path_util.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/settings.h"
|
|
|
|
#include "input_common/drivers/virtual_amiibo.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
constexpr PadIdentifier identifier = {
|
|
|
|
.guid = Common::UUID{},
|
|
|
|
.port = 0,
|
|
|
|
.pad = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
VirtualAmiibo::VirtualAmiibo(std::string input_engine_) : InputEngine(std::move(input_engine_)) {}
|
|
|
|
|
2022-09-25 03:52:33 +00:00
|
|
|
VirtualAmiibo::~VirtualAmiibo() = default;
|
2022-09-25 00:46:49 +00:00
|
|
|
|
2022-12-26 17:11:01 +00:00
|
|
|
Common::Input::DriverResult VirtualAmiibo::SetPollingMode(
|
2022-09-25 00:46:49 +00:00
|
|
|
[[maybe_unused]] const PadIdentifier& identifier_,
|
|
|
|
const Common::Input::PollingMode polling_mode_) {
|
|
|
|
polling_mode = polling_mode_;
|
|
|
|
|
2022-12-26 18:49:49 +00:00
|
|
|
switch (polling_mode) {
|
|
|
|
case Common::Input::PollingMode::NFC:
|
2022-09-25 00:46:49 +00:00
|
|
|
if (state == State::Initialized) {
|
|
|
|
state = State::WaitingForAmiibo;
|
|
|
|
}
|
2022-12-26 18:49:49 +00:00
|
|
|
return Common::Input::DriverResult::Success;
|
|
|
|
default:
|
2022-09-25 00:46:49 +00:00
|
|
|
if (state == State::AmiiboIsOpen) {
|
|
|
|
CloseAmiibo();
|
|
|
|
}
|
2022-12-26 18:49:49 +00:00
|
|
|
return Common::Input::DriverResult::NotSupported;
|
2022-09-25 00:46:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Input::NfcState VirtualAmiibo::SupportsNfc(
|
2022-09-25 03:52:33 +00:00
|
|
|
[[maybe_unused]] const PadIdentifier& identifier_) const {
|
2022-09-25 00:46:49 +00:00
|
|
|
return Common::Input::NfcState::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Input::NfcState VirtualAmiibo::WriteNfcData(
|
|
|
|
[[maybe_unused]] const PadIdentifier& identifier_, const std::vector<u8>& data) {
|
2022-12-02 01:42:39 +00:00
|
|
|
const Common::FS::IOFile nfc_file{file_path, Common::FS::FileAccessMode::ReadWrite,
|
|
|
|
Common::FS::FileType::BinaryFile};
|
2022-09-25 00:46:49 +00:00
|
|
|
|
2022-12-02 01:42:39 +00:00
|
|
|
if (!nfc_file.IsOpen()) {
|
2022-09-25 00:46:49 +00:00
|
|
|
LOG_ERROR(Core, "Amiibo is already on use");
|
|
|
|
return Common::Input::NfcState::WriteFailed;
|
|
|
|
}
|
|
|
|
|
2022-12-02 01:42:39 +00:00
|
|
|
if (!nfc_file.Write(data)) {
|
2023-03-12 03:10:38 +00:00
|
|
|
LOG_ERROR(Service_NFP, "Error writing to file");
|
2022-09-25 00:46:49 +00:00
|
|
|
return Common::Input::NfcState::WriteFailed;
|
|
|
|
}
|
|
|
|
|
2022-12-02 01:42:39 +00:00
|
|
|
nfc_data = data;
|
2022-11-13 16:56:01 +00:00
|
|
|
|
2022-09-25 00:46:49 +00:00
|
|
|
return Common::Input::NfcState::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualAmiibo::State VirtualAmiibo::GetCurrentState() const {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualAmiibo::Info VirtualAmiibo::LoadAmiibo(const std::string& filename) {
|
2022-12-02 01:42:39 +00:00
|
|
|
const Common::FS::IOFile nfc_file{filename, Common::FS::FileAccessMode::Read,
|
|
|
|
Common::FS::FileType::BinaryFile};
|
2023-03-22 17:09:12 +00:00
|
|
|
std::vector<u8> data{};
|
2022-09-25 00:46:49 +00:00
|
|
|
|
2022-12-02 01:42:39 +00:00
|
|
|
if (!nfc_file.IsOpen()) {
|
2022-09-25 00:46:49 +00:00
|
|
|
return Info::UnableToLoad;
|
|
|
|
}
|
|
|
|
|
2022-12-02 01:42:39 +00:00
|
|
|
switch (nfc_file.GetSize()) {
|
|
|
|
case AmiiboSize:
|
|
|
|
case AmiiboSizeWithoutPassword:
|
2023-06-15 23:39:01 +00:00
|
|
|
case AmiiboSizeWithSignature:
|
2023-06-01 05:51:27 +00:00
|
|
|
data.resize(AmiiboSize);
|
|
|
|
if (nfc_file.Read(data) < AmiiboSizeWithoutPassword) {
|
2022-12-02 01:42:39 +00:00
|
|
|
return Info::NotAnAmiibo;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MifareSize:
|
2023-06-01 05:51:27 +00:00
|
|
|
data.resize(MifareSize);
|
|
|
|
if (nfc_file.Read(data) < MifareSize) {
|
2022-12-02 01:42:39 +00:00
|
|
|
return Info::NotAnAmiibo;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-09-25 00:46:49 +00:00
|
|
|
return Info::NotAnAmiibo;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_path = filename;
|
2023-03-22 17:09:12 +00:00
|
|
|
return LoadAmiibo(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualAmiibo::Info VirtualAmiibo::LoadAmiibo(std::span<u8> data) {
|
|
|
|
if (state != State::WaitingForAmiibo) {
|
|
|
|
return Info::WrongDeviceState;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (data.size_bytes()) {
|
2023-05-31 03:50:09 +00:00
|
|
|
case AmiiboSize:
|
|
|
|
case AmiiboSizeWithoutPassword:
|
2023-06-15 23:39:01 +00:00
|
|
|
case AmiiboSizeWithSignature:
|
2023-05-31 03:50:09 +00:00
|
|
|
nfc_data.resize(AmiiboSize);
|
|
|
|
break;
|
|
|
|
case MifareSize:
|
|
|
|
nfc_data.resize(MifareSize);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return Info::NotAnAmiibo;
|
2023-03-22 17:09:12 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 00:46:49 +00:00
|
|
|
state = State::AmiiboIsOpen;
|
2023-05-31 03:50:09 +00:00
|
|
|
memcpy(nfc_data.data(), data.data(), data.size_bytes());
|
2022-12-02 01:42:39 +00:00
|
|
|
SetNfc(identifier, {Common::Input::NfcState::NewAmiibo, nfc_data});
|
2022-09-25 00:46:49 +00:00
|
|
|
return Info::Success;
|
|
|
|
}
|
|
|
|
|
2022-11-13 16:56:01 +00:00
|
|
|
VirtualAmiibo::Info VirtualAmiibo::ReloadAmiibo() {
|
|
|
|
if (state == State::AmiiboIsOpen) {
|
2022-12-02 01:42:39 +00:00
|
|
|
SetNfc(identifier, {Common::Input::NfcState::NewAmiibo, nfc_data});
|
2022-11-13 16:56:01 +00:00
|
|
|
return Info::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LoadAmiibo(file_path);
|
|
|
|
}
|
|
|
|
|
2022-09-25 00:46:49 +00:00
|
|
|
VirtualAmiibo::Info VirtualAmiibo::CloseAmiibo() {
|
|
|
|
state = polling_mode == Common::Input::PollingMode::NFC ? State::WaitingForAmiibo
|
|
|
|
: State::Initialized;
|
|
|
|
SetNfc(identifier, {Common::Input::NfcState::AmiiboRemoved, {}});
|
|
|
|
return Info::Success;
|
|
|
|
}
|
|
|
|
|
2022-11-13 21:14:08 +00:00
|
|
|
std::string VirtualAmiibo::GetLastFilePath() const {
|
2022-11-13 16:56:01 +00:00
|
|
|
return file_path;
|
|
|
|
}
|
|
|
|
|
2022-09-25 00:46:49 +00:00
|
|
|
} // namespace InputCommon
|