From 35589010e655b32f096e3c4a96bbe183c0a910a7 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Fri, 12 Oct 2018 19:11:29 +1100 Subject: [PATCH] Refactored amiibo to not reside in "System" --- src/core/core.cpp | 13 ----- src/core/core.h | 6 --- src/core/hle/service/nfp/nfp.cpp | 83 ++++++++++++++++++++------------ src/core/hle/service/nfp/nfp.h | 9 ++++ src/yuzu/main.cpp | 8 ++- 5 files changed, 67 insertions(+), 52 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index b3801229db..7511eadbf4 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -471,19 +471,6 @@ std::shared_ptr System::GetFilesystem() const { return impl->virtual_filesystem; } -void System::LoadAmiibo(const std::string& filename) { - impl->nfc_filename = filename; - impl->nfc_activate->Signal(); -} - -const Kernel::SharedPtr& System::GetNFCEvent() const { - return impl->nfc_activate; -} - -const std::string& System::GetNFCFilename() const { - return impl->nfc_filename; -} - System::ResultStatus System::Init(Frontend::EmuWindow& emu_window) { return impl->Init(emu_window); } diff --git a/src/core/core.h b/src/core/core.h index 04500e6ee6..4a40d4fea6 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -225,12 +225,6 @@ public: std::shared_ptr GetFilesystem() const; - const std::string& GetNFCFilename() const; - - void LoadAmiibo(const std::string& path); - - const Kernel::SharedPtr& GetNFCEvent() const; - private: System(); diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 49056ee3d9..7680caae08 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -19,13 +19,18 @@ constexpr ResultCode ERR_TAG_FAILED(ErrorModule::NFP, } Module::Interface::Interface(std::shared_ptr module, const char* name) - : ServiceFramework(name), module(std::move(module)) {} + : ServiceFramework(name), module(std::move(module)) { + auto& kernel = Core::System::GetInstance().Kernel(); + nfc_tag_load = + Kernel::Event::Create(kernel, Kernel::ResetType::OneShot, "IUser:NFCTagDetected"); +} Module::Interface::~Interface() = default; class IUser final : public ServiceFramework { public: - IUser() : ServiceFramework("NFP::IUser") { + IUser(Module::Interface& nfp_interface) + : ServiceFramework("NFP::IUser"), nfp_interface(nfp_interface) { static const FunctionInfo functions[] = { {0, &IUser::Initialize, "Initialize"}, {1, &IUser::Finalize, "Finalize"}, @@ -63,6 +68,7 @@ public: } private: + const Module::Interface& nfp_interface; enum class State : u32 { NonInitialized = 0, Initialized = 1, @@ -149,12 +155,9 @@ private: IPC::RequestParser rp{ctx}; const u64 dev_handle = rp.Pop(); LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); - IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - - Core::System& system{Core::System::GetInstance()}; - rb.PushCopyObjects(system.GetNFCEvent()); + rb.PushCopyObjects(nfp_interface.GetNFCEvent()); has_attached_handle = true; } @@ -189,7 +192,7 @@ private: LOG_DEBUG(Service_NFP, "called"); Core::System& system{Core::System::GetInstance()}; - const auto event = system.GetNFCEvent(); + const auto event = nfp_interface.GetNFCEvent(); if (!event->ShouldWait(Kernel::GetCurrentThread()) && !has_attached_handle) { device_state = DeviceState::TagFound; @@ -214,22 +217,19 @@ private: void GetTagInfo(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NFP, "called"); IPC::ResponseBuilder rb{ctx, 2}; + const auto& amiibo_buf = nfp_interface.GetAmiiboBuffer(); + if (amiibo_buf.size() >= sizeof(TagInfo)) { + TagInfo tag_info{}; + std::memcpy(tag_info.uuid.data(), amiibo_buf.data(), sizeof(tag_info.uuid.size())); + tag_info.uuid_length = static_cast(tag_info.uuid.size()); - Core::System& system{Core::System::GetInstance()}; - auto nfc_file = FileUtil::IOFile(system.GetNFCFilename(), "rb"); - if (!nfc_file.IsOpen()) { + tag_info.protocol = 1; // TODO(ogniK): Figure out actual values + tag_info.tag_type = 2; + ctx.WriteBuffer(&tag_info, sizeof(TagInfo)); + rb.Push(RESULT_SUCCESS); + } else { rb.Push(ErrCodes::ERR_TAG_FAILED); - return; } - TagInfo tag_info{}; - size_t read_length = nfc_file.ReadBytes(tag_info.uuid.data(), sizeof(tag_info.uuid.size())); - tag_info.uuid_length = static_cast(read_length); - - tag_info.protocol = 1; // TODO(ogniK): Figure out actual values - tag_info.tag_type = 2; - - ctx.WriteBuffer(&tag_info, sizeof(TagInfo)); - rb.Push(RESULT_SUCCESS); } void Mount(Kernel::HLERequestContext& ctx) { @@ -242,20 +242,19 @@ private: void GetModelInfo(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NFP, "called"); - - Core::System& system{Core::System::GetInstance()}; - auto nfc_file = FileUtil::IOFile(system.GetNFCFilename(), "rb"); + const size_t amiibo_identification_offset = 0x54; + const auto& amiibo_buf = nfp_interface.GetAmiiboBuffer(); IPC::ResponseBuilder rb{ctx, 2}; - if (!nfc_file.IsOpen()) { + if (amiibo_buf.size() >= amiibo_identification_offset + sizeof(ModelInfo)) { + ModelInfo model_info{}; + std::memcpy(model_info.amiibo_identification_block.data(), + amiibo_buf.data() + amiibo_identification_offset, + model_info.amiibo_identification_block.size()); + ctx.WriteBuffer(&model_info, sizeof(ModelInfo)); + rb.Push(RESULT_SUCCESS); + } else { rb.Push(ErrCodes::ERR_TAG_FAILED); - return; } - nfc_file.Seek(0x54, SEEK_SET); - ModelInfo model_info{}; - nfc_file.ReadBytes(model_info.amiibo_identification_block.data(), - model_info.amiibo_identification_block.size()); - ctx.WriteBuffer(&model_info, sizeof(ModelInfo)); - rb.Push(RESULT_SUCCESS); } void Unmount(Kernel::HLERequestContext& ctx) { @@ -344,7 +343,27 @@ void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NFP, "called"); IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(RESULT_SUCCESS); - rb.PushIpcInterface(); + rb.PushIpcInterface(*this); +} + +void Module::Interface::LoadAmiibo(const std::string& filename) { + amiibo_buffer.clear(); + auto nfc_file = FileUtil::IOFile(filename, "rb"); + if (!nfc_file.IsOpen()) { + return; + } + amiibo_buffer.resize(nfc_file.GetSize()); + nfc_file.ReadBytes(amiibo_buffer.data(), amiibo_buffer.size()); + nfc_file.Close(); + nfc_tag_load->Signal(); +} + +const Kernel::SharedPtr& Module::Interface::GetNFCEvent() const { + return nfc_tag_load; +} + +const std::vector& Module::Interface::GetAmiiboBuffer() const { + return amiibo_buffer; } void InstallInterfaces(SM::ServiceManager& service_manager) { diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index 77df343c4a..25eec824b5 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -4,6 +4,8 @@ #pragma once +#include +#include "core/hle/kernel/event.h" #include "core/hle/service/service.h" namespace Service::NFP { @@ -16,6 +18,13 @@ public: ~Interface() override; void CreateUserInterface(Kernel::HLERequestContext& ctx); + void LoadAmiibo(const std::string& path); + const Kernel::SharedPtr& GetNFCEvent() const; + const std::vector& GetAmiiboBuffer() const; + + private: + Kernel::SharedPtr nfc_tag_load{}; + std::vector amiibo_buffer{}; protected: std::shared_ptr module; diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index fb840a5fbe..5e0367ced5 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -60,6 +60,8 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual #include "core/hle/kernel/process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/hle/service/filesystem/fsp_ldr.h" +#include "core/hle/service/nfp/nfp.h" +#include "core/hle/service/sm/sm.h" #include "core/loader/loader.h" #include "core/perf_stats.h" #include "core/settings.h" @@ -1289,7 +1291,11 @@ void GMainWindow::OnLoadAmiibo() { this, tr("Load Amiibo"), UISettings::values.amiibo_path, file_filter); if (!filename.isEmpty()) { Core::System& system{Core::System::GetInstance()}; - system.LoadAmiibo(filename.toStdString()); + Service::SM::ServiceManager& sm = system.ServiceManager(); + auto nfc = sm.GetService("nfp:user"); + if (nfc != nullptr) { + nfc->LoadAmiibo(filename.toStdString()); + } } }