Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

2.4 KB
Raw Blame
import Foundation

enum RemoveEverythingCommand {
    static let flag = "--remove-everything"

    static func run() -> Never {
        Task.detached { exit(AppLog.finished(await perform())) }
        dispatchMain()
    }

    static func perform(remove: @Sendable (@escaping @Sendable (UninstallPhase) -> Void) async throws -> UninstallOutcome = {
        try await Uninstall.run(leftovers: .live, report: $0)
    }) async -> Int32 {
        do {
            let output = Output()
            let result = try await remove { phase in
                if case .restoring(.finished) = phase { return }
                output.say(phase.label)
            }
            output.say(
                result.partialRemoval ?? (result.steamNeedsRedownload
                    ? "Bridgeplay has been removed. Steam could not be restored. "
                        + "Reinstall Steam from steampowered.com before using it again."
                    : "Bridgeplay has been removed.")
            )
            if let kept = result.keptGamefixes {
                output.say("Your own game fixes were kept in \(kept.path(percentEncoded: false)).")
            }
            return result.partialRemoval == nil ? 0 : 1
        } catch {
            AppLog.note("command line failed: \(failureMessage(error))")
            FileHandle.standardError.write(Data("\(failureMessage(error))\n".utf8))
            return 1
        }
    }

    static func failureMessage(_ error: Error) -> String {
        guard let refused = error as? WriteRefused else { return error.localizedDescription }
        // The app's advice, except where the command line asks for something else.
        let advice = switch refused.remedy {
        case .appManagement:
            "Allow your terminal app in System Settings > Privacy & Security > App Management, then run it again."
        case .otherAccount:
            "Steam was installed by another account on this Mac. Run this again while logged in to that account."
        case .rootOwned, .ownership:
            refused.remedy.advice
        }
        return "Could not write \(refused.path). \(advice)"
    }

    private final class Output: @unchecked Sendable {
        private let lock = NSLock()
        private var last: String?

        func say(_ line: String) {
            lock.withLock {
                guard line != last else { return }
                last = line
                FileHandle.standardOutput.write(Data("\(line)\n".utf8))
            }
        }
    }
}