mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-09 20:12:14 +10:00
feat(download & db): combined db and download interface improvements
This commit is contained in:
@ -1,18 +1,18 @@
|
||||
<template>
|
||||
<button
|
||||
type="button"
|
||||
@click="() => buttonActions[props.status]()"
|
||||
@click="() => buttonActions[props.status.type]()"
|
||||
:class="[
|
||||
styles[props.status],
|
||||
styles[props.status.type],
|
||||
'inline-flex uppercase font-display items-center gap-x-2 rounded-md px-4 py-3 text-md font-semibold shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2',
|
||||
]"
|
||||
>
|
||||
<component
|
||||
:is="buttonIcons[props.status]"
|
||||
:is="buttonIcons[props.status.type]"
|
||||
class="-mr-0.5 size-5"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
{{ buttonNames[props.status] }}
|
||||
{{ buttonNames[props.status.type] }}
|
||||
</button>
|
||||
</template>
|
||||
|
||||
@ -22,9 +22,10 @@ import {
|
||||
PlayIcon,
|
||||
QueueListIcon,
|
||||
TrashIcon,
|
||||
WrenchIcon,
|
||||
} from "@heroicons/vue/20/solid";
|
||||
import type { Component } from "vue";
|
||||
import { GameStatus } from "~/types.js";
|
||||
import { GameStatusEnum, type GameStatus } from "~/types.js";
|
||||
|
||||
const props = defineProps<{ status: GameStatus }>();
|
||||
const emit = defineEmits<{
|
||||
@ -33,43 +34,48 @@ const emit = defineEmits<{
|
||||
(e: "play"): void;
|
||||
}>();
|
||||
|
||||
const styles: { [key in GameStatus]: string } = {
|
||||
[GameStatus.Remote]:
|
||||
const styles: { [key in GameStatusEnum]: string } = {
|
||||
[GameStatusEnum.Remote]:
|
||||
"bg-blue-600 text-white hover:bg-blue-500 focus-visible:outline-blue-600",
|
||||
[GameStatus.Queued]:
|
||||
[GameStatusEnum.Queued]:
|
||||
"bg-zinc-800 text-white hover:bg-zinc-700 focus-visible:outline-zinc-700",
|
||||
[GameStatus.Downloading]:
|
||||
[GameStatusEnum.Downloading]:
|
||||
"bg-zinc-800 text-white hover:bg-zinc-700 focus-visible:outline-zinc-700",
|
||||
[GameStatus.Installed]:
|
||||
[GameStatusEnum.SetupRequired]:
|
||||
"bg-yellow-600 text-white hover:bg-yellow-500 focus-visible:outline-yellow-600",
|
||||
[GameStatusEnum.Installed]:
|
||||
"bg-green-600 text-white hover:bg-green-500 focus-visible:outline-green-600",
|
||||
[GameStatus.Updating]: "",
|
||||
[GameStatus.Uninstalling]: "",
|
||||
[GameStatusEnum.Updating]: "",
|
||||
[GameStatusEnum.Uninstalling]: "",
|
||||
};
|
||||
|
||||
const buttonNames: { [key in GameStatus]: string } = {
|
||||
[GameStatus.Remote]: "Install",
|
||||
[GameStatus.Queued]: "Queued",
|
||||
[GameStatus.Downloading]: "Downloading",
|
||||
[GameStatus.Installed]: "Play",
|
||||
[GameStatus.Updating]: "Updating",
|
||||
[GameStatus.Uninstalling]: "Uninstalling",
|
||||
const buttonNames: { [key in GameStatusEnum]: string } = {
|
||||
[GameStatusEnum.Remote]: "Install",
|
||||
[GameStatusEnum.Queued]: "Queued",
|
||||
[GameStatusEnum.Downloading]: "Downloading",
|
||||
[GameStatusEnum.SetupRequired]: "Setup",
|
||||
[GameStatusEnum.Installed]: "Play",
|
||||
[GameStatusEnum.Updating]: "Updating",
|
||||
[GameStatusEnum.Uninstalling]: "Uninstalling",
|
||||
};
|
||||
|
||||
const buttonIcons: { [key in GameStatus]: Component } = {
|
||||
[GameStatus.Remote]: ArrowDownTrayIcon,
|
||||
[GameStatus.Queued]: QueueListIcon,
|
||||
[GameStatus.Downloading]: ArrowDownTrayIcon,
|
||||
[GameStatus.Installed]: PlayIcon,
|
||||
[GameStatus.Updating]: ArrowDownTrayIcon,
|
||||
[GameStatus.Uninstalling]: TrashIcon,
|
||||
const buttonIcons: { [key in GameStatusEnum]: Component } = {
|
||||
[GameStatusEnum.Remote]: ArrowDownTrayIcon,
|
||||
[GameStatusEnum.Queued]: QueueListIcon,
|
||||
[GameStatusEnum.Downloading]: ArrowDownTrayIcon,
|
||||
[GameStatusEnum.SetupRequired]: WrenchIcon,
|
||||
[GameStatusEnum.Installed]: PlayIcon,
|
||||
[GameStatusEnum.Updating]: ArrowDownTrayIcon,
|
||||
[GameStatusEnum.Uninstalling]: TrashIcon,
|
||||
};
|
||||
|
||||
const buttonActions: { [key in GameStatus]: () => void } = {
|
||||
[GameStatus.Remote]: () => emit("install"),
|
||||
[GameStatus.Queued]: () => emit("cancel"),
|
||||
[GameStatus.Downloading]: () => emit("cancel"),
|
||||
[GameStatus.Installed]: () => emit("play"),
|
||||
[GameStatus.Updating]: () => emit("cancel"),
|
||||
[GameStatus.Uninstalling]: () => {},
|
||||
const buttonActions: { [key in GameStatusEnum]: () => void } = {
|
||||
[GameStatusEnum.Remote]: () => emit("install"),
|
||||
[GameStatusEnum.Queued]: () => emit("cancel"),
|
||||
[GameStatusEnum.Downloading]: () => emit("cancel"),
|
||||
[GameStatusEnum.SetupRequired]: () => {},
|
||||
[GameStatusEnum.Installed]: () => emit("play"),
|
||||
[GameStatusEnum.Updating]: () => emit("cancel"),
|
||||
[GameStatusEnum.Uninstalling]: () => {},
|
||||
};
|
||||
</script>
|
||||
|
||||
@ -275,7 +275,11 @@
|
||||
>
|
||||
<LoadingButton
|
||||
:disabled="
|
||||
!(versionOptions && versionOptions.length > 0 && !installDir)
|
||||
!(
|
||||
versionOptions &&
|
||||
versionOptions.length > 0 &&
|
||||
!installDir
|
||||
)
|
||||
"
|
||||
:loading="installLoading"
|
||||
type="submit"
|
||||
|
||||
17
src-tauri/Cargo.lock
generated
17
src-tauri/Cargo.lock
generated
@ -325,12 +325,6 @@ dependencies = [
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.21.7"
|
||||
@ -343,15 +337,6 @@ version = "0.22.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||
|
||||
[[package]]
|
||||
name = "bincode"
|
||||
version = "1.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
@ -3274,8 +3259,6 @@ version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "460d97902465327d69ecfe8cefdb5972c6f94d6127ac9e992acdb51458bebc27"
|
||||
dependencies = [
|
||||
"base64 0.12.3",
|
||||
"bincode",
|
||||
"serde",
|
||||
"tempfile",
|
||||
"thiserror",
|
||||
|
||||
@ -39,15 +39,17 @@ hex = "0.4.3"
|
||||
tauri-plugin-dialog = "2"
|
||||
env_logger = "0.11.5"
|
||||
http = "1.1.0"
|
||||
tokio = { version = "1.40.0", features = ["rt", "tokio-macros", "signal"] }
|
||||
urlencoding = "2.1.3"
|
||||
md5 = "0.7.0"
|
||||
|
||||
[dependencies.tokio]
|
||||
version = "1.40.0"
|
||||
features = ["rt", "tokio-macros", "signal"]
|
||||
|
||||
|
||||
[dependencies.rustix]
|
||||
version = "0.38.37"
|
||||
features = [
|
||||
"fs"
|
||||
]
|
||||
features = ["fs"]
|
||||
|
||||
[dependencies.uuid]
|
||||
version = "1.10.0"
|
||||
@ -62,7 +64,7 @@ version = "0.10.66"
|
||||
|
||||
[dependencies.rustbreak]
|
||||
version = "2"
|
||||
features = ["bin_enc"] # You can also use "yaml_enc" or "bin_enc"
|
||||
features = [] # You can also use "yaml_enc" or "bin_enc"
|
||||
|
||||
[dependencies.reqwest]
|
||||
version = "0.12"
|
||||
|
||||
@ -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(())
|
||||
}
|
||||
|
||||
8
types.ts
8
types.ts
@ -25,11 +25,17 @@ export enum AppStatus {
|
||||
ServerUnavailable = "ServerUnavailable",
|
||||
}
|
||||
|
||||
export enum GameStatus {
|
||||
export enum GameStatusEnum {
|
||||
Remote = "Remote",
|
||||
Queued = "Queued",
|
||||
Downloading = "Downloading",
|
||||
Installed = "Installed",
|
||||
Updating = "Updating",
|
||||
Uninstalling = "Uninstalling",
|
||||
SetupRequired = "SetupRequired",
|
||||
}
|
||||
|
||||
export type GameStatus = {
|
||||
type: GameStatusEnum;
|
||||
version_name?: string;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user