ee104603dd
The launcher's "Import modpack" needs the pipeline as a library and a way to get the jars a pack names without redistributing anything. - The steps move into the library (pipeline.rs) with a line callback, so the command line and the launcher run the same code; main.rs is a thin CLI. - fetch: the jars of the portable mods (content, data tier) into <work>/jars/, the player's own downloads: copied from a folder they have (by SHA-1, or a CurseForge app instance's minecraftinstance.json), else from the pack's own link (.mrpack hosts only) or the same file on Modrinth's public CDN found by SHA-1, kept only when the hash matches. What only CurseForge has is listed as needing a CurseForge API key. <work>/fetch.json records each file. - port: every step after the import for the portable mods (fetch, the inventory again with the jars read, carry, report). - A CurseForge zip whose overrides hold Crash Assistant's modlist.json (All the Mods 10 does) takes its jar names, mod ids and SHA-1s instead of bare project ids, so Modrinth answers for most files. - inventory goes on offline when Modrinth cannot be reached. All the Mods 10 8.3's CurseForge zip: 496 files named; of the 54 data-tier content mods 42 download from Modrinth's CDN (84 MB) and 12 need the key or the player's instance; 42 carried over (partial: the harness is a dev step). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
3.9 KiB
Rust
114 lines
3.9 KiB
Rust
//! Modrinth metadata for the pack's files: the version of each file by
|
|
//! SHA-1, and its project (categories, licence, sides). Only metadata (the
|
|
//! jars are `fetch`'s). Answers are cached in `<work>/modrinth.json`, so a
|
|
//! rerun works offline.
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::Value;
|
|
use std::{collections::BTreeMap, path::Path};
|
|
|
|
const API: &str = "https://api.modrinth.com/v2";
|
|
pub(crate) const AGENT: &str = "ludovic111/horizon horizon-port-pack/0.1 (docs/modpacks.md)";
|
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
|
pub struct Cache {
|
|
/// Version objects by file SHA-1.
|
|
pub versions: BTreeMap<String, Value>,
|
|
/// Project objects by project id.
|
|
pub projects: BTreeMap<String, Value>,
|
|
}
|
|
|
|
impl Cache {
|
|
pub fn load(path: &Path) -> Self {
|
|
std::fs::read_to_string(path)
|
|
.ok()
|
|
.and_then(|t| serde_json::from_str(&t).ok())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
/// Ask Modrinth about the hashes and projects not cached yet.
|
|
pub fn fetch(&mut self, hashes: &[String]) -> Result<(), String> {
|
|
let missing: Vec<&String> = hashes
|
|
.iter()
|
|
.filter(|h| !self.versions.contains_key(*h))
|
|
.collect();
|
|
for chunk in missing.chunks(500) {
|
|
let body = serde_json::json!({"hashes": chunk, "algorithm": "sha1"});
|
|
let found = post(&format!("{API}/version_files"), &body)?;
|
|
for (hash, version) in found.as_object().into_iter().flatten() {
|
|
self.versions.insert(hash.clone(), version.clone());
|
|
}
|
|
}
|
|
let mut ids: Vec<String> = self
|
|
.versions
|
|
.values()
|
|
.filter_map(|v| v["project_id"].as_str().map(str::to_owned))
|
|
.filter(|id| !self.projects.contains_key(id))
|
|
.collect();
|
|
ids.sort();
|
|
ids.dedup();
|
|
for chunk in ids.chunks(100) {
|
|
let list = serde_json::to_string(chunk).unwrap_or_default();
|
|
let found = get(&format!("{API}/projects?ids={}", encode(&list)))?;
|
|
for project in found.as_array().into_iter().flatten() {
|
|
if let Some(id) = project["id"].as_str() {
|
|
self.projects.insert(id.to_owned(), project.clone());
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn project_of(&self, sha1: &str) -> Option<&Value> {
|
|
let id = self.versions.get(sha1)?["project_id"].as_str()?;
|
|
self.projects.get(id)
|
|
}
|
|
|
|
/// Required dependencies of a file's version, as project ids.
|
|
pub fn required(&self, sha1: &str) -> Vec<String> {
|
|
let Some(v) = self.versions.get(sha1) else {
|
|
return Vec::new();
|
|
};
|
|
v["dependencies"]
|
|
.as_array()
|
|
.into_iter()
|
|
.flatten()
|
|
.filter(|d| d["dependency_type"] == "required")
|
|
.filter_map(|d| d["project_id"].as_str().map(str::to_owned))
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
fn post(url: &str, body: &Value) -> Result<Value, String> {
|
|
let response = ureq::post(url)
|
|
.set("User-Agent", AGENT)
|
|
.set("Content-Type", "application/json")
|
|
.send_string(&body.to_string())
|
|
.map_err(|e| format!("{url}: {e}"))?;
|
|
read(url, response)
|
|
}
|
|
|
|
fn get(url: &str) -> Result<Value, String> {
|
|
let response = ureq::get(url)
|
|
.set("User-Agent", AGENT)
|
|
.call()
|
|
.map_err(|e| format!("{url}: {e}"))?;
|
|
read(url, response)
|
|
}
|
|
|
|
fn read(url: &str, response: ureq::Response) -> Result<Value, String> {
|
|
let text = response.into_string().map_err(|e| format!("{url}: {e}"))?;
|
|
serde_json::from_str(&text).map_err(|e| format!("{url}: {e}"))
|
|
}
|
|
|
|
/// Percent-encoding for a query value.
|
|
fn encode(s: &str) -> String {
|
|
s.bytes()
|
|
.map(|b| match b {
|
|
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
|
|
(b as char).to_string()
|
|
}
|
|
_ => format!("%{b:02X}"),
|
|
})
|
|
.collect()
|
|
}
|