Launcher: instances, each with its own game folder
The game's folder stays the default instance, "Horizon", where it is; imported and registry modpacks are listed as instances in their own folders; new ones go to instances/<slug>/ in the game's folder, empty (the player's options and offline name) or a copy of another (never sessions, logs or telemetry). Names, icons and groups live in the launcher's instances.json, so nothing moves. Play, `play --instance` and MCP play start the selected instance, which follows the launcher's data collection answer and No AI. Crash reports are read from the run's own folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
use super::*;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
fn scratch(name: &str) -> PathBuf {
|
||||
let root = std::env::temp_dir().join(format!(
|
||||
"horizon-launcher-instances-{name}-{}",
|
||||
std::process::id()
|
||||
));
|
||||
let _ = fs::remove_dir_all(&root);
|
||||
fs::create_dir_all(&root).unwrap();
|
||||
root
|
||||
}
|
||||
|
||||
fn put(path: &Path, text: &str) {
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, text).unwrap();
|
||||
}
|
||||
|
||||
/// Every file under `dir`, relative, with its bytes.
|
||||
fn files(dir: &Path) -> BTreeMap<PathBuf, Vec<u8>> {
|
||||
fn walk(root: &Path, dir: &Path, out: &mut BTreeMap<PathBuf, Vec<u8>>) {
|
||||
for e in fs::read_dir(dir).unwrap().flatten() {
|
||||
let p = e.path();
|
||||
if p.is_dir() {
|
||||
walk(root, &p, out);
|
||||
} else {
|
||||
out.insert(p.strip_prefix(root).unwrap().into(), fs::read(&p).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = BTreeMap::new();
|
||||
walk(dir, dir, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
/// A game home as a player has it before instances: worlds, options, packs, a session, an
|
||||
/// imported modpack and a registry modpack with their own worlds.
|
||||
fn player_home(home: &Path) {
|
||||
put(&home.join("saves/World/level.dat"), "world");
|
||||
put(&home.join("saves/World/region/r.0.0.mca"), "region");
|
||||
put(&home.join("saves/Other/level.dat"), "other");
|
||||
put(&home.join("options.txt"), "fov:90\n");
|
||||
put(&home.join("horizon-player.txt"), "name:Player123\n");
|
||||
put(&home.join("resourcepacks/faithful.zip"), "pack");
|
||||
put(&home.join("logs/latest.log"), "log");
|
||||
put(&home.join("sessions/a.json"), "token");
|
||||
put(&home.join("telemetry/consent.json"), "{}");
|
||||
let pack = horizon_port_pack::import::Pack {
|
||||
name: "Small Pack".into(),
|
||||
..Default::default()
|
||||
};
|
||||
put(
|
||||
&home.join("ports/small-pack/pack.json"),
|
||||
&serde_json::to_string(&pack).unwrap(),
|
||||
);
|
||||
put(
|
||||
&home.join("ports/small-pack/home/saves/Modded/level.dat"),
|
||||
"modded",
|
||||
);
|
||||
put(
|
||||
&home.join("horizon-packs/cozy/pack.json"),
|
||||
r#"{"id":"cozy","name":"Cozy","version":"1.0.0","sha256":"00","mods":[]}"#,
|
||||
);
|
||||
put(
|
||||
&home.join("horizon-packs/cozy/home/saves/Cozy/level.dat"),
|
||||
"cozy",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn existing_worlds_stay_where_they_are_and_every_folder_becomes_an_instance() {
|
||||
let root = scratch("migrate");
|
||||
let (home, data) = (root.join("horizon"), root.join("launcher"));
|
||||
player_home(&home);
|
||||
let before = files(&home);
|
||||
|
||||
// A launcher with instances, on a player's existing folders: no instances.json yet.
|
||||
let all = list_in(&home, &data);
|
||||
let ids: Vec<&str> = all.iter().map(|i| i.id.as_str()).collect();
|
||||
assert_eq!(ids, [DEFAULT, "pack:cozy", "port:small-pack"]);
|
||||
assert_eq!(all[0].name, "Horizon");
|
||||
assert_eq!(all[0].home, home);
|
||||
assert_eq!(all[1].home, home.join("horizon-packs/cozy/home"));
|
||||
assert_eq!(all[2].name, "Small Pack");
|
||||
assert_eq!(all[2].home, home.join("ports/small-pack/home"));
|
||||
|
||||
// Everything the player can do with instances, then not one byte of theirs has moved.
|
||||
let empty = create_in(&home, &data, "Survival", None).unwrap();
|
||||
let copy = create_in(&home, &data, "My copy", Some(&all[0])).unwrap();
|
||||
edit_in(&home, &data, DEFAULT, |m| m.name = Some("Main".into())).unwrap();
|
||||
edit_in(&home, &data, "Small Pack", |m| {
|
||||
m.icon = Some(Icon::Texture("diamond".into()));
|
||||
m.group = Some("Modded".into());
|
||||
})
|
||||
.unwrap();
|
||||
let after = files(&home);
|
||||
for (path, bytes) in &before {
|
||||
assert_eq!(after.get(path), Some(bytes), "{} changed", path.display());
|
||||
}
|
||||
assert!(!data.join("settings.json").exists());
|
||||
|
||||
// The empty one has only the player's options and name; the copy has the worlds and
|
||||
// packs, never the sessions, logs, data collection or other instances.
|
||||
assert_eq!(empty.home, home.join("instances/survival"));
|
||||
let empty_files: Vec<PathBuf> = files(&empty.home).into_keys().collect();
|
||||
assert_eq!(
|
||||
empty_files,
|
||||
[PathBuf::from("horizon-player.txt"), "options.txt".into()]
|
||||
);
|
||||
let copied = files(©.home);
|
||||
for kept in [
|
||||
"saves/World/region/r.0.0.mca",
|
||||
"saves/Other/level.dat",
|
||||
"resourcepacks/faithful.zip",
|
||||
] {
|
||||
assert_eq!(copied[Path::new(kept)], before[Path::new(kept)]);
|
||||
}
|
||||
assert!(copied.keys().all(|p| {
|
||||
let top = p.components().next().unwrap().as_os_str().to_str().unwrap();
|
||||
!NOT_COPIED.contains(&top)
|
||||
}));
|
||||
|
||||
let all = list_in(&home, &data);
|
||||
assert_eq!(all[0].name, "Main");
|
||||
let port = find_in(&all, "port:small-pack").unwrap();
|
||||
assert_eq!(port.group.as_deref(), Some("Modded"));
|
||||
assert_eq!(port.home, home.join("ports/small-pack/home"));
|
||||
fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn names_stay_unique_and_packs_are_not_copied() {
|
||||
let root = scratch("names");
|
||||
let (home, data) = (root.join("horizon"), root.join("launcher"));
|
||||
player_home(&home);
|
||||
create_in(&home, &data, "Survival", None).unwrap();
|
||||
assert!(create_in(&home, &data, "survival", None).is_err());
|
||||
assert!(create_in(&home, &data, " ", None).is_err());
|
||||
// Another name with the same folder name gets the next free folder.
|
||||
let second = create_in(&home, &data, "Survival!", None).unwrap();
|
||||
assert_eq!(second.id, "survival-2");
|
||||
assert_eq!(second.name, "Survival!");
|
||||
assert!(edit_in(&home, &data, "survival-2", |m| m.name =
|
||||
Some("SURVIVAL".into()))
|
||||
.is_err());
|
||||
let port = find_in(&list_in(&home, &data), "small-pack").unwrap();
|
||||
assert!(create_in(&home, &data, "Pack copy", Some(&port)).is_err());
|
||||
assert!(!home.join("instances/pack-copy").exists());
|
||||
// An empty group is no group.
|
||||
let grouped = edit_in(&home, &data, "Survival", |m| m.group = Some(" ".into())).unwrap();
|
||||
assert_eq!(grouped.group, None);
|
||||
fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_instance_follows_the_players_answers() {
|
||||
let root = scratch("follow");
|
||||
let (main, own) = (root.join("main"), root.join("own"));
|
||||
put(&main.join("options.txt"), "fov:90\n");
|
||||
put(&main.join("horizon-player.txt"), "name:Player123\n");
|
||||
put(&main.join("telemetry/consent.json"), r#"{"version":1}"#);
|
||||
horizon_core::no_ai::set(&main, true).unwrap();
|
||||
put(&own.join("options.txt"), "fov:70\n");
|
||||
follow_player(&main, &own);
|
||||
assert_eq!(
|
||||
fs::read_to_string(own.join("options.txt")).unwrap(),
|
||||
"fov:70\n"
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read_to_string(own.join("horizon-player.txt")).unwrap(),
|
||||
"name:Player123\n"
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read_to_string(own.join("telemetry/consent.json")).unwrap(),
|
||||
r#"{"version":1}"#
|
||||
);
|
||||
assert!(horizon_core::no_ai::is_on(&own));
|
||||
horizon_core::no_ai::set(&main, false).unwrap();
|
||||
follow_player(&main, &own);
|
||||
assert!(!horizon_core::no_ai::is_on(&own));
|
||||
fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn icons_are_a_texture_name_or_a_colour() {
|
||||
assert_eq!(Icon::parse("#3FA7D6"), Ok(Icon::Colour("#3fa7d6".into())));
|
||||
assert_eq!(
|
||||
Icon::parse("grass_block"),
|
||||
Ok(Icon::Texture("grass_block".into()))
|
||||
);
|
||||
for bad in ["#12345", "../x", "Grass", "", "#zzzzzz"] {
|
||||
assert!(Icon::parse(bad).is_err(), "{bad}");
|
||||
}
|
||||
let root = scratch("icon");
|
||||
put(
|
||||
&root.join("assets/minecraft/textures/item/diamond.png"),
|
||||
"png",
|
||||
);
|
||||
let diamond = Icon::Texture("diamond".into()).texture(&root).unwrap();
|
||||
assert!(diamond.ends_with("item/diamond.png"));
|
||||
assert_eq!(Icon::Texture("stone".into()).texture(&root), None);
|
||||
fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user