mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 08:41:21 +10:00
Update on GameDownload
This commit is contained in:
30
src-tauri/src/downloads/game_download.rs
Normal file
30
src-tauri/src/downloads/game_download.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use versions::Version;
|
||||
use crate::downloads::progress::ProgressChecker;
|
||||
|
||||
pub struct GameDownload {
|
||||
id: String,
|
||||
version: Version,
|
||||
progress: Arc<AtomicUsize>
|
||||
}
|
||||
pub struct GameChunkCtx {
|
||||
|
||||
}
|
||||
|
||||
impl GameDownload {
|
||||
pub fn new(id: String, version: Version) -> Self {
|
||||
Self {
|
||||
id,
|
||||
version,
|
||||
progress: Arc::new(AtomicUsize::new(0))
|
||||
}
|
||||
}
|
||||
pub async fn download(&self, max_threads: usize, contexts: Vec<GameChunkCtx>) {
|
||||
let progress = ProgressChecker::new(Box::new(download_game_chunk), self.progress.clone());
|
||||
progress.run_contexts_sequentially_async(contexts).await;
|
||||
}
|
||||
}
|
||||
fn download_game_chunk(ctx: GameChunkCtx) {
|
||||
todo!()
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
mod downloads;
|
||||
mod manifest;
|
||||
pub mod progress;
|
||||
pub mod progress;
|
||||
mod game_download;
|
||||
@ -5,17 +5,17 @@ use rayon::ThreadPoolBuilder;
|
||||
pub struct ProgressChecker<T>
|
||||
where T: 'static + Send + Sync
|
||||
{
|
||||
counter: AtomicUsize,
|
||||
counter: Arc<AtomicUsize>,
|
||||
f: Arc<Box<dyn Fn(T) + Send + Sync + 'static>>,
|
||||
}
|
||||
|
||||
impl<T> ProgressChecker<T>
|
||||
where T: Send + Sync
|
||||
{
|
||||
pub fn new(f: Box<dyn Fn(T) + Send + Sync + 'static>) -> Self {
|
||||
pub fn new(f: Box<dyn Fn(T) + Send + Sync + 'static>, counter_reference: Arc<AtomicUsize>) -> Self {
|
||||
Self {
|
||||
f: f.into(),
|
||||
counter: AtomicUsize::new(0)
|
||||
counter: counter_reference
|
||||
}
|
||||
}
|
||||
pub async fn run_contexts_sequentially_async(&self, contexts: Vec<T>) {
|
||||
|
||||
@ -1,14 +1,18 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use crate::downloads::progress::ProgressChecker;
|
||||
|
||||
#[test]
|
||||
fn test_progress_sequentially() {
|
||||
let p = ProgressChecker::new(Box::new(test_fn));
|
||||
let counter = Arc::new(AtomicUsize::new(0));
|
||||
let p = ProgressChecker::new(Box::new(test_fn), counter.clone());
|
||||
p.run_contexts_sequentially((1..100).collect());
|
||||
println!("Progress: {}", p.get_progress_percentage(100));
|
||||
}
|
||||
#[test]
|
||||
fn test_progress_parallel() {
|
||||
let p = ProgressChecker::new(Box::new(test_fn));
|
||||
let counter = Arc::new(AtomicUsize::new(0));
|
||||
let p = ProgressChecker::new(Box::new(test_fn), counter.clone());
|
||||
p.run_contexts_parallel((1..100).collect(), 10);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user