Added batch replace, fixed palette regeneration

This commit is contained in:
G2-Games 2024-05-24 03:15:55 -05:00
parent 66fe91568d
commit c58b9c92d1

View file

@ -16,6 +16,10 @@ struct Cli {
enum Commands { enum Commands {
/// Converts a CZ file to a PNG /// Converts a CZ file to a PNG
Decode { Decode {
/// Decode a whole folder, and output to another folder
#[arg(short, long)]
batch: bool,
/// Input CZ file of any type /// Input CZ file of any type
#[arg(value_name = "CZ FILE")] #[arg(value_name = "CZ FILE")]
input: PathBuf, input: PathBuf,
@ -27,6 +31,11 @@ enum Commands {
/// Replace a CZ file's image data /// Replace a CZ file's image data
Replace { Replace {
/// Replace a whole folder, and output to another folder,
/// using a folder of replacements
#[arg(short, long)]
batch: bool,
/// Original input CZ file of any type /// Original input CZ file of any type
#[arg(value_name = "CZ FILE")] #[arg(value_name = "CZ FILE")]
input: PathBuf, input: PathBuf,
@ -50,32 +59,74 @@ fn main() {
// Check what subcommand was run // Check what subcommand was run
match &cli.command { match &cli.command {
Commands::Decode { input, output } => { Commands::Decode { input, output, batch } => {
if !input.exists() { if !input.exists() {
Error::raw( Error::raw(
ErrorKind::ValueValidation, ErrorKind::ValueValidation,
format!("The input file provided does not exist\n") format!("The input file/folder provided does not exist\n")
).exit() ).exit()
} }
let cz = match DynamicCz::open(input) { if *batch {
Ok(cz) => cz, if input.is_file() {
Err(err) => {
Error::raw( Error::raw(
ErrorKind::ValueValidation, ErrorKind::ValueValidation,
format!("Could not open input as a CZ file: {}\n", err) format!("Batch input must be a directory\n")
).exit() ).exit()
}, }
};
if let Some(output) = output { if output.is_none() || output.as_ref().unwrap().is_file() {
cz.save_as_png(output).unwrap(); Error::raw(
ErrorKind::ValueValidation,
format!("Batch output must be a directory\n")
).exit()
}
for entry in walkdir::WalkDir::new(input).max_depth(1) {
let path = entry.unwrap().into_path();
if !path.is_file() {
continue;
}
let filename = PathBuf::from(path.file_name().unwrap());
let filename = filename.with_extension("png");
let mut final_path = output.clone().unwrap();
final_path.push(filename);
let cz = match DynamicCz::open(&path) {
Ok(cz) => cz,
Err(_) => {
Error::raw(
ErrorKind::ValueValidation,
format!("Could not open input as a CZ file: {}\n", path.into_os_string().to_str().unwrap())
).print().unwrap();
continue;
},
};
cz.save_as_png(&final_path).unwrap();
}
} else { } else {
let file_stem = PathBuf::from(input.file_stem().unwrap()); let cz = match DynamicCz::open(input) {
cz.save_as_png(&file_stem.with_extension("png")).unwrap(); Ok(cz) => cz,
Err(err) => {
Error::raw(
ErrorKind::ValueValidation,
format!("Could not open input as a CZ file: {}\n", err)
).exit()
},
};
if let Some(output) = output {
cz.save_as_png(output).unwrap();
} else {
let file_stem = PathBuf::from(input.file_name().unwrap());
cz.save_as_png(&file_stem.with_extension("png")).unwrap();
}
} }
} }
Commands::Replace { input, replacement, output, version } => { Commands::Replace { batch, input, replacement, output, version } => {
if !input.exists() { if !input.exists() {
Error::raw( Error::raw(
ErrorKind::ValueValidation, ErrorKind::ValueValidation,
@ -90,51 +141,119 @@ fn main() {
).exit() ).exit()
} }
let mut cz = match DynamicCz::open(input) { if *batch {
Ok(cz) => cz, if input.is_file() {
Err(err) => {
Error::raw( Error::raw(
ErrorKind::ValueValidation, ErrorKind::ValueValidation,
format!("Could not open input as a CZ file: {}\n", err) format!("Batch input must be a directory\n")
).exit() ).exit()
}, }
};
let repl_img = match image::open(replacement) { if replacement.is_file() {
Ok(img) => img,
Err(err) => {
Error::raw( Error::raw(
ErrorKind::ValueValidation, ErrorKind::ValueValidation,
format!("Could not open replacement file as an image: {}\n", err) format!("Batch replacement location must be a directory\n")
).exit() ).exit()
}, }
};
let repl_img = repl_img.to_rgba8();
cz.header_mut().set_width(repl_img.width() as u16); if output.is_file() {
cz.header_mut().set_height(repl_img.height() as u16); Error::raw(
cz.set_bitmap(repl_img.into_raw()); ErrorKind::ValueValidation,
format!("Batch output must be a directory\n")
).exit()
}
if let Some(ver) = version { for entry in walkdir::WalkDir::new(input).max_depth(1) {
match cz.header_mut().set_version(*ver) { let path = entry.unwrap().into_path();
Ok(_) => (), if !path.is_file() {
Err(_) => { continue;
}
let filename = PathBuf::from(path.file_name().unwrap());
let mut final_path = output.clone();
final_path.push(&filename);
let mut final_replacement = replacement.clone();
final_replacement.push(filename.with_extension("png"));
let repl_img = match image::open(&final_replacement) {
Ok(img) => img,
Err(_) => {
Error::raw(
ErrorKind::ValueValidation,
format!("Could not open replacement file as an image: {}\n", final_replacement.into_os_string().to_str().unwrap())
).exit()
},
};
let repl_img = repl_img.to_rgba8();
let mut cz = match DynamicCz::open(&path) {
Ok(cz) => cz,
Err(_) => {
Error::raw(
ErrorKind::ValueValidation,
format!("Could not open input as a CZ file: {}\n", path.into_os_string().to_str().unwrap())
).print().unwrap();
continue;
},
};
cz.header_mut().set_width(repl_img.width() as u16);
cz.header_mut().set_height(repl_img.height() as u16);
cz.set_bitmap(repl_img.into_raw());
cz.remove_palette();
cz.save_as_cz(&final_path).unwrap();
}
} else {
let mut cz = match DynamicCz::open(input) {
Ok(cz) => cz,
Err(err) => {
Error::raw( Error::raw(
ErrorKind::ValueValidation, ErrorKind::ValueValidation,
format!("Invalid CZ Version {}; expected 0, 1, 2, 3, or 4\n", ver) format!("Could not open input as a CZ file: {}\n", err)
).exit() ).exit()
}, },
}; };
}
match cz.save_as_cz(output) { let repl_img = match image::open(replacement) {
Ok(_) => (), Ok(img) => img,
Err(err) => { Err(err) => {
Error::raw( Error::raw(
ErrorKind::ValueValidation, ErrorKind::ValueValidation,
format!("Failed to save CZ file: {}\n", err) format!("Could not open replacement file as an image: {}\n", err)
).exit() ).exit()
}, },
};
let repl_img = repl_img.to_rgba8();
cz.header_mut().set_width(repl_img.width() as u16);
cz.header_mut().set_height(repl_img.height() as u16);
cz.set_bitmap(repl_img.into_raw());
cz.remove_palette();
if let Some(ver) = version {
match cz.header_mut().set_version(*ver) {
Ok(_) => (),
Err(_) => {
Error::raw(
ErrorKind::ValueValidation,
format!("Invalid CZ Version {}; expected 0, 1, 2, 3, or 4\n", ver)
).exit()
},
};
}
match cz.save_as_cz(output) {
Ok(_) => (),
Err(err) => {
Error::raw(
ErrorKind::ValueValidation,
format!("Failed to save CZ file: {}\n", err)
).exit()
},
}
} }
}, },
} }