minor cleanup

This commit is contained in:
mxve 2023-07-25 03:24:52 +02:00
parent a5d3d9162e
commit b46065a800

View File

@ -1,6 +1,6 @@
mod http; mod http;
use std::{fs, path::PathBuf};
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use std::{fs, path::PathBuf};
#[derive(serde::Deserialize, serde::Serialize)] #[derive(serde::Deserialize, serde::Serialize)]
struct CdnFile { struct CdnFile {
@ -25,7 +25,7 @@ fn get_cache_buster() -> u64 {
} }
} }
fn file_get_sha1(path: &PathBuf) -> String { fn get_file_sha1(path: &PathBuf) -> String {
let mut sha1 = sha1_smol::Sha1::new(); let mut sha1 = sha1_smol::Sha1::new();
sha1.update(&fs::read(path).unwrap()); sha1.update(&fs::read(path).unwrap());
sha1.digest().to_string() sha1.digest().to_string()
@ -33,26 +33,18 @@ fn file_get_sha1(path: &PathBuf) -> String {
fn update(game: &Game) { fn update(game: &Game) {
let cdn_info: Vec<CdnFile> = serde_json::from_str(&http::get_body_string( let cdn_info: Vec<CdnFile> = serde_json::from_str(&http::get_body_string(
format!( format!("{}/files.json?{}", MASTER, get_cache_buster()).as_str(),
"{}/files.json?{}",
MASTER,
get_cache_buster()
)
.as_str(),
)) ))
.unwrap(); .unwrap();
let mut files_to_update: Vec<CdnFile> = Vec::new();
for file in cdn_info { for file in cdn_info {
if file.name.starts_with(game.engine) { if !file.name.starts_with(game.engine) {
files_to_update.push(file); continue;
}
} }
for file in files_to_update {
let file_path = PathBuf::from(&file.name.replace(&format!("{}/", game.engine), "")); let file_path = PathBuf::from(&file.name.replace(&format!("{}/", game.engine), ""));
if file_path.exists() { if file_path.exists() {
let sha1_local = file_get_sha1(&file_path).to_lowercase(); let sha1_local = get_file_sha1(&file_path).to_lowercase();
let sha1_remote = file.hash.to_lowercase(); let sha1_remote = file.hash.to_lowercase();
if sha1_local != sha1_remote { if sha1_local != sha1_remote {
println!( println!(
@ -62,12 +54,7 @@ fn update(game: &Game) {
sha1_remote sha1_remote
); );
http::download_file( http::download_file(
&format!( &format!("{}/{}?{}", MASTER, file.name, get_cache_buster()),
"{}/{}?{}",
MASTER,
file.name,
get_cache_buster()
),
&file_path, &file_path,
); );
} }
@ -79,12 +66,7 @@ fn update(game: &Game) {
} }
} }
http::download_file( http::download_file(
&format!( &format!("{}/{}?{}", MASTER, file.name, get_cache_buster()),
"{}/{}?{}",
MASTER,
file.name,
get_cache_buster()
),
&file_path, &file_path,
); );
} }
@ -95,9 +77,9 @@ fn launch(file_path: &PathBuf) {
println!("Launching {}...", file_path.display()); println!("Launching {}...", file_path.display());
std::process::Command::new(file_path) std::process::Command::new(file_path)
.spawn() .spawn()
.unwrap() .expect("Failed to launch the game")
.wait() .wait()
.unwrap(); .expect("Failed to wait for the game process to finish");
} }
fn main() { fn main() {