diff --git a/src/instance/content/mod.rs b/src/instance/content/mod.rs index 7893d1b..f2e1dd1 100644 --- a/src/instance/content/mod.rs +++ b/src/instance/content/mod.rs @@ -7,9 +7,10 @@ pub mod shaders; pub mod worlds; pub use mods::{ContentEntry, scan_mods, toggle_entry}; -pub use resource_packs::scan_resource_packs; -pub use shaders::scan_shaders; -pub use worlds::scan_worlds; +pub use mods::scan_one_mod; +pub use resource_packs::{scan_resource_packs, scan_one_resource_pack}; +pub use shaders::{scan_shaders, scan_one_shader}; +pub use worlds::{scan_worlds, scan_one_world}; use std::io::Read; diff --git a/src/instance/content/mods.rs b/src/instance/content/mods.rs index 8cfe6e2..0dcd5ac 100644 --- a/src/instance/content/mods.rs +++ b/src/instance/content/mods.rs @@ -91,6 +91,30 @@ 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 icon_lines = icon_bytes + .as_ref() + .and_then(|bytes| make_icon_pixels(bytes, 6, 3)) + .or_else(|| Some(fallback_icon())); + + let display_name = if name.is_empty() { + file_stem.to_owned() + } else { + name + }; + + ContentEntry { + file_stem: file_stem.to_owned(), + name: display_name, + description, + enabled, + icon_bytes, + path: path.to_path_buf(), + icon_lines, + } +} + pub fn scan_mods(instances_dir: &Path, instance_name: &str) -> Vec { let mods_dir = instances_dir .join(instance_name) @@ -115,26 +139,7 @@ pub fn scan_mods(instances_dir: &Path, instance_name: &str) -> Vec continue; }; - let (name, description, icon_bytes) = read_mod_metadata(&path); - let icon_lines = icon_bytes - .as_ref() - .and_then(|bytes| make_icon_pixels(bytes, 6, 3)) - .or_else(|| Some(fallback_icon())); - - let display_name = if name.is_empty() { - file_stem.clone() - } else { - name - }; - entries.push(ContentEntry { - file_stem, - name: display_name, - description, - enabled, - icon_bytes, - path, - icon_lines, - }); + entries.push(scan_one_mod(&path, &file_stem, enabled)); } entries.sort_by_cached_key(|e| e.name.to_lowercase()); diff --git a/src/instance/content/resource_packs.rs b/src/instance/content/resource_packs.rs index 22be392..a3e2b0c 100644 --- a/src/instance/content/resource_packs.rs +++ b/src/instance/content/resource_packs.rs @@ -43,6 +43,36 @@ pub(crate) fn extract_description(value: &serde_json::Value) -> String { } } +pub fn scan_one_resource_pack(path: &Path, file_stem: &str, enabled: bool) -> ContentEntry { + let is_dir = path.is_dir(); + let (name, description, icon_bytes) = if is_dir { + read_pack_metadata_from_dir(path) + } else { + read_pack_metadata_from_zip(path) + }; + + let icon_lines = icon_bytes + .as_ref() + .and_then(|bytes| make_icon_pixels(bytes, 6, 3)) + .or_else(|| Some(super::mods::fallback_icon())); + + let display_name = if name.is_empty() { + file_stem.to_owned() + } else { + name + }; + + ContentEntry { + file_stem: file_stem.to_owned(), + name: display_name, + description, + enabled, + icon_bytes, + path: path.to_path_buf(), + icon_lines, + } +} + pub fn scan_resource_packs(instances_dir: &Path, instance_name: &str) -> Vec { let packs_dir = instances_dir .join(instance_name) @@ -63,8 +93,7 @@ pub fn scan_resource_packs(instances_dir: &Path, instance_name: &str) -> Vec continue, }; - let is_dir = path.is_dir(); - let (enabled, file_stem) = if is_dir { + let (enabled, file_stem) = if path.is_dir() { super::parse_enabled_stem_dir(&file_name) } else if let Some(pair) = super::parse_enabled_stem(&file_name, ".zip") { pair @@ -72,27 +101,7 @@ pub fn scan_resource_packs(instances_dir: &Path, instance_name: &str) -> Vec ContentEntry { + let is_dir = path.is_dir(); + let (description, icon_bytes) = if is_dir { + read_shader_metadata_from_dir(path) + } else { + read_shader_metadata_from_zip(path) + }; + + let icon_lines = icon_bytes + .as_ref() + .and_then(|bytes| make_icon_pixels(bytes, 6, 3)); + + ContentEntry { + file_stem: file_stem.to_owned(), + name: file_stem.to_owned(), + description, + enabled, + icon_bytes, + path: path.to_path_buf(), + icon_lines, + } +} + pub fn scan_shaders(instances_dir: &Path, instance_name: &str) -> Vec { let shaders_dir = instances_dir .join(instance_name) @@ -26,8 +49,7 @@ pub fn scan_shaders(instances_dir: &Path, instance_name: &str) -> Vec continue, }; - let is_dir = path.is_dir(); - let (enabled, file_stem) = if is_dir { + let (enabled, file_stem) = if path.is_dir() { super::parse_enabled_stem_dir(&file_name) } else if let Some(pair) = super::parse_enabled_stem(&file_name, ".zip") { pair @@ -35,25 +57,7 @@ pub fn scan_shaders(instances_dir: &Path, instance_name: &str) -> Vec ContentEntry { + let icon_bytes = std::fs::read(path.join("icon.png")).ok(); + let icon_lines = icon_bytes + .as_ref() + .and_then(|bytes| make_icon_pixels(bytes, 12, 6)) + .or_else(|| Some(super::mods::fallback_icon_large())); + + let description = world_description(path); + + ContentEntry { + name: file_stem.to_owned(), + file_stem: file_stem.to_owned(), + description, + enabled, + icon_bytes, + path: path.to_path_buf(), + icon_lines, + } +} + pub fn scan_worlds(instances_dir: &Path, instance_name: &str) -> Vec { let saves_dir = instances_dir .join(instance_name) @@ -31,24 +51,7 @@ pub fn scan_worlds(instances_dir: &Path, instance_name: &str) -> Vec