check for launcher update

This commit is contained in:
mxve 2023-08-03 02:49:55 +02:00
parent f5216d7e29
commit bb1ab05588
4 changed files with 49 additions and 3 deletions

7
Cargo.lock generated
View File

@ -8,6 +8,7 @@ version = "0.2.3"
dependencies = [ dependencies = [
"http_req", "http_req",
"rand", "rand",
"semver",
"serde", "serde",
"serde_json", "serde_json",
"sha1_smol", "sha1_smol",
@ -192,6 +193,12 @@ dependencies = [
"untrusted", "untrusted",
] ]
[[package]]
name = "semver"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.179" version = "1.0.179"

View File

@ -18,6 +18,7 @@ sha1_smol = "1.0.0"
serde = { version = "1.0.179", features = ["derive"] } serde = { version = "1.0.179", features = ["derive"] }
serde_json = "1.0.104" serde_json = "1.0.104"
rand = "0.8.5" rand = "0.8.5"
semver = "1.0.18"
[build-dependencies] [build-dependencies]
winres = "0.1.12" winres = "0.1.12"

View File

@ -2,9 +2,20 @@ use std::{fs, io::Write, path::Path, str};
pub fn get_body(url: &str) -> Vec<u8> { pub fn get_body(url: &str) -> Vec<u8> {
let mut res: Vec<u8> = Vec::new(); let mut res: Vec<u8> = Vec::new();
http_req::request::get(url, &mut res).unwrap_or_else(|error| { let req = http_req::request::Request::new(&url.try_into().unwrap())
panic!("\n\n{}:\n{:?}", "Error", error); .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 res
} }

View File

@ -1,6 +1,8 @@
mod http; mod http;
use semver::Version;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
use std::{thread, time};
#[derive(serde::Deserialize, serde::Serialize)] #[derive(serde::Deserialize, serde::Serialize)]
struct CdnFile { struct CdnFile {
@ -17,6 +19,7 @@ struct Game<'a> {
} }
const MASTER: &str = "https://master.alterware.dev"; const MASTER: &str = "https://master.alterware.dev";
const REPO: &str = "mxve/alterware-launcher";
fn get_cache_buster() -> u64 { fn get_cache_buster() -> u64 {
match SystemTime::now().duration_since(UNIX_EPOCH) { match SystemTime::now().duration_since(UNIX_EPOCH) {
@ -31,6 +34,28 @@ fn get_file_sha1(path: &PathBuf) -> String {
sha1.digest().to_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) { 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!("{}/files.json?{}", MASTER, get_cache_buster()).as_str(), format!("{}/files.json?{}", MASTER, get_cache_buster()).as_str(),
@ -83,6 +108,8 @@ fn launch(file_path: &PathBuf) {
} }
fn main() { fn main() {
check_for_launcher_update();
let mut args: Vec<String> = std::env::args().collect(); let mut args: Vec<String> = std::env::args().collect();
let games_json = http::get_body_string(format!("{}/games.json", MASTER).as_str()); let games_json = http::get_body_string(format!("{}/games.json", MASTER).as_str());