Skip to content

Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.

When Highball takes over a Mac game opened outside it is decided in HighballKit, and tested

Gabe3ab373cf4207
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swift
index 29abec5..6648b42 100644
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@ -3593,7 +3593,7 @@ final class AppState {
guard Self.relaunchEnabled, let bundle = app.bundleURL?.standardizedFileURL.path else { return }
guard let item = libraryItems.first(where: { item in
guard (item.source == .gog || item.source == .epic), let folder = macFolder(for: item)?.standardizedFileURL.path else { return false }
- return bundle == folder || bundle.hasPrefix(folder + "/")
+ return MacRelaunch.opens(bundle: bundle, from: folder)
}), !relaunching.contains(item.id) else { return }
relaunching.insert(item.id)
// A moment for Highball's own launch to be recorded (Epic's is, once legendary answers).
@@ -3611,23 +3611,20 @@ final class AppState {
}
}
- /// Whether to close a game just opened outside Highball: only when it is not Highball's own
- /// launch (within 15 seconds of Highball starting it, or while the game Highball started has
- /// not ended: an app from its folder then is its own, a launcher's hand-over or a helper,
- /// however late it opens), opened in the last few seconds, not closed already in the last
- /// minute, and only when Highball can start it again right now. A busy Highball (a download
- /// holding the strip) would close it and start nothing.
+ /// Whether to close a game just opened outside Highball (MacRelaunch.shouldTakeOver, which
+ /// the tests cover): what Highball knows of the game and the app, and whether it can start
+ /// the game again right now.
private func relaunchShouldTakeOver(_ app: NSRunningApplication, item: LibraryItem) -> Bool {
- let now = Date()
- if macGameProcesses[item.id] != nil { return false }
- if let started = macLaunchedAt[item.id], now.timeIntervalSince(started) < 15 { return false }
- if let closed = macRelaunchedAt[item.id], now.timeIntervalSince(closed) < 60 { return false }
- guard !app.isTerminated, let launched = app.launchDate, now.timeIntervalSince(launched) < 8, !busy else { return false }
+ let canStart: Bool
switch item.source {
- case .gog: return item.gogID.flatMap { gogMacInstalled[$0]?.app } != nil
- case .epic: return item.epicAppName.flatMap { epicMacInstalls[$0] } != nil
- default: return false
- }
+ case .gog: canStart = item.gogID.flatMap { gogMacInstalled[$0]?.app } != nil
+ case .epic: canStart = item.epicAppName.flatMap { epicMacInstalls[$0] } != nil
+ default: canStart = false
+ }
+ return MacRelaunch.shouldTakeOver(now: Date(), gameRunning: macGameProcesses[item.id] != nil,
+ startedAt: macLaunchedAt[item.id], takenOverAt: macRelaunchedAt[item.id],
+ appLaunchedAt: app.launchDate, appTerminated: app.isTerminated,
+ busy: busy, canStart: canStart)
}
/// Removes an Epic game's Mac build through legendary, so its library stays right.
diff --git a/Sources/HighballKit/MacRelaunch.swift b/Sources/HighballKit/MacRelaunch.swift
new file mode 100644
index 0000000..572e47b
--- /dev/null
+++ b/Sources/HighballKit/MacRelaunch.swift
@@ -0,0 +1,34 @@
+import Foundation
+
+/// When Highball closes a GOG or Epic Mac game opened outside it (Applications, the Dock,
+/// Spotlight) and starts it again through Highball (AppState.macAppLaunched). Pure, so the tests
+/// reach the choice the app makes.
+public enum MacRelaunch {
+ /// A launch this soon after Highball started the game is Highball's own.
+ public static let ownLaunchWindow: TimeInterval = 15
+ /// A game closed to start it again is not closed again this soon, so the watch never loops.
+ public static let takenOverWindow: TimeInterval = 60
+ /// Only an app opened this recently is closed: one running longer is being played.
+ public static let freshLaunchWindow: TimeInterval = 8
+
+ /// Whether to close a game just opened outside Highball: never while the game Highball
+ /// started has not ended (an app from its folder then is its own, a launcher's hand-over or
+ /// a helper, however late it opens), nor within `ownLaunchWindow` of Highball starting it, nor
+ /// within `takenOverWindow` of closing it already. Only an app that is still running and
+ /// opened within `freshLaunchWindow`, and only when Highball can start the game again right
+ /// now: a busy Highball (a download holding the strip) would close it and start nothing.
+ public static func shouldTakeOver(now: Date, gameRunning: Bool, startedAt: Date?, takenOverAt: Date?,
+ appLaunchedAt: Date?, appTerminated: Bool, busy: Bool, canStart: Bool) -> Bool {
+ if gameRunning { return false }
+ if let startedAt, now.timeIntervalSince(startedAt) < ownLaunchWindow { return false }
+ if let takenOverAt, now.timeIntervalSince(takenOverAt) < takenOverWindow { return false }
+ guard !appTerminated, let appLaunchedAt, now.timeIntervalSince(appLaunchedAt) < freshLaunchWindow, !busy else { return false }
+ return canStart
+ }
+
+ /// Whether an app at `bundle` is from a game's `folder`: the folder itself (a GOG build's
+ /// .app) or anything inside it, never a sibling whose name only starts the same.
+ public static func opens(bundle: String, from folder: String) -> Bool {
+ bundle == folder || bundle.hasPrefix(folder + "/")
+ }
+}
diff --git a/Tests/HighballKitTests/MacRelaunchTests.swift b/Tests/HighballKitTests/MacRelaunchTests.swift
new file mode 100644
index 0000000..659a524
--- /dev/null
+++ b/Tests/HighballKitTests/MacRelaunchTests.swift
@@ -0,0 +1,60 @@
+import XCTest
+@testable import HighballKit
+
+/// Closing a Mac game opened outside Highball to start it again through Highball.
+final class MacRelaunchTests: XCTestCase {
+ private let now = Date(timeIntervalSinceReferenceDate: 1_000_000)
+
+ private func takesOver(gameRunning: Bool = false, startedAgo: TimeInterval? = nil, takenOverAgo: TimeInterval? = nil,
+ appLaunchedAgo: TimeInterval? = 2, appTerminated: Bool = false, busy: Bool = false,
+ canStart: Bool = true) -> Bool {
+ MacRelaunch.shouldTakeOver(now: now, gameRunning: gameRunning,
+ startedAt: startedAgo.map { now.addingTimeInterval(-$0) },
+ takenOverAt: takenOverAgo.map { now.addingTimeInterval(-$0) },
+ appLaunchedAt: appLaunchedAgo.map { now.addingTimeInterval(-$0) },
+ appTerminated: appTerminated, busy: busy, canStart: canStart)
+ }
+
+ func testAFreshOutsideLaunchHighballCanStartIsTakenOver() {
+ XCTAssertTrue(takesOver())
+ XCTAssertTrue(takesOver(startedAgo: 20, takenOverAgo: 90))
+ }
+
+ /// A launcher's hand-over opens the game long after Highball started it: still its own.
+ func testNothingIsClosedWhileTheGameHighballStartedRuns() {
+ XCTAssertFalse(takesOver(gameRunning: true))
+ XCTAssertFalse(takesOver(gameRunning: true, startedAgo: 600))
+ }
+
+ func testALaunchSoonAfterHighballStartedTheGameIsHighballsOwn() {
+ XCTAssertFalse(takesOver(startedAgo: 3))
+ XCTAssertFalse(takesOver(startedAgo: 14.9))
+ XCTAssertTrue(takesOver(startedAgo: 15))
+ }
+
+ func testAGameClosedOnceIsNotClosedAgainWithinAMinute() {
+ XCTAssertFalse(takesOver(takenOverAgo: 5))
+ XCTAssertFalse(takesOver(takenOverAgo: 59))
+ XCTAssertTrue(takesOver(takenOverAgo: 60))
+ }
+
+ func testOnlyAnAppStillRunningAndJustOpenedIsClosed() {
+ XCTAssertFalse(takesOver(appTerminated: true))
+ XCTAssertFalse(takesOver(appLaunchedAgo: nil))
+ XCTAssertFalse(takesOver(appLaunchedAgo: 8))
+ XCTAssertTrue(takesOver(appLaunchedAgo: 7.9))
+ }
+
+ /// Closing the game is only worth it when Highball starts it again at once.
+ func testABusyHighballOrOneThatCannotStartTheGameLeavesItAlone() {
+ XCTAssertFalse(takesOver(busy: true))
+ XCTAssertFalse(takesOver(canStart: false))
+ }
+
+ func testAnAppIsFromAGamesFolderOnlyWhenItIsTheFolderOrInsideIt() {
+ XCTAssertTrue(MacRelaunch.opens(bundle: "/Applications/Foo.app", from: "/Applications/Foo.app"))
+ XCTAssertTrue(MacRelaunch.opens(bundle: "/Games/Garlic/Garlic.app", from: "/Games/Garlic"))
+ XCTAssertFalse(MacRelaunch.opens(bundle: "/Applications/Foo Deluxe.app", from: "/Applications/Foo"))
+ XCTAssertFalse(MacRelaunch.opens(bundle: "/Games/Garlic2/Garlic.app", from: "/Games/Garlic"))
+ }
+}