Skip to content

Commit f5a7660

Browse files
quexekyDecDuck
authored andcommitted
Theoretically adding queue support and optimistic manifest downloading (#1). Needs tests when actual functions are implemented
1 parent 5f652cd commit f5a7660

File tree

1 file changed

+57
-22
lines changed

1 file changed

+57
-22
lines changed

src-tauri/src/downloads/game_download.rs

+57-22
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
1+
use std::future::Future;
12
use std::sync::{Arc, Mutex};
23
use std::sync::atomic::AtomicUsize;
34
use serde::{Deserialize, Serialize};
45
use versions::Version;
56
use crate::AppState;
67
use crate::downloads::progress::ProgressChecker;
78

8-
#[derive(Serialize, Deserialize, Clone)]
9+
#[derive(Serialize, Deserialize)]
910
#[serde(rename_all="camelCase")]
1011
pub struct GameDownload {
1112
id: String,
1213
version: Version,
13-
progress: GameDownloadState
14+
progress: Arc<AtomicUsize>,
15+
state: Mutex<GameDownloadState>,
16+
manifest: Option<Mutex<GameDownloadManifest>>
1417
}
15-
#[derive(Serialize, Deserialize, Clone)]
18+
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
1619
pub enum GameDownloadState {
1720
Uninitialised,
21+
Queued,
1822
Manifest,
19-
Downloading(Arc<AtomicUsize>),
23+
Downloading,
2024
Finished,
2125
Stalled,
2226
Failed,
2327
Cancelled
2428
}
2529

26-
#[derive(Serialize, Deserialize, Clone)]
30+
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
2731
pub enum GameDownloadError {
28-
32+
ManifestAlreadyExists,
33+
ManifestDoesNotExist,
34+
ManifestDownloadError,
2935
}
30-
#[derive(Serialize, Deserialize)]
36+
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Ord, PartialOrd)]
3137
#[serde(rename_all="camelCase")]
3238
pub struct GameChunkCtx {
3339
chunk_id: usize,
3440
}
3541

36-
#[derive(Serialize, Deserialize, Clone)]
42+
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
3743
pub struct GameDownloadManifest {
3844
// TODO: Implement game manifest
3945
}
@@ -43,17 +49,38 @@ impl GameDownload {
4349
Self {
4450
id,
4551
version,
46-
progress: GameDownloadState::Uninitialised
52+
progress: Arc::new(AtomicUsize::new(0)),
53+
state: Mutex::from(GameDownloadState::Uninitialised),
54+
manifest: None
55+
}
56+
}
57+
pub async fn queue(&self) -> Result<(), GameDownloadError> {
58+
self.change_state(GameDownloadState::Queued);
59+
if self.manifest.is_none() {
60+
return Ok(())
4761
}
62+
self.download_manifest().await
4863
}
49-
pub async fn download(&mut self, max_threads: usize, contexts: Vec<GameChunkCtx>) -> Result<(), GameDownloadError> {
64+
pub async fn download(&self, max_threads: usize, contexts: Vec<GameChunkCtx>) -> Result<(), GameDownloadError> {
5065
let progress = Arc::new(AtomicUsize::new(0));
51-
self.progress = GameDownloadState::Downloading(progress.clone());
66+
self.change_state(GameDownloadState::Downloading);
5267
let progress = ProgressChecker::new(Box::new(download_game_chunk), progress);
5368
progress.run_contexts_parallel_async(contexts, max_threads).await;
5469
Ok(())
5570
}
56-
pub async fn download_manifest(&mut self) -> Result<GameDownloadManifest, GameDownloadError> {
71+
pub async fn download_manifest(&self) -> Result<(), GameDownloadError> {
72+
if self.manifest.is_some() {
73+
return Err(GameDownloadError::ManifestAlreadyExists);
74+
}
75+
todo!() // Need to actually download the manifest
76+
}
77+
pub fn change_state(&self, state: GameDownloadState) {
78+
let mut lock = self.state.lock().unwrap();
79+
*lock = state;
80+
}
81+
}
82+
impl GameDownloadManifest {
83+
fn parse_to_chunks(&self) -> Vec<GameChunkCtx> {
5784
todo!()
5885
}
5986
}
@@ -71,18 +98,26 @@ pub async fn start_game_download(
7198
) -> Result<(), GameDownloadError> {
7299
let mut download = Arc::new(GameDownload::new(game_id, game_version));
73100
let mut app_state = state.lock().unwrap();
74-
app_state.game_downloads.push(download.clone());
75101

76-
let manifest = match download.download_manifest().await {
77-
Ok(manifest) => { manifest }
78-
Err(e) => { return Err(e) }
102+
let tmp = download.clone();
103+
let manifest = &tmp.manifest;
104+
105+
let Some(unlocked) = manifest else { return Err(GameDownloadError::ManifestDoesNotExist) };
106+
let lock = unlocked.lock().unwrap();
107+
108+
let chunks = lock.parse_to_chunks();
109+
110+
/*
111+
let manifest = match d.manifest {
112+
Some(lock) => {
113+
let lock = lock.lock().unwrap();
114+
lock.parse_to_chunks()
115+
},
116+
None => { return Err(GameDownloadError::ManifestDoesNotExist) }
79117
};
118+
*/
80119

81-
download.download(max_threads, manifest.parse_to_chunks()).await
120+
app_state.game_downloads.push(download.clone());
121+
download.download(max_threads, chunks).await
82122
}
83123

84-
impl GameDownloadManifest {
85-
fn parse_to_chunks(self) -> Vec<GameChunkCtx> {
86-
todo!()
87-
}
88-
}

0 commit comments

Comments
 (0)