Skip to content

Commit

Permalink
add discardable results to some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vapidinfinity committed Feb 26, 2024
1 parent 4d1a571 commit 71ff69f
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Mythic/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
}
}
}

}

func applicationWillTerminate(_ notification: Notification) {
if defaults.bool(forKey: "quitOnAppClose") { _ = Wine.killAll() }
if defaults.bool(forKey: "quitOnAppClose") { Wine.killAll() }
}
}
15 changes: 9 additions & 6 deletions Mythic/Controllers/Legendary/LegendaryInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Legendary {

- Returns: A tuple containing stdout and stderr data.
*/
@discardableResult
static func command(
args: [String],
useCache: Bool,
Expand All @@ -97,7 +98,7 @@ class Legendary {
if useCache, let cachedOutput = queue.cache.sync(execute: { commandCache[commandKey] }), !cachedOutput.stdout.isEmpty && !cachedOutput.stderr.isEmpty {
log.debug("Cached, returning.")
Task(priority: .background) {
_ = await run()
await run()
log.debug("New cache appended.")
}
log.debug("Cache returned.")
Expand All @@ -109,6 +110,7 @@ class Legendary {

// MARK: - Run Method
@Sendable
@discardableResult
func run() async -> (stdout: Data, stderr: Data) {
let task = Process()
task.executableURL = URL(filePath: Bundle.main.path(forResource: "legendary/cli", ofType: nil)!)
Expand Down Expand Up @@ -153,6 +155,7 @@ class Legendary {
task.environment = defaultEnvironmentVariables

let fullCommand = "\((defaultEnvironmentVariables.map { "\($0.key)=\"\($0.value)\"" }).joined(separator: " ")) \(task.executableURL!.relativePath.replacingOccurrences(of: " ", with: "\\ ")) \(task.arguments!.joined(separator: " "))"
task.qualityOfService = .userInitiated

log.debug("executing \(fullCommand)")

Expand Down Expand Up @@ -355,7 +358,7 @@ class Legendary {
gameModification.type = type
}

_ = await command(
await command(
args: argBuilder + [game.appName],
useCache: false,
identifier: "install",
Expand Down Expand Up @@ -458,7 +461,7 @@ class Legendary {
if let oldPath = try getGamePath(game: game) {
guard files.isWritableFile(atPath: oldPath) else { throw FileLocations.FileNotModifiableError(.init(filePath: oldPath)) }
try files.moveItem(atPath: oldPath, toPath: "\(newPath)/\(oldPath.components(separatedBy: "/").last!)")
_ = await command(
await command(
args: ["move", game.appName, newPath, "--skip-move"],
useCache: false,
identifier: "moveGame"
Expand Down Expand Up @@ -511,7 +514,7 @@ class Legendary {
VariableManager.shared.setVariable("launching_\(game.appName)", value: true)
defaults.set(try PropertyListEncoder().encode(game), forKey: "recentlyPlayed")

_ = await command(
await command(
args: [
"launch",
game.appName,
Expand Down Expand Up @@ -748,11 +751,11 @@ class Legendary {
if let metadataContents = try? files.contentsOfDirectory(atPath: metadata),
!metadataContents.isEmpty {
Task(priority: .background) {
_ = await command(args: ["status"], useCache: false, identifier: "refreshMetadata")
await command(args: ["status"], useCache: false, identifier: "refreshMetadata")
}
} else {
Task.sync(priority: .high) { // called during onboarding for speed
_ = await command(args: ["status"], useCache: false, identifier: "refreshMetadata")
await command(args: ["status"], useCache: false, identifier: "refreshMetadata")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mythic/Controllers/LocalGames/LocalGames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class LocalGames {
VariableManager.shared.setVariable("launching_\(game.appName)", value: true)
defaults.set(try PropertyListEncoder().encode(game), forKey: "recentlyPlayed")

_ = try await Wine.command(
try await Wine.command(
args: [game.path!],
identifier: "launch_\(game.title)",
bottleURL: bottle.url, // TODO: whichever prefix is set for it or as default
Expand Down
6 changes: 5 additions & 1 deletion Mythic/Controllers/Wine/WineInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Wine { // TODO: https://forum.winehq.org/viewtopic.php?t=15416

- Returns: A tuple containing stdout and stderr data.
*/
@discardableResult
static func command(
args: [String],
identifier: String,
Expand Down Expand Up @@ -212,6 +213,7 @@ class Wine { // TODO: https://forum.winehq.org/viewtopic.php?t=15416
task.environment = defaultEnvironmentVariables

let fullCommand = "\((defaultEnvironmentVariables.map { "\($0.key)=\"\($0.value)\"" }).joined(separator: " ")) \(task.executableURL!.relativePath.replacingOccurrences(of: " ", with: "\\ ")) \(task.arguments!.joined(separator: " "))"
task.qualityOfService = .userInitiated

log.debug("executing \(fullCommand)")

Expand Down Expand Up @@ -394,6 +396,7 @@ class Wine { // TODO: https://forum.winehq.org/viewtopic.php?t=15416
}

// MARK: - Delete Bottle Method
@discardableResult
static func deleteBottle(bottleURL: URL) throws -> Bool {
guard bottleExists(bottleURL: bottleURL) else { throw BottleDoesNotExistError() }

Expand All @@ -404,6 +407,7 @@ class Wine { // TODO: https://forum.winehq.org/viewtopic.php?t=15416
}

// MARK: - Kill All Method
@discardableResult
static func killAll(bottleURL: URL? = nil) -> Bool {
let task = Process()
task.executableURL = Libraries.directory.appending(path: "Wine/bin/wineserver")
Expand Down Expand Up @@ -465,7 +469,7 @@ class Wine { // TODO: https://forum.winehq.org/viewtopic.php?t=15416
private static func addRegistryKey(bottleURL: URL, key: String, name: String, data: String, type: RegistryType) async throws {
guard bottleExists(bottleURL: bottleURL) else { throw BottleDoesNotExistError() }

_ = try await command( // FIXME: errors may create problems later
try await command( // FIXME: errors may create problems later
args: ["reg", "add", key, "-v", name, "-t", type.rawValue, "-d", data, "-f"],
identifier: "regadd",
bottleURL: bottleURL
Expand Down
2 changes: 1 addition & 1 deletion Mythic/Views/BottleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct BottleListView: View {
message: .init("This process cannot be undone."),
primaryButton: .destructive(.init("Delete")) {
do {
_ = try Wine.deleteBottle(bottleURL: bottles[bottleNameToDelete]!.url) // FIXME: this is where the crashes will happen
try Wine.deleteBottle(bottleURL: bottles[bottleNameToDelete]!.url) // FIXME: this is where the crashes will happen
} catch {
Logger.file.error("Unable to delete bottle \(bottleNameToDelete): \(error.localizedDescription)")
bottleNameToDelete = .init()
Expand Down
2 changes: 2 additions & 0 deletions Mythic/Views/GameList/Extensions/GameSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ extension GameListView {
Section("Wine", isExpanded: $isWineSectionExpanded) {
BottleSettingsView(selectedBottle: $selectedBottle, withPicker: true)
}
.disabled(game.platform == .macOS)

Section("DXVK", isExpanded: $isDXVKSectionExpanded) {
Toggle("DXVK", isOn: Binding(get: {return .init()}, set: {_ in}))
.help("Sorry, this isn't implemented yet!")
.disabled(true)
}
.disabled(game.platform == .macOS)
}
.formStyle(.grouped)
.frame(width: geometry.size.width, height: geometry.size.height)
Expand Down
2 changes: 1 addition & 1 deletion Mythic/Views/GameList/GameList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ struct GameListView: View {
Task(priority: .userInitiated) {
updateCurrentGame(game: game, mode: .normal)

_ = try await Legendary.install(
try await Legendary.install(
game: game,
platform: try Legendary.getGamePlatform(game: game)
)
Expand Down
2 changes: 1 addition & 1 deletion Mythic/Views/Navigation/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct MainView: View {
message: .init("This will sign you out of the account \"\(Legendary.whoAmI())\"."),
primaryButton: .destructive(.init("Sign Out")) {
Task(priority: .high) {
_ = await Legendary.command(
await Legendary.command(
args: ["auth", "--delete"],
useCache: false,
identifier: "userAreaSignOut"
Expand Down

0 comments on commit 71ff69f

Please sign in to comment.