Skip to content

Commit

Permalink
feat: romname command (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
francisdb authored Nov 28, 2024
1 parent f4e817c commit 2508618
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Show help

```
> vpxtool --help
Vpxtool v0.16.0
Extracts and assembles vpx files
Usage: vpxtool [COMMAND]
Expand All @@ -46,6 +48,7 @@ Commands:
config Vpxtool related config file
images Vpx image related commands
gamedata Vpx gamedata related commands
romname Prints the PinMAME ROM name from a vpx file
help Print this message or the help of the given subcommand(s)
Options:
Expand Down
13 changes: 13 additions & 0 deletions src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,19 @@ fn index_vpx_file(vpx_file_path: &PathWithMetadata) -> io::Result<(PathBuf, Inde
Ok((indexed.path.clone(), indexed))
}

pub(crate) fn get_romname_from_vpx(vpx_path: &Path) -> io::Result<Option<String>> {
let mut vpx_file = vpx::open(vpx_path)?;
let game_data = vpx_file.read_gamedata()?;
let code = consider_sidecar_vbs(vpx_path, game_data)?;
let game_name = extract_game_name(&code);
let requires_pinmame = requires_pinmame(&code);
if requires_pinmame {
Ok(game_name)
} else {
Ok(None)
}
}

fn read_table_info_json(info_file_path: &Path) -> io::Result<TableInfo> {
let mut info_file = File::open(info_file_path)?;
let json = serde_json::from_reader(&mut info_file).map_err(|e| {
Expand Down
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const CMD_IMAGES_WEBP: &str = "webp";
const CMD_GAMEDATA: &str = "gamedata";
const CMD_GAMEDATA_SHOW: &str = "show";

const CMD_ROMNAME: &str = "romname";

pub struct ProgressBarProgress {
pb: ProgressBar,
}
Expand Down Expand Up @@ -759,6 +761,17 @@ fn handle_command(matches: ArgMatches) -> io::Result<ExitCode> {
}
_ => unreachable!(),
},
Some((CMD_ROMNAME, sub_matches)) => {
let path = sub_matches
.get_one::<String>("VPXPATH")
.map(|s| s.as_str())
.unwrap_or_default();
let expanded_path = expand_path_exists(path)?;
if let Some(rom_name) = indexer::get_romname_from_vpx(&expanded_path)? {
println!("{rom_name}")?;
}
Ok(ExitCode::SUCCESS)
}
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachable!()
}
}
Expand Down Expand Up @@ -1052,6 +1065,12 @@ fn build_command() -> Command {
),
),
)
.subcommand(
Command::new(CMD_ROMNAME)
.about("Prints the PinMAME ROM name from a vpx file")
.long_about("Extracts the PinMAME ROM name from a vpx file by searching for specific patterns in the table script. If the table is not PinMAME based, no output is produced.")
.arg(arg!(<VPXPATH> "The path to the vpx file").required(true)),
)
}

fn open_or_fail(vbs_path: &Path, config: Option<&ResolvedConfig>) -> io::Result<ExitCode> {
Expand Down

0 comments on commit 2508618

Please sign in to comment.