Skip to content

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

An option to open GOG and Epic Mac games through Highball

Gabecedf95f84bdc
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swift
index d2104c1..02d04ea 100644
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@ -1164,6 +1164,7 @@ final class AppState {
readBattleNetInstalls()
startSteamClientWatch()
startDiscordWatch()
+ startMacLaunchWatch()
libraryPlays = libraryStore.load()
libraryOverrides = libraryStore.rendererOverrides()
libraryLaunchArgs = libraryStore.launchArguments()
@@ -3517,6 +3518,7 @@ final class AppState {
}
let store = epicStore
runBusy(String(format: L("Starting %@"), item.title), done: .handedOff) { [self] in
+ macLaunchedAt[item.id] = Date()
appendLog("\(item.title): opening the Mac version")
let info = try await Task.detached { try store.launchInfo(app) }.value
try Task.checkCancellation()
@@ -3536,6 +3538,74 @@ final class AppState {
}
}
+ // MARK: Mac games opened outside Highball
+
+ static let relaunchDefaultsKey = "openMacGamesThroughHighball"
+ /// Off unless turned on in Settings: closing an app the player opened is a surprise otherwise.
+ static var relaunchEnabled: Bool { UserDefaults.standard.bool(forKey: relaunchDefaultsKey) }
+ @ObservationIgnored private var macLaunchObserver: NSObjectProtocol?
+ @ObservationIgnored private var relaunching: Set<String> = []
+ /// When Highball last started each Mac game, and last closed one to start it again: a launch
+ /// near either is Highball's own (a game whose app hands over to another process) and is left
+ /// alone, so the watcher can never loop.
+ @ObservationIgnored private var macLaunchedAt: [String: Date] = [:]
+ @ObservationIgnored private var macRelaunchedAt: [String: Date] = [:]
+
+ /// A GOG or Epic Mac game opened from Applications, the Dock or Spotlight starts without
+ /// what Highball brings (comet, which a game looks for only at startup, the save sync and its
+ /// backups, Epic's sign-in). With the setting on, such a launch is closed within its first
+ /// seconds and started again through Highball. Never one Highball started, and never one
+ /// running for longer than a few seconds.
+ private func startMacLaunchWatch() {
+ guard macLaunchObserver == nil else { return }
+ macLaunchObserver = NSWorkspace.shared.notificationCenter.addObserver(
+ forName: NSWorkspace.didLaunchApplicationNotification, object: nil, queue: .main) { [weak self] note in
+ guard let app = note.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication else { return }
+ MainActor.assumeIsolated { self?.macAppLaunched(app) }
+ }
+ }
+
+ private func macAppLaunched(_ app: NSRunningApplication) {
+ 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 + "/")
+ }), !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).
+ DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
+ guard let self else { return }
+ defer { self.relaunching.remove(item.id) }
+ guard self.relaunchShouldTakeOver(app, item: item) else { return }
+ self.macRelaunchedAt[item.id] = Date()
+ self.appendLog("\(item.title) was opened outside Highball; starting it again through Highball")
+ app.terminate()
+ DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { [weak self] in
+ if !app.isTerminated { app.forceTerminate() }
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { self?.playOnMac(item) }
+ }
+ }
+ }
+
+ /// 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.
+ 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 }
+ 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
+ }
+ }
+
/// Removes an Epic game's Mac build through legendary, so its library stays right.
func epicUninstallMac(_ item: LibraryItem) {
guard let app = item.epicAppName else { return }
@@ -3912,6 +3982,7 @@ final class AppState {
throw HighballError.failed(String(format: L("The Mac version of %@ did not start: %@"), item.title, error.localizedDescription))
}
macGameProcesses[item.id] = process
+ macLaunchedAt[item.id] = Date()
recordPlay(item)
appendLog("\(item.title): the Mac version is running\(comet != nil ? ", with comet for achievements" : "")")
watchMacGame(process, item: item, game: game, comet: comet, cometLog: cometLog, saveFolders: saveFolders)
diff --git a/Sources/HighballApp/L10n.swift b/Sources/HighballApp/L10n.swift
index e6e81d9..c9248ac 100644
--- a/Sources/HighballApp/L10n.swift
+++ b/Sources/HighballApp/L10n.swift
@@ -437,6 +437,9 @@ enum L10n {
"Switch %@ to the Windows version?": "Passer %@ à la version Windows ?",
"Epic keeps one version of a game installed at a time, so the Mac version is removed first, then the Windows version downloads into your environment.":
"Epic n’installe qu’une version d’un jeu à la fois : la version Mac est d’abord supprimée, puis la version Windows est téléchargée dans votre environnement.",
+ "A Mac game opened from Applications, the Dock or Spotlight is closed within its first seconds and started again through Highball, which brings GOG's achievements and cloud saves and Epic's sign-in. Only while Highball is open, and never while it is busy with another task.":
+ "Un jeu Mac ouvert depuis Applications, le Dock ou Spotlight est fermé dans ses premières secondes et relancé avec Highball, qui apporte les succès et sauvegardes en ligne de GOG et la connexion d’Epic. Seulement quand Highball est ouvert, et jamais pendant une autre tâche.",
+ "Open GOG and Epic Mac games through Highball": "Ouvrir les jeux Mac GOG et Epic avec Highball",
"Highball could not find the Mac version of %@ to open.": "Highball ne trouve pas la version Mac de %@ à ouvrir.",
"The Mac version of %@ did not start: %@": "La version Mac de %@ n’a pas démarré : %@",
"Removing the Windows version of %@": "Suppression de la version Windows de %@",
diff --git a/Sources/HighballApp/SettingsView.swift b/Sources/HighballApp/SettingsView.swift
index 3bf9c7f..a47014e 100644
--- a/Sources/HighballApp/SettingsView.swift
+++ b/Sources/HighballApp/SettingsView.swift
@@ -240,6 +240,7 @@ struct EnginePane: View {
/// The updater delegate reads the same key: AppStorage so the switch redraws when flipped.
@AppStorage(UpdateChannels.betaDefaultsKey) private var betaUpdates = false
@AppStorage(AchievementToast.enabledDefaultsKey) private var achievementPopups = true
+ @AppStorage(AppState.relaunchDefaultsKey) private var openMacGamesThroughHighball = false
@State private var showLicense = false
@State private var pendingRemove: String?
@State private var engineSizes: [String: String] = [:]
@@ -293,6 +294,13 @@ struct EnginePane: View {
.font(.caption).foregroundStyle(.secondary)
}.frame(maxWidth: .infinity, alignment: .leading)
}
+ GroupBox {
+ VStack(alignment: .leading, spacing: 6) {
+ Toggle(L("Open GOG and Epic Mac games through Highball"), isOn: $openMacGamesThroughHighball)
+ Text(L("A Mac game opened from Applications, the Dock or Spotlight is closed within its first seconds and started again through Highball, which brings GOG's achievements and cloud saves and Epic's sign-in. Only while Highball is open, and never while it is busy with another task."))
+ .font(.caption).foregroundStyle(.secondary)
+ }.frame(maxWidth: .infinity, alignment: .leading)
+ }
// Only where it means something: a Mac with Steam for Mac, or one where it was turned off.
if state.steamForMacInstalled || !state.includeMacSteam {
GroupBox {