You've already forked AstralRinth
forked from didirus/AstralRinth
Fabric support
This commit is contained in:
188
daedalus_client/src/fabric.rs
Normal file
188
daedalus_client/src/fabric.rs
Normal file
@@ -0,0 +1,188 @@
|
||||
use crate::{format_url, upload_file_to_bucket, Error};
|
||||
use daedalus::fabric::PartialVersionInfo;
|
||||
use daedalus::minecraft::Library;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub async fn retrieve_data() -> Result<(), Error> {
|
||||
let mut list = daedalus::fabric::fetch_fabric_versions(None).await?;
|
||||
|
||||
let loaders = RwLock::new(Vec::new());
|
||||
let visited_artifacts_mutex = Arc::new(Mutex::new(Vec::new()));
|
||||
|
||||
if let Some(latest) = list.loader.get(0) {
|
||||
{
|
||||
let mut loaders = match loaders.write() {
|
||||
Ok(guard) => guard,
|
||||
Err(poisoned) => poisoned.into_inner(),
|
||||
};
|
||||
|
||||
loaders.push(latest.version.clone());
|
||||
|
||||
if !latest.stable {
|
||||
if let Some(stable) = list.loader.iter().find(|x| x.stable) {
|
||||
loaders.push(stable.version.clone());
|
||||
}
|
||||
}
|
||||
|
||||
list.loader = list
|
||||
.loader
|
||||
.into_iter()
|
||||
.filter(|x| loaders.contains(&x.version))
|
||||
.collect();
|
||||
}
|
||||
|
||||
let mut versions = list
|
||||
.game
|
||||
.iter_mut()
|
||||
.map(|game_version| {
|
||||
let loaders = match loaders.read() {
|
||||
Ok(guard) => guard,
|
||||
Err(poisoned) => poisoned.into_inner(),
|
||||
};
|
||||
|
||||
let visited_artifacts_mutex = Arc::clone(&visited_artifacts_mutex);
|
||||
let game_version_mutex = Mutex::new(HashMap::new());
|
||||
|
||||
async move {
|
||||
let versions = futures::future::try_join_all(loaders.clone().into_iter().map(
|
||||
|loader| async {
|
||||
let version = daedalus::fabric::fetch_fabric_version(
|
||||
&*game_version.version,
|
||||
&*loader,
|
||||
)
|
||||
.await
|
||||
.expect(&*format!("{}, {}", game_version.version, loader));
|
||||
|
||||
Ok::<(String, PartialVersionInfo), Error>((loader, version))
|
||||
},
|
||||
))
|
||||
.await?;
|
||||
|
||||
futures::future::try_join_all(versions.into_iter().map(
|
||||
|(loader, version)| async {
|
||||
let libs = futures::future::try_join_all(
|
||||
version.libraries.into_iter().map(|mut lib| async {
|
||||
{
|
||||
let mut visited_assets =
|
||||
match visited_artifacts_mutex.lock() {
|
||||
Ok(guard) => guard,
|
||||
Err(poisoned) => poisoned.into_inner(),
|
||||
};
|
||||
|
||||
if visited_assets.contains(&lib.name) {
|
||||
lib.url = Some(format_url("maven/"));
|
||||
|
||||
return Ok(lib);
|
||||
} else {
|
||||
visited_assets.push(lib.name.clone())
|
||||
}
|
||||
}
|
||||
|
||||
let artifact_path =
|
||||
daedalus::get_path_from_artifact(&*lib.name)?;
|
||||
|
||||
let artifact = daedalus::download_file(
|
||||
&*format!(
|
||||
"{}{}",
|
||||
lib.url.unwrap_or_else(|| {
|
||||
"https://maven.fabricmc.net/".to_string()
|
||||
}),
|
||||
artifact_path
|
||||
),
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
|
||||
lib.url = Some(format_url("maven/"));
|
||||
|
||||
upload_file_to_bucket(
|
||||
format!("{}/{}", "maven", artifact_path),
|
||||
artifact.to_vec(),
|
||||
Some("application/java-archive".to_string()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok::<Library, Error>(lib)
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let version_path = format!(
|
||||
"fabric/v{}/versions/{}-{}.json",
|
||||
daedalus::fabric::CURRENT_FORMAT_VERSION,
|
||||
version.inherits_from,
|
||||
loader
|
||||
);
|
||||
|
||||
upload_file_to_bucket(
|
||||
version_path.clone(),
|
||||
serde_json::to_vec(&PartialVersionInfo {
|
||||
arguments: version.arguments,
|
||||
id: version.id,
|
||||
main_class: version.main_class,
|
||||
release_time: version.release_time,
|
||||
time: version.time,
|
||||
type_: version.type_,
|
||||
inherits_from: version.inherits_from,
|
||||
libraries: libs,
|
||||
})?,
|
||||
Some("application/json".to_string()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
{
|
||||
let mut game_version_map = match game_version_mutex.lock() {
|
||||
Ok(guard) => guard,
|
||||
Err(poisoned) => poisoned.into_inner(),
|
||||
};
|
||||
game_version_map.insert(loader, format_url(&*version_path));
|
||||
}
|
||||
|
||||
Ok::<(), Error>(())
|
||||
},
|
||||
))
|
||||
.await?;
|
||||
|
||||
game_version.urls = Some(
|
||||
match game_version_mutex.lock() {
|
||||
Ok(guard) => guard,
|
||||
Err(poisoned) => poisoned.into_inner(),
|
||||
}
|
||||
.clone(),
|
||||
);
|
||||
|
||||
Ok::<(), Error>(())
|
||||
}
|
||||
})
|
||||
.peekable();
|
||||
|
||||
let mut chunk_index = 0;
|
||||
while versions.peek().is_some() {
|
||||
let now = Instant::now();
|
||||
|
||||
let chunk: Vec<_> = versions.by_ref().take(10).collect();
|
||||
futures::future::try_join_all(chunk).await?;
|
||||
|
||||
std::thread::sleep(Duration::from_secs(1));
|
||||
|
||||
chunk_index += 1;
|
||||
|
||||
let elapsed = now.elapsed();
|
||||
println!("Chunk {} Elapsed: {:.2?}", chunk_index, elapsed);
|
||||
}
|
||||
}
|
||||
|
||||
upload_file_to_bucket(
|
||||
format!(
|
||||
"fabric/v{}/manifest.json",
|
||||
daedalus::fabric::CURRENT_FORMAT_VERSION,
|
||||
),
|
||||
serde_json::to_vec(&list)?,
|
||||
Some("application/json".to_string()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
use log::{error, info, warn};
|
||||
use rusoto_core::credential::StaticProvider;
|
||||
use rusoto_core::{HttpClient, Region, RusotoError};
|
||||
use rusoto_s3::{S3Client, PutObjectError};
|
||||
use rusoto_s3::{PutObjectError, S3Client};
|
||||
use rusoto_s3::{PutObjectRequest, S3};
|
||||
|
||||
mod fabric;
|
||||
mod minecraft;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
@@ -13,10 +14,7 @@ pub enum Error {
|
||||
#[error("Error while deserializing JSON")]
|
||||
SerdeError(#[from] serde_json::Error),
|
||||
#[error("Unable to fetch {item}")]
|
||||
FetchError {
|
||||
inner: reqwest::Error,
|
||||
item: String,
|
||||
},
|
||||
FetchError { inner: reqwest::Error, item: String },
|
||||
#[error("Error while managing asynchronous tasks")]
|
||||
TaskError(#[from] tokio::task::JoinError),
|
||||
#[error("Error while uploading file to S3")]
|
||||
@@ -34,7 +32,7 @@ async fn main() {
|
||||
return;
|
||||
}
|
||||
|
||||
minecraft::retrieve_data().await.unwrap();
|
||||
fabric::retrieve_data().await.unwrap();
|
||||
}
|
||||
|
||||
fn check_env_vars() -> bool {
|
||||
@@ -96,7 +94,11 @@ lazy_static::lazy_static! {
|
||||
);
|
||||
}
|
||||
|
||||
pub async fn upload_file_to_bucket(path: String, bytes: Vec<u8>, content_type: Option<String>) -> Result<(), Error> {
|
||||
pub async fn upload_file_to_bucket(
|
||||
path: String,
|
||||
bytes: Vec<u8>,
|
||||
content_type: Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
CLIENT
|
||||
.put_object(PutObjectRequest {
|
||||
bucket: dotenv::var("S3_BUCKET_NAME").unwrap(),
|
||||
@@ -109,7 +111,7 @@ pub async fn upload_file_to_bucket(path: String, bytes: Vec<u8>, content_type: O
|
||||
.await
|
||||
.map_err(|err| Error::S3Error {
|
||||
inner: err,
|
||||
file: format!("{}/{}", &*dotenv::var("BASE_FOLDER").unwrap(), path)
|
||||
file: format!("{}/{}", &*dotenv::var("BASE_FOLDER").unwrap(), path),
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -6,14 +6,13 @@ use std::time::{Duration, Instant};
|
||||
pub async fn retrieve_data() -> Result<(), Error> {
|
||||
let old_manifest =
|
||||
daedalus::minecraft::fetch_version_manifest(Some(&*crate::format_url(&*format!(
|
||||
"minecraft/v{}/version_manifest.json",
|
||||
"minecraft/v{}/manifest.json",
|
||||
daedalus::minecraft::CURRENT_FORMAT_VERSION
|
||||
))))
|
||||
.await
|
||||
.ok();
|
||||
|
||||
let mut manifest = daedalus::minecraft::fetch_version_manifest(None)
|
||||
.await?;
|
||||
let mut manifest = daedalus::minecraft::fetch_version_manifest(None).await?;
|
||||
let cloned_manifest = Arc::new(Mutex::new(manifest.clone()));
|
||||
|
||||
let visited_assets_mutex = Arc::new(Mutex::new(Vec::new()));
|
||||
@@ -44,12 +43,7 @@ pub async fn retrieve_data() -> Result<(), Error> {
|
||||
async move {
|
||||
let mut upload_futures = Vec::new();
|
||||
|
||||
let now = Instant::now();
|
||||
let mut version_println = daedalus::minecraft::fetch_version_info(version)
|
||||
.await
|
||||
?;
|
||||
let elapsed = now.elapsed();
|
||||
println!("Version {} Elapsed: {:.2?}", version.id, elapsed);
|
||||
let mut version_info = daedalus::minecraft::fetch_version_info(version).await?;
|
||||
|
||||
let version_path = format!(
|
||||
"minecraft/v{}/versions/{}.json",
|
||||
@@ -59,9 +53,9 @@ pub async fn retrieve_data() -> Result<(), Error> {
|
||||
let assets_path = format!(
|
||||
"minecraft/v{}/assets/{}.json",
|
||||
daedalus::minecraft::CURRENT_FORMAT_VERSION,
|
||||
version_println.asset_index.id
|
||||
version_info.asset_index.id
|
||||
);
|
||||
let assets_index_url = version_println.asset_index.url.clone();
|
||||
let assets_index_url = version_info.asset_index.url.clone();
|
||||
|
||||
{
|
||||
let mut cloned_manifest = match cloned_manifest_mutex.lock() {
|
||||
@@ -76,10 +70,10 @@ pub async fn retrieve_data() -> Result<(), Error> {
|
||||
.unwrap();
|
||||
cloned_manifest.versions[position].url = format_url(&version_path);
|
||||
cloned_manifest.versions[position].assets_index_sha1 =
|
||||
Some(version_println.asset_index.sha1.clone());
|
||||
Some(version_info.asset_index.sha1.clone());
|
||||
cloned_manifest.versions[position].assets_index_url =
|
||||
Some(format_url(&assets_path));
|
||||
version_println.asset_index.url = format_url(&assets_path);
|
||||
version_info.asset_index.url = format_url(&assets_path);
|
||||
}
|
||||
|
||||
let mut download_assets = false;
|
||||
@@ -90,9 +84,9 @@ pub async fn retrieve_data() -> Result<(), Error> {
|
||||
Err(poisoned) => poisoned.into_inner(),
|
||||
};
|
||||
|
||||
if !visited_assets.contains(&version_println.asset_index.id) {
|
||||
if !visited_assets.contains(&version_info.asset_index.id) {
|
||||
if let Some(assets_hash) = assets_hash {
|
||||
if version_println.asset_index.sha1 != assets_hash {
|
||||
if version_info.asset_index.sha1 != assets_hash {
|
||||
download_assets = true;
|
||||
}
|
||||
} else {
|
||||
@@ -101,26 +95,29 @@ pub async fn retrieve_data() -> Result<(), Error> {
|
||||
}
|
||||
|
||||
if download_assets {
|
||||
visited_assets.push(version_println.asset_index.id.clone());
|
||||
visited_assets.push(version_info.asset_index.id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
if download_assets {
|
||||
let assets_index =
|
||||
download_file(&assets_index_url, Some(&version_println.asset_index.sha1))
|
||||
download_file(&assets_index_url, Some(&version_info.asset_index.sha1))
|
||||
.await?;
|
||||
|
||||
{
|
||||
upload_futures
|
||||
.push(upload_file_to_bucket(assets_path, assets_index.to_vec(), Some("application/json".to_string())));
|
||||
upload_futures.push(upload_file_to_bucket(
|
||||
assets_path,
|
||||
assets_index.to_vec(),
|
||||
Some("application/json".to_string()),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
upload_futures.push(upload_file_to_bucket(
|
||||
version_path,
|
||||
serde_json::to_vec(&version_println)?,
|
||||
Some("application/json".to_string())
|
||||
serde_json::to_vec(&version_info)?,
|
||||
Some("application/json".to_string()),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -154,14 +151,14 @@ pub async fn retrieve_data() -> Result<(), Error> {
|
||||
|
||||
upload_file_to_bucket(
|
||||
format!(
|
||||
"minecraft/v{}/version_manifest.json",
|
||||
"minecraft/v{}/manifest.json",
|
||||
daedalus::minecraft::CURRENT_FORMAT_VERSION
|
||||
),
|
||||
serde_json::to_vec(&*match cloned_manifest.lock() {
|
||||
Ok(guard) => guard,
|
||||
Err(poisoned) => poisoned.into_inner(),
|
||||
})?,
|
||||
Some("application/json".to_string())
|
||||
Some("application/json".to_string()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user