diff --git a/Cargo.lock b/Cargo.lock index 5d5d9b9..a391a32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,7 @@ version = "0.2.3" dependencies = [ "http_req", "rand", + "semver", "serde", "serde_json", "sha1_smol", @@ -192,6 +193,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "semver" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" + [[package]] name = "serde" version = "1.0.179" diff --git a/Cargo.toml b/Cargo.toml index 1c8795f..31b804b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ sha1_smol = "1.0.0" serde = { version = "1.0.179", features = ["derive"] } serde_json = "1.0.104" rand = "0.8.5" +semver = "1.0.18" [build-dependencies] winres = "0.1.12" diff --git a/src/http.rs b/src/http.rs index 46e16e5..bec9408 100644 --- a/src/http.rs +++ b/src/http.rs @@ -2,9 +2,20 @@ use std::{fs, io::Write, path::Path, str}; pub fn get_body(url: &str) -> Vec { let mut res: Vec = Vec::new(); - http_req::request::get(url, &mut res).unwrap_or_else(|error| { - panic!("\n\n{}:\n{:?}", "Error", error); - }); + let req = http_req::request::Request::new(&url.try_into().unwrap()) + .header( + "User-Agent", + "AlterWare Launcher | github.com/mxve/alterware-launcher", + ) + .send(&mut res) + .unwrap_or_else(|error| { + panic!("\n\n{}:\n{:?}", "Error", error); + }); + + if req.status_code() == http_req::response::StatusCode::new(302) { + let location = req.headers().get("Location").unwrap().as_str(); + return get_body(location); + } res } diff --git a/src/main.rs b/src/main.rs index d3d7f58..f42fbc5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ mod http; +use semver::Version; use std::time::{SystemTime, UNIX_EPOCH}; use std::{fs, path::PathBuf}; +use std::{thread, time}; #[derive(serde::Deserialize, serde::Serialize)] struct CdnFile { @@ -17,6 +19,7 @@ struct Game<'a> { } const MASTER: &str = "https://master.alterware.dev"; +const REPO: &str = "mxve/alterware-launcher"; fn get_cache_buster() -> u64 { match SystemTime::now().duration_since(UNIX_EPOCH) { @@ -31,6 +34,28 @@ fn get_file_sha1(path: &PathBuf) -> String { sha1.digest().to_string() } +fn check_for_launcher_update() { + let current_version: Version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); + let github_body = http::get_body_string( + format!("https://api.github.com/repos/{}/releases/latest", REPO).as_str(), + ); + let github_json: serde_json::Value = serde_json::from_str(&github_body).unwrap(); + let latest_version = github_json["tag_name"] + .to_string() + .replace(['v', '"'].as_ref(), ""); + let latest_version = Version::parse(&latest_version).unwrap(); + + if current_version < latest_version { + println!( + "A new version of the AlterWare launcher is available: {}", + latest_version + ); + println!("Download it at https://github.com/{}/releases/latest", REPO); + println!("Launching in 10 seconds.."); + thread::sleep(time::Duration::from_secs(10)); + } +} + fn update(game: &Game) { let cdn_info: Vec = serde_json::from_str(&http::get_body_string( format!("{}/files.json?{}", MASTER, get_cache_buster()).as_str(), @@ -83,6 +108,8 @@ fn launch(file_path: &PathBuf) { } fn main() { + check_for_launcher_update(); + let mut args: Vec = std::env::args().collect(); let games_json = http::get_body_string(format!("{}/games.json", MASTER).as_str());