Skip to content

Commit

Permalink
MAME bioses can be referenced by the parents
Browse files Browse the repository at this point in the history
  • Loading branch information
alucryd committed Jan 23, 2025
1 parent 45df3ac commit aba8f59
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Add a new `wanted` game state in the web UI, replaces `incomplete` which now means games with some (but not all) its ROMs
- Fix a regression when importing multi-track CHDs which contain tracks identical to other games
- Fix parsing archives containing files with `=` symbols in their names
- Fix several issues affecting the import of MAME DATs

# 0.20.1

Expand Down
33 changes: 33 additions & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,39 @@ pub async fn find_rom_by_size_and_crc_and_game_id(
})
}

pub async fn find_rom_by_size_and_crc_and_game_ids(
connection: &mut SqliteConnection,
size: i64,
crc: &str,
game_ids: &[i64],
) -> Vec<Rom> {
let crc = crc.to_lowercase();
let sql = format!(
"
SELECT *
FROM roms
WHERE size = {}
AND crc = '{}'
AND game_id IN ({})
AND parent_id IS NULL
",
size,
crc,
game_ids.iter().join(",")
);
sqlx::query_as::<_, Rom>(&sql)
.fetch_all(connection)
.await
.unwrap_or_else(|_| {
panic!(
"Error while finding rom with size {} and CRC {} and game ids {}",
size,
crc,
game_ids.iter().join(",")
)
})
}

pub async fn delete_rom_by_name_and_game_id(
connection: &mut SqliteConnection,
name: &str,
Expand Down
75 changes: 45 additions & 30 deletions src/import_dats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ async fn create_or_update_roms(
connection: &mut SqliteConnection,
progress_bar: &ProgressBar,
roms_xml: &[RomXml],
mut bios: bool,
bios: bool,
disk: bool,
game_id: i64,
) -> Vec<i64> {
Expand All @@ -566,34 +566,31 @@ async fn create_or_update_roms(
continue;
}
// find parent rom if needed
let mut parent_id = None;
let mut rom_parent_bios = false;
let mut rom_parent_id = None;
if rom_xml.merge.is_some() && rom_xml.crc.is_some() {
let game = find_game_by_id(connection, game_id).await;
let parent_rom = if let Some(parent_id) = game.parent_id {
find_rom_by_size_and_crc_and_game_id(
connection,
rom_xml.size,
rom_xml.crc.as_ref().unwrap(),
parent_id,
)
.await
} else {
None
};
let bios_rom = if let Some(bios_id) = game.bios_id {
find_rom_by_size_and_crc_and_game_id(
connection,
rom_xml.size,
rom_xml.crc.as_ref().unwrap(),
bios_id,
)
.await
} else {
None
};
if let Some(rom) = parent_rom.or(bios_rom) {
bios = rom.bios;
parent_id = Some(rom.id);
let mut game_ids: Vec<i64> = vec![];
if let Some(bios_id) = game.bios_id {
game_ids.push(bios_id);
}
if let Some(parent_id) = game.parent_id {
game_ids.push(parent_id);
let parent_game = find_game_by_id(connection, parent_id).await;
if let Some(bios_id) = parent_game.bios_id {
game_ids.push(bios_id);
}
}
let roms = find_rom_by_size_and_crc_and_game_ids(
connection,
rom_xml.size,
rom_xml.crc.as_ref().unwrap(),
&game_ids,
)
.await;
if let Some(rom) = roms.first() {
rom_parent_bios = rom.bios;
rom_parent_id = Some(rom.id);
} else {
progress_bar.println(format!(
"Rom \"{}\" not found in game \"{}\" parent/bios, please fix your DAT file",
Expand All @@ -603,8 +600,16 @@ async fn create_or_update_roms(
}
match find_rom_by_name_and_game_id(connection, &rom_xml.name, game_id).await {
Some(rom) => {
update_rom_from_xml(connection, rom.id, rom_xml, bios, disk, game_id, parent_id)
.await;
update_rom_from_xml(
connection,
rom.id,
rom_xml,
bios || rom_parent_bios,
disk,
game_id,
rom_parent_id,
)
.await;
if rom_xml.size != rom.size
|| rom_xml.crc.is_some()
&& rom_xml.crc.as_ref().unwrap() != rom.crc.as_ref().unwrap()
Expand All @@ -616,7 +621,17 @@ async fn create_or_update_roms(
}
rom.id
}
None => create_rom_from_xml(connection, rom_xml, bios, disk, game_id, parent_id).await,
None => {
create_rom_from_xml(
connection,
rom_xml,
bios || rom_parent_bios,
disk,
game_id,
rom_parent_id,
)
.await
}
};
}
orphan_romfile_ids
Expand Down

0 comments on commit aba8f59

Please sign in to comment.