mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 08:41:21 +10:00
feat(download & db): combined db and download interface improvements
This commit is contained in:
@ -6,10 +6,11 @@ use std::{
|
||||
};
|
||||
|
||||
use directories::BaseDirs;
|
||||
use log::{debug, info};
|
||||
use rustbreak::{deser::Bincode, PathDatabase};
|
||||
use log::debug;
|
||||
use rustbreak::{DeSerError, DeSerializer, PathDatabase};
|
||||
use rustix::path::Arg;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use url::Url;
|
||||
|
||||
use crate::DB;
|
||||
@ -22,14 +23,27 @@ pub struct DatabaseAuth {
|
||||
pub client_id: String,
|
||||
}
|
||||
|
||||
// Strings are version names for a particular game
|
||||
#[derive(Serialize, Clone, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum DatabaseGameStatus {
|
||||
Remote,
|
||||
Queued,
|
||||
Downloading,
|
||||
Installed,
|
||||
Updating,
|
||||
Uninstalling,
|
||||
Remote {},
|
||||
Queued { version_name: String },
|
||||
Downloading { version_name: String },
|
||||
SetupRequired { version_name: String },
|
||||
Installed { version_name: String },
|
||||
Updating { version_name: String },
|
||||
Uninstalling {},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct GameVersion {
|
||||
pub version_index: usize,
|
||||
pub version_name: String,
|
||||
pub launch_command: String,
|
||||
pub setup_command: String,
|
||||
pub platform: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone, Deserialize)]
|
||||
@ -38,6 +52,7 @@ pub struct DatabaseGames {
|
||||
pub install_dirs: Vec<String>,
|
||||
// Guaranteed to exist if the game also exists in the app state map
|
||||
pub games_statuses: HashMap<String, DatabaseGameStatus>,
|
||||
pub game_versions: HashMap<String, HashMap<String, GameVersion>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone, Deserialize)]
|
||||
@ -50,8 +65,22 @@ pub struct Database {
|
||||
pub static DATA_ROOT_DIR: LazyLock<Mutex<PathBuf>> =
|
||||
LazyLock::new(|| Mutex::new(BaseDirs::new().unwrap().data_dir().join("drop")));
|
||||
|
||||
// Custom JSON serializer to support everything we need
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct DropDatabaseSerializer;
|
||||
|
||||
impl<T: Serialize + DeserializeOwned> DeSerializer<T> for DropDatabaseSerializer {
|
||||
fn serialize(&self, val: &T) -> rustbreak::error::DeSerResult<Vec<u8>> {
|
||||
serde_json::to_vec(val).map_err(|e| DeSerError::Internal(e.to_string()))
|
||||
}
|
||||
|
||||
fn deserialize<R: std::io::Read>(&self, s: R) -> rustbreak::error::DeSerResult<T> {
|
||||
serde_json::from_reader(s).map_err(|e| DeSerError::Internal(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
pub type DatabaseInterface =
|
||||
rustbreak::Database<Database, rustbreak::backend::PathBackend, Bincode>;
|
||||
rustbreak::Database<Database, rustbreak::backend::PathBackend, DropDatabaseSerializer>;
|
||||
|
||||
pub trait DatabaseImpls {
|
||||
fn set_up_database() -> DatabaseInterface;
|
||||
@ -80,6 +109,7 @@ impl DatabaseImpls for DatabaseInterface {
|
||||
games: DatabaseGames {
|
||||
install_dirs: vec![games_base_dir.to_str().unwrap().to_string()],
|
||||
games_statuses: HashMap::new(),
|
||||
game_versions: HashMap::new(),
|
||||
},
|
||||
};
|
||||
debug!("Creating database at path {}", db_path.as_str().unwrap());
|
||||
|
||||
@ -187,7 +187,6 @@ impl GameDownloadAgent {
|
||||
drop(db_lock);
|
||||
|
||||
let manifest = self.manifest.lock().unwrap().clone().unwrap();
|
||||
let version = self.version.clone();
|
||||
let game_id = self.id.clone();
|
||||
|
||||
let data_base_dir_path = Path::new(&data_base_dir);
|
||||
|
||||
@ -11,7 +11,11 @@ use log::{error, info, warn};
|
||||
use rustbreak::Database;
|
||||
use tauri::{AppHandle, Emitter};
|
||||
|
||||
use crate::{db::DatabaseGameStatus, library::GameUpdateEvent, DB};
|
||||
use crate::{
|
||||
db::DatabaseGameStatus,
|
||||
library::{on_game_complete, GameUpdateEvent},
|
||||
DB,
|
||||
};
|
||||
|
||||
use super::{
|
||||
download_agent::{GameDownloadAgent, GameDownloadError},
|
||||
@ -167,11 +171,11 @@ impl DownloadManagerBuilder {
|
||||
if interface.id == game_id {
|
||||
info!("Popping consumed data");
|
||||
self.download_queue.pop_front();
|
||||
self.download_agent_registry.remove(&game_id);
|
||||
let download_agent = self.download_agent_registry.remove(&game_id).unwrap();
|
||||
self.active_control_flag = None;
|
||||
*self.progress.lock().unwrap() = None;
|
||||
|
||||
self.set_game_status(game_id, DatabaseGameStatus::Installed);
|
||||
on_game_complete(game_id, download_agent.version.clone(), &self.app_handle);
|
||||
}
|
||||
}
|
||||
self.sender.send(DownloadManagerSignal::Go).unwrap();
|
||||
@ -190,11 +194,12 @@ impl DownloadManagerBuilder {
|
||||
id: id.clone(),
|
||||
status: Mutex::new(agent_status),
|
||||
};
|
||||
let version_name = download_agent.version.clone();
|
||||
self.download_agent_registry
|
||||
.insert(interface_data.id.clone(), download_agent);
|
||||
self.download_queue.append(interface_data);
|
||||
|
||||
self.set_game_status(id, DatabaseGameStatus::Queued);
|
||||
self.set_game_status(id, DatabaseGameStatus::Queued { version_name });
|
||||
}
|
||||
|
||||
fn manage_go_signal(&mut self) {
|
||||
@ -213,6 +218,8 @@ impl DownloadManagerBuilder {
|
||||
.clone();
|
||||
self.current_game_interface = Some(agent_data);
|
||||
|
||||
let version_name = download_agent.version.clone();
|
||||
|
||||
let progress_object = download_agent.progress.clone();
|
||||
*self.progress.lock().unwrap() = Some(progress_object);
|
||||
|
||||
@ -240,7 +247,7 @@ impl DownloadManagerBuilder {
|
||||
self.set_status(DownloadManagerStatus::Downloading);
|
||||
self.set_game_status(
|
||||
self.current_game_interface.as_ref().unwrap().id.clone(),
|
||||
DatabaseGameStatus::Downloading,
|
||||
DatabaseGameStatus::Downloading { version_name },
|
||||
);
|
||||
}
|
||||
fn manage_error_signal(&self, error: GameDownloadError) {
|
||||
@ -251,7 +258,7 @@ impl DownloadManagerBuilder {
|
||||
|
||||
self.set_game_status(
|
||||
self.current_game_interface.as_ref().unwrap().id.clone(),
|
||||
DatabaseGameStatus::Remote,
|
||||
DatabaseGameStatus::Remote {},
|
||||
);
|
||||
}
|
||||
fn manage_cancel_signal(&mut self, game_id: String) {
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
#![feature(map_try_insert)]
|
||||
|
||||
mod auth;
|
||||
mod db;
|
||||
mod downloads;
|
||||
|
||||
@ -1,14 +1,17 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::format;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use log::info;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use tauri::Emitter;
|
||||
use tauri::{AppHandle, Manager};
|
||||
use urlencoding::encode;
|
||||
|
||||
use crate::db;
|
||||
use crate::db::DatabaseGameStatus;
|
||||
use crate::db::DatabaseImpls;
|
||||
use crate::db::{self, GameVersion};
|
||||
use crate::downloads::download_manager::GameDownloadStatus;
|
||||
use crate::remote::RemoteAccessError;
|
||||
use crate::{auth::generate_authorization_header, AppState, DB};
|
||||
@ -68,7 +71,7 @@ fn fetch_library_logic(app: AppHandle) -> Result<String, RemoteAccessError> {
|
||||
return Err(response.status().as_u16().into());
|
||||
}
|
||||
|
||||
let games = response.json::<Vec<Game>>()?;
|
||||
let games: Vec<Game> = response.json::<Vec<Game>>()?;
|
||||
|
||||
let state = app.state::<Mutex<AppState>>();
|
||||
let mut handle = state.lock().unwrap();
|
||||
@ -81,7 +84,7 @@ fn fetch_library_logic(app: AppHandle) -> Result<String, RemoteAccessError> {
|
||||
db_handle
|
||||
.games
|
||||
.games_statuses
|
||||
.insert(game.id.clone(), DatabaseGameStatus::Remote);
|
||||
.insert(game.id.clone(), DatabaseGameStatus::Remote {});
|
||||
}
|
||||
}
|
||||
|
||||
@ -145,7 +148,7 @@ fn fetch_game_logic(id: String, app: tauri::AppHandle) -> Result<String, RemoteA
|
||||
db_handle
|
||||
.games
|
||||
.games_statuses
|
||||
.insert(id, DatabaseGameStatus::Remote);
|
||||
.insert(id, DatabaseGameStatus::Remote {});
|
||||
}
|
||||
|
||||
let data = FetchGameStruct {
|
||||
@ -179,14 +182,16 @@ pub fn fetch_game_status(id: String) -> Result<DatabaseGameStatus, String> {
|
||||
.games
|
||||
.games_statuses
|
||||
.get(&id)
|
||||
.unwrap_or(&DatabaseGameStatus::Remote)
|
||||
.unwrap_or(&DatabaseGameStatus::Remote {})
|
||||
.clone();
|
||||
drop(db_handle);
|
||||
|
||||
return Ok(status);
|
||||
}
|
||||
|
||||
fn fetch_game_verion_options_logic(game_id: String) -> Result<Vec<GameVersionOption>, RemoteAccessError> {
|
||||
fn fetch_game_verion_options_logic(
|
||||
game_id: String,
|
||||
) -> Result<Vec<GameVersionOption>, RemoteAccessError> {
|
||||
let base_url = DB.fetch_base_url();
|
||||
|
||||
let endpoint =
|
||||
@ -214,3 +219,65 @@ fn fetch_game_verion_options_logic(game_id: String) -> Result<Vec<GameVersionOpt
|
||||
pub fn fetch_game_verion_options(game_id: String) -> Result<Vec<GameVersionOption>, String> {
|
||||
fetch_game_verion_options_logic(game_id).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
pub fn on_game_complete(
|
||||
game_id: String,
|
||||
version_name: String,
|
||||
app_handle: &AppHandle,
|
||||
) -> Result<(), RemoteAccessError> {
|
||||
// Fetch game version information from remote
|
||||
let base_url = DB.fetch_base_url();
|
||||
|
||||
let endpoint = base_url.join(
|
||||
format!(
|
||||
"/api/v1/client/metadata/version?id={}&version={}",
|
||||
game_id,
|
||||
encode(&version_name)
|
||||
)
|
||||
.as_str(),
|
||||
)?;
|
||||
let header = generate_authorization_header();
|
||||
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let response = client
|
||||
.get(endpoint.to_string())
|
||||
.header("Authorization", header)
|
||||
.send()?;
|
||||
|
||||
let data = response.json::<GameVersion>()?;
|
||||
|
||||
let mut handle = DB.borrow_data_mut().unwrap();
|
||||
handle
|
||||
.games
|
||||
.game_versions
|
||||
.entry(game_id.clone())
|
||||
.or_insert(HashMap::new())
|
||||
.insert(version_name.clone(), data.clone());
|
||||
drop(handle);
|
||||
DB.save().unwrap();
|
||||
|
||||
let status = if data.setup_command.is_empty() {
|
||||
DatabaseGameStatus::Installed { version_name }
|
||||
} else {
|
||||
DatabaseGameStatus::SetupRequired { version_name }
|
||||
};
|
||||
|
||||
let mut db_handle = DB.borrow_data_mut().unwrap();
|
||||
db_handle
|
||||
.games
|
||||
.games_statuses
|
||||
.insert(game_id.clone(), status.clone());
|
||||
drop(db_handle);
|
||||
DB.save().unwrap();
|
||||
app_handle
|
||||
.emit(
|
||||
&format!("update_game/{}", game_id),
|
||||
GameUpdateEvent {
|
||||
game_id: game_id,
|
||||
status: status,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user