Merge pull request #266 from wheremyfoodat/amiibi

More amiibo work
This commit is contained in:
wheremyfoodat 2023-09-09 02:19:52 +03:00 committed by GitHub
commit abf01d824d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 95 additions and 4 deletions

View file

@ -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/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/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/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 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 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/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/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/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( cmrc_add_resource_library(

View file

@ -98,6 +98,7 @@ class Emulator {
void pause(); // Pause the emulator void pause(); // Pause the emulator
void togglePause(); void togglePause();
bool loadAmiibo(const std::filesystem::path& path);
bool loadROM(const std::filesystem::path& path); bool loadROM(const std::filesystem::path& path);
bool loadNCSD(const std::filesystem::path& path, ROMType type); bool loadNCSD(const std::filesystem::path& path, ROMType type);
bool load3DSX(const std::filesystem::path& path); bool load3DSX(const std::filesystem::path& path);

View file

@ -18,6 +18,11 @@ public:
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override; FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;
std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override; std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override;
Rust::Result<FormatInfo, HorizonResult> getFormatInfo(const FSPath& path) override {
Helpers::warn("Stubbed ExtSaveData::GetFormatInfo");
return Ok(FormatInfo{.size = 1_GB, .numOfDirectories = 255, .numOfFiles = 255, .duplicateData = false});
}
// Takes in a binary ExtSaveData path, outputs a combination of the backing folder with the low and high save entries of the path // Takes in a binary ExtSaveData path, outputs a combination of the backing folder with the low and high save entries of the path
// Used for identifying the archive format info files // Used for identifying the archive format info files
std::string getExtSaveDataPathFromBinary(const FSPath& path); std::string getExtSaveDataPathFromBinary(const FSPath& path);

View file

@ -72,7 +72,15 @@ public:
// Signals an event, returns true on success or false if the event does not exist // Signals an event, returns true on success or false if the event does not exist
bool signalEvent(Handle e); bool signalEvent(Handle e);
private:
void clearEvent(Handle e) {
KernelObject* object = getObject(e, KernelObjectType::Event);
if (object != nullptr) {
object->getData<Event>()->fired = false;
}
}
private:
void signalArbiter(u32 waitingAddress, s32 threadCount); void signalArbiter(u32 waitingAddress, s32 threadCount);
void sleepThread(s64 ns); void sleepThread(s64 ns);
void sleepThreadOnArbiter(u32 waitingAddress); void sleepThreadOnArbiter(u32 waitingAddress);

View file

@ -0,0 +1,15 @@
#pragma once
#include <array>
#include "helpers.hpp"
#include "io_file.hpp"
class AmiiboDevice {
public:
static constexpr size_t tagSize = 0x21C;
bool loaded = false;
std::array<u8, tagSize> raw;
void reset();
};

View file

@ -1,4 +1,7 @@
#pragma once #pragma once
#include <filesystem>
#include "amiibo_device.hpp"
#include "helpers.hpp" #include "helpers.hpp"
#include "kernel_types.hpp" #include "kernel_types.hpp"
#include "logger.hpp" #include "logger.hpp"
@ -33,6 +36,7 @@ class NFCService {
// Kernel events signaled when an NFC tag goes in and out of range respectively // Kernel events signaled when an NFC tag goes in and out of range respectively
std::optional<Handle> tagInRangeEvent, tagOutOfRangeEvent; std::optional<Handle> tagInRangeEvent, tagOutOfRangeEvent;
AmiiboDevice device;
Old3DSAdapterStatus adapterStatus; Old3DSAdapterStatus adapterStatus;
TagStatus tagStatus; TagStatus tagStatus;
bool initialized = false; bool initialized = false;
@ -49,8 +53,10 @@ class NFCService {
void startTagScanning(u32 messagePointer); void startTagScanning(u32 messagePointer);
void stopCommunication(u32 messagePointer); void stopCommunication(u32 messagePointer);
public: public:
NFCService(Memory& mem, Kernel& kernel) : mem(mem), kernel(kernel) {} NFCService(Memory& mem, Kernel& kernel) : mem(mem), kernel(kernel) {}
void reset(); void reset();
void handleSyncRequest(u32 messagePointer); void handleSyncRequest(u32 messagePointer);
bool loadAmiibo(const std::filesystem::path& path);
}; };

View file

@ -104,4 +104,5 @@ class ServiceManager {
// Input function wrappers // Input function wrappers
HIDService& getHID() { return hid; } HIDService& getHID() { return hid; }
NFCService& getNFC() { return nfc; }
}; };

View file

@ -0,0 +1,3 @@
#include "services/amiibo_device.hpp"
void AmiiboDevice::reset() { loaded = false; }

View file

@ -1,4 +1,5 @@
#include "services/nfc.hpp" #include "services/nfc.hpp"
#include "io_file.hpp"
#include "ipc.hpp" #include "ipc.hpp"
#include "kernel.hpp" #include "kernel.hpp"
@ -18,6 +19,7 @@ namespace NFCCommands {
} }
void NFCService::reset() { void NFCService::reset() {
device.reset();
tagInRangeEvent = std::nullopt; tagInRangeEvent = std::nullopt;
tagOutOfRangeEvent = std::nullopt; tagOutOfRangeEvent = std::nullopt;
@ -43,6 +45,43 @@ void NFCService::handleSyncRequest(u32 messagePointer) {
} }
} }
bool NFCService::loadAmiibo(const std::filesystem::path& path) {
IOFile file(path, "rb");
if (!initialized || tagStatus != TagStatus::Scanning) {
Helpers::warn("It's not the correct time to load an amiibo! Make sure to load amiibi when the game is searching for one!");
file.close();
return false;
}
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;
}
if (tagOutOfRangeEvent.has_value()) {
kernel.clearEvent(tagOutOfRangeEvent.value());
}
if (tagInRangeEvent.has_value()) {
kernel.signalEvent(tagInRangeEvent.value());
}
file.close();
return true;
}
void NFCService::initialize(u32 messagePointer) { void NFCService::initialize(u32 messagePointer) {
const u8 type = mem.read8(messagePointer + 4); const u8 type = mem.read8(messagePointer + 4);
log("NFC::Initialize (type = %d)\n", type); log("NFC::Initialize (type = %d)\n", type);

View file

@ -353,7 +353,14 @@ void Emulator::run() {
char* droppedDir = event.drop.file; char* droppedDir = event.drop.file;
if (droppedDir) { if (droppedDir) {
loadROM(droppedDir); const std::filesystem::path path(droppedDir);
if (path.extension() == ".amiibo") {
loadAmiibo(path);
} else {
loadROM(path);
}
SDL_free(droppedDir); SDL_free(droppedDir);
} }
break; break;
@ -478,6 +485,11 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
return success; return success;
} }
bool Emulator::loadAmiibo(const std::filesystem::path& path) {
NFCService& nfc = kernel.getServiceManager().getNFC();
return nfc.loadAmiibo(path);
}
// Used for loading both CXI and NCSD files since they are both so similar and use the same interface // Used for loading both CXI and NCSD files since they are both so similar and use the same interface
// (We promote CXI files to NCSD internally for ease) // (We promote CXI files to NCSD internally for ease)
bool Emulator::loadNCSD(const std::filesystem::path& path, ROMType type) { bool Emulator::loadNCSD(const std::filesystem::path& path, ROMType type) {