feat: vanilla server downloading + lots of ux improvements

This commit is contained in:
RafayAhmad7548 2026-08-30 18:29:17 +05:00
parent 505bfbbb5d
commit 0a12b87a2a
No known key found for this signature in database
6 changed files with 241 additions and 22 deletions

View file

@ -1,10 +1,17 @@
mod models;
mod utils;
use std::{error::Error, fs};
use clap::{Parser, Subcommand, builder::styling::{self}};
use crate::{models::vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions}, utils::with_spinner};
use clap::{
Parser, Subcommand,
builder::styling::{self},
};
use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme};
use crate::models::vanilla::{VanillaVersionType, VanillaVersions};
use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use sha1::{Digest, Sha1};
use std::error::Error;
use tokio::io::AsyncWriteExt;
fn styles() -> styling::Styles {
styling::Styles::styled()
@ -22,17 +29,17 @@ struct Mcsm {
#[derive(Subcommand)]
enum Commands {
Init {
name: String
}
Init { name: String },
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mcsm = Mcsm::parse();
match mcsm.command {
Commands::Init { name } => {
fs::create_dir(name)?;
Commands::Init { name: project_name } => {
tokio::fs::create_dir(&project_name).await?;
std::env::set_current_dir(&project_name)?;
let loaders = vec!["Vanilla", "Fabric"];
let loader_index = Select::with_theme(&ColorfulTheme::default())
@ -40,31 +47,74 @@ async fn main() -> Result<(), Box<dyn Error>> {
.items(&loaders)
.default(0)
.interact()?;
match loader_index {
0 => {
const URL: &str = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
let res: VanillaVersions = reqwest::get(URL).await?.json().await?;
let release_versions = res.versions
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
.iter()
.filter(|version| version.version_type == VanillaVersionType::Release)
.collect::<Vec<_>>();
let release_version_names = release_versions
.iter()
.map(|version| version.id.clone())
.collect::<Vec<_>>();
let version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select Version")
.items(&release_versions)
.items(&release_version_names)
.default(0)
.interact()?;
println!("you selected {} with version {}", loaders[loader_index], release_versions[version_index]);
let version_detail: VanillaVersionDetail =
reqwest::get(release_versions[version_index].url.clone())
.await?
.json()
.await?;
},
1 => {
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");
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();
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?;
}
_ => return Err("Invalid selection".into())
1 => {}
_ => return Err("Invalid selection".into()),
}
}
}