Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

7.0 KB
Raw Blame
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: v0lumehi <[email protected]>
Date: Wed, 28 Oct 2026 13:33:54 +0100
Subject: [PATCH] Mac launcher: saves and log live in the game library folder

One-time copy of the old saves and log from ~/Library; prints both paths at start.

Upstream-Status: pending (seedds/nxnative#460 is the upstream design: a dataFolder key)
Reason: Saves and log follow the game library folder (<folder of the .app>/.nx2apple-library marker); replaced by the dataFolder pref once #460 merges
Issue: LNPANDY/NX2Apple#123
---
 dm/mac/launcher.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/dm/mac/launcher.c b/dm/mac/launcher.c
index 1268b18..42e8ba7 100644
--- a/dm/mac/launcher.c
+++ b/dm/mac/launcher.c
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <dirent.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include "romfs_image.h"
@@ -73,6 +74,43 @@ static void mkdirs(const char *path) {   // mkdir -p
     mkdir(p, 0755);
 }
 
+static int dir_has_entries(const char *path) {   // anything inside at all (a save folder, a log folder)
+    DIR *d = opendir(path); if (!d) return 0;
+    struct dirent *e; int any = 0;
+    while ((e = readdir(d))) if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { any = 1; break; }
+    closedir(d); return any;
+}
+
+static int copy_file(const char *from, const char *to) {
+    FILE *in = fopen(from, "rb"); if (!in) return 0;
+    FILE *out = fopen(to, "wb"); if (!out) { fclose(in); return 0; }
+    char buf[1 << 16]; size_t n; int ok = 1;
+    while ((n = fread(buf, 1, sizeof buf, in)) > 0) if (fwrite(buf, 1, n, out) != n) { ok = 0; break; }
+    if (ferror(in)) ok = 0;
+    fclose(in); if (fclose(out)) ok = 0;
+    if (!ok) unlink(to);
+    return ok;
+}
+
+/// Recursive copy for the one-time migration of a save folder. Symlinks are followed (a save is
+/// plain files); a file that already exists is left alone, so a half-finished copy can be resumed.
+static void copy_tree(const char *from, const char *to) {
+    mkdirs(to);
+    DIR *d = opendir(from); if (!d) return;
+    struct dirent *e;
+    while ((e = readdir(d))) {
+        if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;
+        char a[PATH_MAX], b[PATH_MAX];
+        snprintf(a, sizeof a, "%s/%s", from, e->d_name);
+        snprintf(b, sizeof b, "%s/%s", to, e->d_name);
+        struct stat st;
+        if (stat(a, &st)) continue;
+        if (S_ISDIR(st.st_mode)) copy_tree(a, b);
+        else if (S_ISREG(st.st_mode) && access(b, F_OK)) copy_file(a, b);
+    }
+    closedir(d);
+}
+
 static void env_file(const char *path) {   // KEY=VALUE lines, # starts a comment; a variable already set stays
     FILE *f = fopen(path, "r"); if (!f) return;
     char ln[4096];
@@ -186,8 +224,57 @@ int main(int argc, char **argv) {
     snprintf(sup, sizeof sup, "%s/Library/Application Support/nxnative/%s", home, id);
     snprintf(save, sizeof save, "%s/save", sup); snprintf(temp, sizeof temp, "%s/temp", sup);
     snprintf(logs, sizeof logs, "%s/Library/Logs/nxnative", home);
+    // Saves and logs beside the game, not under ~/Library: a library folder the user can move or
+    // keep on an external drive has to carry its saves with it, or they are effectively lost the
+    // moment the folder moves (the files are still in ~/Library, where nobody looks for them).
+    //
+    // The library root is the folder the app sits in, when that folder also holds games/ — the
+    // layout the Mac front end writes (<out>/<id>.app, <out>/games/<id>/romfs.img). A bundle
+    // copied somewhere else keeps the old ~/Library paths, so nothing breaks for a user who never
+    // used the launcher's folder.
+    {
+        char appdir[PATH_MAX]; snprintf(appdir, sizeof appdir, "%s", exe);
+        // Four cuts: <…>/<id>.app/Contents/MacOS/<launcher> → MacOS → Contents → <id>.app → the folder
+        // holding it. Three landed on the .app itself, so the probe looked for <id>.app/games — which
+        // never exists — and the migration never ran (the report: saves stayed in ~/Library).
+        *strrchr(appdir, '/') = 0; *strrchr(appdir, '/') = 0;
+        *strrchr(appdir, '/') = 0; *strrchr(appdir, '/') = 0;
+        // The marker file the Mac front end writes beside the .app (a dot file, so the Finder hides
+        // it): this folder is a game library, and the game's saves and log live in it. DM_ROOT (the
+        // learned shader state, env.txt), temp/ and everything else stay under ~/Library — only
+        // what is needed and reused comes along, so the library folder stays lean.
+        char probe[PATH_MAX]; snprintf(probe, sizeof probe, "%s/.nx2apple-library", appdir);
+        if (!stat(probe, &st)) {
+            snprintf(save, sizeof save, "%s/save/%s", appdir, id);
+            snprintf(logs, sizeof logs, "%s/log", appdir);
+            // First run after this change: bring the old saves across rather than starting the
+            // player on an empty save slot. Copied, not moved — an older build of the same app
+            // still reads the old place, and a copy is cheap next to a save.
+            char oldsave[PATH_MAX], oldlog[PATH_MAX];
+            snprintf(oldsave, sizeof oldsave, "%s/save", sup);
+            snprintf(oldlog, sizeof oldlog, "%s/Library/Logs/nxnative/%s.log", home, id);
+            mkdirs(save); mkdirs(logs);
+            if (dir_has_entries(oldsave) && !dir_has_entries(save)) {
+                copy_tree(oldsave, save);
+                fprintf(stderr, "launcher: saves copied from %s to %s (they now live with the game)\n", oldsave, save);
+            }
+            // The previous session's log becomes the .prev.log, as the rotation below would have
+            // it: the .log itself is opened fresh right after, and copying into it would be
+            // truncated away by that open.
+            char newprev[PATH_MAX]; snprintf(newprev, sizeof newprev, "%s/%s.prev.log", logs, id);
+            // Explicit compares, not !stat(): stat() returns -1 when the file is missing, which is
+            // truthy — reading "!" as "does not exist" here inverted the condition and skipped the
+            // copy (found by the end-to-end test below).
+            if (stat(oldlog, &st) == 0 && stat(newprev, &st) != 0) {
+                if (copy_file(oldlog, newprev)) fprintf(stderr, "launcher: the previous log was copied from %s\n", oldlog);
+            }
+        }
+    }
     snprintf(log, sizeof log, "%s/%s.log", logs, id); snprintf(prev, sizeof prev, "%s/%s.prev.log", logs, id);
     mkdirs(save); mkdirs(temp); mkdirs(logs);
+    // Say where this game reads and writes, on every start: a player checking whether their saves
+    // moved with the game should not have to know the migration rules.
+    fprintf(stderr, "launcher: saves %s\nlauncher: log %s\n", save, log);
     rename(log, prev);
     int fd = open(log, O_WRONLY | O_CREAT | O_TRUNC, 0644);
     if (fd >= 0) { dup2(fd, 1); dup2(fd, 2); close(fd); setvbuf(stdout, NULL, _IOLBF, 0); }