From 52a6b921a9a57932618981e9b2aa544d98eccd3f Mon Sep 17 00:00:00 2001 From: ALEZ-DEV Date: Fri, 3 May 2024 23:03:52 +0200 Subject: [PATCH] refactor and add some features to the config file --- babylonia-terminal-cli/src/main.rs | 37 +++++++++++++-- .../src/components/dxvk_component.rs | 5 +- .../src/components/game_component.rs | 2 +- .../src/components/proton_component.rs | 4 +- babylonia-terminal-sdk/src/game_manager.rs | 21 +++------ babylonia-terminal-sdk/src/game_state.rs | 47 ++++++++++++------- babylonia-terminal-sdk/src/lib.rs | 1 + 7 files changed, 80 insertions(+), 37 deletions(-) diff --git a/babylonia-terminal-cli/src/main.rs b/babylonia-terminal-cli/src/main.rs index a3944f0..72639bf 100644 --- a/babylonia-terminal-cli/src/main.rs +++ b/babylonia-terminal-cli/src/main.rs @@ -1,8 +1,12 @@ +use std::{path::PathBuf, str::FromStr}; + use babylonia_terminal_sdk::{ - components::proton_component::ProtonComponent, game_manager::GameManager, game_state::GameState, + components::proton_component::ProtonComponent, game_manager::GameManager, game_patcher, + game_state::GameState, }; use log::{debug, info, LevelFilter}; use simple_logger::SimpleLogger; +use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader}; use wincompatlib::prelude::*; pub mod reporter; @@ -32,8 +36,7 @@ async fn main() { let state = GameState::get_current_state().await; if state != GameState::WineNotInstalled && proton == None { - let proton_component = - ProtonComponent::new(GameState::get_config_directory().join("wine")); + let proton_component = ProtonComponent::new(GameState::get_config_directory()); match proton_component.init_proton() { Ok(p) => proton = Some(p), Err(err) => panic!("{}", err), @@ -84,6 +87,34 @@ async fn main() { } GameState::GameNotInstalled => { info!("Game not installed, installing it..."); + if GameState::get_game_dir().await.is_none() { + info!( + "You can choose where to put your game directory, (default '{}')", + GameState::get_config_directory().to_str().unwrap(), + ); + info!("Please enter your wanted game directory : "); + let mut input = BufReader::new(tokio::io::stdin()) + .lines() + .next_line() + .await + .unwrap(); + + let mut dir = PathBuf::new(); + if let Some(i) = &mut input { + if i.is_empty() { + dir = GameState::get_config_directory(); + } else { + dir = PathBuf::from_str(i).expect("This is not a valid directory!\n Please restart the launcher and put a valid path."); + } + } else { + dir = GameState::get_config_directory(); + } + + GameState::set_game_dir(Some(dir)).await.expect( + "Failed to save the game directory into the config file, please retry!", + ); + } + GameManager::install_game( GameState::get_config_directory().join("PGR"), DownloadReporter::create(false), diff --git a/babylonia-terminal-sdk/src/components/dxvk_component.rs b/babylonia-terminal-sdk/src/components/dxvk_component.rs index 730d2c8..cbd9055 100644 --- a/babylonia-terminal-sdk/src/components/dxvk_component.rs +++ b/babylonia-terminal-sdk/src/components/dxvk_component.rs @@ -23,7 +23,10 @@ pub struct DXVKComponent<'a> { impl<'a> DXVKComponent<'a> { pub fn from_wine<'b: 'a>(wine: &'b Wine, path: PathBuf) -> Self { - DXVKComponent { wine, path } + DXVKComponent { + wine, + path: path.join("dxvk"), + } } } diff --git a/babylonia-terminal-sdk/src/components/game_component.rs b/babylonia-terminal-sdk/src/components/game_component.rs index 29ececb..6504f0e 100644 --- a/babylonia-terminal-sdk/src/components/game_component.rs +++ b/babylonia-terminal-sdk/src/components/game_component.rs @@ -39,7 +39,7 @@ impl GameComponent { game_dir: &std::path::PathBuf, resources: &Resources, ) -> anyhow::Result> { - info!("checking all files..."); + info!("checking all files, this can take a while..."); let mut to_download: Vec = vec![]; for r in &resources.resource { diff --git a/babylonia-terminal-sdk/src/components/proton_component.rs b/babylonia-terminal-sdk/src/components/proton_component.rs index 08fb842..a4b254b 100644 --- a/babylonia-terminal-sdk/src/components/proton_component.rs +++ b/babylonia-terminal-sdk/src/components/proton_component.rs @@ -91,7 +91,9 @@ impl ComponentDownloader for ProtonComponent { impl ProtonComponent { pub fn new(path: PathBuf) -> Self { - ProtonComponent { path } + ProtonComponent { + path: path.join("proton"), + } } pub fn init_proton(&self) -> Result { diff --git a/babylonia-terminal-sdk/src/game_manager.rs b/babylonia-terminal-sdk/src/game_manager.rs index c8ac441..d25cd4d 100644 --- a/babylonia-terminal-sdk/src/game_manager.rs +++ b/babylonia-terminal-sdk/src/game_manager.rs @@ -35,19 +35,12 @@ impl GameManager { where P: Reporter + 'static, { - let wine_component = ProtonComponent::new(config_dir.join("wine")); + let wine_component = ProtonComponent::new(config_dir); wine_component.install(progress).await?; - let mut config = GameState::get_config().await?; + let mut config = GameState::get_config().await; config.is_wine_installed = true; - config.wine_path = Some( - GameState::get_config_directory() - .join("wine") - .to_str() - .unwrap() - .to_string(), - ); GameState::save_config(config).await?; Ok(wine_component) @@ -61,10 +54,10 @@ impl GameManager { where P: Reporter + 'static, { - let dxvk_component = DXVKComponent::from_wine(proton.wine(), config_dir.join("dxvk")); + let dxvk_component = DXVKComponent::from_wine(proton.wine(), config_dir); dxvk_component.install(progress).await?; - let mut config = GameState::get_config().await?; + let mut config = GameState::get_config().await; config.is_dxvk_installed = true; GameState::save_config(config).await?; @@ -113,7 +106,7 @@ impl GameManager { wine_with_proton_prefix.install_font(Font::Webdings)?; progress.progress(10); - let mut config = GameState::get_config().await?; + let mut config = GameState::get_config().await; config.is_font_installed = true; GameState::save_config(config).await?; @@ -130,7 +123,7 @@ impl GameManager { //winetricks.install("corefonts")?; winetricks.install("vcrun2022")?; - let mut config = GameState::get_config().await?; + let mut config = GameState::get_config().await; config.is_dependecies_installed = true; GameState::save_config(config).await?; @@ -146,7 +139,7 @@ impl GameManager { let game_component = GameComponent::new(game_dir); game_component.install(Some(progress)).await?; - let mut config = GameState::get_config().await?; + let mut config = GameState::get_config().await; config.is_game_installed = true; GameState::save_config(config).await?; diff --git a/babylonia-terminal-sdk/src/game_state.rs b/babylonia-terminal-sdk/src/game_state.rs index 6b34fe7..c8d8aaf 100644 --- a/babylonia-terminal-sdk/src/game_state.rs +++ b/babylonia-terminal-sdk/src/game_state.rs @@ -1,3 +1,4 @@ +use dirs::home_dir; use serde::{Deserialize, Serialize}; use std::{ io::{Read, Write}, @@ -7,40 +8,42 @@ use tokio::{ fs::{read_to_string, File}, io::{AsyncReadExt, AsyncWriteExt}, }; -//use wincompatlib::prelude::*; #[derive(Debug, PartialEq, Eq)] pub enum GameState { WineNotInstalled, DXVKNotInstalled, FontNotInstalled, - DependecieNotInstalled, // that's just the missing dll to install - LauncherNotInstalled, + DependecieNotInstalled, GameNotInstalled, - InstallingGame, GameInstalled, } #[derive(Debug, Serialize, Deserialize)] pub struct GameConfig { - pub dedicated_wine: bool, + pub config_dir: PathBuf, pub is_wine_installed: bool, - pub wine_path: Option, pub is_dxvk_installed: bool, pub is_font_installed: bool, pub is_dependecies_installed: bool, - pub is_game_installed: bool, // will be remove, just for test purpose + pub game_dir: Option, + pub is_game_installed: bool, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct GameConfigPath { + path: PathBuf, } impl Default for GameConfig { fn default() -> Self { GameConfig { - dedicated_wine: true, + config_dir: dirs::home_dir().unwrap().join(".babylonia-terminal"), is_wine_installed: false, - wine_path: None, is_dxvk_installed: false, is_font_installed: false, is_dependecies_installed: false, + game_dir: None, is_game_installed: false, } } @@ -48,13 +51,24 @@ impl Default for GameConfig { impl GameState { pub fn get_config_directory() -> PathBuf { - dirs::home_dir().unwrap().join(".babylonia-terminal") + home_dir().unwrap().join(".babylonia-terminal") // I will try to change that to a dynamic one if people want to change the config dir } fn get_config_file_path() -> PathBuf { GameState::get_config_directory().join("babylonia-terminal-config") } + pub async fn set_game_dir(path: Option) -> anyhow::Result<()> { + let mut config = GameState::get_config().await; + config.game_dir = path; + GameState::save_config(config).await?; + Ok(()) + } + + pub async fn get_game_dir() -> Option { + GameState::get_config().await.game_dir + } + async fn try_get_config_file() -> anyhow::Result { let _ = tokio::fs::create_dir(GameState::get_config_directory()).await; @@ -69,20 +83,19 @@ impl GameState { Ok(()) } - pub async fn get_config() -> anyhow::Result { + pub async fn get_config() -> GameConfig { let content = match read_to_string(GameState::get_config_file_path()).await { - Err(_) => return Ok(GameConfig::default()), + Err(_) => return GameConfig::default(), Ok(c) => c, }; - if let Ok(config) = serde_json::from_str::(&content) { - Ok(config) - } else { - Ok(GameConfig::default()) + match serde_json::from_str::(&content) { + Ok(config) => return config, + Err(_) => return GameConfig::default(), } } pub async fn get_current_state() -> Self { - let config = GameState::get_config().await.unwrap(); + let config = GameState::get_config().await; if !config.is_wine_installed { return GameState::WineNotInstalled; diff --git a/babylonia-terminal-sdk/src/lib.rs b/babylonia-terminal-sdk/src/lib.rs index adbf4ee..770fe8d 100644 --- a/babylonia-terminal-sdk/src/lib.rs +++ b/babylonia-terminal-sdk/src/lib.rs @@ -1,4 +1,5 @@ pub mod components; pub mod game_manager; +pub mod game_patcher; pub mod game_state; pub mod utils;