Skip to content

Commit 4cf85dc

Browse files
committed
importer better bios matching by filename
Signed-off-by: Joseph Mattiello <git@joemattiello.com>
1 parent 0d1b2aa commit 4cf85dc

2 files changed

Lines changed: 51 additions & 34 deletions

File tree

PVLibrary/Sources/PVLibrary/Importer/Services/GameImporter/GameImporter+Utils.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,42 @@
77

88

99
extension GameImporter {
10-
11-
10+
11+
1212
/// Checks if a given ROM file is a CD-ROM
1313
internal func isCDROM(_ queueItem: ImportQueueItem) -> Bool {
1414
return isCDROM(queueItem.url)
1515
}
16-
16+
1717
/// Checks if a given path is a CD-ROM
1818
internal func isCDROM(_ path: URL) -> Bool {
1919
let cdromExtensions: Set<String> = Extensions.discImageExtensions.union(Extensions.playlistExtensions)
2020
let fileExtension = path.pathExtension.lowercased()
2121
return cdromExtensions.contains(fileExtension)
2222
}
23-
23+
2424
/// Checks if a given path is artwork
2525
internal func isArtwork(_ queueItem: ImportQueueItem) -> Bool {
2626
let artworkExtensions = Extensions.artworkExtensions
2727
let fileExtension = queueItem.url.pathExtension.lowercased()
2828
return artworkExtensions.contains(fileExtension)
2929
}
30-
30+
3131
internal func isBIOS(_ queueItem: ImportQueueItem) throws -> Bool {
32-
guard let md5 = queueItem.md5?.uppercased() else {
33-
throw GameImporterError.couldNotCalculateMD5
34-
}
35-
36-
DLOG("Checking MD5: \(md5) for possible BIOS match")
37-
3832
// First check if this is a BIOS file by MD5
39-
let biosMatches = PVEmulatorConfiguration.biosEntries.filter("expectedMD5 == %@", md5).map({ $0 })
40-
41-
//it's a bios if it's md5 matches known BIOS
42-
return !biosMatches.isEmpty
33+
if let md5 = queueItem.md5?.uppercased() {
34+
DLOG("Checking MD5: \(md5) for possible BIOS match")
35+
let biosMatches = PVEmulatorConfiguration.biosEntries.filter("expectedMD5 == %@", md5).map({ $0 })
36+
if !biosMatches.isEmpty {
37+
return true
38+
}
39+
}
40+
41+
// If no MD5 match, try matching by filename
42+
let filename = queueItem.url.lastPathComponent.lowercased()
43+
DLOG("Checking filename: \(filename) for possible BIOS match")
44+
let filenameMatches = PVEmulatorConfiguration.biosEntries.filter("expectedFilename == %@", filename).map({ $0 })
45+
46+
return !filenameMatches.isEmpty
4347
}
4448
}

PVLibrary/Sources/PVLibrary/Importer/Services/GameImporter/GameImporterDatabaseService.swift

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)