1
- use std:: sync:: Arc ;
1
+ use std:: sync:: { Arc , Mutex } ;
2
2
use std:: sync:: atomic:: AtomicUsize ;
3
3
use serde:: { Deserialize , Serialize } ;
4
4
use versions:: Version ;
5
+ use crate :: AppState ;
5
6
use crate :: downloads:: progress:: ProgressChecker ;
6
7
7
8
#[ derive( Serialize , Deserialize , Clone ) ]
8
9
#[ serde( rename_all="camelCase" ) ]
9
10
pub struct GameDownload {
10
11
id : String ,
11
12
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
+
13
29
}
14
30
#[ derive( Serialize , Deserialize ) ]
15
31
#[ serde( rename_all="camelCase" ) ]
16
32
pub struct GameChunkCtx {
17
33
chunk_id : usize ,
18
34
}
19
35
36
+ #[ derive( Serialize , Deserialize , Clone ) ]
37
+ pub struct GameDownloadManifest {
38
+ // TODO: Implement game manifest
39
+ }
40
+
20
41
impl GameDownload {
21
42
pub fn new ( id : String , version : Version ) -> Self {
22
43
Self {
23
44
id,
24
45
version,
25
- progress : Arc :: new ( AtomicUsize :: new ( 0 ) )
46
+ progress : GameDownloadState :: Uninitialised
26
47
}
27
48
}
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) ;
30
53
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 ! ( )
31
58
}
32
59
}
33
60
fn download_game_chunk ( ctx : GameChunkCtx ) {
34
61
todo ! ( ) ;
35
62
// 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
+ }
36
88
}
0 commit comments