Compare commits
No commits in common. "10920719681f585d6a67fc2235c57e4a9c02fd3c" and "8bf56d2c03d717f61d26295a4e479126e5d41b1f" have entirely different histories.
1092071968
...
8bf56d2c03
2 changed files with 60 additions and 166 deletions
|
|
@ -16,41 +16,21 @@ use crate::{
|
||||||
|
|
||||||
pub struct Create {
|
pub struct Create {
|
||||||
// project_name: String,
|
// project_name: String,
|
||||||
config_file: File,
|
|
||||||
|
|
||||||
version: Option<String>,
|
|
||||||
loader: Option<String>,
|
loader: Option<String>,
|
||||||
loader_version: Option<String>,
|
config_file: File,
|
||||||
|
|
||||||
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(
|
pub async fn new(project_name: String) -> Result<Self, Box<dyn Error>> {
|
||||||
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,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,12 +44,11 @@ impl Create {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn download_server(&mut self) -> Result<Config, Box<dyn Error>> {
|
pub async fn download_server(&self) -> Result<Config, Box<dyn Error>> {
|
||||||
Ok(match self.loader.as_deref() {
|
Ok(match self.loader.as_deref() {
|
||||||
Some("Vanilla") => VanillaDownloader::download(self).await?,
|
Some("Vanilla") => VanillaDownloader::download().await?,
|
||||||
Some("Fabric") => FabricDownloader::download(self).await?,
|
Some("Fabric") => FabricDownloader::download().await?,
|
||||||
Some(l) => return Err(format!("Invalid loader specified: {}", l).into()),
|
_ => return Err("Invalid selection".into()),
|
||||||
None => return Err("No loader specified".into()),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,7 +62,7 @@ impl Create {
|
||||||
|
|
||||||
trait LoaderDownloader {
|
trait LoaderDownloader {
|
||||||
fn name() -> String;
|
fn name() -> String;
|
||||||
async fn download(create: &mut Create) -> Result<Config, Box<dyn Error>>;
|
async fn download() -> Result<Config, Box<dyn Error>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VanillaDownloader {}
|
struct VanillaDownloader {}
|
||||||
|
|
@ -92,7 +71,7 @@ impl LoaderDownloader for VanillaDownloader {
|
||||||
"Vanilla".to_string()
|
"Vanilla".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn download(create: &mut Create) -> Result<Config, Box<dyn Error>> {
|
async fn download() -> 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?;
|
||||||
|
|
@ -100,47 +79,25 @@ impl LoaderDownloader for VanillaDownloader {
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some(version) = create.version.clone() {
|
let release_versions = res
|
||||||
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()
|
||||||
.find(|v| &v.id == create.version.as_ref().unwrap())
|
.filter(|version| version.version_type == VanillaVersionType::Release)
|
||||||
.unwrap();
|
.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()?;
|
||||||
|
|
||||||
let version_detail: VanillaVersionDetail = with_spinner("", "", || async {
|
let version_detail: VanillaVersionDetail = with_spinner("", "", || async {
|
||||||
let version_detail: VanillaVersionDetail =
|
let version_detail: VanillaVersionDetail =
|
||||||
reqwest::get(version.url.clone())
|
reqwest::get(release_versions[version_index].url.clone())
|
||||||
.await?
|
.await?
|
||||||
.json()
|
.json()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
@ -151,7 +108,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(),
|
||||||
create.version.as_ref().unwrap(),
|
release_version_names[version_index]
|
||||||
);
|
);
|
||||||
with_progressbar(
|
with_progressbar(
|
||||||
&start_msg,
|
&start_msg,
|
||||||
|
|
@ -189,7 +146,7 @@ impl LoaderDownloader for VanillaDownloader {
|
||||||
|
|
||||||
let config = Config {
|
let config = Config {
|
||||||
server: Server {
|
server: Server {
|
||||||
version: create.version.clone().unwrap(),
|
version: release_version_names[version_index].to_string(),
|
||||||
loader: Self::name(),
|
loader: Self::name(),
|
||||||
loader_version: None,
|
loader_version: None,
|
||||||
},
|
},
|
||||||
|
|
@ -206,7 +163,7 @@ impl LoaderDownloader for FabricDownloader {
|
||||||
"Fabric".to_string()
|
"Fabric".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn download(create: &mut Create) -> Result<Config, Box<dyn Error>> {
|
async fn download() -> 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?;
|
||||||
|
|
@ -214,54 +171,34 @@ impl LoaderDownloader for FabricDownloader {
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some(version) = create.version.clone() {
|
let game_version_names = res
|
||||||
let exists = res
|
.game
|
||||||
.game
|
.iter()
|
||||||
.iter()
|
.filter(|game| game.stable)
|
||||||
.any(|game| game.version == version);
|
.map(|game| game.version.clone())
|
||||||
if !exists { return Err("Invalid minecraft version specified".into()) }
|
.collect::<Vec<_>>();
|
||||||
}
|
|
||||||
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()?;
|
||||||
|
|
||||||
create.version = Some(game_version_names[game_version_index].to_string());
|
let loader_version_names = res
|
||||||
}
|
.loader
|
||||||
|
.iter()
|
||||||
|
.filter(|loader| loader.stable)
|
||||||
|
.map(|loader| loader.version.clone())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
if let Some(loader_version) = create.loader_version.clone() {
|
let loader_version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
|
||||||
let exists = res
|
.with_prompt("Select Loader Version")
|
||||||
.loader
|
.items(&loader_version_names)
|
||||||
.iter()
|
.default(0)
|
||||||
.any(|loader| loader.version == loader_version);
|
.interact()?;
|
||||||
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()
|
||||||
|
|
@ -277,13 +214,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(),
|
||||||
create.loader_version.as_ref().unwrap(),
|
loader_version_names[loader_version_index],
|
||||||
create.version.as_ref().unwrap(),
|
game_version_names[game_version_index],
|
||||||
);
|
);
|
||||||
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, create.version.as_ref().unwrap(), create.loader_version.as_ref().unwrap(), installer_version
|
URL, game_version, loader_version, 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?;
|
||||||
|
|
@ -299,9 +236,9 @@ impl LoaderDownloader for FabricDownloader {
|
||||||
|
|
||||||
let config = Config {
|
let config = Config {
|
||||||
server: Server {
|
server: Server {
|
||||||
version: create.version.clone().unwrap(),
|
version: game_version,
|
||||||
loader: Self::name(),
|
loader: Self::name(),
|
||||||
loader_version: create.loader_version.clone(),
|
loader_version: Some(loader_version),
|
||||||
},
|
},
|
||||||
mods: Mods {},
|
mods: Mods {},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
53
src/main.rs
53
src/main.rs
|
|
@ -25,34 +25,8 @@ struct Mcsm {
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Commands {
|
enum Commands {
|
||||||
/// Create a new minecraft server
|
/// create a new minecraft server
|
||||||
Create {
|
Create { project_name: String },
|
||||||
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]
|
||||||
|
|
@ -60,26 +34,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let mcsm = Mcsm::parse();
|
let mcsm = Mcsm::parse();
|
||||||
|
|
||||||
match mcsm.command {
|
match mcsm.command {
|
||||||
Commands::Create {
|
Commands::Create { project_name } => {
|
||||||
project_name,
|
let mut create = Create::new(project_name.clone()).await?;
|
||||||
version,
|
create.init_loader().await?;
|
||||||
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