use std::path::{Path, PathBuf};
use crate::package::S2ModPackage;
const USAGE: &str = "\
Usage: s2mod-packer <command> [options]
Pack/unpack .s2mod Content Pack archives for OpenStranded.
Commands:
pack <input_dir> <output.s2mod> Pack a directory into a .s2mod archive
unpack <input.s2mod> <output_dir> Unpack a .s2mod archive to a directory
info <input.s2mod> Show information about a .s2mod archive
help --help, -? Show this help message
Examples:
s2mod-packer pack ./my_mod_dir/ ./output.s2mod
s2mod-packer unpack ./pack.s2mod ./output_dir/
s2mod-packer info ./pack.s2mod
";
enum Command {
Pack { input_dir: PathBuf, output: PathBuf },
Unpack { input: PathBuf, output_dir: PathBuf },
Info { input: PathBuf },
}
fn parse_args() -> Result<Command, String> {
let raw: Vec<String> = std::env::args().collect();
if raw.len() >= 2 && (raw[1] == "--help" || raw[1] == "-?") {
return Err(USAGE.to_string());
}
if raw.len() < 2 {
return Err(format!("too few arguments\n\n{USAGE}"));
}
match raw[1].as_str() {
"pack" => {
if raw.len() < 4 {
return Err(format!("pack requires 2 arguments\n\n{USAGE}"));
}
Ok(Command::Pack {
input_dir: PathBuf::from(&raw[2]),
output: PathBuf::from(&raw[3]),
})
}
"unpack" => {
if raw.len() < 4 {
return Err(format!("unpack requires 2 arguments\n\n{USAGE}"));
}
Ok(Command::Unpack {
input: PathBuf::from(&raw[2]),
output_dir: PathBuf::from(&raw[3]),
})
}
"info" => {
if raw.len() < 3 {
return Err(format!("info requires 1 argument\n\n{USAGE}"));
}
Ok(Command::Info {
input: PathBuf::from(&raw[2]),
})
}
other => Err(format!("unknown command '{other}'\n\n{USAGE}")),
}
}
pub fn run() -> Result<(), Box<dyn std::error::Error>> {
let command = match parse_args() {
Ok(c) => c,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
match command {
Command::Pack { input_dir, output } => {
if !input_dir.is_dir() {
return Err(format!("not a directory: {}", input_dir.display()).into());
}
cmd_pack(&input_dir, &output)?;
}
Command::Unpack { input, output_dir } => {
cmd_unpack(&input, &output_dir)?;
}
Command::Info { input } => {
cmd_info(&input)?;
}
}
Ok(())
}
fn cmd_pack(input_dir: &Path, output: &Path) -> Result<(), Box<dyn std::error::Error>> {
if !input_dir.join("manifest.toml").exists() {
return Err(format!("no manifest.toml found in {}", input_dir.display()).into());
}
S2ModPackage::pack_to_zip(input_dir, output)?;
eprintln!("wrote {}", output.display());
Ok(())
}
fn cmd_unpack(input: &Path, output_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
S2ModPackage::unpack_to_dir(input, output_dir)?;
eprintln!("extracted to {}", output_dir.display());
Ok(())
}
fn cmd_info(input: &Path) -> Result<(), Box<dyn std::error::Error>> {
let pack = S2ModPackage::open(input)?;
let manifest = pack.manifest();
println!("Pack: {} v{}", manifest.pack.name, manifest.pack.version);
println!("ID: {}", manifest.pack.id);
if !manifest.pack.author.is_empty() {
println!("Author: {}", manifest.pack.author);
}
if !manifest.pack.description.is_empty() {
println!("Description: {}", manifest.pack.description);
}
if !manifest.registry.domains.is_empty() {
println!("\nRegistry:");
for (domain, files) in &manifest.registry.domains {
println!(" {domain}:");
for f in files {
println!(" - {f}");
}
}
}
Ok(())
}