-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: pbo extract, unpack, inspect (#606)
* utils: pbo extract, unpack, inspect * fmt
- Loading branch information
1 parent
4491221
commit 7dcb671
Showing
8 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod inspect; | ||
pub mod pbo; | ||
pub mod verify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::{fs::File, path::PathBuf}; | ||
|
||
use clap::{ArgMatches, Command}; | ||
use hemtt_pbo::ReadablePbo; | ||
|
||
use crate::Error; | ||
|
||
#[must_use] | ||
pub fn cli() -> Command { | ||
Command::new("extract") | ||
.about("Extract a file from a PBO") | ||
.arg( | ||
clap::Arg::new("pbo") | ||
.help("PBO file to extract from") | ||
.required(true), | ||
) | ||
.arg( | ||
clap::Arg::new("file") | ||
.help("File to extract") | ||
.required(true), | ||
) | ||
.arg(clap::Arg::new("output").help("Where to save the extracted file")) | ||
} | ||
|
||
/// Execute the extract command | ||
/// | ||
/// # Errors | ||
/// [`Error`] depending on the modules | ||
pub fn execute(matches: &ArgMatches) -> Result<(), Error> { | ||
let path = PathBuf::from(matches.get_one::<String>("pbo").expect("required")); | ||
let mut pbo = ReadablePbo::from(File::open(path)?)?; | ||
let file = matches.get_one::<String>("file").expect("required"); | ||
let Some(mut file) = pbo.file(file)? else { | ||
error!("File `{file}` not found in PBO"); | ||
return Ok(()); | ||
}; | ||
let output = matches.get_one::<String>("output").map(PathBuf::from); | ||
if let Some(output) = output { | ||
std::io::copy(&mut file, &mut File::create(output)?)?; | ||
} else { | ||
std::io::copy(&mut file, &mut std::io::stdout())?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::{fs::File, path::PathBuf}; | ||
|
||
use clap::{ArgMatches, Command}; | ||
|
||
use crate::Error; | ||
|
||
use super::inspect::pbo; | ||
|
||
mod extract; | ||
mod unpack; | ||
|
||
#[must_use] | ||
pub fn cli() -> Command { | ||
Command::new("pbo") | ||
.about("Commands for PBO files") | ||
.arg_required_else_help(true) | ||
.subcommand(extract::cli()) | ||
.subcommand(unpack::cli()) | ||
.subcommand( | ||
Command::new("inspect") | ||
.about("Inspect a PBO") | ||
.arg(clap::Arg::new("pbo").help("PBO to inspect").required(true)), | ||
) | ||
} | ||
|
||
/// Execute the pbo command | ||
/// | ||
/// # Errors | ||
/// [`Error`] depending on the modules | ||
/// | ||
/// # Panics | ||
/// If the args are not present from clap | ||
pub fn execute(matches: &ArgMatches) -> Result<(), Error> { | ||
match matches.subcommand() { | ||
Some(("extract", matches)) => extract::execute(matches), | ||
Some(("unpack", matches)) => unpack::execute(matches), | ||
|
||
Some(("inspect", matches)) => pbo(File::open(PathBuf::from( | ||
matches.get_one::<String>("pbo").expect("required"), | ||
))?), | ||
|
||
_ => unreachable!(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::{ | ||
fs::{File, OpenOptions}, | ||
io::Write, | ||
path::PathBuf, | ||
}; | ||
|
||
use clap::{ArgMatches, Command}; | ||
use hemtt_pbo::ReadablePbo; | ||
|
||
use crate::Error; | ||
|
||
#[must_use] | ||
pub fn cli() -> Command { | ||
Command::new("unpack") | ||
.about("Unpack a PBO") | ||
.arg( | ||
clap::Arg::new("pbo") | ||
.help("PBO file to unpack") | ||
.required(true), | ||
) | ||
.arg( | ||
clap::Arg::new("output") | ||
.help("Directory to unpack to") | ||
.required(true), | ||
) | ||
} | ||
|
||
/// Execute the unpack command | ||
/// | ||
/// # Errors | ||
/// [`Error`] depending on the modules | ||
pub fn execute(matches: &ArgMatches) -> Result<(), Error> { | ||
let path = PathBuf::from(matches.get_one::<String>("pbo").expect("required")); | ||
let mut pbo = ReadablePbo::from(File::open(path)?)?; | ||
let output = PathBuf::from(matches.get_one::<String>("output").expect("required")); | ||
if output.exists() { | ||
error!("Output directory already exists"); | ||
return Ok(()); | ||
} | ||
std::fs::create_dir_all(&output)?; | ||
for (key, value) in pbo.properties() { | ||
debug!("{}: {}", key, value); | ||
if key == "prefix" { | ||
let mut file = File::create(output.join("$PBOPREFIX$"))?; | ||
file.write_all(value.as_bytes())?; | ||
} else { | ||
let mut file = OpenOptions::new() | ||
.write(true) | ||
.create(true) | ||
.append(true) | ||
.open(output.join("properties.txt"))?; | ||
file.write_all(format!("{key}={value}\n").as_bytes())?; | ||
} | ||
} | ||
for header in pbo.files() { | ||
let path = output.join(header.filename().replace('\\', "/")); | ||
std::fs::create_dir_all(path.parent().unwrap())?; | ||
let mut out = File::create(path)?; | ||
let mut file = pbo | ||
.file(header.filename())? | ||
.expect("file must exist if header exists"); | ||
std::io::copy(&mut file, &mut out)?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters