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