Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Cloud saves: ask the player which save wins, and wait for a listed iC…
…loud save to download

Both launchers read the cloud's save before Play. When the save changed on both this device and the cloud, they show both with their dates and device and set DM_SAVE_SYNC_CHOICE from the player's pick, instead of the newer file date winning. A save started fresh a minute after a real one has the later date and none of the progress: on Cadence of Hyrule the phone's new slot, saved 45 s after the Mac's, replaced a 54-minute save in the cloud, which survived only in the phone's save-backups. The second symptom is iCloud: a save it has only listed (.save.dmsync.icloud) could come back empty from the coordinated read, so the first start played this device's save and the second, after the download landed, brought the cloud's in.

dm/host/save_sync.{h,c}: save_sync_report() answers one key=value line about both saves (hash, newest date, file count, device), the sync base and whether both changed, fetching the cloud's save through the same front-end call the mount uses; dm_save_sync_title_id() gives a launcher the title ID the host was built for. FETCH_MS 10 s -> 20 s, since what it waits on is an iCloud download.

dm/ios/Sources/SaveSync.m: front_save_sync_fetch asks for a save iCloud has only listed and waits up to 25 s for the file itself before the coordinated read, logging how long it took; a read that still fails stays -1, which the host treats as unreadable (it plays this device's save and sends nothing), so a slow iCloud never replaces the cloud's progress.

dm/ios/Sources/Launcher.swift and dm/mac/Launcher.swift: the report runs off the main thread when Play is pressed; on a conflict a confirmationDialog (iPhone) or an NSAlert (Mac) offers this device's save, the cloud's, or Cancel, and sets DM_SAVE_SYNC_CHOICE=local:<hash> or cloud:<hash>; with no conflict it sets ask. A report that could not be read leaves the choice unset, because ask without a comparison keeps the cloud's save out of the session. Both read DM_SAVE with getenv: ProcessInfo's environment is a snapshot from before env.txt and the saved choices are applied. The iPhone shows "Checking the cloud save…" while the report waits.

dm/host/tests/t_save_sync.c: section 15, the report's states (nothing anywhere, only here, in step, only in the cloud, both changed with the cloud's date newer, the cloud unreadable, sync off).

Front-ends: new: save_sync_report(save, cloud_file, title, out, n) and dm_save_sync_title_id() in dm/host/save_sync.h — the line a launcher reads to ask before it starts the game
Front-ends: interface: DM_SAVE_SYNC_CHOICE=ask must be set only after a successful report; set blind, it keeps the cloud's save out of the session and sends nothing

Checked: make -C dm/host/tests GAME=0100000000010000 run (all pass; save sync 96 checks, metadata 41); Cadence of Hyrule 01000B900D8B0000 1.5.0 on an iPhone (iPhone19,4) and a Mac mini M2 Pro, macOS 26.6: both saves' dates shown on Play, the cloud choice brought the Mac's save in and the phone's was kept in save-backups, and a start with no conflict played as before. The iCloud wait was checked with a planted .save.dmsync.icloud in a scratch folder (DM_LAUNCHER=0): the host gave up at 20 s, the front end at 25 s, nothing written.
  • Loading branch information
kendrasloane90 authored and Kendra Sloane committed Nov 10, 2026
commit bb88fcf04ca41f5f757cb09a0a7d5b87be78e926
40 changes: 39 additions & 1 deletion dm/host/save_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@
#define SLOG(...) do { fprintf(stderr, "[dm] save sync: " __VA_ARGS__); fputc('\n', stderr); } while (0)
#define SRED(...) do { fprintf(stderr, "[dm] [RED] save sync: " __VA_ARGS__); fputc('\n', stderr); } while (0)
#define GUEST_NAME "org.d2rmobile.guest-name" // hle_fs.c FS_GUEST_NAME: a file's guest name when its host name had to differ (a case collision); part of the save
#if __has_include("game_profile.h") // the iOS build's -I reaches the generated profile; the Mac launcher compiles this file alone (dm/mac/pack_app.py) and has none
#include "game_profile.h"
#define SS_TITLE_ID DM_GAME_ID
#else
#define SS_TITLE_ID ""
#endif
#define QUIET_MS 3000 // a game often commits several times in a row: the sender waits until it has been quiet this long
#define FETCH_MS 10000 // the longest the game waits for the cloud's copy (a download); then it plays on with this device's save, which is not sent until the two have been compared
#define FETCH_MS 20000 // the longest the game waits for the cloud's copy (an iCloud download: the phone's front end also waits for the file, SaveSync.m)
#define KEEP_BACKUPS 10 // backups kept of each side (local, cloud); older ones are removed when a new one is made
enum { HDR = 8 + 4 + 4 + SS_HASH + 8 + 24 + 64, ENT = 24, CHUNK = 1 << 16 }; // header bytes, entry header bytes, the copy buffer
static const char MAGIC[8] __attribute__((nonstring)) = "DMSYNC1\n", END[8] __attribute__((nonstring)) = "DMSYNCE\n";
Expand Down Expand Up @@ -560,6 +566,38 @@
atomic_store(&g_on, 1);
}

// ---------- what a front end asks the player with ----------
// The two saves and the one last synced, as ss_decide sees them with a front end that asks (dm/host/save_sync.h). It fetches the cloud's file the way the mount does,
// so on the iPhone the read downloads a file iCloud has only listed; no game runs yet, so <save>.sync-tmp is free (the mount clears it anyway).
int save_sync_report(const char *save, const char *cloud, const char *title, char *out, size_t n) {
char dd[PATH_MAX], tmp[PATH_MAX], theirs[PATH_MAX], base[SS_HASH + 1], dev[64], line[512];
ss_meta mine, other; int got = -1, both = 0, same = 1;
memset(&mine, 0, sizeof mine); memset(&other, 0, sizeof other); base[0] = 0;
if (!save || !*save || !cloud || !*cloud) return -1;
snprintf(dd, sizeof dd, "%s", save); size_t L = strlen(dd); while (L > 1 && dd[L - 1] == '/') dd[--L] = 0;
if (ss_scan(dd, &mine)) return -1;
read_base(dd, base);
if (!strcmp(getenv("DM_SAVE_SYNC") ? getenv("DM_SAVE_SYNC") : "", "0")) { got = -1; } // off: report the local save alone

Check failure on line 580 in dm/host/save_sync.c

View workflow job for this annotation

GitHub Actions / clang-tidy (ratchet)

Null pointer passed as 1st argument to string comparison function [clang-analyzer-unix.cstring.NullArg] (1 in this file, the baseline has 0)
else {
snprintf(tmp, sizeof tmp, "%s.sync-tmp", dd); snprintf(theirs, sizeof theirs, "%s/front.dmsync", tmp);
if (!mkdirs(tmp)) {
got = fetch_timed(cloud, theirs, FETCH_MS);
if (got > 0 && ss_read(theirs, NULL, &other)) { SRED("the cloud's save %s cannot be read", cloud); got = -1; }
unlink(theirs); rmdir(tmp);
}
}
if (got > 0 && other.title[0] && strcasecmp(other.title, title)) same = 0;
ss_decide(mine.hash, base, got > 0 && other.files && same ? other.hash : "", mine.saved, other.saved, 1, &both);
snprintf(dev, sizeof dev, "%s", other.device); for (char *s = dev; *s; s++) if (*s == ' ') *s = '_';
snprintf(line, sizeof line, "cloud=%d local=%s saved=%lld files=%d chash=%s csaved=%lld cfiles=%d cdevice=%s sametitle=%d conflict=%d",
got, *mine.hash ? mine.hash : "-", (long long)mine.saved, mine.files,
got > 0 && *other.hash ? other.hash : "-", (long long)other.saved, other.files, *dev ? dev : "-", same, both && same);
if (snprintf(out, n, "%s\n", line) >= (int)n) return -1;
return 0;
}

const char *dm_save_sync_title_id(void) { return SS_TITLE_ID; }

void save_sync_mount(const char *save, const char *title) { // the first mount does it; a second one (the device save) waits for it
pthread_mutex_lock(&g_mount_mu);
if (!g_mounted) { g_mounted = 1; mount_once(save, title); }
Expand Down
14 changes: 14 additions & 0 deletions dm/host/save_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// one last synced, damaged, or the one the start chose against. A cloud that changed since (or that the start could not read) waits for the next start.
// Every save this replaces, here or in the cloud, is kept first in <DM_SAVE>-backups/<date> <side>; the newest 10 of each side stay.
#pragma once
#include <stddef.h>
#include <stdint.h>

#define SS_HASH 32 // hex digits of a save's hash: the first 16 bytes of a SHA-256
Expand All @@ -35,6 +36,19 @@ typedef enum { SS_KEEP, SS_PULL, SS_PUSH, SS_ASK } ss_way;
// player (`ask`), else the newer one by the times (the same time: this device's)
ss_way ss_decide(const char *local, const char *base, const char *remote, int64_t local_saved, int64_t remote_saved, int ask, int *both);

// ---- what a front end asks the player with (docs/frontends.md, The cloud saves folder) ----
// One line of key=value pairs, for the settings screen before the start:
// cloud=<1 read | 0 no file there | -1 unreadable> local=<hash|-> saved=<secs> files=<n> chash=<hash|-> csaved=<secs> cfiles=<n>
// cdevice=<hostname, '_' for a space> sametitle=<0|1> conflict=<0|1>
// conflict = both this device's save and the cloud's changed since the last sync (or this device's save has never synced and both exist):
// exactly the case a front end shows the two saves for and answers with DM_SAVE_SYNC_CHOICE. `save` is the committed save folder (DM_SAVE),
// `cloud` the packed file (<folder>/<title ID>/save.dmsync), `title` the title ID. 0 on success, -1 when the arguments do not fit or the save cannot be read.
int save_sync_report(const char *save, const char *cloud, const char *title, char *out, size_t n);

// The title ID of this build (dm/host/gen/<title>/game_profile.h), which the cloud folder and file are named after; "" when the build has no profile header
// (the Mac launcher, which reads the app's Info.plist DMTitleID instead).
const char *dm_save_sync_title_id(void);

// ---- the host ----
void save_sync_mount(const char *save, const char *title); // the game's first save mount (hle_fs.c): brings the cloud's save in when it is the one to keep, starts the sender
void save_sync_committed(void); // a Commit went through (hle_fs.c, under its locks: it only wakes the sender)
Expand Down
35 changes: 35 additions & 0 deletions dm/host/tests/t_save_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,41 @@ int main(void) {
backup_of(X, "cloud", back, sizeof back); snprintf(q, sizeof q, "%s/save/game.dat", back);
CHECK(*back && same(q, "y3"), "and the iPhone's save it replaced is kept on the Mac (%s)", back);

printf("(15) the report a front end reads before it plays\n");
char R1[1200], R2[1200], cloud4[1200], cf4[1400], line[2048];
snprintf(R1, sizeof R1, "%s/mac6/save", root); snprintf(R2, sizeof R2, "%s/iphone4/save", root);
snprintf(cloud4, sizeof cloud4, "%s/iCloud Drive/Fourth Saves", root);
mkd(R1); mkd(R2); mkd(cloud4); snprintf(cf4, sizeof cf4, "%s/%s/save.dmsync", cloud4, id);
CHECK(!save_sync_report(R1, cf4, DM_GAME_ID, line, sizeof line) && strstr(line, "cloud=0 ") && strstr(line, "local=- ") && strstr(line, "conflict=0"),
"no save here, none in the cloud: no conflict, nothing to replace (%s)", line);
snprintf(p, sizeof p, "%s/game.dat", R1); put(p, "r1");
CHECK(!save_sync_report(R1, cf4, DM_GAME_ID, line, sizeof line) && strstr(line, "files=1") && strstr(line, "chash=- ") && strstr(line, "conflict=0"),
"a save here, the cloud's empty: no conflict, the mount sends it (%s)", line);
CHECK(session(R1, cloud4, NULL, NULL) == 0 && cloud_has(cf4, root, "game.dat", "r1"), "the session sends it");
CHECK(!save_sync_report(R1, cf4, DM_GAME_ID, line, sizeof line) && strstr(line, "cloud=1 ") && strstr(line, "sametitle=1 ") && strstr(line, "conflict=0") && !strstr(line, "cdevice=- "),
"the same save here and in the cloud: no conflict, the cloud's device named (%s)", line);
snprintf(p, sizeof p, "%s/game.dat", R2);
CHECK(!save_sync_report(R2, cf4, DM_GAME_ID, line, sizeof line) && strstr(line, "local=- ") && strstr(line, "cfiles=1 ") && strstr(line, "conflict=0"),
"the cloud has one and this device none: it comes in, no conflict (%s)", line);
CHECK(session(R2, cloud4, NULL, NULL) == 0 && same(p, "r1"), "and the session brings it in");
CHECK(session(R2, NULL, "game.dat", "r2") == 0 && same(p, "r2"), "the iPhone plays with sync off and saves r2");
set_mtime(p, time(NULL) - 100);
CHECK(session(R1, cloud4, "game.dat", "r3") == 0 && cloud_has(cf4, root, "game.dat", "r3"), "then the Mac saves r3 and sends it");
CHECK(!save_sync_report(R2, cf4, DM_GAME_ID, line, sizeof line) && strstr(line, "conflict=1") && strstr(line, "cfiles=1 ") && strstr(line, "cdevice=Mac"),
"both changed: the report says conflict, with the cloud's device (%s)", line);
{ // the player is told which is newer by the two dates, so they have to be each save's own
char *cs = strstr(line, "csaved="), *sv = strstr(line, " saved=");
CHECK(cs && sv && atoll(cs + 7) > atoll(sv + 7), "the cloud's date is the newer of the two (%s)", line);
}
chmod(cf4, 0);
CHECK(!save_sync_report(R2, cf4, DM_GAME_ID, line, sizeof line) && strstr(line, "cloud=-1 ") && strstr(line, "chash=- ") && strstr(line, "conflict=0"),
"the cloud cannot be read: said so, and no conflict to offer (%s)", line);
chmod(cf4, 0644);
setenv("DM_SAVE_SYNC", "0", 1);
CHECK(!save_sync_report(R2, cf4, DM_GAME_ID, line, sizeof line) && strstr(line, "cloud=-1 ") && !strstr(line, "local=- "),
"sync off (DM_SAVE_SYNC=0): the local save alone is reported (%s)", line);
unsetenv("DM_SAVE_SYNC");

printf("== save sync: %d checks, %d failed ==\n", checks, fails);
if (!fails) { char c[1200]; snprintf(c, sizeof c, "rm -rf '%s'", root); system(c); }
return fails != 0;
Expand Down
1 change: 1 addition & 0 deletions dm/ios/Sources/DmIOS-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// The Objective-C functions Launcher.swift and GameMenu.swift call (CloudSaves.h, DmGameMenu.h). project.yml points SWIFT_OBJC_BRIDGING_HEADER here.
#import "CloudSaves.h"
#import "DmGameMenu.h"
#include "save_sync.h"
Loading
Loading