Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp bottle loading and add bottle validation #266

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions Whisky/View Models/BottleVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,61 @@ class BottleVM: ObservableObject {
@MainActor
func loadBottles() {
bottles.removeAll()

for (index, path) in bottlesList.paths.enumerated().reversed() where loadBottle(bottleURL: path) == nil {
bottlesList.paths.remove(at: index)
}

// Update if needed
if !BottleVMEntries.exists() {
do {
let files = try FileManager.default.contentsOfDirectory(at: BottleVM.bottleDir,
includingPropertiesForKeys: nil,
options: .skipsHiddenFiles)
for file in files where file.pathExtension == "plist" {
if let bottlePath = convertFormat(plistPath: file) {
bottlesList.paths.append(bottlePath)
}
for file in files where loadBottle(bottleURL: file) != nil {
bottlesList.paths.append(file)
}
} catch {
print("Failed to list files")
}
bottlesList.encode()
}

bottles = bottlesList.paths.map({
Bottle(bottleUrl: $0)
})
bottles.sortByName()
}

func loadBottle(bottleURL: URL) -> BottleSettings? {
// Try loading as legacy bottle
do {
let files = try FileManager.default.contentsOfDirectory(at: bottleURL, includingPropertiesForKeys: nil)
for file in files where file.pathExtension == "plist" {
if let bottlePath = convertFormat(plistPath: file) {
return BottleSettings(bottleURL: bottlePath)
} else {
print("Failed to load as legacy bottle")
}
}
} catch {
print("Failed to load as legacy bottle")
}

// Try loading as a normal bottle
let bottleMetadata = bottleURL
.appendingPathComponent("Metadata")
.appendingPathExtension("plist")
.path()

if FileManager.default.fileExists(atPath: bottleMetadata) {
let bottle = BottleSettings(bottleURL: bottleURL)
bottle.encode()
return bottle
}

return .none
}

func createNewBottle(bottleName: String, winVersion: WinVersion, bottleURL: URL) -> URL {
let newBottleDir = bottleURL.appendingPathComponent(UUID().uuidString)

Expand Down
12 changes: 1 addition & 11 deletions Whisky/Views/WhiskyApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,8 @@ struct WhiskyApp: App {
panel.begin { result in
if result == .OK {
if let url = panel.urls.first {
let bottleMetadata = url
.appendingPathComponent("Metadata")
.appendingPathExtension("plist")
.path()

if FileManager.default.fileExists(atPath: bottleMetadata) {
// Legacy files
let bottle = BottleSettings(bottleURL: url)
bottle.encode()
}

BottleVM.shared.bottlesList.paths.append(url)
BottleVM.shared.bottlesList.encode()
BottleVM.shared.loadBottles()
}
}
Expand Down