feat(tui): add resource packs and shaders tabs
This commit is contained in:
1 parent
8749f8361a
commit
794fb4d08c
10 files changed
+877
-211
No files matched your search
@@ -3,9 +3,13 @@ pub mod manager;
|
||||
pub mod loader;
|
||||
pub mod models;
|
||||
pub mod mods;
|
||||
pub mod resource_packs;
|
||||
pub mod shaders;
|
||||
|
||||
pub use launch::LaunchError;
|
||||
pub use loader::{get_installer, GameVersion, ModLoaderInstaller, VanillaInstaller};
|
||||
pub use manager::{InstanceError, InstanceManager};
|
||||
pub use models::{InstanceConfig, ModLoader};
|
||||
pub use mods::{scan_mods, toggle_mod, ModEntry};
|
||||
pub use resource_packs::scan_resource_packs;
|
||||
pub use shaders::scan_shaders;
|
||||
@@ -60,7 +60,7 @@ pub fn scan_mods(instances_dir: &Path, instance_name: &str) -> Vec<ModEntry> {
|
||||
let (name, description, icon_bytes) = read_mod_metadata(&path);
|
||||
let icon_lines = icon_bytes
|
||||
.as_ref()
|
||||
.and_then(|bytes| make_icon_pixels(bytes, 4, 2));
|
||||
.and_then(|bytes| make_icon_pixels(bytes, 6, 3));
|
||||
|
||||
entries.push(ModEntry {
|
||||
file_stem: file_stem.clone(),
|
||||
@@ -116,7 +116,11 @@ fn read_zip_bytes(archive: &mut zip::ZipArchive<std::fs::File>, path: &str) -> O
|
||||
Some(bytes)
|
||||
}
|
||||
|
||||
fn make_icon_pixels(bytes: &[u8], width: u16, height: u16) -> Option<Vec<Vec<IconCell>>> {
|
||||
pub(crate) fn make_icon_pixels(
|
||||
bytes: &[u8],
|
||||
width: u16,
|
||||
height: u16,
|
||||
) -> Option<Vec<Vec<IconCell>>> {
|
||||
let img = image::load_from_memory(bytes).ok()?;
|
||||
let resized = img.resize_exact(
|
||||
u32::from(width),
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::mods::{make_icon_pixels, ModEntry};
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
pub(crate) struct PackMcMeta {
|
||||
#[serde(default)]
|
||||
pub pack: PackInfo,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
pub(crate) struct PackInfo {
|
||||
#[serde(default)]
|
||||
pub description: serde_json::Value,
|
||||
}
|
||||
|
||||
pub(crate) fn extract_description(value: &serde_json::Value) -> String {
|
||||
match value {
|
||||
serde_json::Value::String(s) => s.clone(),
|
||||
serde_json::Value::Object(obj) => obj
|
||||
.get("text")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string(),
|
||||
serde_json::Value::Array(arr) => arr
|
||||
.iter()
|
||||
.filter_map(|v| match v {
|
||||
serde_json::Value::String(s) => Some(s.as_str()),
|
||||
serde_json::Value::Object(obj) => obj.get("text").and_then(|v| v.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(""),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scan_resource_packs(instances_dir: &Path, instance_name: &str) -> Vec<ModEntry> {
|
||||
let packs_dir = instances_dir
|
||||
.join(instance_name)
|
||||
.join(".minecraft")
|
||||
.join("resourcepacks");
|
||||
|
||||
let read_dir = match std::fs::read_dir(&packs_dir) {
|
||||
Ok(rd) => rd,
|
||||
Err(_) => return Vec::new(),
|
||||
};
|
||||
|
||||
let mut entries = Vec::new();
|
||||
|
||||
for entry in read_dir.flatten() {
|
||||
let path = entry.path();
|
||||
let file_name = match path.file_name().and_then(|n| n.to_str()) {
|
||||
Some(n) => n.to_string(),
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let is_dir = path.is_dir();
|
||||
let (enabled, file_stem) = if is_dir {
|
||||
if file_name.ends_with(".disabled") {
|
||||
(false, file_name.trim_end_matches(".disabled").to_string())
|
||||
} else {
|
||||
(true, file_name.clone())
|
||||
}
|
||||
} else if file_name.ends_with(".zip") {
|
||||
(true, file_name.trim_end_matches(".zip").to_string())
|
||||
} else if file_name.ends_with(".zip.disabled") {
|
||||
(
|
||||
false,
|
||||
file_name.trim_end_matches(".zip.disabled").to_string(),
|
||||
)
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
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));
|
||||
|
||||
entries.push(ModEntry {
|
||||
file_stem: file_stem.clone(),
|
||||
name: if name.is_empty() { file_stem } else { name },
|
||||
description,
|
||||
enabled,
|
||||
icon_bytes,
|
||||
path,
|
||||
icon_lines,
|
||||
});
|
||||
}
|
||||
|
||||
entries.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
|
||||
entries
|
||||
}
|
||||
|
||||
fn read_pack_metadata_from_zip(zip_path: &Path) -> (String, String, Option<Vec<u8>>) {
|
||||
let file = match std::fs::File::open(zip_path) {
|
||||
Ok(f) => f,
|
||||
Err(_) => return (String::new(), String::new(), None),
|
||||
};
|
||||
|
||||
let mut archive = match zip::ZipArchive::new(file) {
|
||||
Ok(a) => a,
|
||||
Err(_) => return (String::new(), String::new(), None),
|
||||
};
|
||||
|
||||
let description = read_pack_description(&mut archive);
|
||||
|
||||
let icon_bytes = {
|
||||
let mut buf = Vec::new();
|
||||
match archive.by_name("pack.png") {
|
||||
Ok(mut entry) => {
|
||||
if entry.read_to_end(&mut buf).is_ok() {
|
||||
Some(buf)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
};
|
||||
|
||||
(String::new(), description, icon_bytes)
|
||||
}
|
||||
|
||||
fn read_pack_description(archive: &mut zip::ZipArchive<std::fs::File>) -> String {
|
||||
let entry = match archive.by_name("pack.mcmeta") {
|
||||
Ok(e) => e,
|
||||
Err(_) => return String::new(),
|
||||
};
|
||||
|
||||
let meta: PackMcMeta = match serde_json::from_reader(entry) {
|
||||
Ok(m) => m,
|
||||
Err(_) => return String::new(),
|
||||
};
|
||||
|
||||
extract_description(&meta.pack.description)
|
||||
}
|
||||
|
||||
fn read_pack_metadata_from_dir(dir: &Path) -> (String, String, Option<Vec<u8>>) {
|
||||
let actual_dir = if dir
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.is_some_and(|n| n.ends_with(".disabled"))
|
||||
{
|
||||
dir.to_path_buf()
|
||||
} else {
|
||||
dir.to_path_buf()
|
||||
};
|
||||
|
||||
let description = {
|
||||
let mcmeta_path = actual_dir.join("pack.mcmeta");
|
||||
match std::fs::read_to_string(&mcmeta_path) {
|
||||
Ok(content) => match serde_json::from_str::<PackMcMeta>(&content) {
|
||||
Ok(meta) => extract_description(&meta.pack.description),
|
||||
Err(_) => String::new(),
|
||||
},
|
||||
Err(_) => String::new(),
|
||||
}
|
||||
};
|
||||
|
||||
let icon_bytes = {
|
||||
let icon_path = actual_dir.join("pack.png");
|
||||
std::fs::read(&icon_path).ok()
|
||||
};
|
||||
|
||||
(String::new(), description, icon_bytes)
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
use super::mods::{make_icon_pixels, ModEntry};
|
||||
use super::resource_packs::{extract_description, PackMcMeta};
|
||||
|
||||
pub fn scan_shaders(instances_dir: &Path, instance_name: &str) -> Vec<ModEntry> {
|
||||
let shaders_dir = instances_dir
|
||||
.join(instance_name)
|
||||
.join(".minecraft")
|
||||
.join("shaderpacks");
|
||||
|
||||
let read_dir = match std::fs::read_dir(&shaders_dir) {
|
||||
Ok(rd) => rd,
|
||||
Err(_) => return Vec::new(),
|
||||
};
|
||||
|
||||
let mut entries = Vec::new();
|
||||
|
||||
for entry in read_dir.flatten() {
|
||||
let path = entry.path();
|
||||
let file_name = match path.file_name().and_then(|n| n.to_str()) {
|
||||
Some(n) => n.to_string(),
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let is_dir = path.is_dir();
|
||||
let (enabled, file_stem) = if is_dir {
|
||||
if file_name.ends_with(".disabled") {
|
||||
(false, file_name.trim_end_matches(".disabled").to_string())
|
||||
} else {
|
||||
(true, file_name.clone())
|
||||
}
|
||||
} else if file_name.ends_with(".zip") {
|
||||
(true, file_name.trim_end_matches(".zip").to_string())
|
||||
} else if file_name.ends_with(".zip.disabled") {
|
||||
(
|
||||
false,
|
||||
file_name.trim_end_matches(".zip.disabled").to_string(),
|
||||
)
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
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));
|
||||
|
||||
entries.push(ModEntry {
|
||||
file_stem: file_stem.clone(),
|
||||
name: file_stem,
|
||||
description,
|
||||
enabled,
|
||||
icon_bytes,
|
||||
path,
|
||||
icon_lines,
|
||||
});
|
||||
}
|
||||
|
||||
entries.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
|
||||
entries
|
||||
}
|
||||
|
||||
fn read_shader_metadata_from_zip(zip_path: &Path) -> (String, Option<Vec<u8>>) {
|
||||
let file = match std::fs::File::open(zip_path) {
|
||||
Ok(f) => f,
|
||||
Err(_) => return (String::new(), None),
|
||||
};
|
||||
|
||||
let mut archive = match zip::ZipArchive::new(file) {
|
||||
Ok(a) => a,
|
||||
Err(_) => return (String::new(), None),
|
||||
};
|
||||
|
||||
let description = match archive.by_name("pack.mcmeta") {
|
||||
Ok(entry) => match serde_json::from_reader::<_, PackMcMeta>(entry) {
|
||||
Ok(meta) => extract_description(&meta.pack.description),
|
||||
Err(_) => String::new(),
|
||||
},
|
||||
Err(_) => String::new(),
|
||||
};
|
||||
|
||||
let icon_bytes = {
|
||||
let mut buf = Vec::new();
|
||||
match archive.by_name("pack.png") {
|
||||
Ok(mut entry) => {
|
||||
if entry.read_to_end(&mut buf).is_ok() {
|
||||
Some(buf)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
};
|
||||
|
||||
(description, icon_bytes)
|
||||
}
|
||||
|
||||
fn read_shader_metadata_from_dir(dir: &Path) -> (String, Option<Vec<u8>>) {
|
||||
let description = match std::fs::read_to_string(dir.join("pack.mcmeta")) {
|
||||
Ok(content) => match serde_json::from_str::<PackMcMeta>(&content) {
|
||||
Ok(meta) => extract_description(&meta.pack.description),
|
||||
Err(_) => String::new(),
|
||||
},
|
||||
Err(_) => String::new(),
|
||||
};
|
||||
|
||||
let icon_bytes = std::fs::read(dir.join("pack.png")).ok();
|
||||
|
||||
(description, icon_bytes)
|
||||
}
|
||||
Reference in new issue
Block a user