From 10c50eeec646c8e37f6407a62e7703a599e32c27 Mon Sep 17 00:00:00 2001 From: Mykhaylo Marfeychuk Date: Wed, 26 Jul 2023 17:13:20 +0100 Subject: [PATCH] Validate bottle --- Whisky/View Models/BottleVM.swift | 42 +++++++++++++++++++++++++++---- Whisky/Views/WhiskyApp.swift | 12 +-------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/Whisky/View Models/BottleVM.swift b/Whisky/View Models/BottleVM.swift index 99dc5df74..be165255d 100644 --- a/Whisky/View Models/BottleVM.swift +++ b/Whisky/View Models/BottleVM.swift @@ -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) diff --git a/Whisky/Views/WhiskyApp.swift b/Whisky/Views/WhiskyApp.swift index f2af940fe..1384d61dc 100644 --- a/Whisky/Views/WhiskyApp.swift +++ b/Whisky/Views/WhiskyApp.swift @@ -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() } }