From 72ae5d2bfa237c75adccae4119a9ac5dcd4a8e01 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Fri, 8 Sep 2023 23:16:33 +0300 Subject: [PATCH] Add reading amiibo from .amiibo file --- CMakeLists.txt | 3 ++- include/services/amiibo_device.hpp | 15 +++++++++++++++ include/services/nfc.hpp | 2 ++ src/core/services/amiibo_device.cpp | 3 +++ src/core/services/nfc.cpp | 24 +++++++++++++++++++++++- 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 include/services/amiibo_device.hpp create mode 100644 src/core/services/amiibo_device.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d0ac2804..b4ca6745 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -137,7 +137,7 @@ set(SERVICE_SOURCE_FILES src/core/services/service_manager.cpp src/core/services src/core/services/y2r.cpp src/core/services/cam.cpp src/core/services/ldr_ro.cpp src/core/services/act.cpp src/core/services/nfc.cpp src/core/services/dlp_srvr.cpp src/core/services/ir_user.cpp src/core/services/http.cpp src/core/services/soc.cpp - src/core/services/ssl.cpp src/core/services/news_u.cpp + src/core/services/ssl.cpp src/core/services/news_u.cpp src/core/services/amiibo_device.cpp ) set(PICA_SOURCE_FILES src/core/PICA/gpu.cpp src/core/PICA/regs.cpp src/core/PICA/shader_unit.cpp src/core/PICA/shader_interpreter.cpp src/core/PICA/dynapica/shader_rec.cpp @@ -181,6 +181,7 @@ set(HEADER_FILES include/emulator.hpp include/helpers.hpp include/termcolor.hpp include/fs/romfs.hpp include/fs/ivfc.hpp include/discord_rpc.hpp include/services/http.hpp include/result/result_cfg.hpp include/applets/applet.hpp include/applets/mii_selector.hpp include/math_util.hpp include/services/soc.hpp include/services/news_u.hpp include/applets/software_keyboard.hpp include/applets/applet_manager.hpp include/fs/archive_user_save_data.hpp + include/services/amiibo_device.hpp ) cmrc_add_resource_library( diff --git a/include/services/amiibo_device.hpp b/include/services/amiibo_device.hpp new file mode 100644 index 00000000..0b9f21e1 --- /dev/null +++ b/include/services/amiibo_device.hpp @@ -0,0 +1,15 @@ +#pragma once +#include + +#include "helpers.hpp" +#include "io_file.hpp" + +class AmiiboDevice { + public: + static constexpr size_t tagSize = 0x21C; + + bool loaded = false; + std::array raw; + + void reset(); +}; \ No newline at end of file diff --git a/include/services/nfc.hpp b/include/services/nfc.hpp index 94667f21..151d8408 100644 --- a/include/services/nfc.hpp +++ b/include/services/nfc.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include "amiibo_device.hpp" #include "helpers.hpp" #include "kernel_types.hpp" #include "logger.hpp" @@ -35,6 +36,7 @@ class NFCService { // Kernel events signaled when an NFC tag goes in and out of range respectively std::optional tagInRangeEvent, tagOutOfRangeEvent; + AmiiboDevice device; Old3DSAdapterStatus adapterStatus; TagStatus tagStatus; bool initialized = false; diff --git a/src/core/services/amiibo_device.cpp b/src/core/services/amiibo_device.cpp new file mode 100644 index 00000000..58a0e2c6 --- /dev/null +++ b/src/core/services/amiibo_device.cpp @@ -0,0 +1,3 @@ +#include "services/amiibo_device.hpp" + +void AmiiboDevice::reset() { loaded = false; } \ No newline at end of file diff --git a/src/core/services/nfc.cpp b/src/core/services/nfc.cpp index 0cb3e699..9d9a37d7 100644 --- a/src/core/services/nfc.cpp +++ b/src/core/services/nfc.cpp @@ -1,4 +1,5 @@ #include "services/nfc.hpp" +#include "io_file.hpp" #include "ipc.hpp" #include "kernel.hpp" @@ -18,6 +19,7 @@ namespace NFCCommands { } void NFCService::reset() { + device.reset(); tagInRangeEvent = std::nullopt; tagOutOfRangeEvent = std::nullopt; @@ -43,7 +45,27 @@ void NFCService::handleSyncRequest(u32 messagePointer) { } } -bool NFCService::loadAmiibo(const std::filesystem::path& path) { return true; } +bool NFCService::loadAmiibo(const std::filesystem::path& path) { + IOFile file(path, "rb"); + + if (!file.isOpen()) { + printf("Failed to open Amiibo file"); + file.close(); + + return false; + } + + auto [success, bytesRead] = file.readBytes(&device.raw, AmiiboDevice::tagSize); + if (!success || bytesRead != AmiiboDevice::tagSize) { + printf("Failed to read entire tag from Amiibo file: File might not be a proper amiibo file\n"); + file.close(); + + return false; + } + + file.close(); + return true; +} void NFCService::initialize(u32 messagePointer) { const u8 type = mem.read8(messagePointer + 4);