Skip to content

Commit

Permalink
import queue use Systemidentifier more, remove mains
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Mattiello <git@joemattiello.com>
  • Loading branch information
JoeMatt committed Dec 27, 2024
1 parent 14c77a9 commit cce13a5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}()

Expand Down Expand Up @@ -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
})
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,15 @@ public final class GameImporter: GameImporting, ObservableObject {
var importAutoStartDelayTask: Task<Void, Never>?
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()
}
}
}
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cce13a5

Please sign in to comment.