Skip to content

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

feat: live engine-download progress and a stall timeout (Reddit report)

Gauthier Piarrette3c06eb3e56fb
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swift
index 4b7fd29..10f300e 100644
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@ -103,8 +103,16 @@ final class AppState {
let manifest = try EngineManifest.load(from: manifestURL)
var accepted: Set<String> = []
if acceptGPTK { accepted.insert("apple-gptk-license-2023-08-17") }
- _ = try await engineStore.install(manifest, accepted: accepted) { name, _, _ in
- Task { @MainActor in self.appendLog("downloaded \(name)") }
+ _ = try await engineStore.install(manifest, accepted: accepted) { name, received, total in
+ Task { @MainActor in
+ let mb = { (b: Int64) in String(format: "%.0f", Double(b) / 1_048_576) }
+ if let total, total > 0 {
+ self.stage = "Downloading \(name) — \(mb(received)) / \(mb(total)) MB"
+ if received >= total { self.appendLog("downloaded \(name) — verifying and unpacking…") }
+ } else {
+ self.stage = "Downloading \(name) — \(mb(received)) MB"
+ }
+ }
}
await MainActor.run { self.appendLog("engine installed") }
}
diff --git a/Sources/HighballKit/EngineStore.swift b/Sources/HighballKit/EngineStore.swift
index 164ace9..80f88a3 100644
--- a/Sources/HighballKit/EngineStore.swift
+++ b/Sources/HighballKit/EngineStore.swift
@@ -99,18 +99,28 @@ public struct EngineStore: Sendable {
if FileManager.default.fileExists(atPath: dest.path), try sha256(of: dest) == component.sha256.lowercased() {
return dest
}
- let (tmp, response) = try await URLSession.shared.download(from: component.url, delegate: nil)
- if let http = response as? HTTPURLResponse, !(200..<300).contains(http.statusCode) {
- throw HighballError.invalid("HTTP \(http.statusCode) for \(component.url)")
+ // Delegate-based download: real progress every ~2 MB (the app was showing a blank sheet
+ // for the whole 270 MB), and a 60 s no-data timeout so a stalled connection fails loudly
+ // instead of hanging forever (URLSession.shared's resource timeout is 7 days).
+ let expected = Int64(component.size ?? 0)
+ let delegate = ProgressingDownload(dest: dest) { received, total in
+ progress?(name, received, total > 0 ? total : expected)
+ }
+ let cfg = URLSessionConfiguration.ephemeral
+ cfg.timeoutIntervalForRequest = 60
+ cfg.timeoutIntervalForResource = 3600
+ let session = URLSession(configuration: cfg, delegate: delegate, delegateQueue: nil)
+ defer { session.finishTasksAndInvalidate() }
+ try await withCheckedThrowingContinuation { (cont: CheckedContinuation<Void, Error>) in
+ delegate.continuation = cont
+ session.downloadTask(with: component.url).resume()
}
- try? FileManager.default.removeItem(at: dest)
- try FileManager.default.moveItem(at: tmp, to: dest)
let actual = try sha256(of: dest)
guard actual == component.sha256.lowercased() else {
try? FileManager.default.removeItem(at: dest)
throw HighballError.checksumMismatch(file: dest.lastPathComponent, expected: component.sha256, actual: actual)
}
- progress?(name, Int64(component.size ?? 0), Int64(component.size ?? 0))
+ progress?(name, expected > 0 ? expected : 1, expected > 0 ? expected : 1)
return dest
}
@@ -234,3 +244,46 @@ public struct InstalledEngine: Sendable {
return env
}
}
+
+/// URLSessionDownloadDelegate that streams byte progress (~every 2 MB) and moves the finished
+/// file into place before resuming its continuation. State is confined to the session's own
+/// serial delegate queue, hence @unchecked Sendable.
+final class ProgressingDownload: NSObject, URLSessionDownloadDelegate, @unchecked Sendable {
+ private let dest: URL
+ private let onProgress: @Sendable (Int64, Int64) -> Void
+ private var lastReported: Int64 = 0
+ private var moveError: Error?
+ var continuation: CheckedContinuation<Void, Error>?
+
+ init(dest: URL, onProgress: @escaping @Sendable (Int64, Int64) -> Void) {
+ self.dest = dest
+ self.onProgress = onProgress
+ }
+
+ func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
+ didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
+ if totalBytesWritten - lastReported >= 2_000_000 || totalBytesWritten == totalBytesExpectedToWrite {
+ lastReported = totalBytesWritten
+ onProgress(totalBytesWritten, totalBytesExpectedToWrite)
+ }
+ }
+
+ func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
+ if let http = downloadTask.response as? HTTPURLResponse, !(200..<300).contains(http.statusCode) {
+ moveError = HighballError.invalid("HTTP \(http.statusCode) for \(downloadTask.originalRequest?.url?.absoluteString ?? "download")")
+ return
+ }
+ do {
+ try? FileManager.default.removeItem(at: dest)
+ try FileManager.default.moveItem(at: location, to: dest)
+ } catch { moveError = error }
+ }
+
+ func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
+ let cont = continuation
+ continuation = nil
+ if let error { cont?.resume(throwing: error) }
+ else if let moveError { cont?.resume(throwing: moveError) }
+ else { cont?.resume() }
+ }
+}
diff --git a/Sources/highball/main.swift b/Sources/highball/main.swift
index 2a2506c..2764f15 100644
--- a/Sources/highball/main.swift
+++ b/Sources/highball/main.swift
@@ -55,7 +55,11 @@ struct Engine: AsyncParsableCommand {
var accepted: Set<String> = []
if acceptD3DMetal { accepted.insert("apple-gptk-license-2023-08-17") }
let engine = try await EngineStore().install(m, accepted: accepted) { name, got, total in
- print(" \(name): \(got)/\(total ?? 0) bytes")
+ let t = total ?? 0
+ let mb = { (b: Int64) in String(format: "%.0f", Double(b) / 1_048_576) }
+ // \r keeps one live-updating line per component; newline when it completes.
+ print("\r \(name): \(mb(got))\(t > 0 ? " / \(mb(t))" : "") MB", terminator: t > 0 && got >= t ? "\n" : "")
+ fflush(stdout)
}
print("installed \(engine.id) at \(engine.root.path)")
print("wine: \(try engine.wineVersion())")