From 940e41e291940069e3957552e610f2d7f3f9017d Mon Sep 17 00:00:00 2001 From: LynithDev <61880709+LynithDev@users.noreply.github.com> Date: Mon, 1 Apr 2024 16:21:28 +0200 Subject: [PATCH] feat: Downloading assets --- onelauncher/src/game/client.rs | 11 ++++++++++- onelauncher/src/game/clients/vanilla.rs | 14 ++++++++++---- src/ui/pages/Home.tsx | 2 +- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/onelauncher/src/game/client.rs b/onelauncher/src/game/client.rs index 7d6d4902..d8cc47da 100644 --- a/onelauncher/src/game/client.rs +++ b/onelauncher/src/game/client.rs @@ -5,6 +5,7 @@ use async_trait::async_trait; use chrono::{DateTime, Local}; use serde::{Deserialize, Serialize}; use tokio::{io::{AsyncBufReadExt, BufReader}, process}; +use tracing::info; use uuid::Uuid; use crate::auth::Account; @@ -29,6 +30,12 @@ pub struct Cluster { pub client: ClientType, } +impl Cluster { + pub fn dir(&self) -> crate::Result { + Ok(crate::utils::dirs::cluster_dir(self.id.to_string())?) + } +} + #[derive(Debug)] pub struct LaunchInfo { pub java: PathBuf, @@ -44,6 +51,8 @@ pub struct SetupInfo { pub libraries: String, pub game_dir: PathBuf, pub natives_dir: PathBuf, + pub assets_dir: PathBuf, + pub asset_index: String, } pub struct LaunchCallbacks { @@ -63,7 +72,7 @@ pub trait ClientTrait<'a>: Send + Sync { .replace("${auth_player_name}", &info.account.username) .replace("${version_name}", &info.setup.version) .replace("${game_directory}", info.setup.game_dir.to_str().unwrap()) - .replace("${assets_root}", format!("{}/assets", info.setup.game_dir.to_str().unwrap()).as_str()) + .replace("${assets_root}", info.setup.assets_dir.to_str().unwrap()) .replace("${assets_index_name}", &manifest.asset_index.id) .replace("${auth_uuid}", &info.account.uuid) .replace("${auth_access_token}", &info.account.access_token) diff --git a/onelauncher/src/game/clients/vanilla.rs b/onelauncher/src/game/clients/vanilla.rs index ca79437f..4d206488 100644 --- a/onelauncher/src/game/clients/vanilla.rs +++ b/onelauncher/src/game/clients/vanilla.rs @@ -30,14 +30,17 @@ impl<'a> ClientTrait<'a> for VanillaClient<'a> { async fn setup(&self) -> crate::Result { let manifest = &self.manifest.minecraft_manifest; + // Install everything let client_path = install_game(manifest).await?; let mut libraries = install_libraries(manifest).await?; + let assets = install_assets(manifest).await?; // Append client path to the libraries string at the end libraries.push_str(constants::LIBRARY_SPLITTER); libraries.push_str(client_path.to_str().unwrap()); - let game_dir = dirs::cluster_dir(self.cluster.id.to_string())?.join("game"); + // Create game directory + let game_dir = self.cluster.dir()?.join("game"); fs::create_dir_all(&game_dir)?; Ok(SetupInfo { @@ -45,6 +48,8 @@ impl<'a> ClientTrait<'a> for VanillaClient<'a> { libraries, natives_dir: dirs::natives_dir()?, game_dir, + assets_dir: dirs::assets_dir()?, + asset_index: assets, }) } } @@ -125,7 +130,7 @@ pub async fn install_natives(natives: Vec<&Library>) -> crate::Result<()> { Ok(()) } -pub async fn install_assets(manifest: &MinecraftManifest) -> crate::Result<()> { +pub async fn install_assets(manifest: &MinecraftManifest) -> crate::Result { let assets = dirs::assets_dir()?; let objects = assets.join("objects"); let indexes = assets.join("indexes"); @@ -140,7 +145,8 @@ pub async fn install_assets(manifest: &MinecraftManifest) -> crate::Result<()> { } let contents = serde_json::from_str::(&fs::read_to_string(&index)?)?; - for (hash, asset) in contents.objects { + for (_, asset) in contents.objects { + let hash = asset.hash.clone(); let short = &hash[..2]; let file = objects.join(&short).join(&hash); fs::create_dir_all(file.parent().ok_or(anyhow!("Couldn't get asset parent"))?)?; @@ -151,7 +157,7 @@ pub async fn install_assets(manifest: &MinecraftManifest) -> crate::Result<()> { } } - Ok(()) + Ok(manifest.asset_index.id.clone()) } #[derive(Clone, Serialize, Deserialize)] diff --git a/src/ui/pages/Home.tsx b/src/ui/pages/Home.tsx index d1194eb4..07836348 100644 --- a/src/ui/pages/Home.tsx +++ b/src/ui/pages/Home.tsx @@ -197,7 +197,7 @@ function HomePage() { async function newInstance() { await createCluster({ name: 'Modern cluster', - version: '1.20.4', + version: '1.8.9', client: { type: 'Vanilla', },