Compare commits

..

2 commits

2 changed files with 166 additions and 60 deletions

View file

@ -16,21 +16,41 @@ use crate::{
pub struct Create {
// project_name: String,
loader: Option<String>,
config_file: File,
version: Option<String>,
loader: Option<String>,
loader_version: Option<String>,
show_snapshots: bool,
show_betas: bool,
show_alphas: bool,
}
impl Create {
const LOADERS: [&str; 2] = ["Vanilla", "Fabric"];
pub async fn new(project_name: String) -> Result<Self, Box<dyn Error>> {
pub async fn new(
project_name: String,
version: Option<String>,
loader: Option<String>,
loader_version: Option<String>,
show_snapshots: bool,
show_betas: bool,
show_alphas: bool,
) -> Result<Self, Box<dyn Error>> {
tokio::fs::create_dir_all(&project_name).await?;
std::env::set_current_dir(&project_name)?;
let config_file = tokio::fs::File::create("mcsm.toml").await?;
Ok(Create {
/*project_name,*/
loader: None,
config_file,
version,
loader,
loader_version,
show_snapshots,
show_betas,
show_alphas,
})
}
@ -44,11 +64,12 @@ impl Create {
Ok(())
}
pub async fn download_server(&self) -> Result<Config, Box<dyn Error>> {
pub async fn download_server(&mut self) -> Result<Config, Box<dyn Error>> {
Ok(match self.loader.as_deref() {
Some("Vanilla") => VanillaDownloader::download().await?,
Some("Fabric") => FabricDownloader::download().await?,
_ => return Err("Invalid selection".into()),
Some("Vanilla") => VanillaDownloader::download(self).await?,
Some("Fabric") => FabricDownloader::download(self).await?,
Some(l) => return Err(format!("Invalid loader specified: {}", l).into()),
None => return Err("No loader specified".into()),
})
}
@ -62,7 +83,7 @@ impl Create {
trait LoaderDownloader {
fn name() -> String;
async fn download() -> Result<Config, Box<dyn Error>>;
async fn download(create: &mut Create) -> Result<Config, Box<dyn Error>>;
}
struct VanillaDownloader {}
@ -71,7 +92,7 @@ impl LoaderDownloader for VanillaDownloader {
"Vanilla".to_string()
}
async fn download() -> Result<Config, Box<dyn Error>> {
async fn download(create: &mut Create) -> Result<Config, Box<dyn Error>> {
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?;
@ -79,25 +100,47 @@ impl LoaderDownloader for VanillaDownloader {
})
.await?;
let release_versions = res
if let Some(version) = create.version.clone() {
let version_exists = res
.versions
.iter()
.any(|v| v.id == version);
if !version_exists { return Err("Invalid minecraft version specified".into()) }
}
else {
let versions = res
.versions
.iter()
.filter(|version| {
version.version_type == VanillaVersionType::Release ||
(create.show_snapshots && version.version_type == VanillaVersionType::Snapshot) ||
(create.show_betas && version.version_type == VanillaVersionType::OldBeta) ||
(create.show_alphas && version.version_type == VanillaVersionType::OldAlpha)
})
.collect::<Vec<_>>();
let version_names = versions
.iter()
.map(|version| version.id.as_str())
.collect::<Vec<_>>();
let version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select Version")
.items(&version_names)
.default(0)
.interact()?;
create.version = Some(version_names[version_index].to_string());
}
let version = 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_version_names)
.default(0)
.interact()?;
.find(|v| &v.id == create.version.as_ref().unwrap())
.unwrap();
let version_detail: VanillaVersionDetail = with_spinner("", "", || async {
let version_detail: VanillaVersionDetail =
reqwest::get(release_versions[version_index].url.clone())
reqwest::get(version.url.clone())
.await?
.json()
.await?;
@ -108,7 +151,7 @@ impl LoaderDownloader for VanillaDownloader {
let start_msg = format!(
"downloading server.jar for {} {}",
Self::name(),
release_version_names[version_index]
create.version.as_ref().unwrap(),
);
with_progressbar(
&start_msg,
@ -146,7 +189,7 @@ impl LoaderDownloader for VanillaDownloader {
let config = Config {
server: Server {
version: release_version_names[version_index].to_string(),
version: create.version.clone().unwrap(),
loader: Self::name(),
loader_version: None,
},
@ -163,7 +206,7 @@ impl LoaderDownloader for FabricDownloader {
"Fabric".to_string()
}
async fn download() -> Result<Config, Box<dyn Error>> {
async fn download(create: &mut Create) -> Result<Config, Box<dyn Error>> {
const URL: &str = "https://meta.fabricmc.net/v2/versions";
let res = with_spinner("", "", || async {
let res: FabricVersions = reqwest::get(URL).await?.json().await?;
@ -171,34 +214,54 @@ impl LoaderDownloader for FabricDownloader {
})
.await?;
let game_version_names = res
.game
.iter()
.filter(|game| game.stable)
.map(|game| game.version.clone())
.collect::<Vec<_>>();
if let Some(version) = create.version.clone() {
let exists = res
.game
.iter()
.any(|game| game.version == version);
if !exists { return Err("Invalid minecraft version specified".into()) }
}
else {
let game_version_names = res
.game
.iter()
.filter(|game| create.show_snapshots || game.stable)
.map(|game| game.version.as_str())
.collect::<Vec<_>>();
let game_version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select Game Version")
.items(&game_version_names)
.default(0)
.interact()?;
let game_version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select Game Version")
.items(&game_version_names)
.default(0)
.interact()?;
let loader_version_names = res
.loader
.iter()
.filter(|loader| loader.stable)
.map(|loader| loader.version.clone())
.collect::<Vec<_>>();
create.version = Some(game_version_names[game_version_index].to_string());
}
let loader_version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select Loader Version")
.items(&loader_version_names)
.default(0)
.interact()?;
if let Some(loader_version) = create.loader_version.clone() {
let exists = res
.loader
.iter()
.any(|loader| loader.version == loader_version);
if !exists { return Err(format!("Invalid {} loader version specified", Self::name()).into()) }
}
else {
let loader_version_names = res
.loader
.iter()
.filter(|loader| loader.stable)
.map(|loader| loader.version.clone())
.collect::<Vec<_>>();
let loader_version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select Loader Version")
.items(&loader_version_names)
.default(0)
.interact()?;
create.loader_version = Some(loader_version_names[loader_version_index].to_string());
}
let game_version = game_version_names[game_version_index].clone();
let loader_version = loader_version_names[loader_version_index].clone();
let installer_version = res
.installer
.iter()
@ -214,13 +277,13 @@ impl LoaderDownloader for FabricDownloader {
let start_msg = format!(
"downloading server.jar for {} {}, {}",
Self::name(),
loader_version_names[loader_version_index],
game_version_names[game_version_index],
create.loader_version.as_ref().unwrap(),
create.version.as_ref().unwrap(),
);
with_spinner(&start_msg, "download finished", || async {
let download_url = format!(
"{}/loader/{}/{}/{}/server/jar",
URL, game_version, loader_version, installer_version
URL, create.version.as_ref().unwrap(), create.loader_version.as_ref().unwrap(), installer_version
);
let res = reqwest::get(download_url).await?;
let mut file = tokio::fs::File::create("server.jar").await?;
@ -236,9 +299,9 @@ impl LoaderDownloader for FabricDownloader {
let config = Config {
server: Server {
version: game_version,
version: create.version.clone().unwrap(),
loader: Self::name(),
loader_version: Some(loader_version),
loader_version: create.loader_version.clone(),
},
mods: Mods {},
};

View file

@ -25,8 +25,34 @@ struct Mcsm {
#[derive(Subcommand)]
enum Commands {
/// create a new minecraft server
Create { project_name: String },
/// Create a new minecraft server
Create {
project_name: String,
/// Minecraft version
#[arg(long)]
version: Option<String>,
/// Mod Loader, can be either Vanilla or Fabric
#[arg(long)]
loader: Option<String>,
/// Mod Loader version, ignored if loader is Vanilla
#[arg(long)]
loader_version: Option<String>,
/// Show snapshot versions
#[arg(long)]
show_snapshots: bool,
/// Show old beta versions
#[arg(long)]
show_betas: bool,
///
/// Show old alpha versions
#[arg(long)]
show_alphas: bool,
},
}
#[tokio::main]
@ -34,9 +60,26 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mcsm = Mcsm::parse();
match mcsm.command {
Commands::Create { project_name } => {
let mut create = Create::new(project_name.clone()).await?;
create.init_loader().await?;
Commands::Create {
project_name,
version,
loader,
loader_version,
show_snapshots,
show_betas,
show_alphas,
} => {
let loader_is_unset = loader == None;
let mut create = Create::new(
project_name,
version,
loader,
loader_version,
show_snapshots,
show_betas,
show_alphas
).await?;
if loader_is_unset { create.init_loader().await? }
let config = create.download_server().await?;
create.save_config(config).await?;
}