feat(settings): Added max_download_threads setting and separated settings from db

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2025-01-06 07:36:35 +11:00
parent 2822b7a593
commit 5ea47d733b
7 changed files with 154 additions and 29 deletions

92
src-tauri/Cargo.lock generated
View File

@ -846,6 +846,37 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "derive_builder"
version = "0.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "507dfb09ea8b7fa618fcf76e953f4f5e192547945816d5358edffe39f6f94947"
dependencies = [
"derive_builder_macro",
]
[[package]]
name = "derive_builder_core"
version = "0.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d5bcf7b024d6835cfb3d473887cd966994907effbe9227e8c8219824d06c4e8"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 2.0.91",
]
[[package]]
name = "derive_builder_macro"
version = "0.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab63b0e2bf4d5928aff72e83a7dace85d7bba5fe12dcc3c5a572d78caffd3f3c"
dependencies = [
"derive_builder_core",
"syn 2.0.91",
]
[[package]]
name = "derive_more"
version = "0.99.18"
@ -1004,6 +1035,7 @@ version = "0.1.0"
dependencies = [
"boxcar",
"chrono",
"derive_builder",
"directories",
"hex",
"http",
@ -1011,6 +1043,7 @@ dependencies = [
"log4rs",
"md5",
"openssl",
"parking_lot 0.12.3",
"rayon",
"reqwest",
"rustbreak",
@ -1029,6 +1062,7 @@ dependencies = [
"tauri-plugin-os",
"tauri-plugin-shell",
"tauri-plugin-single-instance",
"throttle_my_fn",
"tokio",
"umu-wrapper-lib",
"url",
@ -2342,7 +2376,7 @@ dependencies = [
"log",
"log-mdc",
"once_cell",
"parking_lot",
"parking_lot 0.12.3",
"rand 0.8.5",
"serde",
"serde-value",
@ -2968,6 +3002,17 @@ version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba"
[[package]]
name = "parking_lot"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
dependencies = [
"instant",
"lock_api",
"parking_lot_core 0.8.6",
]
[[package]]
name = "parking_lot"
version = "0.12.3"
@ -2975,7 +3020,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [
"lock_api",
"parking_lot_core",
"parking_lot_core 0.9.10",
]
[[package]]
name = "parking_lot_core"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc"
dependencies = [
"cfg-if",
"instant",
"libc",
"redox_syscall 0.2.16",
"smallvec",
"winapi",
]
[[package]]
@ -2986,7 +3045,7 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"redox_syscall 0.5.8",
"smallvec",
"windows-targets 0.52.6",
]
@ -3421,6 +3480,15 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "redox_syscall"
version = "0.5.8"
@ -4051,7 +4119,7 @@ dependencies = [
"objc2-foundation",
"objc2-quartz-core",
"raw-window-handle",
"redox_syscall",
"redox_syscall 0.5.8",
"wasm-bindgen",
"web-sys",
"windows-sys 0.59.0",
@ -4109,7 +4177,7 @@ checksum = "f91138e76242f575eb1d3b38b4f1362f10d3a43f47d182a5b359af488a02293b"
dependencies = [
"new_debug_unreachable",
"once_cell",
"parking_lot",
"parking_lot 0.12.3",
"phf_shared 0.10.0",
"precomputed-hash",
"serde",
@ -4262,7 +4330,7 @@ dependencies = [
"ndk-sys",
"objc",
"once_cell",
"parking_lot",
"parking_lot 0.12.3",
"raw-window-handle",
"scopeguard",
"tao-macros",
@ -4723,6 +4791,18 @@ dependencies = [
"winapi",
]
[[package]]
name = "throttle_my_fn"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "482c185e5675626c9a130b3a8f362c322a239338c882f745a1d9a85838b987f0"
dependencies = [
"parking_lot 0.11.2",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "time"
version = "0.3.37"

View File

@ -47,6 +47,9 @@ tauri-plugin-autostart = "2.0.0"
shared_child = "1.0.1"
serde_with = "3.12.0"
slice-deque = "0.3.0"
derive_builder = "0.20.2"
throttle_my_fn = "0.2.6"
parking_lot = "0.12.3"
[dependencies.tauri]
version = "2.1.1"

View File

@ -15,10 +15,7 @@ use tauri::AppHandle;
use url::Url;
use crate::{
download_manager::downloadable_metadata::DownloadableMetadata,
games::{library::push_game_update, state::GameStatusManager},
process::process_manager::Platform,
DB,
download_manager::downloadable_metadata::DownloadableMetadata, games::{library::push_game_update, state::GameStatusManager}, process::process_manager::Platform, settings::Settings, DB
};
#[derive(serde::Serialize, Clone, Deserialize)]
@ -76,11 +73,6 @@ pub struct DatabaseApplications {
pub transient_statuses: HashMap<DownloadableMetadata, ApplicationTransientStatus>,
}
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct Settings {
pub autostart: bool,
// ... other settings ...
}
#[derive(Serialize, Deserialize, Clone)]
pub struct Database {
@ -267,7 +259,7 @@ fn handle_invalid_database(
installed_game_version: HashMap::new(),
},
prev_database: Some(new_path.into()),
settings: Settings { autostart: false },
settings: Settings::default(),
};
PathDatabase::create_at_path(db_path, db).expect("Database could not be created")

View File

@ -4,10 +4,11 @@ use std::{
mpsc::Sender,
Arc, Mutex, RwLock,
},
time::Instant,
time::{Duration, Instant},
};
use log::info;
use throttle_my_fn::throttle;
use super::download_manager::DownloadManagerSignal;
@ -17,7 +18,6 @@ pub struct ProgressObject {
progress_instances: Arc<Mutex<Vec<Arc<AtomicUsize>>>>,
start: Arc<Mutex<Instant>>,
sender: Sender<DownloadManagerSignal>,
points_towards_update: Arc<AtomicUsize>,
points_to_push_update: Arc<AtomicUsize>,
last_update: Arc<RwLock<Instant>>,
@ -76,9 +76,7 @@ impl ProgressObject {
if current_amount >= to_update {
self.points_towards_update
.fetch_sub(to_update, Ordering::Relaxed);
self.sender
.send(DownloadManagerSignal::UpdateUIQueue)
.unwrap();
update_queue(&self);
}
let last_update = self.last_update.read().unwrap();
@ -103,12 +101,8 @@ impl ProgressObject {
let remaining = max - current_amount; // bytes
let time_remaining = (remaining / 1000) / kilobytes_per_second.max(1);
self.sender
.send(DownloadManagerSignal::UpdateUIStats(
kilobytes_per_second,
time_remaining,
))
.unwrap();
update_ui(&self, kilobytes_per_second, time_remaining);
}
}
@ -143,3 +137,20 @@ impl ProgressObject {
self.progress_instances.lock().unwrap()[index].clone()
}
}
#[throttle(10, Duration::from_secs(1))]
fn update_ui(progress_object: &ProgressObject, kilobytes_per_second: usize, time_remaining: usize) {
progress_object.sender
.send(DownloadManagerSignal::UpdateUIStats(
kilobytes_per_second,
time_remaining,
))
.unwrap();
}
#[throttle(10, Duration::from_secs(1))]
fn update_queue(progress: &ProgressObject) {
progress.sender
.send(DownloadManagerSignal::UpdateUIQueue)
.unwrap();
}

View File

@ -246,10 +246,10 @@ impl GameDownloadAgent {
pub fn run(&self) -> Result<bool, ()> {
info!("downloading game: {}", self.id);
const DOWNLOAD_MAX_THREADS: usize = 1;
let max_download_threads = DB.borrow_data().unwrap().settings.max_download_threads;
let pool = ThreadPoolBuilder::new()
.num_threads(DOWNLOAD_MAX_THREADS)
.num_threads(max_download_threads)
.build()
.unwrap();

View File

@ -8,6 +8,7 @@ mod debug;
pub mod download_manager;
mod process;
mod remote;
pub mod settings;
#[cfg(test)]
mod tests;
mod tools;
@ -45,6 +46,7 @@ use process::process_commands::{kill_game, launch_game};
use process::process_manager::ProcessManager;
use remote::{gen_drop_url, use_remote};
use serde::{Deserialize, Serialize};
use settings::amend_settings;
use std::path::Path;
use std::sync::Arc;
use std::{
@ -228,6 +230,8 @@ pub fn run() {
fetch_state,
quit,
fetch_system_data,
// User utils
amend_settings,
// Auth
auth_initiate,
retry_connect,

35
src-tauri/src/settings.rs Normal file
View File

@ -0,0 +1,35 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::DB;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Settings {
pub autostart: bool,
pub max_download_threads: usize,
// ... other settings ...
}
impl Default for Settings {
fn default() -> Self {
Self {
autostart: false,
max_download_threads: 4
}
}
}
fn deserialize_into<T>(v: serde_json::Value, t: &mut T) -> Result<(), serde_json::Error>
where T: for<'a> Deserialize<'a>
{
*t = serde_json::from_value(v)?;
Ok(())
}
#[tauri::command]
pub fn amend_settings(new_settings: Value) {
let db_lock = DB.borrow_data_mut().unwrap();
let mut current_settings = db_lock.settings.clone();
let e = deserialize_into(new_settings, &mut current_settings);
println!("Amend status: {:?}", e);
println!("New settings: {:?}", current_settings);
}