From 177ad185e0a7e653e83be18561348f7fd934861d Mon Sep 17 00:00:00 2001 From: BernardoGiordano Date: Fri, 13 Dec 2019 21:51:36 +0100 Subject: [PATCH] Let checkpoint compile with libnx 3.0.0 --- switch/include/account.hpp | 34 ++++++++++++++++++---- switch/include/filesystem.hpp | 4 +-- switch/include/io.hpp | 5 ++-- switch/include/main.hpp | 5 ++-- switch/include/pksmbridge.hpp | 5 ++-- switch/include/title.hpp | 16 +++++------ switch/source/account.cpp | 34 +++++++++++----------- switch/source/filesystem.cpp | 9 ++---- switch/source/io.cpp | 18 ++++++------ switch/source/main.cpp | 4 +-- switch/source/pksmbridge.cpp | 4 +-- switch/source/title.cpp | 53 ++++++++++++++++------------------- switch/source/util.cpp | 4 +-- 13 files changed, 105 insertions(+), 90 deletions(-) diff --git a/switch/include/account.hpp b/switch/include/account.hpp index 517f5eb..bdb55cb 100644 --- a/switch/include/account.hpp +++ b/switch/include/account.hpp @@ -36,8 +36,30 @@ #define USER_ICON_SIZE 64 +namespace std { + template <> + struct hash { + size_t operator()(const AccountUid& a) const { return ((hash()(a.uid[0]) ^ (hash()(a.uid[1]) << 1)) >> 1); } + }; +} + +inline bool operator==(const AccountUid& x, const AccountUid& y) +{ + return x.uid[0] == y.uid[0] && x.uid[1] == y.uid[1]; +} + +inline bool operator==(const AccountUid& x, u64 y) +{ + return x.uid[0] == y && x.uid[1] == y; +} + +inline bool operator<(const AccountUid& x, const AccountUid& y) +{ + return x.uid[0] < y.uid[0] && x.uid[1] == y.uid[1]; +} + struct User { - u128 id; + AccountUid id; std::string name; std::string shortName; SDL_Texture* icon; @@ -47,11 +69,11 @@ namespace Account { Result init(void); void exit(void); - std::vector ids(void); - SDL_Texture* icon(u128 id); - u128 selectAccount(void); - std::string username(u128 id); - std::string shortName(u128 id); + std::vector ids(void); + SDL_Texture* icon(AccountUid id); + AccountUid selectAccount(void); + std::string username(AccountUid id); + std::string shortName(AccountUid id); } #endif \ No newline at end of file diff --git a/switch/include/filesystem.hpp b/switch/include/filesystem.hpp index 68478a4..33a7960 100644 --- a/switch/include/filesystem.hpp +++ b/switch/include/filesystem.hpp @@ -27,11 +27,11 @@ #ifndef FILESYSTEM_HPP #define FILESYSTEM_HPP +#include "account.hpp" #include namespace FileSystem { - Result mount(FsFileSystem* fileSystem, u64 titleID, u128 userID); - Result mount(FsFileSystem* fileSystem, u64 saveID); + Result mount(FsFileSystem* fileSystem, u64 titleID, AccountUid userID); int mount(FsFileSystem fs); void unmount(void); } diff --git a/switch/include/io.hpp b/switch/include/io.hpp index c154224..4f7b041 100644 --- a/switch/include/io.hpp +++ b/switch/include/io.hpp @@ -28,6 +28,7 @@ #define IO_HPP #include "KeyboardManager.hpp" +#include "account.hpp" #include "directory.hpp" #include "multiselection.hpp" #include "title.hpp" @@ -42,8 +43,8 @@ #define BUFFER_SIZE 0x80000 namespace io { - std::tuple backup(size_t index, u128 uid, size_t cellIndex); - std::tuple restore(size_t index, u128 uid, size_t cellIndex, const std::string& nameFromCell); + std::tuple backup(size_t index, AccountUid uid, size_t cellIndex); + std::tuple restore(size_t index, AccountUid uid, size_t cellIndex, const std::string& nameFromCell); Result copyDirectory(const std::string& srcPath, const std::string& dstPath); void copyFile(const std::string& srcPath, const std::string& dstPath); diff --git a/switch/include/main.hpp b/switch/include/main.hpp index aac6398..beb7d71 100644 --- a/switch/include/main.hpp +++ b/switch/include/main.hpp @@ -28,13 +28,14 @@ #define MAIN_HPP #include "Screen.hpp" +#include "account.hpp" #include "title.hpp" #include "util.hpp" #include #include -inline float g_currentTime = 0; -inline u128 g_currentUId = 0; +inline float g_currentTime = 0; +inline AccountUid g_currentUId; inline bool g_backupScrollEnabled = 0; inline bool g_notificationLedAvailable = false; inline std::shared_ptr g_screen = nullptr; diff --git a/switch/include/pksmbridge.hpp b/switch/include/pksmbridge.hpp index bd14592..9a62148 100644 --- a/switch/include/pksmbridge.hpp +++ b/switch/include/pksmbridge.hpp @@ -25,6 +25,7 @@ */ #include "KeyboardManager.hpp" +#include "account.hpp" #include "title.hpp" #include #include @@ -41,5 +42,5 @@ #define PKSM_PORT 34567 bool isPKSMBridgeTitle(u64 id); -std::tuple sendToPKSMBrigde(size_t index, u128 uid, size_t cellIndex); -std::tuple recvFromPKSMBridge(size_t index, u128 uid, size_t cellIndex); \ No newline at end of file +std::tuple sendToPKSMBrigde(size_t index, AccountUid uid, size_t cellIndex); +std::tuple recvFromPKSMBridge(size_t index, AccountUid uid, size_t cellIndex); \ No newline at end of file diff --git a/switch/include/title.hpp b/switch/include/title.hpp index deef7d7..c217c64 100644 --- a/switch/include/title.hpp +++ b/switch/include/title.hpp @@ -28,6 +28,7 @@ #define TITLE_HPP #include "SDLHelper.hpp" +#include "account.hpp" #include "configuration.hpp" #include "filesystem.hpp" #include "io.hpp" @@ -41,7 +42,7 @@ class Title { public: - void init(u8 saveDataType, u64 titleid, u128 userID, const std::string& name, const std::string& author); + void init(u8 saveDataType, u64 titleid, AccountUid userID, const std::string& name, const std::string& author); ~Title(void){}; std::string author(void); @@ -58,14 +59,13 @@ public: void saveId(u64 id); std::vector saves(void); u8 saveDataType(void); - bool systemSave(void); - u128 userId(void); + AccountUid userId(void); std::string userName(void); private: u64 mId; u64 mSaveId; - u128 mUserId; + AccountUid mUserId; std::string mUserName; std::string mName; std::string mSafeName; @@ -78,13 +78,13 @@ private: std::string mPlayTime; }; -void getTitle(Title& dst, u128 uid, size_t i); -size_t getTitleCount(u128 uid); +void getTitle(Title& dst, AccountUid uid, size_t i); +size_t getTitleCount(AccountUid uid); void loadTitles(void); void refreshDirectories(u64 id); -bool favorite(u128 uid, int i); +bool favorite(AccountUid uid, int i); void freeIcons(void); -SDL_Texture* smallIcon(u128 uid, size_t i); +SDL_Texture* smallIcon(AccountUid uid, size_t i); std::unordered_map getCompleteTitleList(void); #endif \ No newline at end of file diff --git a/switch/source/account.cpp b/switch/source/account.cpp index fe59260..2584b51 100644 --- a/switch/source/account.cpp +++ b/switch/source/account.cpp @@ -26,11 +26,11 @@ #include "account.hpp" -static std::map mUsers; +static std::map mUsers; Result Account::init(void) { - return accountInitialize(); + return accountInitialize(AccountServiceType_Application); } void Account::exit(void) @@ -41,16 +41,16 @@ void Account::exit(void) accountExit(); } -std::vector Account::ids(void) +std::vector Account::ids(void) { - std::vector v; + std::vector v; for (auto& value : mUsers) { v.push_back(value.second.id); } return v; } -static User getUser(u128 id) +static User getUser(AccountUid id) { User user{id, "", "", NULL}; AccountProfile profile; @@ -58,12 +58,12 @@ static User getUser(u128 id) memset(&profilebase, 0, sizeof(profilebase)); if (R_SUCCEEDED(accountGetProfile(&profile, id)) && R_SUCCEEDED(accountProfileGet(&profile, NULL, &profilebase))) { - user.name = std::string(profilebase.username, 0x20); + user.name = std::string(profilebase.nickname, 0x20); user.shortName = trimToFit(user.name, 96 - g_username_dotsize * 2, 13); // load icon u8* buffer; - size_t image_size, real_size; + u32 image_size, real_size; if (R_SUCCEEDED(accountProfileGetImageSize(&profile, &image_size)) && (buffer = (u8*)malloc(image_size)) != NULL && R_SUCCEEDED(accountProfileLoadImage(&profile, buffer, image_size, &real_size))) { SDLH_LoadImage(&user.icon, buffer, image_size); @@ -75,9 +75,9 @@ static User getUser(u128 id) return user; } -std::string Account::username(u128 id) +std::string Account::username(AccountUid id) { - std::map::const_iterator got = mUsers.find(id); + std::map::const_iterator got = mUsers.find(id); if (got == mUsers.end()) { User user = getUser(id); mUsers.insert({id, user}); @@ -87,9 +87,9 @@ std::string Account::username(u128 id) return got->second.name; } -std::string Account::shortName(u128 id) +std::string Account::shortName(AccountUid id) { - std::map::const_iterator got = mUsers.find(id); + std::map::const_iterator got = mUsers.find(id); if (got == mUsers.end()) { User user = getUser(id); mUsers.insert({id, user}); @@ -99,9 +99,9 @@ std::string Account::shortName(u128 id) return got->second.shortName; } -SDL_Texture* Account::icon(u128 id) +SDL_Texture* Account::icon(AccountUid id) { - std::map::const_iterator got = mUsers.find(id); + std::map::const_iterator got = mUsers.find(id); if (got == mUsers.end()) { User user = getUser(id); mUsers.insert({id, user}); @@ -110,9 +110,9 @@ SDL_Texture* Account::icon(u128 id) return got->second.icon; } -u128 Account::selectAccount(void) +AccountUid Account::selectAccount(void) { - u128 out_id = 0; + AccountUid out_id; LibAppletArgs args; libappletArgsCreate(&args, 0x10000); u8 st_in[0xA0] = {0}; @@ -121,8 +121,8 @@ u128 Account::selectAccount(void) Result res = libappletLaunch(AppletId_playerSelect, &args, st_in, 0xA0, st_out, 0x18, &repsz); if (R_SUCCEEDED(res)) { - u64 lres = *(u64*)st_out; - u128 uid = *(u128*)&st_out[8]; + u64 lres = *(u64*)st_out; + AccountUid uid = *(AccountUid*)&st_out[8]; if (lres == 0) out_id = uid; } diff --git a/switch/source/filesystem.cpp b/switch/source/filesystem.cpp index e47ad5a..479543e 100644 --- a/switch/source/filesystem.cpp +++ b/switch/source/filesystem.cpp @@ -26,14 +26,9 @@ #include "filesystem.hpp" -Result FileSystem::mount(FsFileSystem* fileSystem, u64 titleID, u128 userID) +Result FileSystem::mount(FsFileSystem* fileSystem, u64 titleID, AccountUid userID) { - return fsMount_SaveData(fileSystem, titleID, userID); -} - -Result FileSystem::mount(FsFileSystem* fileSystem, u64 saveID) -{ - return fsMount_SystemSaveData(fileSystem, saveID); + return fsOpen_SaveData(fileSystem, titleID, userID); } int FileSystem::mount(FsFileSystem fs) diff --git a/switch/source/io.cpp b/switch/source/io.cpp index 33cd82b..ac0ed24 100644 --- a/switch/source/io.cpp +++ b/switch/source/io.cpp @@ -151,7 +151,7 @@ Result io::deleteFolderRecursively(const std::string& path) return 0; } -std::tuple io::backup(size_t index, u128 uid, size_t cellIndex) +std::tuple io::backup(size_t index, AccountUid uid, size_t cellIndex) { const bool isNewFolder = cellIndex == 0; Result res = 0; @@ -160,7 +160,7 @@ std::tuple io::backup(size_t index, u128 uid, size_t getTitle(title, uid, index); Logger::getInstance().log(Logger::INFO, "Started backup of %s. Title id: 0x%016lX; User id: 0x%lX%lX.", title.name().c_str(), title.id(), - (u64)(title.userId() >> 8), (u64)(title.userId())); + title.userId().uid[1], title.userId().uid[0]); FsFileSystem fileSystem; res = FileSystem::mount(&fileSystem, title.id(), title.userId()); @@ -169,14 +169,14 @@ std::tuple io::backup(size_t index, u128 uid, size_t if (rc == -1) { FileSystem::unmount(); Logger::getInstance().log(Logger::ERROR, "Failed to mount filesystem during backup. Title id: 0x%016lX; User id: 0x%lX%lX.", title.id(), - (u64)(title.userId() >> 8), (u64)(title.userId())); + title.userId().uid[1], title.userId().uid[0]); return std::make_tuple(false, -2, "Failed to mount save."); } } else { Logger::getInstance().log(Logger::ERROR, "Failed to mount filesystem during backup with result 0x%08lX. Title id: 0x%016lX; User id: 0x%lX%lX.", res, title.id(), - (u64)(title.userId() >> 8), (u64)(title.userId())); + title.userId().uid[1], title.userId().uid[0]); return std::make_tuple(false, res, "Failed to mount save."); } @@ -257,7 +257,7 @@ std::tuple io::backup(size_t index, u128 uid, size_t return ret; } -std::tuple io::restore(size_t index, u128 uid, size_t cellIndex, const std::string& nameFromCell) +std::tuple io::restore(size_t index, AccountUid uid, size_t cellIndex, const std::string& nameFromCell) { Result res = 0; std::tuple ret = std::make_tuple(false, -1, ""); @@ -265,23 +265,23 @@ std::tuple io::restore(size_t index, u128 uid, size_t getTitle(title, uid, index); Logger::getInstance().log(Logger::INFO, "Started restore of %s. Title id: 0x%016lX; User id: 0x%lX%lX.", title.name().c_str(), title.id(), - (u64)(title.userId() >> 8), (u64)(title.userId())); + title.userId().uid[1], title.userId().uid[0]); FsFileSystem fileSystem; - res = title.systemSave() ? FileSystem::mount(&fileSystem, title.id()) : FileSystem::mount(&fileSystem, title.id(), title.userId()); + res = FileSystem::mount(&fileSystem, title.id(), title.userId()); if (R_SUCCEEDED(res)) { int rc = FileSystem::mount(fileSystem); if (rc == -1) { FileSystem::unmount(); Logger::getInstance().log(Logger::ERROR, "Failed to mount filesystem during restore. Title id: 0x%016lX; User id: 0x%lX%lX.", title.id(), - (u64)(title.userId() >> 8), (u64)(title.userId())); + title.userId().uid[1], title.userId().uid[0]); return std::make_tuple(false, -2, "Failed to mount save."); } } else { Logger::getInstance().log(Logger::ERROR, "Failed to mount filesystem during restore with result 0x%08lX. Title id: 0x%016lX; User id: 0x%lX%lX.", res, title.id(), - (u64)(title.userId() >> 8), (u64)(title.userId())); + title.userId().uid[1], title.userId().uid[0]); return std::make_tuple(false, res, "Failed to mount save."); } diff --git a/switch/source/main.cpp b/switch/source/main.cpp index f4b9d9d..6df362b 100644 --- a/switch/source/main.cpp +++ b/switch/source/main.cpp @@ -52,13 +52,13 @@ int main(void) loadTitles(); // get the user IDs - std::vector userIds = Account::ids(); + std::vector userIds = Account::ids(); // set g_currentUId to a default user in case we loaded at least one user if (g_currentUId == 0) g_currentUId = userIds.at(0); Thread networkThread; - threadCreate(&networkThread, (ThreadFunc)networkLoop, nullptr, 16 * 1000, 0x2C, -2); + threadCreate(&networkThread, (ThreadFunc)networkLoop, nullptr, nullptr, 16 * 1000, 0x2C, -2); threadStart(&networkThread); while (appletMainLoop() && !(hidKeysDown(CONTROLLER_P1_AUTO) & KEY_PLUS)) { diff --git a/switch/source/pksmbridge.cpp b/switch/source/pksmbridge.cpp index 74022e8..0487b57 100644 --- a/switch/source/pksmbridge.cpp +++ b/switch/source/pksmbridge.cpp @@ -40,7 +40,7 @@ bool validateIpAddress(const std::string& ip) return inet_pton(AF_INET, ip.c_str(), &sa.sin_addr) != 0; } -std::tuple sendToPKSMBrigde(size_t index, u128 uid, size_t cellIndex) +std::tuple sendToPKSMBrigde(size_t index, AccountUid uid, size_t cellIndex) { auto systemKeyboardAvailable = KeyboardManager::get().isSystemKeyboardAvailable(); if (!systemKeyboardAvailable.first) { @@ -111,7 +111,7 @@ std::tuple sendToPKSMBrigde(size_t index, u128 uid, s } } -std::tuple recvFromPKSMBridge(size_t index, u128 uid, size_t cellIndex) +std::tuple recvFromPKSMBridge(size_t index, AccountUid uid, size_t cellIndex) { int fd; struct sockaddr_in servaddr; diff --git a/switch/source/title.cpp b/switch/source/title.cpp index 448ea5d..10cb543 100644 --- a/switch/source/title.cpp +++ b/switch/source/title.cpp @@ -26,7 +26,7 @@ #include "title.hpp" -static std::unordered_map> titles; +static std::unordered_map> titles; static std::unordered_map icons; void freeIcons(void) @@ -47,7 +47,7 @@ static void loadIcon(u64 id, NsApplicationControlData* nsacd, size_t iconsize) } } -void Title::init(u8 saveDataType, u64 id, u128 userID, const std::string& name, const std::string& author) +void Title::init(u8 saveDataType, u64 id, AccountUid userID, const std::string& name, const std::string& author) { mId = id; mUserId = userID; @@ -90,11 +90,6 @@ void Title::init(u8 saveDataType, u64 id, u128 userID, const std::string& name, refreshDirectories(); } -bool Title::systemSave(void) -{ - return mSaveDataType != FsSaveDataType_SaveData; -} - u8 Title::saveDataType(void) { return mSaveDataType; @@ -115,7 +110,7 @@ void Title::saveId(u64 saveId) mSaveId = saveId; } -u128 Title::userId(void) +AccountUid Title::userId(void) { return mUserId; } @@ -214,10 +209,10 @@ void loadTitles(void) { titles.clear(); - FsSaveDataIterator iterator; + FsSaveDataInfoReader reader; FsSaveDataInfo info; - size_t total_entries = 0; - size_t outsize = 0; + s64 total_entries = 0; + size_t outsize = 0; NacpLanguageEntry* nle = NULL; NsApplicationControlData* nsacd = (NsApplicationControlData*)malloc(sizeof(NsApplicationControlData)); @@ -226,29 +221,29 @@ void loadTitles(void) } memset(nsacd, 0, sizeof(NsApplicationControlData)); - Result res = fsOpenSaveDataIterator(&iterator, FsSaveDataSpaceId_NandUser); + Result res = fsOpenSaveDataInfoReader(&reader, FsSaveDataSpaceId_User); if (R_FAILED(res)) { free(nsacd); return; } while (1) { - res = fsSaveDataIteratorRead(&iterator, &info, 1, &total_entries); + res = fsSaveDataInfoReaderRead(&reader, &info, 1, &total_entries); if (R_FAILED(res) || total_entries == 0) { break; } - if (info.saveDataType == FsSaveDataType_SaveData) { - u64 tid = info.titleID; - u64 sid = info.saveID; - u128 uid = info.userID; + if (info.save_data_type == FsSaveDataType_Account) { + u64 tid = info.application_id; + u64 sid = info.save_data_id; + AccountUid uid = info.uid; if (!Configuration::getInstance().filter(tid)) { - res = nsGetApplicationControlData(1, tid, nsacd, sizeof(NsApplicationControlData), &outsize); + res = nsGetApplicationControlData(NsApplicationControlSource_Storage, tid, nsacd, sizeof(NsApplicationControlData), &outsize); if (R_SUCCEEDED(res) && !(outsize < sizeof(nsacd->nacp))) { res = nacpGetLanguageEntry(&nsacd->nacp, &nle); if (R_SUCCEEDED(res) && nle != NULL) { Title title; - title.init(info.saveDataType, tid, uid, std::string(nle->name), std::string(nle->author)); + title.init(info.save_data_type, tid, uid, std::string(nle->name), std::string(nle->author)); title.saveId(sid); // load play statistics @@ -262,7 +257,7 @@ void loadTitles(void) loadIcon(tid, nsacd, outsize - sizeof(nsacd->nacp)); // check if the vector is already created - std::unordered_map>::iterator it = titles.find(uid); + std::unordered_map>::iterator it = titles.find(uid); if (it != titles.end()) { // found it->second.push_back(title); @@ -281,7 +276,7 @@ void loadTitles(void) } free(nsacd); - fsSaveDataIteratorClose(&iterator); + fsSaveDataInfoReaderClose(&reader); for (auto& vect : titles) { std::sort(vect.second.begin(), vect.second.end(), [](Title& l, Title& r) { @@ -295,23 +290,23 @@ void loadTitles(void) } } -void getTitle(Title& dst, u128 uid, size_t i) +void getTitle(Title& dst, AccountUid uid, size_t i) { - std::unordered_map>::iterator it = titles.find(uid); + std::unordered_map>::iterator it = titles.find(uid); if (it != titles.end() && i < getTitleCount(uid)) { dst = it->second.at(i); } } -size_t getTitleCount(u128 uid) +size_t getTitleCount(AccountUid uid) { - std::unordered_map>::iterator it = titles.find(uid); + std::unordered_map>::iterator it = titles.find(uid); return it != titles.end() ? it->second.size() : 0; } -bool favorite(u128 uid, int i) +bool favorite(AccountUid uid, int i) { - std::unordered_map>::iterator it = titles.find(uid); + std::unordered_map>::iterator it = titles.find(uid); return it != titles.end() ? Configuration::getInstance().favorite(it->second.at(i).id()) : false; } @@ -326,9 +321,9 @@ void refreshDirectories(u64 id) } } -SDL_Texture* smallIcon(u128 uid, size_t i) +SDL_Texture* smallIcon(AccountUid uid, size_t i) { - std::unordered_map>::iterator it = titles.find(uid); + std::unordered_map>::iterator it = titles.find(uid); return it != titles.end() ? it->second.at(i).icon() : NULL; } diff --git a/switch/source/util.cpp b/switch/source/util.cpp index a88d3b4..b115b6a 100644 --- a/switch/source/util.cpp +++ b/switch/source/util.cpp @@ -176,13 +176,13 @@ HidsysNotificationLedPattern blinkLedPattern(u8 times) void blinkLed(u8 times) { if (g_notificationLedAvailable) { - size_t n; + s32 n; u64 uniquePadIds[2]; HidsysNotificationLedPattern pattern = blinkLedPattern(times); memset(uniquePadIds, 0, sizeof(uniquePadIds)); Result res = hidsysGetUniquePadsFromNpad(hidGetHandheldMode() ? CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1, uniquePadIds, 2, &n); if (R_SUCCEEDED(res)) { - for (size_t i = 0; i < n; i++) { + for (s32 i = 0; i < n; i++) { hidsysSetNotificationLedPattern(&pattern, uniquePadIds[i]); } }