From 64c63bf24f5f03dc8f3039d021b1cd8b636f54c3 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Tue, 29 Aug 2023 21:53:47 +0200 Subject: [PATCH] obtain iw4x.dll from iw4x/iw4x-client --- src/github.rs | 22 ++++++++++------------ src/global.rs | 2 ++ src/iw4x.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 7 ++++++- src/misc.rs | 4 ++++ src/self_update.rs | 7 ++++--- 6 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 src/iw4x.rs diff --git a/src/github.rs b/src/github.rs index 5bae962..b26b23f 100644 --- a/src/github.rs +++ b/src/github.rs @@ -1,25 +1,23 @@ -use crate::global::*; - use semver::Version; -pub fn latest_version() -> Version { +pub fn latest(owner: &str, repo: &str) -> String { let github_body = crate::http::get_body_string( format!( "https://api.github.com/repos/{}/{}/releases/latest", - GH_OWNER, GH_REPO + owner, repo ) .as_str(), ); let github_json: serde_json::Value = serde_json::from_str(&github_body).unwrap(); - let latest_version = github_json["tag_name"] + github_json["tag_name"] .to_string() - .replace(['v', '"'].as_ref(), ""); - Version::parse(&latest_version).unwrap() + .replace(['v', '"'].as_ref(), "") } -pub fn latest_release_url() -> String { - format!( - "https://github.com/{}/{}/releases/latest", - GH_OWNER, GH_REPO - ) +pub fn latest_version(owner: &str, repo: &str) -> Version { + Version::parse(&latest(owner, repo)).unwrap() +} + +pub fn latest_release_url(owner: &str, repo: &str) -> String { + format!("https://github.com/{}/{}/releases/latest", owner, repo) } diff --git a/src/global.rs b/src/global.rs index 40485bd..d75f49a 100644 --- a/src/global.rs +++ b/src/global.rs @@ -1,3 +1,5 @@ pub const MASTER: &str = "https://master.alterware.dev"; pub const GH_OWNER: &str = "mxve"; pub const GH_REPO: &str = "alterware-launcher"; +pub const GH_IW4X_OWNER: &str = "iw4x"; +pub const GH_IW4X_REPO: &str = "iw4x-client"; \ No newline at end of file diff --git a/src/iw4x.rs b/src/iw4x.rs new file mode 100644 index 0000000..ac14d2a --- /dev/null +++ b/src/iw4x.rs @@ -0,0 +1,40 @@ +use crate::github; +use crate::http; +use crate::misc; +use crate::global::*; + +use std::{fs, path::Path}; + +pub fn local_revision(dir: &Path) -> u16 { + if let Ok(revision) = fs::read_to_string(dir.join(".iw4xrevision")) { + misc::rev_to_int(&revision) + } else { + 0 + } +} + +pub fn remote_revision() -> u16 { + misc::rev_to_int(&github::latest(GH_IW4X_OWNER, GH_IW4X_REPO)) +} + +pub fn update_available(dir: &Path) -> bool { + local_revision(dir) < remote_revision() +} + +pub fn update(dir: &Path) { + if update_available(dir) { + println!("Updating IW4x..."); + http::download_file( + &format!( + "{}/download/iw4x.dll", + github::latest_release_url(GH_IW4X_OWNER, GH_IW4X_REPO) + ), + &dir.join("iw4x.dll"), + ); + fs::write( + dir.join(".iw4xrevision"), + github::latest(GH_IW4X_OWNER, GH_IW4X_REPO), + ) + .unwrap(); + } +} diff --git a/src/main.rs b/src/main.rs index 1c3b240..4d999a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod github; mod global; mod http; +mod iw4x; mod misc; mod self_update; mod structs; @@ -177,7 +178,7 @@ fn update(game: &Game, dir: &Path) { .unwrap(); for file in cdn_info { - if !file.name.starts_with(game.engine) { + if !file.name.starts_with(game.engine) || file.name == "iw4/iw4x.dll" { continue; } @@ -204,6 +205,10 @@ fn update(game: &Game, dir: &Path) { http::download_file(&format!("{}/{}", MASTER, file.name), &file_path); } } + + if game.engine == "iw4" { + iw4x::update(dir); + } } fn launch(file_path: &PathBuf) { diff --git a/src/misc.rs b/src/misc.rs index d2de582..e840b99 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -11,3 +11,7 @@ pub fn stdin() -> String { std::io::stdin().read_line(&mut input).unwrap(); input.trim().to_string() } + +pub fn rev_to_int(rev: &str) -> u16 { + rev.strip_prefix('r').unwrap().parse::().unwrap_or(0) +} diff --git a/src/self_update.rs b/src/self_update.rs index 87eeae4..8491ac4 100644 --- a/src/self_update.rs +++ b/src/self_update.rs @@ -1,4 +1,5 @@ use crate::github; +use crate::global::*; use semver::Version; #[cfg(not(windows))] @@ -6,7 +7,7 @@ use std::{thread, time}; pub fn self_update_available() -> bool { let current_version: Version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); - let latest_version = github::latest_version(); + let latest_version = github::latest_version(GH_OWNER, GH_REPO); current_version < latest_version } @@ -46,7 +47,7 @@ pub fn run(update_only: bool) { println!("Performing launcher self-update."); println!( "If you run into any issues, please download the latest version at {}", - github::latest_release_url() + github::latest_release_url(GH_OWNER, GH_REPO) ); let update_binary = PathBuf::from("alterware-launcher-update.exe"); @@ -59,7 +60,7 @@ pub fn run(update_only: bool) { http::download_file( &format!( "{}/download/alterware-launcher.exe", - github::latest_release_url() + github::latest_release_url(GH_OWNER, GH_REPO) ), &file_path, );