Added a way to add mods
This commit is contained in:
parent
20dddba0d5
commit
d7c50ec8be
@ -24,4 +24,7 @@ pub enum Commands {
|
||||
/// The name of the mod to add
|
||||
name: String,
|
||||
},
|
||||
|
||||
/// Remove a mod from the modpack
|
||||
Remove,
|
||||
}
|
||||
|
@ -12,6 +12,9 @@ async fn main() {
|
||||
cli::Commands::Init => {
|
||||
pack::Pack::init().await;
|
||||
}
|
||||
cli::Commands::Add { name } => {
|
||||
pack::Pack::add(name).await;
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
use core::fmt;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
@ -8,9 +10,63 @@ pub struct MinecraftVersion {
|
||||
pub major: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Mod {
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub project_id: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for Mod {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.title)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Search {
|
||||
hits: Vec<Mod>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct ProjectVersion {
|
||||
pub name: String,
|
||||
pub game_versions: Vec<String>,
|
||||
pub version_type: String,
|
||||
pub id: String,
|
||||
pub project_id: String,
|
||||
}
|
||||
|
||||
pub async fn get_minecraft_versions() -> Result<Vec<MinecraftVersion>, reqwest::Error> {
|
||||
reqwest::get("https://api.modrinth.com/v2/tag/game_version")
|
||||
.await?
|
||||
.json::<Vec<MinecraftVersion>>()
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn search_projects(
|
||||
query: String,
|
||||
version: &String,
|
||||
loader: &String,
|
||||
) -> Result<Vec<Mod>, reqwest::Error> {
|
||||
let url = format!("https://api.modrinth.com/v2/search?query={query}&facets=[[\"project_type:mod\"],[\"versions:{version}\"],[\"categories:{loader}\"]]");
|
||||
|
||||
let response = reqwest::get(url).await?.json::<Search>().await?;
|
||||
|
||||
Ok(response.hits)
|
||||
}
|
||||
|
||||
pub async fn get_project_versions(
|
||||
id: &String,
|
||||
version: &String,
|
||||
loader: &String,
|
||||
) -> Result<Vec<ProjectVersion>, reqwest::Error> {
|
||||
let url = format!("https://api.modrinth.com/v2/project/{id}/version?loaders=[\"{loader}\"]&game_versions=[\"{version}\"]");
|
||||
|
||||
let response = reqwest::get(url)
|
||||
.await?
|
||||
.json::<Vec<ProjectVersion>>()
|
||||
.await?;
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
64
src/pack.rs
64
src/pack.rs
@ -1,21 +1,31 @@
|
||||
use crate::modrinth;
|
||||
use colored::*;
|
||||
use inquire::Select;
|
||||
use serde::Serialize;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
fmt::{Display, Formatter},
|
||||
fs,
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Pack {
|
||||
info: Info,
|
||||
mods: Vec<Mod>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Mod {
|
||||
id: String,
|
||||
version_id: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Info {
|
||||
loader: Modloader,
|
||||
minecraft_version: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug, Clone, Copy)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
|
||||
pub enum Modloader {
|
||||
Fabric,
|
||||
Quilt,
|
||||
@ -35,6 +45,12 @@ impl Display for Modloader {
|
||||
}
|
||||
|
||||
impl Pack {
|
||||
pub fn get() -> Self {
|
||||
let pack_string = fs::read_to_string(".packium.toml").unwrap();
|
||||
|
||||
toml::from_str(&pack_string).unwrap()
|
||||
}
|
||||
|
||||
pub async fn init() {
|
||||
println!("Fetching Minecraft information...");
|
||||
|
||||
@ -43,7 +59,7 @@ impl Pack {
|
||||
let versions = versions
|
||||
.iter()
|
||||
.filter(|x| x.version_type == "release".to_string())
|
||||
.map(|x| x.version.clone())
|
||||
.map(|x| &x.version)
|
||||
.collect();
|
||||
|
||||
let modloader = Select::new("Modloader?", Modloader::VARIANTS.to_vec())
|
||||
@ -56,13 +72,47 @@ impl Pack {
|
||||
|
||||
let pack = Pack {
|
||||
info: Info {
|
||||
minecraft_version: version,
|
||||
minecraft_version: version.to_owned(),
|
||||
loader: modloader,
|
||||
},
|
||||
mods: vec![],
|
||||
};
|
||||
|
||||
let pack_toml = toml::to_string_pretty(&pack).unwrap();
|
||||
|
||||
std::fs::write(".packium.toml", pack_toml).unwrap();
|
||||
}
|
||||
|
||||
pub async fn add(name: &String) {
|
||||
let mut pack = Self::get();
|
||||
|
||||
let mods = modrinth::search_projects(
|
||||
name.to_owned(),
|
||||
&pack.info.minecraft_version,
|
||||
&pack.info.loader.to_string().to_lowercase(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let project = Select::new("Choose a mod", mods).prompt().unwrap();
|
||||
|
||||
let versions = modrinth::get_project_versions(
|
||||
&project.project_id,
|
||||
&pack.info.minecraft_version,
|
||||
&pack.info.loader.to_string().to_lowercase(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let version = versions.get(0).unwrap();
|
||||
|
||||
pack.mods.push(Mod {
|
||||
id: project.project_id.clone(),
|
||||
version_id: version.id.clone(),
|
||||
});
|
||||
|
||||
let pack_toml = toml::to_string_pretty(&pack).unwrap();
|
||||
|
||||
std::fs::write(".packium.toml", pack_toml).unwrap();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user