little refactor

This commit is contained in:
ALEZ-DEV 2024-05-04 20:46:27 +02:00
parent 2d642f412e
commit aa151b8697
2 changed files with 28 additions and 23 deletions

View File

@ -1,12 +1,11 @@
use std::{path::PathBuf, str::FromStr}; use std::{path::PathBuf, str::FromStr};
use babylonia_terminal_sdk::{ use babylonia_terminal_sdk::{
components::proton_component::ProtonComponent, game_manager::GameManager, game_patcher, components::proton_component::ProtonComponent, game_manager::GameManager, game_state::GameState,
game_state::GameState,
}; };
use log::{debug, info, LevelFilter}; use log::{debug, info, LevelFilter};
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader}; use tokio::io::{AsyncBufReadExt, BufReader};
use wincompatlib::prelude::*; use wincompatlib::prelude::*;
pub mod reporter; pub mod reporter;
@ -35,8 +34,8 @@ async fn main() {
loop { loop {
let state = GameState::get_current_state().await; let state = GameState::get_current_state().await;
if state != GameState::WineNotInstalled && proton == None { if state != GameState::ProtonNotInstalled && proton == None {
let proton_component = ProtonComponent::new(GameState::get_config_directory()); let proton_component = ProtonComponent::new(GameState::get_config_directory().await);
match proton_component.init_proton() { match proton_component.init_proton() {
Ok(p) => proton = Some(p), Ok(p) => proton = Some(p),
Err(err) => panic!("{}", err), Err(err) => panic!("{}", err),
@ -44,24 +43,24 @@ async fn main() {
} }
match state { match state {
GameState::WineNotInstalled => { GameState::ProtonNotInstalled => {
info!("Wine not installed, installing it..."); info!("Proton not installed, installing it...");
proton_component = Some( proton_component = Some(
GameManager::install_wine( GameManager::install_wine(
GameState::get_config_directory(), GameState::get_config_directory().await,
Some(DownloadReporter::create(false)), Some(DownloadReporter::create(false)),
) )
.await .await
.expect("Failed to install Wine"), .expect("Failed to install Wine"),
); );
info!("Wine installed"); info!("Proton installed");
} }
GameState::DXVKNotInstalled => { GameState::DXVKNotInstalled => {
info!("DXVK not installed, installing it..."); info!("DXVK not installed, installing it...");
debug!("{:?}", proton_component); debug!("{:?}", proton_component);
GameManager::install_dxvk( GameManager::install_dxvk(
&proton.clone().unwrap(), &proton.clone().unwrap(),
GameState::get_config_directory(), GameState::get_config_directory().await,
Some(DownloadReporter::create(false)), Some(DownloadReporter::create(false)),
) )
.await .await
@ -87,7 +86,7 @@ async fn main() {
if GameState::get_game_dir().await.is_none() { if GameState::get_game_dir().await.is_none() {
info!( info!(
"You can choose where to put your game directory, (default '{}')", "You can choose where to put your game directory, (default '{}')",
GameState::get_config_directory().to_str().unwrap(), GameState::get_config_directory().await.to_str().unwrap(),
); );
info!("Please enter your wanted game directory : "); info!("Please enter your wanted game directory : ");
let mut input = BufReader::new(tokio::io::stdin()) let mut input = BufReader::new(tokio::io::stdin())
@ -99,12 +98,12 @@ async fn main() {
let dir; let dir;
if let Some(i) = &mut input { if let Some(i) = &mut input {
if i.is_empty() { if i.is_empty() {
dir = GameState::get_config_directory(); dir = GameState::get_config_directory().await;
} else { } else {
dir = PathBuf::from_str(i).expect("This is not a valid directory!\n Please restart the launcher and put a valid path."); dir = PathBuf::from_str(i).expect("This is not a valid directory!\n Please restart the launcher and put a valid path.");
} }
} else { } else {
dir = GameState::get_config_directory(); dir = GameState::get_config_directory().await;
} }
GameState::set_game_dir(Some(dir)).await.expect( GameState::set_game_dir(Some(dir)).await.expect(

View File

@ -5,13 +5,13 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use tokio::{ use tokio::{
fs::{read_to_string, File}, fs::{create_dir_all, read_to_string, File},
io::{AsyncReadExt, AsyncWriteExt}, io::{AsyncReadExt, AsyncWriteExt},
}; };
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum GameState { pub enum GameState {
WineNotInstalled, ProtonNotInstalled,
DXVKNotInstalled, DXVKNotInstalled,
FontNotInstalled, FontNotInstalled,
DependecieNotInstalled, DependecieNotInstalled,
@ -53,12 +53,18 @@ impl Default for GameConfig {
} }
impl GameState { impl GameState {
pub fn get_config_directory() -> PathBuf { pub async fn get_config_directory() -> PathBuf {
home_dir().unwrap().join(".babylonia-terminal") // I will try to change that to a dynamic one if people want to change the config dir let path = home_dir().unwrap().join(".babylonia-terminal"); // I will try to change that to a dynamic one if people want to change the config dir
let _ = create_dir_all(path.clone()).await;
path
} }
fn get_config_file_path() -> PathBuf { async fn get_config_file_path() -> PathBuf {
GameState::get_config_directory().join("babylonia-terminal-config") GameState::get_config_directory()
.await
.join("babylonia-terminal-config")
} }
pub async fn set_game_dir(path: Option<PathBuf>) -> anyhow::Result<()> { pub async fn set_game_dir(path: Option<PathBuf>) -> anyhow::Result<()> {
@ -73,9 +79,9 @@ impl GameState {
} }
async fn try_get_config_file() -> anyhow::Result<File> { async fn try_get_config_file() -> anyhow::Result<File> {
let _ = tokio::fs::create_dir(GameState::get_config_directory()).await; let _ = tokio::fs::create_dir(GameState::get_config_directory().await).await;
Ok(tokio::fs::File::create(GameState::get_config_file_path()).await?) Ok(tokio::fs::File::create(GameState::get_config_file_path().await).await?)
} }
pub async fn save_config(config: GameConfig) -> anyhow::Result<()> { pub async fn save_config(config: GameConfig) -> anyhow::Result<()> {
@ -87,7 +93,7 @@ impl GameState {
} }
pub async fn get_config() -> GameConfig { pub async fn get_config() -> GameConfig {
let content = match read_to_string(GameState::get_config_file_path()).await { let content = match read_to_string(GameState::get_config_file_path().await).await {
Err(_) => return GameConfig::default(), Err(_) => return GameConfig::default(),
Ok(c) => c, Ok(c) => c,
}; };
@ -101,7 +107,7 @@ impl GameState {
let config = GameState::get_config().await; let config = GameState::get_config().await;
if !config.is_wine_installed { if !config.is_wine_installed {
return GameState::WineNotInstalled; return GameState::ProtonNotInstalled;
} }
if !config.is_dxvk_installed { if !config.is_dxvk_installed {