Skip to content

Commit 5f652cd

Browse files
quexekyDecDuck
authored andcommitted
More fleshing out on how specifically game downloads will work (#1)
1 parent a139865 commit 5f652cd

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed
+57-5
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,88 @@
1-
use std::sync::Arc;
1+
use std::sync::{Arc, Mutex};
22
use std::sync::atomic::AtomicUsize;
33
use serde::{Deserialize, Serialize};
44
use versions::Version;
5+
use crate::AppState;
56
use crate::downloads::progress::ProgressChecker;
67

78
#[derive(Serialize, Deserialize, Clone)]
89
#[serde(rename_all="camelCase")]
910
pub struct GameDownload {
1011
id: String,
1112
version: Version,
12-
progress: Arc<AtomicUsize>
13+
progress: GameDownloadState
14+
}
15+
#[derive(Serialize, Deserialize, Clone)]
16+
pub enum GameDownloadState {
17+
Uninitialised,
18+
Manifest,
19+
Downloading(Arc<AtomicUsize>),
20+
Finished,
21+
Stalled,
22+
Failed,
23+
Cancelled
24+
}
25+
26+
#[derive(Serialize, Deserialize, Clone)]
27+
pub enum GameDownloadError {
28+
1329
}
1430
#[derive(Serialize, Deserialize)]
1531
#[serde(rename_all="camelCase")]
1632
pub struct GameChunkCtx {
1733
chunk_id: usize,
1834
}
1935

36+
#[derive(Serialize, Deserialize, Clone)]
37+
pub struct GameDownloadManifest {
38+
// TODO: Implement game manifest
39+
}
40+
2041
impl GameDownload {
2142
pub fn new(id: String, version: Version) -> Self {
2243
Self {
2344
id,
2445
version,
25-
progress: Arc::new(AtomicUsize::new(0))
46+
progress: GameDownloadState::Uninitialised
2647
}
2748
}
28-
pub async fn download(&self, max_threads: usize, contexts: Vec<GameChunkCtx>) {
29-
let progress = ProgressChecker::new(Box::new(download_game_chunk), self.progress.clone());
49+
pub async fn download(&mut self, max_threads: usize, contexts: Vec<GameChunkCtx>) -> Result<(), GameDownloadError> {
50+
let progress = Arc::new(AtomicUsize::new(0));
51+
self.progress = GameDownloadState::Downloading(progress.clone());
52+
let progress = ProgressChecker::new(Box::new(download_game_chunk), progress);
3053
progress.run_contexts_parallel_async(contexts, max_threads).await;
54+
Ok(())
55+
}
56+
pub async fn download_manifest(&mut self) -> Result<GameDownloadManifest, GameDownloadError> {
57+
todo!()
3158
}
3259
}
3360
fn download_game_chunk(ctx: GameChunkCtx) {
3461
todo!();
3562
// Need to implement actual download logic
63+
}
64+
65+
#[tauri::command]
66+
pub async fn start_game_download(
67+
game_id: String,
68+
game_version: Version,
69+
max_threads: usize,
70+
state: tauri::State<'_, Mutex<AppState>>,
71+
) -> Result<(), GameDownloadError> {
72+
let mut download = Arc::new(GameDownload::new(game_id, game_version));
73+
let mut app_state = state.lock().unwrap();
74+
app_state.game_downloads.push(download.clone());
75+
76+
let manifest = match download.download_manifest().await {
77+
Ok(manifest) => { manifest }
78+
Err(e) => { return Err(e) }
79+
};
80+
81+
download.download(max_threads, manifest.parse_to_chunks()).await
82+
}
83+
84+
impl GameDownloadManifest {
85+
fn parse_to_chunks(self) -> Vec<GameChunkCtx> {
86+
todo!()
87+
}
3688
}

src-tauri/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use serde::{Deserialize, Serialize};
1919
use std::{
2020
collections::HashMap, sync::{LazyLock, Mutex}
2121
};
22+
use std::sync::Arc;
2223
use tauri_plugin_deep_link::DeepLinkExt;
2324
use crate::db::DatabaseImpls;
2425
use crate::downloads::game_download::GameDownload;
@@ -46,7 +47,7 @@ pub struct AppState {
4647
status: AppStatus,
4748
user: Option<User>,
4849
games: HashMap<String, Game>,
49-
game_downloads: Vec<GameDownload>
50+
game_downloads: Vec<Arc<GameDownload>>
5051
}
5152

5253
#[tauri::command]
@@ -109,7 +110,6 @@ pub fn run() {
109110
// Library
110111
fetch_library,
111112
fetch_game,
112-
// Downloads
113113
])
114114
.plugin(tauri_plugin_shell::init())
115115
.setup(|app| {

0 commit comments

Comments
 (0)