mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-13 00:02:41 +10:00
Compare commits
1 Commits
160-generi
...
fix-cmd-wi
| Author | SHA1 | Date | |
|---|---|---|---|
| 12b39bab68 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -29,6 +29,4 @@ src-tauri/flamegraph.svg
|
|||||||
src-tauri/perf*
|
src-tauri/perf*
|
||||||
|
|
||||||
/*.AppImage
|
/*.AppImage
|
||||||
/squashfs-root
|
/squashfs-root
|
||||||
|
|
||||||
/target/
|
|
||||||
24
README.md
24
README.md
@ -1,21 +1,29 @@
|
|||||||
# Drop Desktop Client
|
# Drop App
|
||||||
|
|
||||||
The Drop Desktop Client is the companion app for [Drop](https://github.com/Drop-OSS/drop). It is the official & intended way to download and play games on your Drop server.
|
Drop app is the companion app for [Drop](https://github.com/Drop-OSS/drop). It uses a Tauri base with Nuxt 3 + TailwindCSS on top of it, so we can re-use components from the web UI.
|
||||||
|
|
||||||
## Internals
|
## Running
|
||||||
|
Before setting up the drop app, be sure that you have a server set up.
|
||||||
|
The instructions for this can be found on the [Drop Docs](https://docs.droposs.org/docs/guides/quickstart)
|
||||||
|
|
||||||
It uses a Tauri base with Nuxt 3 + TailwindCSS on top of it, so we can re-use components from the web UI.
|
## Current features
|
||||||
|
Currently supported are the following features:
|
||||||
|
- Signin (with custom server)
|
||||||
|
- Database registering & recovery
|
||||||
|
- Dynamic library fetching from server
|
||||||
|
- Installing & uninstalling games
|
||||||
|
- Download progress monitoring
|
||||||
|
- Launching / playing games
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
Before setting up a development environemnt, be sure that you have a server set up. The instructions for this can be found on the [Drop Docs](https://docs.droposs.org/docs/guides/quickstart).
|
|
||||||
|
|
||||||
Then, install dependencies with `yarn`. This'll install the custom builder's dependencies. Then, check everything works properly with `yarn tauri build`.
|
Install dependencies with `yarn`
|
||||||
|
|
||||||
Run the app in development with `yarn tauri dev`. NVIDIA users on Linux, use shell script `./nvidia-prop-dev.sh`
|
Run the app in development with `yarn tauri dev`. NVIDIA users on Linux, use shell script `./nvidia-prop-dev.sh`
|
||||||
|
|
||||||
To manually specify the logging level, add the environment variable `RUST_LOG=[debug, info, warn, error]` to `yarn tauri dev`:
|
To manually specify the logging level, add the environment variable `RUST_LOG=[debug, info, warn, error]` to `yarn tauri dev`:
|
||||||
|
|
||||||
e.g. `RUST_LOG=debug yarn tauri dev`
|
e.g. `RUST_LOG=debug yarn tauri dev`
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
Check out the contributing guide on our Developer Docs: [Drop Developer Docs - Contributing](https://developer.droposs.org/contributing).
|
Check the original [Drop repo](https://github.com/Drop-OSS/drop/blob/main/CONTRIBUTING.md) for contributing guidelines.
|
||||||
@ -116,7 +116,7 @@ platformInfo.value = currentPlatform;
|
|||||||
async function openDataDir() {
|
async function openDataDir() {
|
||||||
if (!dataDir.value) return;
|
if (!dataDir.value) return;
|
||||||
try {
|
try {
|
||||||
await invoke("open_fs", { path: dataDir.value });
|
await open(dataDir.value);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to open data dir:", error);
|
console.error("Failed to open data dir:", error);
|
||||||
}
|
}
|
||||||
@ -126,7 +126,7 @@ async function openLogFile() {
|
|||||||
if (!dataDir.value) return;
|
if (!dataDir.value) return;
|
||||||
try {
|
try {
|
||||||
const logPath = `${dataDir.value}/drop.log`;
|
const logPath = `${dataDir.value}/drop.log`;
|
||||||
await invoke("open_fs", { path: logPath });
|
await open(logPath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to open log file:", error);
|
console.error("Failed to open log file:", error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,13 +9,12 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tauri-apps/api": "^2.7.0",
|
"@tauri-apps/api": "^2.7.0",
|
||||||
"@tauri-apps/plugin-deep-link": "^2.4.1",
|
"@tauri-apps/plugin-deep-link": "^2.4.1",
|
||||||
"@tauri-apps/plugin-dialog": "^2.4.0",
|
"@tauri-apps/plugin-dialog": "^2.3.2",
|
||||||
"@tauri-apps/plugin-opener": "^2.5.0",
|
"@tauri-apps/plugin-opener": "^2.4.0",
|
||||||
"@tauri-apps/plugin-os": "^2.3.0",
|
"@tauri-apps/plugin-os": "^2.3.0",
|
||||||
"@tauri-apps/plugin-shell": "^2.3.0",
|
"@tauri-apps/plugin-shell": "^2.3.0",
|
||||||
"pino": "^9.7.0",
|
"pino": "^9.7.0",
|
||||||
"pino-pretty": "^13.1.1",
|
"pino-pretty": "^13.1.1"
|
||||||
"tauri": "^0.15.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@tauri-apps/cli": "^2.7.1"
|
"@tauri-apps/cli": "^2.7.1"
|
||||||
|
|||||||
2456
src-tauri/Cargo.lock
generated
2456
src-tauri/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -65,9 +65,7 @@ whoami = "1.6.0"
|
|||||||
filetime = "0.2.25"
|
filetime = "0.2.25"
|
||||||
walkdir = "2.5.0"
|
walkdir = "2.5.0"
|
||||||
known-folders = "1.2.0"
|
known-folders = "1.2.0"
|
||||||
native_model = { version = "0.6.4", features = [
|
native_model = { version = "0.6.4", features = ["rmp_serde_1_3"], git = "https://github.com/Drop-OSS/native_model.git"}
|
||||||
"rmp_serde_1_3",
|
|
||||||
], git = "https://github.com/Drop-OSS/native_model.git" }
|
|
||||||
tauri-plugin-opener = "2.4.0"
|
tauri-plugin-opener = "2.4.0"
|
||||||
bitcode = "0.6.6"
|
bitcode = "0.6.6"
|
||||||
reqwest-websocket = "0.5.0"
|
reqwest-websocket = "0.5.0"
|
||||||
@ -80,16 +78,6 @@ futures-core = "0.3.31"
|
|||||||
bytes = "1.10.1"
|
bytes = "1.10.1"
|
||||||
# tailscale = { path = "./tailscale" }
|
# tailscale = { path = "./tailscale" }
|
||||||
|
|
||||||
|
|
||||||
# Workspaces
|
|
||||||
client = { version = "0.1.0", path = "./client" }
|
|
||||||
database = { path = "./database" }
|
|
||||||
process = { path = "./process" }
|
|
||||||
remote = { version = "0.1.0", path = "./remote" }
|
|
||||||
utils = { path = "./utils" }
|
|
||||||
games = { version = "0.1.0", path = "./games" }
|
|
||||||
download_manager = { version = "0.1.0", path = "./download_manager" }
|
|
||||||
|
|
||||||
[dependencies.dynfmt]
|
[dependencies.dynfmt]
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
features = ["curly"]
|
features = ["curly"]
|
||||||
@ -139,20 +127,3 @@ features = ["derive", "rc"]
|
|||||||
lto = true
|
lto = true
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
panic = 'abort'
|
panic = 'abort'
|
||||||
|
|
||||||
|
|
||||||
[workspace]
|
|
||||||
members = [
|
|
||||||
"client",
|
|
||||||
"database",
|
|
||||||
"process",
|
|
||||||
"remote",
|
|
||||||
"utils",
|
|
||||||
"cloud_saves",
|
|
||||||
"download_manager",
|
|
||||||
"games",
|
|
||||||
"library",
|
|
||||||
"drop-consts",
|
|
||||||
]
|
|
||||||
|
|
||||||
resolver = "3"
|
|
||||||
|
|||||||
4862
src-tauri/client/Cargo.lock
generated
4862
src-tauri/client/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,13 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "client"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
bitcode = "0.6.7"
|
|
||||||
database = { version = "0.1.0", path = "../database" }
|
|
||||||
drop-consts = { version = "0.1.0", path = "../drop-consts" }
|
|
||||||
log = "0.4.28"
|
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
|
||||||
tauri = "2.8.5"
|
|
||||||
tauri-plugin-autostart = "2.5.0"
|
|
||||||
@ -1,42 +0,0 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use database::models::Game;
|
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
use crate::{app_status::AppStatus, user::User};
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct AppState {
|
|
||||||
status: AppStatus,
|
|
||||||
user: Option<User>,
|
|
||||||
games: HashMap<String, Game>,
|
|
||||||
}
|
|
||||||
impl AppState {
|
|
||||||
pub fn new(status: AppStatus, user: Option<User>, games: HashMap<String, Game>) -> Self {
|
|
||||||
Self {
|
|
||||||
status,
|
|
||||||
user,
|
|
||||||
games,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn status(&self) -> &AppStatus {
|
|
||||||
&self.status
|
|
||||||
}
|
|
||||||
pub fn status_mut(&mut self) -> &mut AppStatus {
|
|
||||||
&mut self.status
|
|
||||||
}
|
|
||||||
pub fn games(&self) -> &HashMap<String, Game> {
|
|
||||||
&self.games
|
|
||||||
}
|
|
||||||
pub fn games_mut(&mut self) -> &mut HashMap<String, Game> {
|
|
||||||
&mut self.games
|
|
||||||
}
|
|
||||||
pub fn user(&self) -> &Option<User> {
|
|
||||||
&self.user
|
|
||||||
}
|
|
||||||
pub fn user_mut(&mut self) -> &mut Option<User> {
|
|
||||||
&mut self.user
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,12 +0,0 @@
|
|||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Serialize, Eq, PartialEq)]
|
|
||||||
pub enum AppStatus {
|
|
||||||
NotConfigured,
|
|
||||||
Offline,
|
|
||||||
ServerError,
|
|
||||||
SignedOut,
|
|
||||||
SignedIn,
|
|
||||||
SignedInNeedsReauth,
|
|
||||||
ServerUnavailable,
|
|
||||||
}
|
|
||||||
@ -1,26 +0,0 @@
|
|||||||
use database::borrow_db_checked;
|
|
||||||
use log::debug;
|
|
||||||
use tauri::AppHandle;
|
|
||||||
use tauri_plugin_autostart::ManagerExt;
|
|
||||||
|
|
||||||
// New function to sync state on startup
|
|
||||||
pub fn sync_autostart_on_startup(app: &AppHandle) -> Result<(), String> {
|
|
||||||
let db_handle = borrow_db_checked();
|
|
||||||
let should_be_enabled = db_handle.settings.autostart;
|
|
||||||
drop(db_handle);
|
|
||||||
|
|
||||||
let manager = app.autolaunch();
|
|
||||||
let current_state = manager.is_enabled().map_err(|e| e.to_string())?;
|
|
||||||
|
|
||||||
if current_state != should_be_enabled {
|
|
||||||
if should_be_enabled {
|
|
||||||
manager.enable().map_err(|e| e.to_string())?;
|
|
||||||
debug!("synced autostart: enabled");
|
|
||||||
} else {
|
|
||||||
manager.disable().map_err(|e| e.to_string())?;
|
|
||||||
debug!("synced autostart: disabled");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@ -1,50 +0,0 @@
|
|||||||
use std::{
|
|
||||||
ffi::OsStr,
|
|
||||||
path::PathBuf,
|
|
||||||
process::{Command, Stdio},
|
|
||||||
sync::LazyLock,
|
|
||||||
};
|
|
||||||
|
|
||||||
use drop_consts::{UMU_BASE_LAUNCHER_EXECUTABLE, UMU_INSTALL_DIRS};
|
|
||||||
use log::info;
|
|
||||||
|
|
||||||
pub static COMPAT_INFO: LazyLock<Option<CompatInfo>> = LazyLock::new(create_new_compat_info);
|
|
||||||
|
|
||||||
pub static UMU_LAUNCHER_EXECUTABLE: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
|
|
||||||
let x = get_umu_executable();
|
|
||||||
info!("{:?}", &x);
|
|
||||||
x
|
|
||||||
});
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct CompatInfo {
|
|
||||||
pub umu_installed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_new_compat_info() -> Option<CompatInfo> {
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
return None;
|
|
||||||
|
|
||||||
let has_umu_installed = UMU_LAUNCHER_EXECUTABLE.is_some();
|
|
||||||
Some(CompatInfo {
|
|
||||||
umu_installed: has_umu_installed,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_umu_executable() -> Option<PathBuf> {
|
|
||||||
if check_executable_exists(UMU_BASE_LAUNCHER_EXECUTABLE) {
|
|
||||||
return Some(PathBuf::from(UMU_BASE_LAUNCHER_EXECUTABLE));
|
|
||||||
}
|
|
||||||
|
|
||||||
for dir in UMU_INSTALL_DIRS {
|
|
||||||
let p = PathBuf::from(dir).join(UMU_BASE_LAUNCHER_EXECUTABLE);
|
|
||||||
if check_executable_exists(&p) {
|
|
||||||
return Some(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
fn check_executable_exists<P: AsRef<OsStr>>(exec: P) -> bool {
|
|
||||||
let has_umu_installed = Command::new(exec).stdout(Stdio::null()).output();
|
|
||||||
has_umu_installed.is_ok()
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
pub mod app_state;
|
|
||||||
pub mod app_status;
|
|
||||||
pub mod autostart;
|
|
||||||
pub mod compat;
|
|
||||||
pub mod user;
|
|
||||||
@ -1,12 +0,0 @@
|
|||||||
use bitcode::{Decode, Encode};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Encode, Decode)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct User {
|
|
||||||
id: String,
|
|
||||||
username: String,
|
|
||||||
admin: bool,
|
|
||||||
display_name: String,
|
|
||||||
profile_picture_object_id: String,
|
|
||||||
}
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "cloud_saves"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
database = { version = "0.1.0", path = "../database" }
|
|
||||||
dirs = "6.0.0"
|
|
||||||
drop-consts = { version = "0.1.0", path = "../drop-consts" }
|
|
||||||
log = "0.4.28"
|
|
||||||
regex = "1.11.3"
|
|
||||||
rustix = "1.1.2"
|
|
||||||
serde = "1.0.228"
|
|
||||||
serde_json = "1.0.145"
|
|
||||||
serde_with = "3.15.0"
|
|
||||||
tar = "0.4.44"
|
|
||||||
tempfile = "3.23.0"
|
|
||||||
uuid = "1.18.1"
|
|
||||||
whoami = "1.6.1"
|
|
||||||
zstd = "0.13.3"
|
|
||||||
@ -1,234 +0,0 @@
|
|||||||
use std::{collections::HashMap, path::PathBuf, str::FromStr};
|
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
use database::platform::Platform;
|
|
||||||
use database::{db::DATA_ROOT_DIR, GameVersion};
|
|
||||||
use log::warn;
|
|
||||||
|
|
||||||
use crate::error::BackupError;
|
|
||||||
|
|
||||||
use super::path::CommonPath;
|
|
||||||
|
|
||||||
pub struct BackupManager<'a> {
|
|
||||||
pub current_platform: Platform,
|
|
||||||
pub sources: HashMap<(Platform, Platform), &'a (dyn BackupHandler + Sync + Send)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for BackupManager<'_> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BackupManager<'_> {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
BackupManager {
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
current_platform: Platform::Windows,
|
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
current_platform: Platform::macOS,
|
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
current_platform: Platform::Linux,
|
|
||||||
|
|
||||||
sources: HashMap::from([
|
|
||||||
// Current platform to target platform
|
|
||||||
(
|
|
||||||
(Platform::Windows, Platform::Windows),
|
|
||||||
&WindowsBackupManager {} as &(dyn BackupHandler + Sync + Send),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
(Platform::Linux, Platform::Linux),
|
|
||||||
&LinuxBackupManager {} as &(dyn BackupHandler + Sync + Send),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
(Platform::macOS, Platform::macOS),
|
|
||||||
&MacBackupManager {} as &(dyn BackupHandler + Sync + Send),
|
|
||||||
),
|
|
||||||
]),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait BackupHandler: Send + Sync {
|
|
||||||
fn root_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> {
|
|
||||||
Ok(DATA_ROOT_DIR.join("games"))
|
|
||||||
}
|
|
||||||
fn game_translate(&self, _path: &PathBuf, game: &GameVersion) -> Result<PathBuf, BackupError> {
|
|
||||||
Ok(PathBuf::from_str(&game.game_id).unwrap())
|
|
||||||
}
|
|
||||||
fn base_translate(&self, path: &PathBuf, game: &GameVersion) -> Result<PathBuf, BackupError> {
|
|
||||||
Ok(self
|
|
||||||
.root_translate(path, game)?
|
|
||||||
.join(self.game_translate(path, game)?))
|
|
||||||
}
|
|
||||||
fn home_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> {
|
|
||||||
let c = CommonPath::Home.get().ok_or(BackupError::NotFound);
|
|
||||||
println!("{:?}", c);
|
|
||||||
c
|
|
||||||
}
|
|
||||||
fn store_user_id_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
PathBuf::from_str(&game.game_id).map_err(|_| BackupError::ParseError)
|
|
||||||
}
|
|
||||||
fn os_user_name_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
Ok(PathBuf::from_str(&whoami::username()).unwrap())
|
|
||||||
}
|
|
||||||
fn win_app_data_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
warn!("Unexpected Windows Reference in Backup <winAppData>");
|
|
||||||
Err(BackupError::InvalidSystem)
|
|
||||||
}
|
|
||||||
fn win_local_app_data_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
warn!("Unexpected Windows Reference in Backup <winLocalAppData>");
|
|
||||||
Err(BackupError::InvalidSystem)
|
|
||||||
}
|
|
||||||
fn win_local_app_data_low_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
warn!("Unexpected Windows Reference in Backup <winLocalAppDataLow>");
|
|
||||||
Err(BackupError::InvalidSystem)
|
|
||||||
}
|
|
||||||
fn win_documents_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
warn!("Unexpected Windows Reference in Backup <winDocuments>");
|
|
||||||
Err(BackupError::InvalidSystem)
|
|
||||||
}
|
|
||||||
fn win_public_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
warn!("Unexpected Windows Reference in Backup <winPublic>");
|
|
||||||
Err(BackupError::InvalidSystem)
|
|
||||||
}
|
|
||||||
fn win_program_data_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
warn!("Unexpected Windows Reference in Backup <winProgramData>");
|
|
||||||
Err(BackupError::InvalidSystem)
|
|
||||||
}
|
|
||||||
fn win_dir_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
warn!("Unexpected Windows Reference in Backup <winDir>");
|
|
||||||
Err(BackupError::InvalidSystem)
|
|
||||||
}
|
|
||||||
fn xdg_data_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
warn!("Unexpected XDG Reference in Backup <xdgData>");
|
|
||||||
Err(BackupError::InvalidSystem)
|
|
||||||
}
|
|
||||||
fn xdg_config_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
warn!("Unexpected XDG Reference in Backup <xdgConfig>");
|
|
||||||
Err(BackupError::InvalidSystem)
|
|
||||||
}
|
|
||||||
fn skip_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> {
|
|
||||||
Ok(PathBuf::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct LinuxBackupManager {}
|
|
||||||
impl BackupHandler for LinuxBackupManager {
|
|
||||||
fn xdg_config_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
CommonPath::Data.get().ok_or(BackupError::NotFound)
|
|
||||||
}
|
|
||||||
fn xdg_data_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
CommonPath::Config.get().ok_or(BackupError::NotFound)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub struct WindowsBackupManager {}
|
|
||||||
impl BackupHandler for WindowsBackupManager {
|
|
||||||
fn win_app_data_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
CommonPath::Config.get().ok_or(BackupError::NotFound)
|
|
||||||
}
|
|
||||||
fn win_local_app_data_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
CommonPath::DataLocal.get().ok_or(BackupError::NotFound)
|
|
||||||
}
|
|
||||||
fn win_local_app_data_low_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
CommonPath::DataLocalLow
|
|
||||||
.get()
|
|
||||||
.ok_or(BackupError::NotFound)
|
|
||||||
}
|
|
||||||
fn win_dir_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
Ok(PathBuf::from_str("C:/Windows").unwrap())
|
|
||||||
}
|
|
||||||
fn win_documents_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
CommonPath::Document.get().ok_or(BackupError::NotFound)
|
|
||||||
}
|
|
||||||
fn win_program_data_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
Ok(PathBuf::from_str("C:/ProgramData").unwrap())
|
|
||||||
}
|
|
||||||
fn win_public_translate(
|
|
||||||
&self,
|
|
||||||
_path: &PathBuf,
|
|
||||||
_game: &GameVersion,
|
|
||||||
) -> Result<PathBuf, BackupError> {
|
|
||||||
CommonPath::Public.get().ok_or(BackupError::NotFound)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub struct MacBackupManager {}
|
|
||||||
impl BackupHandler for MacBackupManager {}
|
|
||||||
@ -1,27 +0,0 @@
|
|||||||
use std::fmt::Display;
|
|
||||||
|
|
||||||
use serde_with::SerializeDisplay;
|
|
||||||
|
|
||||||
#[derive(Debug, SerializeDisplay, Clone, Copy)]
|
|
||||||
|
|
||||||
pub enum BackupError {
|
|
||||||
InvalidSystem,
|
|
||||||
|
|
||||||
NotFound,
|
|
||||||
|
|
||||||
ParseError,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for BackupError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let s = match self {
|
|
||||||
BackupError::InvalidSystem => "Attempted to generate path for invalid system",
|
|
||||||
|
|
||||||
BackupError::NotFound => "Could not generate or find path",
|
|
||||||
|
|
||||||
BackupError::ParseError => "Failed to parse path",
|
|
||||||
};
|
|
||||||
|
|
||||||
write!(f, "{}", s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
pub mod backup_manager;
|
|
||||||
pub mod conditions;
|
|
||||||
pub mod error;
|
|
||||||
pub mod metadata;
|
|
||||||
pub mod normalise;
|
|
||||||
pub mod path;
|
|
||||||
pub mod placeholder;
|
|
||||||
pub mod resolver;
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "database"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
bitcode = "0.6.7"
|
|
||||||
chrono = "0.4.42"
|
|
||||||
dirs = "6.0.0"
|
|
||||||
drop-consts = { version = "0.1.0", path = "../drop-consts" }
|
|
||||||
log = "0.4.28"
|
|
||||||
native_model = { version = "0.6.4", features = ["rmp_serde_1_3"], git = "https://github.com/Drop-OSS/native_model.git"}
|
|
||||||
rustbreak = "2.0.0"
|
|
||||||
serde = "1.0.228"
|
|
||||||
serde_with = "3.15.0"
|
|
||||||
url = "2.5.7"
|
|
||||||
whoami = "1.6.1"
|
|
||||||
@ -1,39 +0,0 @@
|
|||||||
use std::{
|
|
||||||
path::PathBuf,
|
|
||||||
sync::{Arc, LazyLock},
|
|
||||||
};
|
|
||||||
|
|
||||||
use drop_consts::DATA_ROOT_PREFIX;
|
|
||||||
use rustbreak::{DeSerError, DeSerializer};
|
|
||||||
use serde::{Serialize, de::DeserializeOwned};
|
|
||||||
|
|
||||||
use crate::{interface::DatabaseImpls, models::DatabaseInterface};
|
|
||||||
|
|
||||||
pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(DatabaseInterface::set_up_database);
|
|
||||||
|
|
||||||
pub static DATA_ROOT_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
||||||
dirs::data_dir()
|
|
||||||
.expect("Failed to get data dir")
|
|
||||||
.join(DATA_ROOT_PREFIX)
|
|
||||||
});
|
|
||||||
|
|
||||||
// Custom JSON serializer to support everything we need
|
|
||||||
#[derive(Debug, Default, Clone)]
|
|
||||||
pub struct DropDatabaseSerializer;
|
|
||||||
|
|
||||||
impl<T: native_model::Model + Serialize + DeserializeOwned> DeSerializer<T>
|
|
||||||
for DropDatabaseSerializer
|
|
||||||
{
|
|
||||||
fn serialize(&self, val: &T) -> rustbreak::error::DeSerResult<Vec<u8>> {
|
|
||||||
native_model::encode(val).map_err(|e| DeSerError::Internal(e.to_string()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deserialize<R: std::io::Read>(&self, mut s: R) -> rustbreak::error::DeSerResult<T> {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
s.read_to_end(&mut buf)
|
|
||||||
.map_err(|e| rustbreak::error::DeSerError::Other(e.into()))?;
|
|
||||||
let (val, _version) =
|
|
||||||
native_model::decode(buf).map_err(|e| DeSerError::Internal(e.to_string()))?;
|
|
||||||
Ok(val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,14 +0,0 @@
|
|||||||
#![feature(nonpoison_rwlock)]
|
|
||||||
|
|
||||||
pub mod db;
|
|
||||||
pub mod debug;
|
|
||||||
pub mod interface;
|
|
||||||
pub mod models;
|
|
||||||
pub mod platform;
|
|
||||||
|
|
||||||
pub use db::DB;
|
|
||||||
pub use interface::{borrow_db_checked, borrow_db_mut_checked};
|
|
||||||
pub use models::{
|
|
||||||
ApplicationTransientStatus, Database, DatabaseApplications, DatabaseAuth, DownloadType,
|
|
||||||
DownloadableMetadata, GameDownloadStatus, GameVersion, Settings,
|
|
||||||
};
|
|
||||||
@ -1,110 +0,0 @@
|
|||||||
mod v1;
|
|
||||||
mod v2;
|
|
||||||
mod v3;
|
|
||||||
mod v4;
|
|
||||||
|
|
||||||
use std::{hash::Hash, path::PathBuf};
|
|
||||||
|
|
||||||
use native_model::native_model;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use crate::db::DropDatabaseSerializer;
|
|
||||||
|
|
||||||
|
|
||||||
// NOTE: Within each version, you should NEVER use these types.
|
|
||||||
// Declare it using the actual version that it is from, i.e. v1::Settings rather than just Settings from here
|
|
||||||
|
|
||||||
pub type GameVersion = v1::GameVersion;
|
|
||||||
pub type Database = v4::Database;
|
|
||||||
pub type Settings = v1::Settings;
|
|
||||||
pub type DatabaseAuth = v1::DatabaseAuth;
|
|
||||||
|
|
||||||
pub type GameDownloadStatus = v2::GameDownloadStatus;
|
|
||||||
pub type ApplicationTransientStatus = v1::ApplicationTransientStatus;
|
|
||||||
/**
|
|
||||||
* Need to be universally accessible by the ID, and the version is just a couple sprinkles on top
|
|
||||||
*/
|
|
||||||
pub type DownloadableMetadata = v1::DownloadableMetadata;
|
|
||||||
pub type DownloadType = v1::DownloadType;
|
|
||||||
pub type DatabaseApplications = v2::DatabaseApplications;
|
|
||||||
// pub type DatabaseCompatInfo = v2::DatabaseCompatInfo;
|
|
||||||
|
|
||||||
pub type Game = v1::Game;
|
|
||||||
|
|
||||||
impl Game {
|
|
||||||
pub fn id(&self) -> &String {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type DatabaseInterface =
|
|
||||||
rustbreak::Database<Database, rustbreak::backend::PathBackend, DropDatabaseSerializer>;
|
|
||||||
|
|
||||||
pub type LibraryMetadata = v1::LibraryMetadata;
|
|
||||||
pub type LibraryProviderMetadata = v1::LibraryProviderMetadata;
|
|
||||||
pub type ProviderType = v1::ProviderType;
|
|
||||||
|
|
||||||
pub type Collection = v1::Collection;
|
|
||||||
pub type CollectionObject = v1::CollectionObject;
|
|
||||||
|
|
||||||
impl PartialEq for DownloadableMetadata {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.id == other.id && self.download_type == other.download_type
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Hash for DownloadableMetadata {
|
|
||||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
||||||
self.id.hash(state);
|
|
||||||
self.download_type.hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl LibraryProviderMetadata {
|
|
||||||
pub fn provider(&self) -> &ProviderType {
|
|
||||||
&self.provider
|
|
||||||
}
|
|
||||||
pub fn id(&self) -> usize {
|
|
||||||
self.id
|
|
||||||
}
|
|
||||||
pub fn name(&self) -> &String {
|
|
||||||
&self.name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Database {
|
|
||||||
pub fn new<T: Into<PathBuf>>(
|
|
||||||
games_base_dir: T,
|
|
||||||
prev_database: Option<PathBuf>,
|
|
||||||
cache_dir: PathBuf,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
prev_database,
|
|
||||||
settings: Settings::default(),
|
|
||||||
cache_dir,
|
|
||||||
compat_info: None,
|
|
||||||
library: v1::LibraryMetadata { providers: vec![] },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl DatabaseAuth {
|
|
||||||
pub fn new(
|
|
||||||
private: String,
|
|
||||||
cert: String,
|
|
||||||
client_id: String,
|
|
||||||
web_token: Option<String>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
private,
|
|
||||||
cert,
|
|
||||||
client_id,
|
|
||||||
web_token,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LibraryMetadata {
|
|
||||||
pub fn providers(&self) -> &Vec<LibraryProviderMetadata> {
|
|
||||||
&self.providers
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,201 +0,0 @@
|
|||||||
use bitcode::{Decode, Encode};
|
|
||||||
use serde_with::serde_as;
|
|
||||||
use std::{collections::HashMap, path::PathBuf};
|
|
||||||
|
|
||||||
use crate::{models::v1, platform::Platform};
|
|
||||||
|
|
||||||
use super::{Deserialize, Serialize, native_model};
|
|
||||||
|
|
||||||
fn default_template() -> String {
|
|
||||||
"{}".to_owned()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
#[native_model(id = 2, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
pub struct GameVersion {
|
|
||||||
pub game_id: String,
|
|
||||||
pub version_name: String,
|
|
||||||
|
|
||||||
pub platform: Platform,
|
|
||||||
|
|
||||||
pub launch_command: String,
|
|
||||||
pub launch_args: Vec<String>,
|
|
||||||
#[serde(default = "default_template")]
|
|
||||||
pub launch_command_template: String,
|
|
||||||
|
|
||||||
pub setup_command: String,
|
|
||||||
pub setup_args: Vec<String>,
|
|
||||||
#[serde(default = "default_template")]
|
|
||||||
pub setup_command_template: String,
|
|
||||||
|
|
||||||
pub only_setup: bool,
|
|
||||||
|
|
||||||
pub version_index: usize,
|
|
||||||
pub delta: bool,
|
|
||||||
|
|
||||||
pub umu_id_override: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[serde_as]
|
|
||||||
#[derive(Serialize, Clone, Deserialize, Default)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
#[native_model(id = 3, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
pub struct DatabaseApplications {
|
|
||||||
pub install_dirs: Vec<PathBuf>,
|
|
||||||
// Guaranteed to exist if the game also exists in the app state map
|
|
||||||
pub game_statuses: HashMap<String, v1::GameDownloadStatus>,
|
|
||||||
pub game_versions: HashMap<String, HashMap<String, v1::GameVersion>>,
|
|
||||||
pub installed_game_version: HashMap<String, v1::DownloadableMetadata>,
|
|
||||||
|
|
||||||
#[serde(skip)]
|
|
||||||
pub transient_statuses: HashMap<v1::DownloadableMetadata, v1::ApplicationTransientStatus>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
#[native_model(id = 4, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
pub struct Settings {
|
|
||||||
pub autostart: bool,
|
|
||||||
pub max_download_threads: usize,
|
|
||||||
pub force_offline: bool, // ... other settings ...
|
|
||||||
}
|
|
||||||
impl Default for Settings {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
autostart: false,
|
|
||||||
max_download_threads: 4,
|
|
||||||
force_offline: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strings are version names for a particular game
|
|
||||||
#[derive(Serialize, Clone, Deserialize, Debug)]
|
|
||||||
#[serde(tag = "type")]
|
|
||||||
#[native_model(id = 5, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
pub enum GameDownloadStatus {
|
|
||||||
Remote {},
|
|
||||||
SetupRequired {
|
|
||||||
version_name: String,
|
|
||||||
install_dir: String,
|
|
||||||
},
|
|
||||||
Installed {
|
|
||||||
version_name: String,
|
|
||||||
install_dir: String,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stuff that shouldn't be synced to disk
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
|
||||||
pub enum ApplicationTransientStatus {
|
|
||||||
Queued { version_name: String },
|
|
||||||
Downloading { version_name: String },
|
|
||||||
Uninstalling {},
|
|
||||||
Updating { version_name: String },
|
|
||||||
Validating { version_name: String },
|
|
||||||
Running {},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Default, Encode, Decode)]
|
|
||||||
#[native_model(id = 6, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
pub struct DatabaseAuth {
|
|
||||||
pub private: String,
|
|
||||||
pub cert: String,
|
|
||||||
pub client_id: String,
|
|
||||||
pub web_token: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[native_model(id = 8, version = 1)]
|
|
||||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Clone, Copy)]
|
|
||||||
pub enum DownloadType {
|
|
||||||
Game,
|
|
||||||
Tool,
|
|
||||||
Dlc,
|
|
||||||
Mod,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[native_model(id = 7, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
#[derive(Debug, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct DownloadableMetadata {
|
|
||||||
pub id: String,
|
|
||||||
pub version: Option<String>,
|
|
||||||
pub download_type: v1::DownloadType,
|
|
||||||
}
|
|
||||||
impl DownloadableMetadata {
|
|
||||||
pub fn new(id: String, version: Option<String>, download_type: v1::DownloadType) -> Self {
|
|
||||||
Self {
|
|
||||||
id,
|
|
||||||
version,
|
|
||||||
download_type,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[native_model(id = 1, version = 1)]
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Default)]
|
|
||||||
pub struct Database {
|
|
||||||
#[serde(default)]
|
|
||||||
pub settings: Settings,
|
|
||||||
pub auth: Option<v1::DatabaseAuth>,
|
|
||||||
pub base_url: String,
|
|
||||||
pub applications: v1::DatabaseApplications,
|
|
||||||
pub prev_database: Option<PathBuf>,
|
|
||||||
pub cache_dir: PathBuf,
|
|
||||||
}
|
|
||||||
#[native_model(id = 15, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Encode, Decode, Default)]
|
|
||||||
pub struct LibraryMetadata {
|
|
||||||
pub(crate) providers: Vec<v1::LibraryProviderMetadata>
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[native_model(id = 11, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Encode, Decode)]
|
|
||||||
pub struct LibraryProviderMetadata {
|
|
||||||
pub(crate) id: usize,
|
|
||||||
pub(crate) name: String,
|
|
||||||
pub(crate) provider: v1::ProviderType
|
|
||||||
}
|
|
||||||
#[native_model(id = 10, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Encode, Decode)]
|
|
||||||
pub enum ProviderType {
|
|
||||||
Drop(v1::DatabaseAuth),
|
|
||||||
}
|
|
||||||
#[native_model(id = 12, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Encode, Decode)]
|
|
||||||
pub struct Game {
|
|
||||||
pub library_id: LibraryProviderMetadata,
|
|
||||||
pub(crate) id: String,
|
|
||||||
m_name: String,
|
|
||||||
m_short_description: String,
|
|
||||||
m_description: String,
|
|
||||||
// mDevelopers
|
|
||||||
// mPublishers
|
|
||||||
m_icon_object_id: String,
|
|
||||||
m_banner_object_id: String,
|
|
||||||
m_cover_object_id: String,
|
|
||||||
m_image_library_object_ids: Vec<String>,
|
|
||||||
m_image_carousel_object_ids: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[native_model(id = 13, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct Collection {
|
|
||||||
id: String,
|
|
||||||
name: String,
|
|
||||||
is_default: bool,
|
|
||||||
user_id: String,
|
|
||||||
entries: Vec<CollectionObject>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[native_model(id = 14, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct CollectionObject {
|
|
||||||
collection_id: String,
|
|
||||||
game_id: String,
|
|
||||||
game: Game,
|
|
||||||
}
|
|
||||||
@ -1,111 +0,0 @@
|
|||||||
use std::{collections::HashMap, path::PathBuf};
|
|
||||||
|
|
||||||
use serde_with::serde_as;
|
|
||||||
|
|
||||||
use super::{Deserialize, Serialize, native_model, v1};
|
|
||||||
|
|
||||||
#[native_model(id = 1, version = 2, with = native_model::rmp_serde_1_3::RmpSerde, from = v1::Database)]
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Default)]
|
|
||||||
pub struct Database {
|
|
||||||
#[serde(default)]
|
|
||||||
pub settings: v1::Settings,
|
|
||||||
pub auth: Option<v1::DatabaseAuth>,
|
|
||||||
pub base_url: String,
|
|
||||||
pub applications: v1::DatabaseApplications,
|
|
||||||
#[serde(skip)]
|
|
||||||
pub prev_database: Option<PathBuf>,
|
|
||||||
pub cache_dir: PathBuf,
|
|
||||||
pub compat_info: Option<DatabaseCompatInfo>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[native_model(id = 9, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Default)]
|
|
||||||
|
|
||||||
pub struct DatabaseCompatInfo {
|
|
||||||
pub umu_installed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<v1::Database> for Database {
|
|
||||||
fn from(value: v1::Database) -> Self {
|
|
||||||
Self {
|
|
||||||
settings: value.settings,
|
|
||||||
auth: value.auth,
|
|
||||||
base_url: value.base_url,
|
|
||||||
applications: value.applications,
|
|
||||||
prev_database: value.prev_database,
|
|
||||||
cache_dir: value.cache_dir,
|
|
||||||
compat_info: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Strings are version names for a particular game
|
|
||||||
#[derive(Serialize, Clone, Deserialize, Debug)]
|
|
||||||
#[serde(tag = "type")]
|
|
||||||
#[native_model(id = 5, version = 2, with = native_model::rmp_serde_1_3::RmpSerde, from = v1::GameDownloadStatus)]
|
|
||||||
pub enum GameDownloadStatus {
|
|
||||||
Remote {},
|
|
||||||
SetupRequired {
|
|
||||||
version_name: String,
|
|
||||||
install_dir: String,
|
|
||||||
},
|
|
||||||
Installed {
|
|
||||||
version_name: String,
|
|
||||||
install_dir: String,
|
|
||||||
},
|
|
||||||
PartiallyInstalled {
|
|
||||||
version_name: String,
|
|
||||||
install_dir: String,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
impl From<v1::GameDownloadStatus> for GameDownloadStatus {
|
|
||||||
fn from(value: v1::GameDownloadStatus) -> Self {
|
|
||||||
match value {
|
|
||||||
v1::GameDownloadStatus::Remote {} => Self::Remote {},
|
|
||||||
v1::GameDownloadStatus::SetupRequired {
|
|
||||||
version_name,
|
|
||||||
install_dir,
|
|
||||||
} => Self::SetupRequired {
|
|
||||||
version_name,
|
|
||||||
install_dir,
|
|
||||||
},
|
|
||||||
v1::GameDownloadStatus::Installed {
|
|
||||||
version_name,
|
|
||||||
install_dir,
|
|
||||||
} => Self::Installed {
|
|
||||||
version_name,
|
|
||||||
install_dir,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[serde_as]
|
|
||||||
#[derive(Serialize, Clone, Deserialize, Default)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
#[native_model(id = 3, version = 2, with = native_model::rmp_serde_1_3::RmpSerde, from=v1::DatabaseApplications)]
|
|
||||||
pub struct DatabaseApplications {
|
|
||||||
pub install_dirs: Vec<PathBuf>,
|
|
||||||
// Guaranteed to exist if the game also exists in the app state map
|
|
||||||
pub game_statuses: HashMap<String, GameDownloadStatus>,
|
|
||||||
|
|
||||||
pub game_versions: HashMap<String, HashMap<String, v1::GameVersion>>,
|
|
||||||
pub installed_game_version: HashMap<String, v1::DownloadableMetadata>,
|
|
||||||
|
|
||||||
#[serde(skip)]
|
|
||||||
pub transient_statuses:
|
|
||||||
HashMap<v1::DownloadableMetadata, v1::ApplicationTransientStatus>,
|
|
||||||
}
|
|
||||||
impl From<v1::DatabaseApplications> for DatabaseApplications {
|
|
||||||
fn from(value: v1::DatabaseApplications) -> Self {
|
|
||||||
Self {
|
|
||||||
game_statuses: value
|
|
||||||
.game_statuses
|
|
||||||
.into_iter()
|
|
||||||
.map(|x| (x.0, x.1.into()))
|
|
||||||
.collect::<HashMap<String, GameDownloadStatus>>(),
|
|
||||||
install_dirs: value.install_dirs,
|
|
||||||
game_versions: value.game_versions,
|
|
||||||
installed_game_version: value.installed_game_version,
|
|
||||||
transient_statuses: value.transient_statuses,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,32 +0,0 @@
|
|||||||
use std::{collections::HashMap, path::PathBuf};
|
|
||||||
|
|
||||||
use serde_with::serde_as;
|
|
||||||
|
|
||||||
use super::{Deserialize, Serialize, native_model, v1, v2};
|
|
||||||
#[native_model(id = 1, version = 3, with = native_model::rmp_serde_1_3::RmpSerde, from = v2::Database)]
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Default)]
|
|
||||||
pub struct Database {
|
|
||||||
#[serde(default)]
|
|
||||||
pub settings: v1::Settings,
|
|
||||||
pub auth: Option<v1::DatabaseAuth>,
|
|
||||||
pub base_url: String,
|
|
||||||
pub applications: v2::DatabaseApplications,
|
|
||||||
#[serde(skip)]
|
|
||||||
pub prev_database: Option<PathBuf>,
|
|
||||||
pub cache_dir: PathBuf,
|
|
||||||
pub compat_info: Option<v2::DatabaseCompatInfo>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<v2::Database> for Database {
|
|
||||||
fn from(value: v2::Database) -> Self {
|
|
||||||
Self {
|
|
||||||
settings: value.settings,
|
|
||||||
auth: value.auth,
|
|
||||||
base_url: value.base_url,
|
|
||||||
applications: value.applications.into(),
|
|
||||||
prev_database: value.prev_database,
|
|
||||||
cache_dir: value.cache_dir,
|
|
||||||
compat_info: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,43 +0,0 @@
|
|||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use native_model::native_model;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use crate::models::{
|
|
||||||
v1::{self, LibraryMetadata},
|
|
||||||
v2, v3,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[native_model(id = 1, version = 4, with = native_model::rmp_serde_1_3::RmpSerde, from = v3::Database)]
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Default)]
|
|
||||||
pub struct Database {
|
|
||||||
#[serde(default)]
|
|
||||||
pub settings: v1::Settings,
|
|
||||||
#[serde(skip)]
|
|
||||||
pub prev_database: Option<PathBuf>,
|
|
||||||
pub cache_dir: PathBuf,
|
|
||||||
pub compat_info: Option<v2::DatabaseCompatInfo>,
|
|
||||||
pub library: v1::LibraryMetadata,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<v3::Database> for Database {
|
|
||||||
fn from(value: v3::Database) -> Self {
|
|
||||||
Self {
|
|
||||||
settings: value.settings,
|
|
||||||
prev_database: value.prev_database,
|
|
||||||
cache_dir: value.cache_dir,
|
|
||||||
compat_info: value.compat_info,
|
|
||||||
library: v1::LibraryMetadata {
|
|
||||||
providers: if let Some(auth) = value.auth {
|
|
||||||
vec![v1::LibraryProviderMetadata {
|
|
||||||
id: 0,
|
|
||||||
name: String::from("Default"),
|
|
||||||
provider: v1::ProviderType::Drop(auth),
|
|
||||||
}]
|
|
||||||
} else {
|
|
||||||
vec![]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,46 +0,0 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Eq, Hash, PartialEq, Serialize, Deserialize, Clone, Copy, Debug)]
|
|
||||||
pub enum Platform {
|
|
||||||
Windows,
|
|
||||||
Linux,
|
|
||||||
macOS,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Platform {
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
pub const HOST: Platform = Self::Windows;
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
pub const HOST: Platform = Self::macOS;
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
pub const HOST: Platform = Self::Linux;
|
|
||||||
|
|
||||||
pub fn is_case_sensitive(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::Windows | Self::macOS => false,
|
|
||||||
Self::Linux => true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&str> for Platform {
|
|
||||||
fn from(value: &str) -> Self {
|
|
||||||
match value.to_lowercase().trim() {
|
|
||||||
"windows" => Self::Windows,
|
|
||||||
"linux" => Self::Linux,
|
|
||||||
"mac" | "macos" => Self::macOS,
|
|
||||||
_ => unimplemented!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<whoami::Platform> for Platform {
|
|
||||||
fn from(value: whoami::Platform) -> Self {
|
|
||||||
match value {
|
|
||||||
whoami::Platform::Windows => Platform::Windows,
|
|
||||||
whoami::Platform::Linux => Platform::Linux,
|
|
||||||
whoami::Platform::MacOS => Platform::macOS,
|
|
||||||
platform => unimplemented!("Playform {} is not supported", platform),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "download_manager"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
atomic-instant-full = "0.1.0"
|
|
||||||
database = { version = "0.1.0", path = "../database" }
|
|
||||||
humansize = "2.1.3"
|
|
||||||
log = "0.4.28"
|
|
||||||
parking_lot = "0.12.5"
|
|
||||||
remote = { version = "0.1.0", path = "../remote" }
|
|
||||||
serde = "1.0.228"
|
|
||||||
serde_with = "3.15.0"
|
|
||||||
tauri = "2.8.5"
|
|
||||||
throttle_my_fn = "0.2.6"
|
|
||||||
utils = { version = "0.1.0", path = "../utils" }
|
|
||||||
@ -1,80 +0,0 @@
|
|||||||
use humansize::{BINARY, format_size};
|
|
||||||
use std::{
|
|
||||||
fmt::{Display, Formatter},
|
|
||||||
io,
|
|
||||||
sync::{Arc, mpsc::SendError},
|
|
||||||
};
|
|
||||||
|
|
||||||
use remote::error::RemoteAccessError;
|
|
||||||
use serde_with::SerializeDisplay;
|
|
||||||
|
|
||||||
#[derive(SerializeDisplay)]
|
|
||||||
pub enum DownloadManagerError<T> {
|
|
||||||
IOError(io::Error),
|
|
||||||
SignalError(SendError<T>),
|
|
||||||
}
|
|
||||||
impl<T> Display for DownloadManagerError<T> {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
DownloadManagerError::IOError(error) => write!(f, "{error}"),
|
|
||||||
DownloadManagerError::SignalError(send_error) => write!(f, "{send_error}"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<T> From<SendError<T>> for DownloadManagerError<T> {
|
|
||||||
fn from(value: SendError<T>) -> Self {
|
|
||||||
DownloadManagerError::SignalError(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<T> From<io::Error> for DownloadManagerError<T> {
|
|
||||||
fn from(value: io::Error) -> Self {
|
|
||||||
DownloadManagerError::IOError(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Rename / separate from downloads
|
|
||||||
#[derive(Debug, SerializeDisplay)]
|
|
||||||
pub enum ApplicationDownloadError {
|
|
||||||
NotInitialized,
|
|
||||||
Communication(RemoteAccessError),
|
|
||||||
DiskFull(u64, u64),
|
|
||||||
#[allow(dead_code)]
|
|
||||||
Checksum,
|
|
||||||
Lock,
|
|
||||||
IoError(Arc<io::Error>),
|
|
||||||
DownloadError(RemoteAccessError),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for ApplicationDownloadError {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
ApplicationDownloadError::NotInitialized => {
|
|
||||||
write!(f, "Download not initalized, did something go wrong?")
|
|
||||||
}
|
|
||||||
ApplicationDownloadError::DiskFull(required, available) => write!(
|
|
||||||
f,
|
|
||||||
"Game requires {}, {} remaining left on disk.",
|
|
||||||
format_size(*required, BINARY),
|
|
||||||
format_size(*available, BINARY),
|
|
||||||
),
|
|
||||||
ApplicationDownloadError::Communication(error) => write!(f, "{error}"),
|
|
||||||
ApplicationDownloadError::Lock => write!(
|
|
||||||
f,
|
|
||||||
"failed to acquire lock. Something has gone very wrong internally. Please restart the application"
|
|
||||||
),
|
|
||||||
ApplicationDownloadError::Checksum => {
|
|
||||||
write!(f, "checksum failed to validate for download")
|
|
||||||
}
|
|
||||||
ApplicationDownloadError::IoError(error) => write!(f, "io error: {error}"),
|
|
||||||
ApplicationDownloadError::DownloadError(error) => {
|
|
||||||
write!(f, "Download failed with error {error:?}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<io::Error> for ApplicationDownloadError {
|
|
||||||
fn from(value: io::Error) -> Self {
|
|
||||||
ApplicationDownloadError::IoError(Arc::new(value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
use database::DownloadableMetadata;
|
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
use crate::download_manager_frontend::DownloadStatus;
|
|
||||||
|
|
||||||
#[derive(Serialize, Clone)]
|
|
||||||
pub struct QueueUpdateEventQueueData {
|
|
||||||
pub meta: DownloadableMetadata,
|
|
||||||
pub status: DownloadStatus,
|
|
||||||
pub progress: f64,
|
|
||||||
pub current: usize,
|
|
||||||
pub max: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Clone)]
|
|
||||||
pub struct QueueUpdateEvent {
|
|
||||||
pub queue: Vec<QueueUpdateEventQueueData>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Clone)]
|
|
||||||
pub struct StatsUpdateEvent {
|
|
||||||
pub speed: usize,
|
|
||||||
pub time: usize,
|
|
||||||
}
|
|
||||||
@ -1,44 +0,0 @@
|
|||||||
#![feature(duration_millis_float)]
|
|
||||||
#![feature(nonpoison_mutex)]
|
|
||||||
#![feature(sync_nonpoison)]
|
|
||||||
|
|
||||||
use std::{ops::Deref, sync::OnceLock};
|
|
||||||
|
|
||||||
use tauri::AppHandle;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
download_manager_builder::DownloadManagerBuilder, download_manager_frontend::DownloadManager,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod download_manager_builder;
|
|
||||||
pub mod download_manager_frontend;
|
|
||||||
pub mod downloadable;
|
|
||||||
pub mod error;
|
|
||||||
pub mod frontend_updates;
|
|
||||||
pub mod util;
|
|
||||||
|
|
||||||
pub static DOWNLOAD_MANAGER: DownloadManagerWrapper = DownloadManagerWrapper::new();
|
|
||||||
|
|
||||||
pub struct DownloadManagerWrapper(OnceLock<DownloadManager>);
|
|
||||||
impl DownloadManagerWrapper {
|
|
||||||
const fn new() -> Self {
|
|
||||||
DownloadManagerWrapper(OnceLock::new())
|
|
||||||
}
|
|
||||||
pub fn init(app_handle: AppHandle) {
|
|
||||||
DOWNLOAD_MANAGER
|
|
||||||
.0
|
|
||||||
.set(DownloadManagerBuilder::build(app_handle))
|
|
||||||
.expect("Failed to initialise download manager");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for DownloadManagerWrapper {
|
|
||||||
type Target = DownloadManager;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
match self.0.get() {
|
|
||||||
Some(download_manager) => download_manager,
|
|
||||||
None => unreachable!("Download manager should always be initialised"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "drop-consts"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
dirs = "6.0.0"
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
use std::{path::PathBuf, sync::LazyLock};
|
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
pub const DATA_ROOT_PREFIX: &str = "drop";
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
pub const DATA_ROOT_PREFIX: &str = "drop-debug";
|
|
||||||
|
|
||||||
pub const DROP_DATA_PATH: &str = ".dropdata";
|
|
||||||
|
|
||||||
pub const RETRY_COUNT: usize = 3;
|
|
||||||
|
|
||||||
pub const TARGET_BUCKET_SIZE: usize = 63 * 1000 * 1000;
|
|
||||||
pub const MAX_FILES_PER_BUCKET: usize = (1024 / 4) - 1;
|
|
||||||
|
|
||||||
pub const UMU_BASE_LAUNCHER_EXECUTABLE: &str = "umu-run";
|
|
||||||
pub const UMU_INSTALL_DIRS: [&str; 4] = ["/app/share", "/use/local/share", "/usr/share", "/opt"];
|
|
||||||
@ -1,29 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "games"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
atomic-instant-full = "0.1.0"
|
|
||||||
bitcode = "0.6.7"
|
|
||||||
boxcar = "0.2.14"
|
|
||||||
database = { version = "0.1.0", path = "../database" }
|
|
||||||
download_manager = { version = "0.1.0", path = "../download_manager" }
|
|
||||||
hex = "0.4.3"
|
|
||||||
log = "0.4.28"
|
|
||||||
md5 = "0.8.0"
|
|
||||||
rayon = "1.11.0"
|
|
||||||
remote = { version = "0.1.0", path = "../remote" }
|
|
||||||
reqwest = "0.12.23"
|
|
||||||
rustix = "1.1.2"
|
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
|
||||||
serde_with = "3.15.0"
|
|
||||||
sysinfo = "0.37.2"
|
|
||||||
tauri = "2.8.5"
|
|
||||||
throttle_my_fn = "0.2.6"
|
|
||||||
utils = { version = "0.1.0", path = "../utils" }
|
|
||||||
native_model = { version = "0.6.4", features = [
|
|
||||||
"rmp_serde_1_3",
|
|
||||||
], git = "https://github.com/Drop-OSS/native_model.git" }
|
|
||||||
serde_json = "1.0.145"
|
|
||||||
drop-consts = { version = "0.1.0", path = "../drop-consts" }
|
|
||||||
@ -1,29 +0,0 @@
|
|||||||
use std::fmt::Display;
|
|
||||||
|
|
||||||
use serde_with::SerializeDisplay;
|
|
||||||
|
|
||||||
#[derive(SerializeDisplay)]
|
|
||||||
pub enum LibraryError {
|
|
||||||
MetaNotFound(String),
|
|
||||||
VersionNotFound(String),
|
|
||||||
}
|
|
||||||
impl Display for LibraryError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"{}",
|
|
||||||
match self {
|
|
||||||
LibraryError::MetaNotFound(id) => {
|
|
||||||
format!(
|
|
||||||
"Could not locate any installed version of game ID {id} in the database"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
LibraryError::VersionNotFound(game_id) => {
|
|
||||||
format!(
|
|
||||||
"Could not locate any installed version for game id {game_id} in the database"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,278 +0,0 @@
|
|||||||
use database::{
|
|
||||||
borrow_db_checked, borrow_db_mut_checked, models::Game, ApplicationTransientStatus, Database, DownloadableMetadata, GameDownloadStatus, GameVersion
|
|
||||||
};
|
|
||||||
use log::{debug, error, warn};
|
|
||||||
use remote::{
|
|
||||||
auth::generate_authorization_header, error::RemoteAccessError, requests::generate_url,
|
|
||||||
utils::DROP_CLIENT_SYNC,
|
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::fs::remove_dir_all;
|
|
||||||
use std::thread::spawn;
|
|
||||||
use tauri::AppHandle;
|
|
||||||
use utils::app_emit;
|
|
||||||
|
|
||||||
use crate::state::{GameStatusManager, GameStatusWithTransient};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct FetchGameStruct {
|
|
||||||
game: Game,
|
|
||||||
status: GameStatusWithTransient,
|
|
||||||
version: Option<GameVersion>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FetchGameStruct {
|
|
||||||
pub fn new(game: Game, status: GameStatusWithTransient, version: Option<GameVersion>) -> Self {
|
|
||||||
Self {
|
|
||||||
game,
|
|
||||||
status,
|
|
||||||
version,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(serde::Serialize, Clone)]
|
|
||||||
pub struct GameUpdateEvent {
|
|
||||||
pub game_id: String,
|
|
||||||
pub status: (
|
|
||||||
Option<GameDownloadStatus>,
|
|
||||||
Option<ApplicationTransientStatus>,
|
|
||||||
),
|
|
||||||
pub version: Option<GameVersion>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called by:
|
|
||||||
* - on_cancel, when cancelled, for obvious reasons
|
|
||||||
* - when downloading, so if drop unexpectedly quits, we can resume the download. hidden by the "Downloading..." transient state, though
|
|
||||||
* - when scanning, to import the game
|
|
||||||
*/
|
|
||||||
pub fn set_partially_installed(
|
|
||||||
meta: &DownloadableMetadata,
|
|
||||||
install_dir: String,
|
|
||||||
app_handle: Option<&AppHandle>,
|
|
||||||
) {
|
|
||||||
set_partially_installed_db(&mut borrow_db_mut_checked(), meta, install_dir, app_handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_partially_installed_db(
|
|
||||||
db_lock: &mut Database,
|
|
||||||
meta: &DownloadableMetadata,
|
|
||||||
install_dir: String,
|
|
||||||
app_handle: Option<&AppHandle>,
|
|
||||||
) {
|
|
||||||
db_lock.applications.transient_statuses.remove(meta);
|
|
||||||
db_lock.applications.game_statuses.insert(
|
|
||||||
meta.id.clone(),
|
|
||||||
GameDownloadStatus::PartiallyInstalled {
|
|
||||||
version_name: meta.version.as_ref().unwrap().clone(),
|
|
||||||
install_dir,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
db_lock
|
|
||||||
.applications
|
|
||||||
.installed_game_version
|
|
||||||
.insert(meta.id.clone(), meta.clone());
|
|
||||||
|
|
||||||
if let Some(app_handle) = app_handle {
|
|
||||||
push_game_update(
|
|
||||||
app_handle,
|
|
||||||
&meta.id,
|
|
||||||
None,
|
|
||||||
GameStatusManager::fetch_state(&meta.id, db_lock),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn uninstall_game_logic(meta: DownloadableMetadata, app_handle: &AppHandle) {
|
|
||||||
debug!("triggered uninstall for agent");
|
|
||||||
let mut db_handle = borrow_db_mut_checked();
|
|
||||||
db_handle
|
|
||||||
.applications
|
|
||||||
.transient_statuses
|
|
||||||
.insert(meta.clone(), ApplicationTransientStatus::Uninstalling {});
|
|
||||||
|
|
||||||
push_game_update(
|
|
||||||
app_handle,
|
|
||||||
&meta.id,
|
|
||||||
None,
|
|
||||||
GameStatusManager::fetch_state(&meta.id, &db_handle),
|
|
||||||
);
|
|
||||||
|
|
||||||
let previous_state = db_handle.applications.game_statuses.get(&meta.id).cloned();
|
|
||||||
|
|
||||||
let previous_state = if let Some(state) = previous_state {
|
|
||||||
state
|
|
||||||
} else {
|
|
||||||
warn!("uninstall job doesn't have previous state, failing silently");
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some((_, install_dir)) = match previous_state {
|
|
||||||
GameDownloadStatus::Installed {
|
|
||||||
version_name,
|
|
||||||
install_dir,
|
|
||||||
} => Some((version_name, install_dir)),
|
|
||||||
GameDownloadStatus::SetupRequired {
|
|
||||||
version_name,
|
|
||||||
install_dir,
|
|
||||||
} => Some((version_name, install_dir)),
|
|
||||||
GameDownloadStatus::PartiallyInstalled {
|
|
||||||
version_name,
|
|
||||||
install_dir,
|
|
||||||
} => Some((version_name, install_dir)),
|
|
||||||
_ => None,
|
|
||||||
} {
|
|
||||||
db_handle
|
|
||||||
.applications
|
|
||||||
.transient_statuses
|
|
||||||
.insert(meta.clone(), ApplicationTransientStatus::Uninstalling {});
|
|
||||||
|
|
||||||
drop(db_handle);
|
|
||||||
|
|
||||||
let app_handle = app_handle.clone();
|
|
||||||
spawn(move || {
|
|
||||||
if let Err(e) = remove_dir_all(install_dir) {
|
|
||||||
error!("{e}");
|
|
||||||
} else {
|
|
||||||
let mut db_handle = borrow_db_mut_checked();
|
|
||||||
db_handle.applications.transient_statuses.remove(&meta);
|
|
||||||
db_handle
|
|
||||||
.applications
|
|
||||||
.installed_game_version
|
|
||||||
.remove(&meta.id);
|
|
||||||
db_handle
|
|
||||||
.applications
|
|
||||||
.game_statuses
|
|
||||||
.insert(meta.id.clone(), GameDownloadStatus::Remote {});
|
|
||||||
let _ = db_handle.applications.transient_statuses.remove(&meta);
|
|
||||||
|
|
||||||
push_game_update(
|
|
||||||
&app_handle,
|
|
||||||
&meta.id,
|
|
||||||
None,
|
|
||||||
GameStatusManager::fetch_state(&meta.id, &db_handle),
|
|
||||||
);
|
|
||||||
|
|
||||||
debug!("uninstalled game id {}", &meta.id);
|
|
||||||
app_emit!(&app_handle, "update_library", ());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
warn!("invalid previous state for uninstall, failing silently.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_current_meta(game_id: &String) -> Option<DownloadableMetadata> {
|
|
||||||
borrow_db_checked()
|
|
||||||
.applications
|
|
||||||
.installed_game_version
|
|
||||||
.get(game_id)
|
|
||||||
.cloned()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn on_game_complete(
|
|
||||||
meta: &DownloadableMetadata,
|
|
||||||
install_dir: String,
|
|
||||||
app_handle: &AppHandle,
|
|
||||||
) -> Result<(), RemoteAccessError> {
|
|
||||||
// Fetch game version information from remote
|
|
||||||
if meta.version.is_none() {
|
|
||||||
return Err(RemoteAccessError::GameNotFound(meta.id.clone()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let client = DROP_CLIENT_SYNC.clone();
|
|
||||||
let response = generate_url(
|
|
||||||
&["/api/v1/client/game/version"],
|
|
||||||
&[
|
|
||||||
("id", &meta.id),
|
|
||||||
("version", meta.version.as_ref().unwrap()),
|
|
||||||
],
|
|
||||||
)?;
|
|
||||||
let response = client
|
|
||||||
.get(response)
|
|
||||||
.header("Authorization", generate_authorization_header())
|
|
||||||
.send()?;
|
|
||||||
|
|
||||||
let game_version: GameVersion = response.json()?;
|
|
||||||
|
|
||||||
let mut handle = borrow_db_mut_checked();
|
|
||||||
handle
|
|
||||||
.applications
|
|
||||||
.game_versions
|
|
||||||
.entry(meta.id.clone())
|
|
||||||
.or_default()
|
|
||||||
.insert(meta.version.clone().unwrap(), game_version.clone());
|
|
||||||
handle
|
|
||||||
.applications
|
|
||||||
.installed_game_version
|
|
||||||
.insert(meta.id.clone(), meta.clone());
|
|
||||||
|
|
||||||
drop(handle);
|
|
||||||
|
|
||||||
let status = if game_version.setup_command.is_empty() {
|
|
||||||
GameDownloadStatus::Installed {
|
|
||||||
version_name: meta.version.clone().unwrap(),
|
|
||||||
install_dir,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
GameDownloadStatus::SetupRequired {
|
|
||||||
version_name: meta.version.clone().unwrap(),
|
|
||||||
install_dir,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut db_handle = borrow_db_mut_checked();
|
|
||||||
db_handle
|
|
||||||
.applications
|
|
||||||
.game_statuses
|
|
||||||
.insert(meta.id.clone(), status.clone());
|
|
||||||
drop(db_handle);
|
|
||||||
app_emit!(
|
|
||||||
app_handle,
|
|
||||||
&format!("update_game/{}", meta.id),
|
|
||||||
GameUpdateEvent {
|
|
||||||
game_id: meta.id.clone(),
|
|
||||||
status: (Some(status), None),
|
|
||||||
version: Some(game_version),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push_game_update(
|
|
||||||
app_handle: &AppHandle,
|
|
||||||
game_id: &String,
|
|
||||||
version: Option<GameVersion>,
|
|
||||||
status: GameStatusWithTransient,
|
|
||||||
) {
|
|
||||||
if let Some(GameDownloadStatus::Installed { .. } | GameDownloadStatus::SetupRequired { .. }) =
|
|
||||||
&status.0
|
|
||||||
&& version.is_none()
|
|
||||||
{
|
|
||||||
panic!("pushed game for installed game that doesn't have version information");
|
|
||||||
}
|
|
||||||
|
|
||||||
app_emit!(
|
|
||||||
app_handle,
|
|
||||||
&format!("update_game/{game_id}"),
|
|
||||||
GameUpdateEvent {
|
|
||||||
game_id: game_id.clone(),
|
|
||||||
status,
|
|
||||||
version,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct FrontendGameOptions {
|
|
||||||
launch_string: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FrontendGameOptions {
|
|
||||||
pub fn launch_string(&self) -> &String {
|
|
||||||
&self.launch_string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "library"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
async-trait = "0.1.89"
|
|
||||||
client = { version = "0.1.0", path = "../client" }
|
|
||||||
database = { version = "0.1.0", path = "../database" }
|
|
||||||
futures = "0.3.31"
|
|
||||||
itertools = "0.14.0"
|
|
||||||
log = "0.4.28"
|
|
||||||
remote = { version = "0.1.0", path = "../remote" }
|
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
|
||||||
serde_with = "3.15.0"
|
|
||||||
tauri = "2.8.5"
|
|
||||||
url = "2.5.7"
|
|
||||||
@ -1,134 +0,0 @@
|
|||||||
use std::sync::nonpoison::Mutex;
|
|
||||||
|
|
||||||
use async_trait::async_trait;
|
|
||||||
use client::app_state::AppState;
|
|
||||||
use database::{
|
|
||||||
DatabaseAuth, GameDownloadStatus, borrow_db_mut_checked,
|
|
||||||
models::{Collection, Game, LibraryProviderMetadata},
|
|
||||||
};
|
|
||||||
use log::warn;
|
|
||||||
use remote::{
|
|
||||||
auth::generate_authorization_header,
|
|
||||||
cache::{cache_object, get_cached_object, get_cached_object_db},
|
|
||||||
error::{DropServerError, RemoteAccessError},
|
|
||||||
requests::generate_url,
|
|
||||||
utils::DROP_CLIENT_ASYNC,
|
|
||||||
};
|
|
||||||
use url::Url;
|
|
||||||
|
|
||||||
use crate::{error::LibraryError, provider::LibraryProvider};
|
|
||||||
|
|
||||||
pub struct DropLibraryProvider {
|
|
||||||
metadata: LibraryProviderMetadata,
|
|
||||||
auth: DatabaseAuth,
|
|
||||||
base_url: Url,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DropLibraryProvider {
|
|
||||||
pub fn new(metadata: LibraryProviderMetadata, auth: DatabaseAuth, base_url: Url) -> Self {
|
|
||||||
Self {
|
|
||||||
metadata,
|
|
||||||
auth,
|
|
||||||
base_url,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[async_trait]
|
|
||||||
impl LibraryProvider for DropLibraryProvider {
|
|
||||||
async fn get_library(
|
|
||||||
&self,
|
|
||||||
state: &tauri::State<'_, Mutex<AppState>>,
|
|
||||||
) -> Result<Vec<Game>, LibraryError> {
|
|
||||||
// let do_hard_refresh = hard_fresh.unwrap_or(false);
|
|
||||||
if
|
|
||||||
/* !do_hard_refresh &&*/
|
|
||||||
let Ok(library) = get_cached_object("library") {
|
|
||||||
return Ok(library);
|
|
||||||
}
|
|
||||||
|
|
||||||
let client = DROP_CLIENT_ASYNC.clone();
|
|
||||||
let response = generate_url(&["/api/v1/client/user/library"], &[], self.base_url)?;
|
|
||||||
let response = client
|
|
||||||
.get(response)
|
|
||||||
.header("Authorization", generate_authorization_header(self.auth))
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.map_err(|e| LibraryError::FetchError(RemoteAccessError::FetchError(e.into())))?;
|
|
||||||
|
|
||||||
if response.status() != 200 {
|
|
||||||
let err = response.json().await.unwrap_or(DropServerError {
|
|
||||||
status_code: 500,
|
|
||||||
status_message: "Invalid response from server.".to_owned(),
|
|
||||||
});
|
|
||||||
warn!("{err:?}");
|
|
||||||
return Err(LibraryError::FetchError(
|
|
||||||
RemoteAccessError::InvalidResponse(err),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut games: Vec<Game> = response
|
|
||||||
.json()
|
|
||||||
.await
|
|
||||||
.map_err(|e| RemoteAccessError::FetchError(e.into()))?;
|
|
||||||
|
|
||||||
let mut handle = state.lock();
|
|
||||||
|
|
||||||
let mut db_handle = borrow_db_mut_checked();
|
|
||||||
|
|
||||||
for game in &games {
|
|
||||||
handle.games_mut().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 {});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
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) {
|
|
||||||
Ok(game) => game,
|
|
||||||
Err(err) => {
|
|
||||||
warn!(
|
|
||||||
"{} is installed, but encountered error fetching its error: {}.",
|
|
||||||
meta.id, err
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
games.push(game);
|
|
||||||
}
|
|
||||||
|
|
||||||
drop(handle);
|
|
||||||
drop(db_handle);
|
|
||||||
cache_object("library", &games)?;
|
|
||||||
|
|
||||||
Ok(games)
|
|
||||||
}
|
|
||||||
async fn get_collections(&self) -> Result<Vec<Collection>, LibraryError> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn install(&mut self, game_id: String) {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn uninstall(&mut self, game_id: String) {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn metadata(&self) -> LibraryProviderMetadata {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn fetch_library_logic(state: &Mutex<AppState>) -> Result<Vec<Game>, RemoteAccessError> {}
|
|
||||||
async fn fetch_library_logic_offline() -> Result<Vec<Game>, RemoteAccessError> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
@ -1 +0,0 @@
|
|||||||
pub mod drop;
|
|
||||||
@ -1,31 +0,0 @@
|
|||||||
use std::fmt::Display;
|
|
||||||
|
|
||||||
use database::models::LibraryProviderMetadata;
|
|
||||||
use remote::error::RemoteAccessError;
|
|
||||||
use serde_with::SerializeDisplay;
|
|
||||||
|
|
||||||
#[derive(Debug, SerializeDisplay)]
|
|
||||||
pub enum LibraryError {
|
|
||||||
ProviderConnection(ProviderError),
|
|
||||||
FetchError(RemoteAccessError)
|
|
||||||
}
|
|
||||||
#[derive(Debug, SerializeDisplay)]
|
|
||||||
pub struct ProviderError {
|
|
||||||
provider: LibraryProviderMetadata
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for LibraryError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Display for ProviderError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<RemoteAccessError> for LibraryError {
|
|
||||||
fn from(value: RemoteAccessError) -> Self {
|
|
||||||
LibraryError::FetchError(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,69 +0,0 @@
|
|||||||
#![feature(nonpoison_mutex)]
|
|
||||||
#![feature(sync_nonpoison)]
|
|
||||||
|
|
||||||
use std::sync::{LazyLock, nonpoison::Mutex};
|
|
||||||
|
|
||||||
use client::app_state::AppState;
|
|
||||||
use database::{borrow_db_checked, models::{Game, LibraryProviderMetadata, ProviderType}};
|
|
||||||
use futures::{StreamExt, future::join_all};
|
|
||||||
use itertools::Itertools;
|
|
||||||
|
|
||||||
use crate::{drop::drop::DropLibraryProvider, error::LibraryError, provider::LibraryProvider};
|
|
||||||
|
|
||||||
pub mod drop;
|
|
||||||
pub mod error;
|
|
||||||
pub mod provider;
|
|
||||||
|
|
||||||
pub static LIBRARY: LazyLock<Library> = LazyLock::new(Library::init);
|
|
||||||
|
|
||||||
pub struct Library {
|
|
||||||
providers: Vec<Box<dyn LibraryProvider>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Library {
|
|
||||||
pub fn init() -> Self {
|
|
||||||
let metadata = borrow_db_checked();
|
|
||||||
let library = &metadata.library;
|
|
||||||
let providers = library.providers().iter().map(|provider| {
|
|
||||||
Library::construct(provider)
|
|
||||||
}).collect();
|
|
||||||
Self {
|
|
||||||
providers
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn construct(provider: &LibraryProviderMetadata) -> Box<dyn LibraryProvider> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
pub async fn get_library(
|
|
||||||
&self,
|
|
||||||
state: &tauri::State<'_, Mutex<AppState>>,
|
|
||||||
) -> (Vec<Game>, Vec<LibraryError>) {
|
|
||||||
let res = join_all(
|
|
||||||
self.providers
|
|
||||||
.iter()
|
|
||||||
.map(|provider| provider.get_library(state)),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.into_iter()
|
|
||||||
.fold(
|
|
||||||
(Vec::new(), Vec::new()),
|
|
||||||
|(mut acc_ok, mut acc_err), res| {
|
|
||||||
match res {
|
|
||||||
Ok(games) => acc_ok.extend(games),
|
|
||||||
Err(e) => acc_err.push(e),
|
|
||||||
};
|
|
||||||
(acc_ok, acc_err)
|
|
||||||
},
|
|
||||||
);
|
|
||||||
res
|
|
||||||
}
|
|
||||||
pub fn add(&mut self, provider: LibraryProviderMetadata) {
|
|
||||||
let new_provider = Box::new(match provider.provider() {
|
|
||||||
ProviderType::Drop(_) => DropLibraryProvider::new(provider),
|
|
||||||
});
|
|
||||||
self.providers.push(new_provider);
|
|
||||||
}
|
|
||||||
pub fn remove(&mut self, id: usize) {
|
|
||||||
self.providers.retain(|v| v.metadata().id() != id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
use std::sync::nonpoison::Mutex;
|
|
||||||
|
|
||||||
use async_trait::async_trait;
|
|
||||||
use client::app_state::AppState;
|
|
||||||
use database::models::{Collection, Game, LibraryProviderMetadata};
|
|
||||||
|
|
||||||
use crate::error::LibraryError;
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
pub trait LibraryProvider: Sync + Send {
|
|
||||||
async fn get_library(&self, state: &tauri::State<'_, Mutex<AppState>>) -> Result<Vec<Game>, LibraryError>;
|
|
||||||
async fn get_collections(&self) -> Result<Vec<Collection>, LibraryError>;
|
|
||||||
fn install(&mut self, game_id: String);
|
|
||||||
fn uninstall(&mut self, game_id: String);
|
|
||||||
fn metadata(&self) -> LibraryProviderMetadata;
|
|
||||||
}
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "process"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
chrono = "0.4.42"
|
|
||||||
client = { version = "0.1.0", path = "../client" }
|
|
||||||
database = { version = "0.1.0", path = "../database" }
|
|
||||||
dynfmt = "0.1.5"
|
|
||||||
games = { version = "0.1.0", path = "../games" }
|
|
||||||
log = "0.4.28"
|
|
||||||
page_size = "0.6.0"
|
|
||||||
serde = "1.0.228"
|
|
||||||
serde_with = "3.15.0"
|
|
||||||
shared_child = "1.1.1"
|
|
||||||
tauri = "2.8.5"
|
|
||||||
tauri-plugin-opener = "2.5.0"
|
|
||||||
utils = { version = "0.1.0", path = "../utils" }
|
|
||||||
@ -1,41 +0,0 @@
|
|||||||
#![feature(nonpoison_mutex)]
|
|
||||||
#![feature(sync_nonpoison)]
|
|
||||||
|
|
||||||
use std::{
|
|
||||||
ops::Deref,
|
|
||||||
sync::{OnceLock, nonpoison::Mutex},
|
|
||||||
};
|
|
||||||
|
|
||||||
use tauri::AppHandle;
|
|
||||||
|
|
||||||
use crate::process_manager::ProcessManager;
|
|
||||||
|
|
||||||
pub static PROCESS_MANAGER: ProcessManagerWrapper = ProcessManagerWrapper::new();
|
|
||||||
|
|
||||||
pub mod error;
|
|
||||||
pub mod format;
|
|
||||||
pub mod process_handlers;
|
|
||||||
pub mod process_manager;
|
|
||||||
|
|
||||||
pub struct ProcessManagerWrapper(OnceLock<Mutex<ProcessManager<'static>>>);
|
|
||||||
impl ProcessManagerWrapper {
|
|
||||||
const fn new() -> Self {
|
|
||||||
ProcessManagerWrapper(OnceLock::new())
|
|
||||||
}
|
|
||||||
pub fn init(app_handle: AppHandle) {
|
|
||||||
PROCESS_MANAGER
|
|
||||||
.0
|
|
||||||
.set(Mutex::new(ProcessManager::new(app_handle)))
|
|
||||||
.unwrap_or_else(|_| panic!("Failed to initialise Process Manager")); // Using panic! here because we can't implement Debug
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Deref for ProcessManagerWrapper {
|
|
||||||
type Target = Mutex<ProcessManager<'static>>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
match self.0.get() {
|
|
||||||
Some(process_manager) => process_manager,
|
|
||||||
None => unreachable!("Download manager should always be initialised"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "remote"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
bitcode = "0.6.7"
|
|
||||||
chrono = "0.4.42"
|
|
||||||
client = { version = "0.1.0", path = "../client" }
|
|
||||||
database = { version = "0.1.0", path = "../database" }
|
|
||||||
droplet-rs = "0.7.3"
|
|
||||||
gethostname = "1.0.2"
|
|
||||||
hex = "0.4.3"
|
|
||||||
http = "1.3.1"
|
|
||||||
log = "0.4.28"
|
|
||||||
md5 = "0.8.0"
|
|
||||||
reqwest = "0.12.23"
|
|
||||||
reqwest-websocket = "0.5.1"
|
|
||||||
serde = "1.0.228"
|
|
||||||
serde_with = "3.15.0"
|
|
||||||
tauri = "2.8.5"
|
|
||||||
url = "2.5.7"
|
|
||||||
utils = { version = "0.1.0", path = "../utils" }
|
|
||||||
@ -1,133 +0,0 @@
|
|||||||
use std::{collections::HashMap, env};
|
|
||||||
|
|
||||||
use chrono::Utc;
|
|
||||||
use client::{app_status::AppStatus, user::User};
|
|
||||||
use database::{DatabaseAuth, interface::borrow_db_checked};
|
|
||||||
use droplet_rs::ssl::sign_nonce;
|
|
||||||
use gethostname::gethostname;
|
|
||||||
use log::{error, warn};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use url::Url;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
error::{DropServerError, RemoteAccessError},
|
|
||||||
requests::make_authenticated_get,
|
|
||||||
utils::DROP_CLIENT_SYNC,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{
|
|
||||||
cache::{cache_object, get_cached_object},
|
|
||||||
requests::generate_url,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
struct CapabilityConfiguration {}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
struct InitiateRequestBody {
|
|
||||||
name: String,
|
|
||||||
platform: String,
|
|
||||||
capabilities: HashMap<String, CapabilityConfiguration>,
|
|
||||||
mode: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct HandshakeRequestBody {
|
|
||||||
client_id: String,
|
|
||||||
token: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HandshakeRequestBody {
|
|
||||||
pub fn new(client_id: String, token: String) -> Self {
|
|
||||||
Self { client_id, token }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct HandshakeResponse {
|
|
||||||
private: String,
|
|
||||||
certificate: String,
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<HandshakeResponse> for DatabaseAuth {
|
|
||||||
fn from(value: HandshakeResponse) -> Self {
|
|
||||||
DatabaseAuth::new(value.private, value.certificate, value.id, None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_authorization_header(auth: DatabaseAuth) -> String {
|
|
||||||
let nonce = Utc::now().timestamp_millis().to_string();
|
|
||||||
|
|
||||||
let signature =
|
|
||||||
sign_nonce(auth.private, nonce.clone()).expect("Failed to generate authorisation header");
|
|
||||||
|
|
||||||
format!("Nonce {} {} {}", auth.client_id, nonce, signature)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fetch_user(auth: DatabaseAuth, base_url: Url) -> Result<User, RemoteAccessError> {
|
|
||||||
let response = make_authenticated_get(generate_url(&["/api/v1/client/user"], &[], base_url)?, auth).await?;
|
|
||||||
if response.status() != 200 {
|
|
||||||
let err: DropServerError = response.json().await?;
|
|
||||||
warn!("{err:?}");
|
|
||||||
|
|
||||||
if err.status_message == "Nonce expired" {
|
|
||||||
return Err(RemoteAccessError::OutOfSync);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Err(RemoteAccessError::InvalidResponse(err));
|
|
||||||
}
|
|
||||||
|
|
||||||
response
|
|
||||||
.json::<User>()
|
|
||||||
.await
|
|
||||||
.map_err(std::convert::Into::into)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn auth_initiate_logic(mode: String, base_url: Url) -> Result<String, RemoteAccessError> {
|
|
||||||
let hostname = gethostname();
|
|
||||||
|
|
||||||
let endpoint = base_url.join("/api/v1/client/auth/initiate")?;
|
|
||||||
let body = InitiateRequestBody {
|
|
||||||
name: format!("{} (Desktop)", hostname.display()),
|
|
||||||
platform: env::consts::OS.to_string(),
|
|
||||||
capabilities: HashMap::from([
|
|
||||||
("peerAPI".to_owned(), CapabilityConfiguration {}),
|
|
||||||
("cloudSaves".to_owned(), CapabilityConfiguration {}),
|
|
||||||
]),
|
|
||||||
mode,
|
|
||||||
};
|
|
||||||
|
|
||||||
let client = DROP_CLIENT_SYNC.clone();
|
|
||||||
let response = client.post(endpoint.to_string()).json(&body).send()?;
|
|
||||||
|
|
||||||
if response.status() != 200 {
|
|
||||||
let data: DropServerError = response.json()?;
|
|
||||||
error!("could not start handshake: {}", data.status_message);
|
|
||||||
|
|
||||||
return Err(RemoteAccessError::HandshakeFailed(data.status_message));
|
|
||||||
}
|
|
||||||
|
|
||||||
let response = response.text()?;
|
|
||||||
|
|
||||||
Ok(response)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn setup(auth: DatabaseAuth, base_url: Url) -> (AppStatus, Option<User>) {
|
|
||||||
let user_result = match fetch_user(auth, base_url).await {
|
|
||||||
Ok(data) => data,
|
|
||||||
Err(RemoteAccessError::FetchError(_)) => {
|
|
||||||
let user = get_cached_object::<User>("user").ok();
|
|
||||||
return (AppStatus::Offline, user);
|
|
||||||
}
|
|
||||||
Err(_) => return (AppStatus::SignedInNeedsReauth, None),
|
|
||||||
};
|
|
||||||
if let Err(e) = cache_object("user", &user_result) {
|
|
||||||
warn!("Could not cache user object with error {e}");
|
|
||||||
}
|
|
||||||
return (AppStatus::SignedIn, Some(user_result));
|
|
||||||
}
|
|
||||||
@ -1,85 +0,0 @@
|
|||||||
use database::{DB, DatabaseAuth, interface::DatabaseImpls};
|
|
||||||
use http::{Response, header::CONTENT_TYPE, response::Builder as ResponseBuilder};
|
|
||||||
use log::{debug, warn};
|
|
||||||
use tauri::UriSchemeResponder;
|
|
||||||
use url::Url;
|
|
||||||
|
|
||||||
use crate::{error::CacheError, utils::DROP_CLIENT_ASYNC};
|
|
||||||
|
|
||||||
use super::{
|
|
||||||
auth::generate_authorization_header,
|
|
||||||
cache::{ObjectCache, cache_object, get_cached_object},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub async fn fetch_object_wrapper(request: http::Request<Vec<u8>>, responder: UriSchemeResponder, auth: DatabaseAuth, base_url: Url) {
|
|
||||||
match fetch_object(request, auth, base_url).await {
|
|
||||||
Ok(r) => responder.respond(r),
|
|
||||||
Err(e) => {
|
|
||||||
warn!("Cache error: {e}");
|
|
||||||
responder.respond(
|
|
||||||
Response::builder()
|
|
||||||
.status(500)
|
|
||||||
.body(Vec::new())
|
|
||||||
.expect("Failed to build error response"),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fetch_object(
|
|
||||||
request: http::Request<Vec<u8>>,
|
|
||||||
auth: DatabaseAuth,
|
|
||||||
base_url: Url
|
|
||||||
) -> Result<Response<Vec<u8>>, CacheError> {
|
|
||||||
// Drop leading /
|
|
||||||
let object_id = &request.uri().path()[1..];
|
|
||||||
|
|
||||||
let cache_result = get_cached_object::<ObjectCache>(object_id);
|
|
||||||
if let Ok(cache_result) = &cache_result
|
|
||||||
&& !cache_result.has_expired()
|
|
||||||
{
|
|
||||||
return cache_result.try_into();
|
|
||||||
}
|
|
||||||
|
|
||||||
let header = generate_authorization_header(auth);
|
|
||||||
let client = DROP_CLIENT_ASYNC.clone();
|
|
||||||
let url = format!("{}api/v1/client/object/{object_id}", base_url);
|
|
||||||
let response = client.get(url).header("Authorization", header).send().await;
|
|
||||||
|
|
||||||
match response {
|
|
||||||
Ok(r) => {
|
|
||||||
let resp_builder = ResponseBuilder::new().header(
|
|
||||||
CONTENT_TYPE,
|
|
||||||
r.headers()
|
|
||||||
.get("Content-Type")
|
|
||||||
.expect("Failed get Content-Type header"),
|
|
||||||
);
|
|
||||||
let data = match r.bytes().await {
|
|
||||||
Ok(data) => Vec::from(data),
|
|
||||||
Err(e) => {
|
|
||||||
warn!("Could not get data from cache object {object_id} with error {e}",);
|
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let resp = resp_builder
|
|
||||||
.body(data)
|
|
||||||
.expect("Failed to build object cache response body");
|
|
||||||
if cache_result.map_or(true, |x| x.has_expired()) {
|
|
||||||
cache_object::<ObjectCache>(object_id, &resp.clone().try_into()?)
|
|
||||||
.expect("Failed to create cached object");
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(resp)
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
debug!("Object fetch failed with error {e}. Attempting to download from cache");
|
|
||||||
match cache_result {
|
|
||||||
Ok(cache_result) => cache_result.try_into(),
|
|
||||||
Err(e) => {
|
|
||||||
warn!("{e}");
|
|
||||||
Err(CacheError::Remote(e))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,102 +0,0 @@
|
|||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use database::{DatabaseAuth, borrow_db_checked};
|
|
||||||
use http::{Request, Response, StatusCode, Uri, uri::PathAndQuery};
|
|
||||||
use log::warn;
|
|
||||||
use tauri::UriSchemeResponder;
|
|
||||||
use url::Url;
|
|
||||||
use utils::webbrowser_open::webbrowser_open;
|
|
||||||
|
|
||||||
use crate::utils::DROP_CLIENT_SYNC;
|
|
||||||
|
|
||||||
pub async fn handle_server_proto_offline_wrapper(
|
|
||||||
request: Request<Vec<u8>>,
|
|
||||||
responder: UriSchemeResponder,
|
|
||||||
) {
|
|
||||||
responder.respond(match handle_server_proto_offline(request).await {
|
|
||||||
Ok(res) => res,
|
|
||||||
Err(_) => unreachable!(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handle_server_proto_offline(
|
|
||||||
_request: Request<Vec<u8>>,
|
|
||||||
) -> Result<Response<Vec<u8>>, StatusCode> {
|
|
||||||
Ok(Response::builder()
|
|
||||||
.status(StatusCode::NOT_FOUND)
|
|
||||||
.body(Vec::new())
|
|
||||||
.expect("Failed to build error response for proto offline"))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handle_server_proto_wrapper(request: Request<Vec<u8>>, responder: UriSchemeResponder, auth: DatabaseAuth, base_url: Url) {
|
|
||||||
match handle_server_proto(request, auth, base_url).await {
|
|
||||||
Ok(r) => responder.respond(r),
|
|
||||||
Err(e) => {
|
|
||||||
warn!("Cache error: {e}");
|
|
||||||
responder.respond(
|
|
||||||
Response::builder()
|
|
||||||
.status(e)
|
|
||||||
.body(Vec::new())
|
|
||||||
.expect("Failed to build error response"),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_server_proto(
|
|
||||||
request: Request<Vec<u8>>,
|
|
||||||
auth: DatabaseAuth,
|
|
||||||
base_url: Url,
|
|
||||||
) -> Result<Response<Vec<u8>>, StatusCode> {
|
|
||||||
let web_token = match &auth.web_token {
|
|
||||||
Some(token) => token,
|
|
||||||
None => return Err(StatusCode::UNAUTHORIZED),
|
|
||||||
};
|
|
||||||
let remote_uri = base_url.as_str().parse::<Uri>().expect("Failed to parse base url");
|
|
||||||
|
|
||||||
let path = request.uri().path();
|
|
||||||
|
|
||||||
let mut new_uri = request.uri().clone().into_parts();
|
|
||||||
new_uri.path_and_query = Some(
|
|
||||||
PathAndQuery::from_str(&format!("{path}?noWrapper=true"))
|
|
||||||
.expect("Failed to parse request path in proto"),
|
|
||||||
);
|
|
||||||
new_uri.authority = remote_uri.authority().cloned();
|
|
||||||
new_uri.scheme = remote_uri.scheme().cloned();
|
|
||||||
let err_msg = &format!("Failed to build new uri from parts {new_uri:?}");
|
|
||||||
let new_uri = Uri::from_parts(new_uri).expect(err_msg);
|
|
||||||
|
|
||||||
let whitelist_prefix = ["/store", "/api", "/_", "/fonts"];
|
|
||||||
|
|
||||||
if whitelist_prefix.iter().all(|f| !path.starts_with(f)) {
|
|
||||||
webbrowser_open(new_uri.to_string());
|
|
||||||
return Ok(Response::new(Vec::new()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let client = DROP_CLIENT_SYNC.clone();
|
|
||||||
let response = match client
|
|
||||||
.request(request.method().clone(), new_uri.to_string())
|
|
||||||
.header("Authorization", format!("Bearer {web_token}"))
|
|
||||||
.headers(request.headers().clone())
|
|
||||||
.send()
|
|
||||||
{
|
|
||||||
Ok(response) => response,
|
|
||||||
Err(e) => {
|
|
||||||
warn!("Could not send response. Got {e} when sending");
|
|
||||||
return Err(e.status().unwrap_or(StatusCode::BAD_REQUEST));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let response_status = response.status();
|
|
||||||
let response_body = match response.bytes() {
|
|
||||||
Ok(bytes) => bytes,
|
|
||||||
Err(e) => return Err(e.status().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)),
|
|
||||||
};
|
|
||||||
|
|
||||||
let http_response = Response::builder()
|
|
||||||
.status(response_status)
|
|
||||||
.body(response_body.to_vec())
|
|
||||||
.expect("Failed to build server proto response");
|
|
||||||
|
|
||||||
Ok(http_response)
|
|
||||||
}
|
|
||||||
@ -1,119 +0,0 @@
|
|||||||
use std::{
|
|
||||||
fs::{self, File},
|
|
||||||
io::Read,
|
|
||||||
sync::LazyLock,
|
|
||||||
};
|
|
||||||
|
|
||||||
use database::db::DATA_ROOT_DIR;
|
|
||||||
use log::{debug, info, warn};
|
|
||||||
use reqwest::Certificate;
|
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct DropHealthcheck {
|
|
||||||
app_name: String,
|
|
||||||
}
|
|
||||||
impl DropHealthcheck {
|
|
||||||
pub fn app_name(&self) -> &String {
|
|
||||||
&self.app_name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static DROP_CERT_BUNDLE: LazyLock<Vec<Certificate>> = LazyLock::new(fetch_certificates);
|
|
||||||
pub static DROP_CLIENT_SYNC: LazyLock<reqwest::blocking::Client> = LazyLock::new(get_client_sync);
|
|
||||||
pub static DROP_CLIENT_ASYNC: LazyLock<reqwest::Client> = LazyLock::new(get_client_async);
|
|
||||||
pub static DROP_CLIENT_WS_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(get_client_ws);
|
|
||||||
|
|
||||||
fn fetch_certificates() -> Vec<Certificate> {
|
|
||||||
let certificate_dir = DATA_ROOT_DIR.join("certificates");
|
|
||||||
|
|
||||||
let mut certs = Vec::new();
|
|
||||||
match fs::read_dir(certificate_dir) {
|
|
||||||
Ok(c) => {
|
|
||||||
for entry in c {
|
|
||||||
match entry {
|
|
||||||
Ok(c) => {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
match File::open(c.path()) {
|
|
||||||
Ok(f) => f,
|
|
||||||
Err(e) => {
|
|
||||||
warn!(
|
|
||||||
"Failed to open file at {} with error {}",
|
|
||||||
c.path().display(),
|
|
||||||
e
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.read_to_end(&mut buf)
|
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"Failed to read to end of certificate file {} with error {}",
|
|
||||||
c.path().display(),
|
|
||||||
e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
match Certificate::from_pem_bundle(&buf) {
|
|
||||||
Ok(certificates) => {
|
|
||||||
for cert in certificates {
|
|
||||||
certs.push(cert);
|
|
||||||
}
|
|
||||||
info!(
|
|
||||||
"added {} certificate(s) from {}",
|
|
||||||
certs.len(),
|
|
||||||
c.file_name().display()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Err(e) => warn!(
|
|
||||||
"Invalid certificate file {} with error {}",
|
|
||||||
c.path().display(),
|
|
||||||
e
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => todo!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
debug!("not loading certificates due to error: {e}");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
certs
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_client_sync() -> reqwest::blocking::Client {
|
|
||||||
let mut client = reqwest::blocking::ClientBuilder::new();
|
|
||||||
|
|
||||||
for cert in DROP_CERT_BUNDLE.iter() {
|
|
||||||
client = client.add_root_certificate(cert.clone());
|
|
||||||
}
|
|
||||||
client
|
|
||||||
.use_rustls_tls()
|
|
||||||
.build()
|
|
||||||
.expect("Failed to build synchronous client")
|
|
||||||
}
|
|
||||||
pub fn get_client_async() -> reqwest::Client {
|
|
||||||
let mut client = reqwest::ClientBuilder::new();
|
|
||||||
|
|
||||||
for cert in DROP_CERT_BUNDLE.iter() {
|
|
||||||
client = client.add_root_certificate(cert.clone());
|
|
||||||
}
|
|
||||||
client
|
|
||||||
.use_rustls_tls()
|
|
||||||
.build()
|
|
||||||
.expect("Failed to build asynchronous client")
|
|
||||||
}
|
|
||||||
pub fn get_client_ws() -> reqwest::Client {
|
|
||||||
let mut client = reqwest::ClientBuilder::new();
|
|
||||||
|
|
||||||
for cert in DROP_CERT_BUNDLE.iter() {
|
|
||||||
client = client.add_root_certificate(cert.clone());
|
|
||||||
}
|
|
||||||
client
|
|
||||||
.use_rustls_tls()
|
|
||||||
.http1_only()
|
|
||||||
.build()
|
|
||||||
.expect("Failed to build websocket client")
|
|
||||||
}
|
|
||||||
@ -1,81 +0,0 @@
|
|||||||
use std::sync::nonpoison::Mutex;
|
|
||||||
|
|
||||||
use client::app_state::AppState;
|
|
||||||
use database::{borrow_db_checked, borrow_db_mut_checked};
|
|
||||||
use download_manager::DOWNLOAD_MANAGER;
|
|
||||||
use log::{debug, error};
|
|
||||||
use tauri::AppHandle;
|
|
||||||
use tauri_plugin_autostart::ManagerExt;
|
|
||||||
use tauri_plugin_opener::OpenerExt;
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn fetch_state(state: tauri::State<'_, Mutex<AppState>>) -> Result<String, String> {
|
|
||||||
let guard = state.lock();
|
|
||||||
let cloned_state = serde_json::to_string(&guard.clone()).map_err(|e| e.to_string())?;
|
|
||||||
drop(guard);
|
|
||||||
Ok(cloned_state)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn quit(app: tauri::AppHandle) {
|
|
||||||
cleanup_and_exit(&app);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn cleanup_and_exit(app: &AppHandle) {
|
|
||||||
debug!("cleaning up and exiting application");
|
|
||||||
match DOWNLOAD_MANAGER.ensure_terminated() {
|
|
||||||
Ok(res) => match res {
|
|
||||||
Ok(()) => debug!("download manager terminated correctly"),
|
|
||||||
Err(()) => error!("download manager failed to terminate correctly"),
|
|
||||||
},
|
|
||||||
Err(e) => panic!("{e:?}"),
|
|
||||||
}
|
|
||||||
|
|
||||||
app.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn toggle_autostart(app: AppHandle, enabled: bool) -> Result<(), String> {
|
|
||||||
let manager = app.autolaunch();
|
|
||||||
if enabled {
|
|
||||||
manager.enable().map_err(|e| e.to_string())?;
|
|
||||||
debug!("enabled autostart");
|
|
||||||
} else {
|
|
||||||
manager.disable().map_err(|e| e.to_string())?;
|
|
||||||
debug!("eisabled autostart");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store the state in DB
|
|
||||||
let mut db_handle = borrow_db_mut_checked();
|
|
||||||
db_handle.settings.autostart = enabled;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn get_autostart_enabled(app: AppHandle) -> Result<bool, tauri_plugin_autostart::Error> {
|
|
||||||
let db_handle = borrow_db_checked();
|
|
||||||
let db_state = db_handle.settings.autostart;
|
|
||||||
drop(db_handle);
|
|
||||||
|
|
||||||
// Get actual system state
|
|
||||||
let manager = app.autolaunch();
|
|
||||||
let system_state = manager.is_enabled()?;
|
|
||||||
|
|
||||||
// If they don't match, sync to DB state
|
|
||||||
if db_state != system_state {
|
|
||||||
if db_state {
|
|
||||||
manager.enable()?;
|
|
||||||
} else {
|
|
||||||
manager.disable()?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(db_state)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn open_fs(path: String, app_handle: AppHandle) -> Result<(), tauri_plugin_opener::Error> {
|
|
||||||
app_handle
|
|
||||||
.opener()
|
|
||||||
.open_path(path, None::<&str>)
|
|
||||||
}
|
|
||||||
75
src-tauri/src/client/autostart.rs
Normal file
75
src-tauri/src/client/autostart.rs
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
use crate::database::db::{borrow_db_checked, borrow_db_mut_checked};
|
||||||
|
use log::debug;
|
||||||
|
use tauri::AppHandle;
|
||||||
|
use tauri_plugin_autostart::ManagerExt;
|
||||||
|
|
||||||
|
pub fn toggle_autostart_logic(app: AppHandle, enabled: bool) -> Result<(), String> {
|
||||||
|
let manager = app.autolaunch();
|
||||||
|
if enabled {
|
||||||
|
manager.enable().map_err(|e| e.to_string())?;
|
||||||
|
debug!("enabled autostart");
|
||||||
|
} else {
|
||||||
|
manager.disable().map_err(|e| e.to_string())?;
|
||||||
|
debug!("eisabled autostart");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the state in DB
|
||||||
|
let mut db_handle = borrow_db_mut_checked();
|
||||||
|
db_handle.settings.autostart = enabled;
|
||||||
|
drop(db_handle);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_autostart_enabled_logic(app: AppHandle) -> Result<bool, tauri_plugin_autostart::Error> {
|
||||||
|
// First check DB state
|
||||||
|
let db_handle = borrow_db_checked();
|
||||||
|
let db_state = db_handle.settings.autostart;
|
||||||
|
drop(db_handle);
|
||||||
|
|
||||||
|
// Get actual system state
|
||||||
|
let manager = app.autolaunch();
|
||||||
|
let system_state = manager.is_enabled()?;
|
||||||
|
|
||||||
|
// If they don't match, sync to DB state
|
||||||
|
if db_state != system_state {
|
||||||
|
if db_state {
|
||||||
|
manager.enable()?;
|
||||||
|
} else {
|
||||||
|
manager.disable()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(db_state)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New function to sync state on startup
|
||||||
|
pub fn sync_autostart_on_startup(app: &AppHandle) -> Result<(), String> {
|
||||||
|
let db_handle = borrow_db_checked();
|
||||||
|
let should_be_enabled = db_handle.settings.autostart;
|
||||||
|
drop(db_handle);
|
||||||
|
|
||||||
|
let manager = app.autolaunch();
|
||||||
|
let current_state = manager.is_enabled().map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
if current_state != should_be_enabled {
|
||||||
|
if should_be_enabled {
|
||||||
|
manager.enable().map_err(|e| e.to_string())?;
|
||||||
|
debug!("synced autostart: enabled");
|
||||||
|
} else {
|
||||||
|
manager.disable().map_err(|e| e.to_string())?;
|
||||||
|
debug!("synced autostart: disabled");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn toggle_autostart(app: AppHandle, enabled: bool) -> Result<(), String> {
|
||||||
|
toggle_autostart_logic(app, enabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn get_autostart_enabled(app: AppHandle) -> Result<bool, tauri_plugin_autostart::Error> {
|
||||||
|
get_autostart_enabled_logic(app)
|
||||||
|
}
|
||||||
23
src-tauri/src/client/cleanup.rs
Normal file
23
src-tauri/src/client/cleanup.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
use log::{debug, error};
|
||||||
|
use tauri::AppHandle;
|
||||||
|
|
||||||
|
use crate::AppState;
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn quit(app: tauri::AppHandle, state: tauri::State<'_, std::sync::Mutex<AppState<'_>>>) {
|
||||||
|
cleanup_and_exit(&app, &state);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cleanup_and_exit(app: &AppHandle, state: &tauri::State<'_, std::sync::Mutex<AppState<'_>>>) {
|
||||||
|
debug!("cleaning up and exiting application");
|
||||||
|
let download_manager = state.lock().unwrap().download_manager.clone();
|
||||||
|
match download_manager.ensure_terminated() {
|
||||||
|
Ok(res) => match res {
|
||||||
|
Ok(()) => debug!("download manager terminated correctly"),
|
||||||
|
Err(()) => error!("download manager failed to terminate correctly"),
|
||||||
|
},
|
||||||
|
Err(e) => panic!("{e:?}"),
|
||||||
|
}
|
||||||
|
|
||||||
|
app.exit(0);
|
||||||
|
}
|
||||||
11
src-tauri/src/client/commands.rs
Normal file
11
src-tauri/src/client/commands.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use crate::AppState;
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn fetch_state(
|
||||||
|
state: tauri::State<'_, std::sync::Mutex<AppState<'_>>>,
|
||||||
|
) -> Result<String, String> {
|
||||||
|
let guard = state.lock().unwrap();
|
||||||
|
let cloned_state = serde_json::to_string(&guard.clone()).map_err(|e| e.to_string())?;
|
||||||
|
drop(guard);
|
||||||
|
Ok(cloned_state)
|
||||||
|
}
|
||||||
3
src-tauri/src/client/mod.rs
Normal file
3
src-tauri/src/client/mod.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub mod autostart;
|
||||||
|
pub mod cleanup;
|
||||||
|
pub mod commands;
|
||||||
102
src-tauri/src/cloud_saves/backup_manager.rs
Normal file
102
src-tauri/src/cloud_saves/backup_manager.rs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
use std::{collections::HashMap, path::PathBuf, str::FromStr};
|
||||||
|
|
||||||
|
use log::warn;
|
||||||
|
|
||||||
|
use crate::{database::db::{GameVersion, DATA_ROOT_DIR}, error::backup_error::BackupError, process::process_manager::Platform};
|
||||||
|
|
||||||
|
use super::path::CommonPath;
|
||||||
|
|
||||||
|
pub struct BackupManager<'a> {
|
||||||
|
pub current_platform: Platform,
|
||||||
|
pub sources: HashMap<(Platform, Platform), &'a (dyn BackupHandler + Sync + Send)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BackupManager<'_> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
BackupManager {
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
current_platform: Platform::Windows,
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
current_platform: Platform::MacOs,
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
current_platform: Platform::Linux,
|
||||||
|
|
||||||
|
sources: HashMap::from([
|
||||||
|
// Current platform to target platform
|
||||||
|
(
|
||||||
|
(Platform::Windows, Platform::Windows),
|
||||||
|
&WindowsBackupManager {} as &(dyn BackupHandler + Sync + Send),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(Platform::Linux, Platform::Linux),
|
||||||
|
&LinuxBackupManager {} as &(dyn BackupHandler + Sync + Send),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(Platform::MacOs, Platform::MacOs),
|
||||||
|
&MacBackupManager {} as &(dyn BackupHandler + Sync + Send),
|
||||||
|
),
|
||||||
|
|
||||||
|
]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait BackupHandler: Send + Sync {
|
||||||
|
fn root_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { Ok(DATA_ROOT_DIR.lock().unwrap().join("games")) }
|
||||||
|
fn game_translate(&self, _path: &PathBuf, game: &GameVersion) -> Result<PathBuf, BackupError> { Ok(PathBuf::from_str(&game.game_id).unwrap()) }
|
||||||
|
fn base_translate(&self, path: &PathBuf, game: &GameVersion) -> Result<PathBuf, BackupError> { Ok(self.root_translate(path, game)?.join(self.game_translate(path, game)?)) }
|
||||||
|
fn home_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { let c = CommonPath::Home.get().ok_or(BackupError::NotFound); println!("{:?}", c); c }
|
||||||
|
fn store_user_id_translate(&self, _path: &PathBuf, game: &GameVersion) -> Result<PathBuf, BackupError> { PathBuf::from_str(&game.game_id).map_err(|_| BackupError::ParseError) }
|
||||||
|
fn os_user_name_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { Ok(PathBuf::from_str(&whoami::username()).unwrap()) }
|
||||||
|
fn win_app_data_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { warn!("Unexpected Windows Reference in Backup <winAppData>"); Err(BackupError::InvalidSystem) }
|
||||||
|
fn win_local_app_data_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { warn!("Unexpected Windows Reference in Backup <winLocalAppData>"); Err(BackupError::InvalidSystem) }
|
||||||
|
fn win_local_app_data_low_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { warn!("Unexpected Windows Reference in Backup <winLocalAppDataLow>"); Err(BackupError::InvalidSystem) }
|
||||||
|
fn win_documents_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { warn!("Unexpected Windows Reference in Backup <winDocuments>"); Err(BackupError::InvalidSystem) }
|
||||||
|
fn win_public_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { warn!("Unexpected Windows Reference in Backup <winPublic>"); Err(BackupError::InvalidSystem) }
|
||||||
|
fn win_program_data_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { warn!("Unexpected Windows Reference in Backup <winProgramData>"); Err(BackupError::InvalidSystem) }
|
||||||
|
fn win_dir_translate(&self, _path: &PathBuf,_game: &GameVersion) -> Result<PathBuf, BackupError> { warn!("Unexpected Windows Reference in Backup <winDir>"); Err(BackupError::InvalidSystem) }
|
||||||
|
fn xdg_data_translate(&self, _path: &PathBuf,_game: &GameVersion) -> Result<PathBuf, BackupError> { warn!("Unexpected XDG Reference in Backup <xdgData>"); Err(BackupError::InvalidSystem) }
|
||||||
|
fn xdg_config_translate(&self, _path: &PathBuf,_game: &GameVersion) -> Result<PathBuf, BackupError> { warn!("Unexpected XDG Reference in Backup <xdgConfig>"); Err(BackupError::InvalidSystem) }
|
||||||
|
fn skip_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> { Ok(PathBuf::new()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LinuxBackupManager {}
|
||||||
|
impl BackupHandler for LinuxBackupManager {
|
||||||
|
fn xdg_config_translate(&self, _path: &PathBuf,_game: &GameVersion) -> Result<PathBuf, BackupError> {
|
||||||
|
Ok(CommonPath::Data.get().ok_or(BackupError::NotFound)?)
|
||||||
|
}
|
||||||
|
fn xdg_data_translate(&self, _path: &PathBuf,_game: &GameVersion) -> Result<PathBuf, BackupError> {
|
||||||
|
Ok(CommonPath::Config.get().ok_or(BackupError::NotFound)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub struct WindowsBackupManager {}
|
||||||
|
impl BackupHandler for WindowsBackupManager {
|
||||||
|
fn win_app_data_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> {
|
||||||
|
Ok(CommonPath::Config.get().ok_or(BackupError::NotFound)?)
|
||||||
|
}
|
||||||
|
fn win_local_app_data_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> {
|
||||||
|
Ok(CommonPath::DataLocal.get().ok_or(BackupError::NotFound)?)
|
||||||
|
}
|
||||||
|
fn win_local_app_data_low_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> {
|
||||||
|
Ok(CommonPath::DataLocalLow.get().ok_or(BackupError::NotFound)?)
|
||||||
|
}
|
||||||
|
fn win_dir_translate(&self, _path: &PathBuf,_game: &GameVersion) -> Result<PathBuf, BackupError> {
|
||||||
|
Ok(PathBuf::from_str("C:/Windows").unwrap())
|
||||||
|
}
|
||||||
|
fn win_documents_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> {
|
||||||
|
Ok(CommonPath::Document.get().ok_or(BackupError::NotFound)?)
|
||||||
|
|
||||||
|
}
|
||||||
|
fn win_program_data_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> {
|
||||||
|
Ok(PathBuf::from_str("C:/ProgramData").unwrap())
|
||||||
|
}
|
||||||
|
fn win_public_translate(&self, _path: &PathBuf, _game: &GameVersion) -> Result<PathBuf, BackupError> {
|
||||||
|
Ok(CommonPath::Public.get().ok_or(BackupError::NotFound)?)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub struct MacBackupManager {}
|
||||||
|
impl BackupHandler for MacBackupManager {}
|
||||||
@ -1,7 +1,6 @@
|
|||||||
use database::platform::Platform;
|
use crate::process::process_manager::Platform;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
pub enum Condition {
|
pub enum Condition {
|
||||||
Os(Platform),
|
Os(Platform)
|
||||||
Other
|
|
||||||
}
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
use database::GameVersion;
|
use crate::database::db::GameVersion;
|
||||||
|
|
||||||
|
use super::conditions::{Condition};
|
||||||
|
|
||||||
use super::conditions::Condition;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct CloudSaveMetadata {
|
pub struct CloudSaveMetadata {
|
||||||
@ -15,17 +16,15 @@ pub struct GameFile {
|
|||||||
pub id: Option<String>,
|
pub id: Option<String>,
|
||||||
pub data_type: DataType,
|
pub data_type: DataType,
|
||||||
pub tags: Vec<Tag>,
|
pub tags: Vec<Tag>,
|
||||||
pub conditions: Vec<Condition>,
|
pub conditions: Vec<Condition>
|
||||||
}
|
}
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
|
||||||
pub enum DataType {
|
pub enum DataType {
|
||||||
Registry,
|
Registry,
|
||||||
File,
|
File,
|
||||||
Other,
|
Other
|
||||||
}
|
}
|
||||||
#[derive(
|
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
|
||||||
Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
|
|
||||||
)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub enum Tag {
|
pub enum Tag {
|
||||||
Config,
|
Config,
|
||||||
@ -33,4 +32,4 @@ pub enum Tag {
|
|||||||
#[default]
|
#[default]
|
||||||
#[serde(other)]
|
#[serde(other)]
|
||||||
Other,
|
Other,
|
||||||
}
|
}
|
||||||
@ -1,10 +1,11 @@
|
|||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use database::platform::Platform;
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use crate::process::process_manager::Platform;
|
||||||
|
|
||||||
use super::placeholder::*;
|
use super::placeholder::*;
|
||||||
|
|
||||||
|
|
||||||
pub fn normalize(path: &str, os: Platform) -> String {
|
pub fn normalize(path: &str, os: Platform) -> String {
|
||||||
let mut path = path.trim().trim_end_matches(['/', '\\']).replace('\\', "/");
|
let mut path = path.trim().trim_end_matches(['/', '\\']).replace('\\', "/");
|
||||||
|
|
||||||
@ -13,25 +14,18 @@ pub fn normalize(path: &str, os: Platform) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static CONSECUTIVE_SLASHES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"/{2,}").unwrap());
|
static CONSECUTIVE_SLASHES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"/{2,}").unwrap());
|
||||||
static UNNECESSARY_DOUBLE_STAR_1: LazyLock<Regex> =
|
static UNNECESSARY_DOUBLE_STAR_1: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"([^/*])\*{2,}").unwrap());
|
||||||
LazyLock::new(|| Regex::new(r"([^/*])\*{2,}").unwrap());
|
static UNNECESSARY_DOUBLE_STAR_2: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\*{2,}([^/*])").unwrap());
|
||||||
static UNNECESSARY_DOUBLE_STAR_2: LazyLock<Regex> =
|
|
||||||
LazyLock::new(|| Regex::new(r"\*{2,}([^/*])").unwrap());
|
|
||||||
static ENDING_WILDCARD: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(/\*)+$").unwrap());
|
static ENDING_WILDCARD: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(/\*)+$").unwrap());
|
||||||
static ENDING_DOT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(/\.)$").unwrap());
|
static ENDING_DOT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(/\.)$").unwrap());
|
||||||
static INTERMEDIATE_DOT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(/\./)").unwrap());
|
static INTERMEDIATE_DOT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(/\./)").unwrap());
|
||||||
static BLANK_SEGMENT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(/\s+/)").unwrap());
|
static BLANK_SEGMENT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(/\s+/)").unwrap());
|
||||||
static APP_DATA: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)%appdata%").unwrap());
|
static APP_DATA: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)%appdata%").unwrap());
|
||||||
static APP_DATA_ROAMING: LazyLock<Regex> =
|
static APP_DATA_ROAMING: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)%userprofile%/AppData/Roaming").unwrap());
|
||||||
LazyLock::new(|| Regex::new(r"(?i)%userprofile%/AppData/Roaming").unwrap());
|
static APP_DATA_LOCAL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)%localappdata%").unwrap());
|
||||||
static APP_DATA_LOCAL: LazyLock<Regex> =
|
static APP_DATA_LOCAL_2: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)%userprofile%/AppData/Local/").unwrap());
|
||||||
LazyLock::new(|| Regex::new(r"(?i)%localappdata%").unwrap());
|
static USER_PROFILE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)%userprofile%").unwrap());
|
||||||
static APP_DATA_LOCAL_2: LazyLock<Regex> =
|
static DOCUMENTS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)%userprofile%/Documents").unwrap());
|
||||||
LazyLock::new(|| Regex::new(r"(?i)%userprofile%/AppData/Local/").unwrap());
|
|
||||||
static USER_PROFILE: LazyLock<Regex> =
|
|
||||||
LazyLock::new(|| Regex::new(r"(?i)%userprofile%").unwrap());
|
|
||||||
static DOCUMENTS: LazyLock<Regex> =
|
|
||||||
LazyLock::new(|| Regex::new(r"(?i)%userprofile%/Documents").unwrap());
|
|
||||||
|
|
||||||
for (pattern, replacement) in [
|
for (pattern, replacement) in [
|
||||||
(&CONSECUTIVE_SLASHES, "/"),
|
(&CONSECUTIVE_SLASHES, "/"),
|
||||||
@ -72,9 +66,7 @@ pub fn normalize(path: &str, os: Platform) -> String {
|
|||||||
|
|
||||||
fn too_broad(path: &str) -> bool {
|
fn too_broad(path: &str) -> bool {
|
||||||
println!("Path: {}", path);
|
println!("Path: {}", path);
|
||||||
use {
|
use {BASE, HOME, ROOT, STORE_USER_ID, WIN_APP_DATA, WIN_DIR, WIN_DOCUMENTS, XDG_CONFIG, XDG_DATA};
|
||||||
BASE, HOME, ROOT, STORE_USER_ID, WIN_APP_DATA, WIN_DIR, WIN_DOCUMENTS, XDG_CONFIG, XDG_DATA,
|
|
||||||
};
|
|
||||||
|
|
||||||
let path_lower = path.to_lowercase();
|
let path_lower = path.to_lowercase();
|
||||||
|
|
||||||
@ -85,9 +77,7 @@ fn too_broad(path: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for item in AVOID_WILDCARDS {
|
for item in AVOID_WILDCARDS {
|
||||||
if path.starts_with(&format!("{}/*", item))
|
if path.starts_with(&format!("{}/*", item)) || path.starts_with(&format!("{}/{}", item, STORE_USER_ID)) {
|
||||||
|| path.starts_with(&format!("{}/{}", item, STORE_USER_ID))
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -134,6 +124,7 @@ fn too_broad(path: &str) -> bool {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Drive letters:
|
// Drive letters:
|
||||||
let drives: Regex = Regex::new(r"^[a-zA-Z]:$").unwrap();
|
let drives: Regex = Regex::new(r"^[a-zA-Z]:$").unwrap();
|
||||||
@ -168,4 +159,4 @@ pub fn usable(path: &str) -> bool {
|
|||||||
&& !path.starts_with("../")
|
&& !path.starts_with("../")
|
||||||
&& !too_broad(path)
|
&& !too_broad(path)
|
||||||
&& !unprintable.is_match(path)
|
&& !unprintable.is_match(path)
|
||||||
}
|
}
|
||||||
@ -13,12 +13,12 @@ pub enum CommonPath {
|
|||||||
|
|
||||||
impl CommonPath {
|
impl CommonPath {
|
||||||
pub fn get(&self) -> Option<PathBuf> {
|
pub fn get(&self) -> Option<PathBuf> {
|
||||||
static CONFIG: LazyLock<Option<PathBuf>> = LazyLock::new(dirs::config_dir);
|
static CONFIG: LazyLock<Option<PathBuf>> = LazyLock::new(|| dirs::config_dir());
|
||||||
static DATA: LazyLock<Option<PathBuf>> = LazyLock::new(dirs::data_dir);
|
static DATA: LazyLock<Option<PathBuf>> = LazyLock::new(|| dirs::data_dir());
|
||||||
static DATA_LOCAL: LazyLock<Option<PathBuf>> = LazyLock::new(dirs::data_local_dir);
|
static DATA_LOCAL: LazyLock<Option<PathBuf>> = LazyLock::new(|| dirs::data_local_dir());
|
||||||
static DOCUMENT: LazyLock<Option<PathBuf>> = LazyLock::new(dirs::document_dir);
|
static DOCUMENT: LazyLock<Option<PathBuf>> = LazyLock::new(|| dirs::document_dir());
|
||||||
static HOME: LazyLock<Option<PathBuf>> = LazyLock::new(dirs::home_dir);
|
static HOME: LazyLock<Option<PathBuf>> = LazyLock::new(|| dirs::home_dir());
|
||||||
static PUBLIC: LazyLock<Option<PathBuf>> = LazyLock::new(dirs::public_dir);
|
static PUBLIC: LazyLock<Option<PathBuf>> = LazyLock::new(|| dirs::public_dir());
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
static DATA_LOCAL_LOW: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
|
static DATA_LOCAL_LOW: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
|
||||||
@ -48,4 +48,4 @@ pub const XDG_DATA: &str = "<xdgData>"; // %WINDIR% on Windows
|
|||||||
pub const XDG_CONFIG: &str = "<xdgConfig>"; // $XDG_DATA_HOME on Linux
|
pub const XDG_CONFIG: &str = "<xdgConfig>"; // $XDG_DATA_HOME on Linux
|
||||||
pub const SKIP: &str = "<skip>"; // $XDG_CONFIG_HOME on Linux
|
pub const SKIP: &str = "<skip>"; // $XDG_CONFIG_HOME on Linux
|
||||||
|
|
||||||
pub static OS_USERNAME: LazyLock<String> = LazyLock::new(whoami::username);
|
pub static OS_USERNAME: LazyLock<String> = LazyLock::new(|| whoami::username());
|
||||||
@ -1,17 +1,22 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fs::{self, File, create_dir_all},
|
fs::{self, create_dir_all, File},
|
||||||
io::{self, Read, Write},
|
io::{self, ErrorKind, Read, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
thread::sleep,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::error::BackupError;
|
use super::{
|
||||||
|
backup_manager::BackupHandler, conditions::Condition, metadata::GameFile, placeholder::*,
|
||||||
use super::{backup_manager::BackupHandler, placeholder::*};
|
};
|
||||||
use database::GameVersion;
|
|
||||||
use log::{debug, warn};
|
use log::{debug, warn};
|
||||||
use rustix::path::Arg;
|
use rustix::path::Arg;
|
||||||
use tempfile::tempfile;
|
use tempfile::tempfile;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
database::db::GameVersion, error::backup_error::BackupError, process::process_manager::Platform,
|
||||||
|
};
|
||||||
|
|
||||||
use super::{backup_manager::BackupManager, metadata::CloudSaveMetadata, normalise::normalize};
|
use super::{backup_manager::BackupManager, metadata::CloudSaveMetadata, normalise::normalize};
|
||||||
|
|
||||||
pub fn resolve(meta: &mut CloudSaveMetadata) -> File {
|
pub fn resolve(meta: &mut CloudSaveMetadata) -> File {
|
||||||
@ -26,7 +31,7 @@ pub fn resolve(meta: &mut CloudSaveMetadata) -> File {
|
|||||||
.iter()
|
.iter()
|
||||||
.find_map(|p| match p {
|
.find_map(|p| match p {
|
||||||
super::conditions::Condition::Os(os) => Some(os),
|
super::conditions::Condition::Os(os) => Some(os),
|
||||||
_ => None
|
_ => None,
|
||||||
})
|
})
|
||||||
.cloned()
|
.cloned()
|
||||||
{
|
{
|
||||||
@ -59,7 +64,7 @@ pub fn resolve(meta: &mut CloudSaveMetadata) -> File {
|
|||||||
let binding = serde_json::to_string(meta).unwrap();
|
let binding = serde_json::to_string(meta).unwrap();
|
||||||
let serialized = binding.as_bytes();
|
let serialized = binding.as_bytes();
|
||||||
let mut file = tempfile().unwrap();
|
let mut file = tempfile().unwrap();
|
||||||
file.write_all(serialized).unwrap();
|
file.write(serialized).unwrap();
|
||||||
tarball.append_file("metadata", &mut file).unwrap();
|
tarball.append_file("metadata", &mut file).unwrap();
|
||||||
tarball.into_inner().unwrap().finish().unwrap()
|
tarball.into_inner().unwrap().finish().unwrap()
|
||||||
}
|
}
|
||||||
@ -92,7 +97,7 @@ pub fn extract(file: PathBuf) -> Result<(), BackupError> {
|
|||||||
.iter()
|
.iter()
|
||||||
.find_map(|p| match p {
|
.find_map(|p| match p {
|
||||||
super::conditions::Condition::Os(os) => Some(os),
|
super::conditions::Condition::Os(os) => Some(os),
|
||||||
_ => None
|
_ => None,
|
||||||
})
|
})
|
||||||
.cloned()
|
.cloned()
|
||||||
{
|
{
|
||||||
@ -111,7 +116,7 @@ pub fn extract(file: PathBuf) -> Result<(), BackupError> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let new_path = parse_path(file.path.into(), handler, &manifest.game_version)?;
|
let new_path = parse_path(file.path.into(), handler, &manifest.game_version)?;
|
||||||
create_dir_all(new_path.parent().unwrap()).unwrap();
|
create_dir_all(&new_path.parent().unwrap()).unwrap();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"Current path {:?} copying to {:?}",
|
"Current path {:?} copying to {:?}",
|
||||||
@ -128,22 +133,23 @@ pub fn copy_item<P: AsRef<Path>>(src: P, dest: P) -> io::Result<()> {
|
|||||||
let src_path = src.as_ref();
|
let src_path = src.as_ref();
|
||||||
let dest_path = dest.as_ref();
|
let dest_path = dest.as_ref();
|
||||||
|
|
||||||
let metadata = fs::metadata(src_path)?;
|
let metadata = fs::metadata(&src_path)?;
|
||||||
|
|
||||||
if metadata.is_file() {
|
if metadata.is_file() {
|
||||||
// Ensure the parent directory of the destination exists for a file copy
|
// Ensure the parent directory of the destination exists for a file copy
|
||||||
if let Some(parent) = dest_path.parent() {
|
if let Some(parent) = dest_path.parent() {
|
||||||
fs::create_dir_all(parent)?;
|
fs::create_dir_all(parent)?;
|
||||||
}
|
}
|
||||||
fs::copy(src_path, dest_path)?;
|
fs::copy(&src_path, &dest_path)?;
|
||||||
} else if metadata.is_dir() {
|
} else if metadata.is_dir() {
|
||||||
// For directories, we call the recursive helper function.
|
// For directories, we call the recursive helper function.
|
||||||
// The destination for the recursive copy is the `dest_path` itself.
|
// The destination for the recursive copy is the `dest_path` itself.
|
||||||
copy_dir_recursive(src_path, dest_path)?;
|
copy_dir_recursive(&src_path, &dest_path)?;
|
||||||
} else {
|
} else {
|
||||||
// Handle other file types like symlinks if necessary,
|
// Handle other file types like symlinks if necessary,
|
||||||
// for now, return an error or skip.
|
// for now, return an error or skip.
|
||||||
return Err(io::Error::other(
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::Other,
|
||||||
format!("Source {:?} is neither a file nor a directory", src_path),
|
format!("Source {:?} is neither a file nor a directory", src_path),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -152,7 +158,7 @@ pub fn copy_item<P: AsRef<Path>>(src: P, dest: P) -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn copy_dir_recursive(src: &Path, dest: &Path) -> io::Result<()> {
|
fn copy_dir_recursive(src: &Path, dest: &Path) -> io::Result<()> {
|
||||||
fs::create_dir_all(dest)?;
|
fs::create_dir_all(&dest)?;
|
||||||
|
|
||||||
for entry in fs::read_dir(src)? {
|
for entry in fs::read_dir(src)? {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
@ -214,3 +220,43 @@ pub fn parse_path(
|
|||||||
println!("Final line: {:?}", &s);
|
println!("Final line: {:?}", &s);
|
||||||
Ok(s)
|
Ok(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn test() {
|
||||||
|
let mut meta = CloudSaveMetadata {
|
||||||
|
files: vec![
|
||||||
|
GameFile {
|
||||||
|
path: String::from("<home>/favicon.png"),
|
||||||
|
id: None,
|
||||||
|
data_type: super::metadata::DataType::File,
|
||||||
|
tags: Vec::new(),
|
||||||
|
conditions: vec![Condition::Os(Platform::Linux)],
|
||||||
|
},
|
||||||
|
GameFile {
|
||||||
|
path: String::from("<home>/Documents/Pixel Art"),
|
||||||
|
id: None,
|
||||||
|
data_type: super::metadata::DataType::File,
|
||||||
|
tags: Vec::new(),
|
||||||
|
conditions: vec![Condition::Os(Platform::Linux)],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
game_version: GameVersion {
|
||||||
|
game_id: String::new(),
|
||||||
|
version_name: String::new(),
|
||||||
|
platform: Platform::Linux,
|
||||||
|
launch_command: String::new(),
|
||||||
|
launch_args: Vec::new(),
|
||||||
|
launch_command_template: String::new(),
|
||||||
|
setup_command: String::new(),
|
||||||
|
setup_args: Vec::new(),
|
||||||
|
setup_command_template: String::new(),
|
||||||
|
only_setup: true,
|
||||||
|
version_index: 0,
|
||||||
|
delta: false,
|
||||||
|
umu_id_override: None,
|
||||||
|
},
|
||||||
|
save_id: String::from("aaaaaaa"),
|
||||||
|
};
|
||||||
|
//resolve(&mut meta);
|
||||||
|
|
||||||
|
extract("save".into()).unwrap();
|
||||||
|
}
|
||||||
@ -4,14 +4,18 @@ use std::{
|
|||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use database::{
|
|
||||||
Settings, borrow_db_checked, borrow_db_mut_checked, db::DATA_ROOT_DIR, debug::SystemData,
|
|
||||||
};
|
|
||||||
use download_manager::error::DownloadManagerError;
|
|
||||||
use games::scan::scan_install_dirs;
|
|
||||||
use log::error;
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
database::{db::borrow_db_mut_checked, scan::scan_install_dirs}, error::download_manager_error::DownloadManagerError,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
db::{borrow_db_checked, DATA_ROOT_DIR},
|
||||||
|
debug::SystemData,
|
||||||
|
models::data::Settings,
|
||||||
|
};
|
||||||
|
|
||||||
// Will, in future, return disk/remaining size
|
// Will, in future, return disk/remaining size
|
||||||
// Just returns the directories that have been set up
|
// Just returns the directories that have been set up
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
@ -63,25 +67,11 @@ pub fn add_download_dir(new_dir: PathBuf) -> Result<(), DownloadManagerError<()>
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn update_settings(new_settings: Value) {
|
pub fn update_settings(new_settings: Value) {
|
||||||
let mut db_lock = borrow_db_mut_checked();
|
let mut db_lock = borrow_db_mut_checked();
|
||||||
let mut current_settings =
|
let mut current_settings = serde_json::to_value(db_lock.settings.clone()).unwrap();
|
||||||
serde_json::to_value(db_lock.settings.clone()).expect("Failed to parse existing settings");
|
for (key, value) in new_settings.as_object().unwrap() {
|
||||||
let values = match new_settings.as_object() {
|
|
||||||
Some(values) => values,
|
|
||||||
None => {
|
|
||||||
error!("Could not parse settings values into object");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
for (key, value) in values {
|
|
||||||
current_settings[key] = value.clone();
|
current_settings[key] = value.clone();
|
||||||
}
|
}
|
||||||
let new_settings: Settings = match serde_json::from_value(current_settings) {
|
let new_settings: Settings = serde_json::from_value(current_settings).unwrap();
|
||||||
Ok(settings) => settings,
|
|
||||||
Err(e) => {
|
|
||||||
error!("Could not parse settings with error {}", e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
db_lock.settings = new_settings;
|
db_lock.settings = new_settings;
|
||||||
}
|
}
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
@ -3,20 +3,56 @@ use std::{
|
|||||||
mem::ManuallyDrop,
|
mem::ManuallyDrop,
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::{RwLockReadGuard, RwLockWriteGuard},
|
sync::{Arc, LazyLock, RwLockReadGuard, RwLockWriteGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use log::{debug, error, info, warn};
|
use log::{debug, error, info, warn};
|
||||||
use rustbreak::{PathDatabase, RustbreakError};
|
use rustbreak::{DeSerError, DeSerializer, PathDatabase, RustbreakError};
|
||||||
|
use serde::{Serialize, de::DeserializeOwned};
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
use crate::{
|
use crate::DB;
|
||||||
db::{DropDatabaseSerializer, DATA_ROOT_DIR, DB},
|
|
||||||
models::{Database, DatabaseInterface},
|
use super::models::data::Database;
|
||||||
};
|
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
static DATA_ROOT_PREFIX: &'static str = "drop";
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
static DATA_ROOT_PREFIX: &str = "drop-debug";
|
||||||
|
|
||||||
|
pub static DATA_ROOT_DIR: LazyLock<Arc<PathBuf>> =
|
||||||
|
LazyLock::new(|| Arc::new(dirs::data_dir().unwrap().join(DATA_ROOT_PREFIX)));
|
||||||
|
|
||||||
|
// Custom JSON serializer to support everything we need
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub struct DropDatabaseSerializer;
|
||||||
|
|
||||||
|
impl<T: native_model::Model + Serialize + DeserializeOwned> DeSerializer<T>
|
||||||
|
for DropDatabaseSerializer
|
||||||
|
{
|
||||||
|
fn serialize(&self, val: &T) -> rustbreak::error::DeSerResult<Vec<u8>> {
|
||||||
|
native_model::encode(val)
|
||||||
|
.map_err(|e| DeSerError::Internal(e.to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize<R: std::io::Read>(&self, mut s: R) -> rustbreak::error::DeSerResult<T> {
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
s.read_to_end(&mut buf)
|
||||||
|
.map_err(|e| rustbreak::error::DeSerError::Other(e.into()))?;
|
||||||
|
let (val, _version) = native_model::decode(buf)
|
||||||
|
.map_err(|e| DeSerError::Internal(e.to_string()))?;
|
||||||
|
Ok(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type DatabaseInterface =
|
||||||
|
rustbreak::Database<Database, rustbreak::backend::PathBackend, DropDatabaseSerializer>;
|
||||||
|
|
||||||
pub trait DatabaseImpls {
|
pub trait DatabaseImpls {
|
||||||
fn set_up_database() -> DatabaseInterface;
|
fn set_up_database() -> DatabaseInterface;
|
||||||
|
fn database_is_set_up(&self) -> bool;
|
||||||
|
fn fetch_base_url(&self) -> Url;
|
||||||
}
|
}
|
||||||
impl DatabaseImpls for DatabaseInterface {
|
impl DatabaseImpls for DatabaseInterface {
|
||||||
fn set_up_database() -> DatabaseInterface {
|
fn set_up_database() -> DatabaseInterface {
|
||||||
@ -27,49 +63,13 @@ impl DatabaseImpls for DatabaseInterface {
|
|||||||
let pfx_dir = DATA_ROOT_DIR.join("pfx");
|
let pfx_dir = DATA_ROOT_DIR.join("pfx");
|
||||||
|
|
||||||
debug!("creating data directory at {DATA_ROOT_DIR:?}");
|
debug!("creating data directory at {DATA_ROOT_DIR:?}");
|
||||||
create_dir_all(DATA_ROOT_DIR.as_path()).unwrap_or_else(|e| {
|
create_dir_all(DATA_ROOT_DIR.as_path()).unwrap();
|
||||||
panic!(
|
create_dir_all(&games_base_dir).unwrap();
|
||||||
"Failed to create directory {} with error {}",
|
create_dir_all(&logs_root_dir).unwrap();
|
||||||
DATA_ROOT_DIR.display(),
|
create_dir_all(&cache_dir).unwrap();
|
||||||
e
|
create_dir_all(&pfx_dir).unwrap();
|
||||||
)
|
|
||||||
});
|
|
||||||
create_dir_all(&games_base_dir).unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"Failed to create directory {} with error {}",
|
|
||||||
games_base_dir.display(),
|
|
||||||
e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
create_dir_all(&logs_root_dir).unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"Failed to create directory {} with error {}",
|
|
||||||
logs_root_dir.display(),
|
|
||||||
e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
create_dir_all(&cache_dir).unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"Failed to create directory {} with error {}",
|
|
||||||
cache_dir.display(),
|
|
||||||
e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
create_dir_all(&pfx_dir).unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"Failed to create directory {} with error {}",
|
|
||||||
pfx_dir.display(),
|
|
||||||
e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
let exists = fs::exists(db_path.clone()).unwrap_or_else(|e| {
|
let exists = fs::exists(db_path.clone()).unwrap();
|
||||||
panic!(
|
|
||||||
"Failed to find if {} exists with error {}",
|
|
||||||
db_path.display(),
|
|
||||||
e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
match PathDatabase::load_from_path(db_path.clone()) {
|
match PathDatabase::load_from_path(db_path.clone()) {
|
||||||
@ -78,10 +78,22 @@ impl DatabaseImpls for DatabaseInterface {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let default = Database::new(games_base_dir, None, cache_dir);
|
let default = Database::new(games_base_dir, None, cache_dir);
|
||||||
debug!("Creating database at path {}", db_path.display());
|
debug!(
|
||||||
|
"Creating database at path {}",
|
||||||
|
db_path.as_os_str().to_str().unwrap()
|
||||||
|
);
|
||||||
PathDatabase::create_at_path(db_path, default).expect("Database could not be created")
|
PathDatabase::create_at_path(db_path, default).expect("Database could not be created")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn database_is_set_up(&self) -> bool {
|
||||||
|
!self.borrow_data().unwrap().base_url.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fetch_base_url(&self) -> Url {
|
||||||
|
let handle = self.borrow_data().unwrap();
|
||||||
|
Url::parse(&handle.base_url).unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Make the error relelvant rather than just assume that it's a Deserialize error
|
// TODO: Make the error relelvant rather than just assume that it's a Deserialize error
|
||||||
@ -99,16 +111,13 @@ fn handle_invalid_database(
|
|||||||
base
|
base
|
||||||
};
|
};
|
||||||
info!("old database stored at: {}", new_path.to_string_lossy());
|
info!("old database stored at: {}", new_path.to_string_lossy());
|
||||||
fs::rename(&db_path, &new_path).unwrap_or_else(|e| {
|
fs::rename(&db_path, &new_path).unwrap();
|
||||||
panic!(
|
|
||||||
"Could not rename database {} to {} with error {}",
|
|
||||||
db_path.display(),
|
|
||||||
new_path.display(),
|
|
||||||
e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
let db = Database::new(games_base_dir, Some(new_path), cache_dir);
|
let db = Database::new(
|
||||||
|
games_base_dir.into_os_string().into_string().unwrap(),
|
||||||
|
Some(new_path),
|
||||||
|
cache_dir,
|
||||||
|
);
|
||||||
|
|
||||||
PathDatabase::create_at_path(db_path, db).expect("Database could not be created")
|
PathDatabase::create_at_path(db_path, db).expect("Database could not be created")
|
||||||
}
|
}
|
||||||
5
src-tauri/src/database/mod.rs
Normal file
5
src-tauri/src/database/mod.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
pub mod commands;
|
||||||
|
pub mod db;
|
||||||
|
pub mod debug;
|
||||||
|
pub mod models;
|
||||||
|
pub mod scan;
|
||||||
362
src-tauri/src/database/models.rs
Normal file
362
src-tauri/src/database/models.rs
Normal file
@ -0,0 +1,362 @@
|
|||||||
|
pub mod data {
|
||||||
|
use std::{hash::Hash, path::PathBuf};
|
||||||
|
|
||||||
|
use native_model::native_model;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
// NOTE: Within each version, you should NEVER use these types.
|
||||||
|
// Declare it using the actual version that it is from, i.e. v1::Settings rather than just Settings from here
|
||||||
|
|
||||||
|
pub type GameVersion = v1::GameVersion;
|
||||||
|
pub type Database = v3::Database;
|
||||||
|
pub type Settings = v1::Settings;
|
||||||
|
pub type DatabaseAuth = v1::DatabaseAuth;
|
||||||
|
|
||||||
|
pub type GameDownloadStatus = v2::GameDownloadStatus;
|
||||||
|
pub type ApplicationTransientStatus = v1::ApplicationTransientStatus;
|
||||||
|
/**
|
||||||
|
* Need to be universally accessible by the ID, and the version is just a couple sprinkles on top
|
||||||
|
*/
|
||||||
|
pub type DownloadableMetadata = v1::DownloadableMetadata;
|
||||||
|
pub type DownloadType = v1::DownloadType;
|
||||||
|
pub type DatabaseApplications = v2::DatabaseApplications;
|
||||||
|
// pub type DatabaseCompatInfo = v2::DatabaseCompatInfo;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
impl PartialEq for DownloadableMetadata {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.id == other.id && self.download_type == other.download_type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Hash for DownloadableMetadata {
|
||||||
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||||
|
self.id.hash(state);
|
||||||
|
self.download_type.hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod v1 {
|
||||||
|
use crate::process::process_manager::Platform;
|
||||||
|
use serde_with::serde_as;
|
||||||
|
use std::{collections::HashMap, path::PathBuf};
|
||||||
|
|
||||||
|
use super::{Deserialize, Serialize, native_model};
|
||||||
|
|
||||||
|
fn default_template() -> String {
|
||||||
|
"{}".to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
#[native_model(id = 2, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
||||||
|
pub struct GameVersion {
|
||||||
|
pub game_id: String,
|
||||||
|
pub version_name: String,
|
||||||
|
|
||||||
|
pub platform: Platform,
|
||||||
|
|
||||||
|
pub launch_command: String,
|
||||||
|
pub launch_args: Vec<String>,
|
||||||
|
#[serde(default = "default_template")]
|
||||||
|
pub launch_command_template: String,
|
||||||
|
|
||||||
|
pub setup_command: String,
|
||||||
|
pub setup_args: Vec<String>,
|
||||||
|
#[serde(default = "default_template")]
|
||||||
|
pub setup_command_template: String,
|
||||||
|
|
||||||
|
pub only_setup: bool,
|
||||||
|
|
||||||
|
pub version_index: usize,
|
||||||
|
pub delta: bool,
|
||||||
|
|
||||||
|
pub umu_id_override: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[serde_as]
|
||||||
|
#[derive(Serialize, Clone, Deserialize, Default)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
#[native_model(id = 3, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
||||||
|
pub struct DatabaseApplications {
|
||||||
|
pub install_dirs: Vec<PathBuf>,
|
||||||
|
// Guaranteed to exist if the game also exists in the app state map
|
||||||
|
pub game_statuses: HashMap<String, GameDownloadStatus>,
|
||||||
|
pub game_versions: HashMap<String, HashMap<String, GameVersion>>,
|
||||||
|
pub installed_game_version: HashMap<String, DownloadableMetadata>,
|
||||||
|
|
||||||
|
#[serde(skip)]
|
||||||
|
pub transient_statuses: HashMap<DownloadableMetadata, ApplicationTransientStatus>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
#[native_model(id = 4, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
||||||
|
pub struct Settings {
|
||||||
|
pub autostart: bool,
|
||||||
|
pub max_download_threads: usize,
|
||||||
|
pub force_offline: bool, // ... other settings ...
|
||||||
|
}
|
||||||
|
impl Default for Settings {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
autostart: false,
|
||||||
|
max_download_threads: 4,
|
||||||
|
force_offline: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strings are version names for a particular game
|
||||||
|
#[derive(Serialize, Clone, Deserialize)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
#[native_model(id = 5, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
||||||
|
pub enum GameDownloadStatus {
|
||||||
|
Remote {},
|
||||||
|
SetupRequired {
|
||||||
|
version_name: String,
|
||||||
|
install_dir: String,
|
||||||
|
},
|
||||||
|
Installed {
|
||||||
|
version_name: String,
|
||||||
|
install_dir: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stuff that shouldn't be synced to disk
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
|
pub enum ApplicationTransientStatus {
|
||||||
|
Queued { version_name: String },
|
||||||
|
Downloading { version_name: String },
|
||||||
|
Uninstalling {},
|
||||||
|
Updating { version_name: String },
|
||||||
|
Validating { version_name: String },
|
||||||
|
Running {},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, Clone, Deserialize)]
|
||||||
|
#[native_model(id = 6, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
||||||
|
pub struct DatabaseAuth {
|
||||||
|
pub private: String,
|
||||||
|
pub cert: String,
|
||||||
|
pub client_id: String,
|
||||||
|
pub web_token: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[native_model(id = 8, version = 1)]
|
||||||
|
#[derive(
|
||||||
|
Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Clone, Copy,
|
||||||
|
)]
|
||||||
|
pub enum DownloadType {
|
||||||
|
Game,
|
||||||
|
Tool,
|
||||||
|
Dlc,
|
||||||
|
Mod,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[native_model(id = 7, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
||||||
|
#[derive(Debug, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct DownloadableMetadata {
|
||||||
|
pub id: String,
|
||||||
|
pub version: Option<String>,
|
||||||
|
pub download_type: DownloadType,
|
||||||
|
}
|
||||||
|
impl DownloadableMetadata {
|
||||||
|
pub fn new(id: String, version: Option<String>, download_type: DownloadType) -> Self {
|
||||||
|
Self {
|
||||||
|
id,
|
||||||
|
version,
|
||||||
|
download_type,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[native_model(id = 1, version = 1)]
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Default)]
|
||||||
|
pub struct Database {
|
||||||
|
#[serde(default)]
|
||||||
|
pub settings: Settings,
|
||||||
|
pub auth: Option<DatabaseAuth>,
|
||||||
|
pub base_url: String,
|
||||||
|
pub applications: DatabaseApplications,
|
||||||
|
pub prev_database: Option<PathBuf>,
|
||||||
|
pub cache_dir: PathBuf,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod v2 {
|
||||||
|
use std::{collections::HashMap, path::PathBuf};
|
||||||
|
|
||||||
|
use serde_with::serde_as;
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
Deserialize, Serialize, native_model, v1,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[native_model(id = 1, version = 2, with = native_model::rmp_serde_1_3::RmpSerde, from = v1::Database)]
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Default)]
|
||||||
|
pub struct Database {
|
||||||
|
#[serde(default)]
|
||||||
|
pub settings: v1::Settings,
|
||||||
|
pub auth: Option<v1::DatabaseAuth>,
|
||||||
|
pub base_url: String,
|
||||||
|
pub applications: v1::DatabaseApplications,
|
||||||
|
#[serde(skip)]
|
||||||
|
pub prev_database: Option<PathBuf>,
|
||||||
|
pub cache_dir: PathBuf,
|
||||||
|
pub compat_info: Option<DatabaseCompatInfo>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[native_model(id = 9, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Default)]
|
||||||
|
|
||||||
|
pub struct DatabaseCompatInfo {
|
||||||
|
pub umu_installed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<v1::Database> for Database {
|
||||||
|
fn from(value: v1::Database) -> Self {
|
||||||
|
Self {
|
||||||
|
settings: value.settings,
|
||||||
|
auth: value.auth,
|
||||||
|
base_url: value.base_url,
|
||||||
|
applications: value.applications,
|
||||||
|
prev_database: value.prev_database,
|
||||||
|
cache_dir: value.cache_dir,
|
||||||
|
compat_info: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Strings are version names for a particular game
|
||||||
|
#[derive(Serialize, Clone, Deserialize, Debug)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
#[native_model(id = 5, version = 2, with = native_model::rmp_serde_1_3::RmpSerde, from = v1::GameDownloadStatus)]
|
||||||
|
pub enum GameDownloadStatus {
|
||||||
|
Remote {},
|
||||||
|
SetupRequired {
|
||||||
|
version_name: String,
|
||||||
|
install_dir: String,
|
||||||
|
},
|
||||||
|
Installed {
|
||||||
|
version_name: String,
|
||||||
|
install_dir: String,
|
||||||
|
},
|
||||||
|
PartiallyInstalled {
|
||||||
|
version_name: String,
|
||||||
|
install_dir: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
impl From<v1::GameDownloadStatus> for GameDownloadStatus {
|
||||||
|
fn from(value: v1::GameDownloadStatus) -> Self {
|
||||||
|
match value {
|
||||||
|
v1::GameDownloadStatus::Remote {} => Self::Remote {},
|
||||||
|
v1::GameDownloadStatus::SetupRequired {
|
||||||
|
version_name,
|
||||||
|
install_dir,
|
||||||
|
} => Self::SetupRequired {
|
||||||
|
version_name,
|
||||||
|
install_dir,
|
||||||
|
},
|
||||||
|
v1::GameDownloadStatus::Installed {
|
||||||
|
version_name,
|
||||||
|
install_dir,
|
||||||
|
} => Self::Installed {
|
||||||
|
version_name,
|
||||||
|
install_dir,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[serde_as]
|
||||||
|
#[derive(Serialize, Clone, Deserialize, Default)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
#[native_model(id = 3, version = 2, with = native_model::rmp_serde_1_3::RmpSerde, from=v1::DatabaseApplications)]
|
||||||
|
pub struct DatabaseApplications {
|
||||||
|
pub install_dirs: Vec<PathBuf>,
|
||||||
|
// Guaranteed to exist if the game also exists in the app state map
|
||||||
|
pub game_statuses: HashMap<String, GameDownloadStatus>,
|
||||||
|
|
||||||
|
pub game_versions: HashMap<String, HashMap<String, v1::GameVersion>>,
|
||||||
|
pub installed_game_version: HashMap<String, v1::DownloadableMetadata>,
|
||||||
|
|
||||||
|
#[serde(skip)]
|
||||||
|
pub transient_statuses: HashMap<v1::DownloadableMetadata, v1::ApplicationTransientStatus>,
|
||||||
|
}
|
||||||
|
impl From<v1::DatabaseApplications> for DatabaseApplications {
|
||||||
|
fn from(value: v1::DatabaseApplications) -> Self {
|
||||||
|
Self {
|
||||||
|
game_statuses: value
|
||||||
|
.game_statuses
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| (x.0, x.1.into()))
|
||||||
|
.collect::<HashMap<String, GameDownloadStatus>>(),
|
||||||
|
install_dirs: value.install_dirs,
|
||||||
|
game_versions: value.game_versions,
|
||||||
|
installed_game_version: value.installed_game_version,
|
||||||
|
transient_statuses: value.transient_statuses,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mod v3 {
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
Deserialize, Serialize,
|
||||||
|
native_model, v2, v1,
|
||||||
|
};
|
||||||
|
#[native_model(id = 1, version = 3, with = native_model::rmp_serde_1_3::RmpSerde, from = v2::Database)]
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Default)]
|
||||||
|
pub struct Database {
|
||||||
|
#[serde(default)]
|
||||||
|
pub settings: v1::Settings,
|
||||||
|
pub auth: Option<v1::DatabaseAuth>,
|
||||||
|
pub base_url: String,
|
||||||
|
pub applications: v2::DatabaseApplications,
|
||||||
|
#[serde(skip)]
|
||||||
|
pub prev_database: Option<PathBuf>,
|
||||||
|
pub cache_dir: PathBuf,
|
||||||
|
pub compat_info: Option<v2::DatabaseCompatInfo>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<v2::Database> for Database {
|
||||||
|
fn from(value: v2::Database) -> Self {
|
||||||
|
Self {
|
||||||
|
settings: value.settings,
|
||||||
|
auth: value.auth,
|
||||||
|
base_url: value.base_url,
|
||||||
|
applications: value.applications.into(),
|
||||||
|
prev_database: value.prev_database,
|
||||||
|
cache_dir: value.cache_dir,
|
||||||
|
compat_info: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Database {
|
||||||
|
pub fn new<T: Into<PathBuf>>(
|
||||||
|
games_base_dir: T,
|
||||||
|
prev_database: Option<PathBuf>,
|
||||||
|
cache_dir: PathBuf,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
applications: DatabaseApplications {
|
||||||
|
install_dirs: vec![games_base_dir.into()],
|
||||||
|
game_statuses: HashMap::new(),
|
||||||
|
game_versions: HashMap::new(),
|
||||||
|
installed_game_version: HashMap::new(),
|
||||||
|
transient_statuses: HashMap::new(),
|
||||||
|
},
|
||||||
|
prev_database,
|
||||||
|
base_url: String::new(),
|
||||||
|
auth: None,
|
||||||
|
settings: Settings::default(),
|
||||||
|
cache_dir,
|
||||||
|
compat_info: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,12 +1,16 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use database::{DownloadType, DownloadableMetadata, borrow_db_mut_checked};
|
|
||||||
use drop_consts::DROP_DATA_PATH;
|
|
||||||
use log::warn;
|
use log::warn;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
downloads::drop_data::DropData,
|
database::{
|
||||||
library::set_partially_installed_db,
|
db::borrow_db_mut_checked,
|
||||||
|
models::data::{DownloadType, DownloadableMetadata},
|
||||||
|
},
|
||||||
|
games::{
|
||||||
|
downloads::drop_data::{DropData, DROP_DATA_PATH},
|
||||||
|
library::set_partially_installed_db,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn scan_install_dirs() {
|
pub fn scan_install_dirs() {
|
||||||
@ -20,11 +24,11 @@ pub fn scan_install_dirs() {
|
|||||||
if !drop_data_file.exists() {
|
if !drop_data_file.exists() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let game_id = game.file_name().display().to_string();
|
let game_id = game.file_name().into_string().unwrap();
|
||||||
let Ok(drop_data) = DropData::read(&game.path()) else {
|
let Ok(drop_data) = DropData::read(&game.path()) else {
|
||||||
warn!(
|
warn!(
|
||||||
".dropdata exists for {}, but couldn't read it. is it corrupted?",
|
".dropdata exists for {}, but couldn't read it. is it corrupted?",
|
||||||
game.file_name().display()
|
game.file_name().into_string().unwrap()
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
@ -1,22 +0,0 @@
|
|||||||
use database::DownloadableMetadata;
|
|
||||||
use download_manager::DOWNLOAD_MANAGER;
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn pause_downloads() {
|
|
||||||
DOWNLOAD_MANAGER.pause_downloads();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn resume_downloads() {
|
|
||||||
DOWNLOAD_MANAGER.resume_downloads();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn move_download_in_queue(old_index: usize, new_index: usize) {
|
|
||||||
DOWNLOAD_MANAGER.rearrange(old_index, new_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn cancel_game(meta: DownloadableMetadata) {
|
|
||||||
DOWNLOAD_MANAGER.cancel(meta);
|
|
||||||
}
|
|
||||||
31
src-tauri/src/download_manager/commands.rs
Normal file
31
src-tauri/src/download_manager/commands.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::{database::models::data::DownloadableMetadata, AppState};
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn pause_downloads(state: tauri::State<'_, Mutex<AppState>>) {
|
||||||
|
state.lock().unwrap().download_manager.pause_downloads();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn resume_downloads(state: tauri::State<'_, Mutex<AppState>>) {
|
||||||
|
state.lock().unwrap().download_manager.resume_downloads();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn move_download_in_queue(
|
||||||
|
state: tauri::State<'_, Mutex<AppState>>,
|
||||||
|
old_index: usize,
|
||||||
|
new_index: usize,
|
||||||
|
) {
|
||||||
|
state
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.download_manager
|
||||||
|
.rearrange(old_index, new_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn cancel_game(state: tauri::State<'_, Mutex<AppState>>, meta: DownloadableMetadata) {
|
||||||
|
state.lock().unwrap().download_manager.cancel(meta);
|
||||||
|
}
|
||||||
@ -7,15 +7,14 @@ use std::{
|
|||||||
thread::{JoinHandle, spawn},
|
thread::{JoinHandle, spawn},
|
||||||
};
|
};
|
||||||
|
|
||||||
use database::DownloadableMetadata;
|
|
||||||
use log::{debug, error, info, warn};
|
use log::{debug, error, info, warn};
|
||||||
use tauri::AppHandle;
|
use tauri::{AppHandle, Emitter};
|
||||||
use utils::{app_emit, lock, send};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
download_manager_frontend::DownloadStatus,
|
database::models::data::DownloadableMetadata,
|
||||||
error::ApplicationDownloadError,
|
download_manager::download_manager_frontend::DownloadStatus,
|
||||||
frontend_updates::{QueueUpdateEvent, QueueUpdateEventQueueData, StatsUpdateEvent},
|
error::application_download_error::ApplicationDownloadError,
|
||||||
|
games::library::{QueueUpdateEvent, QueueUpdateEventQueueData, StatsUpdateEvent},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@ -106,7 +105,7 @@ impl DownloadManagerBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn set_status(&self, status: DownloadManagerStatus) {
|
fn set_status(&self, status: DownloadManagerStatus) {
|
||||||
*lock!(self.status) = status;
|
*self.status.lock().unwrap() = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_and_cleanup_front_download(&mut self, meta: &DownloadableMetadata) -> DownloadAgent {
|
fn remove_and_cleanup_front_download(&mut self, meta: &DownloadableMetadata) -> DownloadAgent {
|
||||||
@ -120,9 +119,9 @@ impl DownloadManagerBuilder {
|
|||||||
// Make sure the download thread is terminated
|
// Make sure the download thread is terminated
|
||||||
fn cleanup_current_download(&mut self) {
|
fn cleanup_current_download(&mut self) {
|
||||||
self.active_control_flag = None;
|
self.active_control_flag = None;
|
||||||
*lock!(self.progress) = None;
|
*self.progress.lock().unwrap() = None;
|
||||||
|
|
||||||
let mut download_thread_lock = lock!(self.current_download_thread);
|
let mut download_thread_lock = self.current_download_thread.lock().unwrap();
|
||||||
|
|
||||||
if let Some(unfinished_thread) = download_thread_lock.take()
|
if let Some(unfinished_thread) = download_thread_lock.take()
|
||||||
&& !unfinished_thread.is_finished()
|
&& !unfinished_thread.is_finished()
|
||||||
@ -138,7 +137,7 @@ impl DownloadManagerBuilder {
|
|||||||
current_flag.set(DownloadThreadControlFlag::Stop);
|
current_flag.set(DownloadThreadControlFlag::Stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut download_thread_lock = lock!(self.current_download_thread);
|
let mut download_thread_lock = self.current_download_thread.lock().unwrap();
|
||||||
if let Some(current_download_thread) = download_thread_lock.take() {
|
if let Some(current_download_thread) = download_thread_lock.take() {
|
||||||
return current_download_thread.join().is_ok();
|
return current_download_thread.join().is_ok();
|
||||||
};
|
};
|
||||||
@ -200,7 +199,9 @@ impl DownloadManagerBuilder {
|
|||||||
self.download_queue.append(meta.clone());
|
self.download_queue.append(meta.clone());
|
||||||
self.download_agent_registry.insert(meta, download_agent);
|
self.download_agent_registry.insert(meta, download_agent);
|
||||||
|
|
||||||
send!(self.sender, DownloadManagerSignal::UpdateUIQueue);
|
self.sender
|
||||||
|
.send(DownloadManagerSignal::UpdateUIQueue)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn manage_go_signal(&mut self) {
|
fn manage_go_signal(&mut self) {
|
||||||
@ -246,7 +247,7 @@ impl DownloadManagerBuilder {
|
|||||||
|
|
||||||
let sender = self.sender.clone();
|
let sender = self.sender.clone();
|
||||||
|
|
||||||
let mut download_thread_lock = lock!(self.current_download_thread);
|
let mut download_thread_lock = self.current_download_thread.lock().unwrap();
|
||||||
let app_handle = self.app_handle.clone();
|
let app_handle = self.app_handle.clone();
|
||||||
|
|
||||||
*download_thread_lock = Some(spawn(move || {
|
*download_thread_lock = Some(spawn(move || {
|
||||||
@ -257,7 +258,7 @@ impl DownloadManagerBuilder {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("download {:?} has error {}", download_agent.metadata(), &e);
|
error!("download {:?} has error {}", download_agent.metadata(), &e);
|
||||||
download_agent.on_error(&app_handle, &e);
|
download_agent.on_error(&app_handle, &e);
|
||||||
send!(sender, DownloadManagerSignal::Error(e));
|
sender.send(DownloadManagerSignal::Error(e)).unwrap();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -281,7 +282,7 @@ impl DownloadManagerBuilder {
|
|||||||
&e
|
&e
|
||||||
);
|
);
|
||||||
download_agent.on_error(&app_handle, &e);
|
download_agent.on_error(&app_handle, &e);
|
||||||
send!(sender, DownloadManagerSignal::Error(e));
|
sender.send(DownloadManagerSignal::Error(e)).unwrap();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -292,11 +293,10 @@ impl DownloadManagerBuilder {
|
|||||||
|
|
||||||
if validate_result {
|
if validate_result {
|
||||||
download_agent.on_complete(&app_handle);
|
download_agent.on_complete(&app_handle);
|
||||||
send!(
|
sender
|
||||||
sender,
|
.send(DownloadManagerSignal::Completed(download_agent.metadata()))
|
||||||
DownloadManagerSignal::Completed(download_agent.metadata())
|
.unwrap();
|
||||||
);
|
sender.send(DownloadManagerSignal::UpdateUIQueue).unwrap();
|
||||||
send!(sender, DownloadManagerSignal::UpdateUIQueue);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -323,7 +323,7 @@ impl DownloadManagerBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.push_ui_queue_update();
|
self.push_ui_queue_update();
|
||||||
send!(self.sender, DownloadManagerSignal::Go);
|
self.sender.send(DownloadManagerSignal::Go).unwrap();
|
||||||
}
|
}
|
||||||
fn manage_error_signal(&mut self, error: ApplicationDownloadError) {
|
fn manage_error_signal(&mut self, error: ApplicationDownloadError) {
|
||||||
debug!("got signal Error");
|
debug!("got signal Error");
|
||||||
@ -361,7 +361,7 @@ impl DownloadManagerBuilder {
|
|||||||
let index = self.download_queue.get_by_meta(meta);
|
let index = self.download_queue.get_by_meta(meta);
|
||||||
if let Some(index) = index {
|
if let Some(index) = index {
|
||||||
download_agent.on_cancelled(&self.app_handle);
|
download_agent.on_cancelled(&self.app_handle);
|
||||||
let _ = self.download_queue.edit().remove(index);
|
let _ = self.download_queue.edit().remove(index).unwrap();
|
||||||
let removed = self.download_agent_registry.remove(meta);
|
let removed = self.download_agent_registry.remove(meta);
|
||||||
debug!(
|
debug!(
|
||||||
"removed {:?} from queue {:?}",
|
"removed {:?} from queue {:?}",
|
||||||
@ -376,7 +376,7 @@ impl DownloadManagerBuilder {
|
|||||||
fn push_ui_stats_update(&self, kbs: usize, time: usize) {
|
fn push_ui_stats_update(&self, kbs: usize, time: usize) {
|
||||||
let event_data = StatsUpdateEvent { speed: kbs, time };
|
let event_data = StatsUpdateEvent { speed: kbs, time };
|
||||||
|
|
||||||
app_emit!(&self.app_handle, "update_stats", event_data);
|
self.app_handle.emit("update_stats", event_data).unwrap();
|
||||||
}
|
}
|
||||||
fn push_ui_queue_update(&self) {
|
fn push_ui_queue_update(&self) {
|
||||||
let queue = &self.download_queue.read();
|
let queue = &self.download_queue.read();
|
||||||
@ -395,6 +395,6 @@ impl DownloadManagerBuilder {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let event_data = QueueUpdateEvent { queue: queue_objs };
|
let event_data = QueueUpdateEvent { queue: queue_objs };
|
||||||
app_emit!(&self.app_handle, "update_queue", event_data);
|
self.app_handle.emit("update_queue", event_data).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3,18 +3,19 @@ use std::{
|
|||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
sync::{
|
sync::{
|
||||||
Mutex, MutexGuard,
|
|
||||||
mpsc::{SendError, Sender},
|
mpsc::{SendError, Sender},
|
||||||
|
Mutex, MutexGuard,
|
||||||
},
|
},
|
||||||
thread::JoinHandle,
|
thread::JoinHandle,
|
||||||
};
|
};
|
||||||
|
|
||||||
use database::DownloadableMetadata;
|
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use utils::{lock, send};
|
|
||||||
|
|
||||||
use crate::error::ApplicationDownloadError;
|
use crate::{
|
||||||
|
database::models::data::DownloadableMetadata,
|
||||||
|
error::application_download_error::ApplicationDownloadError,
|
||||||
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
download_manager_builder::{CurrentProgressObject, DownloadAgent},
|
download_manager_builder::{CurrentProgressObject, DownloadAgent},
|
||||||
@ -79,7 +80,6 @@ pub enum DownloadStatus {
|
|||||||
/// The actual download queue may be accessed through the .`edit()` function,
|
/// The actual download queue may be accessed through the .`edit()` function,
|
||||||
/// which provides raw access to the underlying queue.
|
/// which provides raw access to the underlying queue.
|
||||||
/// THIS EDITING IS BLOCKING!!!
|
/// THIS EDITING IS BLOCKING!!!
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct DownloadManager {
|
pub struct DownloadManager {
|
||||||
terminator: Mutex<Option<JoinHandle<Result<(), ()>>>>,
|
terminator: Mutex<Option<JoinHandle<Result<(), ()>>>>,
|
||||||
download_queue: Queue,
|
download_queue: Queue,
|
||||||
@ -119,21 +119,22 @@ impl DownloadManager {
|
|||||||
self.download_queue.read()
|
self.download_queue.read()
|
||||||
}
|
}
|
||||||
pub fn get_current_download_progress(&self) -> Option<f64> {
|
pub fn get_current_download_progress(&self) -> Option<f64> {
|
||||||
let progress_object = (*lock!(self.progress)).clone()?;
|
let progress_object = (*self.progress.lock().unwrap()).clone()?;
|
||||||
Some(progress_object.get_progress())
|
Some(progress_object.get_progress())
|
||||||
}
|
}
|
||||||
pub fn rearrange_string(&self, meta: &DownloadableMetadata, new_index: usize) {
|
pub fn rearrange_string(&self, meta: &DownloadableMetadata, new_index: usize) {
|
||||||
let mut queue = self.edit();
|
let mut queue = self.edit();
|
||||||
let current_index =
|
let current_index = get_index_from_id(&mut queue, meta).unwrap();
|
||||||
get_index_from_id(&mut queue, meta).expect("Failed to get meta index from id");
|
let to_move = queue.remove(current_index).unwrap();
|
||||||
let to_move = queue
|
|
||||||
.remove(current_index)
|
|
||||||
.expect("Failed to remove meta at index from queue");
|
|
||||||
queue.insert(new_index, to_move);
|
queue.insert(new_index, to_move);
|
||||||
send!(self.command_sender, DownloadManagerSignal::UpdateUIQueue);
|
self.command_sender
|
||||||
|
.send(DownloadManagerSignal::UpdateUIQueue)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
pub fn cancel(&self, meta: DownloadableMetadata) {
|
pub fn cancel(&self, meta: DownloadableMetadata) {
|
||||||
send!(self.command_sender, DownloadManagerSignal::Cancel(meta));
|
self.command_sender
|
||||||
|
.send(DownloadManagerSignal::Cancel(meta))
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
pub fn rearrange(&self, current_index: usize, new_index: usize) {
|
pub fn rearrange(&self, current_index: usize, new_index: usize) {
|
||||||
if current_index == new_index {
|
if current_index == new_index {
|
||||||
@ -142,31 +143,39 @@ impl DownloadManager {
|
|||||||
|
|
||||||
let needs_pause = current_index == 0 || new_index == 0;
|
let needs_pause = current_index == 0 || new_index == 0;
|
||||||
if needs_pause {
|
if needs_pause {
|
||||||
send!(self.command_sender, DownloadManagerSignal::Stop);
|
self.command_sender
|
||||||
|
.send(DownloadManagerSignal::Stop)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("moving download at index {current_index} to index {new_index}");
|
debug!("moving download at index {current_index} to index {new_index}");
|
||||||
|
|
||||||
let mut queue = self.edit();
|
let mut queue = self.edit();
|
||||||
let to_move = queue.remove(current_index).expect("Failed to get");
|
let to_move = queue.remove(current_index).unwrap();
|
||||||
queue.insert(new_index, to_move);
|
queue.insert(new_index, to_move);
|
||||||
drop(queue);
|
drop(queue);
|
||||||
|
|
||||||
if needs_pause {
|
if needs_pause {
|
||||||
send!(self.command_sender, DownloadManagerSignal::Go);
|
self.command_sender.send(DownloadManagerSignal::Go).unwrap();
|
||||||
}
|
}
|
||||||
send!(self.command_sender, DownloadManagerSignal::UpdateUIQueue);
|
self.command_sender
|
||||||
send!(self.command_sender, DownloadManagerSignal::Go);
|
.send(DownloadManagerSignal::UpdateUIQueue)
|
||||||
|
.unwrap();
|
||||||
|
self.command_sender.send(DownloadManagerSignal::Go).unwrap();
|
||||||
}
|
}
|
||||||
pub fn pause_downloads(&self) {
|
pub fn pause_downloads(&self) {
|
||||||
send!(self.command_sender, DownloadManagerSignal::Stop);
|
self.command_sender
|
||||||
|
.send(DownloadManagerSignal::Stop)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
pub fn resume_downloads(&self) {
|
pub fn resume_downloads(&self) {
|
||||||
send!(self.command_sender, DownloadManagerSignal::Go);
|
self.command_sender.send(DownloadManagerSignal::Go).unwrap();
|
||||||
}
|
}
|
||||||
pub fn ensure_terminated(&self) -> Result<Result<(), ()>, Box<dyn Any + Send>> {
|
pub fn ensure_terminated(&self) -> Result<Result<(), ()>, Box<dyn Any + Send>> {
|
||||||
send!(self.command_sender, DownloadManagerSignal::Finish);
|
self.command_sender
|
||||||
let terminator = lock!(self.terminator).take();
|
.send(DownloadManagerSignal::Finish)
|
||||||
|
.unwrap();
|
||||||
|
let terminator = self.terminator.lock().unwrap().take();
|
||||||
terminator.unwrap().join()
|
terminator.unwrap().join()
|
||||||
}
|
}
|
||||||
pub fn get_sender(&self) -> Sender<DownloadManagerSignal> {
|
pub fn get_sender(&self) -> Sender<DownloadManagerSignal> {
|
||||||
@ -1,9 +1,11 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use database::DownloadableMetadata;
|
|
||||||
use tauri::AppHandle;
|
use tauri::AppHandle;
|
||||||
|
|
||||||
use crate::error::ApplicationDownloadError;
|
use crate::{
|
||||||
|
database::models::data::DownloadableMetadata,
|
||||||
|
error::application_download_error::ApplicationDownloadError,
|
||||||
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
download_manager_frontend::DownloadStatus,
|
download_manager_frontend::DownloadStatus,
|
||||||
5
src-tauri/src/download_manager/mod.rs
Normal file
5
src-tauri/src/download_manager/mod.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
pub mod commands;
|
||||||
|
pub mod download_manager_builder;
|
||||||
|
pub mod download_manager_frontend;
|
||||||
|
pub mod downloadable;
|
||||||
|
pub mod util;
|
||||||
@ -1,6 +1,6 @@
|
|||||||
use std::sync::{
|
use std::sync::{
|
||||||
Arc,
|
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
|
Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
@ -22,11 +22,7 @@ impl From<DownloadThreadControlFlag> for bool {
|
|||||||
/// false => Stop
|
/// false => Stop
|
||||||
impl From<bool> for DownloadThreadControlFlag {
|
impl From<bool> for DownloadThreadControlFlag {
|
||||||
fn from(value: bool) -> Self {
|
fn from(value: bool) -> Self {
|
||||||
if value {
|
if value { DownloadThreadControlFlag::Go } else { DownloadThreadControlFlag::Stop }
|
||||||
DownloadThreadControlFlag::Go
|
|
||||||
} else {
|
|
||||||
DownloadThreadControlFlag::Stop
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -9,13 +9,12 @@ use std::{
|
|||||||
|
|
||||||
use atomic_instant_full::AtomicInstant;
|
use atomic_instant_full::AtomicInstant;
|
||||||
use throttle_my_fn::throttle;
|
use throttle_my_fn::throttle;
|
||||||
use utils::{lock, send};
|
|
||||||
|
|
||||||
use crate::download_manager_frontend::DownloadManagerSignal;
|
use crate::download_manager::download_manager_frontend::DownloadManagerSignal;
|
||||||
|
|
||||||
use super::rolling_progress_updates::RollingProgressWindow;
|
use super::rolling_progress_updates::RollingProgressWindow;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone)]
|
||||||
pub struct ProgressObject {
|
pub struct ProgressObject {
|
||||||
max: Arc<Mutex<usize>>,
|
max: Arc<Mutex<usize>>,
|
||||||
progress_instances: Arc<Mutex<Vec<Arc<AtomicUsize>>>>,
|
progress_instances: Arc<Mutex<Vec<Arc<AtomicUsize>>>>,
|
||||||
@ -75,10 +74,12 @@ impl ProgressObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_time_now(&self) {
|
pub fn set_time_now(&self) {
|
||||||
*lock!(self.start) = Instant::now();
|
*self.start.lock().unwrap() = Instant::now();
|
||||||
}
|
}
|
||||||
pub fn sum(&self) -> usize {
|
pub fn sum(&self) -> usize {
|
||||||
lock!(self.progress_instances)
|
self.progress_instances
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|instance| instance.load(Ordering::Acquire))
|
.map(|instance| instance.load(Ordering::Acquire))
|
||||||
.sum()
|
.sum()
|
||||||
@ -87,25 +88,27 @@ impl ProgressObject {
|
|||||||
self.set_time_now();
|
self.set_time_now();
|
||||||
self.bytes_last_update.store(0, Ordering::Release);
|
self.bytes_last_update.store(0, Ordering::Release);
|
||||||
self.rolling.reset();
|
self.rolling.reset();
|
||||||
lock!(self.progress_instances)
|
self.progress_instances
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|x| x.store(0, Ordering::SeqCst));
|
.for_each(|x| x.store(0, Ordering::SeqCst));
|
||||||
}
|
}
|
||||||
pub fn get_max(&self) -> usize {
|
pub fn get_max(&self) -> usize {
|
||||||
*lock!(self.max)
|
*self.max.lock().unwrap()
|
||||||
}
|
}
|
||||||
pub fn set_max(&self, new_max: usize) {
|
pub fn set_max(&self, new_max: usize) {
|
||||||
*lock!(self.max) = new_max;
|
*self.max.lock().unwrap() = new_max;
|
||||||
}
|
}
|
||||||
pub fn set_size(&self, length: usize) {
|
pub fn set_size(&self, length: usize) {
|
||||||
*lock!(self.progress_instances) =
|
*self.progress_instances.lock().unwrap() =
|
||||||
(0..length).map(|_| Arc::new(AtomicUsize::new(0))).collect();
|
(0..length).map(|_| Arc::new(AtomicUsize::new(0))).collect();
|
||||||
}
|
}
|
||||||
pub fn get_progress(&self) -> f64 {
|
pub fn get_progress(&self) -> f64 {
|
||||||
self.sum() as f64 / self.get_max() as f64
|
self.sum() as f64 / self.get_max() as f64
|
||||||
}
|
}
|
||||||
pub fn get(&self, index: usize) -> Arc<AtomicUsize> {
|
pub fn get(&self, index: usize) -> Arc<AtomicUsize> {
|
||||||
lock!(self.progress_instances)[index].clone()
|
self.progress_instances.lock().unwrap()[index].clone()
|
||||||
}
|
}
|
||||||
fn update_window(&self, kilobytes_per_second: usize) {
|
fn update_window(&self, kilobytes_per_second: usize) {
|
||||||
self.rolling.update(kilobytes_per_second);
|
self.rolling.update(kilobytes_per_second);
|
||||||
@ -117,9 +120,7 @@ pub fn calculate_update(progress: &ProgressObject) {
|
|||||||
let last_update_time = progress
|
let last_update_time = progress
|
||||||
.last_update_time
|
.last_update_time
|
||||||
.swap(Instant::now(), Ordering::SeqCst);
|
.swap(Instant::now(), Ordering::SeqCst);
|
||||||
let time_since_last_update = Instant::now()
|
let time_since_last_update = Instant::now().duration_since(last_update_time).as_millis_f64();
|
||||||
.duration_since(last_update_time)
|
|
||||||
.as_millis_f64();
|
|
||||||
|
|
||||||
let current_bytes_downloaded = progress.sum();
|
let current_bytes_downloaded = progress.sum();
|
||||||
let max = progress.get_max();
|
let max = progress.get_max();
|
||||||
@ -127,8 +128,7 @@ pub fn calculate_update(progress: &ProgressObject) {
|
|||||||
.bytes_last_update
|
.bytes_last_update
|
||||||
.swap(current_bytes_downloaded, Ordering::Acquire);
|
.swap(current_bytes_downloaded, Ordering::Acquire);
|
||||||
|
|
||||||
let bytes_since_last_update =
|
let bytes_since_last_update = current_bytes_downloaded.saturating_sub(bytes_at_last_update) as f64;
|
||||||
current_bytes_downloaded.saturating_sub(bytes_at_last_update) as f64;
|
|
||||||
|
|
||||||
let kilobytes_per_second = bytes_since_last_update / time_since_last_update;
|
let kilobytes_per_second = bytes_since_last_update / time_since_last_update;
|
||||||
|
|
||||||
@ -148,12 +148,18 @@ pub fn push_update(progress: &ProgressObject, bytes_remaining: usize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn update_ui(progress_object: &ProgressObject, kilobytes_per_second: usize, time_remaining: usize) {
|
fn update_ui(progress_object: &ProgressObject, kilobytes_per_second: usize, time_remaining: usize) {
|
||||||
send!(
|
progress_object
|
||||||
progress_object.sender,
|
.sender
|
||||||
DownloadManagerSignal::UpdateUIStats(kilobytes_per_second, time_remaining)
|
.send(DownloadManagerSignal::UpdateUIStats(
|
||||||
);
|
kilobytes_per_second,
|
||||||
|
time_remaining,
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_queue(progress: &ProgressObject) {
|
fn update_queue(progress: &ProgressObject) {
|
||||||
send!(progress.sender, DownloadManagerSignal::UpdateUIQueue)
|
progress
|
||||||
|
.sender
|
||||||
|
.send(DownloadManagerSignal::UpdateUIQueue)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
@ -3,10 +3,9 @@ use std::{
|
|||||||
sync::{Arc, Mutex, MutexGuard},
|
sync::{Arc, Mutex, MutexGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
use database::DownloadableMetadata;
|
use crate::database::models::data::DownloadableMetadata;
|
||||||
use utils::lock;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone)]
|
||||||
pub struct Queue {
|
pub struct Queue {
|
||||||
inner: Arc<Mutex<VecDeque<DownloadableMetadata>>>,
|
inner: Arc<Mutex<VecDeque<DownloadableMetadata>>>,
|
||||||
}
|
}
|
||||||
@ -25,10 +24,10 @@ impl Queue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn read(&self) -> VecDeque<DownloadableMetadata> {
|
pub fn read(&self) -> VecDeque<DownloadableMetadata> {
|
||||||
lock!(self.inner).clone()
|
self.inner.lock().unwrap().clone()
|
||||||
}
|
}
|
||||||
pub fn edit(&self) -> MutexGuard<'_, VecDeque<DownloadableMetadata>> {
|
pub fn edit(&self) -> MutexGuard<'_, VecDeque<DownloadableMetadata>> {
|
||||||
lock!(self.inner)
|
self.inner.lock().unwrap()
|
||||||
}
|
}
|
||||||
pub fn pop_front(&self) -> Option<DownloadableMetadata> {
|
pub fn pop_front(&self) -> Option<DownloadableMetadata> {
|
||||||
self.edit().pop_front()
|
self.edit().pop_front()
|
||||||
@ -3,17 +3,11 @@ use std::sync::{
|
|||||||
atomic::{AtomicUsize, Ordering},
|
atomic::{AtomicUsize, Ordering},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone)]
|
||||||
pub struct RollingProgressWindow<const S: usize> {
|
pub struct RollingProgressWindow<const S: usize> {
|
||||||
window: Arc<[AtomicUsize; S]>,
|
window: Arc<[AtomicUsize; S]>,
|
||||||
current: Arc<AtomicUsize>,
|
current: Arc<AtomicUsize>,
|
||||||
}
|
}
|
||||||
impl<const S: usize> Default for RollingProgressWindow<S> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const S: usize> RollingProgressWindow<S> {
|
impl<const S: usize> RollingProgressWindow<S> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -37,7 +31,7 @@ impl<const S: usize> RollingProgressWindow<S> {
|
|||||||
.collect::<Vec<usize>>();
|
.collect::<Vec<usize>>();
|
||||||
let amount = valid.len();
|
let amount = valid.len();
|
||||||
let sum = valid.into_iter().sum::<usize>();
|
let sum = valid.into_iter().sum::<usize>();
|
||||||
|
|
||||||
sum / amount
|
sum / amount
|
||||||
}
|
}
|
||||||
pub fn reset(&self) {
|
pub fn reset(&self) {
|
||||||
49
src-tauri/src/error/application_download_error.rs
Normal file
49
src-tauri/src/error/application_download_error.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
use std::{
|
||||||
|
fmt::{Display, Formatter},
|
||||||
|
io, sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
|
use serde_with::SerializeDisplay;
|
||||||
|
use humansize::{format_size, BINARY};
|
||||||
|
|
||||||
|
use super::remote_access_error::RemoteAccessError;
|
||||||
|
|
||||||
|
// TODO: Rename / separate from downloads
|
||||||
|
#[derive(Debug, SerializeDisplay)]
|
||||||
|
pub enum ApplicationDownloadError {
|
||||||
|
NotInitialized,
|
||||||
|
Communication(RemoteAccessError),
|
||||||
|
DiskFull(u64, u64),
|
||||||
|
#[allow(dead_code)]
|
||||||
|
Checksum,
|
||||||
|
Lock,
|
||||||
|
IoError(Arc<io::Error>),
|
||||||
|
DownloadError,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for ApplicationDownloadError {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
ApplicationDownloadError::NotInitialized => write!(f, "Download not initalized, did something go wrong?"),
|
||||||
|
ApplicationDownloadError::DiskFull(required, available) => write!(
|
||||||
|
f,
|
||||||
|
"Game requires {}, {} remaining left on disk.",
|
||||||
|
format_size(*required, BINARY),
|
||||||
|
format_size(*available, BINARY),
|
||||||
|
),
|
||||||
|
ApplicationDownloadError::Communication(error) => write!(f, "{error}"),
|
||||||
|
ApplicationDownloadError::Lock => write!(
|
||||||
|
f,
|
||||||
|
"failed to acquire lock. Something has gone very wrong internally. Please restart the application"
|
||||||
|
),
|
||||||
|
ApplicationDownloadError::Checksum => {
|
||||||
|
write!(f, "checksum failed to validate for download")
|
||||||
|
}
|
||||||
|
ApplicationDownloadError::IoError(error) => write!(f, "io error: {error}"),
|
||||||
|
ApplicationDownloadError::DownloadError => write!(
|
||||||
|
f,
|
||||||
|
"Download failed. See Download Manager status for specific error"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src-tauri/src/error/download_manager_error.rs
Normal file
27
src-tauri/src/error/download_manager_error.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
use std::{fmt::Display, io, sync::mpsc::SendError};
|
||||||
|
|
||||||
|
use serde_with::SerializeDisplay;
|
||||||
|
|
||||||
|
#[derive(SerializeDisplay)]
|
||||||
|
pub enum DownloadManagerError<T> {
|
||||||
|
IOError(io::Error),
|
||||||
|
SignalError(SendError<T>),
|
||||||
|
}
|
||||||
|
impl<T> Display for DownloadManagerError<T> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
DownloadManagerError::IOError(error) => write!(f, "{error}"),
|
||||||
|
DownloadManagerError::SignalError(send_error) => write!(f, "{send_error}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T> From<SendError<T>> for DownloadManagerError<T> {
|
||||||
|
fn from(value: SendError<T>) -> Self {
|
||||||
|
DownloadManagerError::SignalError(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T> From<io::Error> for DownloadManagerError<T> {
|
||||||
|
fn from(value: io::Error) -> Self {
|
||||||
|
DownloadManagerError::IOError(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src-tauri/src/error/drop_server_error.rs
Normal file
10
src-tauri/src/error/drop_server_error.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct DropServerError {
|
||||||
|
pub status_code: usize,
|
||||||
|
pub status_message: String,
|
||||||
|
// pub message: String,
|
||||||
|
// pub url: String,
|
||||||
|
}
|
||||||
18
src-tauri/src/error/library_error.rs
Normal file
18
src-tauri/src/error/library_error.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
use serde_with::SerializeDisplay;
|
||||||
|
|
||||||
|
#[derive(SerializeDisplay)]
|
||||||
|
pub enum LibraryError {
|
||||||
|
MetaNotFound(String),
|
||||||
|
}
|
||||||
|
impl Display for LibraryError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
LibraryError::MetaNotFound(id) => write!(
|
||||||
|
f,
|
||||||
|
"Could not locate any installed version of game ID {id} in the database"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src-tauri/src/error/mod.rs
Normal file
6
src-tauri/src/error/mod.rs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
pub mod application_download_error;
|
||||||
|
pub mod download_manager_error;
|
||||||
|
pub mod drop_server_error;
|
||||||
|
pub mod library_error;
|
||||||
|
pub mod process_error;
|
||||||
|
pub mod remote_access_error;
|
||||||
@ -11,9 +11,7 @@ pub enum ProcessError {
|
|||||||
IOError(Error),
|
IOError(Error),
|
||||||
FormatError(String), // String errors supremacy
|
FormatError(String), // String errors supremacy
|
||||||
InvalidPlatform,
|
InvalidPlatform,
|
||||||
OpenerError(tauri_plugin_opener::Error),
|
OpenerError(tauri_plugin_opener::Error)
|
||||||
InvalidArguments(String),
|
|
||||||
FailedLaunch(String),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for ProcessError {
|
impl Display for ProcessError {
|
||||||
@ -25,15 +23,9 @@ impl Display for ProcessError {
|
|||||||
ProcessError::InvalidVersion => "Invalid game version",
|
ProcessError::InvalidVersion => "Invalid game version",
|
||||||
ProcessError::IOError(error) => &error.to_string(),
|
ProcessError::IOError(error) => &error.to_string(),
|
||||||
ProcessError::InvalidPlatform => "This game cannot be played on the current platform",
|
ProcessError::InvalidPlatform => "This game cannot be played on the current platform",
|
||||||
ProcessError::FormatError(error) => &format!("Could not format template: {error:?}"),
|
ProcessError::FormatError(e) => &format!("Failed to format template: {e}"),
|
||||||
ProcessError::OpenerError(error) => &format!("Could not open directory: {error:?}"),
|
ProcessError::OpenerError(error) => &format!("Failed to open directory: {error}"),
|
||||||
ProcessError::InvalidArguments(arguments) => {
|
};
|
||||||
&format!("Invalid arguments in command {arguments}")
|
|
||||||
}
|
|
||||||
ProcessError::FailedLaunch(game_id) => {
|
|
||||||
&format!("Drop detected that the game {game_id} may have failed to launch properly")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
write!(f, "{s}")
|
write!(f, "{s}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4,20 +4,11 @@ use std::{
|
|||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use http::{HeaderName, StatusCode, header::ToStrError};
|
use http::StatusCode;
|
||||||
use serde_with::SerializeDisplay;
|
use serde_with::SerializeDisplay;
|
||||||
use url::ParseError;
|
use url::ParseError;
|
||||||
|
|
||||||
use serde::Deserialize;
|
use super::drop_server_error::DropServerError;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct DropServerError {
|
|
||||||
pub status_code: usize,
|
|
||||||
pub status_message: String,
|
|
||||||
// pub message: String,
|
|
||||||
// pub url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, SerializeDisplay)]
|
#[derive(Debug, SerializeDisplay)]
|
||||||
pub enum RemoteAccessError {
|
pub enum RemoteAccessError {
|
||||||
@ -53,7 +44,8 @@ impl Display for RemoteAccessError {
|
|||||||
error
|
error
|
||||||
.source()
|
.source()
|
||||||
.map(std::string::ToString::to_string)
|
.map(std::string::ToString::to_string)
|
||||||
.unwrap_or("Unknown error".to_string())
|
.or_else(|| Some("Unknown error".to_string()))
|
||||||
|
.unwrap()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
RemoteAccessError::FetchErrorWS(error) => write!(
|
RemoteAccessError::FetchErrorWS(error) => write!(
|
||||||
@ -62,8 +54,9 @@ impl Display for RemoteAccessError {
|
|||||||
error,
|
error,
|
||||||
error
|
error
|
||||||
.source()
|
.source()
|
||||||
.map(std::string::ToString::to_string)
|
.map(|e| e.to_string())
|
||||||
.unwrap_or("Unknown error".to_string())
|
.or_else(|| Some("Unknown error".to_string()))
|
||||||
|
.unwrap()
|
||||||
),
|
),
|
||||||
RemoteAccessError::ParsingError(parse_error) => {
|
RemoteAccessError::ParsingError(parse_error) => {
|
||||||
write!(f, "{parse_error}")
|
write!(f, "{parse_error}")
|
||||||
@ -113,31 +106,3 @@ impl From<ParseError> for RemoteAccessError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl std::error::Error for RemoteAccessError {}
|
impl std::error::Error for RemoteAccessError {}
|
||||||
|
|
||||||
#[derive(Debug, SerializeDisplay)]
|
|
||||||
pub enum CacheError {
|
|
||||||
HeaderNotFound(HeaderName),
|
|
||||||
ParseError(ToStrError),
|
|
||||||
Remote(RemoteAccessError),
|
|
||||||
ConstructionError(http::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for CacheError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let s = match self {
|
|
||||||
CacheError::HeaderNotFound(header_name) => {
|
|
||||||
format!("Could not find header {header_name} in cache")
|
|
||||||
}
|
|
||||||
CacheError::ParseError(to_str_error) => {
|
|
||||||
format!("Could not parse cache with error {to_str_error}")
|
|
||||||
}
|
|
||||||
CacheError::Remote(remote_access_error) => {
|
|
||||||
format!("Cache got remote access error: {remote_access_error}")
|
|
||||||
}
|
|
||||||
CacheError::ConstructionError(error) => {
|
|
||||||
format!("Could not construct cache body with error {error}")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
write!(f, "{s}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,282 +0,0 @@
|
|||||||
use std::sync::nonpoison::Mutex;
|
|
||||||
|
|
||||||
use database::{GameDownloadStatus, GameVersion, borrow_db_checked, borrow_db_mut_checked, models::Game};
|
|
||||||
use games::{
|
|
||||||
downloads::error::LibraryError,
|
|
||||||
library::{FetchGameStruct, FrontendGameOptions, get_current_meta, uninstall_game_logic},
|
|
||||||
state::{GameStatusManager, GameStatusWithTransient},
|
|
||||||
};
|
|
||||||
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},
|
|
||||||
error::{DropServerError, RemoteAccessError},
|
|
||||||
offline,
|
|
||||||
requests::generate_url,
|
|
||||||
utils::DROP_CLIENT_ASYNC,
|
|
||||||
};
|
|
||||||
use tauri::AppHandle;
|
|
||||||
|
|
||||||
use client::app_state::AppState;
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub async fn fetch_library(
|
|
||||||
state: tauri::State<'_, Mutex<AppState>>,
|
|
||||||
hard_refresh: Option<bool>,
|
|
||||||
) -> Result<Vec<Game>, RemoteAccessError> {
|
|
||||||
offline!(
|
|
||||||
state,
|
|
||||||
fetch_library_logic,
|
|
||||||
fetch_library_logic_offline,
|
|
||||||
state,
|
|
||||||
hard_refresh
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fetch_library_logic(
|
|
||||||
state: tauri::State<'_, Mutex<AppState>>,
|
|
||||||
hard_fresh: Option<bool>,
|
|
||||||
) -> (Vec<Game>, Vec<LibraryError>) {
|
|
||||||
|
|
||||||
}
|
|
||||||
pub async fn fetch_library_logic_offline(
|
|
||||||
_state: tauri::State<'_, Mutex<AppState>>,
|
|
||||||
_hard_refresh: Option<bool>,
|
|
||||||
) -> (Vec<Game>, Vec<LibraryError>) {
|
|
||||||
let mut games: Vec<Game> = get_cached_object("library")?;
|
|
||||||
|
|
||||||
let db_handle = borrow_db_checked();
|
|
||||||
|
|
||||||
games.retain(|game| {
|
|
||||||
matches!(
|
|
||||||
&db_handle
|
|
||||||
.applications
|
|
||||||
.game_statuses
|
|
||||||
.get(game.id())
|
|
||||||
.unwrap_or(&GameDownloadStatus::Remote {}),
|
|
||||||
GameDownloadStatus::Installed { .. } | GameDownloadStatus::SetupRequired { .. }
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(games)
|
|
||||||
}
|
|
||||||
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 {
|
|
||||||
None => None,
|
|
||||||
Some(metadata) => db_lock
|
|
||||||
.applications
|
|
||||||
.game_versions
|
|
||||||
.get(&metadata.id)
|
|
||||||
.map(|v| v.get(metadata.version.as_ref().unwrap()).unwrap())
|
|
||||||
.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?;
|
|
||||||
|
|
||||||
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?;
|
|
||||||
|
|
||||||
let mut state_handle = state.lock();
|
|
||||||
state_handle.games_mut().insert(id.clone(), game.clone());
|
|
||||||
|
|
||||||
let mut db_handle = borrow_db_mut_checked();
|
|
||||||
|
|
||||||
db_handle
|
|
||||||
.applications
|
|
||||||
.game_statuses
|
|
||||||
.entry(id.clone())
|
|
||||||
.or_insert(GameDownloadStatus::Remote {});
|
|
||||||
|
|
||||||
let status = GameStatusManager::fetch_state(&id, &db_handle);
|
|
||||||
|
|
||||||
drop(db_handle);
|
|
||||||
|
|
||||||
let data = FetchGameStruct::new(game.clone(), status, version);
|
|
||||||
|
|
||||||
cache_object(&id, &game)?;
|
|
||||||
|
|
||||||
Ok(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fetch_game_version_options_logic(
|
|
||||||
game_id: String,
|
|
||||||
state: tauri::State<'_, Mutex<AppState>>,
|
|
||||||
) -> Result<Vec<GameVersion>, RemoteAccessError> {
|
|
||||||
let client = DROP_CLIENT_ASYNC.clone();
|
|
||||||
|
|
||||||
let response = generate_url(&["/api/v1/client/game/versions"], &[("id", &game_id)])?;
|
|
||||||
let response = client
|
|
||||||
.get(response)
|
|
||||||
.header("Authorization", generate_authorization_header())
|
|
||||||
.send()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if response.status() != 200 {
|
|
||||||
let err = response.json().await?;
|
|
||||||
warn!("{err:?}");
|
|
||||||
return Err(RemoteAccessError::InvalidResponse(err));
|
|
||||||
}
|
|
||||||
|
|
||||||
let data: Vec<GameVersion> = response.json().await?;
|
|
||||||
|
|
||||||
let state_lock = state.lock();
|
|
||||||
let process_manager_lock = PROCESS_MANAGER.lock();
|
|
||||||
let data: Vec<GameVersion> = data
|
|
||||||
.into_iter()
|
|
||||||
.filter(|v| process_manager_lock.valid_platform(&v.platform))
|
|
||||||
.collect();
|
|
||||||
drop(process_manager_lock);
|
|
||||||
drop(state_lock);
|
|
||||||
|
|
||||||
Ok(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fetch_game_logic_offline(
|
|
||||||
id: String,
|
|
||||||
_state: tauri::State<'_, Mutex<AppState>>,
|
|
||||||
) -> Result<FetchGameStruct, RemoteAccessError> {
|
|
||||||
let db_handle = borrow_db_checked();
|
|
||||||
let metadata_option = db_handle.applications.installed_game_version.get(&id);
|
|
||||||
let version = match metadata_option {
|
|
||||||
None => None,
|
|
||||||
Some(metadata) => db_handle
|
|
||||||
.applications
|
|
||||||
.game_versions
|
|
||||||
.get(&metadata.id)
|
|
||||||
.map(|v| v.get(metadata.version.as_ref().unwrap()).unwrap())
|
|
||||||
.cloned(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let status = GameStatusManager::fetch_state(&id, &db_handle);
|
|
||||||
let game = get_cached_object::<Game>(&id)?;
|
|
||||||
|
|
||||||
drop(db_handle);
|
|
||||||
|
|
||||||
Ok(FetchGameStruct::new(game, status, version))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub async fn fetch_game(
|
|
||||||
game_id: String,
|
|
||||||
state: tauri::State<'_, Mutex<AppState>>,
|
|
||||||
) -> Result<FetchGameStruct, RemoteAccessError> {
|
|
||||||
offline!(
|
|
||||||
state,
|
|
||||||
fetch_game_logic,
|
|
||||||
fetch_game_logic_offline,
|
|
||||||
game_id,
|
|
||||||
state
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn fetch_game_status(id: String) -> GameStatusWithTransient {
|
|
||||||
let db_handle = borrow_db_checked();
|
|
||||||
GameStatusManager::fetch_state(&id, &db_handle)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn uninstall_game(game_id: String, app_handle: AppHandle) -> Result<(), LibraryError> {
|
|
||||||
let meta = match get_current_meta(&game_id) {
|
|
||||||
Some(data) => data,
|
|
||||||
None => return Err(LibraryError::MetaNotFound(game_id)),
|
|
||||||
};
|
|
||||||
uninstall_game_logic(meta, &app_handle);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub async fn fetch_game_version_options(
|
|
||||||
game_id: String,
|
|
||||||
state: tauri::State<'_, Mutex<AppState>>,
|
|
||||||
) -> Result<Vec<GameVersion>, RemoteAccessError> {
|
|
||||||
fetch_game_version_options_logic(game_id, state).await
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub fn update_game_configuration(
|
|
||||||
game_id: String,
|
|
||||||
options: FrontendGameOptions,
|
|
||||||
) -> Result<(), LibraryError> {
|
|
||||||
let mut handle = borrow_db_mut_checked();
|
|
||||||
let installed_version = handle
|
|
||||||
.applications
|
|
||||||
.installed_game_version
|
|
||||||
.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 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();
|
|
||||||
|
|
||||||
// Add no more options past here
|
|
||||||
|
|
||||||
handle
|
|
||||||
.applications
|
|
||||||
.game_versions
|
|
||||||
.get_mut(&id)
|
|
||||||
.unwrap()
|
|
||||||
.insert(version.to_string(), existing_configuration);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@ -1,7 +1,8 @@
|
|||||||
use bitcode::{Decode, Encode};
|
use bitcode::{Decode, Encode};
|
||||||
use database::models::Game;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::games::library::Game;
|
||||||
|
|
||||||
pub type Collections = Vec<Collection>;
|
pub type Collections = Vec<Collection>;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Default, Encode, Decode)]
|
#[derive(Serialize, Deserialize, Debug, Clone, Default, Encode, Decode)]
|
||||||
@ -14,7 +15,7 @@ pub struct Collection {
|
|||||||
entries: Vec<CollectionObject>,
|
entries: Vec<CollectionObject>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Encode, Decode)]
|
#[derive(Serialize, Deserialize, Debug, Clone, Default, Encode, Decode)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct CollectionObject {
|
pub struct CollectionObject {
|
||||||
collection_id: String,
|
collection_id: String,
|
||||||
@ -1,13 +1,17 @@
|
|||||||
use games::collections::collection::{Collection, Collections};
|
|
||||||
use remote::{
|
|
||||||
auth::generate_authorization_header,
|
|
||||||
cache::{cache_object, get_cached_object},
|
|
||||||
error::RemoteAccessError,
|
|
||||||
requests::{generate_url, make_authenticated_get},
|
|
||||||
utils::DROP_CLIENT_ASYNC,
|
|
||||||
};
|
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
error::remote_access_error::RemoteAccessError,
|
||||||
|
remote::{
|
||||||
|
auth::generate_authorization_header,
|
||||||
|
cache::{cache_object, get_cached_object},
|
||||||
|
requests::{generate_url, make_authenticated_get},
|
||||||
|
utils::DROP_CLIENT_ASYNC,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::collection::{Collection, Collections};
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn fetch_collections(
|
pub async fn fetch_collections(
|
||||||
hard_refresh: Option<bool>,
|
hard_refresh: Option<bool>,
|
||||||
@ -1 +1,2 @@
|
|||||||
pub mod collection;
|
pub mod collection;
|
||||||
|
pub mod commands;
|
||||||
78
src-tauri/src/games/commands.rs
Normal file
78
src-tauri/src/games/commands.rs
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use tauri::AppHandle;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
AppState,
|
||||||
|
database::{
|
||||||
|
db::borrow_db_checked,
|
||||||
|
models::data::GameVersion,
|
||||||
|
},
|
||||||
|
error::{library_error::LibraryError, remote_access_error::RemoteAccessError},
|
||||||
|
games::library::{
|
||||||
|
fetch_game_logic_offline, fetch_library_logic_offline, get_current_meta,
|
||||||
|
uninstall_game_logic,
|
||||||
|
},
|
||||||
|
offline,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
library::{
|
||||||
|
FetchGameStruct, Game, fetch_game_logic, fetch_game_version_options_logic,
|
||||||
|
fetch_library_logic,
|
||||||
|
},
|
||||||
|
state::{GameStatusManager, GameStatusWithTransient},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn fetch_library(
|
||||||
|
state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
|
hard_refresh: Option<bool>,
|
||||||
|
) -> Result<Vec<Game>, RemoteAccessError> {
|
||||||
|
offline!(
|
||||||
|
state,
|
||||||
|
fetch_library_logic,
|
||||||
|
fetch_library_logic_offline,
|
||||||
|
state,
|
||||||
|
hard_refresh
|
||||||
|
).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn fetch_game(
|
||||||
|
game_id: String,
|
||||||
|
state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
|
) -> Result<FetchGameStruct, RemoteAccessError> {
|
||||||
|
offline!(
|
||||||
|
state,
|
||||||
|
fetch_game_logic,
|
||||||
|
fetch_game_logic_offline,
|
||||||
|
game_id,
|
||||||
|
state
|
||||||
|
).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn fetch_game_status(id: String) -> GameStatusWithTransient {
|
||||||
|
let db_handle = borrow_db_checked();
|
||||||
|
GameStatusManager::fetch_state(&id, &db_handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn uninstall_game(game_id: String, app_handle: AppHandle) -> Result<(), LibraryError> {
|
||||||
|
let meta = match get_current_meta(&game_id) {
|
||||||
|
Some(data) => data,
|
||||||
|
None => return Err(LibraryError::MetaNotFound(game_id)),
|
||||||
|
};
|
||||||
|
uninstall_game_logic(meta, &app_handle);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn fetch_game_version_options(
|
||||||
|
game_id: String,
|
||||||
|
state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
|
) -> Result<Vec<GameVersion>, RemoteAccessError> {
|
||||||
|
fetch_game_version_options_logic(game_id, state).await
|
||||||
|
}
|
||||||
@ -1,31 +1,39 @@
|
|||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{
|
||||||
|
path::PathBuf,
|
||||||
use database::{GameDownloadStatus, borrow_db_checked};
|
sync::{Arc, Mutex},
|
||||||
use download_manager::{
|
|
||||||
DOWNLOAD_MANAGER, downloadable::Downloadable, error::ApplicationDownloadError,
|
|
||||||
};
|
};
|
||||||
use games::downloads::download_agent::GameDownloadAgent;
|
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
AppState,
|
||||||
|
database::{
|
||||||
|
db::borrow_db_checked,
|
||||||
|
models::data::GameDownloadStatus,
|
||||||
|
},
|
||||||
|
download_manager::downloadable::Downloadable,
|
||||||
|
error::application_download_error::ApplicationDownloadError,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::download_agent::GameDownloadAgent;
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn download_game(
|
pub async fn download_game(
|
||||||
game_id: String,
|
game_id: String,
|
||||||
game_version: String,
|
game_version: String,
|
||||||
install_dir: usize,
|
install_dir: usize,
|
||||||
|
state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
) -> Result<(), ApplicationDownloadError> {
|
) -> Result<(), ApplicationDownloadError> {
|
||||||
let sender = { DOWNLOAD_MANAGER.get_sender().clone() };
|
let sender = { state.lock().unwrap().download_manager.get_sender().clone() };
|
||||||
|
|
||||||
let game_download_agent = GameDownloadAgent::new_from_index(
|
let game_download_agent =
|
||||||
game_id.clone(),
|
GameDownloadAgent::new_from_index(game_id.clone(), game_version.clone(), install_dir, sender).await?;
|
||||||
game_version.clone(),
|
|
||||||
install_dir,
|
|
||||||
sender,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let game_download_agent =
|
let game_download_agent =
|
||||||
Arc::new(Box::new(game_download_agent) as Box<dyn Downloadable + Send + Sync>);
|
Arc::new(Box::new(game_download_agent) as Box<dyn Downloadable + Send + Sync>);
|
||||||
|
state
|
||||||
DOWNLOAD_MANAGER
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.download_manager
|
||||||
.queue_download(game_download_agent.clone())
|
.queue_download(game_download_agent.clone())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -33,7 +41,10 @@ pub async fn download_game(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn resume_download(game_id: String) -> Result<(), ApplicationDownloadError> {
|
pub async fn resume_download(
|
||||||
|
game_id: String,
|
||||||
|
state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
|
) -> Result<(), ApplicationDownloadError> {
|
||||||
let s = borrow_db_checked()
|
let s = borrow_db_checked()
|
||||||
.applications
|
.applications
|
||||||
.game_statuses
|
.game_statuses
|
||||||
@ -51,25 +62,23 @@ pub async fn resume_download(game_id: String) -> Result<(), ApplicationDownloadE
|
|||||||
} => (version_name, install_dir),
|
} => (version_name, install_dir),
|
||||||
};
|
};
|
||||||
|
|
||||||
let sender = DOWNLOAD_MANAGER.get_sender();
|
let sender = state.lock().unwrap().download_manager.get_sender();
|
||||||
let parent_dir: PathBuf = install_dir.into();
|
let parent_dir: PathBuf = install_dir.into();
|
||||||
|
|
||||||
let game_download_agent = Arc::new(Box::new(
|
let game_download_agent = Arc::new(Box::new(
|
||||||
GameDownloadAgent::new(
|
GameDownloadAgent::new(
|
||||||
game_id,
|
game_id,
|
||||||
version_name.clone(),
|
version_name.clone(),
|
||||||
parent_dir
|
parent_dir.parent().unwrap().to_path_buf(),
|
||||||
.parent()
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
panic!("Failed to get parent directry of {}", parent_dir.display())
|
|
||||||
})
|
|
||||||
.to_path_buf(),
|
|
||||||
sender,
|
sender,
|
||||||
)
|
)
|
||||||
.await?,
|
.await?,
|
||||||
) as Box<dyn Downloadable + Send + Sync>);
|
) as Box<dyn Downloadable + Send + Sync>);
|
||||||
|
|
||||||
DOWNLOAD_MANAGER
|
state
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.download_manager
|
||||||
.queue_download(game_download_agent)
|
.queue_download(game_download_agent)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -1,45 +1,46 @@
|
|||||||
use database::{
|
use crate::auth::generate_authorization_header;
|
||||||
ApplicationTransientStatus, DownloadType, DownloadableMetadata, borrow_db_checked,
|
use crate::database::db::{borrow_db_checked, borrow_db_mut_checked};
|
||||||
borrow_db_mut_checked,
|
use crate::database::models::data::{
|
||||||
|
ApplicationTransientStatus, DownloadType, DownloadableMetadata,
|
||||||
};
|
};
|
||||||
use download_manager::download_manager_frontend::{DownloadManagerSignal, DownloadStatus};
|
use crate::download_manager::download_manager_frontend::{DownloadManagerSignal, DownloadStatus};
|
||||||
use download_manager::downloadable::Downloadable;
|
use crate::download_manager::downloadable::Downloadable;
|
||||||
use download_manager::error::ApplicationDownloadError;
|
use crate::download_manager::util::download_thread_control_flag::{
|
||||||
use download_manager::util::download_thread_control_flag::{
|
|
||||||
DownloadThreadControl, DownloadThreadControlFlag,
|
DownloadThreadControl, DownloadThreadControlFlag,
|
||||||
};
|
};
|
||||||
use download_manager::util::progress_object::{ProgressHandle, ProgressObject};
|
use crate::download_manager::util::progress_object::{ProgressHandle, ProgressObject};
|
||||||
use drop_consts::{MAX_FILES_PER_BUCKET, RETRY_COUNT, TARGET_BUCKET_SIZE};
|
use crate::error::application_download_error::ApplicationDownloadError;
|
||||||
|
use crate::error::remote_access_error::RemoteAccessError;
|
||||||
|
use crate::games::downloads::manifest::{
|
||||||
|
DownloadBucket, DownloadContext, DownloadDrop, DropManifest, DropValidateContext, ManifestBody,
|
||||||
|
};
|
||||||
|
use crate::games::downloads::validate::validate_game_chunk;
|
||||||
|
use crate::games::library::{on_game_complete, push_game_update, set_partially_installed};
|
||||||
|
use crate::games::state::GameStatusManager;
|
||||||
|
use crate::process::utils::get_disk_available;
|
||||||
|
use crate::remote::requests::generate_url;
|
||||||
|
use crate::remote::utils::{DROP_CLIENT_ASYNC, DROP_CLIENT_SYNC};
|
||||||
use log::{debug, error, info, warn};
|
use log::{debug, error, info, warn};
|
||||||
use rayon::ThreadPoolBuilder;
|
use rayon::ThreadPoolBuilder;
|
||||||
use remote::auth::generate_authorization_header;
|
|
||||||
use remote::error::RemoteAccessError;
|
|
||||||
use remote::requests::generate_url;
|
|
||||||
use remote::utils::{DROP_CLIENT_ASYNC, DROP_CLIENT_SYNC};
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::fs::{OpenOptions, create_dir_all};
|
use std::fs::{OpenOptions, create_dir_all};
|
||||||
use std::io;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use tauri::AppHandle;
|
use tauri::{AppHandle, Emitter};
|
||||||
use utils::{app_emit, lock, send};
|
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
use rustix::fs::{FallocateFlags, fallocate};
|
use rustix::fs::{FallocateFlags, fallocate};
|
||||||
|
|
||||||
use crate::downloads::manifest::{
|
|
||||||
DownloadBucket, DownloadContext, DownloadDrop, DropManifest, DropValidateContext, ManifestBody,
|
|
||||||
};
|
|
||||||
use crate::downloads::utils::get_disk_available;
|
|
||||||
use crate::downloads::validate::validate_game_chunk;
|
|
||||||
use crate::library::{on_game_complete, push_game_update, set_partially_installed};
|
|
||||||
use crate::state::GameStatusManager;
|
|
||||||
|
|
||||||
use super::download_logic::download_game_bucket;
|
use super::download_logic::download_game_bucket;
|
||||||
use super::drop_data::DropData;
|
use super::drop_data::DropData;
|
||||||
|
|
||||||
|
static RETRY_COUNT: usize = 3;
|
||||||
|
|
||||||
|
const TARGET_BUCKET_SIZE: usize = 63 * 1000 * 1000;
|
||||||
|
const MAX_FILES_PER_BUCKET: usize = (1024 / 4) - 1;
|
||||||
|
|
||||||
pub struct GameDownloadAgent {
|
pub struct GameDownloadAgent {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub version: String,
|
pub version: String,
|
||||||
@ -100,7 +101,10 @@ impl GameDownloadAgent {
|
|||||||
|
|
||||||
result.ensure_manifest_exists().await?;
|
result.ensure_manifest_exists().await?;
|
||||||
|
|
||||||
let required_space = lock!(result.manifest)
|
let required_space = result
|
||||||
|
.manifest
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.values()
|
.values()
|
||||||
@ -168,11 +172,11 @@ impl GameDownloadAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn check_manifest_exists(&self) -> bool {
|
pub fn check_manifest_exists(&self) -> bool {
|
||||||
lock!(self.manifest).is_some()
|
self.manifest.lock().unwrap().is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn ensure_manifest_exists(&self) -> Result<(), ApplicationDownloadError> {
|
pub async fn ensure_manifest_exists(&self) -> Result<(), ApplicationDownloadError> {
|
||||||
if lock!(self.manifest).is_some() {
|
if self.manifest.lock().unwrap().is_some() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,10 +207,7 @@ impl GameDownloadAgent {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let manifest_download: DropManifest = response
|
let manifest_download: DropManifest = response.json().await.unwrap();
|
||||||
.json()
|
|
||||||
.await
|
|
||||||
.map_err(|e| ApplicationDownloadError::Communication(e.into()))?;
|
|
||||||
|
|
||||||
if let Ok(mut manifest) = self.manifest.lock() {
|
if let Ok(mut manifest) = self.manifest.lock() {
|
||||||
*manifest = Some(manifest_download);
|
*manifest = Some(manifest_download);
|
||||||
@ -218,7 +219,7 @@ impl GameDownloadAgent {
|
|||||||
|
|
||||||
// Sets it up for both download and validate
|
// Sets it up for both download and validate
|
||||||
fn setup_progress(&self) {
|
fn setup_progress(&self) {
|
||||||
let buckets = lock!(self.buckets);
|
let buckets = self.buckets.lock().unwrap();
|
||||||
|
|
||||||
let chunk_count = buckets.iter().map(|e| e.drops.len()).sum();
|
let chunk_count = buckets.iter().map(|e| e.drops.len()).sum();
|
||||||
|
|
||||||
@ -233,23 +234,21 @@ impl GameDownloadAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn ensure_buckets(&self) -> Result<(), ApplicationDownloadError> {
|
pub fn ensure_buckets(&self) -> Result<(), ApplicationDownloadError> {
|
||||||
if lock!(self.buckets).is_empty() {
|
if self.buckets.lock().unwrap().is_empty() {
|
||||||
self.generate_buckets()?;
|
self.generate_buckets()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
*lock!(self.context_map) = self.dropdata.get_contexts();
|
*self.context_map.lock().unwrap() = self.dropdata.get_contexts();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_buckets(&self) -> Result<(), ApplicationDownloadError> {
|
pub fn generate_buckets(&self) -> Result<(), ApplicationDownloadError> {
|
||||||
let manifest = lock!(self.manifest)
|
let manifest = self.manifest.lock().unwrap().clone().unwrap();
|
||||||
.clone()
|
|
||||||
.ok_or(ApplicationDownloadError::NotInitialized)?;
|
|
||||||
let game_id = self.id.clone();
|
let game_id = self.id.clone();
|
||||||
|
|
||||||
let base_path = Path::new(&self.dropdata.base_path);
|
let base_path = Path::new(&self.dropdata.base_path);
|
||||||
create_dir_all(base_path)?;
|
create_dir_all(base_path).unwrap();
|
||||||
|
|
||||||
let mut buckets = Vec::new();
|
let mut buckets = Vec::new();
|
||||||
|
|
||||||
@ -259,13 +258,8 @@ impl GameDownloadAgent {
|
|||||||
for (raw_path, chunk) in manifest {
|
for (raw_path, chunk) in manifest {
|
||||||
let path = base_path.join(Path::new(&raw_path));
|
let path = base_path.join(Path::new(&raw_path));
|
||||||
|
|
||||||
let container = path
|
let container = path.parent().unwrap();
|
||||||
.parent()
|
create_dir_all(container).unwrap();
|
||||||
.ok_or(ApplicationDownloadError::IoError(Arc::new(io::Error::new(
|
|
||||||
io::ErrorKind::NotFound,
|
|
||||||
"no parent directory",
|
|
||||||
))))?;
|
|
||||||
create_dir_all(container)?;
|
|
||||||
|
|
||||||
let already_exists = path.exists();
|
let already_exists = path.exists();
|
||||||
let file = OpenOptions::new()
|
let file = OpenOptions::new()
|
||||||
@ -273,7 +267,8 @@ impl GameDownloadAgent {
|
|||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.truncate(false)
|
.truncate(false)
|
||||||
.open(&path)?;
|
.open(path.clone())
|
||||||
|
.unwrap();
|
||||||
let mut file_running_offset = 0;
|
let mut file_running_offset = 0;
|
||||||
|
|
||||||
for (index, length) in chunk.lengths.iter().enumerate() {
|
for (index, length) in chunk.lengths.iter().enumerate() {
|
||||||
@ -357,7 +352,7 @@ impl GameDownloadAgent {
|
|||||||
.collect::<Vec<(String, bool)>>(),
|
.collect::<Vec<(String, bool)>>(),
|
||||||
);
|
);
|
||||||
|
|
||||||
*lock!(self.buckets) = buckets;
|
*self.buckets.lock().unwrap() = buckets;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -373,11 +368,9 @@ impl GameDownloadAgent {
|
|||||||
let pool = ThreadPoolBuilder::new()
|
let pool = ThreadPoolBuilder::new()
|
||||||
.num_threads(max_download_threads)
|
.num_threads(max_download_threads)
|
||||||
.build()
|
.build()
|
||||||
.unwrap_or_else(|_| {
|
.unwrap();
|
||||||
panic!("failed to build thread pool with {max_download_threads} threads")
|
|
||||||
});
|
|
||||||
|
|
||||||
let buckets = lock!(self.buckets);
|
let buckets = self.buckets.lock().unwrap();
|
||||||
|
|
||||||
let mut download_contexts = HashMap::<String, DownloadContext>::new();
|
let mut download_contexts = HashMap::<String, DownloadContext>::new();
|
||||||
|
|
||||||
@ -396,7 +389,7 @@ impl GameDownloadAgent {
|
|||||||
|
|
||||||
for version in versions {
|
for version in versions {
|
||||||
let download_context = DROP_CLIENT_SYNC
|
let download_context = DROP_CLIENT_SYNC
|
||||||
.post(generate_url(&["/api/v2/client/context"], &[])?)
|
.post(generate_url(&["/api/v2/client/context"], &[]).unwrap())
|
||||||
.json(&ManifestBody {
|
.json(&ManifestBody {
|
||||||
game: self.id.clone(),
|
game: self.id.clone(),
|
||||||
version: version.clone(),
|
version: version.clone(),
|
||||||
@ -419,7 +412,7 @@ impl GameDownloadAgent {
|
|||||||
let download_contexts = &download_contexts;
|
let download_contexts = &download_contexts;
|
||||||
|
|
||||||
pool.scope(|scope| {
|
pool.scope(|scope| {
|
||||||
let context_map = lock!(self.context_map);
|
let context_map = self.context_map.lock().unwrap();
|
||||||
for (index, bucket) in buckets.iter().enumerate() {
|
for (index, bucket) in buckets.iter().enumerate() {
|
||||||
let mut bucket = (*bucket).clone();
|
let mut bucket = (*bucket).clone();
|
||||||
let completed_contexts = completed_indexes_loop_arc.clone();
|
let completed_contexts = completed_indexes_loop_arc.clone();
|
||||||
@ -449,13 +442,10 @@ impl GameDownloadAgent {
|
|||||||
|
|
||||||
let sender = self.sender.clone();
|
let sender = self.sender.clone();
|
||||||
|
|
||||||
let download_context =
|
let download_context = download_contexts
|
||||||
download_contexts.get(&bucket.version).unwrap_or_else(|| {
|
.get(&bucket.version)
|
||||||
panic!(
|
.ok_or(RemoteAccessError::CorruptedState)
|
||||||
"Could not get bucket version {}. Corrupted state.",
|
.unwrap();
|
||||||
bucket.version
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
scope.spawn(move |_| {
|
scope.spawn(move |_| {
|
||||||
// 3 attempts
|
// 3 attempts
|
||||||
@ -487,7 +477,7 @@ impl GameDownloadAgent {
|
|||||||
|
|
||||||
if i == RETRY_COUNT - 1 || !retry {
|
if i == RETRY_COUNT - 1 || !retry {
|
||||||
warn!("retry logic failed, not re-attempting.");
|
warn!("retry logic failed, not re-attempting.");
|
||||||
send!(sender, DownloadManagerSignal::Error(e));
|
sender.send(DownloadManagerSignal::Error(e)).unwrap();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -500,7 +490,7 @@ impl GameDownloadAgent {
|
|||||||
let newly_completed = completed_contexts.clone();
|
let newly_completed = completed_contexts.clone();
|
||||||
|
|
||||||
let completed_lock_len = {
|
let completed_lock_len = {
|
||||||
let mut context_map_lock = lock!(self.context_map);
|
let mut context_map_lock = self.context_map.lock().unwrap();
|
||||||
for (_, item) in newly_completed.iter() {
|
for (_, item) in newly_completed.iter() {
|
||||||
context_map_lock.insert(item.clone(), true);
|
context_map_lock.insert(item.clone(), true);
|
||||||
}
|
}
|
||||||
@ -508,7 +498,7 @@ impl GameDownloadAgent {
|
|||||||
context_map_lock.values().filter(|x| **x).count()
|
context_map_lock.values().filter(|x| **x).count()
|
||||||
};
|
};
|
||||||
|
|
||||||
let context_map_lock = lock!(self.context_map);
|
let context_map_lock = self.context_map.lock().unwrap();
|
||||||
let contexts = buckets
|
let contexts = buckets
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|x| x.drops.iter().map(|e| e.checksum.clone()))
|
.flat_map(|x| x.drops.iter().map(|e| e.checksum.clone()))
|
||||||
@ -557,7 +547,7 @@ impl GameDownloadAgent {
|
|||||||
pub fn validate(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError> {
|
pub fn validate(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError> {
|
||||||
self.setup_validate(app_handle);
|
self.setup_validate(app_handle);
|
||||||
|
|
||||||
let buckets = lock!(self.buckets);
|
let buckets = self.buckets.lock().unwrap();
|
||||||
let contexts: Vec<DropValidateContext> = buckets
|
let contexts: Vec<DropValidateContext> = buckets
|
||||||
.clone()
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@ -569,9 +559,7 @@ impl GameDownloadAgent {
|
|||||||
let pool = ThreadPoolBuilder::new()
|
let pool = ThreadPoolBuilder::new()
|
||||||
.num_threads(max_download_threads)
|
.num_threads(max_download_threads)
|
||||||
.build()
|
.build()
|
||||||
.unwrap_or_else(|_| {
|
.unwrap();
|
||||||
panic!("failed to build thread pool with {max_download_threads} threads")
|
|
||||||
});
|
|
||||||
|
|
||||||
let invalid_chunks = Arc::new(boxcar::Vec::new());
|
let invalid_chunks = Arc::new(boxcar::Vec::new());
|
||||||
pool.scope(|scope| {
|
pool.scope(|scope| {
|
||||||
@ -589,7 +577,7 @@ impl GameDownloadAgent {
|
|||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("{e}");
|
error!("{e}");
|
||||||
send!(sender, DownloadManagerSignal::Error(e));
|
sender.send(DownloadManagerSignal::Error(e)).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -616,7 +604,7 @@ impl GameDownloadAgent {
|
|||||||
// See docs on usage
|
// See docs on usage
|
||||||
set_partially_installed(
|
set_partially_installed(
|
||||||
&self.metadata(),
|
&self.metadata(),
|
||||||
self.dropdata.base_path.display().to_string(),
|
self.dropdata.base_path.to_str().unwrap().to_string(),
|
||||||
Some(app_handle),
|
Some(app_handle),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -626,12 +614,12 @@ impl GameDownloadAgent {
|
|||||||
|
|
||||||
impl Downloadable for GameDownloadAgent {
|
impl Downloadable for GameDownloadAgent {
|
||||||
fn download(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError> {
|
fn download(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError> {
|
||||||
*lock!(self.status) = DownloadStatus::Downloading;
|
*self.status.lock().unwrap() = DownloadStatus::Downloading;
|
||||||
self.download(app_handle)
|
self.download(app_handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError> {
|
fn validate(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError> {
|
||||||
*lock!(self.status) = DownloadStatus::Validating;
|
*self.status.lock().unwrap() = DownloadStatus::Validating;
|
||||||
self.validate(app_handle)
|
self.validate(app_handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -665,8 +653,10 @@ impl Downloadable for GameDownloadAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn on_error(&self, app_handle: &tauri::AppHandle, error: &ApplicationDownloadError) {
|
fn on_error(&self, app_handle: &tauri::AppHandle, error: &ApplicationDownloadError) {
|
||||||
*lock!(self.status) = DownloadStatus::Error;
|
*self.status.lock().unwrap() = DownloadStatus::Error;
|
||||||
app_emit!(app_handle, "download_error", error.to_string());
|
app_handle
|
||||||
|
.emit("download_error", error.to_string())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
error!("error while managing download: {error:?}");
|
error!("error while managing download: {error:?}");
|
||||||
|
|
||||||
@ -685,20 +675,12 @@ impl Downloadable for GameDownloadAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn on_complete(&self, app_handle: &tauri::AppHandle) {
|
fn on_complete(&self, app_handle: &tauri::AppHandle) {
|
||||||
match on_game_complete(
|
on_game_complete(
|
||||||
&self.metadata(),
|
&self.metadata(),
|
||||||
self.dropdata.base_path.to_string_lossy().to_string(),
|
self.dropdata.base_path.to_string_lossy().to_string(),
|
||||||
app_handle,
|
app_handle,
|
||||||
) {
|
)
|
||||||
Ok(_) => {}
|
.unwrap();
|
||||||
Err(e) => {
|
|
||||||
error!("could not mark game as complete: {e}");
|
|
||||||
send!(
|
|
||||||
self.sender,
|
|
||||||
DownloadManagerSignal::Error(ApplicationDownloadError::DownloadError(e))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_cancelled(&self, app_handle: &tauri::AppHandle) {
|
fn on_cancelled(&self, app_handle: &tauri::AppHandle) {
|
||||||
@ -707,6 +689,6 @@ impl Downloadable for GameDownloadAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn status(&self) -> DownloadStatus {
|
fn status(&self) -> DownloadStatus {
|
||||||
lock!(self.status).clone()
|
self.status.lock().unwrap().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,3 +1,18 @@
|
|||||||
|
use crate::download_manager::util::download_thread_control_flag::{
|
||||||
|
DownloadThreadControl, DownloadThreadControlFlag,
|
||||||
|
};
|
||||||
|
use crate::download_manager::util::progress_object::ProgressHandle;
|
||||||
|
use crate::error::application_download_error::ApplicationDownloadError;
|
||||||
|
use crate::error::drop_server_error::DropServerError;
|
||||||
|
use crate::error::remote_access_error::RemoteAccessError;
|
||||||
|
use crate::games::downloads::manifest::{ChunkBody, DownloadBucket, DownloadContext, DownloadDrop};
|
||||||
|
use crate::remote::auth::generate_authorization_header;
|
||||||
|
use crate::remote::requests::generate_url;
|
||||||
|
use crate::remote::utils::DROP_CLIENT_SYNC;
|
||||||
|
use log::{debug, info, warn};
|
||||||
|
use md5::{Context, Digest};
|
||||||
|
use reqwest::blocking::Response;
|
||||||
|
|
||||||
use std::fs::{Permissions, set_permissions};
|
use std::fs::{Permissions, set_permissions};
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
@ -10,21 +25,6 @@ use std::{
|
|||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
use download_manager::error::ApplicationDownloadError;
|
|
||||||
use download_manager::util::download_thread_control_flag::{
|
|
||||||
DownloadThreadControl, DownloadThreadControlFlag,
|
|
||||||
};
|
|
||||||
use download_manager::util::progress_object::ProgressHandle;
|
|
||||||
use log::{debug, info, warn};
|
|
||||||
use md5::{Context, Digest};
|
|
||||||
use remote::auth::generate_authorization_header;
|
|
||||||
use remote::error::{DropServerError, RemoteAccessError};
|
|
||||||
use remote::requests::generate_url;
|
|
||||||
use remote::utils::DROP_CLIENT_SYNC;
|
|
||||||
use reqwest::blocking::Response;
|
|
||||||
|
|
||||||
use crate::downloads::manifest::{ChunkBody, DownloadBucket, DownloadContext, DownloadDrop};
|
|
||||||
|
|
||||||
static MAX_PACKET_LENGTH: usize = 4096 * 4;
|
static MAX_PACKET_LENGTH: usize = 4096 * 4;
|
||||||
static BUMP_SIZE: usize = 4096 * 16;
|
static BUMP_SIZE: usize = 4096 * 16;
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ impl DropWriter<File> {
|
|||||||
|
|
||||||
fn finish(mut self) -> io::Result<Digest> {
|
fn finish(mut self) -> io::Result<Digest> {
|
||||||
self.flush()?;
|
self.flush()?;
|
||||||
Ok(self.hasher.finalize())
|
Ok(self.hasher.compute())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Write automatically pushes to file and hasher
|
// Write automatically pushes to file and hasher
|
||||||
@ -110,20 +110,18 @@ impl<'a> DropDownloadPipeline<'a, Response, File> {
|
|||||||
let destination = self
|
let destination = self
|
||||||
.destination
|
.destination
|
||||||
.get_mut(index)
|
.get_mut(index)
|
||||||
.ok_or(io::Error::other("no destination"))?;
|
.ok_or(io::Error::other("no destination"))
|
||||||
|
.unwrap();
|
||||||
let mut remaining = drop.length;
|
let mut remaining = drop.length;
|
||||||
if drop.start != 0 {
|
if drop.start != 0 {
|
||||||
destination.seek(SeekFrom::Start(drop.start as u64))?;
|
destination.seek(SeekFrom::Start(drop.start.try_into().unwrap()))?;
|
||||||
}
|
}
|
||||||
let mut last_bump = 0;
|
let mut last_bump = 0;
|
||||||
loop {
|
loop {
|
||||||
let size = MAX_PACKET_LENGTH.min(remaining);
|
let size = MAX_PACKET_LENGTH.min(remaining);
|
||||||
let size = self
|
let size = self.source.read(&mut copy_buffer[0..size]).inspect_err(|_| {
|
||||||
.source
|
info!("got error from {}", drop.filename);
|
||||||
.read(&mut copy_buffer[0..size])
|
})?;
|
||||||
.inspect_err(|_| {
|
|
||||||
info!("got error from {}", drop.filename);
|
|
||||||
})?;
|
|
||||||
remaining -= size;
|
remaining -= size;
|
||||||
last_bump += size;
|
last_bump += size;
|
||||||
|
|
||||||
@ -217,39 +215,20 @@ pub fn download_game_bucket(
|
|||||||
RemoteAccessError::UnparseableResponse("missing Content-Lengths header".to_owned()),
|
RemoteAccessError::UnparseableResponse("missing Content-Lengths header".to_owned()),
|
||||||
))?
|
))?
|
||||||
.to_str()
|
.to_str()
|
||||||
.map_err(|e| {
|
.unwrap();
|
||||||
ApplicationDownloadError::Communication(RemoteAccessError::UnparseableResponse(
|
|
||||||
e.to_string(),
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
|
|
||||||
for (i, raw_length) in lengths.split(",").enumerate() {
|
for (i, raw_length) in lengths.split(",").enumerate() {
|
||||||
let length = raw_length.parse::<usize>().unwrap_or(0);
|
let length = raw_length.parse::<usize>().unwrap_or(0);
|
||||||
let Some(drop) = bucket.drops.get(i) else {
|
let Some(drop) = bucket.drops.get(i) else {
|
||||||
warn!("invalid number of Content-Lengths recieved: {i}, {lengths}");
|
warn!("invalid number of Content-Lengths recieved: {i}, {lengths}");
|
||||||
return Err(ApplicationDownloadError::DownloadError(
|
return Err(ApplicationDownloadError::DownloadError);
|
||||||
RemoteAccessError::InvalidResponse(DropServerError {
|
|
||||||
status_code: 400,
|
|
||||||
status_message: format!(
|
|
||||||
"invalid number of Content-Lengths recieved: {i}, {lengths}"
|
|
||||||
),
|
|
||||||
}),
|
|
||||||
));
|
|
||||||
};
|
};
|
||||||
if drop.length != length {
|
if drop.length != length {
|
||||||
warn!(
|
warn!(
|
||||||
"for {}, expected {}, got {} ({})",
|
"for {}, expected {}, got {} ({})",
|
||||||
drop.filename, drop.length, raw_length, length
|
drop.filename, drop.length, raw_length, length
|
||||||
);
|
);
|
||||||
return Err(ApplicationDownloadError::DownloadError(
|
return Err(ApplicationDownloadError::DownloadError);
|
||||||
RemoteAccessError::InvalidResponse(DropServerError {
|
|
||||||
status_code: 400,
|
|
||||||
status_message: format!(
|
|
||||||
"for {}, expected {}, got {} ({})",
|
|
||||||
drop.filename, drop.length, raw_length, length
|
|
||||||
),
|
|
||||||
}),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1,17 +1,14 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap, fs::File, io::{self, Read, Write}, path::{Path, PathBuf}
|
||||||
fs::File,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
path::{Path, PathBuf},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use drop_consts::DROP_DATA_PATH;
|
|
||||||
use log::error;
|
use log::error;
|
||||||
use native_model::{Decode, Encode};
|
use native_model::{Decode, Encode};
|
||||||
use utils::lock;
|
|
||||||
|
|
||||||
pub type DropData = v1::DropData;
|
pub type DropData = v1::DropData;
|
||||||
|
|
||||||
|
pub static DROP_DATA_PATH: &str = ".dropdata";
|
||||||
|
|
||||||
pub mod v1 {
|
pub mod v1 {
|
||||||
use std::{collections::HashMap, path::PathBuf, sync::Mutex};
|
use std::{collections::HashMap, path::PathBuf, sync::Mutex};
|
||||||
|
|
||||||
@ -52,12 +49,7 @@ impl DropData {
|
|||||||
let mut s = Vec::new();
|
let mut s = Vec::new();
|
||||||
file.read_to_end(&mut s)?;
|
file.read_to_end(&mut s)?;
|
||||||
|
|
||||||
native_model::rmp_serde_1_3::RmpSerde::decode(s).map_err(|e| {
|
Ok(native_model::rmp_serde_1_3::RmpSerde::decode(s).unwrap())
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::InvalidData,
|
|
||||||
format!("Failed to decode drop data: {e}"),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
pub fn write(&self) {
|
pub fn write(&self) {
|
||||||
let manifest_raw = match native_model::rmp_serde_1_3::RmpSerde::encode(&self) {
|
let manifest_raw = match native_model::rmp_serde_1_3::RmpSerde::encode(&self) {
|
||||||
@ -79,15 +71,12 @@ impl DropData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn set_contexts(&self, completed_contexts: &[(String, bool)]) {
|
pub fn set_contexts(&self, completed_contexts: &[(String, bool)]) {
|
||||||
*lock!(self.contexts) = completed_contexts
|
*self.contexts.lock().unwrap() = completed_contexts.iter().map(|s| (s.0.clone(), s.1)).collect();
|
||||||
.iter()
|
|
||||||
.map(|s| (s.0.clone(), s.1))
|
|
||||||
.collect();
|
|
||||||
}
|
}
|
||||||
pub fn set_context(&self, context: String, state: bool) {
|
pub fn set_context(&self, context: String, state: bool) {
|
||||||
lock!(self.contexts).entry(context).insert_entry(state);
|
self.contexts.lock().unwrap().entry(context).insert_entry(state);
|
||||||
}
|
}
|
||||||
pub fn get_contexts(&self) -> HashMap<String, bool> {
|
pub fn get_contexts(&self) -> HashMap<String, bool> {
|
||||||
lock!(self.contexts).clone()
|
self.contexts.lock().unwrap().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,7 +1,6 @@
|
|||||||
|
pub mod commands;
|
||||||
pub mod download_agent;
|
pub mod download_agent;
|
||||||
mod download_logic;
|
mod download_logic;
|
||||||
pub mod drop_data;
|
pub mod drop_data;
|
||||||
pub mod error;
|
|
||||||
mod manifest;
|
mod manifest;
|
||||||
pub mod utils;
|
|
||||||
pub mod validate;
|
pub mod validate;
|
||||||
@ -3,17 +3,17 @@ use std::{
|
|||||||
io::{self, BufWriter, Read, Seek, SeekFrom, Write},
|
io::{self, BufWriter, Read, Seek, SeekFrom, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
use download_manager::{
|
|
||||||
error::ApplicationDownloadError,
|
|
||||||
util::{
|
|
||||||
download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag},
|
|
||||||
progress_object::ProgressHandle,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use md5::Context;
|
use md5::Context;
|
||||||
|
|
||||||
use crate::downloads::manifest::DropValidateContext;
|
use crate::{
|
||||||
|
download_manager::util::{
|
||||||
|
download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag},
|
||||||
|
progress_object::ProgressHandle,
|
||||||
|
},
|
||||||
|
error::application_download_error::ApplicationDownloadError,
|
||||||
|
games::downloads::manifest::DropValidateContext,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn validate_game_chunk(
|
pub fn validate_game_chunk(
|
||||||
ctx: &DropValidateContext,
|
ctx: &DropValidateContext,
|
||||||
@ -22,10 +22,7 @@ pub fn validate_game_chunk(
|
|||||||
) -> Result<bool, ApplicationDownloadError> {
|
) -> Result<bool, ApplicationDownloadError> {
|
||||||
debug!(
|
debug!(
|
||||||
"Starting chunk validation {}, {}, {} #{}",
|
"Starting chunk validation {}, {}, {} #{}",
|
||||||
ctx.path.display(),
|
ctx.path.display(), ctx.index, ctx.offset, ctx.checksum
|
||||||
ctx.index,
|
|
||||||
ctx.offset,
|
|
||||||
ctx.checksum
|
|
||||||
);
|
);
|
||||||
// If we're paused
|
// If we're paused
|
||||||
if control_flag.get() == DownloadThreadControlFlag::Stop {
|
if control_flag.get() == DownloadThreadControlFlag::Stop {
|
||||||
@ -39,18 +36,19 @@ pub fn validate_game_chunk(
|
|||||||
|
|
||||||
if ctx.offset != 0 {
|
if ctx.offset != 0 {
|
||||||
source
|
source
|
||||||
.seek(SeekFrom::Start(ctx.offset as u64))
|
.seek(SeekFrom::Start(ctx.offset.try_into().unwrap()))
|
||||||
.expect("Failed to seek to file offset");
|
.expect("Failed to seek to file offset");
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut hasher = md5::Context::new();
|
let mut hasher = md5::Context::new();
|
||||||
|
|
||||||
let completed = validate_copy(&mut source, &mut hasher, ctx.length, control_flag, progress)?;
|
let completed =
|
||||||
|
validate_copy(&mut source, &mut hasher, ctx.length, control_flag, progress).unwrap();
|
||||||
if !completed {
|
if !completed {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = hex::encode(hasher.finalize().0);
|
let res = hex::encode(hasher.compute().0);
|
||||||
if res != ctx.checksum {
|
if res != ctx.checksum {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
591
src-tauri/src/games/library.rs
Normal file
591
src-tauri/src/games/library.rs
Normal file
@ -0,0 +1,591 @@
|
|||||||
|
use std::fs::remove_dir_all;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
use std::thread::spawn;
|
||||||
|
|
||||||
|
use log::{debug, error, warn};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use tauri::AppHandle;
|
||||||
|
use tauri::Emitter;
|
||||||
|
|
||||||
|
use crate::AppState;
|
||||||
|
use crate::database::db::{borrow_db_checked, borrow_db_mut_checked};
|
||||||
|
use crate::database::models::data::Database;
|
||||||
|
use crate::database::models::data::{
|
||||||
|
ApplicationTransientStatus, DownloadableMetadata, GameDownloadStatus, GameVersion,
|
||||||
|
};
|
||||||
|
use crate::download_manager::download_manager_frontend::DownloadStatus;
|
||||||
|
use crate::error::drop_server_error::DropServerError;
|
||||||
|
use crate::error::library_error::LibraryError;
|
||||||
|
use crate::error::remote_access_error::RemoteAccessError;
|
||||||
|
use crate::games::state::{GameStatusManager, GameStatusWithTransient};
|
||||||
|
use crate::remote::auth::generate_authorization_header;
|
||||||
|
use crate::remote::cache::cache_object_db;
|
||||||
|
use crate::remote::cache::{cache_object, get_cached_object, get_cached_object_db};
|
||||||
|
use crate::remote::requests::generate_url;
|
||||||
|
use crate::remote::utils::DROP_CLIENT_ASYNC;
|
||||||
|
use crate::remote::utils::DROP_CLIENT_SYNC;
|
||||||
|
use bitcode::{Decode, Encode};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct FetchGameStruct {
|
||||||
|
game: Game,
|
||||||
|
status: GameStatusWithTransient,
|
||||||
|
version: Option<GameVersion>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug, Default, Encode, Decode)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct Game {
|
||||||
|
id: String,
|
||||||
|
m_name: String,
|
||||||
|
m_short_description: String,
|
||||||
|
m_description: String,
|
||||||
|
// mDevelopers
|
||||||
|
// mPublishers
|
||||||
|
m_icon_object_id: String,
|
||||||
|
m_banner_object_id: String,
|
||||||
|
m_cover_object_id: String,
|
||||||
|
m_image_library_object_ids: Vec<String>,
|
||||||
|
m_image_carousel_object_ids: Vec<String>,
|
||||||
|
}
|
||||||
|
#[derive(serde::Serialize, Clone)]
|
||||||
|
pub struct GameUpdateEvent {
|
||||||
|
pub game_id: String,
|
||||||
|
pub status: (
|
||||||
|
Option<GameDownloadStatus>,
|
||||||
|
Option<ApplicationTransientStatus>,
|
||||||
|
),
|
||||||
|
pub version: Option<GameVersion>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub struct QueueUpdateEventQueueData {
|
||||||
|
pub meta: DownloadableMetadata,
|
||||||
|
pub status: DownloadStatus,
|
||||||
|
pub progress: f64,
|
||||||
|
pub current: usize,
|
||||||
|
pub max: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, Clone)]
|
||||||
|
pub struct QueueUpdateEvent {
|
||||||
|
pub queue: Vec<QueueUpdateEventQueueData>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, Clone)]
|
||||||
|
pub struct StatsUpdateEvent {
|
||||||
|
pub speed: usize,
|
||||||
|
pub time: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_library_logic(
|
||||||
|
state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
|
hard_fresh: Option<bool>,
|
||||||
|
) -> Result<Vec<Game>, RemoteAccessError> {
|
||||||
|
let do_hard_refresh = hard_fresh.unwrap_or(false);
|
||||||
|
if !do_hard_refresh && let Ok(library) = get_cached_object("library") {
|
||||||
|
return Ok(library);
|
||||||
|
}
|
||||||
|
|
||||||
|
let client = DROP_CLIENT_ASYNC.clone();
|
||||||
|
let response = generate_url(&["/api/v1/client/user/library"], &[])?;
|
||||||
|
let response = client
|
||||||
|
.get(response)
|
||||||
|
.header("Authorization", generate_authorization_header())
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if response.status() != 200 {
|
||||||
|
let err = response.json().await.unwrap_or(DropServerError {
|
||||||
|
status_code: 500,
|
||||||
|
status_message: "Invalid response from server.".to_owned(),
|
||||||
|
});
|
||||||
|
warn!("{err:?}");
|
||||||
|
return Err(RemoteAccessError::InvalidResponse(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut games: Vec<Game> = response.json().await?;
|
||||||
|
|
||||||
|
let mut handle = state.lock().unwrap();
|
||||||
|
|
||||||
|
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 {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
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) {
|
||||||
|
Ok(game) => game,
|
||||||
|
Err(err) => {
|
||||||
|
warn!(
|
||||||
|
"{} is installed, but encountered error fetching its error: {}.",
|
||||||
|
meta.id, err
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
games.push(game);
|
||||||
|
}
|
||||||
|
|
||||||
|
drop(handle);
|
||||||
|
drop(db_handle);
|
||||||
|
cache_object("library", &games)?;
|
||||||
|
|
||||||
|
Ok(games)
|
||||||
|
}
|
||||||
|
pub async fn fetch_library_logic_offline(
|
||||||
|
_state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
|
_hard_refresh: Option<bool>,
|
||||||
|
) -> Result<Vec<Game>, RemoteAccessError> {
|
||||||
|
let mut games: Vec<Game> = get_cached_object("library")?;
|
||||||
|
|
||||||
|
let db_handle = borrow_db_checked();
|
||||||
|
|
||||||
|
games.retain(|game| {
|
||||||
|
matches!(
|
||||||
|
&db_handle
|
||||||
|
.applications
|
||||||
|
.game_statuses
|
||||||
|
.get(&game.id)
|
||||||
|
.unwrap_or(&GameDownloadStatus::Remote {}),
|
||||||
|
GameDownloadStatus::Installed { .. } | GameDownloadStatus::SetupRequired { .. }
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(games)
|
||||||
|
}
|
||||||
|
pub async fn fetch_game_logic(
|
||||||
|
id: String,
|
||||||
|
state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
|
) -> Result<FetchGameStruct, RemoteAccessError> {
|
||||||
|
let version = {
|
||||||
|
let state_handle = state.lock().unwrap();
|
||||||
|
|
||||||
|
let db_lock = borrow_db_checked();
|
||||||
|
|
||||||
|
let metadata_option = db_lock.applications.installed_game_version.get(&id);
|
||||||
|
let version = 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())
|
||||||
|
.cloned(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let game = state_handle.games.get(&id);
|
||||||
|
if let Some(game) = game {
|
||||||
|
let status = GameStatusManager::fetch_state(&id, &db_lock);
|
||||||
|
|
||||||
|
let data = FetchGameStruct {
|
||||||
|
game: 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?;
|
||||||
|
|
||||||
|
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.unwrap();
|
||||||
|
warn!("{err:?}");
|
||||||
|
return Err(RemoteAccessError::InvalidResponse(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
let game: Game = response.json().await?;
|
||||||
|
|
||||||
|
let mut state_handle = state.lock().unwrap();
|
||||||
|
state_handle.games.insert(id.clone(), game.clone());
|
||||||
|
|
||||||
|
let mut db_handle = borrow_db_mut_checked();
|
||||||
|
|
||||||
|
db_handle
|
||||||
|
.applications
|
||||||
|
.game_statuses
|
||||||
|
.entry(id.clone())
|
||||||
|
.or_insert(GameDownloadStatus::Remote {});
|
||||||
|
|
||||||
|
let status = GameStatusManager::fetch_state(&id, &db_handle);
|
||||||
|
|
||||||
|
drop(db_handle);
|
||||||
|
|
||||||
|
let data = FetchGameStruct {
|
||||||
|
game: game.clone(),
|
||||||
|
status,
|
||||||
|
version,
|
||||||
|
};
|
||||||
|
|
||||||
|
cache_object(&id, &game)?;
|
||||||
|
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_game_logic_offline(
|
||||||
|
id: String,
|
||||||
|
_state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
|
) -> Result<FetchGameStruct, RemoteAccessError> {
|
||||||
|
let db_handle = borrow_db_checked();
|
||||||
|
let metadata_option = db_handle.applications.installed_game_version.get(&id);
|
||||||
|
let version = match metadata_option {
|
||||||
|
None => None,
|
||||||
|
Some(metadata) => db_handle
|
||||||
|
.applications
|
||||||
|
.game_versions
|
||||||
|
.get(&metadata.id)
|
||||||
|
.map(|v| v.get(metadata.version.as_ref().unwrap()).unwrap())
|
||||||
|
.cloned(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let status = GameStatusManager::fetch_state(&id, &db_handle);
|
||||||
|
let game = get_cached_object::<Game>(&id)?;
|
||||||
|
|
||||||
|
drop(db_handle);
|
||||||
|
|
||||||
|
Ok(FetchGameStruct {
|
||||||
|
game,
|
||||||
|
status,
|
||||||
|
version,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_game_version_options_logic(
|
||||||
|
game_id: String,
|
||||||
|
state: tauri::State<'_, Mutex<AppState<'_>>>,
|
||||||
|
) -> Result<Vec<GameVersion>, RemoteAccessError> {
|
||||||
|
let client = DROP_CLIENT_ASYNC.clone();
|
||||||
|
|
||||||
|
let response = generate_url(&["/api/v1/client/game/versions"], &[("id", &game_id)])?;
|
||||||
|
let response = client
|
||||||
|
.get(response)
|
||||||
|
.header("Authorization", generate_authorization_header())
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if response.status() != 200 {
|
||||||
|
let err = response.json().await.unwrap();
|
||||||
|
warn!("{err:?}");
|
||||||
|
return Err(RemoteAccessError::InvalidResponse(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
let data: Vec<GameVersion> = response.json().await?;
|
||||||
|
|
||||||
|
let state_lock = state.lock().unwrap();
|
||||||
|
let process_manager_lock = state_lock.process_manager.lock().unwrap();
|
||||||
|
let data: Vec<GameVersion> = data
|
||||||
|
.into_iter()
|
||||||
|
.filter(|v| {
|
||||||
|
process_manager_lock
|
||||||
|
.valid_platform(&v.platform, &state_lock)
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
drop(process_manager_lock);
|
||||||
|
drop(state_lock);
|
||||||
|
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called by:
|
||||||
|
* - on_cancel, when cancelled, for obvious reasons
|
||||||
|
* - when downloading, so if drop unexpectedly quits, we can resume the download. hidden by the "Downloading..." transient state, though
|
||||||
|
* - when scanning, to import the game
|
||||||
|
*/
|
||||||
|
pub fn set_partially_installed(
|
||||||
|
meta: &DownloadableMetadata,
|
||||||
|
install_dir: String,
|
||||||
|
app_handle: Option<&AppHandle>,
|
||||||
|
) {
|
||||||
|
set_partially_installed_db(&mut borrow_db_mut_checked(), meta, install_dir, app_handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_partially_installed_db(
|
||||||
|
db_lock: &mut Database,
|
||||||
|
meta: &DownloadableMetadata,
|
||||||
|
install_dir: String,
|
||||||
|
app_handle: Option<&AppHandle>,
|
||||||
|
) {
|
||||||
|
db_lock.applications.transient_statuses.remove(meta);
|
||||||
|
db_lock.applications.game_statuses.insert(
|
||||||
|
meta.id.clone(),
|
||||||
|
GameDownloadStatus::PartiallyInstalled {
|
||||||
|
version_name: meta.version.as_ref().unwrap().clone(),
|
||||||
|
install_dir,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
db_lock
|
||||||
|
.applications
|
||||||
|
.installed_game_version
|
||||||
|
.insert(meta.id.clone(), meta.clone());
|
||||||
|
|
||||||
|
if let Some(app_handle) = app_handle {
|
||||||
|
push_game_update(
|
||||||
|
app_handle,
|
||||||
|
&meta.id,
|
||||||
|
None,
|
||||||
|
GameStatusManager::fetch_state(&meta.id, db_lock),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn uninstall_game_logic(meta: DownloadableMetadata, app_handle: &AppHandle) {
|
||||||
|
debug!("triggered uninstall for agent");
|
||||||
|
let mut db_handle = borrow_db_mut_checked();
|
||||||
|
db_handle
|
||||||
|
.applications
|
||||||
|
.transient_statuses
|
||||||
|
.insert(meta.clone(), ApplicationTransientStatus::Uninstalling {});
|
||||||
|
|
||||||
|
push_game_update(
|
||||||
|
app_handle,
|
||||||
|
&meta.id,
|
||||||
|
None,
|
||||||
|
GameStatusManager::fetch_state(&meta.id, &db_handle),
|
||||||
|
);
|
||||||
|
|
||||||
|
let previous_state = db_handle.applications.game_statuses.get(&meta.id).cloned();
|
||||||
|
if previous_state.is_none() {
|
||||||
|
warn!("uninstall job doesn't have previous state, failing silently");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let previous_state = previous_state.unwrap();
|
||||||
|
|
||||||
|
if let Some((_, install_dir)) = match previous_state {
|
||||||
|
GameDownloadStatus::Installed {
|
||||||
|
version_name,
|
||||||
|
install_dir,
|
||||||
|
} => Some((version_name, install_dir)),
|
||||||
|
GameDownloadStatus::SetupRequired {
|
||||||
|
version_name,
|
||||||
|
install_dir,
|
||||||
|
} => Some((version_name, install_dir)),
|
||||||
|
GameDownloadStatus::PartiallyInstalled {
|
||||||
|
version_name,
|
||||||
|
install_dir,
|
||||||
|
} => Some((version_name, install_dir)),
|
||||||
|
_ => None,
|
||||||
|
} {
|
||||||
|
db_handle
|
||||||
|
.applications
|
||||||
|
.transient_statuses
|
||||||
|
.insert(meta.clone(), ApplicationTransientStatus::Uninstalling {});
|
||||||
|
|
||||||
|
drop(db_handle);
|
||||||
|
|
||||||
|
let app_handle = app_handle.clone();
|
||||||
|
spawn(move || {
|
||||||
|
if let Err(e) = remove_dir_all(install_dir) {
|
||||||
|
error!("{e}");
|
||||||
|
} else {
|
||||||
|
let mut db_handle = borrow_db_mut_checked();
|
||||||
|
db_handle.applications.transient_statuses.remove(&meta);
|
||||||
|
db_handle
|
||||||
|
.applications
|
||||||
|
.installed_game_version
|
||||||
|
.remove(&meta.id);
|
||||||
|
db_handle
|
||||||
|
.applications
|
||||||
|
.game_statuses
|
||||||
|
.insert(meta.id.clone(), GameDownloadStatus::Remote {});
|
||||||
|
let _ = db_handle.applications.transient_statuses.remove(&meta);
|
||||||
|
|
||||||
|
push_game_update(
|
||||||
|
&app_handle,
|
||||||
|
&meta.id,
|
||||||
|
None,
|
||||||
|
GameStatusManager::fetch_state(&meta.id, &db_handle),
|
||||||
|
);
|
||||||
|
|
||||||
|
debug!("uninstalled game id {}", &meta.id);
|
||||||
|
app_handle.emit("update_library", ()).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
warn!("invalid previous state for uninstall, failing silently.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_current_meta(game_id: &String) -> Option<DownloadableMetadata> {
|
||||||
|
borrow_db_checked()
|
||||||
|
.applications
|
||||||
|
.installed_game_version
|
||||||
|
.get(game_id)
|
||||||
|
.cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_game_complete(
|
||||||
|
meta: &DownloadableMetadata,
|
||||||
|
install_dir: String,
|
||||||
|
app_handle: &AppHandle,
|
||||||
|
) -> Result<(), RemoteAccessError> {
|
||||||
|
// Fetch game version information from remote
|
||||||
|
if meta.version.is_none() {
|
||||||
|
return Err(RemoteAccessError::GameNotFound(meta.id.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let client = DROP_CLIENT_SYNC.clone();
|
||||||
|
let response = generate_url(
|
||||||
|
&["/api/v1/client/game/version"],
|
||||||
|
&[
|
||||||
|
("id", &meta.id),
|
||||||
|
("version", meta.version.as_ref().unwrap()),
|
||||||
|
],
|
||||||
|
)?;
|
||||||
|
let response = client
|
||||||
|
.get(response)
|
||||||
|
.header("Authorization", generate_authorization_header())
|
||||||
|
.send()?;
|
||||||
|
|
||||||
|
let game_version: GameVersion = response.json()?;
|
||||||
|
|
||||||
|
let mut handle = borrow_db_mut_checked();
|
||||||
|
handle
|
||||||
|
.applications
|
||||||
|
.game_versions
|
||||||
|
.entry(meta.id.clone())
|
||||||
|
.or_default()
|
||||||
|
.insert(meta.version.clone().unwrap(), game_version.clone());
|
||||||
|
handle
|
||||||
|
.applications
|
||||||
|
.installed_game_version
|
||||||
|
.insert(meta.id.clone(), meta.clone());
|
||||||
|
|
||||||
|
drop(handle);
|
||||||
|
|
||||||
|
let status = if game_version.setup_command.is_empty() {
|
||||||
|
GameDownloadStatus::Installed {
|
||||||
|
version_name: meta.version.clone().unwrap(),
|
||||||
|
install_dir,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
GameDownloadStatus::SetupRequired {
|
||||||
|
version_name: meta.version.clone().unwrap(),
|
||||||
|
install_dir,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut db_handle = borrow_db_mut_checked();
|
||||||
|
db_handle
|
||||||
|
.applications
|
||||||
|
.game_statuses
|
||||||
|
.insert(meta.id.clone(), status.clone());
|
||||||
|
drop(db_handle);
|
||||||
|
|
||||||
|
app_handle
|
||||||
|
.emit(
|
||||||
|
&format!("update_game/{}", meta.id),
|
||||||
|
GameUpdateEvent {
|
||||||
|
game_id: meta.id.clone(),
|
||||||
|
status: (Some(status), None),
|
||||||
|
version: Some(game_version),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push_game_update(
|
||||||
|
app_handle: &AppHandle,
|
||||||
|
game_id: &String,
|
||||||
|
version: Option<GameVersion>,
|
||||||
|
status: GameStatusWithTransient,
|
||||||
|
) {
|
||||||
|
if let Some(GameDownloadStatus::Installed { .. } | GameDownloadStatus::SetupRequired { .. }) =
|
||||||
|
&status.0
|
||||||
|
&& version.is_none()
|
||||||
|
{
|
||||||
|
panic!("pushed game for installed game that doesn't have version information");
|
||||||
|
}
|
||||||
|
|
||||||
|
app_handle
|
||||||
|
.emit(
|
||||||
|
&format!("update_game/{game_id}"),
|
||||||
|
GameUpdateEvent {
|
||||||
|
game_id: game_id.clone(),
|
||||||
|
status,
|
||||||
|
version,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct FrontendGameOptions {
|
||||||
|
launch_string: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn update_game_configuration(
|
||||||
|
game_id: String,
|
||||||
|
options: FrontendGameOptions,
|
||||||
|
) -> Result<(), LibraryError> {
|
||||||
|
let mut handle = borrow_db_mut_checked();
|
||||||
|
let installed_version = handle
|
||||||
|
.applications
|
||||||
|
.installed_game_version
|
||||||
|
.get(&game_id)
|
||||||
|
.ok_or(LibraryError::MetaNotFound(game_id))?;
|
||||||
|
|
||||||
|
let id = installed_version.id.clone();
|
||||||
|
let version = installed_version.version.clone().unwrap();
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Add no more options past here
|
||||||
|
|
||||||
|
handle
|
||||||
|
.applications
|
||||||
|
.game_versions
|
||||||
|
.get_mut(&id)
|
||||||
|
.unwrap()
|
||||||
|
.insert(version.to_string(), existing_configuration);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user