mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 16:51:18 +10:00
chore(process manager): refactor for generic way to implement cross
platform launchers
This commit is contained in:
4
app.vue
4
app.vue
@ -20,10 +20,10 @@ import {
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const state = useAppState();
|
const state = useAppState();
|
||||||
state.value = await invoke("fetch_state");
|
state.value = JSON.parse(await invoke("fetch_state"));
|
||||||
|
|
||||||
router.beforeEach(async () => {
|
router.beforeEach(async () => {
|
||||||
state.value = await invoke("fetch_state");
|
state.value = JSON.parse(await invoke("fetch_state"));
|
||||||
});
|
});
|
||||||
|
|
||||||
setupHooks();
|
setupHooks();
|
||||||
|
|||||||
@ -3,12 +3,12 @@ mod db;
|
|||||||
mod downloads;
|
mod downloads;
|
||||||
mod library;
|
mod library;
|
||||||
|
|
||||||
|
mod cleanup;
|
||||||
mod process;
|
mod process;
|
||||||
mod remote;
|
mod remote;
|
||||||
mod state;
|
mod state;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
mod cleanup;
|
|
||||||
|
|
||||||
use crate::db::DatabaseImpls;
|
use crate::db::DatabaseImpls;
|
||||||
use auth::{auth_initiate, generate_authorization_header, recieve_handshake, retry_connect};
|
use auth::{auth_initiate, generate_authorization_header, recieve_handshake, retry_connect};
|
||||||
@ -65,7 +65,7 @@ pub struct User {
|
|||||||
|
|
||||||
#[derive(Clone, Serialize)]
|
#[derive(Clone, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct AppState {
|
pub struct AppState<'a> {
|
||||||
status: AppStatus,
|
status: AppStatus,
|
||||||
user: Option<User>,
|
user: Option<User>,
|
||||||
games: HashMap<String, Game>,
|
games: HashMap<String, Game>,
|
||||||
@ -73,18 +73,18 @@ pub struct AppState {
|
|||||||
#[serde(skip_serializing)]
|
#[serde(skip_serializing)]
|
||||||
download_manager: Arc<DownloadManager>,
|
download_manager: Arc<DownloadManager>,
|
||||||
#[serde(skip_serializing)]
|
#[serde(skip_serializing)]
|
||||||
process_manager: Arc<Mutex<ProcessManager>>,
|
process_manager: Arc<Mutex<ProcessManager<'a>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
fn fetch_state(state: tauri::State<'_, Mutex<AppState>>) -> Result<AppState, String> {
|
fn fetch_state(state: tauri::State<'_, Mutex<AppState<'_>>>) -> Result<String, String> {
|
||||||
let guard = state.lock().unwrap();
|
let guard = state.lock().unwrap();
|
||||||
let cloned_state = guard.clone();
|
let cloned_state = serde_json::to_string(&guard.clone()).map_err(|e| e.to_string())?;
|
||||||
drop(guard);
|
drop(guard);
|
||||||
Ok(cloned_state)
|
Ok(cloned_state)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(handle: AppHandle) -> AppState {
|
fn setup(handle: AppHandle) -> AppState<'static> {
|
||||||
let logfile = FileAppender::builder()
|
let logfile = FileAppender::builder()
|
||||||
.encoder(Box::new(PatternEncoder::new("{d} | {l} | {f} - {m}{n}")))
|
.encoder(Box::new(PatternEncoder::new("{d} | {l} | {f} - {m}{n}")))
|
||||||
.append(false)
|
.append(false)
|
||||||
@ -137,7 +137,6 @@ fn setup(handle: AppHandle) -> AppState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(DatabaseInterface::set_up_database);
|
pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(DatabaseInterface::set_up_database);
|
||||||
|
|
||||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fs::{File, OpenOptions},
|
fs::{File, OpenOptions},
|
||||||
io::{Stdout, Write},
|
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::{Child, Command},
|
process::{Child, Command},
|
||||||
sync::LazyLock,
|
sync::LazyLock,
|
||||||
@ -15,13 +14,14 @@ use crate::{
|
|||||||
DB,
|
DB,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct ProcessManager {
|
pub struct ProcessManager<'a> {
|
||||||
current_platform: Platform,
|
current_platform: Platform,
|
||||||
log_output_dir: PathBuf,
|
log_output_dir: PathBuf,
|
||||||
processes: HashMap<String, Child>,
|
processes: HashMap<String, Child>,
|
||||||
|
game_launchers: HashMap<(Platform, Platform), &'a (dyn ProcessHandler + Sync + Send + 'static)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProcessManager {
|
impl ProcessManager<'_> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let root_dir_lock = DATA_ROOT_DIR.lock().unwrap();
|
let root_dir_lock = DATA_ROOT_DIR.lock().unwrap();
|
||||||
let log_output_dir = root_dir_lock.join("logs");
|
let log_output_dir = root_dir_lock.join("logs");
|
||||||
@ -36,6 +36,17 @@ impl ProcessManager {
|
|||||||
|
|
||||||
processes: HashMap::new(),
|
processes: HashMap::new(),
|
||||||
log_output_dir,
|
log_output_dir,
|
||||||
|
game_launchers: HashMap::from([
|
||||||
|
// Current platform to target platform
|
||||||
|
(
|
||||||
|
(Platform::Windows, Platform::Windows),
|
||||||
|
&NativeGameLauncher {} as &(dyn ProcessHandler + Sync + Send + 'static),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(Platform::Linux, Platform::Linux),
|
||||||
|
&NativeGameLauncher {} as &(dyn ProcessHandler + Sync + Send + 'static),
|
||||||
|
),
|
||||||
|
]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,11 +66,15 @@ impl ProcessManager {
|
|||||||
|
|
||||||
pub fn valid_platform(&self, platform: &Platform) -> Result<bool, String> {
|
pub fn valid_platform(&self, platform: &Platform) -> Result<bool, String> {
|
||||||
let current = &self.current_platform;
|
let current = &self.current_platform;
|
||||||
let valid_platforms = PROCESS_COMPATABILITY_MATRIX
|
info!("{:?}", self.game_launchers.keys());
|
||||||
.get(current)
|
info!(
|
||||||
.ok_or("Incomplete platform compatability matrix.")?;
|
"{:?} {}",
|
||||||
|
(current.clone(), platform.clone()),
|
||||||
Ok(valid_platforms.contains(platform))
|
(Platform::Linux, Platform::Linux) == (Platform::Linux, Platform::Linux)
|
||||||
|
);
|
||||||
|
Ok(self
|
||||||
|
.game_launchers
|
||||||
|
.contains_key(&(current.clone(), platform.clone())))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn launch_game(&mut self, game_id: String) -> Result<(), String> {
|
pub fn launch_game(&mut self, game_id: String) -> Result<(), String> {
|
||||||
@ -112,21 +127,33 @@ impl ProcessManager {
|
|||||||
.truncate(true)
|
.truncate(true)
|
||||||
.read(true)
|
.read(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(
|
.open(self.log_output_dir.join(format!(
|
||||||
self.log_output_dir
|
"{}-{}-error.log",
|
||||||
.join(format!("{}-{}-error.log", game_id, current_time.timestamp())),
|
game_id,
|
||||||
)
|
current_time.timestamp()
|
||||||
|
)))
|
||||||
.map_err(|v| v.to_string())?;
|
.map_err(|v| v.to_string())?;
|
||||||
|
|
||||||
info!("opened log file for {}", command);
|
info!("opened log file for {}", command);
|
||||||
|
|
||||||
let launch_process = Command::new(command)
|
let current_platform = self.current_platform.clone();
|
||||||
.current_dir(install_dir)
|
let target_platform = game_version.platform.clone();
|
||||||
.stdout(log_file)
|
|
||||||
.stderr(error_file)
|
let game_launcher = self
|
||||||
.args(args)
|
.game_launchers
|
||||||
.spawn()
|
.get(&(current_platform, target_platform))
|
||||||
.map_err(|v| v.to_string())?;
|
.ok_or("Invalid version for this platform.")
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
let launch_process = game_launcher.launch_game(
|
||||||
|
&game_id,
|
||||||
|
version_name,
|
||||||
|
command,
|
||||||
|
args,
|
||||||
|
install_dir,
|
||||||
|
log_file,
|
||||||
|
error_file,
|
||||||
|
)?;
|
||||||
|
|
||||||
self.processes.insert(game_id, launch_process);
|
self.processes.insert(game_id, launch_process);
|
||||||
|
|
||||||
@ -134,19 +161,43 @@ impl ProcessManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, Hash, PartialEq, Serialize, Deserialize, Clone)]
|
#[derive(Eq, Hash, PartialEq, Serialize, Deserialize, Clone, Debug)]
|
||||||
pub enum Platform {
|
pub enum Platform {
|
||||||
Windows,
|
Windows,
|
||||||
Linux,
|
Linux,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ProcessCompatabilityMatrix = HashMap<Platform, Vec<Platform>>;
|
pub trait ProcessHandler: Send + 'static {
|
||||||
pub static PROCESS_COMPATABILITY_MATRIX: LazyLock<ProcessCompatabilityMatrix> =
|
fn launch_game(
|
||||||
LazyLock::new(|| {
|
&self,
|
||||||
let mut matrix: ProcessCompatabilityMatrix = HashMap::new();
|
game_id: &String,
|
||||||
|
version_name: &String,
|
||||||
|
command: String,
|
||||||
|
args: Vec<String>,
|
||||||
|
install_dir: &String,
|
||||||
|
log_file: File,
|
||||||
|
error_file: File,
|
||||||
|
) -> Result<Child, String>;
|
||||||
|
}
|
||||||
|
|
||||||
matrix.insert(Platform::Windows, vec![Platform::Windows]);
|
struct NativeGameLauncher;
|
||||||
matrix.insert(Platform::Linux, vec![Platform::Linux]); // TODO: add Proton support
|
impl ProcessHandler for NativeGameLauncher {
|
||||||
|
fn launch_game(
|
||||||
return matrix;
|
&self,
|
||||||
});
|
game_id: &String,
|
||||||
|
version_name: &String,
|
||||||
|
command: String,
|
||||||
|
args: Vec<String>,
|
||||||
|
install_dir: &String,
|
||||||
|
log_file: File,
|
||||||
|
error_file: File,
|
||||||
|
) -> Result<Child, String> {
|
||||||
|
Command::new(command)
|
||||||
|
.current_dir(install_dir)
|
||||||
|
.stdout(log_file)
|
||||||
|
.stderr(error_file)
|
||||||
|
.args(args)
|
||||||
|
.spawn()
|
||||||
|
.map_err(|v| v.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -71,7 +71,7 @@ struct DropHealthcheck {
|
|||||||
|
|
||||||
async fn use_remote_logic<'a>(
|
async fn use_remote_logic<'a>(
|
||||||
url: String,
|
url: String,
|
||||||
state: tauri::State<'_, Mutex<AppState>>,
|
state: tauri::State<'_, Mutex<AppState<'a>>>,
|
||||||
) -> Result<(), RemoteAccessError> {
|
) -> Result<(), RemoteAccessError> {
|
||||||
info!("connecting to url {}", url);
|
info!("connecting to url {}", url);
|
||||||
let base_url = Url::parse(&url)?;
|
let base_url = Url::parse(&url)?;
|
||||||
@ -103,7 +103,7 @@ async fn use_remote_logic<'a>(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn use_remote<'a>(
|
pub async fn use_remote<'a>(
|
||||||
url: String,
|
url: String,
|
||||||
state: tauri::State<'_, Mutex<AppState>>,
|
state: tauri::State<'_, Mutex<AppState<'a>>>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let result = use_remote_logic(url, state).await;
|
let result = use_remote_logic(url, state).await;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user