move all config part from game_state.rs to game_config.rs

This commit is contained in:
ALEZ-DEV 2024-07-17 22:15:03 +02:00
parent 5770878390
commit dcf90310e3
5 changed files with 117 additions and 109 deletions

View File

@ -0,0 +1,92 @@
use std::path::PathBuf;
use dirs::home_dir;
use serde::{Deserialize, Serialize};
use tokio::{
fs::{create_dir_all, read_to_string, File},
io::AsyncWriteExt,
};
#[derive(Debug, Serialize, Deserialize)]
pub struct GameConfig {
pub config_dir: PathBuf,
pub is_wine_installed: bool,
pub is_dxvk_installed: bool,
pub is_font_installed: bool,
pub is_dependecies_installed: bool,
pub game_dir: Option<PathBuf>,
pub is_game_installed: bool,
pub is_game_patched: bool,
}
impl GameConfig {
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
}
async fn get_config_file_path() -> PathBuf {
Self::get_config_directory()
.await
.join("babylonia-terminal-config")
}
pub async fn set_game_dir(path: Option<PathBuf>) -> anyhow::Result<()> {
let mut config = Self::get_config().await;
config.game_dir = path;
Self::save_config(config).await?;
Ok(())
}
pub async fn get_game_dir() -> Option<PathBuf> {
Self::get_config().await.game_dir
}
async fn try_get_config_file() -> anyhow::Result<File> {
let _ = tokio::fs::create_dir(Self::get_config_directory().await).await;
Ok(tokio::fs::File::create(Self::get_config_file_path().await).await?)
}
pub async fn save_config(config: Self) -> anyhow::Result<()> {
let mut file = Self::try_get_config_file().await?;
let content = serde_json::to_string(&config)?;
file.write_all(content.as_bytes()).await?;
Ok(())
}
pub async fn get_config() -> Self {
let content = match read_to_string(Self::get_config_file_path().await).await {
Err(_) => return Self::default(),
Ok(c) => c,
};
match serde_json::from_str::<Self>(&content) {
Ok(config) => return config,
Err(_) => return Self::default(),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GameConfigPath {
path: PathBuf,
}
impl Default for GameConfig {
fn default() -> Self {
GameConfig {
config_dir: dirs::home_dir().unwrap().join(".babylonia-terminal"),
is_wine_installed: false,
is_dxvk_installed: false,
is_font_installed: false,
is_dependecies_installed: false,
game_dir: None,
is_game_installed: false,
is_game_patched: false,
}
}
}

View File

@ -1,4 +1,9 @@
use std::{path::PathBuf, sync::Arc, process::{Command, Stdio}, io::{BufReader, BufRead}};
use std::{
io::{BufRead, BufReader},
path::PathBuf,
process::{Command, Stdio},
sync::Arc,
};
use downloader::progress::Reporter;
use log::{debug, info};
@ -10,8 +15,8 @@ use crate::{
component_downloader::ComponentDownloader, dxvk_component::DXVKComponent,
game_component::GameComponent, proton_component::ProtonComponent,
},
game_config::GameConfig,
game_patcher,
game_state::{GameConfig, GameState},
utils::{get_game_name, get_game_name_with_executable, github_requester::GithubRequester},
};
@ -167,22 +172,30 @@ impl GameManager {
pub async fn start_game(proton: &Proton, game_dir: PathBuf, options: Option<String>) {
let proton_version = proton.wine().version().unwrap();
let exec_path = game_dir
.join(get_game_name())
.join(get_game_name_with_executable());
.join(get_game_name())
.join(get_game_name_with_executable());
debug!("Wine version : {:?}", proton_version);
let mut child = if let Some(custom_command) = options {
debug!("Starting game with --options -> {}", custom_command);
let tokens: Vec<&str> = custom_command.split_whitespace().collect();
// position of the %command%
let index = tokens.iter().position(|&s| s == "%command%").expect("You forget to put %command% in your custom launch command");
let index = tokens
.iter()
.position(|&s| s == "%command%")
.expect("You forget to put %command% in your custom launch command");
Command::new(tokens.get(0).unwrap())
.args(&tokens[0..(index - 1)])
.arg(proton.python.as_os_str())
.arg(GameConfig::get_config_directory().await.join("proton").join("proton"))
.arg(
GameConfig::get_config_directory()
.await
.join("proton")
.join("proton"),
)
.arg("run")
.arg(exec_path)
.args(&tokens[(index + 1)..tokens.len()])
@ -194,13 +207,9 @@ impl GameManager {
.unwrap()
} else {
debug!("Starting game without --options");
proton
.run(
exec_path,
)
.unwrap()
proton.run(exec_path).unwrap()
};
let stdout = child.stdout.take().unwrap();
let mut bufread = BufReader::new(stdout);
let mut buf = String::new();

View File

@ -8,7 +8,7 @@ use tokio::{
};
use crate::{
game_state::{GameConfig, GameState},
game_config::GameConfig,
utils::{get_game_name, get_game_name_with_executable},
};

View File

@ -1,12 +1,4 @@
use dirs::home_dir;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tokio::{
fs::{create_dir_all, read_to_string, File},
io::AsyncWriteExt,
};
use crate::utils::kuro_prod_api::GameInfo;
use crate::{game_config::GameConfig, utils::kuro_prod_api::GameInfo};
#[derive(Debug, PartialEq, Eq)]
pub enum GameState {
@ -20,90 +12,6 @@ pub enum GameState {
GameInstalled,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GameConfig {
pub config_dir: PathBuf,
pub is_wine_installed: bool,
pub is_dxvk_installed: bool,
pub is_font_installed: bool,
pub is_dependecies_installed: bool,
pub game_dir: Option<PathBuf>,
pub is_game_installed: bool,
pub is_game_patched: bool,
}
impl GameConfig {
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
}
async fn get_config_file_path() -> PathBuf {
Self::get_config_directory()
.await
.join("babylonia-terminal-config")
}
pub async fn set_game_dir(path: Option<PathBuf>) -> anyhow::Result<()> {
let mut config = Self::get_config().await;
config.game_dir = path;
Self::save_config(config).await?;
Ok(())
}
pub async fn get_game_dir() -> Option<PathBuf> {
Self::get_config().await.game_dir
}
async fn try_get_config_file() -> anyhow::Result<File> {
let _ = tokio::fs::create_dir(Self::get_config_directory().await).await;
Ok(tokio::fs::File::create(Self::get_config_file_path().await).await?)
}
pub async fn save_config(config: Self) -> anyhow::Result<()> {
let mut file = Self::try_get_config_file().await?;
let content = serde_json::to_string(&config)?;
file.write_all(content.as_bytes()).await?;
Ok(())
}
pub async fn get_config() -> Self {
let content = match read_to_string(Self::get_config_file_path().await).await {
Err(_) => return Self::default(),
Ok(c) => c,
};
match serde_json::from_str::<Self>(&content) {
Ok(config) => return config,
Err(_) => return Self::default(),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GameConfigPath {
path: PathBuf,
}
impl Default for GameConfig {
fn default() -> Self {
GameConfig {
config_dir: dirs::home_dir().unwrap().join(".babylonia-terminal"),
is_wine_installed: false,
is_dxvk_installed: false,
is_font_installed: false,
is_dependecies_installed: false,
game_dir: None,
is_game_installed: false,
is_game_patched: false,
}
}
}
impl GameState {
pub async fn get_current_state() -> anyhow::Result<Self> {
let config = GameConfig::get_config().await;

View File

@ -5,8 +5,7 @@ use serde::Serialize;
use tokio::fs::read_to_string;
use tokio::io::AsyncWriteExt;
use crate::game_state::GameConfig;
use crate::game_state::GameState;
use crate::game_config::GameConfig;
// start data ---------------------------------------------------------------------