Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.
Swift apple-silicond3dmetaldxmtdxvkfexgame-porting-toolkitgamingmac-gamingmacosmetalmoltenvksteamswiftuivkd3d-protonwhisky-alternativewindows-gameswinewine-wrapper
Steam accounts: "is signed in" only while Steam runs, the warning follows the player's choice, ActiveUser read only when it matters
Gabe3e6bcd730844
diff --git a/Sources/HighballApp/GameDetailView.swift b/Sources/HighballApp/GameDetailView.swiftindex cd1d692..aab3653 100644--- a/Sources/HighballApp/GameDetailView.swift+++ b/Sources/HighballApp/GameDetailView.swift@@ -7,7 +7,8 @@ struct GameDetailView: View {let passedItem: LibraryItem@Environment(\.accessibilityReduceMotion) private var reduceMotion@State private var storeDetails: SteamGameDetails?- @State private var accountNote: SteamAccounts.GameNote?+ /// The registry's ActiveUser for the game's environment, as last read (accountNote).+ @State private var activeUserRead: ActiveUserRead?@State private var loadingDetails = true@State private var expandedDescription = false@State private var selectedGalleryURL: URL?@@ -55,6 +56,38 @@ struct GameDetailView: View {private var storeAppID: Int? { item.steamAppID }+ private struct ActiveUserRead: Equatable {+ let bottle: String+ let user: UInt64?+ }++ /// What the ActiveUser task depends on: the Steam game's environment, whether its Steam runs,+ /// and whether more than one account uses it.+ private struct AccountWatch: Equatable {+ let bottle: String?+ let running: Bool+ let severalAccounts: Bool+ }++ private var accountWatch: AccountWatch {+ let name = item.source == .steam && item.steamAppID != nil ? item.bottleName : nil+ return AccountWatch(bottle: name, running: name.map { state.steamClients.contains($0) } ?? false,+ severalAccounts: name.map { (state.steamAccountsByBottle[$0]?.count ?? 0) > 1 } ?? false)+ }++ /// Who installed this Steam game, and whether Steam is signed in, or signed in last, to an+ /// account that may not have it: "is signed in" only while that Steam runs, and an account the+ /// player said has the game is never warned about.+ private var accountNote: SteamAccounts.GameNote? {+ guard item.source == .steam, let appID = item.steamAppID, let name = item.bottleName,+ let read = activeUserRead, read.bottle == name else { return nil }+ let owners = state.steamOwners(for: item)+ return SteamAccounts.note(installedBy: state.gamesByBottle[name]?.first { $0.appid == appID }?.lastOwner,+ accounts: state.steamAccountsByBottle[name] ?? [], activeUser: read.user,+ steamRunning: state.steamClients.contains(name),+ chosen: owners?.reason == .chosen ? owners?.accounts : nil)+ }+var body: some View {GeometryReader { geometry inScrollViewReader { scroll in@@ -98,18 +131,19 @@ struct GameDetailView: View {storeDetails = detailsloadingDetails = false}- // Who installed the game, and whether Steam is signed in to another account that may not- // have it. Only the signed-in account is read again, every few seconds: switching accounts- // happens in Steam, and the install records and accounts are already in the app's state.- .task(id: item.id) {- accountNote = nil- guard item.source == .steam, let appID = item.steamAppID,- let bottle = state.bottles.first(where: { $0.name == item.bottleName }) else { return }+ // Which account Steam is signed in to, for accountNote. Only that is read here: the install+ // records and accounts are already in the app's state. Read with more than one account+ // only, again every few seconds while Steam runs (switching accounts happens in Steam),+ // and once more when it starts or quits.+ .task(id: accountWatch) {+ let watch = accountWatch+ guard let name = watch.bottle, watch.severalAccounts,+ let bottle = state.bottles.first(where: { $0.name == name }) else { return }while !Task.isCancelled {- let signedIn = await Task.detached { SteamAccounts.signedIn(bottleURL: bottle.url) }.value- let note = SteamAccounts.note(installedBy: state.gamesByBottle[bottle.name]?.first { $0.appid == appID }?.lastOwner,- accounts: state.steamAccountsByBottle[bottle.name] ?? [], signedIn: signedIn)- if note != accountNote { accountNote = note }+ let user = await Task.detached { SteamAccounts.signedIn(bottleURL: bottle.url) }.value+ let read = ActiveUserRead(bottle: name, user: user)+ if read != activeUserRead { activeUserRead = read }+ guard watch.running else { return }try? await Task.sleep(for: .seconds(5))}}@@ -215,7 +249,7 @@ struct GameDetailView: View {VStack(alignment: .leading, spacing: 6) {row(L("Installed by"), note.installedBy.displayName)if let other = note.otherAccount {- Label(String(format: note.steamRunning+ Label(String(format: note.signedInNow? L("Steam is signed in as %@. That account may not have access to this game."): L("Steam last signed in as %@. That account may not have access to this game."),other.displayName),diff --git a/Sources/HighballKit/SteamAccounts.swift b/Sources/HighballKit/SteamAccounts.swiftindex a4aa0dc..e2174be 100644--- a/Sources/HighballKit/SteamAccounts.swift+++ b/Sources/HighballKit/SteamAccounts.swift@@ -43,8 +43,10 @@ public enum SteamAccounts {return accounts.sorted { ($0.lastSignIn ?? .distantPast) > ($1.lastSignIn ?? .distantPast) }}- /// The account Steam is signed in to while it runs: the registry's ActiveUser (an account- /// ID, 0 once Steam has quit), in the environment's user.reg.+ /// The registry's ActiveUser, in the environment's user.reg: the account Steam is signed in+ /// to while it runs. Steam can leave it set after quitting, so with Steam closed it is the+ /// account signed in last; only a running Steam (AppState.steamClients) makes it the one+ /// signed in now. nil when it is 0 (no account) or missing.public static func signedIn(bottleURL: URL) -> UInt64? {guard let text = try? String(contentsOf: bottleURL.appending(path: "user.reg"), encoding: .utf8) else { return nil }return activeUser(inRegistry: text)@@ -60,17 +62,27 @@ public enum SteamAccounts {/// one account on this Steam, or no record of who installed the game.public struct GameNote: Sendable, Equatable {public let installedBy: SteamAccount- /// The account Steam is signed in to, or signed in to last when it is not running, when- /// that is another one.+ /// The account Steam is signed in to, or signed in to last when it is not, when that one+ /// may not have the game.public let otherAccount: SteamAccount?- /// Whether Steam runs now; when not, `otherAccount` is the one it signed in to last.- public let steamRunning: Bool- }-- public static func note(installedBy owner: UInt64?, accounts: [SteamAccount], signedIn: UInt64?) -> GameNote? {+ /// Whether Steam runs now, signed in to `otherAccount`; when not, that is the account it+ /// signed in to last.+ public let signedInNow: Bool+ }++ /// `activeUser` is the registry's (signedIn(bottleURL:)), `steamRunning` whether this Steam+ /// runs now: ActiveUser can outlive Steam, so only then is it who is signed in. Without it,+ /// the account signed in last is loginusers.vdf's newest. The accounts that have the game are+ /// the player's choice when there is one (`chosen`, Every account included), else the+ /// installer; the warning names the current account only when it is not among them.+ public static func note(installedBy owner: UInt64?, accounts: [SteamAccount], activeUser: UInt64?,+ steamRunning: Bool, chosen: Set<UInt64>? = nil) -> GameNote? {guard accounts.count > 1, let owner, let installer = accounts.first(where: { $0.id == owner }) else { return nil }- let current = signedIn.flatMap { id in accounts.first { $0.id == id } } ?? (signedIn == nil ? accounts.first : nil)- return GameNote(installedBy: installer, otherAccount: current.flatMap { $0.id == owner ? nil : $0 }, steamRunning: signedIn != nil)+ let active = activeUser.flatMap { id in accounts.first { $0.id == id } }+ let current = active ?? (activeUser == nil ? accounts.first : nil)+ let haveIt: Set<UInt64> = chosen.flatMap { $0.isEmpty ? nil : $0 } ?? [owner]+ return GameNote(installedBy: installer, otherAccount: current.flatMap { haveIt.contains($0.id) ? nil : $0 },+ signedInNow: steamRunning && active != nil)}}diff --git a/Tests/HighballKitTests/SteamAccountsTests.swift b/Tests/HighballKitTests/SteamAccountsTests.swiftindex 5be7b78..474f303 100644--- a/Tests/HighballKitTests/SteamAccountsTests.swift+++ b/Tests/HighballKitTests/SteamAccountsTests.swift@@ -44,10 +44,10 @@ final class SteamAccountsTests: XCTestCase {XCTAssertEqual(SteamAccounts.parse(loginUsers: text).map(\.displayName), [#"a {"b"} c"#])}- func testSignedInAccountComesFromTheRegistryWhileSteamRuns() {+ func testActiveUserComesFromTheRegistry() {XCTAssertEqual(SteamAccounts.activeUser(inRegistry: "[Software\\\\Valve\\\\Steam\\\\ActiveProcess]\n\"ActiveUser\"=dword:023c3462\n"),76561197960265728 + 0x023c3462)- XCTAssertNil(SteamAccounts.activeUser(inRegistry: "\"ActiveUser\"=dword:00000000\n"), "0 once Steam has quit")+ XCTAssertNil(SteamAccounts.activeUser(inRegistry: "\"ActiveUser\"=dword:00000000\n"), "0: no account")XCTAssertNil(SteamAccounts.activeUser(inRegistry: ""))}@@ -72,20 +72,47 @@ final class SteamAccountsTests: XCTestCase {let accounts = SteamAccounts.parse(loginUsers: loginUsers)let alder: UInt64 = 76561198000000001, birch: UInt64 = 76561198000000002// Another account signed in now.- let other = SteamAccounts.note(installedBy: alder, accounts: accounts, signedIn: birch)+ let other = SteamAccounts.note(installedBy: alder, accounts: accounts, activeUser: birch, steamRunning: true)XCTAssertEqual(other?.installedBy.displayName, "Alder")XCTAssertEqual(other?.otherAccount?.displayName, "Birch")- XCTAssertEqual(other?.steamRunning, true)+ XCTAssertEqual(other?.signedInNow, true)// The installer is signed in: the row, no warning.- XCTAssertNil(SteamAccounts.note(installedBy: alder, accounts: accounts, signedIn: alder)?.otherAccount)- // Steam not running: the account it signed in to last.- let closed = SteamAccounts.note(installedBy: alder, accounts: accounts, signedIn: nil)+ XCTAssertNil(SteamAccounts.note(installedBy: alder, accounts: accounts, activeUser: alder, steamRunning: true)?.otherAccount)+ // Steam not running, ActiveUser 0: the account it signed in to last, loginusers.vdf's newest.+ let closed = SteamAccounts.note(installedBy: alder, accounts: accounts, activeUser: nil, steamRunning: false)XCTAssertEqual(closed?.otherAccount?.displayName, "Birch")- XCTAssertEqual(closed?.steamRunning, false)+ XCTAssertEqual(closed?.signedInNow, false)// Nothing to say: one account on this Steam, or no record of who installed it.- XCTAssertNil(SteamAccounts.note(installedBy: alder, accounts: [accounts[1]], signedIn: nil))- XCTAssertNil(SteamAccounts.note(installedBy: nil, accounts: accounts, signedIn: birch))- XCTAssertNil(SteamAccounts.note(installedBy: 76561198000000009, accounts: accounts, signedIn: birch))+ XCTAssertNil(SteamAccounts.note(installedBy: alder, accounts: [accounts[1]], activeUser: nil, steamRunning: false))+ XCTAssertNil(SteamAccounts.note(installedBy: nil, accounts: accounts, activeUser: birch, steamRunning: true))+ XCTAssertNil(SteamAccounts.note(installedBy: 76561198000000009, accounts: accounts, activeUser: birch, steamRunning: true))+ }++ /// ActiveUser can stay set after Steam quits: with Steam closed it is who signed in last,+ /// never "is signed in".+ func testActiveUserLeftBehindIsTheLastSignInOnly() {+ let accounts = SteamAccounts.parse(loginUsers: loginUsers)+ let alder: UInt64 = 76561198000000001, birch: UInt64 = 76561198000000002+ let left = SteamAccounts.note(installedBy: alder, accounts: accounts, activeUser: birch, steamRunning: false)+ XCTAssertEqual(left?.otherAccount?.displayName, "Birch")+ XCTAssertEqual(left?.signedInNow, false)+ // Running at the sign-in screen, nobody active yet: still only the last sign-in.+ XCTAssertEqual(SteamAccounts.note(installedBy: alder, accounts: accounts, activeUser: nil, steamRunning: true)?.signedInNow, false)+ }++ /// The player's own answer to whose a game is decides whom the warning is about.+ func testGameNoteGoesByThePlayersChoice() {+ let accounts = SteamAccounts.parse(loginUsers: loginUsers)+ let alder: UInt64 = 76561198000000001, birch: UInt64 = 76561198000000002+ XCTAssertNil(SteamAccounts.note(installedBy: alder, accounts: accounts, activeUser: birch, steamRunning: true,+ chosen: [birch])?.otherAccount, "the player says the signed-in account has it")+ XCTAssertNil(SteamAccounts.note(installedBy: alder, accounts: accounts, activeUser: birch, steamRunning: true,+ chosen: [alder, birch])?.otherAccount, "Every account here")+ XCTAssertEqual(SteamAccounts.note(installedBy: alder, accounts: accounts, activeUser: alder, steamRunning: true,+ chosen: [birch])?.otherAccount?.displayName, "Alder",+ "the player says only the other account has it")+ XCTAssertEqual(SteamAccounts.note(installedBy: alder, accounts: accounts, activeUser: birch, steamRunning: true,+ chosen: [])?.otherAccount?.displayName, "Birch", "an empty choice is no choice")}/// The player's choice wins; otherwise every account with a sign, the strongest named.