In save example, use SaveDataIterator to find what savedata to use, otherwise fallback to the original hard-coded TID + current-userID.

This commit is contained in:
yellows8 2018-03-26 14:52:03 -04:00
parent 412a458014
commit 8073ca30fb

View File

@ -6,6 +6,38 @@
//This example shows how to access savedata for (official) applications/games. //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) int main(int argc, char **argv)
{ {
Result rc=0; Result rc=0;
@ -17,32 +49,40 @@ int main(int argc, char **argv)
FsFileSystem tmpfs; FsFileSystem tmpfs;
u128 userID=0; u128 userID=0;
bool account_selected=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(); gfxInitDefault();
consoleInit(NULL); consoleInit(NULL);
//Get the userID for save mounting. To mount common savedata, use FS_SAVEDATA_USERID_COMMONSAVE. //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)) { 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"); if (R_SUCCEEDED(rc)) {
rc = -1; 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)) { 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)) { if (R_FAILED(rc)) {
printf("fsMount_SaveData() failed: 0x%x\n", rc); printf("fsMount_SaveData() failed: 0x%x\n", rc);
} }