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
play: a program with only Direct3D 12 gets a mode that has it before the launch (#139, highball-db#105)
Gauthier Piarrettee19b0ce0e394
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swiftindex 0ce3ba1..44d02d1 100644--- a/Sources/HighballApp/AppState.swift+++ b/Sources/HighballApp/AppState.swift@@ -245,9 +245,20 @@ final class AppState {if let engine = engine(for: bottle) {let entry = gameDB.entry(for: item)let pinMode = item.pinID.flatMap { id in bottle.settings.pins.first { $0.id == id }?.renderer }- let wanted = Renderer.choose(requested: renderer, gameOverride: rendererOverride(for: item), row: entry?.effectiveRenderer(),+ var wanted = Renderer.choose(requested: renderer, gameOverride: rendererOverride(for: item), row: entry?.effectiveRenderer(),environmentExplicit: bottle.settings.rendererExplicit, pin: pinMode,environment: bottle.settings.renderer, nativeVulkan: entry?.nativeVulkan == true)+ // A program with only Direct3D 12 cannot run on DXMT, DXVK or Wine's Direct3D, so a+ // mode that has it takes over before the launch, and the log says so (Farming+ // Simulator 22 stopped at "Shader model 6.0 is required" on DXMT, highball#139).+ if !wanted.servesDirect3D12, entry?.nativeVulkan != true, programNeedsDirect3D12Only(item) {+ let instead = Renderer.forDirect3D12Only(chosen: wanted, engine: engine)+ if instead != wanted {+ appendLog("\(item.title) only has Direct3D 12 and \(GamePageCopy.plainName(wanted)) has none, playing with \(GamePageCopy.plainName(instead)).")+ wanted = instead+ renderer = instead+ }+ }switch wanted.availability(in: engine) {case .available:break@@ -1058,21 +1069,36 @@ final class AppState {} catch { fail(error) }}- /// The game's own icon as a PNG under covers/icons, read from its executable (the largest- /// .exe in its folder, or a pin's own), or nil when there is none to read.- func programIcon(for item: LibraryItem) -> URL? {+ /// The program an item runs: the largest .exe in a Steam or Epic game's folder, a pin's own.+ func programExecutable(for item: LibraryItem) -> URL? {guard let bottleName = item.bottleName, let bottle = bottles.first(where: { $0.name == bottleName }) else { return nil }- let exe: URL?switch item.source {case .steam:guard let game = gamesByBottle[bottleName]?.first(where: { $0.appid == item.steamAppID }), !game.installdir.isEmpty else { return nil }- exe = PEIcon.bestExecutable(in: bottle.driveC.appending(path: "Program Files (x86)/Steam/steamapps/common/\(game.installdir)"))+ return PEIcon.bestExecutable(in: bottle.driveC.appending(path: "Program Files (x86)/Steam/steamapps/common/\(game.installdir)"))case .epic:- exe = item.epicAppName.flatMap { epicInstalls[$0] }.flatMap { PEIcon.bestExecutable(in: URL(fileURLWithPath: $0)) }+ return item.epicAppName.flatMap { epicInstalls[$0] }.flatMap { PEIcon.bestExecutable(in: URL(fileURLWithPath: $0)) }case .pin:- exe = item.pinID.flatMap { id in bottle.settings.pins.first { $0.id == id } }.map { $0.executableURL(driveC: bottle.driveC) }+ return item.pinID.flatMap { id in bottle.settings.pins.first { $0.id == id } }.map { $0.executableURL(driveC: bottle.driveC) }}- guard let exe, let png = PEIcon.png(from: exe) else { return nil }+ }++ /// Whether the item's program links Direct3D 12 and nothing else to draw with, remembered+ /// per executable and modification date: finding the executable walks the game's folder.+ private var direct3D12OnlyCache: [String: (exe: URL, modified: Date, verdict: Bool)] = [:]+ func programNeedsDirect3D12Only(_ item: LibraryItem) -> Bool {+ guard let exe = programExecutable(for: item) else { return false }+ let modified = (try? exe.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate) ?? .distantPast+ if let hit = direct3D12OnlyCache[item.id], hit.exe == exe, hit.modified == modified { return hit.verdict }+ let verdict = ProgramNeeds.direct3D12Only(program: exe)+ direct3D12OnlyCache[item.id] = (exe, modified, verdict)+ return verdict+ }++ /// The game's own icon as a PNG under covers/icons, read from its executable (the largest+ /// .exe in its folder, or a pin's own), or nil when there is none to read.+ func programIcon(for item: LibraryItem) -> URL? {+ guard let exe = programExecutable(for: item), let png = PEIcon.png(from: exe) else { return nil }let dir = paths.home.appending(path: "covers/icons", directoryHint: .isDirectory)try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)let file = dir.appending(path: MacAppStub.bundleID(for: item.id) + ".png")diff --git a/Sources/HighballKit/Bottle.swift b/Sources/HighballKit/Bottle.swiftindex d8a2e25..9708f6b 100644--- a/Sources/HighballKit/Bottle.swift+++ b/Sources/HighballKit/Bottle.swift@@ -95,6 +95,22 @@ public enum Renderer: String, Codable, CaseIterable, Sendable {return [path, d9vk.appending(path: "wine").path].joined(separator: ":")}+ /// Whether this mode has a Direct3D 12 a game can use. Wine's own d3d12 (vkd3d over+ /// MoltenVK) creates no device on a Mac, so only the two overlays count.+ public var servesDirect3D12: Bool { self == .d3dmetal || self == .vkd3d }++ /// The mode for a program that only has Direct3D 12 (`ProgramNeeds.direct3D12Only`) when+ /// `chosen` cannot serve it: D3DMetal when the engine ships it, licence accepted or still to+ /// ask for (the caller's licence flow handles that), else vkd3d, else `chosen` unchanged so+ /// the launch fails the way it did rather than in a new way. A program with a Direct3D 11+ /// path never comes here; the environment's choice stands for it.+ public static func forDirect3D12Only(chosen: Renderer, engine: InstalledEngine) -> Renderer {+ if chosen.servesDirect3D12 { return chosen }+ if Renderer.d3dmetal.availability(in: engine) != .notShipped { return .d3dmetal }+ if Renderer.vkd3d.availability(in: engine) == .available { return .vkd3d }+ return chosen+ }+/// The backend to offer after `current` failed on launch, cycling through the Metal-backed/// options. Direct3D 9 no longer constrains this: `withD9VK` attaches DXVK's d3d9 to every/// renderer, so switching backend can't drop D3D9 support the way it could before 0.7.17.diff --git a/Sources/HighballKit/PEImports.swift b/Sources/HighballKit/PEImports.swiftnew file mode 100644index 0000000..f652001--- /dev/null+++ b/Sources/HighballKit/PEImports.swift@@ -0,0 +1,96 @@+import Foundation++/// The DLLs a Windows program links against, read from its import table and its delay-load+/// table. Delay-loaded ones count as much as the others: Path of Exile 2 binds Direct3D 12+/// that way (highball-db#53), and a program's need for an API is the same whichever table+/// names it. Anything the parser does not understand is an empty list, never a guess.+public enum PEImports {+ public static func dllNames(of file: URL) -> [String] {+ guard let d = try? Data(contentsOf: file, options: .mappedIfSafe) else { return [] }+ return dllNames(fromPE: d)+ }++ static func dllNames(fromPE d: Data) -> [String] {+ guard d.count > 0x40, d[0] == 0x4D, d[1] == 0x5A else { return [] }+ let pe = Int(u32(d, 0x3C))+ guard pe + 24 < d.count, u32(d, pe) == 0x4550 else { return [] }+ let sections = Int(u16(d, pe + 6)), optSize = Int(u16(d, pe + 20)), opt = pe + 24+ guard opt + 2 <= d.count else { return [] }+ let plus = u16(d, opt) == 0x20B // PE32+ (64-bit) or PE32+ let imageBase = plus ? Int(u64(d, opt + 24)) : Int(u32(d, opt + 28))+ let ddCount = Int(u32(d, opt + (plus ? 108 : 92)))+ let dd = opt + (plus ? 112 : 96) // data directories: 8 bytes each+ var secs: [(va: Int, size: Int, raw: Int)] = []+ let table = opt + optSize+ for i in 0..<sections {+ let s = table + 40 * i+ guard s + 40 <= d.count else { return [] }+ // An old linker leaves VirtualSize 0 and only SizeOfRawData set; take the larger.+ secs.append((Int(u32(d, s + 12)), max(Int(u32(d, s + 8)), Int(u32(d, s + 16))), Int(u32(d, s + 20))))+ }+ func off(_ rva: Int) -> Int? {+ for s in secs where rva >= s.va && rva < s.va + max(s.size, 1) { return rva - s.va + s.raw }+ return nil+ }+ func name(atRVA rva: Int) -> String? {+ guard rva > 0, let o = off(rva), o < d.count else { return nil }+ var end = o+ while end < d.count, d[end] != 0, end - o < 256 { end += 1 }+ return String(data: d[o..<end], encoding: .ascii)+ }+ var names: [String] = []+ // Import directory (entry 1): 20-byte descriptors, the DLL name's RVA at +12, ended by+ // an all-zero descriptor.+ if ddCount > 1, u32(d, dd + 8 + 4) > 0, var e = off(Int(u32(d, dd + 8))) {+ while e + 20 <= d.count, names.count < 1024, u32(d, e + 12) != 0 || u32(d, e + 16) != 0 {+ if let n = name(atRVA: Int(u32(d, e + 12))) { names.append(n) }+ e += 20+ }+ }+ // Delay-load directory (entry 13): 32-byte descriptors, the name at +4, ended by a zero+ // name. Attribute bit 0 set means RVAs; clear (the original Visual C++ 6 form) means+ // virtual addresses, which the image base turns back into RVAs.+ if ddCount > 13, u32(d, dd + 8 * 13 + 4) > 0, var e = off(Int(u32(d, dd + 8 * 13))) {+ while e + 32 <= d.count, names.count < 2048, u32(d, e + 4) != 0 {+ let raw = Int(u32(d, e + 4))+ let rva = u32(d, e) & 1 == 1 ? raw : raw - imageBase+ if let n = name(atRVA: rva) { names.append(n) }+ e += 32+ }+ }+ return names+ }++ private static func u16(_ d: Data, _ o: Int) -> UInt16 { o + 2 <= d.count ? UInt16(d[o]) | UInt16(d[o + 1]) << 8 : 0 }+ private static func u32(_ d: Data, _ o: Int) -> UInt32 {+ o + 4 <= d.count ? UInt32(d[o]) | UInt32(d[o + 1]) << 8 | UInt32(d[o + 2]) << 16 | UInt32(d[o + 3]) << 24 : 0+ }+ private static func u64(_ d: Data, _ o: Int) -> UInt64 { UInt64(u32(d, o)) | UInt64(u32(d, o + 4)) << 32 }+}++/// What a program's imports say it needs from the graphics stack.+public enum ProgramNeeds {+ static let direct3D12 = "d3d12.dll"+ /// Any of these means the program has another way to draw, so the environment's mode may+ /// well be the right one and choosing for it would override a working setup.+ static let otherGraphics: Set<String> = ["d3d11.dll", "d3d10.dll", "d3d10_1.dll", "d3d10core.dll", "d3d9.dll", "d3d8.dll", "ddraw.dll", "opengl32.dll", "vulkan-1.dll"]++ /// True when the program, or a DLL of its own that it links from its folder, imports+ /// Direct3D 12 and no other graphics API. Such a program cannot run on a mode without+ /// Direct3D 12 (DXMT, DXVK, Wine's own Direct3D), whatever the environment says: Farming+ /// Simulator 22 on a DXMT environment stops at "Shader model 6.0 is required" (highball#139)+ /// and an Unreal launcher on the same setup at its sign-in (highball-db#105). One level of+ /// the program's own DLLs is enough: Unity keeps its graphics in UnityPlayer.dll, other+ /// engines in a renderer DLL beside the executable; Windows' own DLLs are not in the folder.+ public static func direct3D12Only(program exe: URL) -> Bool {+ var names = Set(PEImports.dllNames(of: exe).map { $0.lowercased() })+ guard !names.isEmpty else { return false }+ let dir = exe.deletingLastPathComponent()+ for n in names.sorted() where n.hasSuffix(".dll") {+ let beside = dir.appending(path: n)+ guard FileManager.default.fileExists(atPath: beside.path) else { continue }+ names.formUnion(PEImports.dllNames(of: beside).map { $0.lowercased() })+ }+ return names.contains(direct3D12) && names.isDisjoint(with: otherGraphics)+ }+}diff --git a/Tests/HighballKitTests/PEImportsTests.swift b/Tests/HighballKitTests/PEImportsTests.swiftnew file mode 100644index 0000000..ff88d4c--- /dev/null+++ b/Tests/HighballKitTests/PEImportsTests.swift@@ -0,0 +1,103 @@+import XCTest+@testable import HighballKit++/// A program's imports say which graphics API it needs, and a Direct3D 12-only one must not be+/// launched on a mode without Direct3D 12 (highball#139, highball-db#105).+final class PEImportsTests: XCTestCase {+ /// A minimal 64-bit PE with one section holding an import table naming `imports` and a+ /// delay-load table naming `delayImports`, laid out the way the parser reads them.+ static func makePE(imports: [String], delayImports: [String] = [], oldStyleDelay: Bool = false) -> Data {+ let imageBase = 0x400000, sectionVA = 0x1000, sectionRaw = 0x200 // a base that fits the old-style delay descriptors' 32-bit addresses+ var section = Data()+ func put32(_ v: UInt32) { for i in 0..<4 { section.append(UInt8((v >> (8 * i)) & 0xFF)) } }+ // Names first, so their RVAs are known when the descriptors are written.+ var nameRVA: [String: UInt32] = [:]+ for n in imports + delayImports where nameRVA[n] == nil {+ nameRVA[n] = UInt32(sectionVA + section.count)+ section.append(contentsOf: Array(n.utf8) + [0])+ }+ while section.count % 4 != 0 { section.append(0) }+ let importOff = section.count+ for n in imports { put32(0); put32(0); put32(0); put32(nameRVA[n]!); put32(UInt32(sectionVA)) } // FirstThunk non-zero+ put32(0); put32(0); put32(0); put32(0); put32(0)+ let delayOff = section.count+ for n in delayImports {+ put32(oldStyleDelay ? 0 : 1)+ put32(oldStyleDelay ? UInt32(imageBase + Int(nameRVA[n]!)) : nameRVA[n]!)+ for _ in 0..<6 { put32(0) }+ }+ for _ in 0..<8 { put32(0) }++ var d = Data(count: sectionRaw)+ func w16(_ v: UInt16, _ o: Int) { d[o] = UInt8(v & 0xFF); d[o + 1] = UInt8(v >> 8) }+ func w32(_ v: UInt32, _ o: Int) { for i in 0..<4 { d[o + i] = UInt8((v >> (8 * i)) & 0xFF) } }+ d[0] = 0x4D; d[1] = 0x5A; w32(0x40, 0x3C)+ let pe = 0x40+ w32(0x4550, pe); w16(0x8664, pe + 4); w16(1, pe + 6); w16(240, pe + 20)+ let opt = pe + 24+ w16(0x20B, opt)+ w32(UInt32(imageBase & 0xFFFFFFFF), opt + 24); w32(UInt32(imageBase >> 32), opt + 28)+ w32(16, opt + 108)+ let dd = opt + 112+ w32(UInt32(sectionVA + importOff), dd + 8); w32(UInt32(section.count - importOff), dd + 12)+ if !delayImports.isEmpty { w32(UInt32(sectionVA + delayOff), dd + 8 * 13); w32(UInt32(section.count - delayOff), dd + 8 * 13 + 4) }+ let table = opt + 240+ d.replaceSubrange(table..<table + 8, with: Array(".idata\0\0".utf8))+ w32(UInt32(section.count), table + 8); w32(UInt32(sectionVA), table + 12)+ w32(UInt32(section.count), table + 16); w32(UInt32(sectionRaw), table + 20)+ d.append(section)+ return d+ }++ func testReadsImportAndDelayLoadTables() {+ let pe = Self.makePE(imports: ["KERNEL32.dll", "d3d11.dll"], delayImports: ["d3d12.dll", "dxgi.dll"])+ XCTAssertEqual(PEImports.dllNames(fromPE: pe), ["KERNEL32.dll", "d3d11.dll", "d3d12.dll", "dxgi.dll"])+ XCTAssertEqual(PEImports.dllNames(fromPE: Self.makePE(imports: ["USER32.dll"])), ["USER32.dll"])+ }++ func testOldStyleDelayLoadUsesVirtualAddresses() {+ let pe = Self.makePE(imports: ["KERNEL32.dll"], delayImports: ["d3d12.dll"], oldStyleDelay: true)+ XCTAssertEqual(PEImports.dllNames(fromPE: pe), ["KERNEL32.dll", "d3d12.dll"])+ }++ func testRejectsWhatItDoesNotUnderstand() {+ XCTAssertEqual(PEImports.dllNames(fromPE: Data("not a program".utf8)), [])+ XCTAssertEqual(PEImports.dllNames(fromPE: Data(count: 0x100)), [])+ XCTAssertEqual(PEImports.dllNames(fromPE: Self.makePE(imports: []).prefix(0x150)), [], "a truncated section table is not a list")+ }++ func testDirect3D12OnlyLooksAtTheProgramAndItsOwnDLLs() throws {+ let dir = FileManager.default.temporaryDirectory.appending(path: "hb-needs-\(UUID().uuidString)")+ defer { try? FileManager.default.removeItem(at: dir) }+ try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)+ let exe = dir.appending(path: "Game.exe")+ // The executable links only its engine DLL and Direct3D 12 (delay-loaded, as Path of Exile 2 does).+ try Self.makePE(imports: ["KERNEL32.dll", "Engine.dll"], delayImports: ["d3d12.dll"]).write(to: exe)+ XCTAssertTrue(ProgramNeeds.direct3D12Only(program: exe))+ // Its engine DLL beside it has a Direct3D 11 path too: the environment's choice stands.+ try Self.makePE(imports: ["d3d11.dll", "dxgi.dll"]).write(to: dir.appending(path: "Engine.dll"))+ XCTAssertFalse(ProgramNeeds.direct3D12Only(program: exe))+ // A Unity build: the executable names only the player, the player draws with Direct3D 11 or OpenGL.+ try Self.makePE(imports: ["UnityPlayer.dll", "KERNEL32.dll"]).write(to: exe)+ try Self.makePE(imports: ["d3d11.dll", "OPENGL32.dll"]).write(to: dir.appending(path: "UnityPlayer.dll"))+ XCTAssertFalse(ProgramNeeds.direct3D12Only(program: exe))+ // No graphics import at all (a launcher, a .NET stub): nothing to say.+ try Self.makePE(imports: ["KERNEL32.dll"]).write(to: exe)+ XCTAssertFalse(ProgramNeeds.direct3D12Only(program: exe))+ XCTAssertFalse(ProgramNeeds.direct3D12Only(program: dir.appending(path: "missing.exe")))+ }++ func testRealBinariesOnThisMac() throws {+ // GameOverlayRenderer64.dll delay-loads KERNEL32; UnityPlayer.dll (PEAK) links Direct3D 11 and OpenGL.+ let steam = URL(fileURLWithPath: NSHomeDirectory()).appending(path: "Library/Application Support/Highball/bottles/Gaming/drive_c/Program Files (x86)/Steam")+ let overlay = steam.appending(path: "GameOverlayRenderer64.dll")+ try XCTSkipUnless(FileManager.default.fileExists(atPath: overlay.path), "no Gaming bottle with Steam here")+ let names = PEImports.dllNames(of: overlay).map { $0.lowercased() }+ XCTAssertTrue(names.contains("kernel32.dll"), "\(names)")+ let peak = steam.appending(path: "steamapps/common/PEAK/PEAK.exe")+ if FileManager.default.fileExists(atPath: peak.path) {+ XCTAssertFalse(ProgramNeeds.direct3D12Only(program: peak))+ XCTAssertTrue(PEImports.dllNames(of: peak).contains("UnityPlayer.dll"))+ }+ }+}diff --git a/Tests/HighballKitTests/RendererAvailabilityTests.swift b/Tests/HighballKitTests/RendererAvailabilityTests.swiftindex b7a3913..387c858 100644--- a/Tests/HighballKitTests/RendererAvailabilityTests.swift+++ b/Tests/HighballKitTests/RendererAvailabilityTests.swift@@ -46,6 +46,23 @@ final class RendererAvailabilityTests: XCTestCase {return b}+ // MARK: Direct3D 12-only programs (highball#139)++ func testADirect3D12OnlyProgramGetsAModeThatHasIt() throws {+ let full = try engine(accepted: true)+ XCTAssertEqual(Renderer.forDirect3D12Only(chosen: .dxmt, engine: full), .d3dmetal)+ XCTAssertEqual(Renderer.forDirect3D12Only(chosen: .dxvk, engine: full), .d3dmetal)+ XCTAssertEqual(Renderer.forDirect3D12Only(chosen: .wined3d, engine: full), .d3dmetal)+ XCTAssertEqual(Renderer.forDirect3D12Only(chosen: .d3dmetal, engine: full), .d3dmetal, "already serves it")+ XCTAssertEqual(Renderer.forDirect3D12Only(chosen: .vkd3d, engine: full), .vkd3d, "already serves it")+ // The licence still to accept is the caller's ask, not a reason to pick something else.+ XCTAssertEqual(Renderer.forDirect3D12Only(chosen: .dxmt, engine: try engine(accepted: false)), .d3dmetal)+ // No D3DMetal at all: vkd3d when the engine carries it, else the choice stands and the+ // launch fails the way it did.+ XCTAssertEqual(Renderer.forDirect3D12Only(chosen: .dxmt, engine: try engine(ships: ["dxmt", "dxvk", "d9vk", "vkd3d"], accepted: true)), .vkd3d)+ XCTAssertEqual(Renderer.forDirect3D12Only(chosen: .dxmt, engine: try engine(ships: ["dxmt", "dxvk", "d9vk"], accepted: true)), .dxmt)+ }+// MARK: Availabilityfunc testAvailabilityTellsLicenceFromAbsence() throws {