Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
Cloud saves: the Mac and iPhone apps of a game play on from one save through an iCloud Drive folder
mindw0rk5d7f5e953ff8
diff --git a/dm/host/Makefile b/dm/host/Makefileindex 2baa234b..2ea17ce7 100644--- a/dm/host/Makefile+++ b/dm/host/Makefile@@ -16,7 +16,7 @@ endifGAME_H = gen/$(GAME)/game_profile.hCFLAGS=-O1 -g -Wall -Wno-unused-function# Service-layer takeover hle_*.c files are included by wildcard: adding a file does not touch this line (avoids merge conflicts)-SRC=main.c loader.c hle.c romfs_arc.c crash.c probe.c thread.c memstat.m haptics_apple.m $(sort $(wildcard hle_*.c)) front_mac.m entry.S+SRC=main.c loader.c hle.c romfs_arc.c crash.c probe.c thread.c save_sync.c memstat.m haptics_apple.m $(sort $(wildcard hle_*.c)) front_mac.m entry.S# NVN null device + nn::vi + the Vulkan layer (dm/gfx/)# Sources under dm/gfx, dm/gfx/nvn, dm/gfx/tex, dm/gfx/vk and dm/gfx/gl are included by wildcard (tests/ and metal/ do not go into the host): adding a file does not touch this lineGFX=$(sort $(wildcard ../gfx/*.c ../gfx/nvn/*.c ../gfx/nvn/*.m ../gfx/nvn/*.S ../gfx/tex/*.c ../gfx/tex/*.m ../gfx/vk/*.m ../gfx/gl/*.m))@@ -50,7 +50,7 @@ endifendif.PHONY: FORCEFORCE:-dmhost: $(SRC) $(GFX) ../third_party/stb_vorbis/stb_vorbis.c dm.h hle.h romfs_image.h dm_input.h pad_slots.h hle_haptics.h haptics_apple.h motion_fusion.h motion_apple.h motion_pro.h motion_pro_mac.h front.h $(GFX_H) $(GAME_H) $(GAME_STAMP) $(VPX_LIB)+dmhost: $(SRC) $(GFX) ../third_party/stb_vorbis/stb_vorbis.c dm.h hle.h romfs_image.h save_sync.h dm_input.h pad_slots.h hle_haptics.h haptics_apple.h motion_fusion.h motion_apple.h motion_pro.h motion_pro_mac.h front.h $(GFX_H) $(GAME_H) $(GAME_STAMP) $(VPX_LIB)clang $(CFLAGS) -I../gfx/nvn/include -I../third_party/vulkan-headers/include -I$(dir $(GAME_H)) -I$(VPX) -I../third_party/stb_vorbis -fobjc-arc -o $@ $(SRC) $(GFX) $(VPX_LIB) -framework Cocoa -framework QuartzCore -framework Metal -framework MetalFX -framework GameController -framework CoreHaptics -framework IOKit -framework AudioToolbox -framework VideoToolbox -framework CoreMedia -framework CoreVideo -framework ImageIO -framework UniformTypeIdentifiersclean: # the host and everything make generates for it (gen/: the profile headers, the game stamp and the libvpx build; dmhost_* and *.dSYM beside them, as in .gitignore)rm -rf gen dmhost dmhost_* *.dSYMdiff --git a/dm/host/hle_fs.c b/dm/host/hle_fs.cindex 643306f9..df984ef6 100644--- a/dm/host/hle_fs.c+++ b/dm/host/hle_fs.c@@ -18,6 +18,7 @@#include <sys/clonefile.h>#include <sys/xattr.h>#include "romfs_image.h"+#include "save_sync.h"// Every take-over in this file runs on the big host stack (hle_bigstack.c): path buffers are sized by PATH_MAX and one file open takes 6-7 KB of stack, while a game's save thread can have as little as 8 KB (Super Mario 3D World + Bowser's Fury)#undef HLE_FORCE@@ -632,6 +633,7 @@ HLE_FORCE("_ZN2nn2fs13MountSaveDataEPKcRKNS_7account3UidE", "fsp-srv; save dataconst char *d = getenv("DM_SAVE");if (!d) die("DM_SAVE is not set: point it at a writable copy of a save directory");char dd[1024]; strlcpy(dd, d, sizeof dd); size_t L = strlen(dd); while (L > 1 && dd[L - 1] == '/') dd[--L] = 0; // strip trailing slashes (.work / .new are appended at the same level)+ save_sync_mount(dd, dm_profile()->id); // DM_SAVE_SYNC: the cloud's save comes in before the work directory is cloned from the committed one (save_sync.h)return mount_ex(name, dd, 0, 1);}// Device save data: one save for the console instead of one per user. Animal Crossing: New Horizons keeps its island there (3.0.3's main mounts no account save data at all); the host@@ -644,6 +646,7 @@ HLE_FORCE("_ZN2nn2fs19MountDeviceSaveDataEPKc", "fsp-srv; device save data → tconst char *d = getenv("DM_SAVE");if (!d) die("DM_SAVE is not set: point it at a writable copy of a save directory");char dd[1024]; strlcpy(dd, d, sizeof dd); size_t L = strlen(dd); while (L > 1 && dd[L - 1] == '/') dd[--L] = 0;+ save_sync_mount(dd, dm_profile()->id); // the cloud's save holds both, so it comes in at whichever of the two mounts firststrlcat(dd, "_device", sizeof dd);return mount_ex(name, dd, 0, 1);}@@ -653,6 +656,13 @@ HLE_FORCE("_ZN2nn2fs20ExtendDeviceSaveDataEll", "fsp-srv; extend device save datHLE_FORCE("_ZN2nn2fs22GetSaveDataRestoreFlagEPbPKc", "fsp-srv; save data restored from a backup: never", Result, h_fs_RestoreFlag, uint8_t *out, const char *name) { (void)name; *out = 0; return RES_OK; }static _Atomic uint64_t g_save_commits; // number of successful commits (used by the front end when going to background to wait for "the game's current save has been committed")uint64_t dm_fs_commit_count(void) { return atomic_load(&g_save_commits); }+// The cloud sender's copy of a committed save (save_sync.c): a Commit holds both locks for its clone and swap, so the clone taken under them is one whole committed version+int dm_fs_clone_committed(const char *dir, const char *to) {+ PATH_LOCK(); pthread_mutex_lock(&g_lock);+ int r = clonefile(dir, to, 0) ? -1 : 0, e = errno;+ pthread_mutex_unlock(&g_lock);+ errno = e; return r;+}// Both Commit overloads share this transaction engine. Caller holds the path// and mount locks; front-end failure notification is delivered after unlocking.static Result commit_mount_locked(int i, int *notify_error) {@@ -698,6 +708,7 @@ static Result commit_mount_locked(int i, int *notify_error) {static int n; if (r == RES_OK) { uint64_t tc2 = clock_gettime_nsec_np(CLOCK_MONOTONIC);LOG("save commit #%d: %s swapped atomically into %s (%d save writes so far) (clone + swap %llu us, since this transaction's first write %llu us)", ++n, g_mnt[i].dir, g_mnt[i].commit, atomic_load(&g_save_writes),(unsigned long long)((tc2 - tc1) / 1000), (unsigned long long)(tw0 ? (tc2 - tw0) / 1000 : 0)); atomic_fetch_add(&g_save_commits, 1);+ save_sync_committed(); // wakes the cloud sender, which clones the committed save through dm_fs_clone_committed above (save_sync.h)}return r;}diff --git a/dm/host/save_sync.c b/dm/host/save_sync.cnew file mode 100644index 00000000..212e8460--- /dev/null+++ b/dm/host/save_sync.c@@ -0,0 +1,531 @@+// Saves through a cloud folder (iCloud Drive): see save_sync.h. Plain C on system calls and CommonCrypto only, so the Mac launcher links it as the host does (dm/mac/pack_app.py).+// A packed save (<folder>/<title ID>/save.dmsync), little-endian:+// header "DMSYNC1\n", u32 version (1), u32 entries, 32 hex digits of the hash, i64 saved, char title[24], char device[64]+// entry u8 type (1 folder, 2 file), u8 0, u16 path length, u16 guest-name length, u16 0, u64 size, i64 mtime; the path (save/... or save_device/...), the guest name, the contents+// end "DMSYNCE\n", then the end of the file (a file cut off anywhere is refused)+// The hash runs over each entry's first 16 header bytes (not the date), its path, its guest name and its contents, in the order walk() visits them.+#include "save_sync.h"+#include <CommonCrypto/CommonDigest.h>+#include <TargetConditionals.h>+#include <ctype.h>+#include <dirent.h>+#include <errno.h>+#include <fcntl.h>+#include <limits.h>+#include <pthread.h>+#include <stdatomic.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <strings.h>+#include <sys/clonefile.h>+#include <sys/stat.h>+#include <sys/sysctl.h>+#include <sys/xattr.h>+#include <time.h>+#include <unistd.h>++#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+#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 at its first save mount for the cloud's copy (a download); then it plays on with this device's save+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";+static const char *const FOLDERS[2] = {"save", "save_device"}; // their names in a packed save; on disk DM_SAVE and DM_SAVE_device (hle_fs.c h_fs_MountDeviceSave)++// ---------- files ----------+static void folder_path(const char *save, int i, char *out, size_t n) { snprintf(out, n, i ? "%s_device" : "%s", save); }++static void free_names(char **v, size_t n) { for (size_t i = 0; i < n; i++) free(v[i]); free(v); }+static int by_name(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }+// A folder's names in byte order, without . and ..; -1 when it cannot be read+static int names_of(const char *dir, char ***out, size_t *count) {+ DIR *d = opendir(dir); if (!d) return -1;+ char **v = NULL; size_t n = 0, cap = 0; struct dirent *e; int r = 0;+ while ((e = readdir(d))) {+ if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;+ if (n == cap) { char **w = realloc(v, (cap = cap ? cap * 2 : 16) * sizeof *v); if (!w) { r = -1; break; } v = w; }+ if (!(v[n] = strdup(e->d_name))) { r = -1; break; }+ n++;+ }+ closedir(d);+ if (r) { free_names(v, n); return -1; }+ if (n) qsort(v, n, sizeof *v, by_name);+ *out = v; *count = n; return 0;+}++static int mkdirs(const char *path) { // mkdir -p+ char p[PATH_MAX]; if (snprintf(p, sizeof p, "%s", path) >= (int)sizeof p) return -1;+ for (char *s = p + 1; *s; s++) if (*s == '/') { *s = 0; mkdir(p, 0755); *s = '/'; }+ return mkdir(p, 0755) && errno != EEXIST ? -1 : 0;+}++static int rm_tree(const char *p) { // rm -rf: 0 when nothing is left+ struct stat st; if (lstat(p, &st)) return errno == ENOENT ? 0 : -1;+ if (!S_ISDIR(st.st_mode)) return unlink(p);+ char **v; size_t n; if (names_of(p, &v, &n)) return -1;+ int r = 0;+ for (size_t i = 0; i < n; i++) { char c[PATH_MAX]; snprintf(c, sizeof c, "%s/%s", p, v[i]); if (rm_tree(c)) r = -1; }+ free_names(v, n);+ return rmdir(p) ? -1 : r;+}++// Anything in it but Finder's .DS_Store: an empty folder is no save (the host makes DM_SAVE at every mount, DM_SAVE_device at the device save's)+static int has_entries(const char *dir) {+ char **v; size_t n, k = 0; if (names_of(dir, &v, &n)) return 0;+ for (size_t i = 0; i < n; i++) k += strcmp(v[i], ".DS_Store") != 0;+ free_names(v, n); return k > 0;+}++typedef int (*visit_fn)(void *ctx, const char *abs, const char *rel, const struct stat *st);+// Every entry under abs, depth first, a folder before what it holds, names in byte order: the order that gives the same save the same hash on every device.+// Finder's .DS_Store is left out (a save folder someone opened in Finder is still the same save), and so are links, which the host never makes in a save+static int walk(const char *abs, const char *rel, visit_fn fn, void *ctx) {+ char **v; size_t n; if (names_of(abs, &v, &n)) return -1;+ int r = 0;+ for (size_t i = 0; i < n && !r; i++) {+ if (!strcmp(v[i], ".DS_Store")) continue;+ char a[PATH_MAX], p[PATH_MAX]; struct stat st;+ if (snprintf(a, sizeof a, "%s/%s", abs, v[i]) >= (int)sizeof a || snprintf(p, sizeof p, "%s/%s", rel, v[i]) >= (int)sizeof p || lstat(a, &st)) r = -1;+ else if (S_ISDIR(st.st_mode)) { if (!(r = fn(ctx, a, p, &st))) r = walk(a, p, fn, ctx); }+ else if (S_ISREG(st.st_mode)) r = fn(ctx, a, p, &st);+ }+ free_names(v, n); return r;+}++static int write_all(int fd, const void *b, size_t n) {+ for (const char *p = b; n; ) { ssize_t k = write(fd, p, n); if (k < 0 && errno == EINTR) continue; if (k <= 0) return -1; p += k; n -= (size_t)k; }+ return 0;+}++static int copy_fd(int in, int out) {+ char *b = malloc(CHUNK); if (!b) return -1;+ ssize_t k; int r = 0;+ while ((k = read(in, b, CHUNK)) != 0) { if (k < 0) { if (errno == EINTR) continue; r = -1; break; } if (write_all(out, b, (size_t)k)) { r = -1; break; } }+ free(b); return r;+}++static void hex(const uint8_t *d, char *out) { for (int i = 0; i < SS_HASH / 2; i++) snprintf(out + 2 * i, 3, "%02x", d[i]); }++static void when(int64_t t, char *out, size_t n) { // a save's time in the log+ time_t tt = (time_t)t; struct tm tm;+ if (!t || !localtime_r(&tt, &tm) || !strftime(out, n, "%Y-%m-%d %H:%M:%S", &tm)) snprintf(out, n, "unknown");+}++static void device_name(char *out, size_t n) { // the model ("Mac16,1", "iPhone17,1"): which device a save came from, for the log+ char model[64] = ""; size_t len = sizeof model;+ if (sysctlbyname(TARGET_OS_OSX ? "hw.model" : "hw.machine", model, &len, NULL, 0)) model[0] = 0;+ snprintf(out, n, "%s", model[0] ? model : "unknown");+}++static void upper_title(const char *title, char *out, size_t n) { // one spelling of the title ID in the cloud, whoever packed the app+ size_t i = 0; for (; title[i] && i + 1 < n; i++) out[i] = (char)toupper((unsigned char)title[i]);+ out[i] = 0;+}++// ---------- packing ----------+typedef struct { CC_SHA256_CTX sha; FILE *out; ss_meta *m; uint32_t entries; uint8_t buf[CHUNK]; } packer;++static int pack_entry(void *c, const char *abs, const char *rel, const struct stat *st) {+ packer *k = c;+ char guest[1024]; ssize_t gl = getxattr(abs, GUEST_NAME, guest, sizeof guest, 0, XATTR_NOFOLLOW);+ if (gl < 0) gl = 0; // ENOATTR: the host name is the guest's+ size_t pl = strlen(rel); int file = S_ISREG(st->st_mode);+ if (pl > UINT16_MAX) return -1;+ uint8_t h[ENT] = {(uint8_t)(file ? 2 : 1)}; uint16_t pl16 = (uint16_t)pl, gl16 = (uint16_t)gl;+ uint64_t size = file ? (uint64_t)st->st_size : 0; int64_t mt = st->st_mtimespec.tv_sec;+ memcpy(h + 2, &pl16, 2); memcpy(h + 4, &gl16, 2); memcpy(h + 8, &size, 8); memcpy(h + 16, &mt, 8);+ CC_SHA256_Update(&k->sha, h, 16); CC_SHA256_Update(&k->sha, rel, (CC_LONG)pl); CC_SHA256_Update(&k->sha, guest, (CC_LONG)gl);+ if (k->out && (fwrite(h, 1, ENT, k->out) != ENT || fwrite(rel, 1, pl, k->out) != pl || fwrite(guest, 1, (size_t)gl, k->out) != (size_t)gl)) return -1;+ k->entries++;+ if (!file) return 0;+ int fd = open(abs, O_RDONLY); if (fd < 0) return -1;+ uint64_t left = size; ssize_t got = 0;+ while (left && (got = read(fd, k->buf, left < CHUNK ? (size_t)left : CHUNK)) != 0) {+ if (got < 0) { if (errno == EINTR) continue; break; }+ CC_SHA256_Update(&k->sha, k->buf, (CC_LONG)got);+ if (k->out && fwrite(k->buf, 1, (size_t)got, k->out) != (size_t)got) break;+ left -= (uint64_t)got;+ }+ close(fd);+ if (left) return -1; // a failed read, or a file shorter than its size (the save is a clone no Commit touches, so not one being written)+ k->m->files++;+ if (mt > k->m->saved) k->m->saved = mt;+ return 0;+}++static int pack_save(const char *save, packer *k) {+ CC_SHA256_Init(&k->sha);+ for (int i = 0; i < 2; i++) {+ char dir[PATH_MAX]; folder_path(save, i, dir, sizeof dir);+ if (has_entries(dir) && walk(dir, FOLDERS[i], pack_entry, k)) return -1;+ }+ uint8_t d[CC_SHA256_DIGEST_LENGTH]; CC_SHA256_Final(d, &k->sha); hex(d, k->m->hash);+ if (!k->m->files) k->m->hash[0] = 0; // no save: no hash, so that "nothing here" compares as nothing+ return 0;+}++int ss_scan(const char *save, ss_meta *m) {+ memset(m, 0, sizeof *m);+ packer *k = calloc(1, sizeof *k); if (!k) return -1;+ k->m = m; int r = pack_save(save, k);+ free(k); return r;+}++int ss_pack(const char *save, const char *title, const char *out, ss_meta *m) {+ memset(m, 0, sizeof *m);+ upper_title(title, m->title, sizeof m->title); device_name(m->device, sizeof m->device);+ packer *k = calloc(1, sizeof *k); if (!k) return -1;+ k->m = m; int r = -1;+ uint8_t hdr[HDR] = {0}; uint32_t version = 1;+ if (!(k->out = fopen(out, "wb")) || fwrite(hdr, 1, HDR, k->out) != HDR || pack_save(save, k) || fwrite(END, 1, sizeof END, k->out) != sizeof END) goto done;+ memcpy(hdr, MAGIC, 8); memcpy(hdr + 8, &version, 4); memcpy(hdr + 12, &k->entries, 4); memcpy(hdr + 16, m->hash, strlen(m->hash));+ memcpy(hdr + 48, &m->saved, 8); memcpy(hdr + 56, m->title, strlen(m->title)); memcpy(hdr + 80, m->device, strlen(m->device));+ if (fseeko(k->out, 0, SEEK_SET) || fwrite(hdr, 1, HDR, k->out) != HDR || fflush(k->out) || fsync(fileno(k->out))) goto done;+ r = 0;+done:+ if (k->out && fclose(k->out)) r = -1;+ if (r) unlink(out);+ free(k); return r;+}++// A path in a packed save: under save/ or save_device/, with no empty, . or .. part (a damaged file must not write outside `into`)+static int path_ok(const char *p) {+ if (strncmp(p, "save/", 5) && strncmp(p, "save_device/", 12)) return 0;+ for (const char *s = p;; ) {+ const char *e = strchr(s, '/'); size_t n = e ? (size_t)(e - s) : strlen(s);+ if (!n || (n == 1 && s[0] == '.') || (n == 2 && s[0] == '.' && s[1] == '.')) return 0;+ if (!e) return 1;+ s = e + 1;+ }+}++int ss_read(const char *bundle, const char *into, ss_meta *m) {+ memset(m, 0, sizeof *m);+ FILE *f = fopen(bundle, "rb"); if (!f) return -1;+ uint8_t *buf = malloc(CHUNK), hdr[HDR], h[ENT], end[sizeof END], d[CC_SHA256_DIGEST_LENGTH];+ char want[SS_HASH + 1] = "", path[PATH_MAX], guest[1024], out[PATH_MAX];+ uint32_t version, n; int r = -1, fd = -1;+ CC_SHA256_CTX sha; CC_SHA256_Init(&sha);+ if (!buf || fread(hdr, 1, HDR, f) != HDR || memcmp(hdr, MAGIC, 8)) goto done;+ memcpy(&version, hdr + 8, 4); memcpy(&n, hdr + 12, 4);+ if (version != 1) goto done;+ memcpy(want, hdr + 16, SS_HASH); memcpy(&m->saved, hdr + 48, 8); memcpy(m->title, hdr + 56, 23); memcpy(m->device, hdr + 80, 63);+ for (uint32_t i = 0; i < n; i++) {+ uint16_t pl, gl; uint64_t size; int64_t mt;+ if (fread(h, 1, ENT, f) != ENT) goto done;+ memcpy(&pl, h + 2, 2); memcpy(&gl, h + 4, 2); memcpy(&size, h + 8, 8); memcpy(&mt, h + 16, 8);+ if ((h[0] != 1 && h[0] != 2) || !pl || pl >= sizeof path || gl > sizeof guest || (h[0] == 1 && size)) goto done;+ if (fread(path, 1, pl, f) != pl || fread(guest, 1, gl, f) != gl) goto done;+ path[pl] = 0;+ if (strlen(path) != pl || !path_ok(path)) goto done;+ CC_SHA256_Update(&sha, h, 16); CC_SHA256_Update(&sha, path, pl); CC_SHA256_Update(&sha, guest, gl);+ if (into) {+ if (snprintf(out, sizeof out, "%s/%s", into, path) >= (int)sizeof out) goto done;+ if (h[0] == 1) { if (mkdirs(out)) goto done; }+ else {+ *strrchr(out, '/') = 0; int made = mkdirs(out); out[strlen(out)] = '/';+ if (made || (fd = open(out, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) goto done;+ }+ if (gl && setxattr(out, GUEST_NAME, guest, gl, 0, XATTR_NOFOLLOW)) goto done;+ }+ for (uint64_t left = size; left; ) {+ size_t k = left < CHUNK ? (size_t)left : CHUNK;+ if (fread(buf, 1, k, f) != k) goto done;+ CC_SHA256_Update(&sha, buf, (CC_LONG)k);+ if (fd >= 0 && write_all(fd, buf, k)) goto done;+ left -= k;+ }+ if (fd >= 0) { // the file's own date back, so the Mac front end's Saves sheet compares the save's age, not the copy's+ struct timespec t[2] = {{(time_t)mt, 0}, {(time_t)mt, 0}}; futimens(fd, t);+ int bad = close(fd); fd = -1; if (bad) goto done;+ }+ if (h[0] == 2) m->files++;+ }+ if (fread(end, 1, sizeof end, f) != sizeof end || memcmp(end, END, sizeof end) || fgetc(f) != EOF) goto done;+ CC_SHA256_Final(d, &sha); hex(d, m->hash);+ if (!m->files) m->hash[0] = 0;+ if (strcmp(m->hash, want)) goto done;+ r = 0;+done:+ if (fd >= 0) close(fd);+ free(buf); fclose(f);+ if (r) m->files = 0, m->hash[0] = 0;+ return r;+}++ss_way ss_decide(const char *local, const char *base, const char *remote, int64_t local_saved, int64_t remote_saved, int *both) {+ *both = 0;+ if (!*remote) return *local ? SS_PUSH : SS_KEEP; // nothing in the cloud+ if (!strcmp(local, remote)) return SS_KEEP; // already the same+ if (!*local) return SS_PULL; // nothing here+ if (*base && !strcmp(local, base)) return SS_PULL; // only the cloud's changed+ if (*base && !strcmp(remote, base)) return SS_PUSH; // only this one changed+ *both = 1; // both changed, or this device's first sync with a save of its own+ return remote_saved > local_saved ? SS_PULL : SS_PUSH;+}++// ---------- the sync state, backups, the cloud's file ----------+static void read_base(const char *save, char *out) { // <save>.sync: the hash of the save last known to be the same here and in the cloud ("" for none)+ char p[PATH_MAX], ln[128]; out[0] = 0;+ snprintf(p, sizeof p, "%s.sync", save);+ FILE *f = fopen(p, "r"); if (!f) return;+ while (fgets(ln, sizeof ln, f)) if (!strncmp(ln, "base\t", 5) && strspn(ln + 5, "0123456789abcdef") == SS_HASH) { memcpy(out, ln + 5, SS_HASH); out[SS_HASH] = 0; }+ fclose(f);+}++static void write_base(const char *save, const char *hash) { // written beside it and renamed over it: a cut-off write must not read as another base+ char p[PATH_MAX], t[PATH_MAX + 8];+ snprintf(p, sizeof p, "%s.sync", save); snprintf(t, sizeof t, "%s.tmp", p);+ FILE *f = fopen(t, "w");+ if (!f || fprintf(f, "# save sync (dm/host/save_sync.h): the save last known to be the same here and in the cloud\nbase\t%s\n", hash) < 0 || fclose(f) || rename(t, p)) {+ if (f) unlink(t);+ SRED("cannot write %s (errno=%d): the next start compares with the sync before", p, errno);+ }+}++// A folder for a save about to be replaced: <save>-backups/<date> <side> (local: this device's, cloud: the cloud's), beside the ones the Mac front end's save copy makes (SaveTransfer.swift)+static int backup_dir(const char *save, const char *side, char *out, size_t n) {+ char stamp[32]; time_t t = time(NULL); struct tm tm; localtime_r(&t, &tm); strftime(stamp, sizeof stamp, "%Y-%m-%d %H.%M.%S", &tm);+ snprintf(out, n, "%s-backups/%s %s", save, stamp, side);+ for (int k = 2; !access(out, F_OK); k++) snprintf(out, n, "%s-backups/%s %s %d", save, stamp, side, k);+ return mkdirs(out);+}++// DM_SAVE_SYNC → <folder>/<title ID>/save.dmsync; 0 when sync is off+static int cloud_file(const char *title, char *out, size_t n) {+ const char *v = getenv("DM_SAVE_SYNC"); char dir[PATH_MAX], id[24];+ if (!v || !*v || !strcmp(v, "0")) return 0;+ if (!strcmp(v, "icloud")) {+#if TARGET_OS_OSX+ const char *home = getenv("HOME"); char drive[PATH_MAX]; struct stat st;+ snprintf(drive, sizeof drive, "%s/Library/Mobile Documents/com~apple~CloudDocs", home ? home : "");+ if (!home || stat(drive, &st)) {+ if (errno == EPERM || errno == EACCES) SLOG("off: this app may not open iCloud Drive (System Settings > Privacy & Security > Files and Folders)");+ else SLOG("off: iCloud Drive is not on for this Mac (no %s)", drive);+ return 0;+ }+ snprintf(dir, sizeof dir, "%s/NX2Apple Saves", drive);+#else+ SLOG("off: DM_SAVE_SYNC=icloud is the Mac's iCloud Drive folder; on this device, choose the folder in the launcher (Cloud saves)");+ return 0;+#endif+ } else snprintf(dir, sizeof dir, "%s", v);+ size_t L = strlen(dir); while (L > 1 && dir[L - 1] == '/') dir[--L] = 0;+ upper_title(title, id, sizeof id);+ if (!*id || strchr(id, '/')) return 0;+ // The game's own folder chosen instead of the one above it (a picker shows <folder>/<title ID> once another device has synced): taken as it is, not given a second <title ID>+ const char *last = strrchr(dir, '/');+ if (!strcasecmp(last ? last + 1 : dir, id)) return snprintf(out, n, "%s/save.dmsync", dir) < (int)n;+ return snprintf(out, n, "%s/%s/save.dmsync", dir, id) < (int)n;+}++// The platform's fetch, given at most `ms`: a download iCloud never finishes must not hold the game at its first save mount. One that comes too late is dropped+typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int done, abandoned, result; char cloud[PATH_MAX], local[PATH_MAX]; } fetch_job;+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); }+ return NULL;+}+static void deadline(int ms, struct timespec *ts) {+ clock_gettime(CLOCK_REALTIME, ts);+ ts->tv_sec += ms / 1000; ts->tv_nsec += (long)(ms % 1000) * 1000000;+ if (ts->tv_nsec >= 1000000000) { ts->tv_sec++; ts->tv_nsec -= 1000000000; }+}+static int fetch_timed(const char *cloud, const char *local, int ms) { // front_save_sync_fetch's answer, or -1 at the time limit+ fetch_job *j = calloc(1, sizeof *j); pthread_t t;+ if (!j) return -1;+ pthread_mutex_init(&j->mu, NULL); pthread_cond_init(&j->cv, NULL);+ snprintf(j->cloud, sizeof j->cloud, "%s", cloud); snprintf(j->local, sizeof j->local, "%s", local);+ if (pthread_create(&t, NULL, fetch_thread, j)) { pthread_mutex_destroy(&j->mu); pthread_cond_destroy(&j->cv); free(j); return front_save_sync_fetch(cloud, local); }+ pthread_detach(t);+ struct timespec ts; deadline(ms, &ts);+ pthread_mutex_lock(&j->mu);+ while (!j->done && pthread_cond_timedwait(&j->cv, &j->mu, &ts) != ETIMEDOUT) {}+ int r = j->done ? j->result : -1, mine = j->done;+ if (!mine) j->abandoned = 1; // the thread frees it when it ends+ pthread_mutex_unlock(&j->mu);+ if (mine) { pthread_mutex_destroy(&j->mu); pthread_cond_destroy(&j->cv); free(j); }+ else SLOG("the cloud's save did not come within %d s", ms / 1000);+ return r;+}++// ---------- the two directions ----------+// The cloud's save replaces this one. It is unpacked beside it first, so a pull that fails leaves this save as it was; then each folder is swapped in at once+// (renamex_np, as a Commit swaps), and the one it replaced goes to the backups. kept: that backup, "" when this device had no save+static int pull(const char *save, const char *bundle, ss_meta *r, char *kept, size_t kn) {+ char tmp[PATH_MAX]; snprintf(tmp, sizeof tmp, "%s.sync-tmp/in", save);+ kept[0] = 0;+ if (rm_tree(tmp) || mkdirs(tmp) || ss_read(bundle, tmp, r)) { rm_tree(tmp); return -1; }+ for (int i = 0; i < 2; i++) {+ char here[PATH_MAX], incoming[PATH_MAX], old[PATH_MAX];+ folder_path(save, i, here, sizeof here); snprintf(incoming, sizeof incoming, "%s/%s", tmp, FOLDERS[i]);+ int fresh = !access(incoming, F_OK), had = has_entries(here);+ if (had && !*kept && backup_dir(save, "local", kept, kn)) return -1;+ snprintf(old, sizeof old, "%s/%s", kept, FOLDERS[i]);+ 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+ }+ rm_tree(tmp);+ return 0;+}++// What this device committed goes to the cloud, unless the cloud already has it. A cloud save changed since this device's last sync (another device sent it) is kept first.+// 1 = in step with the cloud, 0 = not: the cloud cannot be read or written now (the next Commit, a flush or the Mac launcher tries again)+static int send_save(const char *save, const char *title, const char *cloud) {+ char tmp[PATH_MAX], snap[PATH_MAX], packed[PATH_MAX], theirs[PATH_MAX], base[SS_HASH + 1], dir[PATH_MAX], kept[PATH_MAX], t[32];+ ss_meta mine, other; int ok = 0, got;+ snprintf(tmp, sizeof tmp, "%s.sync-tmp/out", save); snprintf(snap, sizeof snap, "%s/s", tmp);+ snprintf(packed, sizeof packed, "%s/save.dmsync", tmp); snprintf(theirs, sizeof theirs, "%s/cloud.dmsync", tmp);+ if (rm_tree(tmp) || mkdirs(tmp)) { SRED("cannot make %s (errno=%d)", tmp, errno); goto done; }+ // A clone of the committed folders, taken while no Commit can swap them: the game may commit again while this packs+ 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; }+ }+ 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+ if ((got = fetch_timed(cloud, theirs, FETCH_MS)) < 0) { SLOG("the cloud's save %s cannot be read now; sending later", cloud); goto done; }+ if (got && ss_read(theirs, NULL, &other)) SRED("the cloud's save %s is damaged; this device's replaces it", cloud);+ else if (got && !strcmp(other.hash, mine.hash)) { write_base(save, mine.hash); ok = 1; 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);+ }+ snprintf(dir, sizeof dir, "%s", cloud); *strrchr(dir, '/') = 0;+ if (mkdirs(dir) || front_save_sync_store(packed, cloud) != 1) { SRED("sending to %s failed (errno=%d)", cloud, errno); goto done; }+ write_base(save, mine.hash); ok = 1;+ when(mine.saved, t, sizeof t);+ SLOG("sent: %d files, saved %s, to %s", mine.files, t, cloud);+done:+ rm_tree(tmp); *strrchr(tmp, '/') = 0; rmdir(tmp); // <save>.sync-tmp too, once nothing else is in it (a fetch that came too late may still be)+ return ok;+}++// ---------- the host: the first mount, the sender, flushes ----------+static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER, g_mount_mu = PTHREAD_MUTEX_INITIALIZER;+static pthread_cond_t g_cv = PTHREAD_COND_INITIALIZER;+static _Atomic int g_on;+static int g_mounted, g_want, g_busy, g_failed; // the mount ran; a send asked for without a Commit (the mount found this save newer, a flush); a send running; the last one failed+static uint64_t g_commits, g_sent; // Commits seen; the Commits the last send covered+static char g_save[PATH_MAX], g_title[24], g_cloud[PATH_MAX];++static void *sender(void *arg) {+ (void)arg;+ pthread_mutex_lock(&g_mu);+ for (;;) {+ while (g_commits == g_sent && !g_want) pthread_cond_wait(&g_cv, &g_mu);+ for (uint64_t seen = g_commits; !g_want; ) { // quiet for QUIET_MS since the last Commit (a flush does not wait)+ struct timespec ts; deadline(QUIET_MS, &ts);+ int r = 0;+ while (!g_want && g_commits == seen && r != ETIMEDOUT) r = pthread_cond_timedwait(&g_cv, &g_mu, &ts);+ if (g_commits == seen) break;+ seen = g_commits;+ }+ uint64_t upto = g_commits; g_want = 0; g_busy = 1;+ pthread_mutex_unlock(&g_mu);+ int ok = send_save(g_save, g_title, g_cloud);+ pthread_mutex_lock(&g_mu);+ g_busy = 0; g_sent = upto; g_failed = !ok;+ pthread_cond_broadcast(&g_cv);+ }+ return NULL;+}++static void mount_once(const char *save, const char *title) {+ char cloud[PATH_MAX], tmp[PATH_MAX], theirs[PATH_MAX], base[SS_HASH + 1], kept[PATH_MAX], t[32];+ ss_meta mine, other; memset(&other, 0, sizeof other);+ if (!cloud_file(title, cloud, sizeof cloud)) return;+ SLOG("on: %s", cloud);+ snprintf(tmp, sizeof tmp, "%s.sync-tmp", save); snprintf(theirs, sizeof theirs, "%s/in.dmsync", tmp);+ if (rm_tree(tmp) || mkdirs(tmp)) { SRED("cannot make %s (errno=%d): off", tmp, errno); return; } // rm_tree: what an earlier session left+ if (ss_scan(save, &mine)) { SRED("this device's save %s cannot be read (errno=%d): off", save, errno); return; }+ read_base(save, base);+ int got = fetch_timed(cloud, theirs, FETCH_MS), both = 0, failed = 0;+ if (got > 0 && ss_read(theirs, NULL, &other)) { SRED("the cloud's save %s is damaged: playing on with this device's, which replaces it", cloud); got = 0; }+ else if (got > 0 && strcasecmp(other.title, title)) { SRED("the cloud's save %s is %s's, not %s's: not used", cloud, other.title, title); got = -1; }+ if (got < 0) { SLOG("playing on with this device's save; it is sent after the game saves, unless the cloud's has changed meanwhile (then that one is kept)"); failed = 1; }+ switch (got < 0 ? SS_KEEP : ss_decide(mine.hash, base, got ? other.hash : "", mine.saved, other.saved, &both)) {+ case SS_PULL:+ when(other.saved, t, sizeof t);+ if (pull(save, theirs, &other, kept, sizeof kept)) { SRED("bringing the cloud's save in failed (errno=%d): playing on with this device's", errno); failed = 1; break; }+ write_base(save, other.hash);+ SLOG("brought in the cloud's save (from %s, saved %s%s)%s%s", other.device, t, both ? ", the newer of two that both changed" : "", *kept ? "; this device's is kept in " : "", kept);+ break;+ case SS_PUSH:+ when(mine.saved, t, sizeof t);+ SLOG("this device's save (saved %s) %s: it goes to the cloud", t, !got ? "is not in the cloud yet" : both ? "is the newer of two that both changed" : "changed since the last sync");+ g_want = 1;+ break;+ case SS_KEEP:+ if (got > 0 && mine.files) { write_base(save, mine.hash); SLOG("in step with the cloud"); }+ else if (!got) SLOG("no save here or in the cloud yet");+ break;+ }+ rm_tree(tmp);+ snprintf(g_save, sizeof g_save, "%s", save); upper_title(title, g_title, sizeof g_title); snprintf(g_cloud, sizeof g_cloud, "%s", cloud);+ g_failed = failed;+ pthread_t th;+ if (pthread_create(&th, NULL, sender, NULL)) { SRED("cannot start the sender: saves are not sent this session"); return; }+ pthread_detach(th);+ atomic_store(&g_on, 1);+}++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); }+ pthread_mutex_unlock(&g_mount_mu);+}++void save_sync_committed(void) {+ if (!atomic_load(&g_on)) return;+ pthread_mutex_lock(&g_mu); g_commits++; pthread_cond_broadcast(&g_cv); pthread_mutex_unlock(&g_mu);+}++int save_sync_flush(int timeout_ms) {+ if (!atomic_load(&g_on)) return 1;+ struct timespec ts; deadline(timeout_ms, &ts);+ pthread_mutex_lock(&g_mu);+ if (g_commits != g_sent || g_failed || g_busy) { g_want = 1; pthread_cond_broadcast(&g_cv); }+ while ((g_want || g_busy || g_commits != g_sent) && pthread_cond_timedwait(&g_cv, &g_mu, &ts) != ETIMEDOUT) {}+ int ok = !g_want && !g_busy && g_commits == g_sent && !g_failed;+ pthread_mutex_unlock(&g_mu);+ return ok;+}++int save_sync_after_exit(const char *save, const char *title) {+ char cloud[PATH_MAX];+ return !cloud_file(title, cloud, sizeof cloud) || send_save(save, title, cloud);+}++// ---------- the platform's parts: these defaults are plain file copies, which the Mac's iCloud Drive takes (it downloads a file a read asks for) ----------+__attribute__((weak)) int front_save_sync_fetch(const char *cloud, const char *local) {+ int in = open(cloud, O_RDONLY); if (in < 0) return errno == ENOENT ? 0 : -1;+ int out = open(local, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (out < 0) { close(in); return -1; }+ int r = copy_fd(in, out); close(in);+ if (close(out)) r = -1;+ if (r) unlink(local);+ return r ? -1 : 1;+}++// Written beside the cloud file under a .nosync name (iCloud Drive does not upload those) and renamed over it: iCloud sees one whole new file+__attribute__((weak)) int front_save_sync_store(const char *local, const char *cloud) {+ char part[PATH_MAX]; snprintf(part, sizeof part, "%s.%d.nosync", cloud, (int)getpid());+ int in = open(local, O_RDONLY); if (in < 0) return 0;+ int out = open(part, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (out < 0) { close(in); return 0; }+ int r = copy_fd(in, out);+ if (!r && fsync(out)) r = -1;+ close(in);+ if (close(out)) r = -1;+ if (!r && rename(part, cloud)) r = -1;+ if (r) unlink(part);+ return !r;+}++__attribute__((weak)) int dm_fs_clone_committed(const char *dir, const char *to) { return clonefile(dir, to, 0) ? -1 : 0; }diff --git a/dm/host/save_sync.h b/dm/host/save_sync.hnew file mode 100644index 00000000..4f16f89c--- /dev/null+++ b/dm/host/save_sync.h@@ -0,0 +1,47 @@+// Saves through a cloud folder (iCloud Drive), so the Mac app and the iPhone app of one game play on from the same save. Off unless DM_SAVE_SYNC names the folder+// (each app's launcher sets the folder the player chose in its Cloud saves: dm/mac/launcher.c from the Mac settings window, DataBookmark.m on the iPhone; "icloud" is iCloud Drive/NX2Apple Saves).+// The folder holds one file per game, <folder>/<title ID>/save.dmsync (a folder named after the title ID is taken as the game's own): the committed save (DM_SAVE and DM_SAVE_device) packed into one file. 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.+// When: the cloud's copy comes in at the game's first save mount (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.+// Which way: <DM_SAVE>.sync keeps the hash of the save last known to be the same here and in the cloud. Only the cloud's changed: it comes in; only this one changed: it goes out;+// both changed (played on both without syncing in between, or the first sync of a device that has a save of its own): the newer one wins and the other is kept.+// Every save this replaces, here or in the cloud, is kept first in <DM_SAVE>-backups/<date> <side> (the folder the Mac front end's Saves sheet opens).+#pragma once+#include <stdint.h>++#define SS_HASH 32 // hex digits of a save's hash: the first 16 bytes of a SHA-256++typedef struct {+ char hash[SS_HASH + 1]; // over the paths, the guest names and the contents, not the dates: the same save has the same hash on every device+ int64_t saved; // the newest file's modification time (seconds since 1970): which of two changed saves is newer+ char title[24], device[64];+ int files; // regular files; 0 = no save+} ss_meta;++// ---- the parts (dm/host/tests/t_save_sync.c calls them) ----+// The committed save as it is on disk (save, and save + "_device"). 0 on success; m->files == 0: no save yet+int ss_scan(const char *save, ss_meta *m);+// Packs the save into one file. 0 on success; m receives what was packed+int ss_pack(const char *save, const char *title, const char *out, ss_meta *m);+// Reads a packed save and checks all of it (the header, every entry, the hash, the end marker); with `into`, unpacks it there as into/save and into/save_device.+// 0 on success; a damaged or cut-off file, or a path that leaves the save, fails+int ss_read(const char *bundle, const char *into, ss_meta *m);+typedef enum { SS_KEEP, SS_PULL, SS_PUSH } ss_way;+// Which way, from the three hashes ("" for none: no save here, no sync yet, nothing in the cloud) and, when both sides changed, their times. *both = both changed+ss_way ss_decide(const char *local, const char *base, const char *remote, int64_t local_saved, int64_t remote_saved, int *both);++// ---- 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)+int save_sync_flush(int timeout_ms); // sends now what was committed and not sent (the iPhone app going to the background): 1 = in step with the cloud, 0 = not (yet)+int save_sync_after_exit(const char *save, const char *title); // the Mac launcher once the host has exited (a crash included): 1 = in step, 0 = not++// ---- what the platform provides (weak defaults in save_sync.c: plain file copies, which the Mac's iCloud Drive takes) ----+// Copies the cloud's file to a local one; the iPhone app goes through NSFileCoordinator, which downloads a file iCloud has only listed (SaveSync.m).+// 1 = copied, 0 = there is none, -1 = failed+int front_save_sync_fetch(const char *cloud, const char *local);+// Puts a local file at the cloud path, replacing the file there as a whole. 1 = done+int front_save_sync_store(const char *local, const char *cloud);+// Clones the committed save folder `dir` to `to` while no Commit can swap it (hle_fs.c takes its lock; the default, with no host running, does not need one). 0 on success+int dm_fs_clone_committed(const char *dir, const char *to);diff --git a/dm/host/tests/Makefile b/dm/host/tests/Makefileindex 26bc1eac..1998f813 100644--- a/dm/host/tests/Makefile+++ b/dm/host/tests/Makefile@@ -21,6 +21,7 @@ run: $(B)/t_motion $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture $(B)/t_oe $(B)/tTMPDIR="$(abspath $(B))" $(B)/t_fs_commit_list$(B)/t_thread_leakTMPDIR="$(abspath $(B))" $(B)/t_fs_case $(QUIET)+ TMPDIR="$(abspath $(B))" $(B)/t_save_sync $(QUIET)$(B)/t_audio$(B)/t_audio_spsc$(B)/t_fpslock@@ -95,17 +96,23 @@ $(B)/t_gesture: t_gesture.c ../hle_touch.c ../front.h ../dm.h $(GAME_H) | $(B)$(CLANG) -O1 -Wall -Wno-unused-function -I.. $(GINC) -o $@ t_gesture.c ../hle_touch.c$(B)/t_thread_leak: t_thread_leak.c ../thread.c ../dm.h $(GAME_H) | $(B)$(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_thread_leak.c-$(B)/t_fs: t_fs.c t_fs_stub.c ../hle_fs.c ../hle_bigstack.c ../romfs_arc.c ../hle.h ../dm.h $(GAME_H) ../hle_profile.c ../hle_profile.h | $(B)- $(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_fs.c t_fs_stub.c ../hle_bigstack.c ../romfs_arc.c ../hle_profile.c-$(B)/t_fs_commit_list: t_fs_commit_list.c t_fs_stub.c ../hle_fs.c ../hle_bigstack.c ../romfs_arc.c ../hle.h ../dm.h $(GAME_H) ../hle_profile.c ../hle_profile.h | $(B)- $(CLANG) -O1 -Wall -Wno-unused-function -I.. $(GINC) -o $@ t_fs_commit_list.c t_fs_stub.c ../hle_bigstack.c ../romfs_arc.c ../hle_profile.c-$(B)/t_fs_commit_list_tsan: t_fs_commit_list.c t_fs_stub.c ../hle_fs.c ../hle_bigstack.c ../hle.h ../dm.h $(GAME_H) | $(B)- $(CLANG) -O1 -g -fsanitize=thread -Wall -Wno-unused-function -I.. $(GINC) -o $@ t_fs_commit_list.c t_fs_stub.c ../hle_bigstack.c ../hle_profile.c+$(B)/t_fs: t_fs.c t_fs_stub.c ../hle_fs.c ../save_sync.c ../save_sync.h ../hle_bigstack.c ../romfs_arc.c ../hle.h ../dm.h $(GAME_H) ../hle_profile.c ../hle_profile.h | $(B)+ $(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_fs.c t_fs_stub.c ../hle_bigstack.c ../save_sync.c ../romfs_arc.c ../hle_profile.c+$(B)/t_fs_commit_list: t_fs_commit_list.c t_fs_stub.c ../hle_fs.c ../save_sync.c ../save_sync.h ../hle_bigstack.c ../romfs_arc.c ../hle.h ../dm.h $(GAME_H) ../hle_profile.c ../hle_profile.h | $(B)+ $(CLANG) -O1 -Wall -Wno-unused-function -I.. $(GINC) -o $@ t_fs_commit_list.c t_fs_stub.c ../hle_bigstack.c ../save_sync.c ../romfs_arc.c ../hle_profile.c+$(B)/t_fs_commit_list_tsan: t_fs_commit_list.c t_fs_stub.c ../hle_fs.c ../save_sync.c ../save_sync.h ../hle_bigstack.c ../hle.h ../dm.h $(GAME_H) | $(B)+ $(CLANG) -O1 -g -fsanitize=thread -Wall -Wno-unused-function -I.. $(GINC) -o $@ t_fs_commit_list.c t_fs_stub.c ../hle_bigstack.c ../save_sync.c ../hle_profile.c.PHONY: fs-tsanfs-tsan: $(B)/t_fs_commit_list_tsanTMPDIR="$(abspath $(B))" $(B)/t_fs_commit_list_tsan-$(B)/t_fs_case: t_fs_case.c ../hle_fs.c ../hle_bigstack.c ../romfs_arc.c ../hle.h ../dm.h $(GAME_H) ../hle_profile.c ../hle_profile.h | $(B)- $(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_fs_case.c ../hle_bigstack.c ../romfs_arc.c ../hle_profile.c+$(B)/t_fs_case: t_fs_case.c ../hle_fs.c ../save_sync.c ../save_sync.h ../hle_bigstack.c ../romfs_arc.c ../hle.h ../dm.h $(GAME_H) ../hle_profile.c ../hle_profile.h | $(B)+ $(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_fs_case.c ../hle_bigstack.c ../save_sync.c ../romfs_arc.c ../hle_profile.c+run: $(B)/t_save_sync+$(B)/t_save_sync: t_save_sync.c t_fs_stub.c ../hle_fs.c ../save_sync.c ../save_sync.h ../hle_bigstack.c ../romfs_arc.c ../hle.h ../dm.h $(GAME_H) ../hle_profile.c ../hle_profile.h | $(B)+ $(CLANG) -O1 -Wall -Wno-unused-function -I.. $(GINC) -o $@ t_save_sync.c t_fs_stub.c ../hle_bigstack.c ../save_sync.c ../romfs_arc.c ../hle_profile.c+.PHONY: save-sync+save-sync: $(B)/t_save_sync+ TMPDIR="$(abspath $(B))" $(B)/t_save_sync $(QUIET)$(B)/t_audio: t_audio.c ../hle_audio.c ../hle.h ../dm.h $(GAME_H) | $(B)$(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_audio.c -framework AudioToolbox$(B)/t_audio_spsc: t_audio_spsc.c ../hle_audio.c ../hle.h ../dm.h $(GAME_H) | $(B)@@ -237,7 +244,7 @@ include ../../sanitizers/sanitizers.mkasan:@$(call san_fresh,$(B)/asan,$(ASAN_FLAGS))$(ASAN_ENV) $(MAKE) B=$(B)/asan SAN="$(ASAN_FLAGS)" run-TSAN_T = t_audio t_audio_device t_thread_leak t_oe t_fs_commit_list t_fs_case t_condvar t_condvar_old t_preempt t_system_event t_notify_sched t_altstack+TSAN_T = t_audio t_audio_device t_thread_leak t_oe t_fs_commit_list t_fs_case t_save_sync t_condvar t_condvar_old t_preempt t_system_event t_notify_sched t_altstacktsan-threads:@$(call san_fresh,$(B)/tsan,$(TSAN_FLAGS))$(TSAN_ENV) $(MAKE) B=$(B)/tsan SAN="$(TSAN_FLAGS)" tsan-threads-run@@ -248,6 +255,7 @@ tsan-threads-run: $(addprefix $(B)/,$(TSAN_T))$(B)/t_oeTMPDIR="$(abspath $(B))" $(B)/t_fs_commit_listTMPDIR="$(abspath $(B))" $(B)/t_fs_case+ TMPDIR="$(abspath $(B))" $(B)/t_save_sync$(B)/t_condvar$(B)/t_condvar_old$(B)/t_preemptdiff --git a/dm/host/tests/t_save_sync.c b/dm/host/tests/t_save_sync.cnew file mode 100644index 00000000..e31801f6--- /dev/null+++ b/dm/host/tests/t_save_sync.c@@ -0,0 +1,194 @@+// Cloud save sync unit test (takes seconds): dm/host/save_sync.c through hle_fs.c's own save mount and Commit, with a plain folder standing in for iCloud Drive.+// Each device's game session runs in a child process (the host's sync state is once per process, as it is once per start of the app); the parent checks what each left+// in its save, its backups and the cloud. All expected values are written by hand from the semantics in save_sync.h:+// (1) packing: a save with folders, a guest name and a device save comes back the same (contents, guest name, dates); the hash ignores dates and .DS_Store and sees contents;+// a file cut short, a flipped byte, a byte too many and a path out of the save are refused+// (2) ss_decide's table+// (3) Mac A has a save and the cloud none: A's session sends it; iPhone B has none: B's session brings it in; B saves: the cloud has B's+// (4) A starts again: only the cloud's changed: it comes in, and A's old save is kept in A's backups ("local")+// (5) both changed, the cloud's newer: B played without sync, A sent a newer save; B starts with sync: A's comes in, B's is kept+// (6) both changed, this one newer: A played without sync later than B sent; A starts with sync: A's goes out, B's is kept in A's backups ("cloud")+// (7) the Mac launcher's send after the host exited: a save the host committed and did not send goes out+#include "../hle_fs.c"+#include <ctype.h>+#include <sys/wait.h>+#include <sys/time.h>++struct dm_flags g_flag; uint64_t g_t0_ns;+uint64_t now_ns(void) { return clock_gettime_nsec_np(CLOCK_MONOTONIC); }+void dm_guest_write_prepare(void *a, size_t n) { (void)a; (void)n; }++static int fails, checks;+#define CHECK(c, ...) do { checks++; if (!(c)) { fails++; printf(" ✗ "); } else printf(" ✓ "); printf(__VA_ARGS__); printf("\n"); fflush(stdout); } while (0)+static void put(const char *p, const char *s) { FILE *f = fopen(p, "w"); fputs(s, f); fclose(f); }+static int same(const char *p, const char *s) {+ char b[64] = {0}; FILE *f = fopen(p, "r"); if (!f) return 0; size_t k = fread(b, 1, sizeof b - 1, f); fclose(f);+ return k == strlen(s) && !memcmp(b, s, k);+}+static void mkd(const char *p) { char c[1200]; snprintf(c, sizeof c, "%s", p); for (char *s = c + 1; *s; s++) if (*s == '/') { *s = 0; mkdir(c, 0755); *s = '/'; } mkdir(c, 0755); }+static void set_mtime(const char *p, time_t t) { struct timeval tv[2] = {{t, 0}, {t, 0}}; utimes(p, tv); }+static time_t mtime_of(const char *p) { struct stat st; return stat(p, &st) ? 0 : st.st_mtimespec.tv_sec; }+static int exists(const char *p) { return !access(p, F_OK); }++// The newest backup of a save whose name ends in " <side>", "" when there is none+static void backup_of(const char *save, const char *side, char *out, size_t n) {+ char dir[1200]; snprintf(dir, sizeof dir, "%s-backups", save); out[0] = 0;+ DIR *d = opendir(dir); struct dirent *e; char best[256] = "";+ size_t sl = strlen(side);+ while (d && (e = readdir(d))) { size_t l = strlen(e->d_name); if (l > sl && e->d_name[l - sl - 1] == ' ' && !strcmp(e->d_name + l - sl, side) && strcmp(e->d_name, best) > 0) strlcpy(best, e->d_name, sizeof best); }+ if (d) closedir(d);+ if (*best) snprintf(out, n, "%s/%s", dir, best);+}+// What the cloud holds of the game file, unpacked into a folder of its own+static int cloud_has(const char *cloud_file, const char *root, const char *file, const char *value) {+ static int k; char into[1200], p[1400]; ss_meta m;+ snprintf(into, sizeof into, "%s/peek%d", root, k++); mkd(into);+ if (ss_read(cloud_file, into, &m)) return 0;+ snprintf(p, sizeof p, "%s/save/%s", into, file); return same(p, value);+}++static Result write_save(const char *path, const char *s) { // create -> open -> set size -> write -> close, as a game does+ uint64_t h; h_fs_CreateFile(path, 0); Result r = h_fs_OpenFile(&h, path, MODE_WRITE); if (r) return r;+ h_fs_SetFileSize(h, (int64_t)strlen(s));+ r = h_fs_WriteFile(h, 0, s, strlen(s), NULL); h_fs_CloseFile(h); return r;+}++// One start of the game on a device: the save mount (the cloud's save comes in), then, with a file, the game writing it and committing, then a flush (the iPhone app+// going to the background). cloud NULL: sync off. Returns the child's failed steps+static int session(const char *save, const char *cloud, const char *file, const char *value) {+ fflush(stdout); fflush(stderr);+ pid_t p = fork();+ if (!p) {+ setenv("DM_SAVE", save, 1);+ if (cloud) setenv("DM_SAVE_SYNC", cloud, 1); else unsetenv("DM_SAVE_SYNC");+ int bad = h_fs_MountSave("save", NULL) != RES_OK;+ if (file) { char gp[256]; snprintf(gp, sizeof gp, "save:/%s", file); bad += write_save(gp, value) != RES_OK; bad += h_fs_Commit("save") != RES_OK; }+ if (cloud) bad += save_sync_flush(10000) != 1;+ fflush(stdout); fflush(stderr); _exit(bad);+ }+ int st; waitpid(p, &st, 0); return WIFEXITED(st) ? WEXITSTATUS(st) : 99;+}++int main(void) {+ char root[1024]; const char *sp = getenv("TMPDIR"); snprintf(root, sizeof root, "%s/t_save_sync.XXXXXX", sp ? sp : "/private/tmp");+ if (!mkdtemp(root)) { perror("mkdtemp"); return 1; }+ char p[1400], q[1400], u[1400], bundle[1400];+ ss_meta m, m2;++ printf("(1) packing\n");+ char P[1200]; snprintf(P, sizeof P, "%s/pack/save", root);+ snprintf(p, sizeof p, "%s/sub", P); mkd(p); snprintf(p, sizeof p, "%s/empty", P); mkd(p); snprintf(p, sizeof p, "%s_device", P); mkd(p);+ snprintf(p, sizeof p, "%s/a", P); put(p, "hello"); set_mtime(p, 1000000000);+ snprintf(p, sizeof p, "%s/sub/b", P); put(p, "xy"); setxattr(p, FS_GUEST_NAME, "B", 1, 0, XATTR_NOFOLLOW); set_mtime(p, 1000000100);+ snprintf(p, sizeof p, "%s_device/d", P); put(p, "dev"); set_mtime(p, 1000000050);+ CHECK(!ss_scan(P, &m) && m.files == 3 && strlen(m.hash) == SS_HASH && m.saved == 1000000100, "scan: 3 files, a 32-digit hash, saved = the newest file's date (%d files, saved %lld)", m.files, (long long)m.saved);+ snprintf(bundle, sizeof bundle, "%s/pack/save.dmsync", root);+ CHECK(!ss_pack(P, "0100abc", bundle, &m2) && !strcmp(m2.hash, m.hash) && !strcmp(m2.title, "0100ABC"), "pack: the same hash as the scan, the title ID in capitals (%s)", m2.title);+ CHECK(!ss_read(bundle, NULL, &m2) && !strcmp(m2.hash, m.hash) && m2.files == 3 && m2.saved == 1000000100 && !strcmp(m2.title, "0100ABC") && m2.device[0], "read: hash, files, date, title and device come back (from %s)", m2.device);+ snprintf(u, sizeof u, "%s/pack/out", root); mkd(u);+ CHECK(!ss_read(bundle, u, &m2), "unpack");+ snprintf(p, sizeof p, "%s/save/a", u); snprintf(q, sizeof q, "%s/save/sub/b", u);+ char g[8] = {0}; ssize_t gl = getxattr(q, FS_GUEST_NAME, g, sizeof g - 1, 0, XATTR_NOFOLLOW);+ CHECK(same(p, "hello") && same(q, "xy") && gl == 1 && g[0] == 'B', "files and the guest name (hle_fs.c's FS_GUEST_NAME) come back");+ CHECK(mtime_of(p) == 1000000000, "a file's date comes back (%lld)", (long long)mtime_of(p));+ snprintf(p, sizeof p, "%s/save/empty", u); snprintf(q, sizeof q, "%s/save_device/d", u);+ struct stat st; CHECK(!stat(p, &st) && S_ISDIR(st.st_mode) && same(q, "dev"), "an empty folder and the device save come back");+ snprintf(p, sizeof p, "%s/save", u); CHECK(!ss_scan(p, &m2) && !strcmp(m2.hash, m.hash), "the unpacked save has the original's hash");+ snprintf(p, sizeof p, "%s/a", P); set_mtime(p, 1200000000);+ CHECK(!ss_scan(P, &m2) && !strcmp(m2.hash, m.hash) && m2.saved == 1200000000, "another date, the same hash (the date still says which is newer)");+ snprintf(p, sizeof p, "%s/.DS_Store", P); put(p, "finder");+ CHECK(!ss_scan(P, &m2) && !strcmp(m2.hash, m.hash), "Finder's .DS_Store is not part of the save");+ snprintf(p, sizeof p, "%s/a", P); put(p, "hellO");+ CHECK(!ss_scan(P, &m2) && strcmp(m2.hash, m.hash), "one changed byte, another hash");+ snprintf(p, sizeof p, "%s/pack/none", root); mkd(p);+ CHECK(!ss_scan(p, &m2) && !m2.files && !m2.hash[0], "an empty folder is no save: no files, no hash");+ { FILE *f = fopen(bundle, "rb"); fseek(f, 0, SEEK_END); long n = ftell(f); rewind(f); char *b = malloc((size_t)n + 1); fread(b, 1, (size_t)n, f); fclose(f);+ snprintf(p, sizeof p, "%s/pack/bad.dmsync", root);+ f = fopen(p, "wb"); fwrite(b, 1, (size_t)n - 1, f); fclose(f); CHECK(ss_read(p, NULL, &m2) && !m2.files, "a file cut short by one byte is refused");+ f = fopen(p, "wb"); fwrite(b, 1, (size_t)n, f); fputc(0, f); fclose(f); CHECK(ss_read(p, NULL, &m2), "a byte too many is refused");+ b[n - 12] ^= 1; f = fopen(p, "wb"); fwrite(b, 1, (size_t)n, f); fclose(f); CHECK(ss_read(p, NULL, &m2), "a flipped byte in the contents is refused (the hash)");+ free(b);+ }+ { // one entry, save/../escaped, in an otherwise well-formed file+ uint8_t hdr[144] = {0}, h[24] = {2}; uint32_t one = 1, n = 1; uint16_t pl = 15; uint64_t sz = 1;+ memcpy(hdr, "DMSYNC1\n", 8); memcpy(hdr + 8, &one, 4); memcpy(hdr + 12, &n, 4); memcpy(h + 2, &pl, 2); memcpy(h + 8, &sz, 8);+ snprintf(p, sizeof p, "%s/pack/escape.dmsync", root); FILE *f = fopen(p, "wb");+ fwrite(hdr, 1, sizeof hdr, f); fwrite(h, 1, sizeof h, f); fwrite("save/../escaped", 1, 15, f); fputc('z', f); fwrite("DMSYNCE\n", 1, 8, f); fclose(f);+ snprintf(u, sizeof u, "%s/pack/esc/in", root); mkd(u); snprintf(q, sizeof q, "%s/pack/esc/in/escaped", root);+ char q2[1400]; snprintf(q2, sizeof q2, "%s/pack/esc/escaped", root);+ CHECK(ss_read(p, u, &m2) && !exists(q) && !exists(q2), "a path that leaves the save is refused, and nothing is written");+ }++ printf("(2) which way\n");+ int both;+ CHECK(ss_decide("", "", "", 0, 0, &both) == SS_KEEP && !both, "no save anywhere: keep");+ CHECK(ss_decide("L", "", "", 0, 0, &both) == SS_PUSH && !both, "nothing in the cloud: send");+ CHECK(ss_decide("", "", "R", 0, 0, &both) == SS_PULL && !both, "nothing here: bring in");+ CHECK(ss_decide("X", "B", "X", 0, 0, &both) == SS_KEEP && !both, "the same here and in the cloud: keep");+ CHECK(ss_decide("B", "B", "R", 9, 1, &both) == SS_PULL && !both, "only the cloud's changed: bring in, even with an older date");+ CHECK(ss_decide("L", "B", "B", 1, 9, &both) == SS_PUSH && !both, "only this one changed: send, even with an older date");+ CHECK(ss_decide("L", "B", "R", 10, 20, &both) == SS_PULL && both, "both changed, the cloud's newer: bring in");+ CHECK(ss_decide("L", "B", "R", 20, 10, &both) == SS_PUSH && both, "both changed, this one newer: send");+ CHECK(ss_decide("L", "", "R", 20, 10, &both) == SS_PUSH && both, "a first sync with a save of its own counts as both changed");+ CHECK(ss_decide("L", "B", "R", 10, 10, &both) == SS_PUSH && both, "the same date: this device's is kept");++ char A[1200], B[1200], cloud[1200], cf[1400], id[24], back[1400];+ snprintf(A, sizeof A, "%s/mac/save", root); snprintf(B, sizeof B, "%s/iphone/save", root); snprintf(cloud, sizeof cloud, "%s/iCloud Drive/NX2Apple Saves", root);+ mkd(A); mkd(B); mkd(cloud);+ for (size_t i = 0; i <= strlen(dm_profile()->id); i++) id[i] = (char)toupper((unsigned char)dm_profile()->id[i]);+ snprintf(cf, sizeof cf, "%s/%s/save.dmsync", cloud, id);++ printf("(3) Mac -> cloud -> iPhone\n");+ snprintf(p, sizeof p, "%s/game.dat", A); put(p, "v1");+ CHECK(session(A, cloud, NULL, NULL) == 0 && cloud_has(cf, root, "game.dat", "v1"), "the Mac's first session with sync sends its save");+ snprintf(p, sizeof p, "%s.sync", A); CHECK(exists(p), "and remembers it as in step (%s)", p);+ snprintf(p, sizeof p, "%s.sync-tmp", A); CHECK(!exists(p), "nothing is left beside the save");+ snprintf(p, sizeof p, "%s/game.dat", B);+ CHECK(session(B, cloud, NULL, NULL) == 0 && same(p, "v1"), "the iPhone's first session brings it in");+ snprintf(q, sizeof q, "%s-backups", B); CHECK(!exists(q), "with no save of its own, the iPhone keeps no backup");+ CHECK(session(B, cloud, "game.dat", "v2") == 0 && cloud_has(cf, root, "game.dat", "v2"), "the iPhone saves: the cloud has its save");++ printf("(4) the cloud's changed\n");+ snprintf(p, sizeof p, "%s/game.dat", A);+ CHECK(session(A, cloud, NULL, NULL) == 0 && same(p, "v2"), "the Mac starts again: the iPhone's save comes in");+ backup_of(A, "local", back, sizeof back); snprintf(q, sizeof q, "%s/save/game.dat", back);+ CHECK(*back && same(q, "v1"), "the Mac's old save is kept (%s)", back);++ printf("(5) both changed, the cloud's newer\n");+ snprintf(p, sizeof p, "%s/game.dat", B);+ CHECK(session(B, NULL, "game.dat", "v4") == 0 && same(p, "v4"), "the iPhone plays with sync off");+ set_mtime(p, time(NULL) - 100);+ CHECK(session(A, cloud, "game.dat", "v5") == 0 && cloud_has(cf, root, "game.dat", "v5"), "then the Mac saves and sends");+ CHECK(session(B, cloud, NULL, NULL) == 0 && same(p, "v5"), "the iPhone starts with sync: the newer Mac save comes in");+ backup_of(B, "local", back, sizeof back); snprintf(q, sizeof q, "%s/save/game.dat", back);+ CHECK(*back && same(q, "v4"), "the iPhone's own is kept (%s)", back);++ printf("(6) both changed, this one newer\n");+ snprintf(p, sizeof p, "%s/game.dat", A);+ CHECK(session(A, NULL, "game.dat", "v6") == 0, "the Mac plays with sync off");+ set_mtime(p, time(NULL) + 100);+ CHECK(session(B, cloud, "game.dat", "v7") == 0 && cloud_has(cf, root, "game.dat", "v7"), "the iPhone saves and sends");+ CHECK(session(A, cloud, NULL, NULL) == 0 && same(p, "v6") && cloud_has(cf, root, "game.dat", "v6"), "the Mac starts with sync: its newer save stays and goes out");+ backup_of(A, "cloud", back, sizeof back); snprintf(q, sizeof q, "%s/save/game.dat", back);+ CHECK(*back && same(q, "v7"), "the iPhone's save it replaced in the cloud is kept on the Mac (%s)", back);++ printf("(7) the Mac launcher after the host\n");+ snprintf(p, sizeof p, "%s/game.dat", A); put(p, "v8");+ setenv("DM_SAVE_SYNC", cloud, 1);+ CHECK(save_sync_after_exit(A, dm_profile()->id) == 1 && cloud_has(cf, root, "game.dat", "v8"), "a save the host left unsent goes out");+ time_t sent = mtime_of(cf);+ sleep(1);+ CHECK(save_sync_after_exit(A, dm_profile()->id) == 1 && mtime_of(cf) == sent, "a second time there is nothing to send (the cloud's file is not rewritten)");+ unsetenv("DM_SAVE_SYNC");+ CHECK(save_sync_after_exit(A, dm_profile()->id) == 1, "sync off: nothing to do");++ printf("(8) the game's own folder chosen\n");+ char C[1200], own[1300]; snprintf(C, sizeof C, "%s/mac2/save", root); mkd(C); snprintf(own, sizeof own, "%s/%s", cloud, id);+ snprintf(p, sizeof p, "%s/game.dat", C);+ CHECK(session(C, own, NULL, NULL) == 0 && same(p, "v8"), "<folder>/<title ID> itself as the folder: the save in it comes in");+ snprintf(q, sizeof q, "%s/%s", own, id); CHECK(!exists(q), "and no second <title ID> folder is made inside it");++ 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;+}diff --git a/dm/ios/Sources/DataBookmark.h b/dm/ios/Sources/DataBookmark.hindex 3aef3338..420c4214 100644--- a/dm/ios/Sources/DataBookmark.h+++ b/dm/ios/Sources/DataBookmark.h@@ -30,3 +30,21 @@ BOOL dm_store_picked_data(NSURL *url);/// Forget it (a player who wants the app to go back to its Documents copy).void dm_clear_picked_data(void);++// Cloud saves (dm/host/save_sync.h): the iCloud Drive folder the player picked in the launcher's+// Cloud saves section, remembered the same way. The host brings the save in from there when the+// game starts and sends it there after each save; the Mac app chooses the same folder in its settings window.++/// The picked folder, or nil; `stale` as for the game data.+NSString *dm_cloud_saves_path(BOOL *stale);++/// Store a bookmark for the folder the player just picked. Returns NO when the system refuses to make one.+BOOL dm_store_cloud_saves(NSURL *url);++/// Forget it: cloud saves off.+void dm_clear_cloud_saves(void);++/// Puts the picked folder into DM_SAVE_SYNC before the game starts (front_ios.m, at Play), its+/// security scope held for the session. A DM_SAVE_SYNC from an env file or the launch environment+/// wins; turning cloud saves off in the launcher clears the one this set.+void dm_cloud_saves_env(void);diff --git a/dm/ios/Sources/DataBookmark.m b/dm/ios/Sources/DataBookmark.mindex 3dcc3834..bd5a1ec9 100644--- a/dm/ios/Sources/DataBookmark.m+++ b/dm/ios/Sources/DataBookmark.m@@ -2,12 +2,13 @@#import "DataBookmark.h"-static NSString *const kPickedData = @"pickedDataBookmark";+static NSString *const kPickedData = @"pickedDataBookmark", *const kCloudSaves = @"cloudSavesBookmark";// The one security scope the game data lives under, held from the first resolve on: the host reads// the image for the whole session, and a scope released after each read would break the game half-// way through. Never stopped on purpose — the process exits when the game does.-static NSURL *openedPick = nil;+// way through. Never stopped on purpose — the process exits when the game does. The cloud-save+// folder's is held the same way: the host writes there after every save.+static NSURL *openedPick = nil, *openedCloud = nil;NSData *dm_picked_data_bookmark(void) {return [NSUserDefaults.standardUserDefaults dataForKey:kPickedData];@@ -23,9 +24,11 @@ static NSData *fresh_bookmark(NSURL *url) {return bookmark;}-NSString *dm_picked_data_path(BOOL *stale) {+// One remembered pick: the bookmark under `key` resolved to a path, its scope held in `opened`.+// `what` names it in the log ("game-data", "cloud-save folder").+static NSString *resolve(NSString *key, NSURL *__strong *opened, const char *what, BOOL *stale) {if (stale) *stale = NO;- NSData *bookmark = dm_picked_data_bookmark();+ NSData *bookmark = [NSUserDefaults.standardUserDefaults dataForKey:key];if (bookmark.length == 0) return nil;BOOL isStale = NO;NSError *error = nil;@@ -37,19 +40,19 @@ NSString *dm_picked_data_path(BOOL *stale) {if (!url) {// A re-signed build gets a bookmark the system will not resolve for it. Keep the data —// the player may install the same signature again — and let the caller ask for a new pick.- fprintf(stderr, "boot: the picked game-data bookmark does not resolve (%s)\n",+ fprintf(stderr, "boot: the picked %s bookmark does not resolve (%s)\n", what,error.localizedDescription.UTF8String ?: "no reason given");if (stale) *stale = YES;return nil;}- if (openedPick && ![openedPick.path isEqualToString:url.path]) openedPick = nil;- if (!openedPick) [url startAccessingSecurityScopedResource]; // held for the session (see openedPick)- openedPick = url;+ if (*opened && ![(*opened).path isEqualToString:url.path]) *opened = nil;+ if (!*opened) [url startAccessingSecurityScopedResource]; // held for the session (see openedPick)+ *opened = url;if (isStale) {// The system still resolves it but asks for a rewrite (its own wording: stale). Same// signature, so a fresh bookmark sticks: self-heal instead of making the player pick again.NSData *again = fresh_bookmark(url);- if (again) [NSUserDefaults.standardUserDefaults setObject:again forKey:kPickedData];+ if (again) [NSUserDefaults.standardUserDefaults setObject:again forKey:key];if (stale) *stale = YES;}// The picker can hand back a file that was moved or deleted since. The caller checks what it@@ -57,7 +60,9 @@ NSString *dm_picked_data_path(BOOL *stale) {return url.path;}-BOOL dm_store_picked_data(NSURL *url) {+NSString *dm_picked_data_path(BOOL *stale) { return resolve(kPickedData, &openedPick, "game-data", stale); }++static BOOL store(NSString *key, NSURL *url, const char *what) {NSData *bookmark = fresh_bookmark(url);NSError *error = nil;NSURL *back = nil;@@ -69,11 +74,33 @@ BOOL dm_store_picked_data(NSURL *url) {error.localizedDescription.UTF8String ?: (bookmark ? "the bookmark does not resolve again" : "no bookmark data"));return NO;}- [NSUserDefaults.standardUserDefaults setObject:bookmark forKey:kPickedData];- fprintf(stderr, "launcher: game data set to %s\n", url.path.UTF8String ?: "?");+ [NSUserDefaults.standardUserDefaults setObject:bookmark forKey:key];+ fprintf(stderr, "launcher: %s set to %s\n", what, url.path.UTF8String ?: "?");return YES;}+BOOL dm_store_picked_data(NSURL *url) { return store(kPickedData, url, "game data"); }+void dm_clear_picked_data(void) {[NSUserDefaults.standardUserDefaults removeObjectForKey:kPickedData];}++NSString *dm_cloud_saves_path(BOOL *stale) { return resolve(kCloudSaves, &openedCloud, "cloud-save folder", stale); }++BOOL dm_store_cloud_saves(NSURL *url) { return store(kCloudSaves, url, "cloud-save folder"); }++void dm_clear_cloud_saves(void) {+ [NSUserDefaults.standardUserDefaults removeObjectForKey:kCloudSaves];+}++void dm_cloud_saves_env(void) {+ static BOOL fromPick; // DM_SAVE_SYNC is the one this set, not an env file's+ if (getenv("DM_SAVE_SYNC") && !fromPick) return;+ BOOL stale = NO;+ NSString *folder = dm_cloud_saves_path(&stale);+ if (stale && !folder) fprintf(stderr, "boot: the cloud-save folder needs to be chosen again (the app is signed differently now); cloud saves are off\n");+ if (folder.length) {+ setenv("DM_SAVE_SYNC", folder.fileSystemRepresentation, 1); fromPick = YES;+ fprintf(stderr, "boot: cloud saves through %s\n", folder.fileSystemRepresentation);+ } else if (fromPick) { unsetenv("DM_SAVE_SYNC"); fromPick = NO; }+}diff --git a/dm/ios/Sources/Launcher.swift b/dm/ios/Sources/Launcher.swiftindex df41a708..53ad595c 100644--- a/dm/ios/Sources/Launcher.swift+++ b/dm/ios/Sources/Launcher.swift@@ -4,6 +4,7 @@// in UserDefaults; Reset to tested defaults shows the defaults, and Play then forgets them. DM_LAUNCHER=0 in Documents/env.txt starts the game at once with the saved choices; DM_SAVED_CHOICES=0 ignores them (and keeps them)import SwiftUIimport UIKit+import UniformTypeIdentifiersimport osprivate func info(_ key: String) -> Any? { Bundle.main.object(forInfoDictionaryKey: key) }@@ -64,15 +65,66 @@ private struct GameDataSection: View {}}+/// Cloud saves: an iCloud Drive folder shared with the Mac app (chosen in its settings window's+/// Cloud saves too). The host brings the save in from there when the game starts and+/// sends it there after each save (dm/host/save_sync.h); the folder is a bookmark (DataBookmark.m).+private struct CloudSavesSection: View {+ @State private var folder: String = ""+ @State private var note: String = ""+ @State private var picking = false++ var body: some View {+ Section {+ HStack {+ Text("Cloud saves")+ Spacer()+ Text(folder.isEmpty ? "Off" : (folder as NSString).lastPathComponent)+ .foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle)+ }+ Button(folder.isEmpty ? "Choose a folder in iCloud Drive…" : "Choose a different folder…") { picking = true }+ if !folder.isEmpty {+ Button("Turn off cloud saves", role: .destructive) {+ dm_clear_cloud_saves(); refresh(); note = ""+ }+ }+ } footer: {+ Text(note.isEmpty+ ? "Choose a folder in iCloud Drive, and the same one in the Mac app's Cloud saves. The save comes in when the game starts and goes out after each save; a save that is replaced is kept in save-backups."+ : note)+ }+ .sheet(isPresented: $picking) {+ DocumentPicker(types: [.folder]) { url in+ picking = false+ if dm_store_cloud_saves(url) {+ note = "Saved. The game uses this folder when you press Play."+ refresh()+ } else {+ note = "That folder could not be remembered — try a different one."+ }+ } onCancel: { picking = false }+ }+ .onAppear(perform: refresh)+ }++ private func refresh() {+ var stale: ObjCBool = false+ folder = dm_cloud_saves_path(&stale) ?? ""+ if stale.boolValue && folder.isEmpty {+ note = "The folder you chose before is no longer readable (the app was signed differently). Please choose it again."+ }+ }+}+/// UIDocumentPickerViewController for files and folders, in the two callbacks a SwiftUI sheet wants.private struct DocumentPicker: UIViewControllerRepresentable {+ var types: [UTType] = [.item]let onPick: (URL) -> Voidlet onCancel: () -> Voidfunc makeUIViewController(context: Context) -> UIDocumentPickerViewController {// .open (not .import): the file stays where it is and the bookmark keeps pointing at it,// which is the whole point — an import would copy 16 GB into the app's container.- let picker = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: false)+ let picker = UIDocumentPickerViewController(forOpeningContentTypes: types, asCopy: false)picker.allowsMultipleSelection = falsepicker.delegate = context.coordinatorreturn picker@@ -133,6 +185,7 @@ private struct LauncherView: View {Text("Brings back all settings this game was tested with.")}GameDataSection()+ CloudSavesSection()Section {HStack {Text("Memory available")diff --git a/dm/ios/Sources/SaveSync.m b/dm/ios/Sources/SaveSync.mnew file mode 100644index 00000000..2d9d1e52--- /dev/null+++ b/dm/ios/Sources/SaveSync.m@@ -0,0 +1,59 @@+// Cloud saves on the iPhone: the copies to and from the iCloud Drive folder the player picked (dm/host/save_sync.h; the folder is a bookmark, DataBookmark.m).+// They go through NSFileCoordinator: a file iCloud has only listed is downloaded before the read, and neither copy crosses iCloud's own writes of the file.+// These replace the host's plain-copy defaults (weak, in save_sync.c) at link time; the host packs, unpacks and decides, this moves the one file.+#import <Foundation/Foundation.h>+#include <errno.h>+#include <stdio.h>+#include <string.h>+#include <unistd.h>+#include "save_sync.h"++int front_save_sync_fetch(const char *cloud, const char *local) {+ @autoreleasepool {+ NSFileManager *fm = [NSFileManager new];+ NSURL *src = [NSURL fileURLWithPath:@(cloud)], *dst = [NSURL fileURLWithPath:@(local)];+ // Not there, not even as the placeholder iCloud lists for a file it has not downloaded (.<name>.icloud): no save in the cloud yet+ NSString *placeholder = [src.URLByDeletingLastPathComponent URLByAppendingPathComponent:[NSString stringWithFormat:@".%@.icloud", src.lastPathComponent]].path;+ if (![fm fileExistsAtPath:src.path] && ![fm fileExistsAtPath:placeholder]) return 0;+ [fm startDownloadingUbiquitousItemAtURL:src error:nil]; // asked for now; the coordinated read waits for it (save_sync.c gives it a time limit)+ __block int r = -1;+ NSError *error = nil;+ [[[NSFileCoordinator alloc] initWithFilePresenter:nil] coordinateReadingItemAtURL:src options:0 error:&error byAccessor:^(NSURL *u) {+ NSError *e = nil;+ [fm removeItemAtURL:dst error:nil];+ if ([fm copyItemAtURL:u toURL:dst error:&e]) r = 1;+ else if ([e.domain isEqualToString:NSCocoaErrorDomain] && e.code == NSFileReadNoSuchFileError) r = 0;+ else fprintf(stderr, "[dm] [RED] save sync: copying %s from the cloud failed: %s\n", cloud, e.localizedDescription.UTF8String ?: "no reason given");+ }];+ if (error) fprintf(stderr, "[dm] [RED] save sync: reading %s from the cloud failed: %s\n", cloud, error.localizedDescription.UTF8String ?: "no reason given");+ return r;+ }+}++int front_save_sync_store(const char *local, const char *cloud) {+ @autoreleasepool {+ NSFileManager *fm = [NSFileManager new];+ NSURL *dst = [NSURL fileURLWithPath:@(cloud)];+ NSString *src = @(local);+ __block int r = 0;+ NSError *error = nil;+ [[[NSFileCoordinator alloc] initWithFilePresenter:nil] coordinateWritingItemAtURL:dst options:NSFileCoordinatorWritingForReplacing error:&error byAccessor:^(NSURL *u) {+ // Beside it under a .nosync name (iCloud Drive does not upload those), then renamed over it: iCloud sees one whole new file+ NSString *part = [u.path stringByAppendingFormat:@".%d.nosync", (int)getpid()];+ NSError *e = nil;+ [fm removeItemAtPath:part error:nil];+ if (![fm copyItemAtPath:src toPath:part error:&e]) {+ fprintf(stderr, "[dm] [RED] save sync: writing beside %s failed: %s\n", cloud, e.localizedDescription.UTF8String ?: "no reason given");+ return;+ }+ if (rename(part.fileSystemRepresentation, u.fileSystemRepresentation)) {+ fprintf(stderr, "[dm] [RED] save sync: replacing %s failed (errno=%d)\n", cloud, errno);+ [fm removeItemAtPath:part error:nil];+ return;+ }+ r = 1;+ }];+ if (error) fprintf(stderr, "[dm] [RED] save sync: writing %s to the cloud failed: %s\n", cloud, error.localizedDescription.UTF8String ?: "no reason given");+ return r;+ }+}diff --git a/dm/ios/Sources/front_ios.m b/dm/ios/Sources/front_ios.mindex 56a432f1..fc79813e 100644--- a/dm/ios/Sources/front_ios.m+++ b/dm/ios/Sources/front_ios.m@@ -9,6 +9,8 @@#include "hle.h"#include "dm.h"#include "haptics_apple.h"+#include "save_sync.h"+#import "DataBookmark.h"UIView *dm_gamepad_view(CGRect frame);void dm_gamepad_reset(void);@@ -79,6 +81,7 @@ static void launcher_done(void) { [g_root startGame]; }lb.text = @"Game files not found.\nPut the RomFS folder into this App's Documents as \"romfs\" (Files app), then restart.";[self.view addSubview:lb]; return;}+ dm_cloud_saves_env(); // DM_SAVE_SYNC from the launcher's Cloud saves pick, before the game's first save mount reads it (DataBookmark.m, save_sync.h)front_hud_env(); // before the game view makes its device (an env file's DM_METAL_HUD sets it in main.m already)self.game = [[DmGameView alloc] initWithFrame:self.view.bounds];[self.view addSubview:self.game];@@ -271,11 +274,23 @@ static void front_ios_audio_watch(void) {// Scene lifecycle (called by SceneDelegate in main.m): 0 resign active, 1 enter background, 2 return to foreground, 3 become active. On entering background, freeze the guest clock and suspend after in-flight GPU work completes (submitting GPU work in background gets the process killed).// The screen stays on while the game is active: only touches reset the idle timer, so with a controller (or during a cutscene) the screen dimmed and locked mid-game+// Cloud saves (save_sync.h): what the game committed goes out before iOS suspends the app, without the sender's quiet wait. iOS gives a background task about half a minute+static void save_sync_background(void) {+ UIApplication *app = UIApplication.sharedApplication;+ __block UIBackgroundTaskIdentifier task = UIBackgroundTaskInvalid;+ void (^end)(void) = ^{ if (task != UIBackgroundTaskInvalid) { [app endBackgroundTask:task]; task = UIBackgroundTaskInvalid; } }; // on the main queue, as the expiry handler runs+ task = [app beginBackgroundTaskWithName:@"cloud save" expirationHandler:end];+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{+ if (!save_sync_flush(20000)) LOG("front end: the save did not reach the cloud before the app was suspended; it goes after the next save or at the next start");+ dispatch_async(dispatch_get_main_queue(), end);+ });+}+void front_ios_focus_event(int ev) {if (!g_layer) return;switch (ev) {case 0: dm_gamepad_reset(); dm_focus_set(2); break;- case 1: UIApplication.sharedApplication.idleTimerDisabled = NO; dm_focus_set(3); g_audio_bg = 1; front_ios_audio_apply(); guest_clock_freeze_for(1, 1); dm_gfx_suspend(1); break;+ case 1: UIApplication.sharedApplication.idleTimerDisabled = NO; dm_focus_set(3); g_audio_bg = 1; front_ios_audio_apply(); guest_clock_freeze_for(1, 1); dm_gfx_suspend(1); save_sync_background(); break;case 2: dm_gfx_suspend(0); guest_clock_freeze_for(1, 0); front_ios_audio_session(); g_audio_bg = 0; front_ios_audio_apply(); dm_focus_set(2); break;case 3: // Becoming active again also ends an interruption whose "ended" never came (not every one sends it), and retries a session that would not activate on the way backUIApplication.sharedApplication.idleTimerDisabled = YES;diff --git a/dm/mac/Launcher.swift b/dm/mac/Launcher.swiftindex 3b01416a..07247a04 100644--- a/dm/mac/Launcher.swift+++ b/dm/mac/Launcher.swift@@ -29,6 +29,7 @@ struct LauncherView: View {.padding([.horizontal, .top], 20)Form {settingSections(basic: true)+ CloudSavesSection()Section {DisclosureGroup("Advanced settings", isExpanded: $advanced) {}}@@ -71,6 +72,51 @@ struct LauncherView: View {}}+/// Cloud saves: a folder the save is synced through (dm/host/save_sync.h), the one the iPhone app picks in its launcher's Cloud saves section. It is kept in this app's+/// preferences as cloudSaves.folder, which launcher.c puts into DM_SAVE_SYNC before the host starts; a DM_SAVE_SYNC an env file or the launch environment set wins, and is shown here.+struct CloudSavesSection: View {+ @AppStorage("cloudSaves.folder") private var folder = ""+ private let fixed: String? = {+ let env = ProcessInfo.processInfo.environment+ let keys = ((env["DM_ENV_FILE_KEYS"] ?? "") + " " + (env["DM_ENV_LAUNCH_KEYS"] ?? "")).split(separator: " ")+ return keys.contains("DM_SAVE_SYNC") || env["DM_SAVE_SYNC"] != nil ? env["DM_SAVE_SYNC"] : nil+ }()++ var body: some View {+ Section {+ LabeledContent("Cloud saves") {+ Text(fixed.map { "\(Self.shown($0)) (env.txt)" } ?? (folder.isEmpty ? "Off" : Self.shown(folder)))+ .foregroundStyle(.secondary).lineLimit(1).truncationMode(.head)+ }+ if fixed == nil {+ HStack {+ Button(folder.isEmpty ? "Choose a folder…" : "Choose a different folder…", action: choose)+ if !folder.isEmpty { Button("Turn off") { folder = "" } }+ }+ }+ } footer: {+ Text("Choose the iCloud Drive folder the iPhone app uses for this game. The save comes in when the game starts and goes out after each save; a save that is replaced is kept in the save's -backups folder.")+ }+ }++ private func choose() {+ let panel = NSOpenPanel()+ panel.canChooseDirectories = true; panel.canChooseFiles = false; panel.allowsMultipleSelection = false; panel.canCreateDirectories = true+ panel.prompt = "Use for Cloud Saves"+ panel.message = "The folder the iPhone app chose in its launcher's Cloud saves"+ panel.directoryURL = URL(fileURLWithPath: folder.isEmpty ? Self.drive : folder)+ if panel.runModal() == .OK, let url = panel.url { folder = url.path }+ }++ static let drive = NSHomeDirectory() + "/Library/Mobile Documents/com~apple~CloudDocs" // iCloud Drive, as the Finder shows it++ /// A path under iCloud Drive as the Finder and the Files app name it: iCloud Drive › Documents › dev › Zelda; any other path as it is+ static func shown(_ path: String) -> String {+ guard path == drive || path.hasPrefix(drive + "/") else { return path }+ return (["iCloud Drive"] + path.dropFirst(drive.count).split(separator: "/").map(String.init)).joined(separator: " › ")+ }+}+private final class Launcher: NSObject, NSApplicationDelegate, NSWindowDelegate {let model: LauncherModelvar window: NSWindow?diff --git a/dm/mac/launcher.c b/dm/mac/launcher.cindex 2cf5f549..4124e726 100644--- a/dm/mac/launcher.c+++ b/dm/mac/launcher.c@@ -26,6 +26,7 @@#include <sys/wait.h>#include <unistd.h>#include "romfs_image.h"+#include "save_sync.h"extern char **environ;int dm_mac_launcher(void); // Launcher.swift: 1 = Play (the settings are in the environment), 0 = the player quit, 2 = no window (apply the saved choices here)@@ -171,6 +172,18 @@ static void saved_choices(void) { // after the env files, before the bundle's}}+// Cloud saves: the folder chosen in the settings window (Launcher.swift CloudSavesSection, cloudSaves.folder in the app's preferences) becomes DM_SAVE_SYNC+// (dm/host/save_sync.h), with or without the window; a DM_SAVE_SYNC the launch environment or an env file set wins+static void cloud_saves_folder(void) {+ CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());+ if (getenv("DM_SAVE_SYNC") || !domain) return;+ CFPropertyListRef v = CFPreferencesCopyAppValue(CFSTR("cloudSaves.folder"), domain); char s[PATH_MAX];+ if (v && CFGetTypeID(v) == CFStringGetTypeID() && CFStringGetCString(v, s, sizeof s, kCFStringEncodingUTF8) && *s) {+ setenv("DM_SAVE_SYNC", s, 1); fprintf(stderr, "launcher: cloud saves through %s\n", s);+ }+ if (v) CFRelease(v);+}+int main(int argc, char **argv) {char self[PATH_MAX], exe[PATH_MAX]; uint32_t n = sizeof self;if (_NSGetExecutablePath(self, &n) || !realpath(self, exe)) return 1;@@ -266,6 +279,7 @@ int main(int argc, char **argv) {int shown = window ? dm_mac_launcher() : 2;if (!shown) return 0; // Quit: no gameif (shown == 2) saved_choices();+ cloud_saves_folder();setenv("DM_ROOT", sup, 0); setenv("DM_ROMFS", romfs, 0); setenv("DM_SAVE", save, 0); setenv("DM_TEMP", temp, 0);snprintf(p, sizeof p, "%s/msl", res); setenv("DM_MSL_DIR", p, 0);snprintf(p, sizeof p, "%s/font", dm); setenv("DM_FONT_DIR", p, 0);@@ -283,6 +297,8 @@ int main(int argc, char **argv) {if (err) { fprintf(stderr, "[RED] launcher: cannot start %s: %s\n", host, strerror(err)); return 1; }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");if (WIFSIGNALED(status)) { fprintf(stderr, "launcher: the host stopped on signal %d\n", WTERMSIG(status)); return 128 + WTERMSIG(status); }fprintf(stderr, "launcher: the host exited with %d\n", WEXITSTATUS(status));return WEXITSTATUS(status);diff --git a/dm/mac/pack_app.py b/dm/mac/pack_app.pyindex 6f28830c..74048300 100755--- a/dm/mac/pack_app.py+++ b/dm/mac/pack_app.py@@ -84,12 +84,13 @@ def swift_sources(): # the settings window, with the iOS launcher's model andreturn [HERE / 'Launcher.swift', HERE.parent / 'ios/Sources/LauncherSettings.swift', HERE.parent / 'ios/Sources/LauncherRow.swift']-def build_launcher(out, minos, clang, swiftc, sdk): # launcher.c's main, linked by swiftc with the settings window (no Swift main: -parse-as-library)+def build_launcher(out, minos, clang, swiftc, sdk): # launcher.c's main and the host's cloud-save code (its send after the host exits), linked by swiftc with the settings window (no Swift main: -parse-as-library)with tempfile.TemporaryDirectory(dir=out.parents[3]) as t:- obj = Path(t) / 'launcher.o'- run([clang, '-isysroot', sdk, '-arch', 'arm64', f'-mmacosx-version-min={minos}', '-O2', '-Wall', '-I', HERE.parent / 'host', '-c', '-o', obj, HERE / 'launcher.c'])+ objs = [Path(t) / 'launcher.o', Path(t) / 'save_sync.o']+ for obj, src in zip(objs, (HERE / 'launcher.c', HERE.parent / 'host/save_sync.c')):+ run([clang, '-isysroot', sdk, '-arch', 'arm64', f'-mmacosx-version-min={minos}', '-O2', '-Wall', '-I', HERE.parent / 'host', '-c', '-o', obj, src])run([swiftc, '-sdk', sdk, '-target', f'arm64-apple-macos{minos}', '-O', '-swift-version', '5', '-parse-as-library', '-module-cache-path', Path(t) / 'module-cache',- '-o', out, *swift_sources(), obj, '-framework', 'CoreFoundation', '-framework', 'AppKit', '-framework', 'SwiftUI'])+ '-o', out, *swift_sources(), *objs, '-framework', 'CoreFoundation', '-framework', 'AppKit', '-framework', 'SwiftUI'])def make_icns(jpg, out): # the game's icon (256x256 JPEG) at every size an .icns holds@@ -219,7 +220,7 @@ def pack(a):if not codesign: raise SystemExit('codesign is not available')codesign = Path(codesign).resolve()minos = min_os(a.host)- sources = [Path(__file__).resolve(), HERE / 'launcher.c', HERE.parent / 'host/romfs_image.h', HERE.parent / 'tools/romfs.py'] + swift_sources()+ sources = [Path(__file__).resolve(), HERE / 'launcher.c', HERE.parent / 'host/romfs_image.h', HERE.parent / 'host/save_sync.c', HERE.parent / 'host/save_sync.h', HERE.parent / 'tools/romfs.py'] + swift_sources()inputs = {'options': vars(a), 'files': [(str(p), p.stat().st_mode, digest(p), os.readlink(p) if p.is_symlink() else None) for p in files + sources],'trees': [(p, inventory(Path(p))) for p in (a.romfs, a.aoc) if p],'tools': [[str(c), c.stat().st_mode, c.stat().st_size, c.stat().st_mtime_ns, c.stat().st_ctime_ns, run([c, '--version']).strip()] for c in (clang, swiftc)] + [diff --git a/dm/tools/tests/test_nsp_mac_app.py b/dm/tools/tests/test_nsp_mac_app.pyindex eaffe27b..9dee8fc0 100644--- a/dm/tools/tests/test_nsp_mac_app.py+++ b/dm/tools/tests/test_nsp_mac_app.py@@ -117,7 +117,7 @@ class PackReuse(unittest.TestCase):'font/nintendo_ext.ttf': b'font', 'aoc/1/data': b'dlc', 'pso/gpu.mtl4a': b'archive','pso/prebuilt.tsv': b'prebuilt', 'pso/rest_pairs.tsv': b'rest', 'pso/archive.tsv': b'index','env.txt': b'DM_TEST=1', 'split.tsv': b'split', 'icon.jpg': b'jpeg', 'source/launcher.c': b'launcher',- 'host/romfs_image.h': b'validator', 'tools/romfs.py': b'reader',+ 'host/romfs_image.h': b'validator', 'host/save_sync.c': b'sync', 'host/save_sync.h': b'sync api', 'tools/romfs.py': b'reader','source/Launcher.swift': b'window', 'ios/Sources/LauncherSettings.swift': b'model', 'ios/Sources/LauncherRow.swift': b'rows',}for n in pack_app.TABLES: files['profile/' + n] = n.encode()@@ -197,7 +197,8 @@ class PackReuse(unittest.TestCase):names = ['dmhost', 'patched/main.patched.bin', 'patched/main.fixups.tsv', 'patched/main.gates.tsv', 'patched/main.cuts.tsv','patched/main.obs.tsv', 'patched/nro.tsv', 'msl/k.metallib', 'msl/k.json', 'msl/inputs.tsv', 'font/nintendo_ext.ttf','pso/gpu.mtl4a', 'pso/prebuilt.tsv', 'pso/rest_pairs.tsv', 'pso/archive.tsv', 'env.txt', 'icon.jpg', 'source/launcher.c',- 'source/Launcher.swift', 'ios/Sources/LauncherSettings.swift', 'ios/Sources/LauncherRow.swift'] # the settings window's sources+ 'source/Launcher.swift', 'ios/Sources/LauncherSettings.swift', 'ios/Sources/LauncherRow.swift', # the settings window's sources+ 'host/save_sync.c', 'host/save_sync.h'] # the cloud-save code the launcher links (its send after the host exits)names += ['profile/' + n for n in pack_app.TABLES]for name in names:with self.subTest(name=name):@@ -255,6 +256,8 @@ class PackReuse(unittest.TestCase):self.assertIn('-c', c); self.assertIn('-parse-as-library', s)self.assertEqual(s[s.index('-o') + 1], str(Path(self.args.out).resolve().with_name('Game.app.partial') / 'Contents/MacOS/nxnative'))self.assertIn(c[c.index('-o') + 1], s) # launcher.c's object, with its main+ sync = next(c for c in self.commands if Path(c[0]).name == 'clang' and '-o' in c and c[-1].endswith('save_sync.c'))+ self.assertIn(sync[sync.index('-o') + 1], s) # save_sync.c's object: the launcher sends the save once the host has exitedself.assertEqual([Path(x).name for x in s if x.endswith('.swift')], ['Launcher.swift', 'LauncherSettings.swift', 'LauncherRow.swift'])def test_selected_file_modes_and_file_additions_and_removals_invalidate(self):diff --git a/docs/env_switches.tsv b/docs/env_switches.tsvindex e8b7ca63..a03fc1e9 100644--- a/docs/env_switches.tsv+++ b/docs/env_switches.tsv@@ -254,6 +254,7 @@ DM_RUMBLE_LOG diagnostic `=1` logs the game's vibration handle requests and, oncDM_RUMBLE_MIN player `=0..0.9`: controller rumble levels below it stay silent and the rest is stretched over the full range (default 0; the phone is unaffected); for games whose quiet HD rumble texture keeps rotating motors running (Mario Kart 8 Deluxe: 0.45); launcher settingDM_SAVE path This run's save folder (must be set); the device save is `<DM_SAVE>_device`, and `pso_user.tsv` goes in its parent folder; the Mac launcher, the iOS app and the NSP script set itDM_SAVED_CHOICES switch `0` ignores the player's saved choices (launcher.<VAR> in the app's preferences) and leaves them saved; the Mac launcher and the iOS settings screen+DM_SAVE_SYNC player Cloud saves: the folder whose `<title ID>/save.dmsync` the save is synced through, `icloud` for iCloud Drive/NX2Apple Saves (the Mac); `0` or unset, off. The Mac launcher sets the folder chosen in its settings window's Cloud saves, the iPhone app the one picked in its launcher (dm/host/save_sync.h)DM_SCHEME_NAME switch Set by `build_ipa.sh` itself for xcodegen's `project.yml`: the Xcode scheme name, `DmIOS`, or `<display name> (<title ID>)` for a project prepared with `--prepare-xcode`DM_SCREEN_MATCH switch `0` keeps the screen's refresh rate in fullscreen; by default a Mac screen whose rate is not a multiple of the game's frame rate switches to one that is (144 → 120 Hz)DM_SF_TRACE diagnostic `=1` logs every request on the game's own HIPC client sessions that the host answers (`hle_sf.c`)diff --git a/docs/settings.md b/docs/settings.mdindex de8d9803..c371b2d4 100644--- a/docs/settings.md+++ b/docs/settings.md@@ -192,6 +192,20 @@ Turn off **Show this window at launch** to start the game at once with your choi**Window** or **Fullscreen**: how the game's window opens. ⌃⌘F or the green button changes it while you play. In an `env.txt`: `DM_FULLSCREEN=1` or `0`.+### Cloud saves folder++**Choose a folder…** picks the iCloud Drive folder the game's save is synced through, the one the iPhone app chose in its launcher's **Cloud saves**, or the game's own folder inside it, named after its title ID ([Cloud saves](#cloud-saves)); **Turn off** stops it. Off by default. macOS may ask once whether the app may open iCloud Drive; if the game log says `this app may not open iCloud Drive`, allow it in System Settings › Privacy & Security › Files and Folders. In an `env.txt`: `DM_SAVE_SYNC=<folder>`, or `DM_SAVE_SYNC=icloud` for iCloud Drive › NX2Apple Saves; the window then shows it fixed.++## Cloud saves++The Mac app and the iPhone app of one game can share a save through iCloud Drive. Choose the same iCloud Drive folder on both: **Cloud saves** › **Choose a folder…** in the Mac settings window, and **Cloud saves** in the iPhone launcher (any folder, for instance one named **NX2Apple Saves**). Each game keeps one file there, `<title ID>/save.dmsync`, which holds the whole save.++When the game starts, it brings the save from iCloud Drive in if that one is the one to keep; a few seconds after the game saves, it sends its save. The iPhone app also sends when it goes to the background, and the Mac app once the game has quit. If you played on both without the save getting across in between, the newer save wins.++A save that is replaced, on the device or in iCloud Drive, is kept first, beside the save in a folder named after it with `-backups` (`save-backups` in the Files app; on the Mac, the front end's Saves sheet opens it): `<date> local` holds this device's save, `<date> cloud` the one that was in iCloud Drive.++iCloud Drive takes a moment to carry a file from one device to the other. Quit the game on one device and give it a few seconds before you start it on the other. Without a connection the game plays on with the save it has and sends it later. The game log says what came in and what went out (lines starting with `save sync:`).+## Settings in a fileA line like `DM_PRESENT_SNAP=0` in `Documents/env.txt` (in the Files app, under the app's name) sets a setting from the file: the settings screen shows it greyed out, and the file wins over your choice and the game's default. Fully close and reopen the app after editing the file. The variables:@@ -203,6 +217,7 @@ A line like `DM_PRESENT_SNAP=0` in `Documents/env.txt` (in the Files app, under| Console mode | `DM_OPMODE` | `1` docked, `0` handheld || Game language | `DM_LANG` | a language code, such as `ja` || Display mode (Mac) | `DM_FULLSCREEN` | `1` fullscreen, `0` a window |+| Cloud saves folder (Mac) | `DM_SAVE_SYNC` | a folder; `icloud`: iCloud Drive › NX2Apple Saves; `0`: off || Frame-rate counter | `DM_SHOW_FPS` | `1`, `0` || Touch feedback | `DM_TOUCH_HAPTICS` | `1`, `0` || Game rumble | `DM_RUMBLE` | `1`, `0` |