From 496c6a57e3f47e9f65aa08d2b8b87e0712789634 Mon Sep 17 00:00:00 2001 From: quexeky Date: Fri, 18 Oct 2024 07:45:09 +1100 Subject: [PATCH] Progress on downloads. Currently working on parsing functions to be run asynchronously --- src-tauri/Cargo.lock | 13 ++++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/downloads/downloads.rs | 22 ++++++++++++++ src-tauri/src/downloads/manifest.rs | 3 ++ src-tauri/src/downloads/mod.rs | 2 ++ src-tauri/src/lib.rs | 4 +++ src-tauri/src/utils.rs | 44 ++++++++++++++++++++++++++++ 7 files changed, 89 insertions(+) create mode 100644 src-tauri/src/downloads/downloads.rs create mode 100644 src-tauri/src/downloads/manifest.rs create mode 100644 src-tauri/src/downloads/mod.rs create mode 100644 src-tauri/src/utils.rs diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index c28ec7e..bc2f4d7 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1037,6 +1037,7 @@ dependencies = [ "tauri-plugin-dialog", "tauri-plugin-shell", "tauri-plugin-single-instance", + "tokio", "url", "uuid", "webbrowser", @@ -4500,10 +4501,22 @@ dependencies = [ "pin-project-lite", "signal-hook-registry", "socket2", + "tokio-macros", "tracing", "windows-sys 0.52.0", ] +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + [[package]] name = "tokio-native-tls" version = "0.3.1" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 00f4a04..edac0cd 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -37,6 +37,7 @@ hex = "0.4.3" tauri-plugin-dialog = "2" env_logger = "0.11.5" http = "1.1.0" +tokio = { version = "1.40.0", features = ["rt", "tokio-macros"] } [dependencies.uuid] version = "1.10.0" diff --git a/src-tauri/src/downloads/downloads.rs b/src-tauri/src/downloads/downloads.rs new file mode 100644 index 0000000..9b53f45 --- /dev/null +++ b/src-tauri/src/downloads/downloads.rs @@ -0,0 +1,22 @@ +/* GENERAL OUTLINE +When downloading any game, the following details must be provided to the server: + - Game ID + - User token + - TBC + +The steps to then download a game are as follows: + 1. User requests + */ +use tauri::AppHandle; +use crate::auth::generate_authorization_header; +use crate::DB; +use crate::db::DatabaseImpls; +use crate::downloads::manifest::Manifest; + +#[tauri::command] +fn download_game(app: AppHandle, game_id: String) -> Result{ + todo!() +} + + + diff --git a/src-tauri/src/downloads/manifest.rs b/src-tauri/src/downloads/manifest.rs new file mode 100644 index 0000000..7df7764 --- /dev/null +++ b/src-tauri/src/downloads/manifest.rs @@ -0,0 +1,3 @@ +pub(crate) struct Manifest { + +} \ No newline at end of file diff --git a/src-tauri/src/downloads/mod.rs b/src-tauri/src/downloads/mod.rs new file mode 100644 index 0000000..c7dc3df --- /dev/null +++ b/src-tauri/src/downloads/mod.rs @@ -0,0 +1,2 @@ +mod downloads; +mod manifest; \ No newline at end of file diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 0aefa92..e91a19e 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -3,6 +3,8 @@ mod db; mod library; mod remote; mod unpacker; +mod downloads; +mod utils; use auth::{auth_initiate, generate_authorization_header, recieve_handshake}; use db::{DatabaseInterface, DATA_ROOT_DIR}; @@ -101,6 +103,8 @@ pub fn run() { // Library fetch_library, fetch_game, + // Downloads + download_game ]) .plugin(tauri_plugin_shell::init()) .setup(|app| { diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs new file mode 100644 index 0000000..aeb4f37 --- /dev/null +++ b/src-tauri/src/utils.rs @@ -0,0 +1,44 @@ +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::atomic::Ordering::Relaxed; +use crate::utils::ProgressChecker::Complete; + +#[derive(Eq, PartialEq)] +pub enum ProgressChecker { + Complete, + Incomplete +} + +// This function is designed to take in any function which does not regularly return a value, +// and instead loops over it until it returns "Complete". The current number of iterations +// is counted by "progress" +pub async fn progress_updater(function: Box ProgressChecker>, progress: AtomicUsize) { + loop { + if function() == ProgressChecker::Complete { break } + progress.fetch_add(1, Relaxed); + } +} + +pub async fn threaded_progress_updater(f: F, progress: AtomicUsize, max_threads: usize, instances: usize) -> ProgressChecker +where F: Fn() -> ProgressChecker + Send + Clone + Copy + 'static +{ + let mut threads = Vec::new(); + for instance in 0..instances { + let func = tokio::spawn(async move { + let res = f(); + return res + }); + threads.push(func); + } + let mut completed = ProgressChecker::Incomplete; + for thread in threads { + if thread.await.unwrap() == Complete { + completed = Complete + } + progress.fetch_add(1, Ordering::Relaxed); + } + completed +} + +fn test() -> ProgressChecker { + ProgressChecker::Incomplete +} \ No newline at end of file