Compare commits
2 commits
8bf56d2c03
...
1092071968
| Author | SHA1 | Date | |
|---|---|---|---|
| 1092071968 | |||
| 1bd3a23e80 |
2 changed files with 166 additions and 60 deletions
|
|
@ -16,21 +16,41 @@ use crate::{
|
||||||
|
|
||||||
pub struct Create {
|
pub struct Create {
|
||||||
// project_name: String,
|
// project_name: String,
|
||||||
loader: Option<String>,
|
|
||||||
config_file: File,
|
config_file: File,
|
||||||
|
|
||||||
|
version: Option<String>,
|
||||||
|
loader: Option<String>,
|
||||||
|
loader_version: Option<String>,
|
||||||
|
|
||||||
|
show_snapshots: bool,
|
||||||
|
show_betas: bool,
|
||||||
|
show_alphas: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Create {
|
impl Create {
|
||||||
const LOADERS: [&str; 2] = ["Vanilla", "Fabric"];
|
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?;
|
tokio::fs::create_dir_all(&project_name).await?;
|
||||||
std::env::set_current_dir(&project_name)?;
|
std::env::set_current_dir(&project_name)?;
|
||||||
let config_file = tokio::fs::File::create("mcsm.toml").await?;
|
let config_file = tokio::fs::File::create("mcsm.toml").await?;
|
||||||
Ok(Create {
|
Ok(Create {
|
||||||
/*project_name,*/
|
/*project_name,*/
|
||||||
loader: None,
|
|
||||||
config_file,
|
config_file,
|
||||||
|
version,
|
||||||
|
loader,
|
||||||
|
loader_version,
|
||||||
|
show_snapshots,
|
||||||
|
show_betas,
|
||||||
|
show_alphas,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,11 +64,12 @@ impl Create {
|
||||||
Ok(())
|
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() {
|
Ok(match self.loader.as_deref() {
|
||||||
Some("Vanilla") => VanillaDownloader::download().await?,
|
Some("Vanilla") => VanillaDownloader::download(self).await?,
|
||||||
Some("Fabric") => FabricDownloader::download().await?,
|
Some("Fabric") => FabricDownloader::download(self).await?,
|
||||||
_ => return Err("Invalid selection".into()),
|
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 {
|
trait LoaderDownloader {
|
||||||
fn name() -> String;
|
fn name() -> String;
|
||||||
async fn download() -> Result<Config, Box<dyn Error>>;
|
async fn download(create: &mut Create) -> Result<Config, Box<dyn Error>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VanillaDownloader {}
|
struct VanillaDownloader {}
|
||||||
|
|
@ -71,7 +92,7 @@ impl LoaderDownloader for VanillaDownloader {
|
||||||
"Vanilla".to_string()
|
"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";
|
const URL: &str = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
|
||||||
let res = with_spinner("", "", || async {
|
let res = with_spinner("", "", || async {
|
||||||
let res: VanillaVersions = reqwest::get(URL).await?.json().await?;
|
let res: VanillaVersions = reqwest::get(URL).await?.json().await?;
|
||||||
|
|
@ -79,25 +100,47 @@ impl LoaderDownloader for VanillaDownloader {
|
||||||
})
|
})
|
||||||
.await?;
|
.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
|
.versions
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|version| version.version_type == VanillaVersionType::Release)
|
.find(|v| &v.id == create.version.as_ref().unwrap())
|
||||||
.collect::<Vec<_>>();
|
.unwrap();
|
||||||
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()?;
|
|
||||||
|
|
||||||
let version_detail: VanillaVersionDetail = with_spinner("", "", || async {
|
let version_detail: VanillaVersionDetail = with_spinner("", "", || async {
|
||||||
let version_detail: VanillaVersionDetail =
|
let version_detail: VanillaVersionDetail =
|
||||||
reqwest::get(release_versions[version_index].url.clone())
|
reqwest::get(version.url.clone())
|
||||||
.await?
|
.await?
|
||||||
.json()
|
.json()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
@ -108,7 +151,7 @@ impl LoaderDownloader for VanillaDownloader {
|
||||||
let start_msg = format!(
|
let start_msg = format!(
|
||||||
"downloading server.jar for {} {}",
|
"downloading server.jar for {} {}",
|
||||||
Self::name(),
|
Self::name(),
|
||||||
release_version_names[version_index]
|
create.version.as_ref().unwrap(),
|
||||||
);
|
);
|
||||||
with_progressbar(
|
with_progressbar(
|
||||||
&start_msg,
|
&start_msg,
|
||||||
|
|
@ -146,7 +189,7 @@ impl LoaderDownloader for VanillaDownloader {
|
||||||
|
|
||||||
let config = Config {
|
let config = Config {
|
||||||
server: Server {
|
server: Server {
|
||||||
version: release_version_names[version_index].to_string(),
|
version: create.version.clone().unwrap(),
|
||||||
loader: Self::name(),
|
loader: Self::name(),
|
||||||
loader_version: None,
|
loader_version: None,
|
||||||
},
|
},
|
||||||
|
|
@ -163,7 +206,7 @@ impl LoaderDownloader for FabricDownloader {
|
||||||
"Fabric".to_string()
|
"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";
|
const URL: &str = "https://meta.fabricmc.net/v2/versions";
|
||||||
let res = with_spinner("", "", || async {
|
let res = with_spinner("", "", || async {
|
||||||
let res: FabricVersions = reqwest::get(URL).await?.json().await?;
|
let res: FabricVersions = reqwest::get(URL).await?.json().await?;
|
||||||
|
|
@ -171,34 +214,54 @@ impl LoaderDownloader for FabricDownloader {
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let game_version_names = res
|
if let Some(version) = create.version.clone() {
|
||||||
.game
|
let exists = res
|
||||||
.iter()
|
.game
|
||||||
.filter(|game| game.stable)
|
.iter()
|
||||||
.map(|game| game.version.clone())
|
.any(|game| game.version == version);
|
||||||
.collect::<Vec<_>>();
|
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())
|
let game_version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
|
||||||
.with_prompt("Select Game Version")
|
.with_prompt("Select Game Version")
|
||||||
.items(&game_version_names)
|
.items(&game_version_names)
|
||||||
.default(0)
|
.default(0)
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
let loader_version_names = res
|
create.version = Some(game_version_names[game_version_index].to_string());
|
||||||
.loader
|
}
|
||||||
.iter()
|
|
||||||
.filter(|loader| loader.stable)
|
|
||||||
.map(|loader| loader.version.clone())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let loader_version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
|
if let Some(loader_version) = create.loader_version.clone() {
|
||||||
.with_prompt("Select Loader Version")
|
let exists = res
|
||||||
.items(&loader_version_names)
|
.loader
|
||||||
.default(0)
|
.iter()
|
||||||
.interact()?;
|
.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
|
let installer_version = res
|
||||||
.installer
|
.installer
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -214,13 +277,13 @@ impl LoaderDownloader for FabricDownloader {
|
||||||
let start_msg = format!(
|
let start_msg = format!(
|
||||||
"downloading server.jar for {} {}, {}",
|
"downloading server.jar for {} {}, {}",
|
||||||
Self::name(),
|
Self::name(),
|
||||||
loader_version_names[loader_version_index],
|
create.loader_version.as_ref().unwrap(),
|
||||||
game_version_names[game_version_index],
|
create.version.as_ref().unwrap(),
|
||||||
);
|
);
|
||||||
with_spinner(&start_msg, "download finished", || async {
|
with_spinner(&start_msg, "download finished", || async {
|
||||||
let download_url = format!(
|
let download_url = format!(
|
||||||
"{}/loader/{}/{}/{}/server/jar",
|
"{}/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 res = reqwest::get(download_url).await?;
|
||||||
let mut file = tokio::fs::File::create("server.jar").await?;
|
let mut file = tokio::fs::File::create("server.jar").await?;
|
||||||
|
|
@ -236,9 +299,9 @@ impl LoaderDownloader for FabricDownloader {
|
||||||
|
|
||||||
let config = Config {
|
let config = Config {
|
||||||
server: Server {
|
server: Server {
|
||||||
version: game_version,
|
version: create.version.clone().unwrap(),
|
||||||
loader: Self::name(),
|
loader: Self::name(),
|
||||||
loader_version: Some(loader_version),
|
loader_version: create.loader_version.clone(),
|
||||||
},
|
},
|
||||||
mods: Mods {},
|
mods: Mods {},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
53
src/main.rs
53
src/main.rs
|
|
@ -25,8 +25,34 @@ struct Mcsm {
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Commands {
|
enum Commands {
|
||||||
/// create a new minecraft server
|
/// Create a new minecraft server
|
||||||
Create { project_name: String },
|
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]
|
#[tokio::main]
|
||||||
|
|
@ -34,9 +60,26 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let mcsm = Mcsm::parse();
|
let mcsm = Mcsm::parse();
|
||||||
|
|
||||||
match mcsm.command {
|
match mcsm.command {
|
||||||
Commands::Create { project_name } => {
|
Commands::Create {
|
||||||
let mut create = Create::new(project_name.clone()).await?;
|
project_name,
|
||||||
create.init_loader().await?;
|
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?;
|
let config = create.download_server().await?;
|
||||||
create.save_config(config).await?;
|
create.save_config(config).await?;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue