diff --git a/PVLibrary/Sources/PVLibrary/Configuration/PVEmulatorConfiguration.swift b/PVLibrary/Sources/PVLibrary/Configuration/PVEmulatorConfiguration.swift index 895c649fed..1cf5c64dba 100644 --- a/PVLibrary/Sources/PVLibrary/Configuration/PVEmulatorConfiguration.swift +++ b/PVLibrary/Sources/PVLibrary/Configuration/PVEmulatorConfiguration.swift @@ -17,10 +17,9 @@ import PVPrimitives @objc public final class PVEmulatorConfiguration: NSObject { - @objc - public static let availableSystemIdentifiers: [String] = { - systems.map({ (system) -> String in - system.identifier + public static let availableSystemIdentifiers: [SystemIdentifier] = { + systems.map({ (system) -> SystemIdentifier in + system.systemIdentifier }) }() @@ -82,10 +81,9 @@ public final class PVEmulatorConfiguration: NSObject { return systems.first { $0.openvgDatabaseID == databaseID }?.identifier } - @objc - public class func systemIdentifiers(forFileExtension fileExtension: String) -> [String]? { - return systems(forFileExtension: fileExtension)?.compactMap({ (system) -> String? in - system.identifier + public class func systemIdentifiers(forFileExtension fileExtension: String) -> [SystemIdentifier]? { + return systems(forFileExtension: fileExtension)?.compactMap({ (system) -> SystemIdentifier? in + system.systemIdentifier }) } @@ -191,19 +189,6 @@ public extension PVEmulatorConfiguration { public extension PVEmulatorConfiguration { - - @objc - class func system(forDatabaseID databaseID : Int) -> PVSystem? { - guard let systemID = systemID(forDatabaseID: databaseID) else { return nil } - let system = RomDatabase.sharedInstance.object(ofType: PVSystem.self, wherePrimaryKeyEquals: systemID) - return system - } - - class func system(forDatabaseID databaseID : Int) -> System? { - let pvsystem: PVSystem? = system(forDatabaseID: databaseID) - return pvsystem?.asDomain() - } - @objc class func system(forIdentifier systemID: String) -> PVSystem? { let system = RomDatabase.sharedInstance.object(ofType: PVSystem.self, wherePrimaryKeyEquals: systemID) diff --git a/PVLibrary/Sources/PVLibrary/Extensions/ROMMetadata+PVSystem.swift b/PVLibrary/Sources/PVLibrary/Extensions/ROMMetadata+PVSystem.swift index 0da9e1dff6..07bf12328a 100644 --- a/PVLibrary/Sources/PVLibrary/Extensions/ROMMetadata+PVSystem.swift +++ b/PVLibrary/Sources/PVLibrary/Extensions/ROMMetadata+PVSystem.swift @@ -4,6 +4,6 @@ import PVLookupTypes public extension ROMMetadata { /// The corresponding PVSystem for this ROM metadata var system: PVSystem? { - return PVEmulatorConfiguration.system(forDatabaseID: systemID.openVGDBID) + return PVEmulatorConfiguration.system(forIdentifier: self.systemID) } } diff --git a/PVLibrary/Sources/PVLibrary/Importer/Models/ImportQueueItem.swift b/PVLibrary/Sources/PVLibrary/Importer/Models/ImportQueueItem.swift index 2b528165ab..5d110f06d8 100644 --- a/PVLibrary/Sources/PVLibrary/Importer/Models/ImportQueueItem.swift +++ b/PVLibrary/Sources/PVLibrary/Importer/Models/ImportQueueItem.swift @@ -62,17 +62,10 @@ public enum ProcessingState { @Perceptible public class ImportQueueItem: Identifiable, ObservableObject { - // TODO: Make this more generic with AnySystem, some System? - //public typealias System = PVSystem //AnySystem - public let id = UUID() public var url: URL public var fileType: FileType - @MainActor - @PerceptionIgnored public var systems: [SystemIdentifier] = [] // Can be set to the specific system type - @MainActor - @PerceptionIgnored public var userChosenSystem: (SystemIdentifier)? = nil public var destinationUrl: URL? public var errorValue: String? @@ -91,9 +84,8 @@ public class ImportQueueItem: Identifiable, ObservableObject { } } - @MainActor private func updateSystems() { - systems = RomDatabase.sharedInstance.all(PVSystem.self).map { $0.systemIdentifier } + systems = PVEmulatorConfiguration.availableSystemIdentifiers } private let md5Provider: MD5Provider diff --git a/PVLibrary/Sources/PVLibrary/Importer/Services/GameImporter/GameImporter.swift b/PVLibrary/Sources/PVLibrary/Importer/Services/GameImporter/GameImporter.swift index 3b36d824aa..141900c328 100644 --- a/PVLibrary/Sources/PVLibrary/Importer/Services/GameImporter/GameImporter.swift +++ b/PVLibrary/Sources/PVLibrary/Importer/Services/GameImporter/GameImporter.swift @@ -198,10 +198,15 @@ public final class GameImporter: GameImporting, ObservableObject { var importAutoStartDelayTask: Task? public var importQueue: [ImportQueueItem] = [] { didSet { - importAutoStartDelayTask?.cancel() - importAutoStartDelayTask = Task { - await try? Task.sleep(for: .seconds(1)) - self.startProcessing() + // Schedule auto-start if there are queued items OR items with a user-chosen system + if importQueue.contains(where: { + $0.status == .queued || $0.userChosenSystem != nil + }) { + importAutoStartDelayTask?.cancel() + importAutoStartDelayTask = Task { + await try? Task.sleep(for: .seconds(1)) + self.startProcessing() + } } } } @@ -646,17 +651,33 @@ public final class GameImporter: GameImporting, ObservableObject { // Processes each ImportItem in the queue sequentially @MainActor private func processQueue() async { + // Check for items that are either queued or have a user-chosen system + let itemsToProcess = importQueue.filter { + $0.status == .queued || $0.userChosenSystem != nil + } + + guard !itemsToProcess.isEmpty else { + DispatchQueue.main.async { + self.processingState = .idle + } + return + } + ILOG("GameImportQueue - processQueue beginning Import Processing") DispatchQueue.main.async { self.processingState = .processing } - for item in importQueue where item.status == .queued { + for item in itemsToProcess { + // If there's a user-chosen system, ensure the item is queued + if item.userChosenSystem != nil { + item.status = .queued + } await processItem(item) } DispatchQueue.main.async { - self.processingState = .idle // Reset processing status when queue is fully processed + self.processingState = .idle } ILOG("GameImportQueue - processQueue complete Import Processing") } @@ -733,7 +754,7 @@ public final class GameImporter: GameImporting, ObservableObject { //update item's candidate systems with the result of determineSystems item.systems = systems - + //this might be a conflict if we can't infer what to do //for BIOS, we can handle multiple systems, so allow that to proceed if item.fileType != .bios && item.targetSystem() == nil {