diff --git a/docs/launcher-commands.md b/docs/launcher-commands.md index a71df4257..dc4179468 100644 --- a/docs/launcher-commands.md +++ b/docs/launcher-commands.md @@ -331,6 +331,19 @@ Keep an instance on one installed Horizon version, or "latest" to follow updates MCP: `instance_version`. +## `horizon-launcher instance export` + +Export an instance (or a port) to share: its settings, packs, Horizon mods and the worlds named, as a .mrpack (resource packs from Modrinth listed by download) or a zip. Never logs, screenshots or account data; worlds go without their per-player files. + + horizon-launcher instance export [INSTANCE] + +- `INSTANCE`: The instance (default: the selected one) +- `--format` (mrpack, zip) +- `--world`: A world to put in it (its name or folder); repeat for more +- `--out`: Where to write it (default: ".mrpack" in the current folder) + +MCP: `instance_export`. + ## `horizon-launcher worlds list` The worlds: name, mode, version, last played and size (the default). diff --git a/launcher/src/browser/modrinth.rs b/launcher/src/browser/modrinth.rs index 7e821abaf..d62e2a26d 100644 --- a/launcher/src/browser/modrinth.rs +++ b/launcher/src/browser/modrinth.rs @@ -172,6 +172,14 @@ pub(super) fn parse_version(v: &Value) -> Option { }) } +/// The files of one version (`GET /version/{id}`), with their downloads and checksums. +pub fn version_files(version: &str) -> Result, String> { + let v = get(&format!("{API}/version/{}", encode(version)))?; + parse_version(&v) + .map(|v| v.files) + .ok_or_else(|| format!("Modrinth: no version {version}.")) +} + impl Source for Modrinth { fn id(&self) -> SourceId { SourceId::Modrinth diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index 739db4234..a2be8f7ed 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -183,6 +183,29 @@ enum InstanceCmd { Select { instance: String }, /// Keep an instance on one installed Horizon version, or "latest" to follow updates again Version { instance: String, version: String }, + /// Export an instance (or a port) to share: its settings, packs, Horizon mods and the worlds + /// named, as a .mrpack (resource packs from Modrinth listed by download) or a zip. Never + /// logs, screenshots or account data; worlds go without their per-player files + Export { + /// The instance (default: the selected one) + instance: Option, + #[arg(long, value_enum, default_value_t = ExportFormat::Mrpack)] + format: ExportFormat, + /// A world to put in it (its name or folder); repeat for more + #[arg(long)] + world: Vec, + /// Where to write it (default: ".mrpack" in the current folder) + #[arg(long)] + out: Option, + }, +} + +#[derive(Clone, Copy, clap::ValueEnum)] +enum ExportFormat { + /// Modrinth's modpack format + Mrpack, + /// The game folder's layout, zipped + Zip, } #[derive(Subcommand)] @@ -1742,6 +1765,57 @@ fn instance(action: InstanceCmd, as_json: bool) -> Result<(), String> { }; changed(instances::edit(&instance, |m| m.version = pin)?) } + InstanceCmd::Export { + instance, + format, + world, + out, + } => { + use horizon_launcher::export::{self, Format}; + let i = match instance { + Some(name) => instances::find(&name)?, + None => instances::selected(), + }; + let format = match format { + ExportFormat::Mrpack => Format::Mrpack, + ExportFormat::Zip => Format::Zip, + }; + let worlds = world + .iter() + .map(|w| horizon_launcher::worlds::find(&i.home, w).map(|w| w.folder)) + .collect::, _>>()?; + let linked = match format { + Format::Mrpack => export::linkable(&i.home, &export::from_modrinth), + Format::Zip => Vec::new(), + }; + let out = match out { + Some(out) => out, + None => export::free_path( + &std::env::current_dir().map_err(|e| e.to_string())?, + &i.name, + format, + ), + }; + let done = export::export(&i.home, &i.name, format, &worlds, &linked, &out)?; + if as_json { + json(serde_json::json!({ + "path": done.path, "copied": done.copied, "linked": done.linked, + "worlds": done.worlds, + })); + } else { + println!( + "Exported {} to {}: {} files, {} listed by their Modrinth download{}.", + i.name, + done.path.display(), + done.copied, + done.linked, + match done.worlds.is_empty() { + true => String::new(), + false => format!(", worlds: {}", done.worlds.join(", ")), + } + ); + } + } } Ok(()) } diff --git a/launcher/src/export.rs b/launcher/src/export.rs new file mode 100644 index 000000000..c1a1b9e45 --- /dev/null +++ b/launcher/src/export.rs @@ -0,0 +1,286 @@ +//! Sharing an instance (or a port): its settings, packs and Horizon mods, and the worlds the +//! player picks, as a `.mrpack` (Modrinth's modpack format, from its published spec) or a plain +//! `.zip` of the game folder's layout. +//! +//! Only what makes the instance goes in, from a fixed list ([`FILES`], [`FOLDERS`]): never logs, +//! screenshots, backups, caches, or anything tied to the player's account. A world goes without +//! its per-player files (`playerdata/`, `stats/`, `advancements/`) and its level.dat without the +//! player's UUID. +//! +//! In a `.mrpack`, a resource pack the browser installed from Modrinth, still the same file, is +//! listed by its download (`files`, with its SHA-1 and SHA-512) instead of being copied; the +//! rest goes under `overrides/`, which a launcher copies into the game folder. +use crate::browser::installed::{Entry, Installed}; +use crate::browser::{FileRef, Kind, SourceId, GAME}; +use horizon_world::nbt::{self, Limits, Tag}; +use sha2::Digest; +use std::fs; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; + +/// The game folder's own files that are the instance's settings. +pub const FILES: [&str; 6] = [ + "options.txt", + "horizon-graphics.txt", + "horizon-extras.txt", + "horizon-controller.txt", + "horizon-mods.txt", + "servers.dat", +]; +/// Its folders of packs, mods and their settings. +pub const FOLDERS: [&str; 4] = ["resourcepacks", "shaderpacks", "horizon-mods", "config"]; +/// What a world keeps per player: left out. +const PER_PLAYER: [&str; 4] = ["playerdata", "stats", "advancements", "session.lock"]; +const INDEX: &str = "modrinth.index.json"; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Format { + Mrpack, + Zip, +} + +impl Format { + pub fn extension(self) -> &'static str { + match self { + Format::Mrpack => "mrpack", + Format::Zip => "zip", + } + } +} + +/// A file of the instance that its source serves, listed in a `.mrpack` rather than copied. +#[derive(Clone, Debug, PartialEq)] +pub struct Linked { + /// Relative to the game folder: `resourcepacks/`. + pub path: String, + pub file: FileRef, +} + +/// What an export wrote. +#[derive(Debug, PartialEq)] +pub struct Exported { + pub path: PathBuf, + /// Files copied in (overrides, for a `.mrpack`). + pub copied: usize, + /// Files listed by their download. + pub linked: usize, + /// The worlds in it, by folder. + pub worlds: Vec, +} + +/// Writes the instance whose game folder is `home` to `out`. `worlds` are folders of its +/// `saves/`; `linked` comes from [`linkable`] (empty for a zip). +pub fn export( + home: &Path, + name: &str, + format: Format, + worlds: &[String], + linked: &[Linked], + out: &Path, +) -> Result { + let mut files: Vec<(String, PathBuf)> = Vec::new(); + for f in FILES { + if home.join(f).is_file() { + files.push((f.to_string(), home.join(f))); + } + } + for folder in FOLDERS { + walk(home, &home.join(folder), &[], &mut files); + } + for world in worlds { + let dir = home.join("saves").join(world); + if world.contains(['/', '\\']) || !dir.join("level.dat").is_file() { + return Err(format!("{name} has no world {world}.")); + } + walk(home, &dir, &PER_PLAYER, &mut files); + } + let linked: Vec<&Linked> = match format { + Format::Mrpack => linked.iter().collect(), + Format::Zip => Vec::new(), + }; + files.retain(|(rel, _)| !linked.iter().any(|l| l.path == *rel)); + if files.is_empty() && linked.is_empty() { + return Err(format!( + "{name} has nothing to share yet: no settings, packs, mods or worlds." + )); + } + if let Some(dir) = out.parent() { + fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; + } + let part = out.with_extension("part"); + let written = write(&part, name, format, &files, &linked); + let done = written.and_then(|()| fs::rename(&part, out).map_err(|e| e.to_string())); + if let Err(e) = done { + let _ = fs::remove_file(&part); + return Err(format!("Could not export {name}: {e}")); + } + Ok(Exported { + path: out.to_path_buf(), + copied: files.len(), + linked: linked.len(), + worlds: worlds.to_vec(), + }) +} + +fn write( + path: &Path, + name: &str, + format: Format, + files: &[(String, PathBuf)], + linked: &[&Linked], +) -> Result<(), String> { + let mut zip = zip::ZipWriter::new(fs::File::create(path).map_err(|e| e.to_string())?); + let options = zip::write::SimpleFileOptions::default(); + let prefix = match format { + Format::Mrpack => { + zip.start_file(INDEX, options).map_err(|e| e.to_string())?; + let index = serde_json::to_vec_pretty(&index(name, linked)).unwrap(); + zip.write_all(&index).map_err(|e| e.to_string())?; + "overrides/" + } + Format::Zip => "", + }; + for (rel, file) in files { + let bytes = match rel.ends_with("/level.dat") { + true => shareable_level(file)?, + false => fs::read(file).map_err(|e| format!("{}: {e}", file.display()))?, + }; + zip.start_file(format!("{prefix}{rel}"), options) + .map_err(|e| e.to_string())?; + zip.write_all(&bytes).map_err(|e| e.to_string())?; + } + zip.finish().map_err(|e| e.to_string())?; + Ok(()) +} + +/// `modrinth.index.json`: format 1, the files by their downloads, and the Minecraft version. +fn index(name: &str, linked: &[&Linked]) -> serde_json::Value { + let files: Vec = linked + .iter() + .map(|l| { + serde_json::json!({ + "path": l.path, + "hashes": { "sha1": l.file.sha1, "sha512": l.file.sha512 }, + "env": { "client": "required", "server": "unsupported" }, + "downloads": [l.file.url], + "fileSize": l.file.size, + }) + }) + .collect(); + serde_json::json!({ + "formatVersion": 1, + "game": "minecraft", + "versionId": "1", + "name": name, + "summary": format!("{name}, exported from Horizon."), + "files": files, + "dependencies": { "minecraft": GAME }, + }) +} + +/// Every file under `dir`, relative to `home` with `/`, leaving out the names in `skip` at +/// `dir`'s top and dot files. +fn walk(home: &Path, dir: &Path, skip: &[&str], out: &mut Vec<(String, PathBuf)>) { + let Ok(entries) = fs::read_dir(dir) else { + return; + }; + let mut entries: Vec<_> = entries.flatten().collect(); + entries.sort_by_key(|e| e.file_name()); + for e in entries { + let name = e.file_name().to_string_lossy().into_owned(); + if name.starts_with('.') || skip.contains(&name.as_str()) { + continue; + } + let path = e.path(); + if path.is_dir() { + walk(home, &path, &[], out); + } else if let Ok(rel) = path.strip_prefix(home) { + out.push((rel.to_string_lossy().replace('\\', "/"), path)); + } + } +} + +/// A world's level.dat without the player's UUID; unreadable, it goes as it is. +fn shareable_level(file: &Path) -> Result, String> { + let bytes = fs::read(file).map_err(|e| format!("{}: {e}", file.display()))?; + let mut raw = Vec::new(); + if flate2::read::GzDecoder::new(&bytes[..]) + .take(64 << 20) + .read_to_end(&mut raw) + .is_err() + { + return Ok(bytes); + } + let Ok((mut root, _)) = nbt::read_named(&raw, Limits::default()) else { + return Ok(bytes); + }; + if let Some(Tag::Compound(data)) = root.tag.get_mut("Data") { + if let Some(Tag::Compound(player)) = data.get_mut(&"Player".into()) { + player.remove(&"UUID".into()); + } + } + let raw = nbt::write_named(&root, Limits::default()).map_err(|e| e.to_string())?; + let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); + gz.write_all(&raw).map_err(|e| e.to_string())?; + gz.finish().map_err(|e| e.to_string()) +} + +/// The resource packs the browser installed from Modrinth that are still the file it checked: +/// with `lookup` (the source's file for an entry), what a `.mrpack` can list by download. +pub fn linkable(home: &Path, lookup: &dyn Fn(&Entry) -> Option) -> Vec { + Installed::load(home) + .entries + .iter() + .filter(|e| e.source == SourceId::Modrinth && e.kind == Kind::ResourcePack) + .filter(|e| { + let Ok(bytes) = fs::read(home.join(&e.file)) else { + return false; + }; + let sum: String = sha2::Sha512::digest(&bytes) + .iter() + .map(|b| format!("{b:02x}")) + .collect(); + e.checksum.strip_prefix("SHA-512:") == Some(sum.as_str()) + }) + .filter_map(|e| { + let file = lookup(e)?; + let same = file.sha512.as_deref() == e.checksum.strip_prefix("SHA-512:"); + (same && file.sha1.is_some()).then(|| Linked { + path: e.file.clone(), + file, + }) + }) + .collect() +} + +/// An entry's file as Modrinth serves it (asks Modrinth: for [`linkable`]). +pub fn from_modrinth(e: &Entry) -> Option { + let sum = e.checksum.strip_prefix("SHA-512:")?; + crate::browser::modrinth::version_files(&e.version) + .ok()? + .into_iter() + .find(|f| f.sha512.as_deref() == Some(sum)) +} + +/// A free path for `name.` in `dir`: "Name (2).mrpack" when taken. +pub fn free_path(dir: &Path, name: &str, format: Format) -> PathBuf { + let ext = format.extension(); + let clean: String = name + .chars() + .map(|c| match c { + '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '-', + c => c, + }) + .collect(); + (1..) + .map(|n| match n { + 1 => dir.join(format!("{clean}.{ext}")), + n => dir.join(format!("{clean} ({n}).{ext}")), + }) + .find(|p| !p.exists()) + .unwrap() +} + +#[cfg(test)] +#[path = "export/tests.rs"] +mod tests; diff --git a/launcher/src/export/tests.rs b/launcher/src/export/tests.rs new file mode 100644 index 000000000..d58cb4c4e --- /dev/null +++ b/launcher/src/export/tests.rs @@ -0,0 +1,211 @@ +use super::*; +use std::collections::BTreeMap; + +fn scratch(name: &str) -> PathBuf { + let root = std::env::temp_dir().join(format!( + "horizon-launcher-export-{name}-{}", + std::process::id() + )); + let _ = fs::remove_dir_all(&root); + fs::create_dir_all(&root).unwrap(); + root +} + +fn put(path: &Path, bytes: &[u8]) { + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, bytes).unwrap(); +} + +/// A gzipped level.dat whose player has a UUID. +fn level_dat() -> Vec { + let mut player = BTreeMap::new(); + player.insert("UUID".into(), Tag::IntArray(vec![1, 2, 3, 4])); + player.insert("XpLevel".into(), Tag::Int(7)); + let mut data = BTreeMap::new(); + data.insert("LevelName".into(), Tag::String("Base".into())); + data.insert("Player".into(), Tag::Compound(player)); + let mut root = BTreeMap::new(); + root.insert("Data".into(), Tag::Compound(data)); + let raw = nbt::write_named( + &nbt::NamedTag { + name: "".into(), + tag: Tag::Compound(root), + }, + Limits::default(), + ) + .unwrap(); + let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); + gz.write_all(&raw).unwrap(); + gz.finish().unwrap() +} + +fn sha512(bytes: &[u8]) -> String { + sha2::Sha512::digest(bytes) + .iter() + .map(|b| format!("{b:02x}")) + .collect() +} + +/// A game folder with settings, packs, a mod, two worlds, and what never goes out. +fn home(name: &str) -> PathBuf { + let home = scratch(name); + put(&home.join("options.txt"), b"lang:fr_ch\n"); + put(&home.join("horizon-graphics.txt"), b"preset:high\n"); + put(&home.join("servers.dat"), b"\x0a\x00\x00\x00"); + put(&home.join("resourcepacks/Glass.zip"), b"glass pack"); + put(&home.join("resourcepacks/Own.zip"), b"own pack"); + put( + &home.join("horizon-mods/coords/horizon-mod.toml"), + b"id = \"coords\"\n", + ); + put(&home.join("saves/Base/level.dat"), &level_dat()); + put(&home.join("saves/Base/region/r.0.0.mca"), b"region"); + put(&home.join("saves/Base/playerdata/0001.dat"), b"me"); + put(&home.join("saves/Base/stats/0001.json"), b"{}"); + put(&home.join("saves/Base/session.lock"), b""); + put(&home.join("saves/Other/level.dat"), &level_dat()); + for private in [ + "logs/latest.log", + "screenshots/a.png", + "backups/b.zip", + "mod-browser/checks-1/x/check.json", + "horizon-player.txt", + "session.json", + "usercache.json", + "resourcepacks/.DS_Store", + ] { + put(&home.join(private), b"private"); + } + let entry = serde_json::json!({ "entries": [{ + "source": "modrinth", "project": "P1", "slug": "glass", "title": "Glass", + "kind": "resource-pack", "version": "V1", "number": "1.0", + "file": "resourcepacks/Glass.zip", + "checksum": format!("SHA-512:{}", sha512(b"glass pack")), + "page": "https://modrinth.com/resourcepack/glass", + }]}); + put( + &home.join("mod-browser.json"), + &serde_json::to_vec(&entry).unwrap(), + ); + home +} + +fn names(zip: &Path) -> Vec { + let zip = zip::ZipArchive::new(fs::File::open(zip).unwrap()).unwrap(); + let mut names: Vec = zip.file_names().map(str::to_string).collect(); + names.sort(); + names +} + +fn read(zip: &Path, name: &str) -> Vec { + let mut zip = zip::ZipArchive::new(fs::File::open(zip).unwrap()).unwrap(); + let mut out = Vec::new(); + zip.by_name(name).unwrap().read_to_end(&mut out).unwrap(); + out +} + +fn served(e: &Entry) -> Option { + (e.version == "V1").then(|| FileRef { + url: "https://cdn.modrinth.com/data/P1/versions/V1/Glass.zip".into(), + name: "Glass.zip".into(), + size: 10, + sha512: Some(sha512(b"glass pack")), + sha256: None, + sha1: Some("ab".repeat(20)), + primary: true, + }) +} + +#[test] +fn a_zip_has_the_instance_and_the_picked_world_and_nothing_private() { + let home = home("zip"); + let out = home.join("out/Hardcore.zip"); + let done = export(&home, "Hardcore", Format::Zip, &["Base".into()], &[], &out).unwrap(); + assert_eq!((done.copied, done.linked), (8, 0)); + // Settings, packs, the mod and the picked world, without its per-player files; no logs, + // screenshots, backups, caches, account files or the other world. + assert_eq!( + names(&out), + [ + "horizon-graphics.txt", + "horizon-mods/coords/horizon-mod.toml", + "options.txt", + "resourcepacks/Glass.zip", + "resourcepacks/Own.zip", + "saves/Base/level.dat", + "saves/Base/region/r.0.0.mca", + "servers.dat", + ] + ); + // The world's level.dat keeps the player, without the UUID. + let mut raw = Vec::new(); + flate2::read::GzDecoder::new(&read(&out, "saves/Base/level.dat")[..]) + .read_to_end(&mut raw) + .unwrap(); + let (root, _) = nbt::read_named(&raw, Limits::default()).unwrap(); + let Some(Tag::Compound(data)) = root.tag.get("Data") else { + panic!("no Data") + }; + let Some(Tag::Compound(player)) = data.get(&"Player".into()) else { + panic!("no Player") + }; + assert!(player.get(&"UUID".into()).is_none()); + assert_eq!(player.get(&"XpLevel".into()), Some(&Tag::Int(7))); + assert!(!out.with_extension("part").exists()); + + assert!(export(&home, "Hardcore", Format::Zip, &["../x".into()], &[], &out).is_err()); + let empty = scratch("empty"); + let refused = export(&empty, "New", Format::Zip, &[], &[], &empty.join("New.zip")); + assert_eq!( + refused.unwrap_err(), + "New has nothing to share yet: no settings, packs, mods or worlds." + ); + assert!(!empty.join("New.zip").exists()); + fs::remove_dir_all(&empty).unwrap(); + fs::remove_dir_all(&home).unwrap(); +} + +#[test] +fn an_mrpack_lists_what_modrinth_serves_and_carries_the_rest() { + let home = home("mrpack"); + let linked = linkable(&home, &served); + assert_eq!(linked.len(), 1); + let out = free_path(&home, "Hard: core", Format::Mrpack); + assert_eq!(out.file_name().unwrap(), "Hard- core.mrpack"); + let done = export(&home, "Hardcore", Format::Mrpack, &[], &linked, &out).unwrap(); + assert_eq!((done.copied, done.linked), (5, 1)); + assert_eq!( + names(&out), + [ + "modrinth.index.json", + "overrides/horizon-graphics.txt", + "overrides/horizon-mods/coords/horizon-mod.toml", + "overrides/options.txt", + "overrides/resourcepacks/Own.zip", + "overrides/servers.dat", + ] + ); + let index: serde_json::Value = + serde_json::from_slice(&read(&out, "modrinth.index.json")).unwrap(); + assert_eq!(index["formatVersion"], 1); + assert_eq!(index["game"], "minecraft"); + assert_eq!(index["dependencies"]["minecraft"], GAME); + let file = &index["files"][0]; + assert_eq!(file["path"], "resourcepacks/Glass.zip"); + assert_eq!( + file["downloads"][0], + "https://cdn.modrinth.com/data/P1/versions/V1/Glass.zip" + ); + assert_eq!(file["hashes"]["sha512"], sha512(b"glass pack")); + assert_eq!( + free_path(&home, "Hard: core", Format::Mrpack) + .file_name() + .unwrap(), + "Hard- core (2).mrpack" + ); + + // A pack changed since its install is carried, not linked. + put(&home.join("resourcepacks/Glass.zip"), b"edited"); + assert!(linkable(&home, &served).is_empty()); + fs::remove_dir_all(&home).unwrap(); +} diff --git a/launcher/src/lib.rs b/launcher/src/lib.rs index 466c9330d..b8ff41253 100644 --- a/launcher/src/lib.rs +++ b/launcher/src/lib.rs @@ -6,6 +6,7 @@ pub mod browser; pub mod datapacks; pub mod desktop; pub mod desktop_seams; +pub mod export; #[cfg(unix)] pub mod game_app; pub mod heads;