mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 08:41:21 +10:00
feat(cache): Implemented caching for game metadata
This commit is contained in:
@ -74,7 +74,8 @@ pub fn update_settings(new_settings: Value) {
|
||||
}
|
||||
let new_settings: Settings = serde_json::from_value(current_settings).unwrap();
|
||||
db_lock.settings = new_settings;
|
||||
println!("new Settings: {:?}", db_lock.settings);
|
||||
drop(db_lock);
|
||||
save_db();
|
||||
}
|
||||
#[tauri::command]
|
||||
pub fn fetch_settings() -> Settings {
|
||||
|
||||
@ -73,7 +73,6 @@ pub fn fetch_library_logic(
|
||||
let header = generate_authorization_header();
|
||||
|
||||
let client = reqwest::blocking::Client::new();
|
||||
println!("Making library request");
|
||||
let response = make_request(&client, &["/api/v1/client/user/library"], &[], |f| {
|
||||
f.header("Authorization", header)
|
||||
})?
|
||||
@ -84,7 +83,6 @@ pub fn fetch_library_logic(
|
||||
warn!("{:?}", err);
|
||||
return Err(RemoteAccessError::InvalidResponse(err));
|
||||
}
|
||||
println!("Getting Games");
|
||||
|
||||
let games: Vec<Game> = response.json()?;
|
||||
|
||||
@ -103,10 +101,8 @@ pub fn fetch_library_logic(
|
||||
}
|
||||
drop(handle);
|
||||
drop(db_handle);
|
||||
println!("Caching");
|
||||
cache_object("library", &games)?;
|
||||
|
||||
println!("Finished caching");
|
||||
|
||||
Ok(games)
|
||||
}
|
||||
@ -114,18 +110,15 @@ pub fn fetch_library_logic_offline(
|
||||
_state: tauri::State<'_, Mutex<AppState>>,
|
||||
) -> Result<Vec<Game>, RemoteAccessError> {
|
||||
let mut games: Vec<Game> = get_cached_object("library")?;
|
||||
println!("Old games: {:?}", games.len());
|
||||
|
||||
let db_handle = borrow_db_checked();
|
||||
|
||||
games.retain(|game| {
|
||||
println!("Retaining game {}: {}", &game.id, db_handle.applications.installed_game_version.contains_key(&game.id));
|
||||
db_handle
|
||||
.applications
|
||||
.installed_game_version
|
||||
.contains_key(&game.id)
|
||||
});
|
||||
println!("New games: {:?}", games.len());
|
||||
|
||||
Ok(games)
|
||||
}
|
||||
@ -144,6 +137,9 @@ pub fn fetch_game_logic(
|
||||
status,
|
||||
};
|
||||
|
||||
|
||||
cache_object(id, &data)?;
|
||||
|
||||
return Ok(data);
|
||||
}
|
||||
let client = reqwest::blocking::Client::new();
|
||||
@ -189,7 +185,7 @@ pub fn fetch_game_logic_offline(
|
||||
id: String,
|
||||
_state: tauri::State<'_, Mutex<AppState>>,
|
||||
) -> Result<FetchGameStruct, RemoteAccessError> {
|
||||
get_cached_object(id)
|
||||
get_cached_object::<String, FetchGameStruct>(id)
|
||||
}
|
||||
|
||||
pub fn fetch_game_verion_options_logic(
|
||||
@ -227,7 +223,7 @@ pub fn fetch_game_verion_options_logic(
|
||||
}
|
||||
|
||||
pub fn uninstall_game_logic(meta: DownloadableMetadata, app_handle: &AppHandle) {
|
||||
println!("triggered uninstall for agent");
|
||||
debug!("triggered uninstall for agent");
|
||||
let mut db_handle = borrow_db_mut_checked();
|
||||
db_handle
|
||||
.applications
|
||||
|
||||
@ -328,7 +328,7 @@ pub fn run() {
|
||||
}
|
||||
|
||||
_ => {
|
||||
println!("menu event not handled: {:?}", event.id);
|
||||
warn!("menu event not handled: {:?}", event.id);
|
||||
}
|
||||
})
|
||||
.build(app)
|
||||
|
||||
@ -372,7 +372,7 @@ impl ProcessHandler for UMULauncher {
|
||||
_log_file: File,
|
||||
_error_file: File,
|
||||
) -> Result<Child, Error> {
|
||||
println!("Game override: .{:?}.", &game_version.umu_id_override);
|
||||
debug!("Game override: \"{:?}\"", &game_version.umu_id_override);
|
||||
let game_id = match &game_version.umu_id_override {
|
||||
Some(game_override) => game_override.is_empty().then_some(game_version.game_id.clone()).unwrap_or(game_override.clone()) ,
|
||||
None => game_version.game_id.clone()
|
||||
|
||||
@ -191,7 +191,7 @@ pub fn setup() -> (AppStatus, Option<User>) {
|
||||
if auth.is_some() {
|
||||
let user_result = match fetch_user() {
|
||||
Ok(data) => data,
|
||||
Err(RemoteAccessError::FetchError(_)) => return (AppStatus::ServerUnavailable, None),
|
||||
Err(RemoteAccessError::FetchError(_)) => return (AppStatus::Offline, None),
|
||||
Err(_) => return (AppStatus::SignedInNeedsReauth, None),
|
||||
};
|
||||
return (AppStatus::SignedIn, Some(user_result));
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
use cacache::Integrity;
|
||||
use openssl::hash::{hash, MessageDigest};
|
||||
use rustix::path::Arg;
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
use serde_binary::binary_stream::Endian;
|
||||
|
||||
use crate::{database::db::borrow_db_checked, error::remote_access_error::RemoteAccessError};
|
||||
|
||||
@ -17,11 +20,11 @@ macro_rules! offline {
|
||||
}
|
||||
|
||||
pub fn cache_object<'a, K: AsRef<str>, D: Serialize + DeserializeOwned>(key: K, data: &D) -> Result<Integrity, RemoteAccessError> {
|
||||
let bytes = bincode::serialize(data).unwrap();
|
||||
let bytes = serde_json::to_vec(data).unwrap();
|
||||
cacache::write_sync(&borrow_db_checked().cache_dir, key, bytes).map_err(|e| RemoteAccessError::Cache(e))
|
||||
}
|
||||
pub fn get_cached_object<'a, K: AsRef<str>, D: Serialize + DeserializeOwned>(key: K) -> Result<D, RemoteAccessError> {
|
||||
let bytes = cacache::read_sync(&borrow_db_checked().cache_dir, key).map_err(|e| RemoteAccessError::Cache(e))?;
|
||||
let data = bincode::deserialize::<D>(&bytes).unwrap();
|
||||
let data = serde_json::from_slice::<D>(&bytes).unwrap();
|
||||
Ok(data)
|
||||
}
|
||||
Reference in New Issue
Block a user