feat(content): add managed content metadata layout
This commit is contained in:
1 parent
a05b030862
commit
fbf8142ea0
46 files changed
+2404
-166
No files matched your search
+1
-1
@@ -155,7 +155,7 @@ pub async fn download_fabric_libraries(
|
||||
profile: &FabricProfile,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let libraries_dir = meta_dir.join("libraries");
|
||||
let libraries_dir = crate::storage::MetadataPaths::new(meta_dir).libraries();
|
||||
tracing::debug!(
|
||||
"Resolving {} Fabric libraries into {}",
|
||||
profile.libraries.len(),
|
||||
|
||||
+5
-3
@@ -150,7 +150,7 @@ pub async fn run_forge_installer(
|
||||
.arg("-jar")
|
||||
.arg(installer_path)
|
||||
.arg("--installClient")
|
||||
.current_dir(instance_dir.join(".minecraft"))
|
||||
.current_dir(instance_dir.join(crate::storage::MINECRAFT_DIR_NAME))
|
||||
.output()
|
||||
.await
|
||||
{
|
||||
@@ -289,7 +289,9 @@ pub(crate) async fn install_forge_from_profile(
|
||||
})?;
|
||||
|
||||
set_sub_action("Extracting universal JAR...");
|
||||
let universal_dest = meta_dir.join("libraries").join(&universal_maven_path);
|
||||
let universal_dest = crate::storage::MetadataPaths::new(meta_dir)
|
||||
.libraries()
|
||||
.join(&universal_maven_path);
|
||||
if let Some(parent) = universal_dest.parent() {
|
||||
std::fs::create_dir_all(parent)
|
||||
.map_err(|e| InstallError::Installer(InstallerError::Io(e)))?;
|
||||
@@ -318,7 +320,7 @@ pub(crate) async fn install_forge_from_profile(
|
||||
// are forge-hosted, libs without one are typically from mojang's library
|
||||
// server. old forge versions reference libs like launchwrapper that aren't
|
||||
// in mojang's modern version metadata, so we fetch those too.
|
||||
let libraries_dir = meta_dir.join("libraries");
|
||||
let libraries_dir = crate::storage::MetadataPaths::new(meta_dir).libraries();
|
||||
for lib in libraries {
|
||||
let name = lib.get("name").and_then(|v| v.as_str()).unwrap_or_default();
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ pub mod neoforge;
|
||||
pub mod quilt;
|
||||
|
||||
use reqwest::Client;
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::path::Path;
|
||||
use thiserror::Error;
|
||||
@@ -97,6 +98,35 @@ impl HttpClient {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn post_json<B, T>(&self, url: &str, body: &B) -> Result<T, NetError>
|
||||
where
|
||||
B: Serialize + ?Sized,
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
for attempt in 0..=MAX_RETRIES {
|
||||
tracing::trace!("HTTP POST {}", url);
|
||||
let result = async {
|
||||
let response = self.inner.post(url).json(body).send().await?;
|
||||
if !response.status().is_success() {
|
||||
return Err(NetError::StatusError {
|
||||
status: response.status().as_u16(),
|
||||
url: url.to_owned(),
|
||||
});
|
||||
}
|
||||
Ok(response.json().await?)
|
||||
}
|
||||
.await;
|
||||
match result {
|
||||
Ok(value) => return Ok(value),
|
||||
Err(error) if is_retryable(&error) && attempt < MAX_RETRIES => {
|
||||
sleep_before_retry("request", url, attempt, &error).await;
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
}
|
||||
unreachable!("retry loop returns on success or final error")
|
||||
}
|
||||
|
||||
// fetch JSON and also keep the raw bytes. used by install paths that
|
||||
// want both the parsed shape (for downloading libraries from it) and
|
||||
// the original bytes (to write byte-for-byte to the loader-profiles
|
||||
|
||||
+74
-5
@@ -4,16 +4,22 @@
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, serde::Serialize)]
|
||||
pub struct ProjectInfo {
|
||||
pub id: String,
|
||||
pub slug: String,
|
||||
pub title: String,
|
||||
#[serde(default)]
|
||||
pub description: String,
|
||||
#[serde(default)]
|
||||
pub icon_url: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct VersionInfo {
|
||||
pub id: String,
|
||||
#[serde(default)]
|
||||
pub project_id: String,
|
||||
pub name: String,
|
||||
pub version_number: String,
|
||||
pub game_versions: Vec<String>,
|
||||
@@ -27,6 +33,8 @@ pub struct VersionFile {
|
||||
pub filename: String,
|
||||
pub size: u64,
|
||||
pub primary: bool,
|
||||
#[serde(default)]
|
||||
pub hashes: HashMap<String, String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
@@ -129,6 +137,11 @@ pub async fn search_discovery(
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
) -> Result<DiscoveryResults, crate::net::NetError> {
|
||||
let progress = crate::tui::progress::ProgressTask::start(if query.trim().is_empty() {
|
||||
"Loading Modrinth discovery".to_owned()
|
||||
} else {
|
||||
format!("Searching Modrinth for '{}'", query.trim())
|
||||
});
|
||||
let facets = discovery_facets(kind, game_version, loader);
|
||||
let query = query.trim();
|
||||
let index = if query.is_empty() {
|
||||
@@ -141,7 +154,14 @@ pub async fn search_discovery(
|
||||
url_encode(query),
|
||||
url_encode(&facets),
|
||||
);
|
||||
let results: DiscoverySearchResponse = client.get_json(&url).await?;
|
||||
let results: DiscoverySearchResponse = match client.get_json(&url).await {
|
||||
Ok(results) => results,
|
||||
Err(error) => {
|
||||
progress.fail(&error);
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
progress.finish();
|
||||
|
||||
Ok(DiscoveryResults {
|
||||
total_hits: results.total_hits.max(0) as usize,
|
||||
@@ -267,6 +287,7 @@ pub async fn fetch_content_versions(
|
||||
game_version: &str,
|
||||
loader: ModLoader,
|
||||
) -> Result<Vec<VersionInfo>, crate::net::NetError> {
|
||||
let progress = crate::tui::progress::ProgressTask::start("Loading compatible project versions");
|
||||
let url = content_versions_url(API_BASE, project_id, kind, game_version, loader);
|
||||
tracing::debug!(
|
||||
"Fetching compatible Modrinth versions for '{}' ({}, {})",
|
||||
@@ -274,7 +295,16 @@ pub async fn fetch_content_versions(
|
||||
game_version,
|
||||
loader
|
||||
);
|
||||
client.get_json(&url).await
|
||||
match client.get_json(&url).await {
|
||||
Ok(versions) => {
|
||||
progress.finish();
|
||||
Ok(versions)
|
||||
}
|
||||
Err(error) => {
|
||||
progress.fail(&error);
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn content_versions_url(
|
||||
@@ -331,7 +361,13 @@ pub async fn download_version_file(
|
||||
if path.exists() {
|
||||
return Ok(DownloadOutcome::SkippedExisting(path));
|
||||
}
|
||||
crate::net::download_file(client, &file.url, &path, |_, _| {}).await?;
|
||||
let progress =
|
||||
crate::tui::progress::ProgressTask::start(format!("Downloading {}", file.filename));
|
||||
crate::net::download_file(client, &file.url, &path, |current, total| {
|
||||
progress.set_progress(current, total);
|
||||
})
|
||||
.await?;
|
||||
progress.finish();
|
||||
Ok(DownloadOutcome::Downloaded(path))
|
||||
}
|
||||
|
||||
@@ -356,7 +392,13 @@ pub async fn download_version_file_for_update(
|
||||
)));
|
||||
}
|
||||
|
||||
crate::net::download_file(client, &file.url, &temporary, |_, _| {}).await?;
|
||||
let progress =
|
||||
crate::tui::progress::ProgressTask::start(format!("Downloading {}", file.filename));
|
||||
crate::net::download_file(client, &file.url, &temporary, |current, total| {
|
||||
progress.set_progress(current, total);
|
||||
})
|
||||
.await?;
|
||||
progress.finish();
|
||||
replace_installed_file(&temporary, &target, installed_path, &backup).await?;
|
||||
Ok(DownloadOutcome::Downloaded(target))
|
||||
}
|
||||
@@ -395,6 +437,28 @@ pub async fn fetch_version(
|
||||
Ok(version)
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize)]
|
||||
struct VersionFilesRequest<'a> {
|
||||
hashes: &'a [String],
|
||||
algorithm: &'a str,
|
||||
}
|
||||
|
||||
pub async fn resolve_version_files(
|
||||
client: &crate::net::HttpClient,
|
||||
hashes: &[String],
|
||||
algorithm: &str,
|
||||
) -> Result<HashMap<String, VersionInfo>, crate::net::NetError> {
|
||||
if hashes.is_empty() {
|
||||
return Ok(HashMap::new());
|
||||
}
|
||||
client
|
||||
.post_json(
|
||||
&format!("{API_BASE}/version_files"),
|
||||
&VersionFilesRequest { hashes, algorithm },
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
// grabs the primary file from a version, falling back to the first file
|
||||
// if none is marked primary (some projects are sloppy about that)
|
||||
pub async fn download_mrpack(
|
||||
@@ -442,6 +506,7 @@ mod tests {
|
||||
fn version_with_files(files: Vec<VersionFile>) -> VersionInfo {
|
||||
VersionInfo {
|
||||
id: "version-id".to_owned(),
|
||||
project_id: "project-id".to_owned(),
|
||||
name: "Version 1".to_owned(),
|
||||
version_number: "1.0.0".to_owned(),
|
||||
game_versions: vec!["1.21.1".to_owned()],
|
||||
@@ -510,12 +575,14 @@ mod tests {
|
||||
filename: "first.jar".to_owned(),
|
||||
size: 1,
|
||||
primary: false,
|
||||
hashes: HashMap::new(),
|
||||
},
|
||||
VersionFile {
|
||||
url: "https://example.test/primary.jar".to_owned(),
|
||||
filename: "primary.jar".to_owned(),
|
||||
size: 1,
|
||||
primary: true,
|
||||
hashes: HashMap::new(),
|
||||
},
|
||||
]);
|
||||
assert_eq!(
|
||||
@@ -528,6 +595,7 @@ mod tests {
|
||||
filename: "first.jar".to_owned(),
|
||||
size: 1,
|
||||
primary: false,
|
||||
hashes: HashMap::new(),
|
||||
}]);
|
||||
assert_eq!(
|
||||
select_primary_file(&fallback).unwrap().filename,
|
||||
@@ -545,6 +613,7 @@ mod tests {
|
||||
filename: "example.jar".to_owned(),
|
||||
size: 1,
|
||||
primary: true,
|
||||
hashes: HashMap::new(),
|
||||
}]);
|
||||
|
||||
let outcome =
|
||||
|
||||
+9
-7
@@ -152,8 +152,8 @@ pub async fn download_client_jar(
|
||||
meta: &VersionMeta,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let jar_path = meta_dir
|
||||
.join("versions")
|
||||
let jar_path = crate::storage::MetadataPaths::new(meta_dir)
|
||||
.versions()
|
||||
.join(&meta.id)
|
||||
.join(format!("{}.jar", meta.id));
|
||||
|
||||
@@ -225,7 +225,9 @@ pub async fn download_libraries(
|
||||
}
|
||||
};
|
||||
|
||||
let destination = meta_dir.join("libraries").join(&artifact.path);
|
||||
let destination = crate::storage::MetadataPaths::new(meta_dir)
|
||||
.libraries()
|
||||
.join(&artifact.path);
|
||||
|
||||
if destination.exists() {
|
||||
tracing::trace!("Library already cached: {}", artifact.path);
|
||||
@@ -278,8 +280,8 @@ pub async fn download_assets_from(
|
||||
}
|
||||
};
|
||||
|
||||
let index_path = meta_dir
|
||||
.join("assets")
|
||||
let index_path = crate::storage::MetadataPaths::new(meta_dir)
|
||||
.assets()
|
||||
.join("indexes")
|
||||
.join(format!("{}.json", meta.asset_index.id));
|
||||
if !index_path.exists() {
|
||||
@@ -326,8 +328,8 @@ pub async fn download_assets_from(
|
||||
|
||||
let prefix = &object.hash[..2];
|
||||
let url = format!("{}/{}/{}", assets_base, prefix, object.hash);
|
||||
let destination = meta_dir
|
||||
.join("assets")
|
||||
let destination = crate::storage::MetadataPaths::new(meta_dir)
|
||||
.assets()
|
||||
.join("objects")
|
||||
.join(prefix)
|
||||
.join(&object.hash);
|
||||
|
||||
+1
-1
@@ -182,7 +182,7 @@ pub async fn run_neoforge_installer(
|
||||
.arg("-jar")
|
||||
.arg(installer_path)
|
||||
.arg("--installClient")
|
||||
.current_dir(instance_dir.join(".minecraft"))
|
||||
.current_dir(instance_dir.join(crate::storage::MINECRAFT_DIR_NAME))
|
||||
.output()
|
||||
.await
|
||||
{
|
||||
|
||||
+1
-1
@@ -149,7 +149,7 @@ pub async fn download_quilt_libraries(
|
||||
profile: &QuiltProfile,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let libraries_dir = meta_dir.join("libraries");
|
||||
let libraries_dir = crate::storage::MetadataPaths::new(meta_dir).libraries();
|
||||
tracing::debug!(
|
||||
"Resolving {} Quilt libraries into {}",
|
||||
profile.libraries.len(),
|
||||
|
||||
Reference in new issue
Block a user