mod management
This commit is contained in:
116
src/content.rs
Normal file
116
src/content.rs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
pub struct ModEntry {
|
||||||
|
pub filename: String,
|
||||||
|
pub enabled: bool,
|
||||||
|
pub id: String,
|
||||||
|
pub name: Option<String>,
|
||||||
|
}
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct FabricModJson {
|
||||||
|
id: String,
|
||||||
|
#[serde(default)]
|
||||||
|
name: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_mod_meta(jar_path: &Path) -> Option<(String, Option<String>)> {
|
||||||
|
let file = std::fs::File::open(jar_path).ok()?;
|
||||||
|
let mut archive = zip::ZipArchive::new(file).ok()?;
|
||||||
|
let entry = archive.by_name("fabric.mod.json").ok()?;
|
||||||
|
let meta: FabricModJson = serde_json::from_reader(entry).ok()?;
|
||||||
|
Some((meta.id, meta.name))
|
||||||
|
}
|
||||||
|
fn entry_from_name(dir: &Path, name: String) -> Option<ModEntry> {
|
||||||
|
let jar = dir.join(&name);
|
||||||
|
let enabled = if name.ends_with(".jar") {
|
||||||
|
true
|
||||||
|
} else if name.ends_with(".jar.disabled") {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let (id, pretty) = match read_mod_meta(&jar) {
|
||||||
|
Some(info) => info,
|
||||||
|
None => {
|
||||||
|
let base = name
|
||||||
|
.strip_suffix(".disabled")
|
||||||
|
.unwrap_or(&name)
|
||||||
|
.strip_suffix(".jar")
|
||||||
|
.unwrap_or(&name)
|
||||||
|
.to_string();
|
||||||
|
(base, None)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Some(ModEntry {
|
||||||
|
filename: name,
|
||||||
|
enabled,
|
||||||
|
id,
|
||||||
|
name: pretty,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
pub fn list(dir: &Path) -> anyhow::Result<Vec<ModEntry>> {
|
||||||
|
if !dir.exists() {
|
||||||
|
return Ok(Vec::new());
|
||||||
|
}
|
||||||
|
let mut mods = Vec::new();
|
||||||
|
for e in std::fs::read_dir(dir)? {
|
||||||
|
let name = e?.file_name().to_string_lossy().to_string();
|
||||||
|
if let Some(entry) = entry_from_name(dir, name) {
|
||||||
|
mods.push(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mods.sort_by_key(|m| m.filename.clone());
|
||||||
|
Ok(mods)
|
||||||
|
}
|
||||||
|
pub fn disable(dir: &Path, item: &str) -> anyhow::Result<()> {
|
||||||
|
let entries = list(dir)?;
|
||||||
|
let entry = entries
|
||||||
|
.iter()
|
||||||
|
.find(|m| m.id == item || m.filename == item)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("no mod matching {item}"))?;
|
||||||
|
if !entry.enabled {
|
||||||
|
println!("{item} is already disabled");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let new_name = format!("{}.disabled", entry.filename);
|
||||||
|
std::fs::rename(dir.join(&entry.filename), dir.join(&new_name))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn enable(dir: &Path, item: &str) -> anyhow::Result<()> {
|
||||||
|
let entries = list(dir)?;
|
||||||
|
let entry = entries
|
||||||
|
.iter()
|
||||||
|
.find(|m| m.id == item || m.filename == item)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("no mod matching {item}"))?;
|
||||||
|
if entry.enabled {
|
||||||
|
println!("{item} is already enabled");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let new_name = entry.filename.strip_suffix(".disabled").unwrap();
|
||||||
|
std::fs::rename(dir.join(&entry.filename), dir.join(new_name))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete(dir: &Path, item: &str) -> anyhow::Result<()> {
|
||||||
|
let entries = list(dir)?;
|
||||||
|
let entry = entries
|
||||||
|
.iter()
|
||||||
|
.find(|m| m.id == item || m.filename == item)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("no mod matching {item}"))?;
|
||||||
|
std::fs::remove_file(dir.join(&entry.filename))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn add(dir: &Path, source: &Path) -> anyhow::Result<()> {
|
||||||
|
if !source.is_file() {
|
||||||
|
anyhow::bail!("not a file: {}", source.display());
|
||||||
|
}
|
||||||
|
let filename = source
|
||||||
|
.file_name()
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("invalid source path"))?;
|
||||||
|
let dest = dir.join(filename);
|
||||||
|
if dest.exists() {
|
||||||
|
anyhow::bail!("a mod named {:?} already exists", filename);
|
||||||
|
}
|
||||||
|
std::fs::copy(source, &dest)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
64
src/main.rs
64
src/main.rs
@@ -2,6 +2,7 @@
|
|||||||
compile_error!("UML only supports Linux.");
|
compile_error!("UML only supports Linux.");
|
||||||
|
|
||||||
mod auth; // this is painful, ai helped a bit btw (i try to use ai as little as possible, but this part's just ANNOYING)
|
mod auth; // this is painful, ai helped a bit btw (i try to use ai as little as possible, but this part's just ANNOYING)
|
||||||
|
mod content;
|
||||||
mod download;
|
mod download;
|
||||||
mod fabric;
|
mod fabric;
|
||||||
mod instance;
|
mod instance;
|
||||||
@@ -31,6 +32,25 @@ struct Cli {
|
|||||||
enum LoaderKind {
|
enum LoaderKind {
|
||||||
Fabric,
|
Fabric,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum ModVerb {
|
||||||
|
List,
|
||||||
|
Add {
|
||||||
|
path: PathBuf,
|
||||||
|
},
|
||||||
|
Enable {
|
||||||
|
item: String,
|
||||||
|
},
|
||||||
|
Disable {
|
||||||
|
item: String,
|
||||||
|
},
|
||||||
|
#[command(alias = "remove")]
|
||||||
|
Delete {
|
||||||
|
item: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Command {
|
enum Command {
|
||||||
/// Launch an instance
|
/// Launch an instance
|
||||||
@@ -42,6 +62,12 @@ enum Command {
|
|||||||
// #[arg(long)]
|
// #[arg(long)]
|
||||||
// offline: bool,
|
// offline: bool,
|
||||||
},
|
},
|
||||||
|
/// Manage things inside instances (mods, worlds, etc...)
|
||||||
|
Instance {
|
||||||
|
name: String,
|
||||||
|
#[command(subcommand)]
|
||||||
|
action: InstanceAction,
|
||||||
|
},
|
||||||
/// Manage instances
|
/// Manage instances
|
||||||
Instances {
|
Instances {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
@@ -50,7 +76,13 @@ enum Command {
|
|||||||
/// Open an instance folder
|
/// Open an instance folder
|
||||||
Folder { name: String },
|
Folder { name: String },
|
||||||
}
|
}
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum InstanceAction {
|
||||||
|
Mods {
|
||||||
|
#[command(subcommand)]
|
||||||
|
verb: ModVerb,
|
||||||
|
},
|
||||||
|
}
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum InstanceCmd {
|
enum InstanceCmd {
|
||||||
/// Create a new instance
|
/// Create a new instance
|
||||||
@@ -226,6 +258,36 @@ fn main() -> anyhow::Result<()> {
|
|||||||
&game_version,
|
&game_version,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
Command::Instance { name, action } => {
|
||||||
|
if !is_valid_name(&name) {
|
||||||
|
anyhow::bail!("invalid instance name: {name:?}");
|
||||||
|
}
|
||||||
|
let mods_dir = instances_dir.join(&name).join("mods");
|
||||||
|
match action {
|
||||||
|
InstanceAction::Mods { verb } => match verb {
|
||||||
|
ModVerb::List => {
|
||||||
|
let mods = content::list(&mods_dir)?;
|
||||||
|
if mods.is_empty() {
|
||||||
|
println!("No mods installed.");
|
||||||
|
} else {
|
||||||
|
for m in content::list(&mods_dir)? {
|
||||||
|
let display = m.name.as_deref().unwrap_or(&m.id);
|
||||||
|
if m.enabled {
|
||||||
|
println!("{display} ({})", m.id);
|
||||||
|
} else {
|
||||||
|
println!("{display} ({}) (disabled)", m.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("Make sure to specify mods by the ID.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ModVerb::Add { path } => content::add(&mods_dir, &path)?,
|
||||||
|
ModVerb::Enable { item } => content::enable(&mods_dir, &item)?,
|
||||||
|
ModVerb::Disable { item } => content::disable(&mods_dir, &item)?,
|
||||||
|
ModVerb::Delete { item } => content::delete(&mods_dir, &item)?,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user