@@ -93,35 +93,48 @@ class GameImporterDatabaseService : GameImporterDatabaseServicing {
9393 }
9494
9595 internal func importBIOSIntoDatabase( queueItem: ImportQueueItem ) async throws {
96- guard let destinationUrl = queueItem. destinationUrl,
97- let md5 = queueItem. md5? . uppercased ( ) else {
98- //how did we get here, throw?
96+ guard let destinationUrl = queueItem. destinationUrl else {
9997 throw GameImporterError . incorrectDestinationURL
10098 }
10199
102- // Get all BIOS entries that match this MD5
103- let matchingBIOSEntries : [ PVBIOS ] = PVEmulatorConfiguration . biosArray. filter { biosEntry in
104- let frozenBiosEntry = biosEntry. isFrozen ? biosEntry : biosEntry. freeze ( )
105- return frozenBiosEntry. expectedMD5. uppercased ( ) == md5
100+ var matchingBIOSEntries : [ PVBIOS ] = [ ]
101+
102+ // Try matching by MD5 first
103+ if let md5 = queueItem. md5? . uppercased ( ) {
104+ matchingBIOSEntries = PVEmulatorConfiguration . biosArray. filter { biosEntry in
105+ let frozenBiosEntry = biosEntry. isFrozen ? biosEntry : biosEntry. freeze ( )
106+ return frozenBiosEntry. expectedMD5. uppercased ( ) == md5
107+ }
106108 }
107109
108- for biosEntry in matchingBIOSEntries {
109- // Get the first matching system
110+ // If no MD5 matches, try matching by filename
111+ if matchingBIOSEntries. isEmpty {
112+ let filename = queueItem. url. lastPathComponent. lowercased ( )
113+ matchingBIOSEntries = PVEmulatorConfiguration . biosArray. filter { biosEntry in
110114 let frozenBiosEntry = biosEntry. isFrozen ? biosEntry : biosEntry. freeze ( )
115+ return frozenBiosEntry. expectedFilename. lowercased ( ) == filename
116+ }
117+ }
111118
112- // Update BIOS entry in Realm
113- try await MainActor . run {
114- let realm = try Realm ( )
115- try realm. write {
116- if let thawedBios = frozenBiosEntry. thaw ( ) {
117- let biosFile = PVFile ( withURL: destinationUrl)
118- thawedBios. file = biosFile
119- }
119+ // If we still have no matches, throw an error
120+ guard !matchingBIOSEntries. isEmpty else {
121+ throw GameImporterError . noSystemMatched
122+ }
123+
124+ // Update each matching BIOS entry in Realm
125+ for biosEntry in matchingBIOSEntries {
126+ let frozenBiosEntry = biosEntry. isFrozen ? biosEntry : biosEntry. freeze ( )
127+
128+ try await MainActor . run {
129+ let realm = try Realm ( )
130+ try realm. write {
131+ if let thawedBios = frozenBiosEntry. thaw ( ) {
132+ let biosFile = PVFile ( withURL: destinationUrl)
133+ thawedBios. file = biosFile
120134 }
121135 }
136+ }
122137 }
123-
124- return
125138 }
126139
127140 /// Imports a ROM to the database
0 commit comments