Repository navigation
Cloud saves: the Mac and iPhone apps of a game play on from one save through an iCloud Drive folder - #185
Cloud saves: the Mac and iPhone apps of a game play on from one save through an iCloud Drive folder#185 2 commits merged into
Conversation
…through an iCloud Drive folder Until now a save moved between the Mac and an iPhone only by hand (the Mac front end's Saves sheet, over the cable). The apps are signed ad hoc or with a free/personal team, so CloudKit and iCloud entitlements are out; a folder the player picks in iCloud Drive is not. dm/host/save_sync.c packs the committed save (DM_SAVE and DM_SAVE_device) into one file, <folder>/<title ID>/save.dmsync (a folder named after the title ID is taken as the game's own), because iCloud replaces a file as a whole but syncs a folder file by file. The cloud's copy comes in at the game's first save mount, before the work directory is cloned; the committed save goes out a few seconds after a Commit on a thread of its own, when the iPhone app goes to the background, and from the Mac launcher once the host has exited. <DM_SAVE>.sync keeps the hash of the save last known to be the same on both sides: only the cloud's changed, it comes in; only this one changed, it goes out; both changed, the newer wins. Every save this replaces, here or in the cloud, is kept first in <DM_SAVE>-backups/<date> local|cloud. The folder is chosen in each app's launcher: Cloud saves in the Mac settings window (dm/mac/Launcher.swift, defaults key cloudSaves.folder, read by launcher.c) and on the iPhone (a security-scoped bookmark, DataBookmark.m). DM_SAVE_SYNC (a folder, icloud, or 0) overrides both. On the iPhone the copies go through NSFileCoordinator (SaveSync.m), which downloads a file iCloud has only listed. Tested: dm/host/tests/t_save_sync.c (47 checks, also under TSan and ASan/UBSan), and TOTK 1.4.3 between an iPhone and a Mac in both directions.
Exported with scripts/patches.py export; patches.py check passes (20 patches on seedds/nxnative 062cd06 give the committed vendored tree).
|
@dreamw0rk the provided screenshots do not match the current UI design. Did you build the feature on latest main? If not, please pull main into your fork and add the feature to the current UI |
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved sync races and unsafe snapshot/import handling can overwrite newer cloud saves, lose local backups, and publish inconsistent account/device data.
6 open issues
Isolate timed-out fetch staging paths · New Make save import and backup replacement recoverable · New Clone account and device roots under one lock · New Preserve newer cloud saves during delayed sends · New Verify cloud destination before skipping upload · New Use effective DM_SAVE for exit synchronization · New
What changed in this PR
Adds opt-in cloud saves to the per-game Mac and iPhone apps through a user-selected iCloud Drive folder, without requiring iCloud container entitlements. The implementation is carried in the fork’s patch queue.
Changes:
- Adds packed save synchronization, conflict handling, backups, and background sending.
- Adds folder selection in both launchers and synchronization on save mount, commit, backgrounding, and Mac host exit.
- Adds synchronization tests, build integration, and player documentation.
| File | Description |
|---|---|
patches/series |
Registers the cloud-save patch. |
patches/0025-cloud-saves.patch |
Carries the vendored implementation and documentation changes. |
docs/settings.md |
Documents setup, synchronization, and backups. |
docs/env_switches.tsv |
Documents DM_SAVE_SYNC. |
dm/tools/tests/test_nsp_mac_app.py |
Tests launcher linking and cache invalidation. |
dm/mac/pack_app.py |
Links save synchronization into the Mac launcher. |
dm/mac/Launcher.swift |
Adds cloud-save folder settings. |
dm/mac/launcher.c |
Applies folder preferences and sends saves after host exit. |
dm/ios/Sources/SaveSync.m |
Implements coordinated cloud-file transfers. |
dm/ios/Sources/Launcher.swift |
Adds cloud-save folder selection. |
dm/ios/Sources/front_ios.m |
Configures synchronization and requests background flushing. |
dm/ios/Sources/DataBookmark.m |
Persists and resolves cloud-folder bookmarks. |
dm/ios/Sources/DataBookmark.h |
Exposes cloud-folder bookmark interfaces. |
dm/host/tests/t_save_sync.c |
Tests packing, conflicts, backups, and device sessions. |
dm/host/tests/Makefile |
Integrates synchronization tests and sanitizer runs. |
dm/host/save_sync.h |
Defines synchronization interfaces and archive metadata. |
dm/host/save_sync.c |
Implements archive handling, synchronization, and backups. |
dm/host/Makefile |
Includes synchronization in the host build. |
dm/host/hle_fs.c |
Connects save mounts, commits, and snapshot cloning. |
dm/fork_patches.txt |
Records the cloud-save patch in the applied-patch list. |
Review effort: Balanced
| static void *fetch_thread(void *p) { | ||
| fetch_job *j = p; int r = front_save_sync_fetch(j->cloud, j->local); | ||
| pthread_mutex_lock(&j->mu); j->done = 1; j->result = r; int late = j->abandoned; pthread_cond_signal(&j->cv); pthread_mutex_unlock(&j->mu); | ||
| if (late) { unlink(j->local); pthread_mutex_destroy(&j->mu); pthread_cond_destroy(&j->cv); free(j); } |
| if (fresh && !access(here, F_OK)) { // swapped: here holds the cloud's, incoming this device's | ||
| if (renamex_np(incoming, here, RENAME_SWAP) || (had ? rename(incoming, old) : rm_tree(incoming))) return -1; | ||
| } else if (fresh) { if (rename(incoming, here)) return -1; } | ||
| else if (had && rename(here, old)) return -1; // a folder the cloud's save does not have: this device's goes, so both hold the same |
| for (int i = 0; i < 2; i++) { | ||
| char from[PATH_MAX], to[PATH_MAX]; folder_path(save, i, from, sizeof from); folder_path(snap, i, to, sizeof to); | ||
| if (has_entries(from) && dm_fs_clone_committed(from, to)) { SRED("cloning %s failed (errno=%d)", from, errno); goto done; } | ||
| } |
| else if (got && other.files && strcmp(other.hash, base)) { | ||
| if (backup_dir(save, "cloud", kept, sizeof kept) || ss_read(theirs, kept, &other)) { SRED("cannot keep the cloud's save before replacing it (errno=%d); not sent", errno); goto done; } | ||
| when(other.saved, t, sizeof t); | ||
| SLOG("the cloud's save (from %s, saved %s) changed since this device's last sync; this device's newer one replaces it, and it is kept in %s", other.device, t, kept); |
| } | ||
| if (ss_pack(snap, title, packed, &mine)) { SRED("packing %s failed (errno=%d)", save, errno); goto done; } | ||
| read_base(save, base); | ||
| if (!mine.files || !strcmp(mine.hash, base)) { ok = 1; goto done; } // no save yet, or the one already in step |
| int status; | ||
| while (waitpid(g_child, &status, 0) < 0) if (errno != EINTR) return 1; | ||
| // Cloud saves (DM_SAVE_SYNC, dm/host/save_sync.h): what the host committed and had not sent yet goes now, after a crash too (the committed save is whole whatever the host was doing) | ||
| if (!save_sync_after_exit(save, id)) fprintf(stderr, "launcher: the save did not reach the cloud; it goes at the next start\n"); |
|
@dreamw0rk how does this differ from a save that you just put anywhere on the iPhones file system? There are folders on an iPhone that are synced automatically when users put files in them. That way a user can decide if they want a save locally or in one of their iCloud's folders. Saving in a local directory doesn't sync to the Mac and vice versa of course |
|
@dreamw0rk also propose the feature upstream to nxnative. We want to keep patches small here |
|
Review: approved with one addition. The cloud-saves design is right (whole-file sync with a hash, per-date backups, NSFileCoordinator on iOS, DM_SAVE_SYNC override), the test coverage is real (47 checks incl. TSan/ASan, both directions verified on device with TotK), and the patch form is correct — one patch, pending, Reason/Issue present. I verified the patch queue absorbs your work cleanly. One addition needed, which your CI caught: the Qt suite failed because Also noted for the record: your 0025 number will collide with #165's 0025 when both merge — the second one to merge needs a rebase (patches.py refresh renumbers); I will handle whichever comes second. Do not renumber on your side. |
|
The Qt failure is fixed by #190 (merged): the cloud-saves patch adds |
The short version: iCloud syncs a folder one file at a time, and a save isn't one file. TotK's save is 32 files (six slots with progress.sav, caption.sav, footprint.sav etc., plus option.sav and the album), and the host writes them as a transaction. The game writes into a clone, and Commit swaps the whole folder in with renamex_np(RENAME_SWAP) (hle_fs.c). iCloud knows nothing about that, so the other device can end up with the new progress.sav next to an old caption.sav, which is a save the game never wrote. Every Commit would also look to iCloud like a folder being deleted and recreated. Conflicts have the same problem. If you play on both devices without a sync in between, iCloud keeps one version of each file and stashes the other as a conflict version the game never sees, so you get a mix again. This PR decides for the whole save, against the hash of the last save both sides agreed on, and the one that loses goes to save-backups/. I also didn't want the game waiting on the network. iCloud can evict files and download them on demand, so a save read could stall or fail in the middle of a session. On iOS, iCloud also expects NSFileCoordinator, which the host's plain POSIX file code doesn't use. Here the game always plays from local storage. It reads the cloud once, at the first save mount (it gives up after 10 s and keeps the local save), and uploads from a background thread after each save. On local vs. cloud: cloud saves are off by default, and the save always stays local. Turning it on means choosing a folder, and it can be any folder: whatever the document picker offers on the iPhone, any path on the Mac (iCloud Drive, Dropbox, a NAS). The folder just holds one packed file per game, so whatever syncs it can only move it as a whole. |
|
Two answers, plus a confession. On pushing to your branch: I can't — On the rebase struggle — you're right, and here's what actually happened. The rebase itself wasn't the problem; the timing was. Your branch was based on main from before three big merges landed (the Swedish localization, 25 upstream commits, the cloud-saves renumber), and my first attempt chased conflicts one at a time — force-pushing, waiting out a CI round, finding the next breakage. After the second round I switched to squashing onto main, which was the right call, but I should have said so in the PR instead of leaving you to read the commit graph. And on your deeper point: you're correct that I could have just waited — GitHub auto-marked #185 merged when the content landed through another PR anyway. Chasing the CI on a superseding branch was wasted effort and it made the collaboration noisier than it needed to be. Sorry about that; the squash-then-close pattern is what I'll use from now on, and I'll say so in the PR when I do it. One process note so we don't repeat this: for fork PRs where the author is still iterating, I'll leave review comments and hold off on landing anything until you say it's ready — which is what we did for #155/#156, and that worked cleanly. |


What and why
A game's Mac app and iPhone app did not share a save: the only way across was by hand, the Mac front end's Saves sheet over the cable (
frontend/Sources/NSPortBuilder/SaveTransfer.swift). CloudKit or an iCloud container would need entitlements that ad-hoc Mac signing and free/personal iPhone signing do not have, so this goes through a folder the player picks in iCloud Drive, the same folder in both apps.dm/host/save_sync.cpacks the committed save (DM_SAVEandDM_SAVE_device) into<folder>/<title ID>/save.dmsync: iCloud replaces a file as a whole but syncs a folder file by file, and a save that is half one device's and half the other's is worse than either. The file is checked whole on read (header, every entry, a SHA-256 over paths and contents, an end marker). Picking the<title ID>folder itself also works.hle_fs.c), before the work directory is cloned from the committed save. The committed save goes out a few seconds after a Commit, on a thread of its own (the game never waits), when the iPhone app goes to the background, and from the Mac launcher once the host has exited (a crash included).<DM_SAVE>.synckeeps the hash of the save last known to be the same on both sides. Only the cloud's changed: it comes in. Only this one changed: it goes out. Both changed: the newer wins. Every save this replaces, local or in the cloud, is kept first in<DM_SAVE>-backups/<date> local|cloud.dm/mac/Launcher.swift, defaults keycloudSaves.folder, read bylauncher.c) and the iPhone launcher (a security-scoped bookmark,DataBookmark.m).DM_SAVE_SYNC(a folder,icloud, or0) overrides both. On the iPhone the copies go throughNSFileCoordinator(SaveSync.m), which downloads a file iCloud has only listed.The change is in vendored paths, so it is one patch,
patches/0025-cloud-saves.patch(Upstream-Status: pending, not sent to seedds/nxnative yet). There is no issue for it.Checked
dm/host/testsmake save-sync(newt_save_sync.c: 47 checks with forked "device" sessions, also under TSan and ASan/UBSan);python3 -m unittest dm.tools.tests.test_nsp_mac_app dm.tools.tests.test_launcher_settings(59 tests, OK);python3 -m unittest scripts.tests.test_patches(OK);python3 scripts/patches.py checkpasses. The rest ofdm/tools/testswas not run in full here.frontend/is not changed, soswift testwas not run.0100F2C0115B6000, update 1.4.3 (v720896), Mac app (Mac17,9, macOS 27.2) and iPhone app (iPhone 18 Pro Max, iPhone19,7, signed with a personal team) sharing one iCloud Drive folder. iPhone → Mac: the Mac brought in the iPhone's save at Play and kept its own insave-backups/… local. Mac → iPhone: the iPhone brought in the Mac's save at the next launch and kept its own. Both sides' hashes matched after each step.Screenshots
The new Cloud saves section of the Mac settings window (
dm/mac/Launcher.swift,CloudSavesSection), rendered from the view code in the window's grouped form at its width. English only: the launcher is not localized.Off:
A folder chosen (light and dark):
The iPhone launcher's section (the same picker and a Turn off cloud saves button) is not pictured.
Scope
dm/,scripts/,docs/,ref/,AGENTS.md) is edited directly: a change there is a patch inpatches/(scripts/patches.py export;patches.py checkpasses), with itsUpstream-Status