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

Commit

Permalink
fix name of crate
Browse files Browse the repository at this point in the history
add possibility to download core
  • Loading branch information
TOwInOK committed Apr 29, 2024
1 parent 94b7533 commit 0cf4a85
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 63 deletions.
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "traits"
name = "minecraft_addon_controller"
version = "0.1.0"
edition = "2021"

Expand Down
22 changes: 22 additions & 0 deletions src/lock/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ pub struct CoreMeta {
build: Option<String>,
}

impl CoreMeta {
pub fn new(provider: Provider, version: String, build: Option<String>) -> Self {
Self {
provider,
version,
build,
}
}

pub fn provider(&self) -> &Provider {
&self.provider
}

pub fn version(&self) -> &str {
&self.version
}

pub fn build(&self) -> Option<&String> {
self.build.as_ref()
}
}

impl From<Core> for CoreMeta {
fn from(value: Core) -> Self {
Self {
Expand Down
13 changes: 3 additions & 10 deletions src/lock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
mod core;
pub mod core;
pub mod ext;

use serde::{Deserialize, Serialize};

use self::core::CoreMeta;
use self::ext::ExtensionMeta;
use crate::{
settings::core::Core,
tr::{load::Load, save::Save},
};
use crate::tr::{load::Load, save::Save};
use std::collections::HashMap;

#[derive(Default, Serialize, Deserialize)]
pub struct Lock {
core: CoreMeta,
plugins: HashMap<String, ExtensionMeta>,
// mods: HashMap<String, ExtensionMeta>,
mods: HashMap<String, ExtensionMeta>,
}
impl Lock {
fn update_core(&mut self, core: Core) {
self.core = core.into();
}

pub fn core(&self) -> &CoreMeta {
&self.core
}
Expand Down
32 changes: 18 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@ pub mod tr;
use crate::errors::error::Result;
use lock::Lock;
use settings::Settings;
use tr::{load::Load, save::Save};
use tr::load::Load;

#[tokio::main]
async fn main() -> Result<()> {
pretty_env_logger::init();
// let mut query = Query::default();

// for (name, plugin) in settings.plugins().items().iter() {
// let game_version = settings.core().version();
// let a = plugin.get_link(name, game_version).await?;
// query.query_mut().insert(name.to_string(), a.into());
// }
// let settings = Arc::new(Mutex::new(Settings::default()));
// let lock = Arc::new(Mutex::new(Lock::default()));
// let plugins = Plugins::new(path, items);
// settings.get_mut().plugins_mut()
// lock.lock().await.save().await?;
// settings.lock().await.save().await?;
// let game_version = settings.core().version();
// let a = plugin.get_link(name, game_version).await?;
// query.query_mut().insert(name.to_string(), a.into());
// }
// let settings = Arc::new(Mutex::new(Settings::default()));
// let lock = Arc::new(Mutex::new(Lock::default()));
// let plugins = Plugins::new(path, items);
// settings.get_mut().plugins_mut()
// lock.lock().await.save().await?;
// settings.lock().await.save().await?;

let mut lock = Lock::load().await?;
let settings = Settings::load().await?;
// let a = Additions::new(Some("GitHub.link".to_string()), Some("GitHub.key".to_string()));
// settings.set_additions(Some(a));

if let Some(plugins) = settings.plugins() {
plugins.download_all(settings.core().version(), &mut lock).await?;
plugins
.download_all(settings.core().version(), &mut lock)
.await?;
}
settings.core().download(&mut lock).await?;
Ok(())
}
11 changes: 2 additions & 9 deletions src/models/cores/vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ struct Version {
}

///Minecraft types of version
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
enum TypeOfVersion {
#[serde(rename = "release")]
#[default]
Release,
#[serde(rename = "snapshot")]
Snapshot,
Expand All @@ -45,14 +46,6 @@ enum TypeOfVersion {
#[serde(rename = "old_alpha")]
OldAlpha,
}

impl Default for TypeOfVersion {
fn default() -> Self {
// warn!("Use default fn of TypeOfVersion");
TypeOfVersion::Release
}
}

//Area of download from list of details about version

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Query {
}
impl Query {
/// Check Data and add it if is't same
async fn push(&mut self, name: String, data: QueryData) {
async fn _push(&mut self, name: String, data: QueryData) {
match self.query.get_mut(&name) {
Some(e) => {
if data != *e {
Expand Down
64 changes: 64 additions & 0 deletions src/settings/core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
use serde::{Deserialize, Serialize};

use crate::errors::error::Result;
use crate::lock::core::CoreMeta;
use crate::lock::Lock;
use crate::models::cores::folia::Folia;
use crate::models::cores::paper::Paper;
use crate::models::cores::purpur::Purpur;
use crate::models::cores::vanilla::Vanilla;
use crate::models::cores::velocity::Velocity;
use crate::models::cores::waterfall::Waterfall;
use crate::tr::hash::ChooseHash;
use crate::tr::model::core::ModelCore;
use crate::tr::{download::Download, save::Save};

#[derive(Deserialize, Serialize, Debug, Default, PartialEq, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Core {
Expand Down Expand Up @@ -65,6 +78,36 @@ impl Core {
pub fn set_force_update(&mut self, force_update: bool) {
self.force_update = force_update;
}

/// Скачиваем `Core` и сохраняем его по стандартному пути.
pub async fn download(&self, lock: &mut Lock) -> Result<()> {
let (link, hash, build) = self.get_link().await?;
if let Some(e) = lock.core().build() {
if *e == build
&& self.build() == lock.core().build()
&& self.provider() == lock.core().provider()
{
return Ok(());
}
}
let file = self.get_file(link, hash).await?;
self.save_bytes(file, self.provider().as_str()).await?;
*lock.core_mut() = CoreMeta::new(self.provider.clone(), self.version.clone(), Some(build));
lock.save().await
}
async fn get_link(&self) -> Result<(String, ChooseHash, String)> {
match self.provider {
Provider::Vanilla => Vanilla::get_link(self).await,
Provider::Paper => Paper::get_link(self).await,
Provider::Folia => Folia::get_link(self).await,
Provider::Purpur => Purpur::get_link(self).await,
Provider::Fabric => todo!(),
Provider::Forge => todo!(),
Provider::NeoForge => todo!(),
Provider::Waterfall => Waterfall::get_link(self).await,
Provider::Velocity => Velocity::get_link(self).await,
}
}
}

#[derive(Deserialize, Serialize, Debug, Default, PartialEq, Clone)]
Expand All @@ -83,3 +126,24 @@ pub enum Provider {
Waterfall, // done
Velocity, // done
}

impl Provider {
pub fn as_str(&self) -> &'static str {
match self {
Provider::Vanilla => "vanilla",
Provider::Paper => "paper",
Provider::Folia => "folia",
Provider::Purpur => "purpur",
Provider::Fabric => "fabric",
Provider::Forge => "forge",
Provider::NeoForge => "neoforge",
Provider::Waterfall => "waterfall",
Provider::Velocity => "velocity",
}
}
}

impl Download for Core {}
impl Save for Core {
const PATH: &'static str = "./";
}
17 changes: 7 additions & 10 deletions src/settings/extensions/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::errors::error::Result;
use crate::lock::ext::ExtensionMeta;
use crate::models::extensions::modrinth::ModrinthData;
use crate::tr::hash::ChooseHash;
use crate::tr::model::extension::ModelExtensions;
Expand Down Expand Up @@ -46,18 +45,16 @@ impl Plugin {
self.force_update
}
/// Get link from models.
pub fn get_link<'a>(
pub async fn get_link<'a>(
&'a self,
name: &'a str,
game_version: &'a str,
) -> impl std::future::Future<Output = Result<(String, ChooseHash, String)>> + Send + 'a {
async move {
match self.source {
Sources::Spigot => todo!(),
Sources::Hangar => todo!(),
Sources::Modrinth => ModrinthData::get_link(self, name, game_version).await,
Sources::CurseForge => todo!(),
}
) -> Result<(String, ChooseHash, String)> {
match self.source {
Sources::Spigot => todo!(),
Sources::Hangar => todo!(),
Sources::Modrinth => ModrinthData::get_link(self, name, game_version).await,
Sources::CurseForge => todo!(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/settings/extensions/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Plugins {
lock.plugins_mut().insert(name.to_string(), {
ExtensionMeta::new(Some(build), format!("./plugins/{}.jar", name))
});
// Используем на каждой итерации, так как может возникнуть ошибка.
lock.save().await?;
}
Ok(())
Expand Down

0 comments on commit 0cf4a85

Please sign in to comment.