Fix mutex issue

This commit is contained in:
Quentin Legot 2023-04-22 23:05:11 +02:00
parent a2ca88b9cd
commit 6869f97eea
5 changed files with 71 additions and 73 deletions

View File

@ -52,7 +52,7 @@ pub struct ReceivedCode {
pub state: String, pub state: String,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug, Clone)]
pub struct GameProfile { pub struct GameProfile {
pub id: String, pub id: String,
pub name: String, pub name: String,

View File

@ -1,5 +1,5 @@
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use reqwest::blocking::Client; use reqwest::Client;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use serde_json::{Value, Map}; use serde_json::{Value, Map};
@ -21,15 +21,17 @@ pub struct Version {
} }
pub fn get_version_manifest(reqwest: &Client) -> Result<VersionManifestV2> { pub async fn get_version_manifest(reqwest: &Client) -> Result<VersionManifestV2> {
let received: VersionManifestV2 = reqwest let received: VersionManifestV2 = reqwest
.get("https://piston-meta.mojang.com/mc/game/version_manifest_v2.json") .get("https://piston-meta.mojang.com/mc/game/version_manifest_v2.json")
.send()? .send()
.json()?; .await?
.json()
.await?;
Ok(received) Ok(received)
} }
pub fn get_version_from_manifest<'a>(manifest: &'a VersionManifestV2, game_version: String, version_type: &VersionType) -> Result<&'a Version> { pub async fn get_version_from_manifest<'a>(manifest: &'a VersionManifestV2, game_version: String, version_type: &VersionType) -> Result<&'a Version> {
for i in manifest.versions.iter().enumerate() { for i in manifest.versions.iter().enumerate() {
let id = i.1.id.clone(); let id = i.1.id.clone();
let v_type = i.1.v_type; let v_type = i.1.v_type;
@ -62,7 +64,7 @@ pub struct VersionDetail {
pub struct Library { pub struct Library {
pub downloads: LibraryDownload, pub downloads: LibraryDownload,
pub name: String, pub name: String,
pub rules: Vec<LibraryRule> pub rules: Option<Vec<LibraryRule>>
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -98,10 +100,12 @@ struct LibraryArtifact {
url: String, url: String,
} }
pub fn get_version_detail(reqwest: &Client, version : &Version) -> Result<VersionDetail> { pub async fn get_version_detail(reqwest: &Client, version : &Version) -> Result<VersionDetail> {
let received: VersionDetail = reqwest let received: VersionDetail = reqwest
.get(version.url.clone()) .get(version.url.clone())
.send()? .send()
.json()?; .await?
.json()
.await?;
Ok(received) Ok(received)
} }

View File

@ -1,12 +1,11 @@
mod manifest; mod manifest;
use std::{path::Path, fs}; use std::path::Path;
use anyhow::Result; use anyhow::Result;
use reqwest::blocking::Client; use reqwest::Client;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use tokio::fs;
use crate::authentification::GameProfile;
use self::manifest::{VersionDetail, get_version_manifest, get_version_from_manifest, get_version_detail, Library, OSName}; use self::manifest::{VersionDetail, get_version_manifest, get_version_from_manifest, get_version_detail, Library, OSName};
@ -19,7 +18,6 @@ const ACTUAL_OS: OSName = OSName::Linux;
const ACTUAL_OS: OSName = OSName::MacOsX; const ACTUAL_OS: OSName = OSName::MacOsX;
pub struct ClientOptions<'a> { pub struct ClientOptions<'a> {
pub authorization: &'a GameProfile,
pub root_path: &'a Path, pub root_path: &'a Path,
pub java_path: &'a Path, pub java_path: &'a Path,
pub version_number: String, pub version_number: String,
@ -37,9 +35,9 @@ pub struct MinecraftClient<'a> {
} }
impl<'a> MinecraftClient<'_> { impl<'a> MinecraftClient<'_> {
pub fn new(opts: &'a ClientOptions<'a>) -> Result<MinecraftClient<'a>> { pub async fn new(opts: &'a ClientOptions<'a>) -> Result<MinecraftClient<'a>> {
let reqwest_client = Client::new(); let reqwest_client = Client::new();
let details = Self::load_manifest(&reqwest_client, &opts)?; let details = Self::load_manifest(&reqwest_client, &opts).await?;
Ok(MinecraftClient { Ok(MinecraftClient {
opts, opts,
reqwest_client, reqwest_client,
@ -47,18 +45,24 @@ impl<'a> MinecraftClient<'_> {
}) })
} }
fn load_manifest(reqwest_client: &Client, opts: &ClientOptions<'a>) -> Result<VersionDetail> { async fn load_manifest(reqwest_client: &Client, opts: &ClientOptions<'a>) -> Result<VersionDetail> {
let manifest = get_version_manifest(&reqwest_client)?; let manifest = get_version_manifest(&reqwest_client).await?;
let version = get_version_from_manifest(&manifest, opts.version_number.clone(), &opts.version_type)?; let version = get_version_from_manifest(&manifest, opts.version_number.clone(), &opts.version_type).await?;
let details = get_version_detail(&reqwest_client, version)?; let details = get_version_detail(&reqwest_client, version).await?;
Ok(details) Ok(details)
} }
pub fn download_assets(&mut self) -> Result<()> { pub async fn download_assets(&mut self) -> Result<()> {
// create root folder if it doesn't exist // create root folder if it doesn't exist
fs::create_dir_all(self.opts.root_path)?; if !self.opts.root_path.exists() {
fs::create_dir(self.opts.root_path.join("librairies"))?; fs::create_dir_all(self.opts.root_path).await?;
}
let lib = self.opts.root_path.join("librairies");
if !lib.exists() {
fs::create_dir(lib).await?;
}
self.filter_non_necessary_librairies(); self.filter_non_necessary_librairies();
Ok(()) Ok(())
} }
/// Filter non necessary librairies for the current OS /// Filter non necessary librairies for the current OS
@ -67,10 +71,9 @@ impl<'a> MinecraftClient<'_> {
} }
fn should_use_library(library: &Library) -> bool { fn should_use_library(library: &Library) -> bool {
if library.rules.is_empty() { match &library.rules {
true Some(rules) => {
} else { for i in rules.iter().enumerate() {
for i in library.rules.iter().enumerate() {
let op = if i.1.action == "allow" { let op = if i.1.action == "allow" {
true true
} else { } else {
@ -81,6 +84,10 @@ impl<'a> MinecraftClient<'_> {
} }
} }
false false
},
None => {
true
}
} }
} }

View File

@ -9,7 +9,7 @@ pub mod launcher;
use std::sync::Mutex; use std::sync::Mutex;
use authentification::{Authentification, Prompt, GameProfile}; use authentification::{Authentification, Prompt, GameProfile};
use anyhow::Result; use anyhow::{Result, bail};
use directories::BaseDirs; use directories::BaseDirs;
use launcher::{MinecraftClient, ClientOptions}; use launcher::{MinecraftClient, ClientOptions};
@ -44,45 +44,32 @@ async fn login(app: tauri::AppHandle, _window: tauri::Window, state: tauri::Stat
#[tauri::command] #[tauri::command]
async fn download(state: tauri::State<'_, Mutex<CustomState>>) -> Result<String, String> { async fn download(state: tauri::State<'_, Mutex<CustomState>>) -> Result<String, String> {
if let Some(base_dir) = BaseDirs::new() { if let Some(base_dir) = BaseDirs::new() {
let data_folder = base_dir.data_dir().join(".altarik"); let data_folder = base_dir.data_dir().join(".altarik_test");
let root_path = data_folder.as_path(); let root_path = data_folder.as_path();
match state.lock() { let java_path = root_path.join("java");
Ok(game_profile) => { let game_profile = match state.lock() {
match &game_profile.0 { Ok(res) => Ok(res.0.clone()),
Some(game_profile) => { Err(err) => Err(err.to_string())
// let java_path = root_path.join("java"); }?;
// let opts = ClientOptions { let opts = ClientOptions {
// authorization: game_profile, root_path,
// root_path, java_path: &java_path.as_path(),
// java_path: &java_path.as_path(), version_number: "1.19.4".to_string(),
// version_number: "1.19.4".to_string(), version_type: launcher::VersionType::Release,
// version_type: launcher::VersionType::Release, memory_min: "2G".to_string(),
// memory_min: "2G".to_string(), memory_max: "4G".to_string(),
// memory_max: "4G".to_string(), };
// }; let client = MinecraftClient::new(&opts).await;
// let client = MinecraftClient::new(&opts); match client {
// match client { Ok(mut client) => {
// Ok(mut client) => { match client.download_assets().await {
// match client.download_assets() { Ok(_) => {
// Ok(_) => { Ok("Content downloaded".to_string())
// Ok("Content downloaded".to_string())
// },
// Err(err) => {
// Err(err.to_string())
// }
// }
// },
// Err(err) => {
// Err(err.to_string())
// }
// }
Ok("Client created".to_string())
}, },
None => { Err(err) => {
Err("You're not connected".to_string()) Err(err.to_string())
} }
} }
}, },
Err(err) => { Err(err) => {
Err(err.to_string()) Err(err.to_string())

View File

@ -29,7 +29,7 @@
"icons/icon.icns", "icons/icon.icns",
"icons/icon.ico" "icons/icon.ico"
], ],
"identifier": "com.tauri.dev", "identifier": "fr.altarik.launcher",
"longDescription": "", "longDescription": "",
"macOS": { "macOS": {
"entitlements": null, "entitlements": null,