//! `horizon-port-pack`: port a Java modpack to Horizon (`docs/modpacks.md`). //! //! - `import `: a `.mrpack`, CurseForge zip or instance folder //! to `/pack.json`. //! - `inventory [--offline]`: Modrinth metadata (cached in //! `/modrinth.json`), the jars in the instance or `/jars/`, //! classes, tiers and dependency layers in `/inventory.json` and //! `inventory.csv`. //! - `fetch [--from ] [...]`: the jars of the //! portable mods (content, data tier; or the ones named) into //! `/jars/`: copied from the player's folder, or downloaded from the //! pack's own links or Modrinth's CDN, hash checked. What only CurseForge //! has needs a key and is listed in `/fetch.json`. //! - `carry [--resources ] [...]`: the content //! mods' data and assets into `/mods//carry/`, a folder mod for //! local use. With the NeoForge oracle's `/oracle/facts.json` //! (`tools/neoforge_oracle.py`), blocks and items come from its facts; //! `--resources` limits templates to the blocks Horizon's tables have. //! - `report `: the coverage report, per mod ported, partial, not yet //! or not needed with the reason, in `/coverage.json` and //! `coverage.md`. //! - `check `: one mod jar on its own, as the launcher's mod //! browser installs it: carried over into `/carry/`, its classes //! read, and the verdict (Works, Partly, Doesn't run) in //! `/check.json`. //! - `port [--from ] [--resources ]`: every //! step for the portable mods, as the launcher's "Import modpack" runs it. //! //! The work folder belongs to the user: jars and carried-over files stay //! there and are never uploaded or committed. use horizon_port_pack::{fetch, inventory::Record, pipeline}; use std::{ path::{Path, PathBuf}, process::ExitCode, }; const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | fetch [--from ] [...] | carry [--resources ] [...] | report | check | port [--from ] [--resources ]"; fn main() -> ExitCode { let mut args: Vec = std::env::args().skip(1).collect(); let result = (|| { let from = option(&mut args, "--from")?; let resources = option(&mut args, "--resources")?; let offline = take_flag(&mut args, "--offline"); let path = |i: usize| args.get(i).map(Path::new).ok_or(USAGE.to_owned()); let rest = || args.get(2..).unwrap_or_default().to_vec(); let log = &|line: String| println!("{line}"); match args.first().map(String::as_str) { Some("import") => pipeline::import(path(1)?, path(2)?, log).map(drop), Some("inventory") => pipeline::inventory(path(1)?, offline, log).map(drop), Some("fetch") => { let work = path(1)?; let records: Vec = pipeline::read_json(&work.join("inventory.json"))?; let only = rest(); let want = |r: &Record| { if only.is_empty() { pipeline::portable(r) } else { only.contains(&r.key) } }; fetch::fetch(work, &records, from.as_deref(), &want, log).map(drop) } Some("carry") => { pipeline::carry(path(1)?, resources.as_deref(), &rest(), log).map(drop) } Some("report") => { let work = path(1)?; pipeline::report(work, log)?; println!("{}", work.join("coverage.md").display()); Ok(()) } Some("check") => { let c = horizon_port_pack::check::check(path(1)?, path(2)?)?; println!("{}: {}. {}", c.mod_id, c.verdict.label(), c.summary); for p in &c.ports { println!(" ports: {p}"); } for s in &c.skipped { println!(" skipped: {s}"); } Ok(()) } Some("port") => { let work = path(2)?; pipeline::import(path(1)?, work, log)?; pipeline::inventory(work, offline, log)?; pipeline::port(work, resources.as_deref(), from.as_deref(), log)?; println!("{}", work.join("coverage.md").display()); Ok(()) } _ => Err(USAGE.to_owned()), } })(); match result { Ok(()) => ExitCode::SUCCESS, Err(e) => { eprintln!("error: {e}"); ExitCode::FAILURE } } } /// Removes `--name ` from `args`. fn option(args: &mut Vec, name: &str) -> Result, String> { match args.iter().position(|a| a == name) { Some(i) if i + 1 < args.len() => { let value = args.remove(i + 1); args.remove(i); Ok(Some(PathBuf::from(value))) } Some(_) => Err(USAGE.to_owned()), None => Ok(None), } } fn take_flag(args: &mut Vec, name: &str) -> bool { let before = args.len(); args.retain(|a| a != name); args.len() != before }