@@ -24,6 +24,15 @@ pub enum Index {
24
24
output : PathBuf
25
25
} ,
26
26
27
+ /// Install a mod from the index to the current profile
28
+ Install {
29
+ /// Mod ID to install
30
+ id : String ,
31
+
32
+ /// Mod version to install, defaults to latest
33
+ version : Option < VersionReq > ,
34
+ } ,
35
+
27
36
/// Updates the index cache
28
37
Update ,
29
38
}
@@ -173,18 +182,29 @@ pub fn install_mod(config: &Config, id: &String, version: &VersionReq) -> PathBu
173
182
174
183
info ! ( "Installing mod '{}' version '{}'" , id, version) ;
175
184
176
- let mut pkg_data = io:: Cursor :: new ( Vec :: new ( ) ) ;
177
-
178
- reqwest:: blocking:: get ( entry. r#mod . download )
185
+ let bytes = reqwest:: blocking:: get ( entry. r#mod . download )
179
186
. expect ( "Unable to download mod" )
180
- . copy_to ( & mut pkg_data )
187
+ . bytes ( )
181
188
. expect ( "Unable to download mod" ) ;
182
189
183
190
let dest = config. get_current_profile ( ) . mods_dir ( ) . join ( format ! ( "{id}.geode" ) ) ;
184
- let mut file = std:: fs:: File :: create ( & dest)
185
- . expect ( "Unable to create destination file for mod" ) ;
186
-
187
- std:: io:: copy ( & mut pkg_data, & mut file) . expect ( "Unable to install mod" ) ;
191
+
192
+ let mut hasher = Sha3_256 :: new ( ) ;
193
+ hasher. update ( & bytes) ;
194
+ let hash = hex:: encode ( hasher. finalize ( ) . to_vec ( ) ) ;
195
+
196
+ if hash != entry. r#mod . hash {
197
+ fatal ! (
198
+ "Downloaded file doesn't match expected hash\n \
199
+ {hash}\n \
200
+ vs {}\n \
201
+ Try again, and if the issue persists, report this on GitHub: \
202
+ https://github.com/geode-sdk/cli/issues/new",
203
+ entry. r#mod. hash
204
+ ) ;
205
+ }
206
+
207
+ fs:: write ( & dest, bytes) . expect ( "Unable to install .geode file" ) ;
188
208
189
209
dest
190
210
}
@@ -279,5 +299,10 @@ pub fn subcommand(config: &mut Config, cmd: Index) {
279
299
match cmd {
280
300
Index :: New { output } => create_entry ( & output) ,
281
301
Index :: Update => update_index ( config) ,
302
+ Index :: Install { id, version } => {
303
+ update_index ( config) ;
304
+ install_mod ( config, & id, & version. unwrap_or ( VersionReq :: STAR ) ) ;
305
+ done ! ( "Mod installed" ) ;
306
+ } ,
282
307
}
283
308
}
0 commit comments