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(download): show real byte counts while the engine downloads (#1436)
Raymond Chin81286e324aa0
diff --git a/app/Sources/BridgeplayApp/Support/PinnedDownload.swift b/app/Sources/BridgeplayApp/Support/PinnedDownload.swiftindex 1232602..0c98a19 100644--- a/app/Sources/BridgeplayApp/Support/PinnedDownload.swift+++ b/app/Sources/BridgeplayApp/Support/PinnedDownload.swift@@ -229,8 +229,10 @@ final class ProgressRelay: NSObject, URLSessionTaskDelegate, @unchecked Sendable}func urlSession(_ session: URLSession, didCreateTask task: URLSessionTask) {- let observation = task.progress.observe(\.completedUnitCount) { [weak self] progress, _ in- self?.update(received: progress.completedUnitCount, total: progress.totalUnitCount)+ // The task's own byte counts: on macOS 27 task.progress counts 100 units, not bytes, and+ // sits at 5 until the body is done, so the bar read "5 bytes / 100 bytes" for the whole engine.+ let observation = task.observe(\.countOfBytesReceived) { [weak self] task, _ in+ self?.update(received: task.countOfBytesReceived, total: task.countOfBytesExpectedToReceive)}lock.withLock { self.observation = observation; self.task = task }Task { @MainActor indiff --git a/app/Tests/BridgeplayAppTests/PinnedDownloadTests.swift b/app/Tests/BridgeplayAppTests/PinnedDownloadTests.swiftindex b9943a6..e45dabc 100644--- a/app/Tests/BridgeplayAppTests/PinnedDownloadTests.swift+++ b/app/Tests/BridgeplayAppTests/PinnedDownloadTests.swift@@ -141,6 +141,70 @@ struct PinnedDownloadTests {override func stopLoading() { timer?.cancel() }}+ // 64 KB with a Content-Length, sent in eight pieces, so progress has something between 0 and done.+ private final class Chunked: URLProtocol, @unchecked Sendable {+ static let body = Data(repeating: 0x61, count: 64 * 1024)+ private var timer: DispatchSourceTimer?++ override class func canInit(with request: URLRequest) -> Bool { request.url?.host() == "chunked.invalid" }+ override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }++ override func startLoading() {+ let response = HTTPURLResponse(+ url: request.url!, statusCode: 200, httpVersion: nil,+ headerFields: ["Content-Length": "\(Self.body.count)"])!+ client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)+ var sent = 0+ let timer = DispatchSource.makeTimerSource()+ timer.schedule(deadline: .now(), repeating: .milliseconds(20))+ timer.setEventHandler { [weak self] in+ guard let self else { return }+ let piece = Self.body.count / 8+ self.client?.urlProtocol(self, didLoad: Self.body.subdata(in: sent..<sent + piece))+ sent += piece+ if sent >= Self.body.count {+ self.timer?.cancel()+ self.client?.urlProtocolDidFinishLoading(self)+ }+ }+ timer.resume()+ self.timer = timer+ }++ override func stopLoading() { timer?.cancel() }+ }++ // macOS 27's task.progress counts 100 units and stays at 5 until the end; the bar showed+ // "5 bytes / 100 bytes" for the whole engine download.+ @Test("Progress counts the bytes received, and moves before the download ends")+ func progressCountsBytes() async throws {+ let work = try scratchDirectory("pinned")+ defer { try? FileManager.default.removeItem(at: work) }++ final class Counts: @unchecked Sendable {+ private let lock = NSLock()+ private var seen: [(received: Int64, total: Int64)] = []+ func add(_ p: PinnedDownload.Progress) {+ guard case .receiving(_, let received, let total, _, _) = p else { return }+ lock.withLock { seen.append((received, total)) }+ }+ var all: [(received: Int64, total: Int64)] { lock.withLock { seen } }+ }+ let counts = Counts()+ let size = Int64(Chunked.body.count)++ _ = try await PinnedDownload.obtain(+ file: "package.tar", sha256: Digest.sha256(of: Chunked.body),+ bases: [URL(string: "https://chunked.invalid/")!],+ into: work.appending(path: "downloads"), step: "test",+ session: PinnedDownload.session(deadline: 30, protocols: [Chunked.self]),+ report: { counts.add($0) })++ #expect(!counts.all.isEmpty)+ #expect(counts.all.allSatisfy { $0.total == size })+ #expect(counts.all.contains { $0.received > 0 && $0.received < size })+ }+@Test("A host that never finishes is given up on for the next one")func stalledHostIsPassedOver() async throws {let work = try scratchDirectory("pinned")