Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 365fc81c03 | |||
| bdb7386480 | |||
| 4f802ca298 | |||
| 41afbc53dc | |||
| 30d6749d8c | |||
| fef78fabc8 | |||
| 4ea25bda37 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1166,7 +1166,7 @@ checksum = "81e544489bf3d8ef66c953931f56617f423cd4b5494be343d9b9d3dda037b9a3"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "untitled-minecraft-launcher"
|
name = "untitled-minecraft-launcher"
|
||||||
version = "0.1.0"
|
version = "0.4.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "untitled-minecraft-launcher"
|
name = "untitled-minecraft-launcher"
|
||||||
version = "0.1.0"
|
version = "0.4.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
# UML (untitled minecraft launcher)
|
# UML (untitled minecraft launcher)
|
||||||
This is a VERY work in progress minecraft launcher.
|
This is a custom CLI Minecraft launcher made in Rust.
|
||||||
More info will be available soon.
|
More info is at [uml.rs](https://uml.rs)
|
||||||
Sorry about it.
|
|
||||||
|
|
||||||
## THIS IS ONLY AVAILABLE ON LINUX RIGHT NOW
|
## THIS IS ONLY AVAILABLE ON LINUX
|
||||||
and windows/macos/others will probably not be supported, go use prismlauncher (its the better one anyway, for now at least...)
|
|
||||||
@@ -114,7 +114,7 @@ fn refresh(agent: &Agent, refresh: &str) -> anyhow::Result<(String, String)> {
|
|||||||
}
|
}
|
||||||
fn poll(agent: &Agent, device: &Device) -> anyhow::Result<(String, String)> {
|
fn poll(agent: &Agent, device: &Device) -> anyhow::Result<(String, String)> {
|
||||||
println!(
|
println!(
|
||||||
"Go to {} and enter code: {}",
|
"Go to {} and enter code: {}.",
|
||||||
device.verification_uri, device.user_code
|
device.verification_uri, device.user_code
|
||||||
);
|
);
|
||||||
loop {
|
loop {
|
||||||
@@ -129,7 +129,7 @@ fn poll(agent: &Agent, device: &Device) -> anyhow::Result<(String, String)> {
|
|||||||
|
|
||||||
if let Some(token) = body.access_token {
|
if let Some(token) = body.access_token {
|
||||||
let refresh = body.refresh_token.ok_or_else(|| {
|
let refresh = body.refresh_token.ok_or_else(|| {
|
||||||
anyhow::anyhow!("no refresh token — was offline_access in the scope?")
|
anyhow::anyhow!("no refresh token, was offline_access in the scope?")
|
||||||
})?;
|
})?;
|
||||||
return Ok((token, refresh));
|
return Ok((token, refresh));
|
||||||
}
|
}
|
||||||
@@ -176,10 +176,10 @@ fn xsts(agent: &Agent, xbl_token: &str) -> anyhow::Result<XblResponse> {
|
|||||||
anyhow::bail!(
|
anyhow::bail!(
|
||||||
"{}",
|
"{}",
|
||||||
match e.xerr {
|
match e.xerr {
|
||||||
2148916233 => "no Xbox profile — sign in at minecraft.net once".into(),
|
2148916233 => "no Xbox profile, sign in at minecraft.net once".into(),
|
||||||
2148916235 => "Xbox Live unavailable in this region".into(),
|
2148916235 => "Xbox Live unavailable in this region".into(),
|
||||||
2148916237 => "account needs adult verification".into(),
|
2148916237 => "account needs adult verification".into(),
|
||||||
2148916238 => "child account — must be added to a Family".into(),
|
2148916238 => "child account, must be added to a Family".into(),
|
||||||
other => format!("XSTS failed, XErr {other}"),
|
other => format!("XSTS failed, XErr {other}"),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
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(())
|
||||||
|
}
|
||||||
@@ -10,7 +10,9 @@ pub fn download(url: &str, path: &Path) -> anyhow::Result<()> {
|
|||||||
if let Some(parent) = path.parent() {
|
if let Some(parent) = path.parent() {
|
||||||
create_dir_all(parent)?;
|
create_dir_all(parent)?;
|
||||||
}
|
}
|
||||||
let mut res = get(url).call()?;
|
let mut res = get(url)
|
||||||
|
.header("User-Agent", crate::util::USER_AGENT)
|
||||||
|
.call()?;
|
||||||
let mut file = File::create(path)?;
|
let mut file = File::create(path)?;
|
||||||
copy(&mut res.body_mut().as_reader(), &mut file)?;
|
copy(&mut res.body_mut().as_reader(), &mut file)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
use anyhow::Ok;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
fs::create_dir_all,
|
fs::create_dir_all,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::util::is_valid_name;
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
pub version: String,
|
pub version: String,
|
||||||
@@ -18,8 +19,8 @@ pub struct Loader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn create(instances_dir: &Path, name: &str, version: &str) -> anyhow::Result<PathBuf> {
|
pub fn create(instances_dir: &Path, name: &str, version: &str) -> anyhow::Result<PathBuf> {
|
||||||
if name.contains('/') {
|
if !is_valid_name(name) {
|
||||||
anyhow::bail!("unusable characters in path.");
|
anyhow::bail!("invalid instance name: {name:?}");
|
||||||
}
|
}
|
||||||
let directory = instances_dir.join(name);
|
let directory = instances_dir.join(name);
|
||||||
if directory.join("instance.json").exists() {
|
if directory.join("instance.json").exists() {
|
||||||
@@ -63,6 +64,9 @@ pub fn list(root: &Path) -> anyhow::Result<Vec<String>> {
|
|||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
pub fn remove(instances_dir: &Path, name: &str, yes: bool) -> anyhow::Result<()> {
|
pub fn remove(instances_dir: &Path, name: &str, yes: bool) -> anyhow::Result<()> {
|
||||||
|
if !is_valid_name(name) {
|
||||||
|
anyhow::bail!("invalid instance name: {name:?}");
|
||||||
|
}
|
||||||
let dir = instances_dir.join(&name);
|
let dir = instances_dir.join(&name);
|
||||||
if !dir.exists() {
|
if !dir.exists() {
|
||||||
anyhow::bail!("no instance named {name}");
|
anyhow::bail!("no instance named {name}");
|
||||||
@@ -77,7 +81,25 @@ pub fn remove(instances_dir: &Path, name: &str, yes: bool) -> anyhow::Result<()>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::fs::remove_dir_all(&dir)?;
|
std::fs::remove_dir_all(&dir)?;
|
||||||
println!("Removed {name}.");
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn rename(instances_dir: &Path, old_name: &str, new_name: &str) -> anyhow::Result<()> {
|
||||||
|
if !is_valid_name(old_name) {
|
||||||
|
anyhow::bail!("invalid original instance name: {old_name:?}");
|
||||||
|
}
|
||||||
|
if !is_valid_name(new_name) {
|
||||||
|
anyhow::bail!("invalid new instance name: {new_name:?}");
|
||||||
|
}
|
||||||
|
let instance_dir = instances_dir.join(&old_name);
|
||||||
|
let new_dir = instances_dir.join(&new_name);
|
||||||
|
if !instance_dir.exists() {
|
||||||
|
anyhow::bail!("original instance name doesn't exist");
|
||||||
|
}
|
||||||
|
if new_dir.exists() {
|
||||||
|
anyhow::bail!("new instance name already exists");
|
||||||
|
}
|
||||||
|
std::fs::rename(instance_dir, new_dir)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn sanitize(name: &str) -> String {
|
pub fn sanitize(name: &str) -> String {
|
||||||
|
|||||||
135
src/main.rs
135
src/main.rs
@@ -1,18 +1,30 @@
|
|||||||
|
#[cfg(not(target_os = "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;
|
||||||
mod launch;
|
mod launch;
|
||||||
mod meta;
|
mod meta;
|
||||||
|
mod modrinth;
|
||||||
mod mrpack;
|
mod mrpack;
|
||||||
|
mod overlay;
|
||||||
mod prism;
|
mod prism;
|
||||||
use crate::{launch::run, meta::fetch_version};
|
mod util;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
use launch::run;
|
||||||
|
use meta::fetch_version;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use util::{is_valid_name, open_path};
|
||||||
|
|
||||||
/// Untitled Minecraft Launcher
|
/// Untitled Minecraft Launcher
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(name = "uml")]
|
#[command(name = "uml")]
|
||||||
|
#[command(
|
||||||
|
after_help = "For any instance names with spaces, other than the launch command, enclose it in double quotes."
|
||||||
|
)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
command: Command,
|
command: Command,
|
||||||
@@ -21,6 +33,30 @@ 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)]
|
||||||
|
enum OverlayCmd {
|
||||||
|
/// Apply an overlay
|
||||||
|
Apply { overlay: String, instance: String },
|
||||||
|
// add List, Create, Add, Remove, Unapply, Strip
|
||||||
|
}
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Command {
|
enum Command {
|
||||||
/// Launch an instance
|
/// Launch an instance
|
||||||
@@ -32,13 +68,33 @@ 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 overlays
|
||||||
|
Overlays {
|
||||||
|
#[command(subcommand)]
|
||||||
|
cmd: OverlayCmd,
|
||||||
|
},
|
||||||
/// Manage instances
|
/// Manage instances
|
||||||
Instances {
|
Instances {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
action: InstanceCmd,
|
action: InstanceCmd,
|
||||||
},
|
},
|
||||||
|
/// Open an instance folder
|
||||||
|
Folder { name: String },
|
||||||
|
}
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum InstanceAction {
|
||||||
|
/// Manage instance mods
|
||||||
|
Mods {
|
||||||
|
#[command(subcommand)]
|
||||||
|
verb: ModVerb,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum InstanceCmd {
|
enum InstanceCmd {
|
||||||
/// Create a new instance
|
/// Create a new instance
|
||||||
@@ -47,6 +103,11 @@ enum InstanceCmd {
|
|||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
version: String,
|
version: String,
|
||||||
},
|
},
|
||||||
|
/// Rename an instance
|
||||||
|
Rename {
|
||||||
|
current_name: String,
|
||||||
|
new_name: String,
|
||||||
|
},
|
||||||
/// Import a Modrinth modpack
|
/// Import a Modrinth modpack
|
||||||
Import {
|
Import {
|
||||||
pack: PathBuf,
|
pack: PathBuf,
|
||||||
@@ -93,10 +154,20 @@ fn data_dir() -> anyhow::Result<PathBuf> {
|
|||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let uml_dir = data_dir()?;
|
let uml_dir = data_dir()?;
|
||||||
let instances_dir = uml_dir.join("instances");
|
let instances_dir = uml_dir.join("instances");
|
||||||
|
let overlays_dir = uml_dir.join("overlays");
|
||||||
let shared_dir = uml_dir.join("shared");
|
let shared_dir = uml_dir.join("shared");
|
||||||
|
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
match cli.command {
|
match cli.command {
|
||||||
|
Command::Folder { name } => {
|
||||||
|
if !is_valid_name(&name) {
|
||||||
|
anyhow::bail!("invalid instance name: {name:?}");
|
||||||
|
}
|
||||||
|
let dir = instances_dir.join(&name);
|
||||||
|
if !dir.exists() {
|
||||||
|
anyhow::bail!("no instance named {name}");
|
||||||
|
}
|
||||||
|
open_path(&dir)?;
|
||||||
|
}
|
||||||
Command::Instances { action } => match action {
|
Command::Instances { action } => match action {
|
||||||
InstanceCmd::New { name, version } => {
|
InstanceCmd::New { name, version } => {
|
||||||
if !meta::version_exists(&version)? {
|
if !meta::version_exists(&version)? {
|
||||||
@@ -104,6 +175,13 @@ fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
instance::create(&instances_dir, &name, &version)?;
|
instance::create(&instances_dir, &name, &version)?;
|
||||||
}
|
}
|
||||||
|
InstanceCmd::Rename {
|
||||||
|
current_name,
|
||||||
|
new_name,
|
||||||
|
} => {
|
||||||
|
instance::rename(&instances_dir, ¤t_name, &new_name)?;
|
||||||
|
println!("Renamed {} to {}.", current_name, new_name);
|
||||||
|
}
|
||||||
InstanceCmd::Import { pack, name, yes } => {
|
InstanceCmd::Import { pack, name, yes } => {
|
||||||
mrpack::import(&pack, &instances_dir, name.as_deref(), yes)?;
|
mrpack::import(&pack, &instances_dir, name.as_deref(), yes)?;
|
||||||
}
|
}
|
||||||
@@ -112,6 +190,7 @@ fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
InstanceCmd::Remove { name, yes } => {
|
InstanceCmd::Remove { name, yes } => {
|
||||||
instance::remove(&instances_dir, &name, yes)?;
|
instance::remove(&instances_dir, &name, yes)?;
|
||||||
|
println!("Removed {name}.");
|
||||||
}
|
}
|
||||||
InstanceCmd::List => {
|
InstanceCmd::List => {
|
||||||
for n in instance::list(&instances_dir)? {
|
for n in instance::list(&instances_dir)? {
|
||||||
@@ -191,6 +270,56 @@ 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)?,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Command::Overlays { cmd } => match cmd {
|
||||||
|
OverlayCmd::Apply { overlay, instance } => {
|
||||||
|
if !is_valid_name(&instance) {
|
||||||
|
anyhow::bail!("invalid instance name: {instance:?}");
|
||||||
|
}
|
||||||
|
if !is_valid_name(&overlay) {
|
||||||
|
anyhow::bail!("invalid overlay name: {overlay:?}");
|
||||||
|
}
|
||||||
|
let mods_dir = instances_dir.join(&instance).join("mods");
|
||||||
|
let (cfg, _) = instance::load(&instances_dir, &instance)?;
|
||||||
|
let loader = cfg
|
||||||
|
.loader
|
||||||
|
.as_ref()
|
||||||
|
.map(|l| l.kind.as_str())
|
||||||
|
.ok_or_else(|| {
|
||||||
|
anyhow::anyhow!("instance has no loader, overlays need a modded instance")
|
||||||
|
})?;
|
||||||
|
overlay::apply(&overlays_dir, &overlay, &mods_dir, loader, &cfg.version)?;
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
62
src/modrinth.rs
Normal file
62
src/modrinth.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
download::{self, verify_sha512},
|
||||||
|
util::Hashes,
|
||||||
|
};
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
const USER_AGENT: &str = "owenthepuppy/uml/0.3.0";
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct Version {
|
||||||
|
pub files: Vec<VersionFile>,
|
||||||
|
}
|
||||||
|
#[derive(Deserialize, Clone)]
|
||||||
|
pub struct VersionFile {
|
||||||
|
pub url: String,
|
||||||
|
pub filename: String,
|
||||||
|
pub hashes: Hashes,
|
||||||
|
pub primary: bool,
|
||||||
|
}
|
||||||
|
pub fn get_versions(slug: &str, loader: &str, game_version: &str) -> anyhow::Result<Vec<Version>> {
|
||||||
|
let versions = match ureq::get(format!(
|
||||||
|
"https://api.modrinth.com/v2/project/{slug}/version"
|
||||||
|
))
|
||||||
|
.header("User-Agent", USER_AGENT)
|
||||||
|
.query("loaders", format!("[\"{loader}\"]"))
|
||||||
|
.query("game_versions", format!("[\"{game_version}\"]"))
|
||||||
|
.call()
|
||||||
|
{
|
||||||
|
Ok(mut resp) => resp.body_mut().read_json()?,
|
||||||
|
Err(ureq::Error::StatusCode(404)) => {
|
||||||
|
anyhow::bail!("mod not found: {slug}");
|
||||||
|
}
|
||||||
|
Err(e) => return Err(e.into()),
|
||||||
|
};
|
||||||
|
Ok(versions)
|
||||||
|
}
|
||||||
|
pub fn get_version(slug: &str, loader: &str, game_version: &str) -> anyhow::Result<VersionFile> {
|
||||||
|
let mod_versions = get_versions(slug, loader, game_version)?;
|
||||||
|
let latest_version = mod_versions
|
||||||
|
.first()
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("no compatible version for {slug}"))?;
|
||||||
|
let file = latest_version
|
||||||
|
.files
|
||||||
|
.iter()
|
||||||
|
.find(|f| f.primary)
|
||||||
|
.or_else(|| latest_version.files.first())
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("version has no files"))?;
|
||||||
|
Ok(file.clone())
|
||||||
|
}
|
||||||
|
pub fn download_mod(file: &VersionFile, mods_dir: &Path) -> anyhow::Result<()> {
|
||||||
|
let dest = mods_dir.join(&file.filename);
|
||||||
|
download::download(&file.url, &dest)?;
|
||||||
|
if !verify_sha512(&dest, &file.hashes.sha512)? {
|
||||||
|
anyhow::bail!(
|
||||||
|
"hash mismatch after download: {:?}. This file is NOT DELETED AND WILL LOAD ON NEXT LAUNCH. DO NOT LAUNCH MINECRAFT, until you are SURE this is safe.",
|
||||||
|
dest
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
download::download,
|
download::{download, verify_sha512},
|
||||||
download::verify_sha512,
|
|
||||||
instance::{Loader, create, load, save},
|
instance::{Loader, create, load, save},
|
||||||
|
util::{Hashes, is_safe_relative_path},
|
||||||
};
|
};
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@@ -26,10 +26,6 @@ struct PackFile {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
env: Option<Env>,
|
env: Option<Env>,
|
||||||
}
|
}
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct Hashes {
|
|
||||||
sha512: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Env {
|
struct Env {
|
||||||
@@ -51,10 +47,9 @@ fn extract_overrides(
|
|||||||
if rel.is_empty() || entry.is_dir() {
|
if rel.is_empty() || entry.is_dir() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if rel.contains("..") || rel.starts_with('/') {
|
if !is_safe_relative_path(rel) {
|
||||||
anyhow::bail!("suspicious path in pack: {name}");
|
anyhow::bail!("suspicious path in pack: {name}");
|
||||||
}
|
}
|
||||||
|
|
||||||
let dest = instance_dir.join(rel);
|
let dest = instance_dir.join(rel);
|
||||||
if let Some(parent) = dest.parent() {
|
if let Some(parent) = dest.parent() {
|
||||||
std::fs::create_dir_all(parent)?;
|
std::fs::create_dir_all(parent)?;
|
||||||
@@ -146,7 +141,7 @@ pub fn import(
|
|||||||
pb.set_style(ProgressStyle::with_template("{bar:40} {pos}/{len} {msg}").unwrap());
|
pb.set_style(ProgressStyle::with_template("{bar:40} {pos}/{len} {msg}").unwrap());
|
||||||
|
|
||||||
for f in &wanted {
|
for f in &wanted {
|
||||||
if f.path.contains("..") || f.path.starts_with('/') {
|
if !is_safe_relative_path(&f.path) {
|
||||||
anyhow::bail!("suspicious path in pack: {}", f.path);
|
anyhow::bail!("suspicious path in pack: {}", f.path);
|
||||||
}
|
}
|
||||||
pb.set_message(f.path.clone());
|
pb.set_message(f.path.clone());
|
||||||
@@ -158,7 +153,10 @@ pub fn import(
|
|||||||
.ok_or_else(|| anyhow::anyhow!("no download url for {}", f.path))?;
|
.ok_or_else(|| anyhow::anyhow!("no download url for {}", f.path))?;
|
||||||
download(url, &dest)?;
|
download(url, &dest)?;
|
||||||
if !verify_sha512(&dest, &f.hashes.sha512)? {
|
if !verify_sha512(&dest, &f.hashes.sha512)? {
|
||||||
anyhow::bail!("hash mismatch after download: {}", f.path);
|
anyhow::bail!(
|
||||||
|
"hash mismatch after download: {}. This file is NOT DELETED AND WILL LOAD ON NEXT LAUNCH. DO NOT LAUNCH MINECRAFT, until you are SURE this is safe.",
|
||||||
|
f.path
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pb.inc(1);
|
pb.inc(1);
|
||||||
|
|||||||
70
src/overlay.rs
Normal file
70
src/overlay.rs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::{content, modrinth};
|
||||||
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct Overlay {
|
||||||
|
pub name: String,
|
||||||
|
pub mods: Vec<OverlayMod>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct OverlayMod {
|
||||||
|
pub source: String,
|
||||||
|
pub id: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub pin: Option<String>, // NOT IMPLEMENTED YET
|
||||||
|
}
|
||||||
|
pub fn load(overlays_dir: &Path, name: &str) -> anyhow::Result<Overlay> {
|
||||||
|
let path = overlays_dir.join(format!("{name}.umloverlay"));
|
||||||
|
if !path.exists() {
|
||||||
|
anyhow::bail!("no overlay named {name}");
|
||||||
|
}
|
||||||
|
let json = std::fs::read_to_string(path)?;
|
||||||
|
Ok(serde_json::from_str(&json)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(overlays_dir: &Path, overlay: &Overlay) -> anyhow::Result<()> {
|
||||||
|
let path = overlays_dir.join(format!("{}.umloverlay", overlay.name));
|
||||||
|
let json = serde_json::to_string_pretty(overlay)?;
|
||||||
|
std::fs::write(path, json)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn apply(
|
||||||
|
overlays_dir: &Path,
|
||||||
|
name: &str,
|
||||||
|
mods_dir: &Path,
|
||||||
|
loader: &str,
|
||||||
|
game_version: &str,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let overlay = load(overlays_dir, name)?;
|
||||||
|
let existing = content::list(mods_dir)?;
|
||||||
|
let all_present = overlay
|
||||||
|
.mods
|
||||||
|
.iter()
|
||||||
|
.all(|m| existing.iter().any(|e| e.id == m.id));
|
||||||
|
if all_present {
|
||||||
|
println!(
|
||||||
|
"Overlay '{}' is already fully applied, nothing to do.",
|
||||||
|
overlay.name
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let pb = ProgressBar::new(overlay.mods.len() as u64);
|
||||||
|
pb.set_style(ProgressStyle::with_template("{bar:40} {pos}/{len} {msg}").unwrap());
|
||||||
|
for m in overlay.mods {
|
||||||
|
if existing.iter().any(|e| e.id == m.id) {
|
||||||
|
println!("Skipping {} (already installed).", m.id);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
pb.println(format!("Downloading {}...", m.id)); // not println!
|
||||||
|
let file = modrinth::get_version(&m.id, loader, game_version)?;
|
||||||
|
modrinth::download_mod(&file, &mods_dir)?;
|
||||||
|
pb.inc(1);
|
||||||
|
}
|
||||||
|
pb.finish_with_message("done");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
27
src/util.rs
Normal file
27
src/util.rs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
pub const USER_AGENT: &str = concat!("owenthepuppy/uml/", env!("CARGO_PKG_VERSION"));
|
||||||
|
|
||||||
|
pub fn is_valid_name(name: &str) -> bool {
|
||||||
|
let mut components = Path::new(name).components();
|
||||||
|
matches!(
|
||||||
|
(components.next(), components.next()),
|
||||||
|
(Some(std::path::Component::Normal(_)), None)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
pub fn is_safe_relative_path(path: &str) -> bool {
|
||||||
|
let p = Path::new(path);
|
||||||
|
!p.as_os_str().is_empty()
|
||||||
|
&& p.components()
|
||||||
|
.all(|c| matches!(c, std::path::Component::Normal(_)))
|
||||||
|
}
|
||||||
|
pub fn open_path(path: &Path) -> anyhow::Result<()> {
|
||||||
|
std::process::Command::new("xdg-open").arg(path).spawn()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
#[derive(Deserialize, Clone)]
|
||||||
|
pub struct Hashes {
|
||||||
|
pub sha512: String,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user