refactor and add some features to the config file

This commit is contained in:
ALEZ-DEV 2024-05-03 23:03:52 +02:00
parent 19e4088b1d
commit 52a6b921a9
7 changed files with 80 additions and 37 deletions

View File

@ -1,8 +1,12 @@
use std::{path::PathBuf, str::FromStr};
use babylonia_terminal_sdk::{ 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 log::{debug, info, LevelFilter};
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader};
use wincompatlib::prelude::*; use wincompatlib::prelude::*;
pub mod reporter; pub mod reporter;
@ -32,8 +36,7 @@ async fn main() {
let state = GameState::get_current_state().await; let state = GameState::get_current_state().await;
if state != GameState::WineNotInstalled && proton == None { if state != GameState::WineNotInstalled && proton == None {
let proton_component = let proton_component = ProtonComponent::new(GameState::get_config_directory());
ProtonComponent::new(GameState::get_config_directory().join("wine"));
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),
@ -84,6 +87,34 @@ async fn main() {
} }
GameState::GameNotInstalled => { GameState::GameNotInstalled => {
info!("Game not installed, installing it..."); 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( GameManager::install_game(
GameState::get_config_directory().join("PGR"), GameState::get_config_directory().join("PGR"),
DownloadReporter::create(false), DownloadReporter::create(false),

View File

@ -23,7 +23,10 @@ pub struct DXVKComponent<'a> {
impl<'a> DXVKComponent<'a> { impl<'a> DXVKComponent<'a> {
pub fn from_wine<'b: 'a>(wine: &'b Wine, path: PathBuf) -> Self { pub fn from_wine<'b: 'a>(wine: &'b Wine, path: PathBuf) -> Self {
DXVKComponent { wine, path } DXVKComponent {
wine,
path: path.join("dxvk"),
}
} }
} }

View File

@ -39,7 +39,7 @@ impl GameComponent {
game_dir: &std::path::PathBuf, game_dir: &std::path::PathBuf,
resources: &Resources, resources: &Resources,
) -> anyhow::Result<Vec<Resource>> { ) -> anyhow::Result<Vec<Resource>> {
info!("checking all files..."); info!("checking all files, this can take a while...");
let mut to_download: Vec<Resource> = vec![]; let mut to_download: Vec<Resource> = vec![];
for r in &resources.resource { for r in &resources.resource {

View File

@ -91,7 +91,9 @@ impl ComponentDownloader for ProtonComponent {
impl ProtonComponent { impl ProtonComponent {
pub fn new(path: PathBuf) -> Self { pub fn new(path: PathBuf) -> Self {
ProtonComponent { path } ProtonComponent {
path: path.join("proton"),
}
} }
pub fn init_proton(&self) -> Result<wincompatlib::prelude::Proton, String> { pub fn init_proton(&self) -> Result<wincompatlib::prelude::Proton, String> {

View File

@ -35,19 +35,12 @@ impl GameManager {
where where
P: Reporter + 'static, P: Reporter + 'static,
{ {
let wine_component = ProtonComponent::new(config_dir.join("wine")); let wine_component = ProtonComponent::new(config_dir);
wine_component.install(progress).await?; 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.is_wine_installed = true;
config.wine_path = Some(
GameState::get_config_directory()
.join("wine")
.to_str()
.unwrap()
.to_string(),
);
GameState::save_config(config).await?; GameState::save_config(config).await?;
Ok(wine_component) Ok(wine_component)
@ -61,10 +54,10 @@ impl GameManager {
where where
P: Reporter + 'static, 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?; dxvk_component.install(progress).await?;
let mut config = GameState::get_config().await?; let mut config = GameState::get_config().await;
config.is_dxvk_installed = true; config.is_dxvk_installed = true;
GameState::save_config(config).await?; GameState::save_config(config).await?;
@ -113,7 +106,7 @@ impl GameManager {
wine_with_proton_prefix.install_font(Font::Webdings)?; wine_with_proton_prefix.install_font(Font::Webdings)?;
progress.progress(10); progress.progress(10);
let mut config = GameState::get_config().await?; let mut config = GameState::get_config().await;
config.is_font_installed = true; config.is_font_installed = true;
GameState::save_config(config).await?; GameState::save_config(config).await?;
@ -130,7 +123,7 @@ impl GameManager {
//winetricks.install("corefonts")?; //winetricks.install("corefonts")?;
winetricks.install("vcrun2022")?; winetricks.install("vcrun2022")?;
let mut config = GameState::get_config().await?; let mut config = GameState::get_config().await;
config.is_dependecies_installed = true; config.is_dependecies_installed = true;
GameState::save_config(config).await?; GameState::save_config(config).await?;
@ -146,7 +139,7 @@ impl GameManager {
let game_component = GameComponent::new(game_dir); let game_component = GameComponent::new(game_dir);
game_component.install(Some(progress)).await?; 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; config.is_game_installed = true;
GameState::save_config(config).await?; GameState::save_config(config).await?;

View File

@ -1,3 +1,4 @@
use dirs::home_dir;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
io::{Read, Write}, io::{Read, Write},
@ -7,40 +8,42 @@ use tokio::{
fs::{read_to_string, File}, fs::{read_to_string, File},
io::{AsyncReadExt, AsyncWriteExt}, io::{AsyncReadExt, AsyncWriteExt},
}; };
//use wincompatlib::prelude::*;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum GameState { pub enum GameState {
WineNotInstalled, WineNotInstalled,
DXVKNotInstalled, DXVKNotInstalled,
FontNotInstalled, FontNotInstalled,
DependecieNotInstalled, // that's just the missing dll to install DependecieNotInstalled,
LauncherNotInstalled,
GameNotInstalled, GameNotInstalled,
InstallingGame,
GameInstalled, GameInstalled,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct GameConfig { pub struct GameConfig {
pub dedicated_wine: bool, pub config_dir: PathBuf,
pub is_wine_installed: bool, pub is_wine_installed: bool,
pub wine_path: Option<String>,
pub is_dxvk_installed: bool, pub is_dxvk_installed: bool,
pub is_font_installed: bool, pub is_font_installed: bool,
pub is_dependecies_installed: bool, pub is_dependecies_installed: bool,
pub is_game_installed: bool, // will be remove, just for test purpose pub game_dir: Option<PathBuf>,
pub is_game_installed: bool,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GameConfigPath {
path: PathBuf,
} }
impl Default for GameConfig { impl Default for GameConfig {
fn default() -> Self { fn default() -> Self {
GameConfig { GameConfig {
dedicated_wine: true, config_dir: dirs::home_dir().unwrap().join(".babylonia-terminal"),
is_wine_installed: false, is_wine_installed: false,
wine_path: None,
is_dxvk_installed: false, is_dxvk_installed: false,
is_font_installed: false, is_font_installed: false,
is_dependecies_installed: false, is_dependecies_installed: false,
game_dir: None,
is_game_installed: false, is_game_installed: false,
} }
} }
@ -48,13 +51,24 @@ impl Default for GameConfig {
impl GameState { impl GameState {
pub fn get_config_directory() -> PathBuf { 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 { fn get_config_file_path() -> PathBuf {
GameState::get_config_directory().join("babylonia-terminal-config") GameState::get_config_directory().join("babylonia-terminal-config")
} }
pub async fn set_game_dir(path: Option<PathBuf>) -> 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<PathBuf> {
GameState::get_config().await.game_dir
}
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;
@ -69,20 +83,19 @@ impl GameState {
Ok(()) Ok(())
} }
pub async fn get_config() -> anyhow::Result<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 {
Err(_) => return Ok(GameConfig::default()), Err(_) => return GameConfig::default(),
Ok(c) => c, Ok(c) => c,
}; };
if let Ok(config) = serde_json::from_str::<GameConfig>(&content) { match serde_json::from_str::<GameConfig>(&content) {
Ok(config) Ok(config) => return config,
} else { Err(_) => return GameConfig::default(),
Ok(GameConfig::default())
} }
} }
pub async fn get_current_state() -> Self { 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 { if !config.is_wine_installed {
return GameState::WineNotInstalled; return GameState::WineNotInstalled;

View File

@ -1,4 +1,5 @@
pub mod components; pub mod components;
pub mod game_manager; pub mod game_manager;
pub mod game_patcher;
pub mod game_state; pub mod game_state;
pub mod utils; pub mod utils;