fix: switch default profile to terminal and fixed some comments
This commit is contained in:
1 parent
971375bf1f
commit
52787b57c7
71 files changed
+671
-188
No files matched your search
+13
-12
@@ -1,27 +1,32 @@
|
||||
// scanning and toggling instance content (mods, resource packs, shaders, worlds).
|
||||
// minecraft uses a ".disabled" suffix convention for disabled content, so this leans on that heavily.
|
||||
|
||||
pub mod mods;
|
||||
pub mod resource_packs;
|
||||
pub mod shaders;
|
||||
pub mod worlds;
|
||||
|
||||
pub use mods::{scan_mods, toggle_entry, ContentEntry};
|
||||
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;
|
||||
|
||||
use std::io::Read;
|
||||
|
||||
/// Determine enabled/disabled state and file stem from a filename.
|
||||
///
|
||||
/// Given the file extension (e.g. ".jar", ".zip") and the file name, returns
|
||||
/// `Some((enabled, file_stem))` or `None` if the file doesn't match.
|
||||
// figures out if a file is enabled or disabled based on the ".disabled" suffix,
|
||||
// and strips the extension to get a clean stem name
|
||||
pub(crate) fn parse_enabled_stem(file_name: &str, ext: &str) -> Option<(bool, String)> {
|
||||
let disabled_ext = format!("{ext}.disabled");
|
||||
if let Some(stem) = file_name.strip_suffix(&disabled_ext) {
|
||||
Some((false, stem.to_string()))
|
||||
} else { file_name.strip_suffix(ext).map(|stem| (true, stem.to_string())) }
|
||||
} else {
|
||||
file_name
|
||||
.strip_suffix(ext)
|
||||
.map(|stem| (true, stem.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
/// For directories: check if the name ends in `.disabled`.
|
||||
// same idea but for directories, which don't have a file extension to strip
|
||||
pub(crate) fn parse_enabled_stem_dir(file_name: &str) -> (bool, String) {
|
||||
if let Some(stem) = file_name.strip_suffix(".disabled") {
|
||||
(false, stem.to_string())
|
||||
@@ -30,17 +35,13 @@ pub(crate) fn parse_enabled_stem_dir(file_name: &str) -> (bool, String) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Read icon bytes from a zip archive (pack.png).
|
||||
pub(crate) fn read_icon_from_zip(
|
||||
archive: &mut zip::ZipArchive<std::fs::File>,
|
||||
) -> Option<Vec<u8>> {
|
||||
pub(crate) fn read_icon_from_zip(archive: &mut zip::ZipArchive<std::fs::File>) -> Option<Vec<u8>> {
|
||||
let mut entry = archive.by_name("pack.png").ok()?;
|
||||
let mut buf = Vec::new();
|
||||
entry.read_to_end(&mut buf).ok()?;
|
||||
Some(buf)
|
||||
}
|
||||
|
||||
/// Open a zip file and return the archive handle.
|
||||
pub(crate) fn open_zip(path: &std::path::Path) -> Option<zip::ZipArchive<std::fs::File>> {
|
||||
let file = std::fs::File::open(path).ok()?;
|
||||
zip::ZipArchive::new(file).ok()
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
// mod scanning, metadata extraction, and icon rendering for the content list.
|
||||
// jar files are just zips, so it cracks them open looking for fabric.mod.json to get
|
||||
// names, descriptions, and icons. if that fails, just the filename is used. it's fine.
|
||||
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
/// A single icon cell representing two vertically stacked pixels using the "▄" half-block.
|
||||
/// `bg_*` = top pixel (background color), `fg_*` = bottom pixel (foreground color).
|
||||
// a single "pixel" in the terminal icon. uses the unicode half-block trick
|
||||
// where each character cell shows two vertical pixels (fg = top, bg = bottom)
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct IconCell {
|
||||
pub bg_r: u8,
|
||||
@@ -37,6 +41,8 @@ struct FabricModJson {
|
||||
}
|
||||
|
||||
impl FabricModJson {
|
||||
// fabric's icon field can be a string path or a map of resolution -> path.
|
||||
// if it's a map, just grab the first one. good enough.
|
||||
fn icon_path(&self) -> String {
|
||||
match &self.icon {
|
||||
serde_json::Value::String(s) => s.clone(),
|
||||
@@ -80,7 +86,11 @@ pub fn scan_mods(instances_dir: &Path, instance_name: &str) -> Vec<ContentEntry>
|
||||
.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 };
|
||||
let display_name = if name.is_empty() {
|
||||
file_stem.clone()
|
||||
} else {
|
||||
name
|
||||
};
|
||||
entries.push(ContentEntry {
|
||||
file_stem,
|
||||
name: display_name,
|
||||
@@ -96,6 +106,8 @@ pub fn scan_mods(instances_dir: &Path, instance_name: &str) -> Vec<ContentEntry>
|
||||
entries
|
||||
}
|
||||
|
||||
// tries fabric.mod.json first for rich metadata. if that's missing (forge/other mods),
|
||||
// still attempts to grab pack.png as a last resort for an icon
|
||||
fn read_mod_metadata(jar_path: &Path) -> (String, String, Option<Vec<u8>>) {
|
||||
let file = match std::fs::File::open(jar_path) {
|
||||
Ok(file) => file,
|
||||
@@ -126,14 +138,15 @@ fn read_fabric_meta(
|
||||
let mut entry = archive.by_name("fabric.mod.json").ok()?;
|
||||
let mut raw = String::new();
|
||||
entry.read_to_string(&mut raw).ok()?;
|
||||
// Some mods have literal newlines in JSON strings — sanitize them
|
||||
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))
|
||||
}
|
||||
|
||||
/// Replace literal control characters inside JSON string values with escaped versions.
|
||||
// some mod authors put raw newlines/tabs inside json string values which is
|
||||
// technically invalid json. walks through character by character, tracking
|
||||
// whether it's inside a string, and escapes the offending characters.
|
||||
fn sanitize_json_strings(input: &str) -> String {
|
||||
let mut result = String::with_capacity(input.len());
|
||||
let mut in_string = false;
|
||||
@@ -175,6 +188,8 @@ fn read_zip_bytes(archive: &mut zip::ZipArchive<std::fs::File>, path: &str) -> O
|
||||
Some(bytes)
|
||||
}
|
||||
|
||||
// downscales an icon to terminal resolution. resizes to height*2 because each
|
||||
// terminal cell renders two vertical pixels using half-block characters (▀)
|
||||
pub(crate) fn make_icon_pixels(
|
||||
bytes: &[u8],
|
||||
width: u16,
|
||||
@@ -196,7 +211,14 @@ pub(crate) fn make_icon_pixels(
|
||||
let bottom_y = (u32::from(row) * 2 + 1).min(rgb.height().saturating_sub(1));
|
||||
let [tr, tg, tb] = rgb.get_pixel(u32::from(col), top_y).0;
|
||||
let [br, bg, bb] = rgb.get_pixel(u32::from(col), bottom_y).0;
|
||||
cols.push(IconCell { bg_r: br, bg_g: bg, bg_b: bb, fg_r: tr, fg_g: tg, fg_b: tb });
|
||||
cols.push(IconCell {
|
||||
bg_r: br,
|
||||
bg_g: bg,
|
||||
bg_b: bb,
|
||||
fg_r: tr,
|
||||
fg_g: tg,
|
||||
fg_b: tb,
|
||||
});
|
||||
}
|
||||
rows.push(cols);
|
||||
}
|
||||
@@ -206,9 +228,30 @@ pub(crate) fn make_icon_pixels(
|
||||
|
||||
/// 6x3 fallback icon showing a "?" pattern for mods without icons.
|
||||
fn fallback_icon() -> Vec<Vec<IconCell>> {
|
||||
let b = IconCell { bg_r: 50, bg_g: 50, bg_b: 50, fg_r: 50, fg_g: 50, fg_b: 50 };
|
||||
let tb = IconCell { bg_r: 50, bg_g: 50, bg_b: 50, fg_r: 130, fg_g: 130, fg_b: 130 };
|
||||
let bt = IconCell { bg_r: 130, bg_g: 130, bg_b: 130, fg_r: 50, fg_g: 50, fg_b: 50 };
|
||||
let b = IconCell {
|
||||
bg_r: 50,
|
||||
bg_g: 50,
|
||||
bg_b: 50,
|
||||
fg_r: 50,
|
||||
fg_g: 50,
|
||||
fg_b: 50,
|
||||
};
|
||||
let tb = IconCell {
|
||||
bg_r: 50,
|
||||
bg_g: 50,
|
||||
bg_b: 50,
|
||||
fg_r: 130,
|
||||
fg_g: 130,
|
||||
fg_b: 130,
|
||||
};
|
||||
let bt = IconCell {
|
||||
bg_r: 130,
|
||||
bg_g: 130,
|
||||
bg_b: 130,
|
||||
fg_r: 50,
|
||||
fg_g: 50,
|
||||
fg_b: 50,
|
||||
};
|
||||
vec![
|
||||
vec![b, tb, tb, tb, tb, b],
|
||||
vec![b, b, b, bt, bt, b],
|
||||
@@ -216,6 +259,8 @@ fn fallback_icon() -> Vec<Vec<IconCell>> {
|
||||
]
|
||||
}
|
||||
|
||||
// enable/disable by renaming the file with/without ".disabled" suffix.
|
||||
// the minecraft way, apparently.
|
||||
pub fn toggle_entry(entry: &ContentEntry) -> Result<(), std::io::Error> {
|
||||
let file_name = match entry.path.file_name().and_then(|name| name.to_str()) {
|
||||
Some(name) => name,
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
// resource pack scanning. packs can be either .zip files or plain directories,
|
||||
// and metadata lives in pack.mcmeta (which mojang decided should have like 3
|
||||
// different ways to encode a description, because why not)
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
@@ -16,6 +20,8 @@ pub(crate) struct PackInfo {
|
||||
pub description: serde_json::Value,
|
||||
}
|
||||
|
||||
// description can be a plain string, a chat component object with "text",
|
||||
// or an array mixing both. thanks mojang, very cool.
|
||||
pub(crate) fn extract_description(value: &serde_json::Value) -> String {
|
||||
match value {
|
||||
serde_json::Value::String(s) => s.clone(),
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// shader pack scanning. structurally almost identical to resource packs
|
||||
// (zip or dir, pack.mcmeta for metadata, pack.png for icon)
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use super::mods::{make_icon_pixels, ContentEntry};
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
// world save scanning. worlds are always directories (never zips) and store
|
||||
// their icon as icon.png. also computes an approximate size from top-level
|
||||
// files + region data so the user gets some sense of how chonky their world is.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use super::mods::{make_icon_pixels, ContentEntry};
|
||||
@@ -88,6 +92,8 @@ fn world_description(world_dir: &Path) -> String {
|
||||
lines.join("\n")
|
||||
}
|
||||
|
||||
// only counts top-level files + region/ contents, not a full recursive walk.
|
||||
// good enough for a quick size estimate without blocking the UI on huge worlds.
|
||||
fn dir_size_approx(path: &Path) -> u64 {
|
||||
let mut total = 0u64;
|
||||
if let Ok(rd) = std::fs::read_dir(path) {
|
||||
|
||||
Reference in new issue
Block a user