From cce5fe9c4a9de205ce916c64ef57b6f3826a9c19 Mon Sep 17 00:00:00 2001 From: Avery Felts Date: Fri, 20 Nov 2026 08:25:48 -0700 Subject: [PATCH] fix(backend): a game's Retina in every prefix, and a stale launch wrapper replaced A game's own Retina setting reached only a bottle (bottles/). A Windows program runs in Steam's prefix, so the setting it shows in Game settings was ignored there; and when another game in that prefix did change it, it stayed for every Steam game after it until Steam's next start. Each launch now sets the game's own Retina, or Settings > Graphics', in whatever prefix it starts in, and only when user.reg does not hold it already. The Wine launch wrapper in cache/ was written once and, when an earlier MetalSharp's wrapper was there instead, refused: a game's window mode and resolution were then dropped until MetalSharp was reinstalled. It is now replaced atomically with this MetalSharp's. Follow-up to #811. --- app/src-c/runtime/game_tuning.c | 16 +++---- app/src-c/runtime/steam_actions.c | 52 +++++++++++++++-------- app/src-c/tests/game_tuning_test.c | 60 +++++++++++++++++++++++++++ app/src-c/tests/runtime_routes_test.c | 12 ++++++ 4 files changed, 115 insertions(+), 25 deletions(-) diff --git a/app/src-c/runtime/game_tuning.c b/app/src-c/runtime/game_tuning.c index 74bc9ab8..ab54b673 100644 --- a/app/src-c/runtime/game_tuning.c +++ b/app/src-c/runtime/game_tuning.c @@ -1030,16 +1030,18 @@ static bool prefix_section_has(const char* prefix, const char* section, const ch return found; } -/* A game in a bottle of its own (bottles/): the prefix's Retina (Mac Driver RetinaMode and its DPI), the game's - * own setting or else Settings > Graphics' (metalsharp/MetalSharp#797; Steam's prefix has it from Steam's start). - * Imported only when the prefix's user.reg does not already hold it. */ -static void apply_bottle_retina(const char* home, const char* prefix, const ms_game_tuning* tuning) { +/* The prefix's Retina (Mac Driver RetinaMode and its DPI) for the game about to start: the game's own setting or + * else Settings > Graphics' (metalsharp/MetalSharp#797). Every prefix, not only a bottle of the game's own: a Windows + * program runs in Steam's prefix, and its own setting was ignored there; and one program's setting stayed in Steam's + * prefix for every Steam game after it, until Steam's next start set Settings > Graphics' again (#811). Each launch + * now sets its own. Imported only when the prefix's user.reg does not already hold it. */ +static void apply_prefix_retina(const char* home, const char* prefix, const ms_game_tuning* tuning) { bool retina = tuning->retina >= 0 ? tuning->retina > 0 : ms_config_retina_enabled(home); const char* retina_line = retina ? "\"RetinaMode\"=\"Y\"" : "\"RetinaMode\"=\"N\""; const char* dpi_line = retina ? "\"LogPixels\"=dword:000000c0" : "\"LogPixels\"=dword:00000060"; bool current; char reg[256]; - if (!prefix || !strstr(prefix, "/bottles/")) + if (!prefix || !prefix[0]) return; /* A bottle Wine has not made yet starts with Retina off at 96 DPI, so only Retina on needs writing to it. */ if (!retina) { @@ -1059,7 +1061,7 @@ static void apply_bottle_retina(const char* home, const char* prefix, const ms_g "[HKEY_CURRENT_USER\\Control Panel\\Desktop]\r\n\"LogPixels\"=dword:%08x\r\n", retina ? 'Y' : 'N', retina ? 192 : 96); if (!import_registry(home, prefix, reg)) - ms_log_event(home, "Game settings: could not write the bottle's Retina setting"); + ms_log_event(home, "Game settings: could not write the prefix's Retina setting"); } void ms_game_tuning_prepare_launch(const char* home, const char* provider, const char* id, const char* prefix, @@ -1074,7 +1076,7 @@ void ms_game_tuning_prepare_launch(const char* home, const char* provider, const free(exe_dir); return; } - apply_bottle_retina(home, prefix, &tuning); + apply_prefix_retina(home, prefix, &tuning); applied_load(home, provider, id, &state); if (ms_game_tuning_is_default(&tuning) && !state.prefix && !state.dirs[0]) { /* Never tuned: nothing to write or remove. */ diff --git a/app/src-c/runtime/steam_actions.c b/app/src-c/runtime/steam_actions.c index b224d7f7..f0ea3b4e 100644 --- a/app/src-c/runtime/steam_actions.c +++ b/app/src-c/runtime/steam_actions.c @@ -241,10 +241,39 @@ char* ms_steam_wine_launch_wrapper_path(const char* home) { free(path); return NULL; } - int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0700); - if (fd >= 0) { + /* The wrapper this MetalSharp writes, unless it is already there. One an earlier MetalSharp wrote (another + * wrapper text) is replaced, not kept: with it refused, a game's window mode and resolution were dropped until + * MetalSharp was reinstalled (metalsharp/MetalSharp#811). */ + { + char* existing = read_bounded_file(path); + bool matches = existing && !strcmp(existing, wrapper) && access(path, X_OK) == 0; + free(existing); + if (matches) { + free(cache); + return path; + } + } + { + char temporary[PATH_MAX]; size_t length = sizeof(wrapper) - 1; size_t offset = 0; + bool saved; + int fd; + static unsigned serial; + unsigned mine = __atomic_fetch_add(&serial, 1, __ATOMIC_RELAXED); + if ((size_t)snprintf(temporary, sizeof(temporary), "%s.%ld.%u.tmp", path, (long)getpid(), mine) >= + sizeof(temporary)) { + free(cache); + free(path); + return NULL; + } + (void)unlink(temporary); + fd = open(temporary, O_WRONLY | O_CREAT | O_EXCL, 0700); + if (fd < 0) { + free(cache); + free(path); + return NULL; + } while (offset < length) { ssize_t written = write(fd, wrapper + offset, length - offset); if (written < 0 && errno == EINTR) @@ -253,28 +282,15 @@ char* ms_steam_wine_launch_wrapper_path(const char* home) { break; offset += (size_t)written; } - bool saved = offset == length && fchmod(fd, 0700) == 0; + saved = offset == length && fchmod(fd, 0700) == 0; if (close(fd) != 0) saved = false; - if (!saved) { - (void)unlink(path); - free(cache); - free(path); - return NULL; - } - } else if (errno == EEXIST) { - char* existing = read_bounded_file(path); - bool matches = existing && !strcmp(existing, wrapper) && access(path, X_OK) == 0; - free(existing); - if (!matches) { + if (!saved || rename(temporary, path) != 0) { + (void)unlink(temporary); free(cache); free(path); return NULL; } - } else { - free(cache); - free(path); - return NULL; } free(cache); return path; diff --git a/app/src-c/tests/game_tuning_test.c b/app/src-c/tests/game_tuning_test.c index 6846f1fe..e6979f06 100644 --- a/app/src-c/tests/game_tuning_test.c +++ b/app/src-c/tests/game_tuning_test.c @@ -392,6 +392,65 @@ static void test_renderers_and_hud(const char* home) { } } +/* A game's own Retina reaches the prefix it starts in, Steam's included, where Windows programs run; a game that keeps + * the default sets Settings > Graphics' back (issue 811). Imported only when user.reg does not hold it already. */ +static void test_prefix_retina(const char* home) { + char prefix[PATH_MAX], path[PATH_MAX], log[PATH_MAX], game[PATH_MAX], exe[PATH_MAX]; + int status = 0; + char *out, *text; + const char* body = "{\"provider\":\"windows\",\"id\":\"retina-app\",\"retina\":\"on\"}"; + snprintf(prefix, sizeof(prefix), "%s/prefix-steam", home); + snprintf(path, sizeof(path), "%s/drive_c", prefix); + (void)mkdir(prefix, 0700); + assert(mkdir(path, 0700) == 0); + snprintf(path, sizeof(path), "%s/runtime", home); + (void)mkdir(path, 0700); + snprintf(path, sizeof(path), "%s/runtime/wine", home); + (void)mkdir(path, 0700); + snprintf(path, sizeof(path), "%s/runtime/wine/bin", home); + (void)mkdir(path, 0700); + /* Wine's `reg import`: the imported file goes into a log */ + snprintf(log, sizeof(log), "%s/imported.reg", home); + snprintf(path, sizeof(path), "%s/runtime/wine/bin/wine", home); + { + char script[PATH_MAX * 2]; + snprintf(script, sizeof(script), + "#!/bin/sh\ncat \"$WINEPREFIX/drive_c/metalsharp-game-settings.reg\" >> '%s'\n", log); + spit(path, script); + assert(chmod(path, 0700) == 0); + } + snprintf(path, sizeof(path), "%s/user.reg", prefix); + spit(path, "WINE REGISTRY Version 2\n\n[Software\\\\Wine\\\\Mac Driver] 1\n\"RetinaMode\"=\"N\"\n\n" + "[Control Panel\\\\Desktop] 1\n\"LogPixels\"=dword:00000060\n"); + snprintf(game, sizeof(game), "%s/Retina Program", home); + snprintf(exe, sizeof(exe), "%s/Program.exe", game); + assert(mkdir(game, 0700) == 0); + spit(exe, "MZ"); + + out = ms_game_tuning_save_json(home, body, strlen(body), &status); + assert(status == 200 && strstr(out, "\"retina\":\"on\"")); + free(out); + ms_game_tuning_prepare_launch(home, "windows", "retina-app", prefix, NULL, exe); + text = slurp(log); + assert(text && strstr(text, "\"RetinaMode\"=\"Y\"") && strstr(text, "\"LogPixels\"=dword:000000c0")); + free(text); + (void)unlink(log); + + /* The prefix holds it now (as Wine's import would leave it): nothing is imported again. */ + snprintf(path, sizeof(path), "%s/user.reg", prefix); + spit(path, "WINE REGISTRY Version 2\n\n[Software\\\\Wine\\\\Mac Driver] 1\n\"RetinaMode\"=\"Y\"\n\n" + "[Control Panel\\\\Desktop] 1\n\"LogPixels\"=dword:000000c0\n"); + ms_game_tuning_prepare_launch(home, "windows", "retina-app", prefix, NULL, exe); + assert(access(log, F_OK) != 0); + + /* A Steam game with no Retina of its own after it: Settings > Graphics' (off here) goes back. */ + ms_game_tuning_prepare_launch(home, "steam", "4000", prefix, game, exe); + text = slurp(log); + assert(text && strstr(text, "\"RetinaMode\"=\"N\"") && strstr(text, "\"LogPixels\"=dword:00000060")); + free(text); + (void)unlink(log); +} + int main(int argc, char** argv) { char temporary[] = "/tmp/metalsharp-game-tuning-test-XXXXXX"; const char* home = argc > 1 ? argv[1] : mkdtemp(temporary); @@ -403,6 +462,7 @@ int main(int argc, char** argv) { test_local_env(home); test_routes_and_launch(home); test_renderers_and_hud(home); + test_prefix_retina(home); printf("game tuning tests passed\n"); return 0; } diff --git a/app/src-c/tests/runtime_routes_test.c b/app/src-c/tests/runtime_routes_test.c index 65442e7d..44896856 100644 --- a/app/src-c/tests/runtime_routes_test.c +++ b/app/src-c/tests/runtime_routes_test.c @@ -2667,6 +2667,18 @@ int main(int argc, char** argv) { args = read_bounded_file(wine_log); assert(args && !strstr(args, "explorer\n") && strstr(args, "Windowed Game.exe\n")); free(args); + /* A wrapper an earlier MetalSharp wrote is replaced with this one's, so a game's window mode and + * resolution still reach it (issue 811); before, it was refused and they were dropped until a reinstall. */ + fixture(home, "cache/metalsharp-wine-game-launcher", "#!/bin/sh\nexec \"$METALSHARP_WINE_BINARY\" \"$@\"\n"); + assert(chmod(wrapper, 0700) == 0); + { + char* refreshed = ms_steam_wine_launch_wrapper_path(home); + char* text = refreshed ? read_bounded_file(refreshed) : NULL; + assert(refreshed && !strcmp(refreshed, wrapper) && access(refreshed, X_OK) == 0); + assert(text && strstr(text, "/desktop=$desktop")); + free(text); + free(refreshed); + } unsetenv("METALSHARP_GAME_WINDOW_MODE"); unsetenv("METALSHARP_GAME_RESOLUTION"); unsetenv("METALSHARP_WINE_BINARY");