mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-08-24 15:22:05 +10:00
Depot API & executor launch (#173)
* feat: depot api downloads * feat: frontend fixes and experimental webview store * feat: sync downloader * feat: cleanup and fixes * feat: encrypted database and fixed resuming * feat: launch option selector * fix: autostart when no options * fix: clippy * fix: clippy x2 * feat: executor launch * feat: executor launch * feat: not installed error handling * feat: better offline handling * feat: dependency popup * fix: cancelation and resuming issues * feat: dedup by platform * feat: new ui for additional components and fix dl manager clog * feat: auto-queue dependencies * feat: depot scanning and ranking * feat: new library fetching stack * In-app store page (Windows + macOS) (#176) * feat: async store loading * feat: fix overscroll behaviour * fix: query params in server protocol * fix: clippy
This commit is contained in:
+148
-88
@@ -1,44 +1,60 @@
|
||||
use std::sync::nonpoison::Mutex;
|
||||
|
||||
use database::{GameDownloadStatus, GameVersion, borrow_db_checked, borrow_db_mut_checked};
|
||||
use bitcode::{Decode, Encode};
|
||||
use database::{
|
||||
DownloadableMetadata, GameDownloadStatus, borrow_db_checked,
|
||||
borrow_db_mut_checked, platform::Platform,
|
||||
};
|
||||
use games::{
|
||||
collections::collection::Collection,
|
||||
downloads::error::LibraryError,
|
||||
library::{FetchGameStruct, FrontendGameOptions, Game, get_current_meta, uninstall_game_logic},
|
||||
state::{GameStatusManager, GameStatusWithTransient},
|
||||
};
|
||||
use log::{info, warn};
|
||||
use log::warn;
|
||||
use process::PROCESS_MANAGER;
|
||||
use remote::{
|
||||
auth::generate_authorization_header,
|
||||
cache::{cache_object, cache_object_db, get_cached_object, get_cached_object_db},
|
||||
cache::{cache_object, cache_object_db, get_cached_object},
|
||||
error::{DropServerError, RemoteAccessError},
|
||||
offline,
|
||||
requests::generate_url,
|
||||
utils::DROP_CLIENT_ASYNC,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::AppHandle;
|
||||
|
||||
use crate::AppState;
|
||||
use crate::{AppState, collections::fetch_collections};
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn fetch_library(
|
||||
state: tauri::State<'_, Mutex<AppState>>,
|
||||
app_handle: AppHandle,
|
||||
hard_refresh: Option<bool>,
|
||||
) -> Result<Vec<Game>, RemoteAccessError> {
|
||||
) -> Result<FetchLibraryResponse, RemoteAccessError> {
|
||||
offline!(
|
||||
state,
|
||||
fetch_library_logic,
|
||||
fetch_library_logic_offline,
|
||||
state,
|
||||
app_handle,
|
||||
hard_refresh
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Serialize)]
|
||||
pub struct FetchLibraryResponse {
|
||||
library: Vec<Game>,
|
||||
collections: Vec<Collection>,
|
||||
other: Vec<Game>,
|
||||
}
|
||||
|
||||
pub async fn fetch_library_logic(
|
||||
state: tauri::State<'_, Mutex<AppState>>,
|
||||
app_handle: AppHandle,
|
||||
hard_fresh: Option<bool>,
|
||||
) -> Result<Vec<Game>, RemoteAccessError> {
|
||||
) -> Result<FetchLibraryResponse, RemoteAccessError> {
|
||||
let do_hard_refresh = hard_fresh.unwrap_or(false);
|
||||
if !do_hard_refresh && let Ok(library) = get_cached_object("library") {
|
||||
return Ok(library);
|
||||
@@ -62,57 +78,83 @@ pub async fn fetch_library_logic(
|
||||
return Err(RemoteAccessError::InvalidResponse(err));
|
||||
}
|
||||
|
||||
let mut games: Vec<Game> = response.json().await?;
|
||||
let library: Vec<Game> = response.json().await?;
|
||||
let collections = fetch_collections(state, hard_fresh).await?;
|
||||
|
||||
let mut handle = state.lock();
|
||||
let mut all_games = library.clone();
|
||||
all_games.extend(
|
||||
collections
|
||||
.iter()
|
||||
.flat_map(|v| v.entries.iter().map(|v| v.game.clone())),
|
||||
);
|
||||
|
||||
let mut db_handle = borrow_db_mut_checked();
|
||||
let installed_metas = {
|
||||
let mut db_handle = borrow_db_mut_checked();
|
||||
|
||||
for game in &games {
|
||||
handle.games.insert(game.id().clone(), game.clone());
|
||||
if !db_handle.applications.game_statuses.contains_key(game.id()) {
|
||||
db_handle
|
||||
.applications
|
||||
.game_statuses
|
||||
.insert(game.id().clone(), GameDownloadStatus::Remote {});
|
||||
for game in &all_games {
|
||||
if !db_handle.applications.game_statuses.contains_key(game.id()) {
|
||||
db_handle
|
||||
.applications
|
||||
.game_statuses
|
||||
.insert(game.id().clone(), GameDownloadStatus::Remote {});
|
||||
}
|
||||
cache_object_db(&format!("game/{}", game.id), game, &db_handle)?;
|
||||
}
|
||||
}
|
||||
|
||||
db_handle
|
||||
.applications
|
||||
.installed_game_version
|
||||
.values()
|
||||
.cloned()
|
||||
.collect::<Vec<DownloadableMetadata>>()
|
||||
};
|
||||
|
||||
// Add games that are installed but no longer in library
|
||||
for meta in db_handle.applications.installed_game_version.values() {
|
||||
if games.iter().any(|e| *e.id() == meta.id) {
|
||||
let mut other = Vec::new();
|
||||
for meta in installed_metas {
|
||||
if all_games.iter().any(|e| *e.id() == meta.id) {
|
||||
continue;
|
||||
}
|
||||
// We should always have a cache of the object
|
||||
// Pass db_handle because otherwise we get a gridlock
|
||||
let game = match get_cached_object_db::<Game>(&meta.id.clone(), &db_handle) {
|
||||
let game = match get_cached_object::<Game>(&meta.id.clone()) {
|
||||
Ok(game) => game,
|
||||
Err(err) => {
|
||||
warn!(
|
||||
"{} is installed, but encountered error fetching its error: {}.",
|
||||
meta.id, err
|
||||
);
|
||||
/*
|
||||
* We can't return a dummy object here because it needs to be in the cache to work
|
||||
* So we uninstall the game so we don't "lose" it
|
||||
*/
|
||||
uninstall_game_logic(meta.clone(), &app_handle);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
games.push(game);
|
||||
other.push(game);
|
||||
}
|
||||
|
||||
drop(handle);
|
||||
drop(db_handle);
|
||||
cache_object("library", &games)?;
|
||||
let response = FetchLibraryResponse {
|
||||
library,
|
||||
collections,
|
||||
other,
|
||||
};
|
||||
|
||||
Ok(games)
|
||||
cache_object("library", &response)?;
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
pub async fn fetch_library_logic_offline(
|
||||
_state: tauri::State<'_, Mutex<AppState>>,
|
||||
_app_handle: AppHandle,
|
||||
_hard_refresh: Option<bool>,
|
||||
) -> Result<Vec<Game>, RemoteAccessError> {
|
||||
let mut games: Vec<Game> = get_cached_object("library")?;
|
||||
) -> Result<FetchLibraryResponse, RemoteAccessError> {
|
||||
let mut response: FetchLibraryResponse = get_cached_object("library")?;
|
||||
|
||||
let db_handle = borrow_db_checked();
|
||||
|
||||
games.retain(|game| {
|
||||
let retain_filter = |game: &Game| {
|
||||
matches!(
|
||||
&db_handle
|
||||
.applications
|
||||
@@ -121,70 +163,74 @@ pub async fn fetch_library_logic_offline(
|
||||
.unwrap_or(&GameDownloadStatus::Remote {}),
|
||||
GameDownloadStatus::Installed { .. } | GameDownloadStatus::SetupRequired { .. }
|
||||
)
|
||||
};
|
||||
|
||||
response.library.retain(retain_filter);
|
||||
response.other.retain(retain_filter);
|
||||
response.collections.iter_mut().for_each(|k| {
|
||||
k.entries.retain(|object| {
|
||||
matches!(
|
||||
&db_handle
|
||||
.applications
|
||||
.game_statuses
|
||||
.get(object.game.id())
|
||||
.unwrap_or(&GameDownloadStatus::Remote {}),
|
||||
GameDownloadStatus::Installed { .. } | GameDownloadStatus::SetupRequired { .. }
|
||||
)
|
||||
})
|
||||
});
|
||||
|
||||
Ok(games)
|
||||
Ok(response)
|
||||
}
|
||||
pub async fn fetch_game_logic(
|
||||
id: String,
|
||||
state: tauri::State<'_, Mutex<AppState>>,
|
||||
) -> Result<FetchGameStruct, RemoteAccessError> {
|
||||
let version = {
|
||||
let state_handle = state.lock();
|
||||
|
||||
let db_lock = borrow_db_checked();
|
||||
|
||||
let metadata_option = db_lock.applications.installed_game_version.get(&id);
|
||||
let version = match metadata_option {
|
||||
|
||||
|
||||
match metadata_option {
|
||||
None => None,
|
||||
Some(metadata) => db_lock
|
||||
.applications
|
||||
.game_versions
|
||||
.get(&metadata.id)
|
||||
.map(|v| v.get(metadata.version.as_ref().unwrap()).unwrap())
|
||||
.get(&metadata.version)
|
||||
.cloned(),
|
||||
};
|
||||
|
||||
let game = state_handle.games.get(&id);
|
||||
if let Some(game) = game {
|
||||
let status = GameStatusManager::fetch_state(&id, &db_lock);
|
||||
|
||||
let data = FetchGameStruct::new(game.clone(), status, version);
|
||||
|
||||
cache_object_db(&id, game, &db_lock)?;
|
||||
|
||||
return Ok(data);
|
||||
}
|
||||
|
||||
version
|
||||
};
|
||||
|
||||
let client = DROP_CLIENT_ASYNC.clone();
|
||||
let response = generate_url(&["/api/v1/client/game/", &id], &[])?;
|
||||
let response = client
|
||||
.get(response)
|
||||
.header("Authorization", generate_authorization_header())
|
||||
.send()
|
||||
.await?;
|
||||
let game = match get_cached_object::<Game>(&format!("game/{}", id)) {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
let client = DROP_CLIENT_ASYNC.clone();
|
||||
let response = generate_url(&["/api/v1/client/game", &id], &[])?;
|
||||
let response = client
|
||||
.get(response)
|
||||
.header("Authorization", generate_authorization_header())
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if response.status() == 404 {
|
||||
let offline_fetch = fetch_game_logic_offline(id.clone(), state).await;
|
||||
if let Ok(fetch_data) = offline_fetch {
|
||||
return Ok(fetch_data);
|
||||
if response.status() == 404 {
|
||||
let offline_fetch = fetch_game_logic_offline(id.clone(), state).await;
|
||||
if let Ok(fetch_data) = offline_fetch {
|
||||
return Ok(fetch_data);
|
||||
}
|
||||
|
||||
return Err(RemoteAccessError::GameNotFound(id));
|
||||
}
|
||||
if response.status() != 200 {
|
||||
let err = response.json().await?;
|
||||
warn!("{err:?}");
|
||||
return Err(RemoteAccessError::InvalidResponse(err));
|
||||
}
|
||||
|
||||
let game: Game = response.json().await?;
|
||||
game
|
||||
}
|
||||
|
||||
return Err(RemoteAccessError::GameNotFound(id));
|
||||
}
|
||||
if response.status() != 200 {
|
||||
let err = response.json().await?;
|
||||
warn!("{err:?}");
|
||||
return Err(RemoteAccessError::InvalidResponse(err));
|
||||
}
|
||||
|
||||
let game: Game = response.json().await?;
|
||||
|
||||
let mut state_handle = state.lock();
|
||||
state_handle.games.insert(id.clone(), game.clone());
|
||||
};
|
||||
|
||||
let mut db_handle = borrow_db_mut_checked();
|
||||
|
||||
@@ -205,10 +251,31 @@ pub async fn fetch_game_logic(
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct VersionDownloadOptionRequiredContent {
|
||||
version_id: String,
|
||||
name: String,
|
||||
icon_object_id: String,
|
||||
short_description: String,
|
||||
size: usize,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct VersionDownloadOption {
|
||||
version_id: String,
|
||||
display_name: Option<String>,
|
||||
version_path: String,
|
||||
platform: Platform,
|
||||
size: usize,
|
||||
required_content: Vec<VersionDownloadOptionRequiredContent>,
|
||||
}
|
||||
|
||||
pub async fn fetch_game_version_options_logic(
|
||||
game_id: String,
|
||||
state: tauri::State<'_, Mutex<AppState>>,
|
||||
) -> Result<Vec<GameVersion>, RemoteAccessError> {
|
||||
) -> Result<Vec<VersionDownloadOption>, RemoteAccessError> {
|
||||
let client = DROP_CLIENT_ASYNC.clone();
|
||||
|
||||
let response = generate_url(&["/api/v1/client/game/versions"], &[("id", &game_id)])?;
|
||||
@@ -223,15 +290,16 @@ pub async fn fetch_game_version_options_logic(
|
||||
warn!("{err:?}");
|
||||
return Err(RemoteAccessError::InvalidResponse(err));
|
||||
}
|
||||
|
||||
let data: Vec<GameVersion> = response.json().await?;
|
||||
|
||||
let data: Vec<VersionDownloadOption> = response.json().await?;
|
||||
|
||||
let state_lock = state.lock();
|
||||
let process_manager_lock = PROCESS_MANAGER.lock();
|
||||
let data: Vec<GameVersion> = data
|
||||
let data = data
|
||||
.into_iter()
|
||||
.filter(|v| process_manager_lock.valid_platform(&v.platform))
|
||||
.collect();
|
||||
//data.dedup_by_key(|v| v.platform);
|
||||
drop(process_manager_lock);
|
||||
drop(state_lock);
|
||||
|
||||
@@ -249,8 +317,7 @@ pub async fn fetch_game_logic_offline(
|
||||
Some(metadata) => db_handle
|
||||
.applications
|
||||
.game_versions
|
||||
.get(&metadata.id)
|
||||
.map(|v| v.get(metadata.version.as_ref().unwrap()).unwrap())
|
||||
.get(&metadata.version)
|
||||
.cloned(),
|
||||
};
|
||||
|
||||
@@ -298,7 +365,7 @@ pub fn uninstall_game(game_id: String, app_handle: AppHandle) -> Result<(), Libr
|
||||
pub async fn fetch_game_version_options(
|
||||
game_id: String,
|
||||
state: tauri::State<'_, Mutex<AppState>>,
|
||||
) -> Result<Vec<GameVersion>, RemoteAccessError> {
|
||||
) -> Result<Vec<VersionDownloadOption>, RemoteAccessError> {
|
||||
fetch_game_version_options_logic(game_id, state).await
|
||||
}
|
||||
|
||||
@@ -314,31 +381,24 @@ pub fn update_game_configuration(
|
||||
.get(&game_id)
|
||||
.ok_or(LibraryError::MetaNotFound(game_id))?;
|
||||
|
||||
let id = installed_version.id.clone();
|
||||
let version = installed_version
|
||||
.version
|
||||
.clone()
|
||||
.ok_or(LibraryError::VersionNotFound(id.clone()))?;
|
||||
let _id = installed_version.id.clone();
|
||||
let version = installed_version.version.clone();
|
||||
|
||||
let mut existing_configuration = handle
|
||||
.applications
|
||||
.game_versions
|
||||
.get(&id)
|
||||
.unwrap()
|
||||
.get(&version)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
// Add more options in here
|
||||
existing_configuration.launch_command_template = options.launch_string().clone();
|
||||
existing_configuration.launch_template = options.launch_string().clone();
|
||||
|
||||
// Add no more options past here
|
||||
|
||||
handle
|
||||
.applications
|
||||
.game_versions
|
||||
.get_mut(&id)
|
||||
.unwrap()
|
||||
.insert(version.to_string(), existing_configuration);
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user