Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
auto-patch on HDD copy
create saves with current date-time
  • Loading branch information
bucanero committed Dec 15, 2024
1 parent d79b8f2 commit 48ee918
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 56 deletions.
12 changes: 5 additions & 7 deletions include/saves.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,24 @@
#define USB1_PATH "/mnt/usb1/"
#define USB_PATH "/mnt/usb%d/"
#define FAKE_USB_PATH "/data/fakeusb/"
#define USER_PATH_HDD "/system_data/savedata/%08x/db/user/savedata.db"
#define SAVES_DB_PATH "/system_data/savedata/%08x/db/user/savedata.db"

#define PS4_SAVES_PATH_USB "PS4/APOLLO/"
#define PS2_SAVES_PATH_USB "PS2/SAVEDATA/"
#define PS1_SAVES_PATH_USB "PS1/SAVEDATA/"
#define PSV_SAVES_PATH_USB "PS3/EXPORT/PSV/"
#define TROPHIES_PATH_USB "PS4/EXPORT/TROPHY/"

#define PS3_LICENSE_PATH "exdata/"
#define PS4_SAVES_PATH_HDD "/user/home/%08x/savedata_meta/user/"
#define PS2_SAVES_PATH_HDD "ps2emu2_savedata/"
#define PSP_SAVES_PATH_HDD "minis_savedata/"
#define SAVES_PATH_HDD "/user/home/%08x/savedata/"
#define SAVE_ICON_PATH_HDD "/user/home/%08x/savedata_meta/user/"
#define TROPHY_PATH_HDD "/user/home/%08x/trophy/data/"

#define SAVES_PATH_USB0 USB0_PATH PS4_SAVES_PATH_USB
#define SAVES_PATH_USB1 USB1_PATH PS4_SAVES_PATH_USB

#define TROPHY_PATH_USB0 USB0_PATH TROPHIES_PATH_USB
#define TROPHY_PATH_USB1 USB1_PATH TROPHIES_PATH_USB
#define TROPHY_PATH_HDD "/user/home/%08x/trophy/db/trophy_local.db"
#define TROPHY_DB_PATH "/user/home/%08x/trophy/db/trophy_local.db"
#define APP_DB_PATH_HDD "/system_data/priv/mms/app.db"

#define EXPORT_PATH_USB0 USB0_PATH "PS4/EXPORT/"
Expand All @@ -55,7 +54,6 @@
#define VMC_PS1_PATH_USB "PS1/VMC/"

#define IMP_PS2VMC_PATH_USB USB_PATH "PS2/VMC/"
#define IMPORT_RAP_PATH_USB USB_PATH PS3_LICENSE_PATH

#define ONLINE_URL "https://bucanero.github.io/apollo-saves/"
#define ONLINE_PATCH_URL "https://bucanero.github.io/apollo-patches/PS4/"
Expand Down
20 changes: 13 additions & 7 deletions source/exec_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static int _update_save_details(const char* sys_path, const save_entry_t* save)
snprintf(file_path, sizeof(file_path), "%s" "icon0.png", sys_path);
if (read_buffer(file_path, &iconBuf, &iconSize) == SUCCESS)
{
snprintf(file_path, sizeof(file_path), PS4_SAVES_PATH_HDD "%s/%s_icon0.png", apollo_config.user_id, save->title_id, save->dir_name);
snprintf(file_path, sizeof(file_path), SAVE_ICON_PATH_HDD "%s/%s_icon0.png", apollo_config.user_id, save->title_id, save->dir_name);
mkdirs(file_path);

if (write_buffer(file_path, iconBuf, iconSize) < 0)
Expand All @@ -199,6 +199,10 @@ static int _copy_save_hdd(const save_entry_t* save)
{
char copy_path[256];
char mount[ORBIS_SAVE_DATA_DIRNAME_DATA_MAXSIZE];
sfo_patch_t patch = {
.user_id = apollo_config.user_id,
.account_id = apollo_config.account_id,
};

if (!orbis_SaveMount(save, ORBIS_SAVE_DATA_MOUNT_MODE_RDWR | ORBIS_SAVE_DATA_MOUNT_MODE_CREATE2 | ORBIS_SAVE_DATA_MOUNT_MODE_COPY_ICON, mount))
return 0;
Expand All @@ -211,7 +215,10 @@ static int _copy_save_hdd(const save_entry_t* save)
snprintf(copy_path, sizeof(copy_path), "%s" "sce_sys/", save->path);
_update_save_details(copy_path, save);

snprintf(copy_path, sizeof(copy_path), APOLLO_SANDBOX_PATH "sce_sys/param.sfo", mount);
patch_sfo(copy_path, &patch);
orbis_SaveUmount(mount);

return 1;
}

Expand Down Expand Up @@ -451,7 +458,7 @@ static void copySavePFS(const save_entry_t* save)
orbis_SaveUmount(mount);

snprintf(src_path, sizeof(src_path), "%s%s", save->path, save->dir_name);
snprintf(hdd_path, sizeof(hdd_path), "/user/home/%08x/savedata/%s/sdimg_%s", apollo_config.user_id, save->title_id, save->dir_name);
snprintf(hdd_path, sizeof(hdd_path), SAVES_PATH_HDD "%s/sdimg_%s", apollo_config.user_id, save->title_id, save->dir_name);
LOG("Copying <%s> to %s...", src_path, hdd_path);
if (copy_file(src_path, hdd_path) != SUCCESS)
{
Expand All @@ -460,7 +467,7 @@ static void copySavePFS(const save_entry_t* save)
}

snprintf(src_path, sizeof(src_path), "%s%s.bin", save->path, save->dir_name);
snprintf(hdd_path, sizeof(hdd_path), "/user/home/%08x/savedata/%s/%s.bin", apollo_config.user_id, save->title_id, save->dir_name);
snprintf(hdd_path, sizeof(hdd_path), SAVES_PATH_HDD "%s/%s.bin", apollo_config.user_id, save->title_id, save->dir_name);
LOG("Copying <%s> to %s...", src_path, hdd_path);
if (copy_file(src_path, hdd_path) != SUCCESS)
{
Expand All @@ -476,8 +483,7 @@ static void copySavePFS(const save_entry_t* save)
}

snprintf(hdd_path, sizeof(hdd_path), APOLLO_SANDBOX_PATH "sce_sys/param.sfo", mount);
if (show_dialog(DIALOG_TYPE_YESNO, "Resign save %s/%s?", save->title_id, save->dir_name))
patch_sfo(hdd_path, &patch);
patch_sfo(hdd_path, &patch);

*strrchr(hdd_path, 'p') = 0;
_update_save_details(hdd_path, save);
Expand Down Expand Up @@ -655,7 +661,7 @@ static int webReqHandler(dWebRequest_t* req, dWebResponse_t* res, void* list)
continue;

if (item->flags & SAVE_FLAG_HDD)
asprintf(&res->data, PS4_SAVES_PATH_HDD "%s/%s_icon0.png", apollo_config.user_id, item->title_id, item->dir_name);
asprintf(&res->data, SAVE_ICON_PATH_HDD "%s/%s_icon0.png", apollo_config.user_id, item->title_id, item->dir_name);
else
asprintf(&res->data, "%ssce_sys/icon0.png", item->path);

Expand All @@ -675,7 +681,7 @@ static int webReqHandler(dWebRequest_t* req, dWebResponse_t* res, void* list)
// http://ps3-ip:8080/icon/CUSA12345/DIR-NAME_icon0.png
if (wildcard_match(req->resource, "/icon/\?\?\?\?\?\?\?\?\?/*_icon0.png"))
{
asprintf(&res->data, PS4_SAVES_PATH_HDD "%s", apollo_config.user_id, req->resource + 6);
asprintf(&res->data, SAVE_ICON_PATH_HDD "%s", apollo_config.user_id, req->resource + 6);
return (file_exists(res->data) == SUCCESS);
}

Expand Down
4 changes: 2 additions & 2 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,12 @@ void update_usb_path(char* path)

void update_hdd_path(char* path)
{
sprintf(path, USER_PATH_HDD, apollo_config.user_id);
sprintf(path, SAVES_DB_PATH, apollo_config.user_id);
}

void update_trophy_path(char* path)
{
sprintf(path, TROPHY_PATH_HDD, apollo_config.user_id);
sprintf(path, TROPHY_DB_PATH, apollo_config.user_id);
}

void update_db_path(char* path)
Expand Down
48 changes: 24 additions & 24 deletions source/menu_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ static void SetMenu(int id)
}

else if (selected_entry->flags & SAVE_FLAG_HDD)
snprintf(iconfile, sizeof(iconfile), PS4_SAVES_PATH_HDD "%s/%s_icon0.png", apollo_config.user_id, selected_entry->title_id, selected_entry->dir_name);
snprintf(iconfile, sizeof(iconfile), SAVE_ICON_PATH_HDD "%s/%s_icon0.png", apollo_config.user_id, selected_entry->title_id, selected_entry->dir_name);

if (file_exists(iconfile) == SUCCESS)
LoadMenuTexture(iconfile, icon_png_file_index);
Expand Down Expand Up @@ -400,36 +400,36 @@ static void doSaveMenu(save_list_t * save_list)
{
selected_entry = list_get_item(save_list->list, menu_sel);

if (selected_entry->type == FILE_TYPE_VMC && selected_entry->flags & SAVE_FLAG_VMC)
{
char tmp_path[256];
if (selected_entry->type == FILE_TYPE_VMC && selected_entry->flags & SAVE_FLAG_VMC)
{
char tmp_path[256];

strncpy(tmp_path, selected_entry->path, sizeof(tmp_path));
strncpy(tmp_path, selected_entry->path, sizeof(tmp_path));

if (selected_entry->flags & SAVE_FLAG_HDD)
{
char mount[32];

if (!orbis_SaveMount(selected_entry, ORBIS_SAVE_DATA_MOUNT_MODE_RDWR, mount))
return;
if (selected_entry->flags & SAVE_FLAG_HDD)
{
char mount[ORBIS_SAVE_DATA_DIRNAME_DATA_MAXSIZE];

snprintf(tmp_path, sizeof(tmp_path), APOLLO_SANDBOX_PATH "%s", mount, selected_entry->path);
}
if (!orbis_SaveMount(selected_entry, ORBIS_SAVE_DATA_MOUNT_MODE_RDWR, mount))
return;

if (selected_entry->flags & SAVE_FLAG_PS1)
{
strncpy(vmc1_saves.path, tmp_path, sizeof(vmc1_saves.path));
SetMenu(MENU_PS1VMC_SAVES);
}
else
{
strncpy(vmc2_saves.path, tmp_path, sizeof(vmc2_saves.path));
SetMenu(MENU_PS2VMC_SAVES);
}
snprintf(tmp_path, sizeof(tmp_path), APOLLO_SANDBOX_PATH "%s", mount, selected_entry->path);
}

return;
if (selected_entry->flags & SAVE_FLAG_PS1)
{
strncpy(vmc1_saves.path, tmp_path, sizeof(vmc1_saves.path));
SetMenu(MENU_PS1VMC_SAVES);
}
else
{
strncpy(vmc2_saves.path, tmp_path, sizeof(vmc2_saves.path));
SetMenu(MENU_PS2VMC_SAVES);
}

return;
}

if (!selected_entry->codes && !save_list->ReadCodes(selected_entry))
{
show_message("No data found in folder:\n%s", selected_entry->path);
Expand Down
39 changes: 23 additions & 16 deletions source/saves.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,6 @@ int orbis_SaveMount(const save_entry_t *save, uint32_t mount_mode, char* mount_p
char volumePath[256];

snprintf(mountDir, sizeof(mountDir), APOLLO_SANDBOX_PATH, save->dir_name);
snprintf(keyPath, sizeof(keyPath), "/user/home/%08x/savedata/%s/%s.bin", apollo_config.user_id, save->title_id, save->dir_name);
snprintf(volumePath, sizeof(volumePath), "/user/home/%08x/savedata/%s/sdimg_%s", apollo_config.user_id, save->title_id, save->dir_name);

if (mkdirs(mountDir) < 0)
{
LOG("ERROR: can't create '%s'", mountDir);
Expand All @@ -441,22 +438,39 @@ int orbis_SaveMount(const save_entry_t *save, uint32_t mount_mode, char* mount_p

if (mount_mode & SAVE_FLAG_TROPHY)
{
snprintf(keyPath, sizeof(keyPath), "/user/home/%08x/trophy/data/%s/sealedkey", apollo_config.user_id, save->title_id);
snprintf(volumePath, sizeof(volumePath), "/user/home/%08x/trophy/data/%s/trophy.img", apollo_config.user_id, save->title_id);
snprintf(keyPath, sizeof(keyPath), TROPHY_PATH_HDD "%s/sealedkey", apollo_config.user_id, save->title_id);
snprintf(volumePath, sizeof(volumePath), TROPHY_PATH_HDD "%s/trophy.img", apollo_config.user_id, save->title_id);
}
else if (mount_mode & SAVE_FLAG_LOCKED)
{
snprintf(keyPath, sizeof(keyPath), "%s%s.bin", save->path, save->dir_name);
snprintf(volumePath, sizeof(volumePath), "%s%s", save->path, save->dir_name);
}
else
{
snprintf(keyPath, sizeof(keyPath), SAVES_PATH_HDD "%s/%s.bin", apollo_config.user_id, save->title_id, save->dir_name);
snprintf(volumePath, sizeof(volumePath), SAVES_PATH_HDD "%s/sdimg_%s", apollo_config.user_id, save->title_id, save->dir_name);
}

if ((mount_mode & ORBIS_SAVE_DATA_MOUNT_MODE_CREATE2) && (file_exists(keyPath) != SUCCESS))
{
sqlite3 *db;
char *query, dbpath[256];

LOG("WARN: can't find '%s'", keyPath);
snprintf(dbpath, sizeof(dbpath), USER_PATH_HDD, apollo_config.user_id);
LOG("Creating save '%s'...", keyPath);
mkdirs(volumePath);
if (createSave(volumePath, keyPath, save->blocks) < 0)
{
LOG("ERROR: can't create '%s'", keyPath);
return 0;
}

snprintf(dbpath, sizeof(dbpath), SAVES_DB_PATH, apollo_config.user_id);
if ((db = open_sqlite_db(dbpath)) == NULL)
return 0;

query = sqlite3_mprintf("INSERT INTO savedata(title_id, dir_name, main_title, sub_title, detail, tmp_dir_name, is_broken, user_param, blocks, free_blocks, size_kib, mtime, fake_broken, account_id, user_id, faked_owner, cloud_icon_url, cloud_revision, game_title_id) "
"VALUES (%Q, %Q, '', '', '', '', 0, 0, %d, %d, %d, '2020-01-01T20:20:01.00Z', 0, %lld, %ld, 0, '', 0, %Q);",
"VALUES (%Q, %Q, '', '', '', '', 0, 0, %d, %d, %d, strftime('%%Y-%%m-%%dT%%H:%%M:%%S.00Z', CURRENT_TIMESTAMP), 0, %ld, %d, 0, '', 0, %Q);",
save->title_id, save->dir_name, save->blocks, save->blocks, (save->blocks*32), apollo_config.account_id, apollo_config.user_id, save->title_id);

if (sqlite3_exec(db, query, NULL, NULL, NULL) != SQLITE_OK)
Expand All @@ -470,13 +484,6 @@ int orbis_SaveMount(const save_entry_t *save, uint32_t mount_mode, char* mount_p
LOG("Saving database to %s", dbpath);
sqlite3_memvfs_dump(db, NULL, dbpath);
sqlite3_close(db);

mkdirs(volumePath);
if (createSave(volumePath, keyPath, save->blocks) < 0)
{
LOG("ERROR: can't create '%s'", keyPath);
return 0;
}
}

int mountErrorCode = mountSave(volumePath, keyPath, mountDir);
Expand All @@ -497,7 +504,7 @@ int orbis_UpdateSaveParams(const save_entry_t* save, const char* title, const ch
sqlite3 *db;
char* query, dbpath[256];

snprintf(dbpath, sizeof(dbpath), USER_PATH_HDD, apollo_config.user_id);
snprintf(dbpath, sizeof(dbpath), SAVES_DB_PATH, apollo_config.user_id);
db = open_sqlite_db(dbpath);
if (!db)
return 0;
Expand Down
1 change: 1 addition & 0 deletions source/sfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ int patch_sfo(const char *in_file_path, sfo_patch_t* patches) {

if (sfo_write(sfo, in_file_path) < 0) {
LOG("Unable to write to '%s'", in_file_path);
sfo_free(sfo);
return -1;
}

Expand Down

0 comments on commit 48ee918

Please sign in to comment.