@@ -4,32 +4,108 @@ mod game;
44mod global;
55mod hash;
66mod http;
7+ mod utils;
78
8- use std:: path:: Path ;
9+ use std:: path:: PathBuf ;
10+ use strum:: IntoEnumIterator ;
11+
12+ fn arg_value ( args : & [ String ] , possible_args : & [ & str ] ) -> Option < String > {
13+ for arg in possible_args {
14+ if let Some ( e) = args. iter ( ) . position ( |r| r == * arg) {
15+ if e + 1 < args. len ( ) {
16+ let value = args[ e + 1 ] . clone ( ) ;
17+ if value. starts_with ( '-' ) {
18+ continue ;
19+ }
20+
21+ arg_value_remove ( & mut args. to_vec ( ) , arg) ;
22+ return Some ( value) ;
23+ }
24+ }
25+ }
26+ None
27+ }
28+
29+ fn arg_value_remove ( args : & mut Vec < String > , arg : & str ) {
30+ if let Some ( e) = args. iter ( ) . position ( |r| r == arg) {
31+ args. remove ( e) ;
32+ args. remove ( e) ;
33+ } ;
34+ }
35+
36+ fn arg_bool ( args : & [ String ] , possible_args : & [ & str ] ) -> bool {
37+ possible_args
38+ . iter ( )
39+ . any ( |arg| args. iter ( ) . any ( |r| r == * arg) )
40+ }
41+
42+ fn arg_path ( args : & [ String ] , possible_args : & [ & str ] ) -> Option < PathBuf > {
43+ arg_value ( args, possible_args) . map ( PathBuf :: from)
44+ }
945
1046#[ tokio:: main]
1147async fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
12- let path = Path :: new ( "I:\\ SteamLibrary\\ steamapps\\ common\\ Call of Duty Modern Warfare 2" ) ;
13- let game = game:: detect_game ( path) ;
14-
15- if let Some ( game) = game {
16- println ! ( "Game: {:?}" , game) ;
17- let clients = game. clients ( ) ;
18- println ! ( "Clients: {:?}" , clients) ;
19-
20- let info = cdn:: get_info ( ) . await ?;
21- let files = cdn:: filter_files ( info. files . clone ( ) , game) ;
22- println ! ( "Files: {:?}" , files) ;
23- for file in files {
24- println ! ( "File: {:?}" , file) ;
25- println ! ( "Size: {:?}" , file. size_human( ) ) ;
26- println ! ( "URL: {:?}" , file. url( ) ) ;
27- println ! ( "Cache name: {:?}" , file. cache_name( ) ) ;
28- println ! ( "Cache path: {:?}" , file. cache_path( ) ) ;
29- }
48+ let args = std:: env:: args ( ) . collect :: < Vec < String > > ( ) ;
49+
50+ utils:: set_mutex (
51+ & global:: CDN_PROTOCOL ,
52+ arg_value ( & args, & [ "--protocol" ] ) . unwrap_or ( "https" . to_owned ( ) ) ,
53+ ) ;
54+ utils:: set_mutex (
55+ & global:: CDN_BRANCH ,
56+ arg_value ( & args, & [ "--branch" ] ) . unwrap_or ( "stable" . to_owned ( ) ) ,
57+ ) ;
58+ utils:: set_mutex (
59+ & global:: GAME_DIR ,
60+ arg_path ( & args, & [ "-p" , "--path" , "--game-path" ] )
61+ . unwrap_or ( std:: env:: current_dir ( ) . unwrap ( ) ) ,
62+ ) ;
63+
64+ let client = args
65+ . iter ( )
66+ . find_map ( |arg| game:: Client :: iter ( ) . find ( |& client| client. internal_name ( ) == arg) ) ;
67+
68+ if let Some ( client) = client {
69+ utils:: set_mutex ( & global:: GAME_CLIENT , Some ( client) ) ;
70+ utils:: set_mutex ( & global:: GAME , client. game ( ) ) ;
3071 } else {
31- println ! ( "No game detected" ) ;
72+ if let Some ( game) = game:: detect_game ( & utils:: get_mutex ( & global:: GAME_DIR ) ) {
73+ utils:: set_mutex ( & global:: GAME , game) ;
74+ println ! ( "Game: {:?}" , game) ;
75+
76+ match game. clients ( ) . len ( ) {
77+ 0 => {
78+ println ! ( "No clients found for game" ) ;
79+ return Ok ( ( ) ) ;
80+ }
81+ 1 => {
82+ utils:: set_mutex ( & global:: GAME_CLIENT , Some ( game. clients ( ) [ 0 ] ) ) ;
83+ }
84+ _ => {
85+ println ! ( "Multiple clients found for game, please specify one" ) ;
86+ return Ok ( ( ) ) ;
87+ }
88+ }
89+ } else {
90+ println ! ( "No game detected" ) ;
91+ return Ok ( ( ) ) ;
92+ }
3293 }
3394
95+ let game = utils:: get_mutex ( & global:: GAME ) ;
96+ let clients = game. clients ( ) ;
97+ println ! ( "Clients: {:?}" , clients) ;
98+
99+ let info = cdn:: get_info ( ) . await ?;
100+ let files = cdn:: filter_files ( info. files . clone ( ) , game) ;
101+ //println!("Files: {:?}", files);
102+ //for file in files {
103+ //println!("File: {:?}", file);
104+ // println!("Size: {:?}", file.size_human());
105+ // println!("URL: {:?}", file.url());
106+ // println!("Cache name: {:?}", file.cache_name());
107+ // println!("Cache path: {:?}", file.cache_path());
108+ //}
109+
34110 Ok ( ( ) )
35111}
0 commit comments