s2mod-packer 0.1.1

Pack/unpack .s2mod Content Pack archives for OpenStranded
Documentation
// s2mod-packer – pack/unpack .s2mod Content Pack archives for OpenStranded
// Copyright (C) 2025  s2mod-packer contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

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}")),
    }
}

/// Run the CLI.
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(())
}