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

Commit

Permalink
The kernel loading functionality has been added. #8 #2 #3
Browse files Browse the repository at this point in the history
The ability to specify download paths has been added.
Now, instead of using Vec<u8>, BufWriter is used.
A "architecture" of how this might look has emerged:
    Unification of downloading,
    Unification of what the model should do (an example in Vanilla).
The behavior of "hash" has been rewritten; now we process the file immediately and compare it.

Появилась загрузка ядра.
Добавлена возможность указание путей для скачивания.
Теперь используется не vec[u8], а bufwriter.
Появилась "архитектура" того как это может выглядеть:
    Унификация скачивания,
    Унификация того что должна делать модель (пример в Vanilla).
Переписано поведения "hash", теперь мы сразу обрабатываем файл и сравниваем его.
  • Loading branch information
TOwInOK committed Mar 21, 2024
1 parent bc3eb14 commit 03075cd
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 146 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"rust-analyzer.showUnlinkedFileNotification": false,
"cSpell.words": [
"парсинга",
"Bukkit",
"Datapack",
"Datapacks",
"modrinth",
Expand Down
4 changes: 4 additions & 0 deletions Sample of config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@
# Ключ если в привате
# Key if in private
key = "SUPEREKLy"
# path_to_core = ""
# path_to_mods = ""
# path_to_plugins = ""
# path_to_configs = ""
4 changes: 2 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[core]
# Выбери один из вариантов: Vanila, Spigot, Bucket, Paper, Purpur, Forge, NeoForge, Fabric
provider = "Paper"
# Выбери один из вариантов: Vanilla, Spigot, Bucket, Paper, Purpur, Forge, NeoForge, Fabric
provider = "Vanilla"
version = "1.14.4"
# Замораживать ли объекты при наличии в системе
freeze = false
Expand Down
28 changes: 26 additions & 2 deletions src/config/additions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,31 @@ use serde::{Deserialize, Serialize};
pub struct Additions {
// git link
#[serde(rename = "configPluguinsFrom")]
config_plugins_from: String,
#[serde(default)]
config_plugins_from: Option<String>,
// git key
key: String,
#[serde(default)]
key: Option<String>,
// Paths
#[serde(default = "core")]
pub path_to_core: String,
#[serde(default = "mods")]
pub path_to_mods: String,
#[serde(default = "plugins")]
pub path_to_plugins: String,
#[serde(default = "configs")]
pub path_to_configs: String,
}

fn core() -> String {
"./core".to_string()
}
fn mods() -> String {
"./mods".to_string()
}
fn plugins() -> String {
"./plugins".to_string()
}
fn configs() -> String {
"./configs".to_string()
}
2 changes: 1 addition & 1 deletion src/config/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Core {
pub enum Provider {
#[default]
Vanilla,
Bucket,
Bukkit,
Spigot,
Paper,
Purpur,
Expand Down
8 changes: 4 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
pub mod plugins;
mod versions;
pub mod versions;
pub mod core;
mod additions;
pub mod additions;
mod lock;

use crate::errors::errors::ConfigErrors;
use additions::Additions;
use std::collections::HashMap;
use core::Core;
use versions::Versions;
use log::{info, warn};
use log::info;
use serde::{Deserialize, Serialize};
use tokio::fs;
use plugins::Plugin;
Expand All @@ -26,7 +26,7 @@ pub struct Config {
pub plugins: HashMap<String, Plugin>,
/// Additions for git or keys
#[serde(default)]
pub additions: Option<Additions>,
pub additions: Additions,
}

impl Config {
Expand Down
2 changes: 1 addition & 1 deletion src/config/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Plugin {

#[derive(Deserialize, Serialize, Debug, Default)]
pub enum Sources {
Bucket,
Bukkit,
Spigot,
Hangar,
#[default]
Expand Down
65 changes: 0 additions & 65 deletions src/downloader/downloader.rs

This file was deleted.

17 changes: 9 additions & 8 deletions src/downloader/hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::errors::errors::CompareHashError;
use md5::Md5;
use sha1::Digest as Digest1;
use sha1::Sha1;
Expand All @@ -19,9 +18,9 @@ impl ChooseHash {
pub async fn calculate_hash(
self,
mut reader: impl tokio::io::AsyncRead + Unpin,
) -> Result<Self, CompareHashError> {
) -> bool {
match self {
ChooseHash::SHA1(_) => {
ChooseHash::SHA1(e) => {
let mut hashed = <Sha1 as Digest1>::new();
let mut buffer = [0; 4096];
while let Ok(n) = reader.read(&mut buffer).await {
Expand All @@ -31,9 +30,9 @@ impl ChooseHash {
hashed.update(&buffer[..n]);
}
let result = hashed.finalize();
Ok(ChooseHash::SHA1(format!("{:x}", result)))
e.eq(&format!("{:x}", result))
}
ChooseHash::SHA256(_) => {
ChooseHash::SHA256(e) => {
let mut hashed = <Sha256 as Digest256>::new();
let mut buffer = [0; 4096];
while let Ok(n) = reader.read(&mut buffer).await {
Expand All @@ -43,9 +42,9 @@ impl ChooseHash {
hashed.update(&buffer[..n]);
}
let result = hashed.finalize();
Ok(ChooseHash::SHA256(format!("{:x}", result)))
e.eq(&format!("{:x}", result))
}
ChooseHash::MD5(_) => {
ChooseHash::MD5(e) => {
let mut hashed = <Md5 as md5::Digest>::new();
let mut buffer = [0; 4096];
while let Ok(n) = reader.read(&mut buffer).await {
Expand All @@ -55,12 +54,14 @@ impl ChooseHash {
hashed.update(&buffer[..n]);
}
let result = hashed.finalize();
Ok(ChooseHash::MD5(format!("{:x}", result)))
e.eq(&format!("{:x}", result))
}
}
}
}



impl std::fmt::Display for ChooseHash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
99 changes: 53 additions & 46 deletions src/downloader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,48 @@
mod hash;
mod downloader;
mod models;

use std::collections::HashMap;
use crate::{config::{core::{Core, Provider}, plugins::{Plugin, Sources}, Config}, errors::errors::DownloadErrors};
use self::hash::ChooseHash;
use log::{debug, info};

use crate::{config::{additions::Additions, core::{Core, Provider}, plugins::{Plugin, Sources}, Config}, errors::errors::DownloadErrors};
use self::{hash::ChooseHash, models::vanilla::Vanilla};

type Name = String;
type Link = String;

struct Downloader {
//List for download
download_list: Vec<Type>,
}
//Query for download

///name, link, hash
enum Type {
Plugin(Name, Link, ChooseHash),
Core(Name, Link, ChooseHash),
}
#[derive(Debug)]
pub struct Downloader();

impl Downloader {

pub async fn check(&mut self, config: &mut Config) -> Result<(), DownloadErrors> {
self.check_core(&config.core).await?;
self.check_plugins(&config.plugins).await?;
pub async fn new () -> Self {
Self {}
}

pub async fn check(self, config: &mut Config) -> Result<(), DownloadErrors> {
info!("Start check fn");
self.check_core(&config.core, &config.additions.path_to_core).await?;
// self.check_plugins(&config.plugins).await?;
todo!()
}

// ///Function download core by info in [`Config`]
// async fn choose_core(&self) -> Result<(), DownloadErrors> {
// match &self.version {
// //Download vanilla
// Versions::Vanilla(ver, freeze) => {
// let (link, hash) = Vanilla::find(&**ver).await?;
// Downloader::download_core(*freeze, link, hash).await
// }
// Versions::Purpur(_, _) => todo!(),
// Versions::Paper(_, _) => todo!(),
// Versions::Spigot(_, _) => todo!(),
// Versions::Bucket(_, _) => todo!(),
// }
// }
// async fn choose_plugin(&self) -> Result<(), DownloadErrors> {
// if let Some(plugins) = &self.plugins {
// todo!()
// } else {
// info!("Нет плагинов для скачивания");
// Ok(())
// }
// }

///Check core and add it into list for download.
async fn check_core(&mut self, core: &Core) -> Result<(), DownloadErrors> {
async fn check_core(self, core: &Core, path: &String) -> Result<(), DownloadErrors> {
info!("Check freeze and force_update");
if core.freeze && !core.force_update {return Ok(());};

info!("Start to match provider of core");
match &core.provider {
Provider::Vanilla => todo!(),
Provider::Bucket => todo!(),
Provider::Vanilla => {
info!("Find vanilla!");
let (link,hash) = Vanilla::find(&core.version).await?;
debug!("Find vanilla link: {}, hash: {}", &link, &hash);
info!("Start to download core!");
self.download_core("Vanilla", link, hash, path).await
},
Provider::Bukkit => todo!(),
Provider::Spigot => todo!(),
Provider::Paper => todo!(),
Provider::Purpur => todo!(),
Expand All @@ -65,17 +51,14 @@ impl Downloader {
Provider::NeoForge => todo!(),
}
}
// async fn download_mods(config: &Mods) {
// todo!();
// }
///Check plugins and add it into list for download.
async fn check_plugins(&mut self, plugins: &HashMap<String, Plugin>) -> Result<(), DownloadErrors> {
if plugins.is_empty() {return Ok(());};

for (name,plugin) in plugins.iter() {
if plugin.freeze && !plugin.force_update {return Ok(());};
match plugin.source {
Sources::Bucket => todo!(),
Sources::Bukkit => todo!(),
Sources::Spigot => todo!(),
Sources::Hangar => todo!(),
Sources::Modrinth => todo!(),
Expand All @@ -84,9 +67,33 @@ impl Downloader {
}
todo!()
}
async fn download_core(self, name: &str, link: String, hash: ChooseHash, download_dir: &String) -> Result<(), DownloadErrors> {
get_file(link, hash, download_dir).await?;
todo!()
}
async fn download_plugin(self) -> Result<(), DownloadErrors> {
todo!()
}
}

use std::path::Path;
use std::fs::File;
use std::io::Write;

impl Sources {
async fn get_file(link: String, hash: ChooseHash, download_dir: &str) -> Result<(), DownloadErrors> {
let response = reqwest::get(&link).await?;
let content = response.bytes().await?;

// Проверка хеша
if hash.calculate_hash(&*content).await {
let file_name = Path::new(&link).file_name().unwrap_or_default(); // Name of file
debug!("File name: {:#?}", &file_name);
let file_path = Path::new(download_dir).join(file_name); // Where to download with file name
debug!("File path: {:#?}", &file_path);
let mut file = File::create(&file_path)?; // Create the file
file.write_all(&content)?; //write
Ok(())
} else {
Err(DownloadErrors::DownloadCorrupt("Hash doesn't match".to_string()))
}
}
3 changes: 3 additions & 0 deletions src/downloader/models/modrinth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;

type ProjectID = String;
type PluginID = String;

///# Example
///we have cdn like this: `https://cdn.modrinth.com/data/PROJECT_ID/versions/ID/NAME-LOADER-VERSION.jar`
///we can take `[project_id]` -> `AANobbMI`
Expand Down
Loading

0 comments on commit 03075cd

Please sign in to comment.