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

Commit

Permalink
Create fn to get link for download minecraft.jar
Browse files Browse the repository at this point in the history
  • Loading branch information
TOwInOK committed Mar 3, 2024
1 parent d0d3f8a commit e4f2142
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/config/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ pub enum DownloadErrors {
#[derive(Error, Debug)]
pub enum ConfigErrors {
#[error("Загрузка файла не была успешна: {0}")]
LoadCorrapt(String),
LoadCorrupt(String),
}
28 changes: 15 additions & 13 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
mod datapack;
mod errors;
mod models;
mod plugin;
mod version;
mod models;

use models::{vanila::Vanila, *};
use tempfile::Builder;
use datapack::*;
use errors::*;
use log::{error, info};
use models::{vanila::Vanilla, *};
use plugin::Plugin;
use serde::{Deserialize, Serialize};
use tempfile::Builder;
use tokio::fs;
use version::Versions;

Expand Down Expand Up @@ -81,7 +81,7 @@ impl Config {
todo!()
}

///Function download core by info in [`Config`]
///Function download core by info in [`Config`]
async fn download_core(self) -> Result<Option<()>, DownloadErrors> {
match self.version {
//Download purpur
Expand All @@ -101,7 +101,7 @@ impl Config {
}
//use if error
Err(DownloadErrors::DownloadCorrapt("ff".to_string()))
},
}
//Download Spigot
Versions::Spigot(ver, freez) => {
if freez {
Expand All @@ -110,7 +110,7 @@ impl Config {
}
//use if error
Err(DownloadErrors::DownloadCorrapt("ff".to_string()))
},
}
//Download Bucket
Versions::Bucket(ver, freez) => {
if freez {
Expand All @@ -119,7 +119,7 @@ impl Config {
}
//use if error
Err(DownloadErrors::DownloadCorrapt("ff".to_string()))
},
}
//Download Vanila
Versions::Vanila(ver, freez) => {
if freez {
Expand All @@ -129,13 +129,15 @@ impl Config {
//use if error
// Err(DownloadErrors::DownloadCorrapt("ff".to_string()))
// let tmp_dir = Builder::new().tempdir().map_err(|er| ConfigErrors::LoadCorrapt(er.to_string()));
let _ = match Vanila::find(&*ver).await {
Ok(_) => {},
Err(e) => {error!("{:#?}", e)},
let _ = match Vanilla::find(&*ver).await {
Ok(_) => {}
Err(e) => {
error!("{:#?}", e)
}
};

todo!()
},
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/config/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod item;
pub mod vanila;
pub mod vanila;
95 changes: 72 additions & 23 deletions src/config/models/vanila.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use log::info;
use reqwest::Request;
use log::warn;
use serde::Deserialize;
use serde::Serialize;

use crate::config::ConfigErrors;

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Vanila {
pub struct Vanilla {
pub latest: Latest,
pub versions: Vec<Version>,
}
Expand All @@ -22,17 +22,36 @@ pub struct Latest {
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Version {
pub id: String,
#[serde(rename = "id")]
pub version: String,

#[serde(rename = "type")]
#[serde(skip)]
pub type_field: String,
pub type_field: TypeOfVersion,

pub url: String,
#[serde(skip)]
pub time: String,
#[serde(skip)]
pub release_time: String,
}

///Minecraft types of version
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TypeOfVersion {
#[serde(rename = "release")]
Release,
#[serde(rename = "snapshot")]
Snapshot,
#[serde(rename = "old_beta")]
OldBeta,
#[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)]
#[serde(rename_all = "camelCase")]
Expand All @@ -53,22 +72,52 @@ pub struct Server {
pub url: String,
}

impl Vanila {
///Making request to mojang api and find the link to download minecraft.jar
//

impl Server {
pub async fn download(self) {
todo!()
}
}

impl Vanilla {
/// Making request to mojang api and find the link to download minecraft.jar
pub async fn find(version: &str) -> Result<(), ConfigErrors> {
const LINK: &str = "https://piston-meta.mojang.com/v1/packages/8bcd47def18efee744bd0700e86ab44a96ade21f/1.20.4.json";
let body = match reqwest::get(LINK).await {
Ok(e) => {match e.json::<DownloadSection>().await {
Ok(e) => {e.downloads.server},
Err(e) => {Err(ConfigErrors::LoadCorrapt(e.to_string()))}?,
}},
Err(e) => {Err(ConfigErrors::LoadCorrapt(e.to_string()))}?,
};
info!("Check body: {:#?}", &body);
let link = Vanilla::find_version(version)
.await
.map_err(|e| ConfigErrors::LoadCorrupt(e.to_string()))?;
let response = reqwest::get(link)
.await
.map_err(|e| ConfigErrors::LoadCorrupt(e.to_string()))?;
let download_section: DownloadSection = response
.json()
.await
.map_err(|e| ConfigErrors::LoadCorrupt(e.to_string()))?;

info!("Check body: {:#?}", &download_section.downloads.server);

Ok(())
}

pub async fn download(link: &str) {
todo!()
///Return `url` for get a json which contain link to donwload
pub async fn find_version(version: &str) -> Result<String, ConfigErrors> {
const LINK: &str = "https://launchermeta.mojang.com/mc/game/version_manifest.json";

let response = reqwest::get(LINK)
.await
.map_err(|e| ConfigErrors::LoadCorrupt(e.to_string()))?;
let vanilla: Vanilla = response
.json()
.await
.map_err(|e| ConfigErrors::LoadCorrupt(e.to_string()))?;

vanilla
.versions
.iter()
.find(|x| x.version.contains(version))
.map(|x| x.url.clone())
.ok_or_else(|| {
ConfigErrors::LoadCorrupt(format!("No one version like: {}, not found", version))
})
}
}
}

0 comments on commit e4f2142

Please sign in to comment.