Add context to errors
This commit is contained in:
parent
7d7fe618f0
commit
00b8c54022
83
src/main.rs
83
src/main.rs
|
@ -1,7 +1,7 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use clap::Clap;
|
use clap::Clap;
|
||||||
use log::{debug, info, warn};
|
use log::{debug, info, warn};
|
||||||
|
|
||||||
|
@ -93,9 +93,14 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
match opts.subcmd {
|
match opts.subcmd {
|
||||||
SubCommand::UnXWB(opts) => {
|
SubCommand::UnXWB(opts) => {
|
||||||
let xwb_data = fs::read(&opts.file)?;
|
let xwb_data = fs::read(&opts.file)
|
||||||
let wave_bank = WaveBank::parse(&xwb_data)?;
|
.with_context(|| format!("failed to read XWB file {}", &opts.file.display()))?;
|
||||||
info!("Opened wave bank “{}” from {:?}", wave_bank.name, opts.file);
|
let wave_bank = WaveBank::parse(&xwb_data).context("failed to parse XWB file")?;
|
||||||
|
info!(
|
||||||
|
"Opened wave bank “{}” from {}",
|
||||||
|
wave_bank.name,
|
||||||
|
&opts.file.display()
|
||||||
|
);
|
||||||
|
|
||||||
let entries = match opts.single_entry {
|
let entries = match opts.single_entry {
|
||||||
Some(name) => match wave_bank.sounds.get(&name) {
|
Some(name) => match wave_bank.sounds.get(&name) {
|
||||||
|
@ -112,37 +117,66 @@ fn main() -> Result<()> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
info!("Extracting {}", name);
|
info!("Extracting {}", name);
|
||||||
fs::write(format!("{}.wav", name), &sound.to_wav()?)?;
|
let file_name = format!("{}.wav", name);
|
||||||
|
fs::write(
|
||||||
|
file_name.clone(),
|
||||||
|
&sound.to_wav().with_context(|| {
|
||||||
|
format!("failed to convert wave bank sound entry “{}” to WAV", name)
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.with_context(|| format!("failed to write sound to {}", file_name))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SubCommand::DDR2osu(opts) => {
|
SubCommand::DDR2osu(opts) => {
|
||||||
let sound_name = &opts
|
let sound_name =
|
||||||
|
&opts
|
||||||
.sound_name
|
.sound_name
|
||||||
|
.clone()
|
||||||
.unwrap_or(match get_basename(&opts.ssq_file) {
|
.unwrap_or(match get_basename(&opts.ssq_file) {
|
||||||
Some(basename) => basename.to_string(),
|
Some(basename) => basename.to_string(),
|
||||||
None => return error(
|
None => {
|
||||||
"Could not extract chart id from file name. Please specify it manually."
|
return Err(anyhow!(
|
||||||
.to_string(),
|
"Could not extract chart id from file name. Please specify it manually."))
|
||||||
),
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
"Converting {:?} and sound {} from {:?} to {:?}",
|
"Converting {} and sound {} from {} to {}",
|
||||||
opts.ssq_file, sound_name, opts.xwb_file, opts.out_file
|
opts.ssq_file.display(),
|
||||||
|
sound_name,
|
||||||
|
opts.xwb_file.display(),
|
||||||
|
opts.out_file.display()
|
||||||
);
|
);
|
||||||
|
|
||||||
let ssq_data = fs::read(&opts.ssq_file)?;
|
let ssq_data = fs::read(&opts.ssq_file)
|
||||||
let ssq = SSQ::parse(&ssq_data)?;
|
.with_context(|| format!("failed to read SSQ file {}", &opts.ssq_file.display()))?;
|
||||||
|
let ssq = SSQ::parse(&ssq_data).context("failed to parse SSQ file")?;
|
||||||
|
|
||||||
let convert_config = opts.convert;
|
let beatmaps = ssq
|
||||||
let beatmaps = ssq.to_beatmaps(&convert_config)?;
|
.to_beatmaps(&opts.convert)
|
||||||
|
.context("failed to convert DDR step chart to osu!mania beatmap")?;
|
||||||
|
|
||||||
let xwb_data = fs::read(&opts.xwb_file)?;
|
let xwb_data = fs::read(&opts.xwb_file).with_context(|| {
|
||||||
let wave_bank = WaveBank::parse(&xwb_data)?;
|
format!(
|
||||||
|
"failed to read XWB file {}",
|
||||||
|
&opts.xwb_file.clone().display()
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
let wave_bank = WaveBank::parse(&xwb_data).context("failed to parse XWB file")?;
|
||||||
|
|
||||||
let audio_data = if wave_bank.sounds.contains_key(sound_name) {
|
let audio_data = if wave_bank.sounds.contains_key(sound_name) {
|
||||||
wave_bank.sounds.get(sound_name).unwrap().to_wav()?
|
wave_bank
|
||||||
|
.sounds
|
||||||
|
.get(sound_name)
|
||||||
|
.unwrap()
|
||||||
|
.to_wav()
|
||||||
|
.with_context(|| {
|
||||||
|
format!(
|
||||||
|
"failed to convert wave bank sound entry “{}” to WAV",
|
||||||
|
sound_name
|
||||||
|
)
|
||||||
|
})?
|
||||||
} else if wave_bank.sounds.len() == 2 {
|
} else if wave_bank.sounds.len() == 2 {
|
||||||
warn!(
|
warn!(
|
||||||
"Sound {} not found in wave bank, but it has two entries; assuming these are preview and full song",
|
"Sound {} not found in wave bank, but it has two entries; assuming these are preview and full song",
|
||||||
|
@ -150,7 +184,12 @@ fn main() -> Result<()> {
|
||||||
);
|
);
|
||||||
let mut sounds = wave_bank.sounds.values().collect::<Vec<&XWBSound>>();
|
let mut sounds = wave_bank.sounds.values().collect::<Vec<&XWBSound>>();
|
||||||
sounds.sort_unstable_by(|a, b| b.size.cmp(&a.size));
|
sounds.sort_unstable_by(|a, b| b.size.cmp(&a.size));
|
||||||
sounds[0].to_wav()?
|
sounds[0].to_wav().with_context(|| {
|
||||||
|
format!(
|
||||||
|
"failed to convert wave bank sound entry “{}” to WAV",
|
||||||
|
sound_name
|
||||||
|
)
|
||||||
|
})?
|
||||||
} else {
|
} else {
|
||||||
return Err(anyhow!(
|
return Err(anyhow!(
|
||||||
"Could not find matching sound in wave bank (searched for {})",
|
"Could not find matching sound in wave bank (searched for {})",
|
||||||
|
@ -162,7 +201,9 @@ fn main() -> Result<()> {
|
||||||
beatmaps,
|
beatmaps,
|
||||||
assets: vec![("audio.wav", &audio_data)],
|
assets: vec![("audio.wav", &audio_data)],
|
||||||
};
|
};
|
||||||
osz.write(&opts.out_file)?;
|
osz.write(&opts.out_file).with_context(|| {
|
||||||
|
format!("failed to write OSZ file to {}", opts.out_file.display())
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Reference in a new issue