mirror of
https://github.com/switchbrew/switch-examples.git
synced 2025-06-21 21:32:40 +02:00
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:
parent
412a458014
commit
8073ca30fb
@ -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,12 +49,15 @@ 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.
|
||||||
|
|
||||||
|
//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();
|
rc = accountInitialize();
|
||||||
if (R_FAILED(rc)) {
|
if (R_FAILED(rc)) {
|
||||||
printf("accountInitialize() failed: 0x%x\n", rc);
|
printf("accountInitialize() failed: 0x%x\n", rc);
|
||||||
@ -40,9 +75,14 @@ int main(int argc, char **argv)
|
|||||||
rc = -1;
|
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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user