mcsm/src/main.rs

124 lines
4.7 KiB
Rust
Raw Normal View History

2026-08-30 16:38:53 +05:00
mod models;
mod utils;
2026-08-30 16:38:53 +05:00
use crate::{models::vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions}, utils::with_spinner};
use clap::{
Parser, Subcommand,
builder::styling::{self},
};
2026-08-30 16:38:53 +05:00
use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme};
use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use sha1::{Digest, Sha1};
use std::error::Error;
use tokio::io::AsyncWriteExt;
2026-08-30 16:38:53 +05:00
fn styles() -> styling::Styles {
styling::Styles::styled()
.header(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
.usage(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
.literal(styling::AnsiColor::Cyan.on_default() | styling::Effects::BOLD)
}
#[derive(Parser)]
#[command(name = "mcsm", version, about, styles = styles())]
struct Mcsm {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Init { name: String },
2026-08-30 16:38:53 +05:00
}
2026-08-30 16:38:53 +05:00
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mcsm = Mcsm::parse();
2026-08-30 16:38:53 +05:00
match mcsm.command {
Commands::Init { name: project_name } => {
tokio::fs::create_dir(&project_name).await?;
std::env::set_current_dir(&project_name)?;
2026-08-30 16:38:53 +05:00
let loaders = vec!["Vanilla", "Fabric"];
let loader_index = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select Loader")
.items(&loaders)
.default(0)
.interact()?;
2026-08-30 16:38:53 +05:00
match loader_index {
0 => {
const URL: &str =
"https://launchermeta.mojang.com/mc/game/version_manifest.json";
let res = with_spinner("", "", || async {
let res: VanillaVersions = reqwest::get(URL).await?.json().await?;
Ok(res)
}).await?;
let release_versions = res
.versions
2026-08-30 16:38:53 +05:00
.iter()
.filter(|version| version.version_type == VanillaVersionType::Release)
.collect::<Vec<_>>();
let release_version_names = release_versions
.iter()
2026-08-30 16:38:53 +05:00
.map(|version| version.id.clone())
.collect::<Vec<_>>();
let version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select Version")
.items(&release_version_names)
2026-08-30 16:38:53 +05:00
.default(0)
.interact()?;
let version_detail: VanillaVersionDetail =
reqwest::get(release_versions[version_index].url.clone())
.await?
.json()
.await?;
println!(
"downloading server.jar for {} {}",
loaders[loader_index], release_version_names[version_index]
);
let pb = ProgressBar::new(version_detail.downloads.server.size);
pb.set_style(ProgressStyle::default_bar().template(
"{bar:40.green/white} {bytes}/{total_bytes} ({bytes_per_sec})",
)?);
let server_bytes = reqwest::get(version_detail.downloads.server.url).await?;
let mut file = tokio::fs::File::create("server.jar").await?;
let mut stream = server_bytes.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
file.write_all(&chunk).await?;
pb.inc(chunk.len() as u64);
}
pb.finish_with_message("download finished");
2026-08-30 16:38:53 +05:00
with_spinner("verifying sha1 hash", "sha1 hash verified", || async {
let data = tokio::fs::read("server.jar").await?;
let mut hasher = Sha1::new();
hasher.update(&data);
let hash = hasher.finalize();
let hex: String = hash.iter().map(|b| format!("{:02x}", b)).collect();
2026-08-30 16:38:53 +05:00
if hex != version_detail.downloads.server.sha1 {
tokio::fs::remove_file("server.jar").await?;
return Err("sha1 verifaction failed for downloaded file".into());
}
Ok(())
}).await?;
2026-08-30 16:38:53 +05:00
}
1 => {}
_ => return Err("Invalid selection".into()),
2026-08-30 16:38:53 +05:00
}
}
}
Ok(())
}