From 9e5440245c02efea62e5b597ebd07d3e9b271e49 Mon Sep 17 00:00:00 2001 From: TOwInOK <60252419+TOwInOK@users.noreply.github.com> Date: Thu, 25 Apr 2024 06:02:07 +0700 Subject: [PATCH] fix lock paths --- config.lock | 14 +++--------- config.toml | 4 ---- src/config/additions.rs | 3 ++- src/config/models/cores/purpur.rs | 27 +++++++++++----------- src/controller/mod.rs | 37 +++++++++++++------------------ src/lock/locker.rs | 8 +++++-- 6 files changed, 39 insertions(+), 54 deletions(-) diff --git a/config.lock b/config.lock index 0d4318c..53b6501 100644 --- a/config.lock +++ b/config.lock @@ -1,13 +1,5 @@ [core] -name = "purpur" -version = "1.20.4" -build = "2166" -path = "core/purpur.jar" +name = "vanilla" +path = "" -[plugins.simple-voice-chat] -version = "r7e564VW" -path = "plugins/simple-voice-chat.jar" - -[plugins.chunky] -version = "iwsbfPGg" -path = "plugins/chunky.jar" +[plugins] diff --git a/config.toml b/config.toml index 06956b9..f7dcc63 100644 --- a/config.toml +++ b/config.toml @@ -8,10 +8,6 @@ force_update = false [plugins] [plugins.simple-voice-chat] [plugins.chunky] -version = "ail5iPUK" -channel = "Stable" -freeze = false -update = false [additions] # configPluguinsFrom = "git@github.com:TOwInOK/test.git" diff --git a/src/config/additions.rs b/src/config/additions.rs index 629d785..3d65d06 100644 --- a/src/config/additions.rs +++ b/src/config/additions.rs @@ -34,9 +34,10 @@ fn plugins() -> String { warn!("We use default plugins path!"); "plugins".to_string() } +//folder for config files fn configs() -> String { warn!("We use default config path!"); - "configs.toml".to_string() + "./configs".to_string() } fn lock() -> String { warn!("We use default lock path!"); diff --git a/src/config/models/cores/purpur.rs b/src/config/models/cores/purpur.rs index 130460f..d7b3802 100644 --- a/src/config/models/cores/purpur.rs +++ b/src/config/models/cores/purpur.rs @@ -1,4 +1,4 @@ -use log::info; +use log::{info, trace}; use serde::{Deserialize, Serialize}; use crate::{ @@ -34,44 +34,43 @@ pub struct FileHash { // Download // https://api.purpurmc.org/v2/purpur/{Version}/{Build}/download +// +const MAIN_LINK: &str = "https://api.purpurmc.org/v2/purpur"; impl ModelCore for Purpur { //find build and push link async fn get_link(core: &Core) -> Result<(String, ChooseHash, String)> { let build = core.build.as_deref(); let version = core.version.as_deref(); + trace!("Find version started!"); let version = Self::find_version(version).await?; //Version string - let verlink = format!("https://api.purpurmc.org/v2/purpur/{}", version); + let verlink = format!("{}/{}", MAIN_LINK, version); info!("Get BuildList"); let build_list: BuildList = reqwest::get(verlink).await?.json().await?; let build_list_latest: &str = build_list.builds.latest.as_ref(); let build_list = build_list.builds.all; match build { - Some(e) => { - if build_list.contains(&e.to_owned()) { + Some(build) => { + if build_list.contains(&build.to_owned()) { info!("Find build, download"); - let build_link = - format!("https://api.purpurmc.org/v2/purpur/{}/{}", version, e); + let build_link = format!("{}/{}/{}", MAIN_LINK, version, build); info!("Get Url"); let file_hash: FileHash = reqwest::get(&build_link).await?.json().await?; Ok(( format!("{}/download", build_link), ChooseHash::MD5(file_hash.md5), - e.to_owned(), + build.to_owned(), )) } else { - not_found_build_error!(e) + not_found_build_error!(build) } } None => { info!("Download latest build"); info!("Get Url"); - let build_link = format!( - "https://api.purpurmc.org/v2/purpur/{}/{}", - version, build_list_latest - ); + let build_link = format!("{}/{}/{}", MAIN_LINK, version, build_list_latest); let file_hash: FileHash = reqwest::get(&build_link).await?.json().await?; Ok(( format!("{}/download", build_link), @@ -84,11 +83,11 @@ impl ModelCore for Purpur { //Find version in version list, if exist give out version or give error async fn find_version(version: Option<&str>) -> Result { - let link = "https://api.purpurmc.org/v2/purpur"; - let verlist: VersionList = reqwest::get(link).await?.json().await?; + let verlist: VersionList = reqwest::get(MAIN_LINK).await?.json().await?; let verlist: &[String] = verlist.versions.as_ref(); match version { Some(ver) => { + trace!("have ver"); if verlist.contains(&ver.to_owned()) { Ok(ver.to_owned()) } else { diff --git a/src/controller/mod.rs b/src/controller/mod.rs index b49c9f1..0a470fb 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -6,6 +6,8 @@ use tokio_util::sync::CancellationToken; use crate::{config::Config, downloader::Downloader, errors::error::Result, lock::locker::Lock}; +const CONFIG_PATH: &str = "./config.toml"; + pub struct Controller { config: Arc>, lock: Arc>, @@ -16,28 +18,20 @@ impl Controller { let controller = Self::new().await.unwrap(); let token = CancellationToken::new(); let token_clone = token.clone(); - { - // clone data - let config = Arc::clone(&controller.config); - let lock = Arc::clone(&controller.lock); - tokio::spawn(async move { - run(config, lock, &token).await; - }); - } let config = Arc::clone(&controller.config); - let config = config.lock().await; - watch_config_changes(&config, &token_clone).await + let lock = Arc::clone(&controller.lock); + tokio::spawn(async move { + run(config, lock, &token).await; + }); + + watch_config_changes(&token_clone).await } async fn new() -> Result { // Load Config file - let path = "./config.toml"; - let config = match Config::load_config(path).await { - Ok(mut config) => { - config.additions.path_to_configs = path.to_owned(); - Arc::new(Mutex::new(config)) - } + let config = match Config::load_config(CONFIG_PATH).await { + Ok(config) => Arc::new(Mutex::new(config)), Err(e) => { log::error!("Failed to load config: {}", e); log::warn!("Loading default config"); @@ -113,10 +107,9 @@ async fn run(config: Arc>, lock: Arc>, token: &Cancell /// Load downloader module. /// Always check config file. /// Use `token` for canceling minecraft task -pub async fn watch_config_changes(config: &Config, _token: &CancellationToken) { +pub async fn watch_config_changes(_token: &CancellationToken) { // Load new Config file - let path = config.additions.path_to_configs.to_owned(); - let config = match Config::load_config(&path).await { + let config = match Config::load_config(CONFIG_PATH).await { Ok(config) => { log::debug!("{:#?}", config); config @@ -131,12 +124,12 @@ pub async fn watch_config_changes(config: &Config, _token: &CancellationToken) { // Load new lock let mut lock = Lock::default(); - if let Err(e) = lock.load(&config.additions.path_to_configs).await { + if let Err(e) = lock.load(&config.additions.path_to_lock).await { error!("{:?}", e); - lock.create(&config.additions.path_to_configs) + lock.create(&config.additions.path_to_lock) .await .unwrap_or_else(|e| error!("{:?}", e)); - lock.load(&config.additions.path_to_configs) + lock.load(&config.additions.path_to_lock) .await .unwrap_or_else(|e| error!("{:?}", e)); } diff --git a/src/lock/locker.rs b/src/lock/locker.rs index e07fb1e..202bb3d 100644 --- a/src/lock/locker.rs +++ b/src/lock/locker.rs @@ -4,7 +4,7 @@ use std::{ path::{Path, PathBuf}, }; -use log::{debug, info, trace}; +use log::{debug, error, info, trace, warn}; use serde::{Deserialize, Serialize}; use crate::{ @@ -56,7 +56,11 @@ impl Lock { info!("Deserializing lock file contents..."); // Deserialize the file contents into Lock struct - let lock: Lock = toml::from_str(&toml)?; + let lock: Lock = toml::from_str(&toml).unwrap_or_else(|e| { + error!("Parse error: {}", e); + warn!("Load default Lock!"); + Lock::default() + }); info!("Lock file deserialized successfully."); *self = lock; info!("Lock reload successfully");