Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
fix lock paths
Browse files Browse the repository at this point in the history
  • Loading branch information
TOwInOK committed Apr 24, 2024
1 parent 35eb728 commit 9e54402
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 54 deletions.
14 changes: 3 additions & 11 deletions config.lock
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 0 additions & 4 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ force_update = false
[plugins]
[plugins.simple-voice-chat]
[plugins.chunky]
version = "ail5iPUK"
channel = "Stable"
freeze = false
update = false

[additions]
# configPluguinsFrom = "[email protected]:TOwInOK/test.git"
Expand Down
3 changes: 2 additions & 1 deletion src/config/additions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
27 changes: 13 additions & 14 deletions src/config/models/cores/purpur.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::info;
use log::{info, trace};
use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -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),
Expand All @@ -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<String> {
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 {
Expand Down
37 changes: 15 additions & 22 deletions src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<Config>>,
lock: Arc<Mutex<Lock>>,
Expand All @@ -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<Self> {
// 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");
Expand Down Expand Up @@ -113,10 +107,9 @@ async fn run(config: Arc<Mutex<Config>>, lock: Arc<Mutex<Lock>>, 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
Expand All @@ -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));
}
Expand Down
8 changes: 6 additions & 2 deletions src/lock/locker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 9e54402

Please sign in to comment.