obtain iw4x.dll from iw4x/iw4x-client

This commit is contained in:
mxve 2023-08-29 21:53:47 +02:00
parent 9dc569e646
commit 64c63bf24f
6 changed files with 66 additions and 16 deletions

View File

@ -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)
}

View File

@ -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";

40
src/iw4x.rs Normal file
View File

@ -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();
}
}

View File

@ -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) {

View File

@ -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::<u16>().unwrap_or(0)
}

View File

@ -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,
);