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
fix(controllers): append new shipped mappings to an existing gamecontrollerdb.txt (#2400)
Raystorms5ca50cb9028c
diff --git a/AGENTS.md b/AGENTS.mdindex a61178a..aeb964e 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -60,7 +60,7 @@ lint targets, and its list is `CI_CHECKS` in the Makefile:- **C, shell and Python suites:** `sigcheck`, `callscheck`, `np-test-batch-check`, `webpatch-fixtures`, `peicon-fixtures`,`panel-behavior`, `spawn-env`, `activatecheck`, `compat-reported-import-check`, `compatcheck`, `compatsvc-check`, `prefixstate-check`,`shortcutcheck`, `launch-shell`, `envcheck`, `buildcheck`, `settingscheck`, `routecheck`,- `bridgecheck`, `envpasscheck`, `prefixnotecheck`, `powernotecheck`, `exitcheck`, `anticheatcheck`, `launchprogresscheck`, `gamedrivecheck`, `launchercheck`, `seedcheck`, `gamefixescheck`, `versioncheck`, `cicheck`,+ `bridgecheck`, `envpasscheck`, `prefixnotecheck`, `powernotecheck`, `exitcheck`, `anticheatcheck`, `launchprogresscheck`, `gamedrivecheck`, `launchercheck`, `seedcheck`, `mappingscheck`, `gamefixescheck`, `versioncheck`, `cicheck`,`padcheck`, `detourcheck`, `compat-reported-check`, `np-inventory-check`, `np-native-tests`, `native-shim-tests`,`native-installercheck`diff --git a/Makefile b/Makefileindex 9cbcac9..84133e4 100644--- a/Makefile+++ b/Makefile@@ -86,7 +86,7 @@ DEPS := $(OBJS:.o=.d)overlay-shim-bench iconmaker icon \appinfo helpers-install ntdll-resolve detourcheck bridge runcheck compatcheck \compatsvc-check prefixstate-check shortcutcheck scriptcheck envcheck buildcheck settingscheck routecheck bridgecheck envpasscheck prefixnotecheck powernotecheck exitcheck anticheatcheck launchprogresscheck gamedrivecheck \- gamefixescheck launchercheck padcheck launch-shell activatecheck stamp-check versioncheck cicheck compat-reported-check FORCE+ gamefixescheck launchercheck padcheck mappingscheck launch-shell activatecheck stamp-check versioncheck cicheck compat-reported-check FORCEAPP_PAYLOAD := app/Sources/BridgeplayApp/Resources/payload@@ -190,6 +190,9 @@ padcheck:@if [ ! -f dylib/tests/padcheck.sh ]; then $(call SKIP,padcheck,dylib/tests/padcheck.sh); exit 0; fi; \sh dylib/tests/padcheck.sh dylib/feats/compat_run.sh+mappingscheck:+ @sh installer/tests/mappingscheck.sh installer/Install.command+launchercheck:@if [ ! -f dylib/tests/launchercheck.sh ]; then $(call SKIP,launchercheck,dylib/tests/launchercheck.sh); exit 0; fi; \sh dylib/tests/launchercheck.sh dylib/feats/compat_run.sh@@ -928,7 +931,7 @@ CI_CHECKS := mincheck l10ncheck \sigcheck callscheck np-test-batch-check webpatch-fixtures peicon-fixtures panel-behavior spawn-env \activatecheck compat-reported-import-check compatcheck compatsvc-check prefixstate-check shortcutcheck launch-shell envcheck \buildcheck settingscheck routecheck bridgecheck envpasscheck prefixnotecheck powernotecheck exitcheck anticheatcheck launchprogresscheck gamedrivecheck launchercheck \- seedcheck gamefixescheck versioncheck cicheck padcheck detourcheck compat-reported-check np-inventory-check np-native-tests native-shim-tests \+ seedcheck gamefixescheck versioncheck cicheck padcheck mappingscheck detourcheck compat-reported-check np-inventory-check np-native-tests native-shim-tests \app-tests native-installercheck# app.yml runs the checks that need the built app in its build job and the rest in adiff --git a/app/Sources/BridgeplayApp/Model/SteamInstaller.swift b/app/Sources/BridgeplayApp/Model/SteamInstaller.swiftindex 2e64472..3136e9c 100644--- a/app/Sources/BridgeplayApp/Model/SteamInstaller.swift+++ b/app/Sources/BridgeplayApp/Model/SteamInstaller.swift@@ -1,5 +1,6 @@// 'Install' logic for the Steam patch componen+import BridgeplayInstallerCoreimport Foundationenum InstallPhase: Sendable {@@ -312,16 +313,18 @@ enum SteamInstaller {}}- // The user's own mappings, so an existing file stays. Without one a pad SDL does not know,- // such as the GameSir G7 Pro, reaches games in its raw button order.+ // The user's own mappings stay; shipped ones they lack are appended. Without a mapping a pad+ // SDL does not know, such as the GameSir G7 Pro, reaches games in its raw button order.static func seedControllerMappings(from payload: URL, in support: URL) {let fm = FileManager.defaultlet source = payload.appending(path: "gamecontrollerdb.txt")let mappings = support.appending(path: "gamecontrollerdb.txt")- guard fm.fileExists(atPath: source.path(percentEncoded: false)),- !fm.fileExists(atPath: mappings.path(percentEncoded: false))- else { return }+ guard fm.fileExists(atPath: source.path(percentEncoded: false)) else { return }do {+ if fm.fileExists(atPath: mappings.path(percentEncoded: false)) {+ try ControllerMappings.update(mappings, from: source)+ return+ }try fm.createDirectory(at: support, withIntermediateDirectories: true)try fm.copyItem(at: source, to: mappings)} catch {diff --git a/app/Sources/BridgeplayInstallerCore/ComponentsPlacement.swift b/app/Sources/BridgeplayInstallerCore/ComponentsPlacement.swiftindex e3ed4d9..ddcf2af 100644--- a/app/Sources/BridgeplayInstallerCore/ComponentsPlacement.swift+++ b/app/Sources/BridgeplayInstallerCore/ComponentsPlacement.swift@@ -75,11 +75,17 @@ struct ComponentsPlacement {}}- // The user's own controller mappings: only seeded when there are none yet.+ // The user's own controller mappings: seeded when there are none yet, otherwise only+ // the shipped mappings they lack are appended.let mappings = support.appending(path: "gamecontrollerdb.txt")- if !fm.fileExists(atPath: mappings.path) {+ let shippedMappings = source.appending(path: "config/gamecontrollerdb.txt")+ if fm.fileExists(atPath: mappings.path) {+ // Best effort: a file that cannot be appended to is not worth failing the install.+ do { try ControllerMappings.update(mappings, from: shippedMappings) }+ catch { log("gamecontrollerdb.txt could not be updated: \(error)") }+ } else {try fail("gamecontrollerdb.txt could not be copied.") {- try Self.put(source.appending(path: "config/gamecontrollerdb.txt"), at: mappings, replacing: false)+ try Self.put(shippedMappings, at: mappings, replacing: false)}}resetSetupAssistant()diff --git a/app/Sources/BridgeplayInstallerCore/ControllerMappings.swift b/app/Sources/BridgeplayInstallerCore/ControllerMappings.swiftnew file mode 100644index 0000000..9cb1f70--- /dev/null+++ b/app/Sources/BridgeplayInstallerCore/ControllerMappings.swift@@ -0,0 +1,42 @@+import Foundation++/// The user's gamecontrollerdb.txt is seeded from the shipped one. When they already have a+/// file, only the shipped mappings whose GUID it lacks are appended, so new pads reach+/// existing installs while the user's own lines stay as they are.+public enum ControllerMappings {+ /// The text to append to `existing` for the missing mappings, or nil when there is nothing to add.+ public static func additions(to existing: String, shipped: String) -> String? {+ func guid(_ line: Substring) -> String? {+ let line = line.trimmingCharacters(in: .whitespaces)+ return line.isEmpty || line.hasPrefix("#") ? nil : String(line.prefix { $0 != "," }).lowercased()+ }+ let have = Set(existing.split(whereSeparator: \.isNewline).compactMap(guid))+ let haveComments = Set(existing.split(whereSeparator: \.isNewline).map { $0.trimmingCharacters(in: .whitespaces) })+ var added: [String] = [], comments: [String] = [], seen = have+ for raw in shipped.split(whereSeparator: \.isNewline) {+ let line = raw.trimmingCharacters(in: .whitespaces)+ if line.isEmpty { comments = [] }+ else if line.hasPrefix("#") { comments.append(line) }+ else {+ if let id = guid(raw), seen.insert(id).inserted {+ added += comments.filter { !haveComments.contains($0) } + [line]+ }+ comments = []+ }+ }+ guard !added.isEmpty else { return nil }+ let separator = existing.isEmpty || existing.utf8.last == 0x0A ? "" : "\n"+ return separator + added.joined(separator: "\n") + "\n"+ }++ /// Appends what `shipped` has and `file` lacks, in place so a symlink or the file's mode stays.+ public static func update(_ file: URL, from shipped: URL) throws {+ let existing = String(decoding: try Data(contentsOf: file), as: UTF8.self)+ let shippedText = String(decoding: try Data(contentsOf: shipped), as: UTF8.self)+ guard let more = additions(to: existing, shipped: shippedText) else { return }+ let handle = try FileHandle(forWritingTo: file)+ defer { try? handle.close() }+ try handle.seekToEnd()+ try handle.write(contentsOf: Data(more.utf8))+ }+}diff --git a/app/Tests/BridgeplayAppTests/SteamInstallerTests.swift b/app/Tests/BridgeplayAppTests/SteamInstallerTests.swiftindex 47d271a..fe982cb 100644--- a/app/Tests/BridgeplayAppTests/SteamInstallerTests.swift+++ b/app/Tests/BridgeplayAppTests/SteamInstallerTests.swift@@ -1062,7 +1062,7 @@ struct SteamInstallerTests {#expect(!FileManager.default.fileExists(atPath: fixture.deployedVersion.path))}- @Test("An install seeds the controller mappings and leaves the user's own alone")+ @Test("An install seeds the controller mappings and only appends the missing ones to the user's own")func seedsControllerMappings() throws {let fm = FileManager.defaultlet dir = fm.temporaryDirectory.appending(path: UUID().uuidString)@@ -1079,9 +1079,13 @@ struct SteamInstallerTests {SteamInstaller.seedControllerMappings(from: payload, in: support)#expect(fm.contents(atPath: mappings.path(percentEncoded: false)) == Data("shipped".utf8))- try Data("mine".utf8).write(to: mappings)+ try Data("aaaa,mine,a:b9\n".utf8).write(to: mappings)+ try Data("aaaa,shipped,a:b0\nbbbb,new,a:b1\n".utf8).write(to: payload.appending(path: "gamecontrollerdb.txt"))SteamInstaller.seedControllerMappings(from: payload, in: support)- #expect(fm.contents(atPath: mappings.path(percentEncoded: false)) == Data("mine".utf8))+ let merged = Data("aaaa,mine,a:b9\nbbbb,new,a:b1\n".utf8)+ #expect(fm.contents(atPath: mappings.path(percentEncoded: false)) == merged)+ SteamInstaller.seedControllerMappings(from: payload, in: support)+ #expect(fm.contents(atPath: mappings.path(percentEncoded: false)) == merged)}}diff --git a/app/Tests/BridgeplayInstallerCoreTests/ComponentsPlacementTests.swift b/app/Tests/BridgeplayInstallerCoreTests/ComponentsPlacementTests.swiftindex 236c6cb..b591947 100644--- a/app/Tests/BridgeplayInstallerCoreTests/ComponentsPlacementTests.swift+++ b/app/Tests/BridgeplayInstallerCoreTests/ComponentsPlacementTests.swift@@ -34,7 +34,7 @@ struct ComponentsPlacementTests {"config/gamefixes-default/default.conf": "SHIPPED=1\n","config/gamefixes-default/448510.conf": "BACKEND=dxmt\n","config/gamefixes-default/notes.txt": "not a conf",- "config/gamecontrollerdb.txt": "shipped mappings",+ "config/gamecontrollerdb.txt": "# shipped\naaaa,Pad A,a:b0\nbbbb,Pad B,a:b1\n",]for (path, text) in files {let url = source.appending(path: path)@@ -75,7 +75,7 @@ struct ComponentsPlacementTests {// The user's own default.conf is never written.#expect(!FileManager.default.fileExists(atPath: f.support.appending(path: "gamefixes/default.conf").path))#expect(FileManager.default.fileExists(atPath: f.support.appending(path: ".installer").path))- #expect(text(f.support.appending(path: "gamecontrollerdb.txt")) == "shipped mappings")+ #expect(text(f.support.appending(path: "gamecontrollerdb.txt")) == "# shipped\naaaa,Pad A,a:b0\nbbbb,Pad B,a:b1\n")#expect(calls.steam.isEmpty)#expect(calls.resets == 1)#expect(calls.statuses == [ComponentsPlacement.copyStatus])@@ -122,12 +122,12 @@ struct ComponentsPlacementTests {#expect(lines.all.first?.contains("ditto") == true)}- @Test("An older GPTK is replaced whole, and the user's mappings and edited confs stay")+ @Test("An older GPTK is replaced whole, and the user's mappings and edited confs stay (missing mappings are appended)")func update() throws {let f = try fixture()defer { try? FileManager.default.removeItem(at: f.root) }let fm = FileManager.default- for (path, text) in ["gptk/stale.dylib": "old", "gamecontrollerdb.txt": "my mappings",+ for (path, text) in ["gptk/stale.dylib": "old", "gamecontrollerdb.txt": "aaaa,my pad A,a:b9\n","gamefixes/111.conf": "untouched copy", "gamefixes/222.conf": "edited copy","gamefixes-default/448510.conf": "BACKEND=d3dmetal\n"] {let url = f.support.appending(path: path)@@ -141,7 +141,7 @@ struct ComponentsPlacementTests {#expect(!fm.fileExists(atPath: f.support.appending(path: "gptk/stale.dylib").path))#expect(try fm.contentsOfDirectory(atPath: f.support.path).filter { $0.hasPrefix(".gptk-") }.isEmpty)- #expect(text(f.support.appending(path: "gamecontrollerdb.txt")) == "my mappings")+ #expect(text(f.support.appending(path: "gamecontrollerdb.txt")) == "aaaa,my pad A,a:b9\nbbbb,Pad B,a:b1\n")#expect(!fm.fileExists(atPath: f.support.appending(path: "gamefixes/111.conf").path))#expect(text(f.support.appending(path: "gamefixes/222.conf")) == "edited copy")#expect(text(f.support.appending(path: "gamefixes-default/448510.conf")) == "BACKEND=dxmt\n")diff --git a/app/Tests/BridgeplayInstallerCoreTests/ControllerMappingsTests.swift b/app/Tests/BridgeplayInstallerCoreTests/ControllerMappingsTests.swiftnew file mode 100644index 0000000..be1513e--- /dev/null+++ b/app/Tests/BridgeplayInstallerCoreTests/ControllerMappingsTests.swift@@ -0,0 +1,57 @@+import Foundation+import Testing+@testable import BridgeplayInstallerCore++struct ControllerMappingsTests {+ private let shipped = """+ # header+ aaaa,Pad A,a:b0+ # about B+ # second line+ bbbb,Pad B,a:b1+ # about C+ cccc,Pad C,a:b2++ """++ @Test("Only the GUIDs the file lacks are appended, with their comments, and the user's lines stay")+ func appendsMissing() {+ let mine = "# header\naaaa,My Pad A,a:b9\nbbbb,Pad B,a:b1\nzzzz,Mine,a:b3"+ #expect(ControllerMappings.additions(to: mine, shipped: shipped) == "\n# about C\ncccc,Pad C,a:b2\n")+ }++ @Test("A CRLF-terminated file gets no extra blank line, and a GUID in another case is the same pad")+ func crlfAndCase() {+ #expect(ControllerMappings.additions(to: "AAAA,Mine,a:b9\r\nBBBB,Mine,a:b9\r\n", shipped: shipped) == "# about C\ncccc,Pad C,a:b2\n")+ }++ @Test("Running it again changes nothing")+ func idempotent() throws {+ let mine = "zzzz,Mine,a:b3\n"+ let once = mine + (try #require(ControllerMappings.additions(to: mine, shipped: shipped)))+ #expect(ControllerMappings.additions(to: once, shipped: shipped) == nil)+ #expect(ControllerMappings.additions(to: shipped, shipped: shipped) == nil)+ }++ @Test("A file on disk is appended to in place, a non-UTF-8 one too, and a missing one throws")+ func updatesFile() throws {+ let fm = FileManager.default+ let dir = fm.temporaryDirectory.appending(path: UUID().uuidString)+ try fm.createDirectory(at: dir, withIntermediateDirectories: true)+ defer { try? fm.removeItem(at: dir) }+ let source = dir.appending(path: "shipped.txt"), mine = dir.appending(path: "mine.txt")+ try Data(shipped.utf8).write(to: source)+ try Data("aaaa,Mine,a:b9\n".utf8).write(to: mine)+ try ControllerMappings.update(mine, from: source)+ let text = try String(contentsOf: mine, encoding: .utf8)+ #expect(text.hasPrefix("aaaa,Mine,a:b9\n"))+ #expect(text.contains("bbbb,Pad B,a:b1") && text.contains("cccc,Pad C,a:b2"))+ let after = try Data(contentsOf: mine)+ try ControllerMappings.update(mine, from: source)+ #expect(try Data(contentsOf: mine) == after)+ try Data([0x61, 0x61, 0x61, 0x61, 0x2c, 0xe9, 0x0a]).write(to: mine)+ try ControllerMappings.update(mine, from: source)+ #expect(try Data(contentsOf: mine).count > 7)+ #expect(throws: (any Error).self) { try ControllerMappings.update(dir.appending(path: "missing.txt"), from: source) }+ }+}diff --git a/installer/Install.command b/installer/Install.commandindex 88320d1..cb7712a 100755--- a/installer/Install.command+++ b/installer/Install.command@@ -32,6 +32,23 @@ if [ ! -d "$caches" ] && [ -d "$legacy_caches" ]; thenmigrate_later=1fi+# merge_mappings SHIPPED FILE: appends the SHIPPED mappings whose GUID (the text before the first+# comma) FILE lacks, with the comment lines right above them that FILE does not have yet.+merge_mappings() {+ local add+ add=$(awk -F, '+ { sub(/\r$/, ""); sub(/^[[:space:]]+/, "") }+ FILENAME == ARGV[1] { if (/^#/) have_c[$0] = 1; else if ($0 != "") have[tolower($1)] = 1; next }+ $0 == "" { n = 0; next }+ /^#/ { c[++n] = $0; next }+ { if (!(tolower($1) in have)) { have[tolower($1)] = 1; for (i = 1; i <= n; i++) if (!(c[i] in have_c)) print c[i]; print }+ n = 0 }+ ' "$2" "$1") || return 1+ [ -z "$add" ] && return 0+ [ -n "$(tail -c1 "$2")" ] && printf '\n' >> "$2"+ printf '%s\n' "$add" >> "$2"+}+say() { printf '\n\033[1;32m==>\033[0m %s\n' "$*"; }die() {printf '\n\033[1;31mGagal:\033[0m %s\n' "$*" >&2@@ -157,9 +174,12 @@ done# never written here. On its next start the app trims a copy earlier installers wrote there,# using .installer/default.conf, the record of that copy, which therefore stays as it is.-# The user's own controller mappings: only seeded when there are none yet.+# The user's own controller mappings: seeded when there are none yet, otherwise only the+# shipped mappings whose GUID they lack are appended (their own lines stay).if [ ! -f "$support/gamecontrollerdb.txt" ]; thencp "$here/config/gamecontrollerdb.txt" "$support/" || die "gamecontrollerdb.txt gagal disalin."+else+ merge_mappings "$here/config/gamecontrollerdb.txt" "$support/gamecontrollerdb.txt" || say "gamecontrollerdb.txt tidak bisa diperbarui, dilewati."fi# Show the Setup Assistant again even if an earlier Bridgeplay was dismissed.defaults delete com.bridgeplay.Bridgeplay setupAssistantFinished >/dev/null 2>&1 || truediff --git a/installer/tests/mappingscheck.sh b/installer/tests/mappingscheck.shnew file mode 100755index 0000000..b047484--- /dev/null+++ b/installer/tests/mappingscheck.sh@@ -0,0 +1,59 @@+#!/bin/sh+# mappingscheck: how Install.command seeds and updates the user's gamecontrollerdb.txt.+# The merge_mappings function and the seeding block are cut out of the script and run here.+set -eu+SRC="${1:-$(dirname "$0")/../Install.command}"+work=$(mktemp -d "${TMPDIR:-/tmp}/np-mappingscheck.XXXXXX")+trap 'rm -rf "$work"' EXIT+failed=0+is() { # is <what> <expected> <actual>+ if [ "$2" = "$3" ]; then return; fi+ printf 'FAIL: %s\n--- expected\n%s\n--- got\n%s\n' "$1" "$2" "$3" >&2+ failed=1+}+cut_block() { from="$1" to="$2" awk 'BEGIN { from = ENVIRON["from"]; to = ENVIRON["to"] } $0 ~ from { on = 1 } on { print } on && $0 ~ to { exit }' "$SRC"; }+cut_block '^merge_mappings\(\) \{' '^\}$' >"$work/block.sh"+cut_block "^# The user's own controller mappings" '^fi$' >>"$work/block.sh"+[ "$(grep -c . "$work/block.sh")" -gt 5 ] || { echo "FAIL: no mappings block in $SRC" >&2; exit 1; }++here="$work/here"+support="$work/support"+mkdir -p "$here/config" "$support"+die() { echo "die: $*" >&2; exit 1; }+say() { echo "say: $*" >&2; }+# shellcheck disable=SC1091 # block.sh is cut out of Install.command above+run() { . "$work/block.sh"; }++g1=03000000aaaa0000000000000000a1b1+g2=03000000bbbb0000000000000000a2b2+g3=03000000cccc0000000000000000a3b3+shipped="$here/config/gamecontrollerdb.txt"+mine="$support/gamecontrollerdb.txt"+printf '# header\n%s,Pad A,a:b0\n# about B\n# second line\n%s,Pad B,a:b1\n' "$g1" "$g2" >"$shipped"++run+is "fresh install copies the file" "$(cat "$shipped")" "$(cat "$mine")"++printf '# header\n%s,Pad A (shipped),a:b0\n# about B\n# second line\n%s,Pad B,a:b1\n# about C\n%s,Pad C,a:b2\n' "$g1" "$g2" "$g3" >"$shipped"+run+is "upgrade appends only the missing GUID, with its comment" "$(printf '# header\n%s,Pad A,a:b0\n# about B\n# second line\n%s,Pad B,a:b1\n# about C\n%s,Pad C,a:b2' "$g1" "$g2" "$g3")" "$(cat "$mine")"++before=$(cat "$mine")+run+is "running twice changes nothing" "$before" "$(cat "$mine")"++# the user's own edits, an extra line of theirs and a file without a final newline+printf '%s,My own Pad A,a:b9\n%s,Mine,a:b3' "$g1" "030000000000000000000000000000ff" >"$mine"+run+is "edited lines and own lines stay, only missing GUIDs are added" "$(printf '%s,My own Pad A,a:b9\n%s,Mine,a:b3\n# about B\n# second line\n%s,Pad B,a:b1\n# about C\n%s,Pad C,a:b2' "$g1" "030000000000000000000000000000ff" "$g2" "$g3")" "$(cat "$mine")"+before=$(cat "$mine")+run+is "running twice changes nothing after an edit" "$before" "$(cat "$mine")"++# a GUID in another case is the same pad+printf '%s,Pad A,a:b0\n' "$(printf '%s' "$g1" | tr a-f A-F)" >"$mine"+run+is "GUIDs match in any case" "$(printf '%s,Pad A,a:b0' "$(printf '%s' "$g1" | tr a-f A-F)")" "$(grep -i "^$g1" "$mine" | tr -d '\n')"++[ "$failed" = 0 ] || exit 1+echo "mappingscheck: ok"