mirror of
https://github.com/ALEZ-DEV/Babylonia-terminal.git
synced 2025-12-15 17:08:51 +00:00
some refactor
This commit is contained in:
parent
245e042560
commit
5770878390
@ -6,7 +6,7 @@ use babylonia_terminal_sdk::{
|
||||
proton_component::{ProtonComponent, PROTON_DEV, PROTON_REPO},
|
||||
},
|
||||
game_manager::GameManager,
|
||||
game_state::GameState,
|
||||
game_state::{GameConfig, GameState},
|
||||
};
|
||||
use clap::Parser;
|
||||
use log::{debug, info, LevelFilter};
|
||||
@ -52,7 +52,7 @@ async fn main() {
|
||||
let state = state_result.unwrap();
|
||||
|
||||
if state != GameState::ProtonNotInstalled && proton == None {
|
||||
let proton_component = ProtonComponent::new(GameState::get_config_directory().await);
|
||||
let proton_component = ProtonComponent::new(GameConfig::get_config_directory().await);
|
||||
match proton_component.init_proton() {
|
||||
Ok(p) => proton = Some(p),
|
||||
Err(err) => panic!("{}", err),
|
||||
@ -77,7 +77,7 @@ async fn main() {
|
||||
info!("Proton not installed, installing it...");
|
||||
proton_component = Some(
|
||||
GameManager::install_wine(
|
||||
GameState::get_config_directory().await,
|
||||
GameConfig::get_config_directory().await,
|
||||
release,
|
||||
Some(DownloadReporter::create(false)),
|
||||
)
|
||||
@ -106,7 +106,7 @@ async fn main() {
|
||||
debug!("{:?}", proton_component);
|
||||
GameManager::install_dxvk(
|
||||
&proton.clone().unwrap(),
|
||||
GameState::get_config_directory().await,
|
||||
GameConfig::get_config_directory().await,
|
||||
release,
|
||||
Some(DownloadReporter::create(false)),
|
||||
)
|
||||
@ -130,10 +130,10 @@ async fn main() {
|
||||
}
|
||||
GameState::GameNotInstalled => {
|
||||
info!("Game not installed, installing it...");
|
||||
if GameState::get_game_dir().await.is_none() {
|
||||
if GameConfig::get_game_dir().await.is_none() {
|
||||
info!(
|
||||
"You can choose where to put your game directory, (default '{}')",
|
||||
GameState::get_config_directory().await.to_str().unwrap(),
|
||||
GameConfig::get_config_directory().await.to_str().unwrap(),
|
||||
);
|
||||
info!("Please enter your wanted game directory : ");
|
||||
let mut input = BufReader::new(tokio::io::stdin())
|
||||
@ -145,21 +145,21 @@ async fn main() {
|
||||
let dir;
|
||||
if let Some(i) = &mut input {
|
||||
if i.is_empty() {
|
||||
dir = GameState::get_config_directory().await;
|
||||
dir = GameConfig::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().await;
|
||||
dir = GameConfig::get_config_directory().await;
|
||||
}
|
||||
|
||||
GameState::set_game_dir(Some(dir)).await.expect(
|
||||
GameConfig::set_game_dir(Some(dir)).await.expect(
|
||||
"Failed to save the game directory into the config file, please retry!",
|
||||
);
|
||||
}
|
||||
|
||||
GameManager::install_game(
|
||||
GameState::get_game_dir().await.unwrap(),
|
||||
GameConfig::get_game_dir().await.unwrap(),
|
||||
DownloadReporter::create(false),
|
||||
)
|
||||
.await
|
||||
@ -174,7 +174,7 @@ async fn main() {
|
||||
}
|
||||
GameState::GameNotPatched => {
|
||||
info!("Patching game...");
|
||||
GameManager::patch_game(GameState::get_game_dir().await.unwrap())
|
||||
GameManager::patch_game(GameConfig::get_game_dir().await.unwrap())
|
||||
.await
|
||||
.expect("Failed to patch the game");
|
||||
info!("Game patched!");
|
||||
@ -189,7 +189,7 @@ async fn main() {
|
||||
debug!("{:?}", proton);
|
||||
GameManager::start_game(
|
||||
&proton.unwrap(),
|
||||
GameState::get_game_dir()
|
||||
GameConfig::get_game_dir()
|
||||
.await
|
||||
.expect("Failed to start game, the game directory was not found"),
|
||||
args.options,
|
||||
|
||||
@ -11,7 +11,7 @@ use crate::{
|
||||
game_component::GameComponent, proton_component::ProtonComponent,
|
||||
},
|
||||
game_patcher,
|
||||
game_state::GameState,
|
||||
game_state::{GameConfig, GameState},
|
||||
utils::{get_game_name, get_game_name_with_executable, github_requester::GithubRequester},
|
||||
};
|
||||
|
||||
@ -31,9 +31,9 @@ impl GameManager {
|
||||
|
||||
wine_component.install(progress).await?;
|
||||
|
||||
let mut config = GameState::get_config().await;
|
||||
let mut config = GameConfig::get_config().await;
|
||||
config.is_wine_installed = true;
|
||||
GameState::save_config(config).await?;
|
||||
GameConfig::save_config(config).await?;
|
||||
|
||||
Ok(wine_component)
|
||||
}
|
||||
@ -52,9 +52,9 @@ impl GameManager {
|
||||
|
||||
dxvk_component.install(progress).await?;
|
||||
|
||||
let mut config = GameState::get_config().await;
|
||||
let mut config = GameConfig::get_config().await;
|
||||
config.is_dxvk_installed = true;
|
||||
GameState::save_config(config).await?;
|
||||
GameConfig::save_config(config).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -103,9 +103,9 @@ impl GameManager {
|
||||
wine_with_proton_prefix.install_font(Font::Webdings)?;
|
||||
notify_fonts_progress(10, &progress);
|
||||
|
||||
let mut config = GameState::get_config().await;
|
||||
let mut config = GameConfig::get_config().await;
|
||||
config.is_font_installed = true;
|
||||
GameState::save_config(config).await?;
|
||||
GameConfig::save_config(config).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -124,9 +124,9 @@ impl GameManager {
|
||||
.wait()
|
||||
.expect("Something failed when waiting for the installation");
|
||||
|
||||
let mut config = GameState::get_config().await;
|
||||
let mut config = GameConfig::get_config().await;
|
||||
config.is_dependecies_installed = true;
|
||||
GameState::save_config(config).await?;
|
||||
GameConfig::save_config(config).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -140,9 +140,9 @@ 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 = GameConfig::get_config().await;
|
||||
config.is_game_installed = true;
|
||||
GameState::save_config(config).await?;
|
||||
GameConfig::save_config(config).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -150,10 +150,10 @@ impl GameManager {
|
||||
// this function just pass is_game_installed and is_game_patched to false,
|
||||
// so the launcher on the next iteration download the new file and delete the old one with the check process in the installation process
|
||||
pub async fn update_game() -> anyhow::Result<()> {
|
||||
let mut config = GameState::get_config().await;
|
||||
let mut config = GameConfig::get_config().await;
|
||||
config.is_game_installed = false;
|
||||
config.is_game_patched = false;
|
||||
GameState::save_config(config).await?;
|
||||
GameConfig::save_config(config).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -182,7 +182,7 @@ impl GameManager {
|
||||
Command::new(tokens.get(0).unwrap())
|
||||
.args(&tokens[0..(index - 1)])
|
||||
.arg(proton.python.as_os_str())
|
||||
.arg(GameState::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()])
|
||||
|
||||
@ -8,7 +8,7 @@ use tokio::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
game_state::GameState,
|
||||
game_state::{GameConfig, GameState},
|
||||
utils::{get_game_name, get_game_name_with_executable},
|
||||
};
|
||||
|
||||
@ -82,9 +82,9 @@ pub async fn patch_game(game_dir: PathBuf) -> anyhow::Result<()> {
|
||||
),
|
||||
}
|
||||
|
||||
let mut config = GameState::get_config().await;
|
||||
let mut config = GameConfig::get_config().await;
|
||||
config.is_game_patched = true;
|
||||
GameState::save_config(config).await?;
|
||||
GameConfig::save_config(config).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -32,6 +32,58 @@ pub struct GameConfig {
|
||||
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,
|
||||
@ -53,58 +105,8 @@ impl Default for GameConfig {
|
||||
}
|
||||
|
||||
impl GameState {
|
||||
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 {
|
||||
GameState::get_config_directory()
|
||||
.await
|
||||
.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).await;
|
||||
|
||||
Ok(tokio::fs::File::create(GameState::get_config_file_path().await).await?)
|
||||
}
|
||||
|
||||
pub async fn save_config(config: GameConfig) -> anyhow::Result<()> {
|
||||
let mut file = GameState::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() -> GameConfig {
|
||||
let content = match read_to_string(GameState::get_config_file_path().await).await {
|
||||
Err(_) => return GameConfig::default(),
|
||||
Ok(c) => c,
|
||||
};
|
||||
match serde_json::from_str::<GameConfig>(&content) {
|
||||
Ok(config) => return config,
|
||||
Err(_) => return GameConfig::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_current_state() -> anyhow::Result<Self> {
|
||||
let config = GameState::get_config().await;
|
||||
let config = GameConfig::get_config().await;
|
||||
|
||||
if !config.is_wine_installed {
|
||||
return Ok(GameState::ProtonNotInstalled);
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
pub mod components;
|
||||
pub mod game_config;
|
||||
pub mod game_manager;
|
||||
pub mod game_patcher;
|
||||
pub mod game_state;
|
||||
|
||||
@ -5,6 +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;
|
||||
|
||||
// start data ---------------------------------------------------------------------
|
||||
@ -157,13 +158,13 @@ impl GameInfo {
|
||||
}
|
||||
|
||||
async fn get_cache_file_path() -> PathBuf {
|
||||
GameState::get_config_directory()
|
||||
GameConfig::get_config_directory()
|
||||
.await
|
||||
.join("version-cache")
|
||||
}
|
||||
|
||||
async fn save_in_cache(&self) -> anyhow::Result<()> {
|
||||
let _ = tokio::fs::create_dir(GameState::get_config_directory().await).await;
|
||||
let _ = tokio::fs::create_dir(GameConfig::get_config_directory().await).await;
|
||||
let mut file = tokio::fs::File::create(GameInfo::get_cache_file_path().await).await?;
|
||||
|
||||
let content = serde_json::to_string(self)?;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user