Update on GameDownload

This commit is contained in:
quexeky
2024-10-18 22:35:03 +11:00
parent 7fec00ded0
commit e71e4cf0fa
6 changed files with 80 additions and 7 deletions

View 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!()
}

View File

@ -1,3 +1,4 @@
mod downloads;
mod manifest;
pub mod progress;
pub mod progress;
mod game_download;

View File

@ -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>) {

View File

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