Launcher: Horizon's registry as a mod browser source

A browser source for Horizon's own registry (docs/api/mod-index.md): it reads index.json
(HORIZON_MOD_INDEX for a test registry, cached in the game folder for when the registry can't
be reached), searches it by kind and words, lists versions with the SHA-256 the index gives,
and blocks the versions it says were taken down. Downloads accept the registry's release host
and check SHA-256; file:// files only from a local index.

Installing, the takedown check before play and the CLI come next.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-24 22:47:37 +02:00
parent bffb68b0c5
commit 7e513d2d5c
11 changed files with 423 additions and 18 deletions
+9
View File
@@ -2673,6 +2673,7 @@ dependencies = [
"horizon-bench",
"horizon-core",
"horizon-friends",
"horizon-mods",
"horizon-port-pack",
"horizon-prepare",
"horizon-release-sign",
@@ -2698,8 +2699,10 @@ name = "horizon-mods"
version = "0.1.0"
dependencies = [
"log",
"minisign-verify",
"serde",
"serde_json",
"sha2 0.10.9",
"toml 1.1.8+spec-1.1.0",
"zip",
]
@@ -3470,6 +3473,12 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "minisign-verify"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "871285dc19d8d0ebe0eef3d0e99a205f2a71363b122632cbbfa4a6c370a960ce"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
+2
View File
@@ -31,6 +31,8 @@ horizon-friends = { path = "../crates/horizon-friends" }
horizon-bench = { path = "../crates/horizon-bench" }
# Import modpack: the porting pipeline, run on the player's own files (docs/modpacks.md).
horizon-port-pack = { path = "../crates/horizon-port-pack" }
# Horizon's mod registry: its index and the checks a download passes before install.
horizon-mods = { path = "../crates/horizon-mods", features = ["index"] }
# Signed updates: archives are checked against the release's signed checksums (docs/security.md).
horizon-release-sign = { path = "../crates/horizon-release-sign" }
# Opt-in data collection: the consent, the outbox and its upload (docs/telemetry.md).
+32 -8
View File
@@ -8,6 +8,7 @@
pub mod curseforge;
pub mod details;
pub mod download;
pub mod horizon;
pub mod images;
pub mod installed;
pub mod java;
@@ -56,6 +57,8 @@ impl Kind {
pub enum SourceId {
Modrinth,
CurseForge,
/// Horizon's own registry of reviewed mods, modpacks and resource packs ([`horizon`]).
Horizon,
}
impl SourceId {
@@ -63,6 +66,7 @@ impl SourceId {
match self {
SourceId::Modrinth => "Modrinth",
SourceId::CurseForge => "CurseForge",
SourceId::Horizon => "Horizon",
}
}
}
@@ -132,6 +136,8 @@ pub struct FileRef {
pub name: String,
pub size: u64,
pub sha512: Option<String>,
/// What Horizon's registry lists.
pub sha256: Option<String>,
pub sha1: Option<String>,
pub primary: bool,
}
@@ -139,11 +145,13 @@ pub struct FileRef {
impl FileRef {
/// The strongest checksum the source gives: what the download is checked against.
pub fn checksum(&self) -> Option<(&'static str, &str)> {
match (&self.sha512, &self.sha1) {
(Some(h), _) => Some(("SHA-512", h)),
(None, Some(h)) => Some(("SHA-1", h)),
(None, None) => None,
}
[
("SHA-512", &self.sha512),
("SHA-256", &self.sha256),
("SHA-1", &self.sha1),
]
.into_iter()
.find_map(|(algo, h)| Some((algo, h.as_deref()?)))
}
}
@@ -230,10 +238,11 @@ pub trait Source: Send + Sync {
/// What the launcher says wherever CurseForge would be.
pub const CURSEFORGE_SOON: &str = "CurseForge support is coming soon.";
/// The sources this launcher uses: Modrinth. CurseForge support is coming soon: its API needs a
/// paid key, so no search or install goes to it; `curseforge` is kept for then.
/// The sources this launcher uses: Modrinth and Horizon's registry. CurseForge support is coming
/// soon: its API needs a paid key, so no search or install goes to it; `curseforge` is kept for
/// then.
pub fn sources() -> Vec<Box<dyn Source>> {
vec![Box::new(modrinth::Modrinth)]
vec![Box::new(modrinth::Modrinth), Box::new(horizon::Registry)]
}
/// The source of that id, when this launcher can use it.
@@ -243,6 +252,15 @@ pub fn source(id: SourceId) -> Option<Box<dyn Source>> {
/// How well a project of `kind` made for `game_versions` and `loaders` runs in Horizon.
pub fn support(kind: Kind, game_versions: &[String], loaders: &[String]) -> Support {
if loaders.iter().any(|l| l == horizon::LOADER) {
// Made for Horizon and reviewed: it runs when it is for the version Horizon plays.
return match newest_release(game_versions)
.filter(|_| !game_versions.iter().any(|v| v == GAME))
{
Some(other) => Support::No(format!("Made for Horizon on {other}.")),
None => Support::Works,
};
}
match kind {
Kind::ResourcePack => {
if game_versions.iter().any(|v| v == GAME) {
@@ -309,6 +327,12 @@ pub fn pick(kind: Kind, versions: &[Version], everything: bool) -> Option<&Versi
let mut usable = versions
.iter()
.filter(|v| v.blocked.is_none() && v.file().is_some());
if versions
.iter()
.any(|v| v.loaders.iter().any(|l| l == horizon::LOADER))
{
return usable.find(|v| v.game_versions.iter().any(|g| g == GAME));
}
if kind == Kind::Mod {
let java = |v: &&Version| {
v.loaders
+1
View File
@@ -164,6 +164,7 @@ pub(super) fn parse_file(f: &Value) -> Option<Version> {
name: name.clone(),
size: f["fileLength"].as_u64().unwrap_or(0),
sha512: None,
sha256: None,
sha1,
primary: true,
}]
+32 -9
View File
@@ -1,16 +1,18 @@
//! Downloads that reach the game folder only when their checksum matches the source's.
use super::{safe_name, FileRef};
use sha1::Sha1;
use sha2::{Digest, Sha512};
use sha2::{Digest, Sha256, Sha512};
use std::fs;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
/// Hosts a file may come from: Modrinth's and CurseForge's file servers.
const HOSTS: [&str; 3] = [
/// Hosts a file may come from: Modrinth's and CurseForge's file servers, and the release assets
/// of Horizon's registry.
const HOSTS: [&str; 4] = [
"https://cdn.modrinth.com/",
"https://edge.forgecdn.net/",
"https://mediafilez.forgecdn.net/",
horizon_mods::index::FILE_HOST,
];
/// Bigger than any pack these sources serve (Modrinth caps uploads at 500 MiB).
const MAX: u64 = 1 << 30;
@@ -31,10 +33,15 @@ pub(super) fn agent() -> ureq::Agent {
pub fn refuse(file: &FileRef) -> Option<String> {
if !HOSTS.iter().any(|h| file.url.starts_with(h)) {
return Some(format!(
"{} isn't on Modrinth's or CurseForge's file servers.",
"{} isn't on Modrinth's, CurseForge's or Horizon's file servers.",
file.url
));
}
refuse_file(file)
}
/// [`refuse`] without the host: the name, the checksum and the size.
fn refuse_file(file: &FileRef) -> Option<String> {
if safe_name(&file.name).is_none() {
return Some(format!("{:?} isn't a plain file name.", file.name));
}
@@ -50,26 +57,29 @@ pub fn refuse(file: &FileRef) -> Option<String> {
/// Hashes as they are written: the strongest one the file lists.
enum Hasher {
Sha512(Sha512),
Sha256(Sha256),
Sha1(Sha1),
}
impl Hasher {
fn for_file(file: &FileRef) -> Self {
if file.sha512.is_some() {
Hasher::Sha512(Sha512::new())
} else {
Hasher::Sha1(Sha1::new())
match file.checksum() {
Some(("SHA-512", _)) => Hasher::Sha512(Sha512::new()),
Some(("SHA-256", _)) => Hasher::Sha256(Sha256::new()),
_ => Hasher::Sha1(Sha1::new()),
}
}
fn update(&mut self, bytes: &[u8]) {
match self {
Hasher::Sha512(h) => h.update(bytes),
Hasher::Sha256(h) => h.update(bytes),
Hasher::Sha1(h) => h.update(bytes),
}
}
fn hex(self) -> String {
let bytes: Vec<u8> = match self {
Hasher::Sha512(h) => h.finalize().to_vec(),
Hasher::Sha256(h) => h.finalize().to_vec(),
Hasher::Sha1(h) => h.finalize().to_vec(),
};
bytes.iter().map(|b| format!("{b:02x}")).collect()
@@ -81,13 +91,26 @@ impl Hasher {
/// `progress` gets the fraction done.
pub fn save(
file: &FileRef,
mut reader: impl Read,
reader: impl Read,
folder: &Path,
progress: &dyn Fn(f32),
) -> Result<PathBuf, String> {
if let Some(why) = refuse(file) {
return Err(why);
}
save_from_anywhere(file, reader, folder, progress)
}
/// [`save`] for a file that comes from no host: a local test registry's.
pub(super) fn save_from_anywhere(
file: &FileRef,
mut reader: impl Read,
folder: &Path,
progress: &dyn Fn(f32),
) -> Result<PathBuf, String> {
if let Some(why) = refuse_file(file) {
return Err(why);
}
let (algo, want) = file.checksum().expect("refuse checks for one");
fs::create_dir_all(folder).map_err(|e| format!("{}: {e}", folder.display()))?;
let target = folder.join(&file.name);
+280
View File
@@ -0,0 +1,280 @@
//! Horizon's own registry (docs/api/mod-index.md): mods, modpacks and resource packs made for
//! Horizon, reviewed as pull requests in ludovic111/horizon-mods. One file lists them all,
//! `index.json`: this is the browser source that searches it and downloads its files, checked
//! against the SHA-256 it lists. Versions the index says were taken down are blocked.
use super::{
details::{Details, Link},
download, FileRef, Kind, Project, Query, Source, SourceId, Version, GAME,
};
use horizon_mods::index::{self, Index};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
/// Where the registry publishes its index (GitHub Pages of ludovic111/horizon-mods).
pub const INDEX_URL: &str = "https://ludovic111.github.io/horizon-mods/index.json";
/// The registry's pages on the website, `<SITE>/<id>`.
const SITE: &str = "https://playhorizon.dev/mods";
/// The report form of a project.
const REPORT: &str = "https://github.com/ludovic111/horizon-mods/issues/new?template=report.yml";
/// The loader name the browser gives registry versions, so `support` and `pick` know them.
pub const LOADER: &str = "horizon";
/// How long a fetched index is used before it is fetched again.
const FRESH: Duration = Duration::from_secs(600);
/// Larger than any index should be.
const MAX_INDEX: u64 = 16 << 20;
/// The last index fetched, kept in the game folder for when the registry can't be reached.
const CACHE: &str = "mod-browser/horizon-index.json";
/// Where the index comes from: `HORIZON_MOD_INDEX` (a file or an `https://` address, for a
/// test registry), else [`INDEX_URL`].
fn location() -> String {
std::env::var("HORIZON_MOD_INDEX")
.ok()
.filter(|v| !v.is_empty())
.unwrap_or_else(|| INDEX_URL.to_owned())
}
/// True when the index is a local file: its files may then be local too (`file://`), for
/// trying a registry out before it is published.
pub fn local() -> bool {
!location().starts_with("https://")
}
static LOADED: Mutex<Option<(Instant, Arc<Index>)>> = Mutex::new(None);
/// The index: the one fetched in the last ten minutes, else fetched again (and kept in the game
/// folder), else the copy kept last time.
pub fn index() -> Result<Arc<Index>, String> {
if let Some((at, index)) = LOADED.lock().unwrap().as_ref() {
if at.elapsed() < FRESH {
return Ok(index.clone());
}
}
let cache = crate::backend::game_home().join(CACHE);
let index = match fetch_index() {
Ok((index, text)) => {
if let Some(dir) = cache.parent() {
let _ = fs::create_dir_all(dir);
}
let _ = fs::write(&cache, text);
index
}
Err(e) => {
let kept = fs::read_to_string(&cache).map_err(|_| e.clone())?;
match local() {
true => Index::parse_local(&kept),
false => Index::parse(&kept),
}
.map_err(|_| e)?
}
};
let index = Arc::new(index);
*LOADED.lock().unwrap() = Some((Instant::now(), index.clone()));
Ok(index)
}
fn fetch_index() -> Result<(Index, String), String> {
use std::io::Read;
let at = location();
let text = if at.starts_with("https://") {
let response = download::agent().get(&at).call().map_err(|e| match e {
ureq::Error::Status(code, _) => format!("The mod registry answered {code}."),
ureq::Error::Transport(t) => format!("The mod registry can't be reached: {t}"),
})?;
let mut text = String::new();
response
.into_reader()
.take(MAX_INDEX)
.read_to_string(&mut text)
.map_err(|e| e.to_string())?;
text
} else {
fs::read_to_string(&at).map_err(|e| format!("{at}: {e}"))?
};
let index = match local() {
true => Index::parse_local(&text)?,
false => Index::parse(&text)?,
};
Ok((index, text))
}
/// The browser's kind for a registry kind; None for one the launcher doesn't install yet
/// (Horizon modpacks and shader packs, kinds newer than this launcher).
fn kind_of(kind: &index::Kind) -> Option<Kind> {
match kind {
index::Kind::Mod => Some(Kind::Mod),
index::Kind::ResourcePack => Some(Kind::ResourcePack),
index::Kind::Modpack | index::Kind::Shader | index::Kind::Other(_) => None,
}
}
fn project_of(p: &index::Project) -> Option<Project> {
let mut game_versions: Vec<String> = p.versions.iter().map(|v| v.game.clone()).collect();
game_versions.dedup();
Some(Project {
source: SourceId::Horizon,
id: p.id.clone(),
slug: p.id.clone(),
title: p.name.clone(),
summary: p.summary.clone(),
author: p.authors.join(", "),
downloads: 0,
page: format!("{SITE}/{}", p.id),
icon: p.icon.clone(),
license: p.license.clone(),
kind: kind_of(&p.kind)?,
game_versions,
loaders: vec![LOADER.into()],
})
}
fn version_of(index: &Index, p: &index::Project, v: &index::Version) -> Version {
Version {
id: v.version.clone(),
number: v.version.clone(),
published: v.published.clone(),
game_versions: vec![v.game.clone()],
loaders: vec![LOADER.into()],
files: vec![FileRef {
url: v.file.url.clone(),
name: v.file.name.clone(),
size: v.file.size,
sha512: None,
sha256: Some(v.file.sha256.clone()),
sha1: None,
primary: true,
}],
required: v.requires.iter().map(|r| r.id.clone()).collect(),
blocked: index
.revoked(&p.id, &v.file.sha256)
.map(|r| format!("taken down: {}", r.reason)),
}
}
/// The words of a query found in a project's id, name, summary or authors.
fn matches(p: &index::Project, text: &str) -> bool {
let hay = format!("{} {} {} {}", p.id, p.name, p.summary, p.authors.join(" ")).to_lowercase();
text.split_whitespace()
.all(|word| hay.contains(&word.to_lowercase()))
}
/// The projects of the query's kind whose words match, for this game unless `everything`.
fn search(index: &Index, query: &Query) -> Vec<Project> {
index
.projects
.iter()
.filter(|p| kind_of(&p.kind) == Some(query.kind) && matches(p, &query.text))
.filter(|p| query.everything || p.versions.iter().any(|v| v.game == GAME))
.filter_map(project_of)
.skip(query.offset)
.take(query.limit)
.collect()
}
/// The registry as a browser source.
pub struct Registry;
impl Source for Registry {
fn id(&self) -> SourceId {
SourceId::Horizon
}
fn search(&self, query: &Query) -> Result<Vec<Project>, String> {
Ok(search(&*index()?, query))
}
fn project(&self, id: &str) -> Result<Project, String> {
let index = index()?;
let p = index
.project(id)
.ok_or_else(|| format!("{id} isn't in Horizon's registry."))?;
project_of(p).ok_or_else(|| {
format!(
"{} is a {} in Horizon's registry: this launcher can't install those yet.",
p.name,
p.kind.name()
)
})
}
fn versions(&self, project: &Project) -> Result<Vec<Version>, String> {
let index = index()?;
let p = index
.project(&project.id)
.ok_or_else(|| format!("{} isn't in Horizon's registry.", project.title))?;
Ok(p.versions
.iter()
.map(|v| version_of(&index, p, v))
.collect())
}
fn details(&self, project: &Project) -> Result<Details, String> {
let index = index()?;
let p = index
.project(&project.id)
.ok_or_else(|| format!("{} isn't in Horizon's registry.", project.title))?;
let newest = p.versions.first();
let mut body = p.description.clone();
if let Some(v) = newest.filter(|v| !v.capabilities.is_empty()) {
body.push_str("\n\n## What it may do\n\n");
for c in &v.capabilities {
body.push_str(&format!("- May {}\n", c.describe()));
}
}
Ok(Details {
body,
links: vec![
Link {
label: "Source".into(),
url: p.source.clone(),
},
Link {
label: "Page".into(),
url: project.page.clone(),
},
Link {
label: "Report".into(),
url: format!("{REPORT}&title={}", p.id),
},
],
authors: p.authors.clone(),
published: p
.versions
.last()
.map(|v| v.published.clone())
.unwrap_or_default(),
updated: newest.map(|v| v.published.clone()).unwrap_or_default(),
..Details::default()
})
}
fn fetch(
&self,
file: &FileRef,
folder: &Path,
progress: &dyn Fn(f32),
) -> Result<PathBuf, String> {
fetch(file, folder, local(), progress)
}
}
/// Downloads a registry file, checked; `file://` files only from a local index.
fn fetch(
file: &FileRef,
folder: &Path,
local: bool,
progress: &dyn Fn(f32),
) -> Result<PathBuf, String> {
match file.url.strip_prefix("file://").filter(|_| local) {
Some(path) => {
let reader = fs::File::open(path).map_err(|e| format!("{path}: {e}"))?;
download::save_from_anywhere(file, reader, folder, progress)
}
None => download::fetch(file, folder, progress),
}
}
#[cfg(test)]
mod tests;
+61
View File
@@ -0,0 +1,61 @@
//! The registry source against the sample index of `horizon_mods::index`
//! (`crates/horizon-mods/tests/index/`), its files served from disk.
use super::*;
fn fixtures() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../crates/horizon-mods/tests/index")
}
/// The sample index with its files on disk, as a local test registry lists them.
fn sample() -> Index {
let text = fs::read_to_string(fixtures().join("index-v1.json")).unwrap();
let mut index = Index::parse(&text).unwrap();
for p in &mut index.projects {
for v in &mut p.versions {
let path = fixtures().join(&v.file.name);
v.file.url = format!("file://{}", path.canonicalize().unwrap_or(path).display());
}
}
index
}
fn hud(index: &Index) -> Project {
project_of(index.project("coords_hud").unwrap()).unwrap()
}
#[test]
fn searches_by_kind_and_words() {
let index = sample();
let found = |text: &str, kind| -> Vec<String> {
search(&index, &Query::new(text, kind))
.into_iter()
.map(|p| p.id)
.collect()
};
assert_eq!(found("", Kind::Mod), ["coords_hud"]);
assert_eq!(found("coordinates HUD", Kind::Mod), ["coords_hud"]);
assert!(found("zoom", Kind::Mod).is_empty());
// Kinds this launcher can't install yet (modpacks, a world) are never offered.
assert!(found("", Kind::Modpack).is_empty());
assert!(found("skyblock", Kind::ResourcePack).is_empty());
let p = hud(&index);
assert_eq!(p.support(), super::super::Support::Works);
assert_eq!(p.page, "https://playhorizon.dev/mods/coords_hud");
}
#[test]
fn registry_versions_run_when_made_for_this_game() {
use super::super::{pick, support, Support};
let v = |game: &str| vec![game.to_owned()];
let horizon = vec![LOADER.to_owned()];
assert_eq!(support(Kind::Mod, &v(GAME), &horizon), Support::Works);
assert!(!support(Kind::Mod, &v("26.2"), &horizon).installable());
let index = sample();
let p = index.project("coords_hud").unwrap();
let versions: Vec<Version> = p
.versions
.iter()
.map(|x| version_of(&index, p, x))
.collect();
assert_eq!(pick(Kind::Mod, &versions, false).unwrap().number, "0.1.0");
}
+1
View File
@@ -86,6 +86,7 @@ impl Fake {
.map(|b| format!("{b:02x}"))
.collect(),
),
sha256: None,
sha1: None,
primary: true,
};
+1
View File
@@ -326,6 +326,7 @@ mod tests {
name: "a.jar".into(),
size: 1,
sha512: sha512.map(str::to_owned),
sha256: None,
sha1: sha1.map(str::to_owned),
primary: true,
};
+1
View File
@@ -139,6 +139,7 @@ pub(super) fn parse_version(v: &Value) -> Option<Version> {
name: f["filename"].as_str()?.to_owned(),
size: f["size"].as_u64().unwrap_or(0),
sha512: f["hashes"]["sha512"].as_str().map(str::to_ascii_lowercase),
sha256: None,
sha1: f["hashes"]["sha1"].as_str().map(str::to_ascii_lowercase),
primary: f["primary"].as_bool().unwrap_or(false),
})
+3 -1
View File
@@ -18,6 +18,7 @@ fn version(id: &str, games: &[&str], loaders: &[&str]) -> Version {
name: format!("{id}.zip"),
size: 3,
sha512: Some("ab".into()),
sha256: None,
sha1: None,
primary: true,
}],
@@ -305,6 +306,7 @@ fn file_for(body: &[u8], sha512: bool) -> FileRef {
name: "pack.zip".into(),
size: body.len() as u64,
sha512: sha512.then(|| hex(&sha2::Sha512::digest(body))),
sha256: None,
sha1: (!sha512).then(|| hex(&sha1::Sha1::digest(body))),
primary: true,
}
@@ -383,6 +385,6 @@ fn live_modrinth_search_pick_and_download() {
#[test]
fn curseforge_is_coming_soon_and_never_searched() {
let ids: Vec<SourceId> = sources().iter().map(|s| s.id()).collect();
assert_eq!(ids, [SourceId::Modrinth]);
assert_eq!(ids, [SourceId::Modrinth, SourceId::Horizon]);
assert!(source(SourceId::CurseForge).is_none());
}