Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
Improve compatibility tool registration
NotProtonNotd754e1babaaa
diff --git a/app/Sources/NotProtonApp/Model/RunnerStore.swift b/app/Sources/NotProtonApp/Model/RunnerStore.swiftindex 7da95d6..251ef7a 100644--- a/app/Sources/NotProtonApp/Model/RunnerStore.swift+++ b/app/Sources/NotProtonApp/Model/RunnerStore.swift@@ -108,7 +108,7 @@ enum CompatToolList {return (builds, listed, SupportedRunners.tools(for: builds, legacy: holder))}- // 1.0/1.0.1 used the single 'notproton' tool name and wrote no tool list, so the+ // 1.0.x used the single 'notproton' tool name and wrote no tool list, so the// runners/current symlink is the record of which version of CrossOver was deployed.static func legacyHolder(builds: [RunnerBuild], listed: String?, runners: URL = SupportPaths.runners@@ -127,7 +127,7 @@ enum CompatToolList {ids.contains(id), SupportedRunners.legacyHolders.contains(id) {return .build(id)}- return .preferred+ return .nobody}// Returns true when the list file changed.diff --git a/app/Sources/NotProtonApp/Model/SupportedRunner.swift b/app/Sources/NotProtonApp/Model/SupportedRunner.swiftindex 7ad77ca..3b26042 100644--- a/app/Sources/NotProtonApp/Model/SupportedRunner.swift+++ b/app/Sources/NotProtonApp/Model/SupportedRunner.swift@@ -81,15 +81,13 @@ enum SupportedRunners {static let legacyHolders = ["27.0.0.40921-fex", "27.0.0.40921", "27.0.0.41069-fex", "27.0.0.41069"]enum LegacyHolder: Equatable, Sendable {- case preferredcase build(String)case nobody}- static func tools(for builds: [RunnerBuild], legacy: LegacyHolder = .preferred) -> [InstalledTool] {+ static func tools(for builds: [RunnerBuild], legacy: LegacyHolder = .nobody) -> [InstalledTool] {let installed = Set(builds.map(\.id))let holder: String? = switch legacy {- case .preferred: legacyHolders.first(where: installed.contains)case .build(let id): idcase .nobody: nil}diff --git a/app/Tests/NotProtonAppTests/DetectionTests.swift b/app/Tests/NotProtonAppTests/DetectionTests.swiftindex 93351c1..096ee36 100644--- a/app/Tests/NotProtonAppTests/DetectionTests.swift+++ b/app/Tests/NotProtonAppTests/DetectionTests.swift@@ -104,7 +104,7 @@ struct CompatToolOrderTests {let tools = SupportedRunners.tools(for: [try build("26.3.0.39832"), try build("27.0.0.40921-fex"),])- #expect(tools.map(\.name) == ["notproton", "notproton-fex-rosetta", "notproton-26.3"])+ #expect(tools.map(\.name) == ["notproton-fex", "notproton-fex-rosetta", "notproton-26.3"])#expect(tools.map(\.build) == ["27.0.0.40921-fex", "27.0.0.40921-fex", "26.3.0.39832"])}@@ -113,14 +113,14 @@ struct CompatToolOrderTests {let tools = SupportedRunners.tools(for: [try build("27.0.0.40921-fex"), try build("27.0.0.40921"),])- #expect(tools.map(\.name) == ["notproton", "notproton-fex-rosetta", "notproton-preview"])+ #expect(tools.map(\.name) == ["notproton-fex", "notproton-fex-rosetta", "notproton-preview"])#expect(tools.map(\.build) == ["27.0.0.40921-fex", "27.0.0.40921-fex", "27.0.0.40921"])#expect(Set(tools.map(\.display)).count == tools.count)}- @Test("The Rosetta-only Preview serves the legacy name when it is the only Preview")+ @Test("The Rosetta-only Preview can hold the legacy name")func rosettaPreviewAlone() throws {- let tools = SupportedRunners.tools(for: [try build("27.0.0.40921")])+ let tools = SupportedRunners.tools(for: [try build("27.0.0.40921")], legacy: .build("27.0.0.40921"))#expect(tools.map(\.name) == ["notproton"])#expect(tools.first?.tool.flavor == .rosetta)}diff --git a/app/Tests/NotProtonAppTests/RunnerSwitchTests.swift b/app/Tests/NotProtonAppTests/RunnerSwitchTests.swiftindex 36a8241..9ffb1f4 100644--- a/app/Tests/NotProtonAppTests/RunnerSwitchTests.swift+++ b/app/Tests/NotProtonAppTests/RunnerSwitchTests.swift@@ -9,6 +9,8 @@ struct RunnerPrepareTests {private static let rosetta = SupportedRunners.all.first { $0.id == "27.0.0.40921" }!private static let fex = SupportedRunners.all.first { $0.flavor == "fex" }!private static let release = SupportedRunners.all.first { $0.id == "26.3.0.39832" }!+ private static let preview41069 = SupportedRunners.all.first { $0.id == "27.0.0.41069" }!+ private static let fex41069 = SupportedRunners.all.first { $0.id == "27.0.0.41069-fex" }!private static let licensed = CrossOverLicense.Status(licensed: true, detail: "CrossOver is activated.", diagnostic: "test"@@ -106,7 +108,7 @@ struct RunnerPrepareTests {#expect(toolList(runners) == CompatToolList.contents(SupportedRunners.tools(for: [Self.release, Self.fex])))- #expect(toolList(runners)?.hasPrefix("notproton\t27.0.0.40921-fex\tfex\t") == true)+ #expect(toolList(runners)?.hasPrefix("notproton-fex\t27.0.0.40921-fex\tfex\t") == true)let again = try prepare(Self.release, runners: runners, calls: calls)#expect(!again.toolsChanged)@@ -242,6 +244,10 @@ struct RunnerPrepareTests {func secondPreviewRenamesNothing() throws {let runners = try makeRunners(cloning: [Self.fex])defer { try? FileManager.default.removeItem(at: runners) }+ try FileManager.default.createSymbolicLink(+ atPath: runners.appending(path: "current").path(percentEncoded: false),+ withDestinationPath: "crossover-\(Self.fex.id)/CrossOver"+ )try sync(runners)#expect(holder(runners) == Self.fex.id)@@ -255,6 +261,33 @@ struct RunnerPrepareTests {#expect(toolList(runners)?.contains("notproton-fex-rosetta\t\(Self.fex.id)\t") == true)}+ @Test("A fresh install gives the legacy name to nobody")+ func freshInstallHasNoLegacyName() throws {+ let runners = try makeRunners(cloning: [Self.preview41069, Self.fex41069])+ defer { try? FileManager.default.removeItem(at: runners) }++ try sync(runners)+ #expect(holder(runners) == nil)+ #expect(toolList(runners)?.hasPrefix("notproton-fex-41069\t\(Self.fex41069.id)\tfex\t") == true)+ #expect(toolList(runners)?.contains("notproton-preview-41069\t\(Self.preview41069.id)\t") == true)+ }++ @Test("A 1.0.3 install keeps the legacy name on 41069")+ func legacyNameFollows41069() throws {+ let runners = try makeRunners(cloning: [Self.preview41069, Self.fex41069])+ defer { try? FileManager.default.removeItem(at: runners) }+ try FileManager.default.createSymbolicLink(+ atPath: runners.appending(path: "current").path(percentEncoded: false),+ withDestinationPath: "crossover-\(Self.preview41069.id)/CrossOver"+ )++ try sync(runners)+ #expect(holder(runners) == Self.preview41069.id)+ try sync(runners)+ #expect(holder(runners) == Self.preview41069.id)+ #expect(toolList(runners)?.contains("notproton-fex-41069\t\(Self.fex41069.id)\tfex\t") == true)+ }+@Test("A Preview listed under its own name does not take the legacy name later")func ownNameSticks() throws {let runners = try makeRunners(cloning: [Self.rosetta, Self.fex])diff --git a/dylib/feats/compat.c b/dylib/feats/compat.cindex 4947703..7b6f25c 100644--- a/dylib/feats/compat.c+++ b/dylib/feats/compat.c@@ -291,8 +291,6 @@ void np_compat_export_tools_path(void) {tools_dir);}-#define TOOL_LIST_MAX 8-typedef struct {char name[64];char build[64];@@ -301,8 +299,10 @@ typedef struct {char dir[640];} tool_entry_t;-static tool_entry_t g_tools[TOOL_LIST_MAX];+// Steam keeps pointers into g_tools.+static tool_entry_t *g_tools;static int g_tool_count;+static int g_tool_cap;static int g_tool_list_present;static int is_name_char(char c, const char *extra) {@@ -387,10 +387,16 @@ int np_compat_load_tool_list(const char *path, const char *tools_dir) {"(build %s) is skipped", entry.name, entry.build);continue;}- if (g_tool_count == TOOL_LIST_MAX) {- NP_WARN("np_compat_load_tool_list: more than %d tools listed, %s skipped",- TOOL_LIST_MAX, entry.name);- continue;+ if (g_tool_count == g_tool_cap) {+ int cap = g_tool_cap ? g_tool_cap * 2 : 8;+ tool_entry_t *grown = realloc(g_tools, (size_t)cap * sizeof(*grown));+ if (!grown) {+ NP_WARN("np_compat_load_tool_list: out of memory at %s, the rest is skipped",+ entry.name);+ break;+ }+ g_tools = grown;+ g_tool_cap = cap;}snprintf(entry.dir, sizeof(entry.dir), "%s/%s", tools_dir, entry.name);@@ -421,6 +427,11 @@ static void load_tool_list_once(void) {np_compat_load_tool_list(path, tools_dir);}+uint32_t np_compat_manager_tools_max(void) {+ load_tool_list_once();+ return COMPAT_MANAGER_TOOLS_HEADROOM + (uint32_t)g_tool_count;+}+static int write_tool(const tool_entry_t *tool) {if (ensure_dir(tool->dir) != 0) {NP_WARN("np_compat_ensure_tool_manifest: cannot create %s", tool->dir);@@ -730,7 +741,7 @@ static void *find_tool(void *compat_mgr, const char *wanted) {uint8_t *base = (uint8_t *)compat_mgr;uint8_t *array = *(uint8_t **)(base + COMPAT_MANAGER_TOOL_ARRAY_OFF);uint32_t count = *(uint32_t *)(base + COMPAT_MANAGER_TOOL_COUNT_OFF);- if (!array || count > COMPAT_MANAGER_TOOLS_MAX) {+ if (!array || count > np_compat_manager_tools_max()) {// Once, because Steam walks this for every windows-only app as the library// redraws and a count this wrong does not correct itself.static const char once = 0;@@ -768,7 +779,11 @@ void np_compat_register_crossover(void *compat_mgr) {// the registration gate, from_oslist=windows intersects the app mask so the// dropdown keeps it, and appid 0 marks a manager-local tool the resolver routes// to the local run script.- static uint8_t buffers[TOOL_LIST_MAX][COMPAT_TOOL_STRIDE + 64];+ uint8_t (*buffers)[COMPAT_TOOL_STRIDE + 64] = calloc((size_t)g_tool_count, sizeof(*buffers));+ if (g_tool_count && !buffers) {+ NP_WARN("np_compat_register_crossover: out of memory, no tool registered");+ return;+ }static const char from_oslist[] = "windows";static const char to_oslist[] = "macos";diff --git a/dylib/feats/compat.h b/dylib/feats/compat.hindex 3d531bb..0dbb5b9 100644--- a/dylib/feats/compat.h+++ b/dylib/feats/compat.h@@ -12,9 +12,7 @@#define COMPAT_MANAGER_TOOL_COUNT_OFF 0x330#define COMPAT_TOOL_NAME_OFF 0x40-// Sanity cap: the client ships one tool on macOS, so a count above this means-// the offset moved and the array walk would read past the end.-#define COMPAT_MANAGER_TOOLS_MAX 32+#define COMPAT_MANAGER_TOOLS_HEADROOM 32// Platform bits GetValidPlatforms reports.#define COMPAT_PLATFORM_WINDOWS 0x1@@ -119,6 +117,8 @@ void *np_compat_manager(void);// Manager-owned. Callers read fields without taking ownership.void *np_compat_registered_tool(void *compat_mgr);+uint32_t np_compat_manager_tools_max(void);+// Absolute path to the directory of the first tool in the app's list, or NULL.const char *np_compat_tool_dir(void);diff --git a/dylib/feats/compatsvc.c b/dylib/feats/compatsvc.cindex 954ba18..7bf5542 100644--- a/dylib/feats/compatsvc.c+++ b/dylib/feats/compatsvc.c@@ -1,4 +1,5 @@// CompatManager WebUI service.+#include <stdlib.h>#include <string.h>#include "compatsvc.h"@@ -52,9 +53,7 @@#define WIRE_VARINT 0#define WIRE_BYTES 2-// Per-tool and per-reply size caps, for safety.#define TOOL_MAX 512-#define BODY_MAX (TOOL_MAX * 8)// No appid equals the global Steam Play setting, answered with windows.#define PLATFORMS_UNSCOPED COMPAT_PLATFORM_WINDOWS@@ -155,7 +154,7 @@ static int get_compat_tools(uintptr_t request, uintptr_t response) {const uint8_t *array = *(const uint8_t **)(mgr + COMPAT_MANAGER_TOOL_ARRAY_OFF);uint32_t count = *(const uint32_t *)(mgr + COMPAT_MANAGER_TOOL_COUNT_OFF);- if (!array || count > COMPAT_MANAGER_TOOLS_MAX) {+ if (!array || count > np_compat_manager_tools_max()) {NP_ERR("compatsvc: the manager reports %u tool(s) at %p, which is not a list ""this reads", count, (const void *)array);return RESULT_FAIL;@@ -195,8 +194,15 @@ static int get_compat_tools(uintptr_t request, uintptr_t response) {if (prio > best) best = prio;}- uint8_t body[BODY_MAX];- np_pb_t w = { body, sizeof body, 0, 1 };+ // Every tool plus the none entry and the selected and default names.+ size_t room = ((size_t)count + 3) * TOOL_MAX;+ uint8_t *body = malloc(room);+ if (!body) {+ NP_ERR("compatsvc: no memory for the reply to app %u", appid);+ return RESULT_FAIL;+ }+ np_pb_t w = { body, room, 0, 1 };+ int result = RESULT_FAIL;uint32_t listed = 0;for (uint32_t i = 0; i < count; i++) {@@ -213,7 +219,7 @@ static int get_compat_tools(uintptr_t request, uintptr_t response) {best != 0 && prio != best && prio > 0);if (!w.ok) {NP_ERR("compatsvc: tool %s does not fit the reply", name);- return RESULT_FAIL;+ goto done;}listed++;}@@ -224,7 +230,7 @@ static int get_compat_tools(uintptr_t request, uintptr_t response) {holds_none, 0);if (!w.ok) {NP_ERR("compatsvc: the entry for using no tool does not fit the reply");- return RESULT_FAIL;+ goto done;}listed++;}@@ -237,19 +243,22 @@ static int get_compat_tools(uintptr_t request, uintptr_t response) {if (!w.ok) {NP_ERR("compatsvc: %u tool(s) do not fit the reply to app %u", listed, appid);- return RESULT_FAIL;+ goto done;}if (!np_webui_parse_into(response, body, (uint32_t)w.len)) {NP_ERR("compatsvc: the client rejected the %zu byte reply built for app %u",w.len, appid);- return RESULT_FAIL;+ goto done;}NP_LOG("compatsvc: app %u is offered %u entr(ies) from %u tool(s) on platforms ""0x%x, mapped to %s, running under %s, defaulting to %s", appid, listed,count, platforms, chosen ? chosen : "no tool",active ? active : "no tool", fallback ? fallback : "no tool");- return RESULT_OK;+ result = RESULT_OK;+done:+ free(body);+ return result;}// Reads a protobuf string field.diff --git a/dylib/tests/compatcheck.c b/dylib/tests/compatcheck.cindex 9675824..5ed5e65 100644--- a/dylib/tests/compatcheck.c+++ b/dylib/tests/compatcheck.c@@ -149,7 +149,7 @@ static void enabled_cases(void) {// A manager holding `count` entries, the one at `named_slot` carrying the tool name.static uint8_t manager[0x400];-static uint8_t entries[COMPAT_MANAGER_TOOLS_MAX * (COMPAT_TOOL_STRIDE + 64)];+static uint8_t entries[(COMPAT_MANAGER_TOOLS_HEADROOM + 64) * (COMPAT_TOOL_STRIDE + 64)];static void *build_manager(uint32_t count, int named_slot) {memset(manager, 0, sizeof manager);@@ -176,9 +176,11 @@ static void manager_cases(void) {// A count this large means the offset it was read from moved, so the array it// describes is not one to walk.- check(np_compat_registered_tool(build_manager(COMPAT_MANAGER_TOOLS_MAX + 1, 1)) == NULL,+ uint32_t cap = np_compat_manager_tools_max();+ check(cap <= COMPAT_MANAGER_TOOLS_HEADROOM + 64, "the manager fixture holds the cap");+ check(np_compat_registered_tool(build_manager(cap + 1, 1)) == NULL,"a count past the cap is refused rather than walked");- check(np_compat_registered_tool(build_manager(COMPAT_MANAGER_TOOLS_MAX, -1)) == NULL,+ check(np_compat_registered_tool(build_manager(cap, -1)) == NULL,"a count at the cap is still walked");mgr = build_manager(3, 1);@@ -229,6 +231,19 @@ static void tool_list_cases(void) {check(np_compat_registered_tool(mgr) == entries,"the first listed tool wins when the manager holds it");+ f = fopen(path, "w");+ if (!f) {+ check(0, "the long tool list fixture can be written");+ return;+ }+ for (int i = 0; i < 40; i++)+ fprintf(f, "notproton-%d\t1\trosetta\tTool %d\n", i, i);+ fclose(f);+ kept = np_compat_load_tool_list(path, "/tools");+ unlink(path);+ check(kept == 40 && strcmp(g_tools[39].name, "notproton-39") == 0+ && strcmp(g_tools[0].dir, "/tools/notproton-0") == 0, "a long list keeps every tool");+check(np_compat_load_tool_list("out/compatcheck-missing", "/tools") == 0&& !g_tool_list_present, "a missing list keeps no tools and says so");}diff --git a/dylib/tests/compatsvc-check.c b/dylib/tests/compatsvc-check.cindex 4090e68..bc8e0e1 100644--- a/dylib/tests/compatsvc-check.c+++ b/dylib/tests/compatsvc-check.c@@ -35,6 +35,8 @@ size_t np_compat_tool_stride(void) { return 0x130 + stub_tool_shift; }uint32_t np_compat_enabled_off(void) { return stub_enabled_off_val; }+uint32_t np_compat_manager_tools_max(void) { return 40; }+uint32_t np_compat_valid_platforms(void *mgr, uint32_t appid) {(void)mgr; (void)appid;return stub_platforms_val;