fix(content): refine installed update workflow

This commit is contained in:
objz committed 2026-08-05 11:36:41 +02:00
1 parent ed7387beb3
commit bfd8be6abe
19 files changed
+815 -216

No files matched your search

+1
View File
@@ -42,6 +42,7 @@ pub struct ContentEntry {
pub world_details: Option<WorldDetails>,
pub title_suffix: Option<String>,
pub footer_label: Option<String>,
pub footer_change: Option<(String, String)>,
pub description: String,
pub enabled: bool,
pub icon_bytes: Option<Vec<u8>>,
+41 -19
View File
@@ -19,6 +19,8 @@ struct FabricModJson {
#[serde(default)]
description: String,
#[serde(default)]
version: String,
#[serde(default)]
icon: serde_json::Value,
}
@@ -51,6 +53,8 @@ struct QuiltModJson {
#[derive(Deserialize, Default)]
struct QuiltLoader {
#[serde(default)]
version: String,
#[serde(default)]
metadata: QuiltMetadata,
}
@@ -72,7 +76,7 @@ impl QuiltMetadata {
}
pub fn scan_one_mod(path: &Path, file_stem: &str, enabled: bool) -> ContentEntry {
let (name, description, icon_bytes) = read_mod_metadata(path);
let (name, description, version, icon_bytes) = read_mod_metadata(path);
let icon_lines = icon_bytes
.as_ref()
.and_then(|bytes| make_icon_pixels(bytes, 6, 3))
@@ -92,7 +96,8 @@ pub fn scan_one_mod(path: &Path, file_stem: &str, enabled: bool) -> ContentEntry
provider_project: None,
world_details: None,
title_suffix: None,
footer_label: None,
footer_label: display_version(version),
footer_change: None,
description,
enabled,
icon_bytes,
@@ -138,12 +143,12 @@ pub fn scan_mods(instances_dir: &Path, instance_name: &str) -> Vec<ContentEntry>
// checks fabric.mod.json, quilt.mod.json, META-INF/mods.toml (forge),
// META-INF/neoforge.mods.toml, and mcmod.info (legacy forge). if none of
// those yield an icon, falls back to common root-level paths.
fn read_mod_metadata(jar_path: &Path) -> (String, String, Option<Vec<u8>>) {
fn read_mod_metadata(jar_path: &Path) -> (String, String, String, Option<Vec<u8>>) {
let file = match std::fs::File::open(jar_path) {
Ok(file) => file,
Err(e) => {
tracing::trace!("Failed to open mod JAR {}: {}", jar_path.display(), e);
return (String::new(), String::new(), None);
return (String::new(), String::new(), String::new(), None);
}
};
@@ -155,13 +160,14 @@ fn read_mod_metadata(jar_path: &Path) -> (String, String, Option<Vec<u8>>) {
jar_path.display(),
e
);
return (String::new(), String::new(), None);
return (String::new(), String::new(), String::new(), None);
}
};
// try each loader's metadata in order. if we get metadata but the
// declared icon path is missing, fall back to common root-level icons.
type MetaReader = fn(&mut zip::ZipArchive<std::fs::File>) -> Option<(String, String, String)>;
type MetaReader =
fn(&mut zip::ZipArchive<std::fs::File>) -> Option<(String, String, String, String)>;
let readers: [MetaReader; 4] = [
read_fabric_meta,
read_quilt_meta,
@@ -170,7 +176,7 @@ fn read_mod_metadata(jar_path: &Path) -> (String, String, Option<Vec<u8>>) {
];
for reader in &readers {
if let Some((name, description, icon_path)) = reader(&mut archive) {
if let Some((name, description, version, icon_path)) = reader(&mut archive) {
let icon_path = icon_path.trim_start_matches('/');
let icon = if icon_path.is_empty() {
None
@@ -184,7 +190,7 @@ fn read_mod_metadata(jar_path: &Path) -> (String, String, Option<Vec<u8>>) {
name,
!icon_path.is_empty()
);
return (name, description, icon);
return (name, description, version, icon);
}
}
@@ -195,7 +201,12 @@ fn read_mod_metadata(jar_path: &Path) -> (String, String, Option<Vec<u8>>) {
jar_path.display(),
icon_bytes.is_some()
);
(String::new(), String::new(), icon_bytes)
(String::new(), String::new(), String::new(), icon_bytes)
}
fn display_version(version: String) -> Option<String> {
let version = version.trim();
(!version.is_empty() && !version.contains("${")).then(|| version.to_owned())
}
fn try_fallback_icons(archive: &mut zip::ZipArchive<std::fs::File>) -> Option<Vec<u8>> {
@@ -209,27 +220,28 @@ fn try_fallback_icons(archive: &mut zip::ZipArchive<std::fs::File>) -> Option<Ve
fn read_fabric_meta(
archive: &mut zip::ZipArchive<std::fs::File>,
) -> Option<(String, String, String)> {
) -> Option<(String, String, String, String)> {
let mut entry = archive.by_name("fabric.mod.json").ok()?;
let mut raw = String::new();
entry.read_to_string(&mut raw).ok()?;
let sanitized = sanitize_json_strings(&raw);
let data: FabricModJson = serde_json::from_str(&sanitized).ok()?;
let icon = data.icon_path();
Some((data.name, data.description, icon))
Some((data.name, data.description, data.version, icon))
}
fn read_quilt_meta(
archive: &mut zip::ZipArchive<std::fs::File>,
) -> Option<(String, String, String)> {
) -> Option<(String, String, String, String)> {
let mut entry = archive.by_name("quilt.mod.json").ok()?;
let mut raw = String::new();
entry.read_to_string(&mut raw).ok()?;
let sanitized = sanitize_json_strings(&raw);
let data: QuiltModJson = serde_json::from_str(&sanitized).ok()?;
let version = data.quilt_loader.version;
let meta = data.quilt_loader.metadata;
let icon = meta.icon_path();
Some((meta.name, meta.description, icon))
Some((meta.name, meta.description, version, icon))
}
// forge (META-INF/mods.toml) and neoforge (META-INF/neoforge.mods.toml)
@@ -239,7 +251,7 @@ fn read_quilt_meta(
// but no [[mods]] section. we still want the icon in that case.
fn read_forge_toml_meta(
archive: &mut zip::ZipArchive<std::fs::File>,
) -> Option<(String, String, String)> {
) -> Option<(String, String, String, String)> {
let raw = read_zip_string(archive, "META-INF/neoforge.mods.toml")
.or_else(|| read_zip_string(archive, "META-INF/mods.toml"))?;
let table: toml::Table = raw.parse().ok()?;
@@ -248,7 +260,7 @@ fn read_forge_toml_meta(
.and_then(|v| v.as_str())
.unwrap_or("")
.to_owned();
let (name, description) = table
let (name, description, version) = table
.get("mods")
.and_then(|v| v.as_array())
.and_then(|a| a.first())
@@ -265,17 +277,22 @@ fn read_forge_toml_meta(
.unwrap_or("")
.trim()
.to_owned();
(n, d)
let version = first
.get("version")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_owned();
(n, d, version)
})
.unwrap_or_default();
Some((name, description, logo))
Some((name, description, version, logo))
}
// legacy forge mcmod.info is either a bare json array of mod entries
// or an object with a "modList" key wrapping the array
fn read_mcmod_info(
archive: &mut zip::ZipArchive<std::fs::File>,
) -> Option<(String, String, String)> {
) -> Option<(String, String, String, String)> {
let mut entry = archive.by_name("mcmod.info").ok()?;
let mut raw = String::new();
entry.read_to_string(&mut raw).ok()?;
@@ -296,12 +313,17 @@ fn read_mcmod_info(
.and_then(|v| v.as_str())
.unwrap_or("")
.to_owned();
let version = first
.get("version")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_owned();
let logo = first
.get("logoFile")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_owned();
Some((name, description, logo))
Some((name, description, version, logo))
}
fn read_zip_string(archive: &mut zip::ZipArchive<std::fs::File>, path: &str) -> Option<String> {
+1
View File
@@ -62,6 +62,7 @@ pub(crate) fn scan_one_pack(path: &Path, file_stem: &str, enabled: bool) -> Cont
world_details: None,
title_suffix: None,
footer_label: None,
footer_change: None,
description,
enabled,
icon_bytes,
+70
View File
@@ -20,6 +20,8 @@ pub struct AvailableUpdate {
pub struct UpdateSnapshot {
pub game_version: String,
pub loader: ModLoader,
#[serde(default)]
pub inventory: Vec<ProviderProject>,
pub updates: Vec<AvailableUpdate>,
pub failures: Vec<UpdateCheckFailure>,
}
@@ -47,6 +49,7 @@ pub struct UpdateRequest {
#[derive(Debug, Clone)]
pub struct PlannedRootUpdate {
pub title: String,
pub installed_path: std::path::PathBuf,
pub current_version: String,
pub target: VersionInfo,
}
@@ -79,6 +82,10 @@ impl UpdateSnapshot {
self.game_version == instance.game_version && self.loader == instance.loader
}
pub fn matches_manifest(&self, manifest: &ContentManifest) -> bool {
self.inventory == resolved_inventory(manifest)
}
pub fn update_for(&self, installed: &ProviderProject) -> Option<&AvailableUpdate> {
self.updates.iter().find(|update| {
update.installed.provider == installed.provider
@@ -129,6 +136,10 @@ pub async fn scan(instance: &InstanceConfig, manifest: &ContentManifest) -> Upda
&& left.0.project_id == right.0.project_id
&& left.0.version_id == right.0.version_id
});
let inventory = projects
.iter()
.map(|(project, _)| project.clone())
.collect();
let slots = Arc::new(tokio::sync::Semaphore::new(8));
let mut tasks = tokio::task::JoinSet::new();
@@ -195,11 +206,29 @@ pub async fn scan(instance: &InstanceConfig, manifest: &ContentManifest) -> Upda
UpdateSnapshot {
game_version: instance.game_version.clone(),
loader: instance.loader,
inventory,
updates,
failures,
}
}
fn resolved_inventory(manifest: &ContentManifest) -> Vec<ProviderProject> {
let mut inventory = manifest
.files
.iter()
.filter_map(|record| record.resolved_project().cloned())
.collect::<Vec<_>>();
inventory.sort_by(|left, right| {
(&left.provider, &left.project_id, &left.version_id).cmp(&(
&right.provider,
&right.project_id,
&right.version_id,
))
});
inventory.dedup();
inventory
}
pub async fn plan_bulk(
instance: &InstanceConfig,
manifest: &ContentManifest,
@@ -249,6 +278,7 @@ pub async fn plan_bulk(
}
roots.push(PlannedRootUpdate {
title: request.title,
installed_path: request.installed_path,
current_version: request.update.current.as_ref().map_or_else(
|| request.update.installed.version_id.clone(),
|version| version.version_number.clone(),
@@ -298,6 +328,7 @@ mod tests {
let snapshot = UpdateSnapshot {
game_version: "1.21.1".to_owned(),
loader: ModLoader::Fabric,
inventory: vec![installed.clone()],
updates: vec![AvailableUpdate {
installed: installed.clone(),
current: Some(version("old")),
@@ -317,4 +348,43 @@ mod tests {
.is_none()
);
}
#[test]
fn cached_updates_require_the_same_content_inventory() {
let project = ProviderProject {
provider: "modrinth".to_owned(),
project_id: "project".to_owned(),
version_id: "old".to_owned(),
};
let snapshot = UpdateSnapshot {
game_version: "1.21.1".to_owned(),
loader: ModLoader::Fabric,
inventory: vec![project.clone()],
updates: Vec::new(),
failures: Vec::new(),
};
let mut manifest = ContentManifest::default();
manifest.files.push(crate::instance::ContentFileRecord {
relative_path: "mods/example.jar".into(),
kind: ContentKind::Mod,
enabled: true,
fingerprint: crate::instance::FileFingerprint {
size: 0,
modified_ns: 0,
hashes: Default::default(),
},
resolution: crate::instance::Resolution::Resolved { project },
provider_aliases: Vec::new(),
required_dependencies: Vec::new(),
automatic_dependency: false,
cleanup_eligible: false,
});
assert!(snapshot.matches_manifest(&manifest));
manifest.files[0].resolution = crate::instance::Resolution::Unmatched {
checked_at: 0,
providers: Vec::new(),
};
assert!(!snapshot.matches_manifest(&manifest));
}
}
+1
View File
@@ -47,6 +47,7 @@ pub fn scan_one_world(path: &Path, file_stem: &str, enabled: bool) -> ContentEnt
world_details: Some(world_details),
title_suffix: None,
footer_label: None,
footer_change: None,
description: String::new(),
enabled,
icon_bytes,