feat: Settings manager
This commit is contained in:
parent
fbab841bbd
commit
e12045697e
4 changed files with 94 additions and 26 deletions
|
|
@ -7,6 +7,24 @@ pub mod error;
|
||||||
pub mod game;
|
pub mod game;
|
||||||
pub mod logger;
|
pub mod logger;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
pub mod settings;
|
||||||
|
|
||||||
pub use error::*;
|
pub use error::*;
|
||||||
pub use logger::start_logger;
|
pub use logger::start_logger;
|
||||||
|
|
||||||
|
pub struct AppState {
|
||||||
|
pub settings: settings::SettingsManager,
|
||||||
|
pub clients: game::client_manager::ClientManager,
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl Send for AppState {}
|
||||||
|
unsafe impl Sync for AppState {}
|
||||||
|
|
||||||
|
impl AppState {
|
||||||
|
pub fn new() -> crate::Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
settings: settings::SettingsManager::new()?,
|
||||||
|
clients: game::client_manager::ClientManager::new()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
61
onelauncher/src/settings.rs
Normal file
61
onelauncher/src/settings.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::utils::dirs;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
#[serde(default)]
|
||||||
|
pub struct Setings {
|
||||||
|
pub discord_rpc: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Setings {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
discord_rpc: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SettingsManager {
|
||||||
|
pub settings: Setings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SettingsManager {
|
||||||
|
pub fn new() -> crate::Result<Self> {
|
||||||
|
let path = dirs::app_config_dir()?.join("settings.json");
|
||||||
|
|
||||||
|
if path.exists() {
|
||||||
|
let file = fs::read_to_string(path)?;
|
||||||
|
let settings: Setings = serde_json::from_str(&file)?;
|
||||||
|
|
||||||
|
return Ok(Self {
|
||||||
|
settings
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let this = Self {
|
||||||
|
settings: Setings::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
this.save()?;
|
||||||
|
Ok(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_settings(&self) -> &Setings {
|
||||||
|
&self.settings
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_settings_mut(&mut self) -> &mut Setings {
|
||||||
|
&mut self.settings
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&self) -> crate::Result<()> {
|
||||||
|
let path = dirs::app_config_dir()?.join("settings.json");
|
||||||
|
fs::create_dir_all(path.parent().unwrap())?;
|
||||||
|
fs::write(path, serde_json::to_string(&self.settings)?)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,20 +1,18 @@
|
||||||
use onelauncher::game::{client::{Cluster, LaunchCallbacks, Manifest}, clients::ClientType};
|
use onelauncher::{game::{client::{Cluster, LaunchCallbacks, Manifest}, clients::ClientType}, AppState};
|
||||||
use tauri::{Manager, State};
|
use tauri::{Manager, State};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::GameManagerState;
|
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn create_cluster(
|
pub async fn create_cluster(
|
||||||
state: State<'_, Mutex<GameManagerState>>,
|
state: State<'_, Mutex<AppState>>,
|
||||||
name: String,
|
name: String,
|
||||||
version: String,
|
version: String,
|
||||||
client: ClientType,
|
client: ClientType,
|
||||||
cover: Option<String>,
|
cover: Option<String>,
|
||||||
group: Option<Uuid>,
|
group: Option<Uuid>,
|
||||||
) -> Result<Uuid, String> {
|
) -> Result<Uuid, String> {
|
||||||
let manager = &mut state.lock().await.client_manager;
|
let manager = &mut state.lock().await.clients;
|
||||||
let uuid = manager
|
let uuid = manager
|
||||||
.create_cluster(name, version, cover, group, client)
|
.create_cluster(name, version, cover, group, client)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
@ -23,18 +21,18 @@ pub async fn create_cluster(
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_clusters(
|
pub async fn get_clusters(
|
||||||
state: State<'_, Mutex<GameManagerState>>,
|
state: State<'_, Mutex<AppState>>,
|
||||||
) -> Result<Vec<Cluster>, String> {
|
) -> Result<Vec<Cluster>, String> {
|
||||||
let manager = &mut state.lock().await.client_manager;
|
let manager = &mut state.lock().await.clients;
|
||||||
Ok(manager.get_clusters_owned())
|
Ok(manager.get_clusters_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_cluster(
|
pub async fn get_cluster(
|
||||||
state: State<'_, Mutex<GameManagerState>>,
|
state: State<'_, Mutex<AppState>>,
|
||||||
uuid: Uuid,
|
uuid: Uuid,
|
||||||
) -> Result<Cluster, String> {
|
) -> Result<Cluster, String> {
|
||||||
let manager = &mut state.lock().await.client_manager;
|
let manager = &mut state.lock().await.clients;
|
||||||
let instance = manager.get_cluster(uuid)?;
|
let instance = manager.get_cluster(uuid)?;
|
||||||
Ok(instance.clone())
|
Ok(instance.clone())
|
||||||
}
|
}
|
||||||
|
|
@ -42,10 +40,10 @@ pub async fn get_cluster(
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn launch_cluster(
|
pub async fn launch_cluster(
|
||||||
app: tauri::AppHandle,
|
app: tauri::AppHandle,
|
||||||
state: State<'_, Mutex<GameManagerState>>,
|
state: State<'_, Mutex<AppState>>,
|
||||||
uuid: Uuid,
|
uuid: Uuid,
|
||||||
) -> Result<i32, String> {
|
) -> Result<i32, String> {
|
||||||
let manager = &mut state.lock().await.client_manager;
|
let manager = &mut state.lock().await.clients;
|
||||||
|
|
||||||
let on_launch_app = app.clone();
|
let on_launch_app = app.clone();
|
||||||
let on_stdout_app = app.clone();
|
let on_stdout_app = app.clone();
|
||||||
|
|
@ -71,10 +69,10 @@ pub async fn launch_cluster(
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_manifest(
|
pub async fn get_manifest(
|
||||||
state: State<'_, Mutex<GameManagerState>>,
|
state: State<'_, Mutex<AppState>>,
|
||||||
uuid: Uuid,
|
uuid: Uuid,
|
||||||
) -> Result<Manifest, String> {
|
) -> Result<Manifest, String> {
|
||||||
let manager = &mut state.lock().await.client_manager;
|
let manager = &mut state.lock().await.clients;
|
||||||
let manifest = manager.get_manifest(uuid)?;
|
let manifest = manager.get_manifest(uuid)?;
|
||||||
Ok(manifest.clone())
|
Ok(manifest.clone())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,14 @@
|
||||||
use onelauncher::game::client_manager::ClientManager;
|
use onelauncher::{game::client_manager::ClientManager, AppState};
|
||||||
use tauri::{plugin::TauriPlugin, Manager, State};
|
use tauri::{plugin::TauriPlugin, Manager, State};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
mod auth;
|
mod auth;
|
||||||
mod game;
|
mod game;
|
||||||
|
|
||||||
pub struct GameManagerState {
|
|
||||||
pub client_manager: ClientManager,
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe impl Send for GameManagerState {}
|
|
||||||
unsafe impl Sync for GameManagerState {}
|
|
||||||
|
|
||||||
pub fn init() -> TauriPlugin<tauri::Wry> {
|
pub fn init() -> TauriPlugin<tauri::Wry> {
|
||||||
tauri::plugin::Builder::new("onelauncher")
|
tauri::plugin::Builder::new("onelauncher")
|
||||||
.setup(|app, _| {
|
.setup(|app, _| {
|
||||||
app.manage(Mutex::new(GameManagerState {
|
app.manage(Mutex::new(AppState::new()?));
|
||||||
client_manager: ClientManager::new().expect("Failed to initialize client manager"),
|
|
||||||
}));
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
|
@ -35,8 +26,8 @@ pub fn init() -> TauriPlugin<tauri::Wry> {
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
async fn refresh_client_manager(state: State<'_, Mutex<GameManagerState>>) -> Result<(), String> {
|
async fn refresh_client_manager(state: State<'_, Mutex<AppState>>) -> Result<(), String> {
|
||||||
let mut state = state.lock().await;
|
let mut state = state.lock().await;
|
||||||
state.client_manager = ClientManager::new()?;
|
state.clients = ClientManager::new()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue