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

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);
}