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::{
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),

View File

@ -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"),
}
}
}

View File

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

View File

@ -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<wincompatlib::prelude::Proton, String> {

View File

@ -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?;

View File

@ -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<String>,
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<PathBuf>,
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<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> {
let _ = tokio::fs::create_dir(GameState::get_config_directory()).await;
@ -69,20 +83,19 @@ impl GameState {
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 {
Err(_) => return Ok(GameConfig::default()),
Err(_) => return GameConfig::default(),
Ok(c) => c,
};
if let Ok(config) = serde_json::from_str::<GameConfig>(&content) {
Ok(config)
} else {
Ok(GameConfig::default())
match serde_json::from_str::<GameConfig>(&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;

View File

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