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 babylonia_terminal_sdk::{
components::proton_component::ProtonComponent, game_manager::GameManager, game_patcher,
game_state::GameState,
components::proton_component::ProtonComponent, game_manager::GameManager, game_state::GameState,
};
use log::{debug, info, LevelFilter};
use simple_logger::SimpleLogger;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader};
use tokio::io::{AsyncBufReadExt, BufReader};
use wincompatlib::prelude::*;
pub mod reporter;
@ -35,8 +34,8 @@ async fn main() {
loop {
let state = GameState::get_current_state().await;
if state != GameState::WineNotInstalled && proton == None {
let proton_component = ProtonComponent::new(GameState::get_config_directory());
if state != GameState::ProtonNotInstalled && proton == None {
let proton_component = ProtonComponent::new(GameState::get_config_directory().await);
match proton_component.init_proton() {
Ok(p) => proton = Some(p),
Err(err) => panic!("{}", err),
@ -44,24 +43,24 @@ async fn main() {
}
match state {
GameState::WineNotInstalled => {
info!("Wine not installed, installing it...");
GameState::ProtonNotInstalled => {
info!("Proton not installed, installing it...");
proton_component = Some(
GameManager::install_wine(
GameState::get_config_directory(),
GameState::get_config_directory().await,
Some(DownloadReporter::create(false)),
)
.await
.expect("Failed to install Wine"),
);
info!("Wine installed");
info!("Proton installed");
}
GameState::DXVKNotInstalled => {
info!("DXVK not installed, installing it...");
debug!("{:?}", proton_component);
GameManager::install_dxvk(
&proton.clone().unwrap(),
GameState::get_config_directory(),
GameState::get_config_directory().await,
Some(DownloadReporter::create(false)),
)
.await
@ -87,7 +86,7 @@ async fn main() {
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(),
GameState::get_config_directory().await.to_str().unwrap(),
);
info!("Please enter your wanted game directory : ");
let mut input = BufReader::new(tokio::io::stdin())
@ -99,12 +98,12 @@ async fn main() {
let dir;
if let Some(i) = &mut input {
if i.is_empty() {
dir = GameState::get_config_directory();
dir = GameState::get_config_directory().await;
} 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();
dir = GameState::get_config_directory().await;
}
GameState::set_game_dir(Some(dir)).await.expect(

View File

@ -5,13 +5,13 @@ use std::{
path::PathBuf,
};
use tokio::{
fs::{read_to_string, File},
fs::{create_dir_all, read_to_string, File},
io::{AsyncReadExt, AsyncWriteExt},
};
#[derive(Debug, PartialEq, Eq)]
pub enum GameState {
WineNotInstalled,
ProtonNotInstalled,
DXVKNotInstalled,
FontNotInstalled,
DependecieNotInstalled,
@ -53,12 +53,18 @@ impl Default for GameConfig {
}
impl GameState {
pub 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
pub async fn get_config_directory() -> PathBuf {
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 {
GameState::get_config_directory().join("babylonia-terminal-config")
async fn get_config_file_path() -> PathBuf {
GameState::get_config_directory()
.await
.join("babylonia-terminal-config")
}
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> {
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<()> {
@ -87,7 +93,7 @@ impl GameState {
}
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(),
Ok(c) => c,
};
@ -101,7 +107,7 @@ impl GameState {
let config = GameState::get_config().await;
if !config.is_wine_installed {
return GameState::WineNotInstalled;
return GameState::ProtonNotInstalled;
}
if !config.is_dxvk_installed {