Skip to content

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

6.9 KB
Raw Blame
import XCTest
@testable import HighballKit

/// A minimal PE32+ with one section holding an export directory whose name is `exportName`.
/// Enough of a DLL for the parts of Highball that read PE headers.
enum SyntheticPE {
    static func make(exportName: String, extraNameRoom: Int = 0) -> Data {
        var d = Data(count: 0x600)
        func put16(_ v: UInt16, _ o: Int) { d[o] = UInt8(v & 0xff); d[o + 1] = UInt8(v >> 8) }
        func put32(_ v: UInt32, _ o: Int) { for i in 0..<4 { d[o + i] = UInt8((v >> (8 * i)) & 0xff) } }
        d[0] = 0x4D; d[1] = 0x5A                     // MZ
        put32(0x40, 0x3c)                            // e_lfanew
        d[0x40] = 0x50; d[0x41] = 0x45               // PE\0\0
        put16(0x8664, 0x44)                          // machine x64
        put16(1, 0x46)                               // one section
        put16(240, 0x54)                             // optional header size (PE32+)
        let opt = 0x58
        put16(0x20b, opt)                            // PE32+ magic
        put32(0x1000, opt + 112)                     // data directory 0: export table RVA
        put32(0x100, opt + 116)                      //   size
        let sec = opt + 240
        d.replaceSubrange(sec..<sec + 5, with: Array(".edata".utf8.prefix(5)))
        put32(0x200, sec + 8)                        // virtual size
        put32(0x1000, sec + 12)                      // virtual address
        put32(0x200, sec + 16)                       // raw size
        put32(0x400, sec + 20)                       // raw pointer
        let dir = 0x400
        put32(0x1000 + 40, dir + 12)                 // export directory Name RVA: right after the 40-byte directory
        let name = Array(exportName.utf8)
        d.replaceSubrange(dir + 40..<dir + 40 + name.count, with: name)
        // extraNameRoom: the zero padding after the name is not "room" unless the original name was longer
        _ = extraNameRoom
        return d
    }
}

final class PEExportNameTests: XCTestCase {
    func testReadsTheExportName() {
        XCTAssertEqual(PEExportName.read(SyntheticPE.make(exportName: "d3d12.dll")), "d3d12.dll")
        XCTAssertNil(PEExportName.read(Data("not a pe".utf8)))
        XCTAssertNil(PEExportName.read(Data()))
    }

    func testPatchesInPlaceWhenTheNewNameFits() throws {
        let original = SyntheticPE.make(exportName: "d3d12.dll")
        let patched = try PEExportName.patched(original, to: "apd12.dll")
        XCTAssertEqual(PEExportName.read(patched), "apd12.dll")
        XCTAssertEqual(patched.count, original.count, "nothing moves")
        let diff = zip(original, patched).enumerated().filter { $0.element.0 != $0.element.1 }.map(\.offset)
        XCTAssertEqual(diff.count, 2, "only the bytes of the name that differ change (d3d12 -> apd12): \(diff)")
        let shorter = try PEExportName.patched(original, to: "ab.dll")
        XCTAssertEqual(PEExportName.read(shorter), "ab.dll", "a shorter name is zero padded")
    }

    func testRefusesALongerNameAndNonPEs() {
        XCTAssertThrowsError(try PEExportName.patched(SyntheticPE.make(exportName: "d3d12.dll"), to: "d3d12_d3dmetal.dll")) {
            XCTAssertEqual($0 as? PEExportName.Failure, .nameTooLong(max: 9))
        }
        XCTAssertThrowsError(try PEExportName.patched(Data("MZ nope".utf8), to: "x.dll")) {
            XCTAssertEqual($0 as? PEExportName.Failure, .notPE)
        }
    }

    /// The real thing, when this Mac has a licensed D3DMetal: the file Highball lays out must still
    /// be a PE Wine will take, so the patch is checked on Apple's own bytes.
    func testPatchesTheRealD3DMetalWhenPresent() throws {
        let home = FileManager.default.homeDirectoryForCurrentUser
        let engines = home.appending(path: "Library/Application Support/Highball/engines")
        guard let ids = try? FileManager.default.contentsOfDirectory(atPath: engines.path) else { throw XCTSkip("no engines here") }
        let real = ids.map { engines.appending(path: "\($0)/frameworks/renderer/d3dmetal/wine/x86_64-windows/d3d12.dll") }
            .first { FileManager.default.fileExists(atPath: $0.path) }
        guard let real else { throw XCTSkip("no D3DMetal on this Mac") }
        let d = try Data(contentsOf: real)
        XCTAssertEqual(PEExportName.read(d), "d3d12.dll")
        XCTAssertEqual(PEExportName.read(try PEExportName.patched(d, to: "apd12.dll")), "apd12.dll")
    }
}

/// The copy of D3DMetal's d3d12.dll laid out behind the timestamp shim follows the D3DMetal files by
/// content. D3DMetal 4.0 beta 2 and beta 3 ship d3d12.dll at the same size, and an unpacked file keeps
/// its build date, so the earlier size and date check kept beta 2's copy in front of beta 3 when the
/// D3DMetal files were replaced inside an engine (2026-10-15).
final class TimestampShimLayoutTests: XCTestCase {
    func testTheCopyFollowsD3DMetalWhenItChangesAtTheSameSize() throws {
        let fm = FileManager.default
        let root = fm.temporaryDirectory.appending(path: "hb-shim-\(UUID().uuidString)")
        defer { try? fm.removeItem(at: root) }
        let d3dmetal = root.appending(path: "renderers/d3dmetal")
        let realPE = d3dmetal.appending(path: "wine/x86_64-windows/d3d12.dll")
        try fm.createDirectory(at: realPE.deletingLastPathComponent(), withIntermediateDirectories: true)
        try fm.createDirectory(at: d3dmetal.appending(path: "wine/x86_64-unix"), withIntermediateDirectories: true)
        try Data().write(to: d3dmetal.appending(path: "wine/x86_64-unix/d3d12.so"))
        let shimPE = root.appending(path: "renderers/d3dmetal-tsshim/wine/x86_64-windows/d3d12.dll")
        try fm.createDirectory(at: shimPE.deletingLastPathComponent(), withIntermediateDirectories: true)
        try Data("shim".utf8).write(to: shimPE)
        var m = EngineManifest(id: "x64-test-r1", displayName: "test", arch: "x86_64", minMacOS: "14.0", components: [:])
        m.acceptedLicenses = [EngineManifest.gatedRenderers["d3dmetal"]!]
        let engine = InstalledEngine(manifest: m, root: root)

        var beta2 = SyntheticPE.make(exportName: "d3d12.dll"); beta2[0x500] = 2
        var beta3 = SyntheticPE.make(exportName: "d3d12.dll"); beta3[0x500] = 3
        XCTAssertEqual(beta2.count, beta3.count)
        try beta2.write(to: realPE)
        let shim = try XCTUnwrap(engine.timestampShimDir(d3dmetal: d3dmetal))
        let copy = shim.appending(path: "wine/x86_64-windows/\(InstalledEngine.shimRealName).dll")
        XCTAssertEqual(try Data(contentsOf: copy), try PEExportName.patched(beta2, to: "apd12.dll"))

        try beta3.write(to: realPE)
        try fm.setAttributes([.modificationDate: Date(timeIntervalSinceNow: -86400 * 30)], ofItemAtPath: realPE.path)
        _ = try XCTUnwrap(engine.timestampShimDir(d3dmetal: d3dmetal))
        XCTAssertEqual(try Data(contentsOf: copy), try PEExportName.patched(beta3, to: "apd12.dll"),
                       "same size and an older date, still replaced")
        XCTAssertEqual(PEExportName.read(try Data(contentsOf: copy)), "apd12.dll")
    }
}