From 8073ca30fb7c6a01c323c13048e010a642a2aa6f Mon Sep 17 00:00:00 2001 From: yellows8 Date: Mon, 26 Mar 2018 14:52:03 -0400 Subject: [PATCH] In save example, use SaveDataIterator to find what savedata to use, otherwise fallback to the original hard-coded TID + current-userID. --- fs/save/source/main.c | 68 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/fs/save/source/main.c b/fs/save/source/main.c index d6b0c05..7f8d5b7 100644 --- a/fs/save/source/main.c +++ b/fs/save/source/main.c @@ -6,6 +6,38 @@ //This example shows how to access savedata for (official) applications/games. +Result get_save(u64 *titleID, u128 *userID) +{ + Result rc=0; + FsSaveDataIterator iterator; + size_t total_entries=0; + FsSaveDataInfo info; + + rc = fsOpenSaveDataIterator(&iterator, FsSaveDataSpaceId_NandUser);//See libnx fs.h. + if (R_FAILED(rc)) { + printf("fsOpenSaveDataIterator() failed: 0x%x\n", rc); + return rc; + } + + //Find the first savedata with FsSaveDataType_SaveData. + while(1) { + rc = fsSaveDataIteratorRead(&iterator, &info, 1, &total_entries);//See libnx fs.h. + if (R_FAILED(rc) || total_entries==0) break; + + if (info.SaveDataType == FsSaveDataType_SaveData) {//Filter by FsSaveDataType_SaveData, however note that NandUser can have non-FsSaveDataType_SaveData. + *titleID = info.titleID; + *userID = info.userID; + return 0; + } + } + + fsSaveDataIteratorClose(&iterator); + + if (R_SUCCEEDED(rc)) return MAKERESULT(Module_Libnx, LibnxError_NotFound); + + return rc; +} + int main(int argc, char **argv) { Result rc=0; @@ -17,32 +49,40 @@ int main(int argc, char **argv) FsFileSystem tmpfs; u128 userID=0; bool account_selected=0; - u64 titleid=0x01007ef00011e000;//titleID of the save to mount, in this case BOTW. + u64 titleID=0x01007ef00011e000;//titleID of the save to mount, in this case BOTW. gfxInitDefault(); consoleInit(NULL); //Get the userID for save mounting. To mount common savedata, use FS_SAVEDATA_USERID_COMMONSAVE. - rc = accountInitialize(); - if (R_FAILED(rc)) { - printf("accountInitialize() failed: 0x%x\n", rc); - } - - if (R_SUCCEEDED(rc)) { - rc = accountGetActiveUser(&userID, &account_selected); - accountExit(); + //Try to find savedata to use with get_save() first, otherwise fallback to the above hard-coded TID + the userID from accountGetActiveUser(). Note that you can use either method. + if (R_FAILED(get_save(&titleID, &userID))) { + rc = accountInitialize(); if (R_FAILED(rc)) { - printf("accountGetActiveUser() failed: 0x%x\n", rc); + printf("accountInitialize() failed: 0x%x\n", rc); } - else if(!account_selected) { - printf("No user is currently selected.\n"); - rc = -1; + + if (R_SUCCEEDED(rc)) { + rc = accountGetActiveUser(&userID, &account_selected); + accountExit(); + + if (R_FAILED(rc)) { + printf("accountGetActiveUser() failed: 0x%x\n", rc); + } + else if(!account_selected) { + printf("No user is currently selected.\n"); + rc = -1; + } } } if (R_SUCCEEDED(rc)) { - rc = fsMount_SaveData(&tmpfs, titleid, userID);//See also libnx fs.h. + printf("Using titleID=0x%016lx userID: 0x%lx 0x%lx\n", titleID, (u64)(userID>>64), (u64)userID); + } + + if (R_SUCCEEDED(rc)) { + rc = fsMount_SaveData(&tmpfs, titleID, userID);//See also libnx fs.h. if (R_FAILED(rc)) { printf("fsMount_SaveData() failed: 0x%x\n", rc); }