//! The mod browser's sources: search Modrinth (CurseForge support is coming soon) for what //! Horizon loads, say how well each result runs, and download a file only after its checksum //! matches (docs/mod-browser.md). //! //! Offered: Java mods (Fabric, NeoForge, Forge), whose content Horizon ports and whose jar is //! checked before install ([`java`]), modpacks (imported and ported), resource packs //! (`resourcepacks/`) and Java shader packs (`shaderpacks/`, Iris and OptiFine). Java code itself //! doesn't run in Horizon (docs/mods.md). pub mod curseforge; pub mod details; pub mod download; pub mod images; pub mod installed; pub mod java; pub mod modrinth; use serde::{Deserialize, Serialize}; /// The Minecraft version Horizon plays. A version port updates it with the other `"26.3"` sites /// (docs/version-ports.md). pub const GAME: &str = "26.3"; /// What a project is, as far as Horizon is concerned. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum Kind { ResourcePack, ShaderPack, Modpack, /// A Java mod: its content is ported ([`java`]). Mod, } impl Kind { pub const ALL: [Kind; 4] = [ Kind::Mod, Kind::Modpack, Kind::ResourcePack, Kind::ShaderPack, ]; pub fn label(self) -> &'static str { match self { Kind::Mod => "Mods", Kind::ResourcePack => "Resource packs", Kind::ShaderPack => "Shader packs", Kind::Modpack => "Modpacks", } } /// The folder beside `options.txt` the file goes in; a modpack is imported and a mod /// ported instead. pub fn folder(self) -> Option<&'static str> { match self { Kind::ResourcePack => Some("resourcepacks"), Kind::ShaderPack => Some("shaderpacks"), Kind::Modpack | Kind::Mod => None, } } } /// Where a project comes from. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum SourceId { Modrinth, CurseForge, } impl SourceId { pub fn label(self) -> &'static str { match self { SourceId::Modrinth => "Modrinth", SourceId::CurseForge => "CurseForge", } } } /// How well a project runs in Horizon. #[derive(Clone, Debug, PartialEq, Eq)] pub enum Support { /// Made for the version Horizon plays, and Horizon reads everything it uses. Works, /// It loads, with the limit said. Partial(String), /// It can't run in Horizon: never offered for install. No(String), } impl Support { pub fn installable(&self) -> bool { !matches!(self, Support::No(_)) } pub fn label(&self) -> &'static str { match self { Support::Works => "Works", Support::Partial(_) => "Partly", Support::No(_) => "Doesn't run", } } pub fn reason(&self) -> Option<&str> { match self { Support::Works => None, Support::Partial(r) | Support::No(r) => Some(r), } } } /// A search result or a looked-up project. #[derive(Clone, Debug, PartialEq)] pub struct Project { pub source: SourceId, pub id: String, pub slug: String, pub title: String, pub summary: String, pub author: String, pub downloads: u64, /// The project's page on its source, for the player to read before installing. pub page: String, pub icon: Option, pub license: String, pub kind: Kind, /// The Minecraft versions its files list, and the loaders (`iris`, `optifine`, `minecraft`...). pub game_versions: Vec, pub loaders: Vec, } impl Project { pub fn support(&self) -> Support { support(self.kind, &self.game_versions, &self.loaders) } } /// One downloadable file of a version, with the checksums its source publishes. #[derive(Clone, Debug, PartialEq)] pub struct FileRef { pub url: String, pub name: String, pub size: u64, pub sha512: Option, pub sha1: Option, pub primary: bool, } 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, } } } /// A version of a project: its files and what it needs. #[derive(Clone, Debug, PartialEq)] pub struct Version { pub id: String, pub number: String, pub published: String, pub game_versions: Vec, pub loaders: Vec, pub files: Vec, /// Required dependencies, as project ids on the same source. pub required: Vec, /// Why this version can't be downloaded from here (CurseForge authors can forbid it). pub blocked: Option, } impl Version { /// The file to install: the primary one, else the first. pub fn file(&self) -> Option<&FileRef> { self.files .iter() .find(|f| f.primary) .or_else(|| self.files.first()) } } /// A search. #[derive(Clone, Debug, PartialEq)] pub struct Query { pub text: String, pub kind: Kind, /// Off: only what runs (made for [`GAME`], Iris or OptiFine shaders). On: everything of the /// kind, each result marked with how well it runs. pub everything: bool, pub offset: usize, pub limit: usize, pub filters: details::Filters, } impl Query { pub fn new(text: &str, kind: Kind) -> Self { Self { text: text.trim().to_owned(), kind, everything: false, offset: 0, limit: 20, filters: details::Filters::default(), } } } /// A place projects come from. pub trait Source: Send + Sync { fn id(&self) -> SourceId; fn search(&self, query: &Query) -> Result, String>; fn project(&self, id: &str) -> Result; /// Versions, newest first. fn versions(&self, project: &Project) -> Result, String>; /// The project's page: description, gallery, links, authors. fn details(&self, project: &Project) -> Result { Ok(details::Details { body: project.summary.clone(), ..details::Details::default() }) } /// The values the search's filters can take for `kind`. fn tags(&self, _kind: Kind) -> Result { Ok(details::Tags::default()) } /// Downloads one of its files into `folder`, checked ([`download::fetch`]). fn fetch( &self, file: &FileRef, folder: &std::path::Path, progress: &dyn Fn(f32), ) -> Result { download::fetch(file, folder, progress) } } /// 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. pub fn sources() -> Vec> { vec![Box::new(modrinth::Modrinth)] } /// The source of that id, when this launcher can use it. pub fn source(id: SourceId) -> Option> { sources().into_iter().find(|s| s.id() == id) } /// The loaders a Java shader pack may name that Horizon translates. const SHADER_LOADERS: [&str; 2] = ["iris", "optifine"]; /// 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 { match kind { Kind::ResourcePack => { if game_versions.iter().any(|v| v == GAME) { Support::Works } else if let Some(newest) = newest_release(game_versions) { Support::Partial(format!( "Made for {newest}: it loads with vanilla's compatibility warning, and \ what changed since may look wrong." )) } else { Support::Partial("Lists no Minecraft version.".into()) } } Kind::ShaderPack => { if loaders.iter().any(|l| SHADER_LOADERS.contains(&l.as_str())) { Support::Partial( "Java shader packs are translated: their full-screen passes draw, the world \ and shadow passes don't yet (BSL and Complementary tested)." .into(), ) } else { let named = loaders.first().map(String::as_str).unwrap_or("unknown"); Support::No(format!( "A shader pack for {named}: Horizon runs Iris and OptiFine packs only." )) } } Kind::Modpack => { Support::Partial("Horizon runs the mods it can port, from your own downloads.".into()) } Kind::Mod => { if !loaders.iter().any(|l| java::LOADERS.contains(&l.as_str())) { Support::No("Not a Fabric, NeoForge or Forge mod.".into()) } else if !game_versions.iter().any(|v| portable_version(v)) { let newest = newest_release(game_versions).unwrap_or("an unknown version"); Support::No(format!( "Made for {newest}: Horizon ports mods made for 1.21 and later." )) } else { Support::Partial( "Not checked yet: the launcher reads its jar to say what of it ports.".into(), ) } } } } /// A Minecraft version whose mods port-pack carries over: 1.21 and later (the data folders /// took their current names in 1.21). pub fn portable_version(v: &str) -> bool { let parts: Vec = v.split('.').map_while(|p| p.parse().ok()).collect(); match parts.as_slice() { [1, minor, ..] => *minor >= 21 && v.split('.').count() == parts.len(), [year, ..] => *year >= 26 && v.split('.').count() == parts.len(), [] => false, } } /// The newest plain release (`1.21.4`, `26.2`) in a list of Minecraft versions. fn newest_release(versions: &[String]) -> Option<&str> { versions .iter() .filter(|v| { v.split('.') .all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit())) }) .max_by_key(|v| { v.split('.') .map(|p| p.parse::().unwrap_or(0)) .collect::>() }) .map(String::as_str) } /// The version to install: the newest one that runs, or with `everything` the newest at all. /// `versions` is newest first, as [`Source::versions`] gives them. pub fn pick(kind: Kind, versions: &[Version], everything: bool) -> Option<&Version> { let mut usable = versions .iter() .filter(|v| v.blocked.is_none() && v.file().is_some()); if kind == Kind::Mod { let java = |v: &&Version| { v.loaders .iter() .any(|l| java::LOADERS.contains(&l.as_str())) }; let made_for = |v: &&Version| v.game_versions.iter().any(|g| g == GAME); let ported = |v: &&Version| v.game_versions.iter().any(|g| portable_version(g)); return usable .clone() .filter(java) .find(made_for) .or_else(|| usable.filter(java).find(|v| everything && ported(v))); } let runs = |v: &&Version| match support(kind, &v.game_versions, &v.loaders) { Support::Works => true, // A modpack's version is for its own Minecraft; any shader pack Horizon translates. Support::Partial(_) => kind != Kind::ResourcePack, Support::No(_) => false, }; if let Some(v) = usable.clone().find(runs) { return Some(v); } usable.find(|v| everything && support(kind, &v.game_versions, &v.loaders).installable()) } /// A file name from a source, kept only when it is one plain name: no folders, no `..`, no /// hidden file, nothing a file system would read as something else. pub fn safe_name(name: &str) -> Option<&str> { let ok = !name.is_empty() && name.len() <= 200 && !name.starts_with('.') && !name.ends_with(['.', ' ']) && name.chars().all(|c| { !c.is_control() && !matches!(c, '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|') }); ok.then_some(name) } #[cfg(test)] mod tests;