3a474c2060
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
257 lines
8.7 KiB
Rust
257 lines
8.7 KiB
Rust
//! Modpacks: import a Java modpack (a Modrinth `.mrpack`, a CurseForge zip, an instance folder),
|
|
//! show what Horizon runs of it, port that on this computer from the player's own downloads, and
|
|
//! play it as a profile of its own (docs/modpacks.md).
|
|
//!
|
|
//! Each pack lives in `ports/<slug>/` in the game's home: the work folder `horizon-port-pack`
|
|
//! writes (pack.json, inventory, the jars, the carried-over mods, the coverage report) and
|
|
//! `home/`, the profile's own saves and options. Nothing is uploaded or shared: mod jars are
|
|
//! often All Rights Reserved, so the ports stay on the player's machine.
|
|
use crate::backend;
|
|
use horizon_port_pack::{
|
|
fetch::{Fetched, How},
|
|
import::Pack,
|
|
pipeline::{self, Log},
|
|
report::{Row, Status},
|
|
};
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
|
|
/// Where imported packs live.
|
|
pub fn ports_dir() -> PathBuf {
|
|
backend::game_home().join("ports")
|
|
}
|
|
|
|
/// A pack's folder name: its name in lowercase words joined by dashes.
|
|
pub fn slug(name: &str) -> String {
|
|
let words: Vec<String> = name
|
|
.split(|c: char| !c.is_ascii_alphanumeric())
|
|
.filter(|w| !w.is_empty())
|
|
.map(str::to_ascii_lowercase)
|
|
.collect();
|
|
if words.is_empty() {
|
|
"pack".into()
|
|
} else {
|
|
words.join("-")
|
|
}
|
|
}
|
|
|
|
/// An imported pack, as its work folder has it.
|
|
pub struct Imported {
|
|
pub slug: String,
|
|
pub work: PathBuf,
|
|
pub pack: Pack,
|
|
pub rows: Vec<Row>,
|
|
pub fetched: Fetched,
|
|
/// The carried-over mods the profile loads.
|
|
pub mods: Vec<PathBuf>,
|
|
/// The mods the port step gets and carries over (content, data tier).
|
|
pub portable: usize,
|
|
}
|
|
|
|
impl Imported {
|
|
pub fn title(&self) -> &str {
|
|
if self.pack.name.is_empty() {
|
|
&self.slug
|
|
} else {
|
|
&self.pack.name
|
|
}
|
|
}
|
|
pub fn count(&self, status: Status) -> usize {
|
|
self.rows.iter().filter(|r| r.status == status).count()
|
|
}
|
|
/// "Minecraft 1.21.1 · neoforge 21.1.259 · 496 mods".
|
|
pub fn summary(&self) -> String {
|
|
let mut parts = Vec::new();
|
|
if !self.pack.minecraft.is_empty() {
|
|
parts.push(format!("Minecraft {}", self.pack.minecraft));
|
|
}
|
|
if !self.pack.loader.is_empty() {
|
|
parts.push(format!("{} {}", self.pack.loader, self.pack.loader_version));
|
|
}
|
|
parts.push(format!("{} files", self.pack.files.len()));
|
|
parts.join(" · ")
|
|
}
|
|
/// What getting the jars did, in one line, or None before it ran.
|
|
pub fn jars(&self) -> Option<String> {
|
|
if self.fetched.files.is_empty() {
|
|
return None;
|
|
}
|
|
let mut parts = Vec::new();
|
|
for (how, what) in [
|
|
(How::Downloaded, "downloaded"),
|
|
(How::Local, "copied from your folder"),
|
|
(How::Present, "already here"),
|
|
(
|
|
How::NeedsKey,
|
|
"only on CurseForge (needs its API key: choose your installed instance instead)",
|
|
),
|
|
(How::NotFound, "not found"),
|
|
(How::Failed, "failed"),
|
|
] {
|
|
let n = self.fetched.count(how);
|
|
if n > 0 {
|
|
parts.push(format!("{n} {what}"));
|
|
}
|
|
}
|
|
Some(format!("Jars: {}.", parts.join(", ")))
|
|
}
|
|
}
|
|
|
|
/// Reads the pack in `work`.
|
|
pub fn load(work: &Path) -> Result<Imported, String> {
|
|
let pack: Pack = pipeline::read_json(&work.join("pack.json"))?;
|
|
let rows: Vec<Row> = pipeline::read_json(&work.join("coverage.json")).unwrap_or_default();
|
|
let records: Vec<horizon_port_pack::inventory::Record> =
|
|
pipeline::read_json(&work.join("inventory.json")).unwrap_or_default();
|
|
Ok(Imported {
|
|
slug: work
|
|
.file_name()
|
|
.map(|n| n.to_string_lossy().into_owned())
|
|
.unwrap_or_default(),
|
|
work: work.to_path_buf(),
|
|
pack,
|
|
rows,
|
|
fetched: Fetched::load(work),
|
|
mods: carried(work),
|
|
portable: records.iter().filter(|r| pipeline::portable(r)).count(),
|
|
})
|
|
}
|
|
|
|
/// Every imported pack, by name.
|
|
pub fn list() -> Vec<Imported> {
|
|
let mut packs: Vec<Imported> = fs::read_dir(ports_dir())
|
|
.into_iter()
|
|
.flatten()
|
|
.flatten()
|
|
.filter_map(|e| load(&e.path()).ok())
|
|
.collect();
|
|
packs.sort_by_key(|p| p.title().to_ascii_lowercase());
|
|
packs
|
|
}
|
|
|
|
/// The carried-over mods in a work folder (`mods/<id>/carry/` with its manifest).
|
|
pub fn carried(work: &Path) -> Vec<PathBuf> {
|
|
let mut mods: Vec<PathBuf> = fs::read_dir(work.join("mods"))
|
|
.into_iter()
|
|
.flatten()
|
|
.flatten()
|
|
.map(|e| e.path().join("carry"))
|
|
.filter(|c| c.join("horizon-mod.toml").is_file())
|
|
.collect();
|
|
mods.sort();
|
|
mods
|
|
}
|
|
|
|
/// Imports the pack at `path` into `ports/<slug>/` and writes its coverage report from
|
|
/// metadata (Modrinth answers by file hash; nothing is downloaded yet). Importing the same pack
|
|
/// again keeps the jars and ports it already has.
|
|
pub fn import(path: &Path, log: Log) -> Result<Imported, String> {
|
|
let pack = horizon_port_pack::import::import(path)?;
|
|
let name = if pack.name.is_empty() || pack.name == "modlist" {
|
|
path.file_stem()
|
|
.map(|s| s.to_string_lossy().into_owned())
|
|
.unwrap_or_default()
|
|
} else {
|
|
pack.name.clone()
|
|
};
|
|
let work = ports_dir().join(slug(&name));
|
|
log(format!("Importing {name} into {}", work.display()));
|
|
pipeline::import(path, &work, log)?;
|
|
pipeline::inventory(&work, false, log)?;
|
|
pipeline::report(&work, log)?;
|
|
load(&work)
|
|
}
|
|
|
|
/// Ports what this computer can: the portable mods' jars (from `from` when the player has them,
|
|
/// else the pack's links or Modrinth), carried over for `resources`, then the report again.
|
|
pub fn port(
|
|
work: &Path,
|
|
resources: Option<&Path>,
|
|
from: Option<&Path>,
|
|
log: Log,
|
|
) -> Result<Imported, String> {
|
|
pipeline::port(work, resources, from, log)?;
|
|
load(work)
|
|
}
|
|
|
|
/// Removes an imported pack: its jars, ports and the profile's saves.
|
|
pub fn remove(work: &Path) -> Result<(), String> {
|
|
if work.parent() != Some(ports_dir().as_path()) {
|
|
return Err(format!("{} is not an imported pack", work.display()));
|
|
}
|
|
fs::remove_dir_all(work).map_err(|e| format!("{}: {e}", work.display()))
|
|
}
|
|
|
|
/// The profile's own game home: saves, options.txt, logs.
|
|
pub fn profile_home(work: &Path) -> PathBuf {
|
|
work.join("home")
|
|
}
|
|
|
|
/// Turns the game's command into this pack's profile: its own home (the player's options.txt
|
|
/// copied in the first time, so keys and video settings carry over) and its carried-over mods.
|
|
pub fn apply_profile(cmd: &mut Command, work: &Path) -> Result<(), String> {
|
|
let mods = carried(work);
|
|
if mods.is_empty() {
|
|
return Err("Nothing is ported yet: port the pack first.".into());
|
|
}
|
|
let home = profile_home(work);
|
|
fs::create_dir_all(&home).map_err(|e| format!("{}: {e}", home.display()))?;
|
|
let options = home.join("options.txt");
|
|
let own = backend::game_home().join("options.txt");
|
|
if !options.exists() && own.is_file() {
|
|
let _ = fs::copy(own, &options);
|
|
}
|
|
cmd.current_dir(&home).env("HORIZON_HOME", &home);
|
|
for m in mods {
|
|
cmd.arg("--dev-mod").arg(m);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn slugs() {
|
|
assert_eq!(slug("All the Mods 10"), "all-the-mods-10");
|
|
assert_eq!(slug("Create: Above & Beyond!"), "create-above-beyond");
|
|
assert_eq!(slug(" "), "pack");
|
|
}
|
|
|
|
#[test]
|
|
fn a_profile_has_its_own_home_and_mods() {
|
|
let root =
|
|
std::env::temp_dir().join(format!("horizon-launcher-pack-{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&root);
|
|
let work = root.join("ports/small");
|
|
let mut cmd = Command::new("true");
|
|
assert!(apply_profile(&mut cmd, &work).is_err());
|
|
for id in ["a", "b"] {
|
|
let carry = work.join("mods").join(id).join("carry");
|
|
fs::create_dir_all(&carry).unwrap();
|
|
fs::write(carry.join("horizon-mod.toml"), "").unwrap();
|
|
}
|
|
// Left out: no manifest.
|
|
fs::create_dir_all(work.join("mods/c/carry")).unwrap();
|
|
apply_profile(&mut cmd, &work).unwrap();
|
|
let args: Vec<String> = cmd
|
|
.get_args()
|
|
.map(|a| a.to_string_lossy().into_owned())
|
|
.collect();
|
|
assert_eq!(args.len(), 4);
|
|
assert_eq!(
|
|
Path::new(&args[1]),
|
|
work.join("mods").join("a").join("carry")
|
|
);
|
|
assert_eq!(
|
|
Path::new(&args[3]),
|
|
work.join("mods").join("b").join("carry")
|
|
);
|
|
assert_eq!(cmd.get_current_dir(), Some(profile_home(&work).as_path()));
|
|
assert!(profile_home(&work).is_dir());
|
|
fs::remove_dir_all(&root).unwrap();
|
|
}
|
|
}
|