modrinth mod dl and update version num (oops)
This commit is contained in:
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]
|
||||||
|
|||||||
@@ -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(())
|
||||||
|
|||||||
@@ -8,15 +8,17 @@ 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;
|
||||||
mod util;
|
mod util;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use launch::run;
|
use launch::run;
|
||||||
use meta::fetch_version;
|
use meta::fetch_version;
|
||||||
|
use modrinth::{download_mod, get_version};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use util::{is_valid_name, open_path};
|
||||||
use crate::util::{is_valid_name, open_path};
|
|
||||||
|
|
||||||
/// Untitled Minecraft Launcher
|
/// Untitled Minecraft Launcher
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
|||||||
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, verify_sha512},
|
download::{download, verify_sha512},
|
||||||
instance::{Loader, create, load, save},
|
instance::{Loader, create, load, save},
|
||||||
util::is_safe_relative_path,
|
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 {
|
||||||
@@ -157,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);
|
||||||
|
|||||||
0
src/overlay.rs
Normal file
0
src/overlay.rs
Normal file
@@ -1,5 +1,9 @@
|
|||||||
use std::path::Path;
|
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 {
|
pub fn is_valid_name(name: &str) -> bool {
|
||||||
let mut components = Path::new(name).components();
|
let mut components = Path::new(name).components();
|
||||||
matches!(
|
matches!(
|
||||||
@@ -17,3 +21,7 @@ pub fn open_path(path: &Path) -> anyhow::Result<()> {
|
|||||||
std::process::Command::new("xdg-open").arg(path).spawn()?;
|
std::process::Command::new("xdg-open").arg(path).spawn()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
#[derive(Deserialize, Clone)]
|
||||||
|
pub struct Hashes {
|
||||||
|
pub sha512: String,
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user