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,
}
#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct GameProfile {
pub id: String,
pub name: String,

View File

@ -1,5 +1,5 @@
use anyhow::{Result, bail};
use reqwest::blocking::Client;
use reqwest::Client;
use serde::{Serialize, Deserialize};
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
.get("https://piston-meta.mojang.com/mc/game/version_manifest_v2.json")
.send()?
.json()?;
.send()
.await?
.json()
.await?;
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() {
let id = i.1.id.clone();
let v_type = i.1.v_type;
@ -62,7 +64,7 @@ pub struct VersionDetail {
pub struct Library {
pub downloads: LibraryDownload,
pub name: String,
pub rules: Vec<LibraryRule>
pub rules: Option<Vec<LibraryRule>>
}
#[derive(Serialize, Deserialize)]
@ -98,10 +100,12 @@ struct LibraryArtifact {
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
.get(version.url.clone())
.send()?
.json()?;
.send()
.await?
.json()
.await?;
Ok(received)
}

View File

@ -1,12 +1,11 @@
mod manifest;
use std::{path::Path, fs};
use std::path::Path;
use anyhow::Result;
use reqwest::blocking::Client;
use reqwest::Client;
use serde::{Serialize, Deserialize};
use crate::authentification::GameProfile;
use tokio::fs;
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;
pub struct ClientOptions<'a> {
pub authorization: &'a GameProfile,
pub root_path: &'a Path,
pub java_path: &'a Path,
pub version_number: String,
@ -37,9 +35,9 @@ pub struct MinecraftClient<'a> {
}
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 details = Self::load_manifest(&reqwest_client, &opts)?;
let details = Self::load_manifest(&reqwest_client, &opts).await?;
Ok(MinecraftClient {
opts,
reqwest_client,
@ -47,18 +45,24 @@ impl<'a> MinecraftClient<'_> {
})
}
fn load_manifest(reqwest_client: &Client, opts: &ClientOptions<'a>) -> Result<VersionDetail> {
let manifest = get_version_manifest(&reqwest_client)?;
let version = get_version_from_manifest(&manifest, opts.version_number.clone(), &opts.version_type)?;
let details = get_version_detail(&reqwest_client, version)?;
async fn load_manifest(reqwest_client: &Client, opts: &ClientOptions<'a>) -> Result<VersionDetail> {
let manifest = get_version_manifest(&reqwest_client).await?;
let version = get_version_from_manifest(&manifest, opts.version_number.clone(), &opts.version_type).await?;
let details = get_version_detail(&reqwest_client, version).await?;
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
fs::create_dir_all(self.opts.root_path)?;
fs::create_dir(self.opts.root_path.join("librairies"))?;
if !self.opts.root_path.exists() {
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();
Ok(())
}
/// Filter non necessary librairies for the current OS
@ -67,20 +71,23 @@ impl<'a> MinecraftClient<'_> {
}
fn should_use_library(library: &Library) -> bool {
if library.rules.is_empty() {
true
} else {
for i in library.rules.iter().enumerate() {
let op = if i.1.action == "allow" {
true
} else {
false
};
if i.1.os.name == ACTUAL_OS {
return op;
match &library.rules {
Some(rules) => {
for i in rules.iter().enumerate() {
let op = if i.1.action == "allow" {
true
} else {
false
};
if i.1.os.name == ACTUAL_OS {
return op;
}
}
false
},
None => {
true
}
false
}
}

View File

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

View File

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