alterware-launcher/src/self_update.rs

112 lines
3.3 KiB
Rust
Raw Normal View History

2023-08-29 06:47:40 +02:00
use crate::github;
2023-08-29 21:53:47 +02:00
use crate::global::*;
2023-08-29 06:47:40 +02:00
use semver::Version;
#[cfg(not(windows))]
use std::{thread, time};
pub fn self_update_available() -> bool {
let current_version: Version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap();
2023-08-29 21:53:47 +02:00
let latest_version = github::latest_version(GH_OWNER, GH_REPO);
2023-08-29 06:47:40 +02:00
current_version < latest_version
}
#[cfg(not(windows))]
2023-11-06 03:20:46 +01:00
pub fn run(_update_only: bool, _args: Vec<String>) {
2023-08-29 06:47:40 +02:00
if self_update_available() {
println!("A new version of the AlterWare launcher is available.");
println!(
"Download it at {}",
github::latest_release_url(GH_OWNER, GH_REPO)
);
2023-08-29 06:47:40 +02:00
println!("Launching in 10 seconds..");
thread::sleep(time::Duration::from_secs(10));
}
}
#[cfg(windows)]
2023-11-06 22:27:09 +01:00
pub fn restart() -> std::io::Error {
2023-11-06 03:20:46 +01:00
use std::os::windows::process::CommandExt;
match std::process::Command::new(std::env::current_exe().unwrap())
2023-11-06 22:27:09 +01:00
.args(std::env::args().skip(1))
2023-11-06 03:20:46 +01:00
.creation_flags(0x00000010) // CREATE_NEW_CONSOLE
.spawn()
{
Ok(_) => std::process::exit(0),
Err(err) => err,
}
}
#[cfg(windows)]
2023-11-06 22:27:09 +01:00
pub fn run(update_only: bool) {
2023-08-29 06:47:40 +02:00
use std::{fs, path::PathBuf};
use crate::http;
2023-10-06 08:34:42 +02:00
use crate::misc;
2023-08-29 06:47:40 +02:00
let working_dir = std::env::current_dir().unwrap();
let files = fs::read_dir(&working_dir).unwrap();
for file in files {
let file = file.unwrap();
let file_name = file.file_name().into_string().unwrap();
if file_name.contains("alterware-launcher")
&& (file_name.contains(".__relocated__.exe")
|| file_name.contains(".__selfdelete__.exe"))
{
fs::remove_file(file.path()).unwrap_or_else(|_| {
println!("Failed to remove old launcher file.");
});
2023-08-29 06:47:40 +02:00
}
}
if self_update_available() {
println!("Performing launcher self-update.");
println!(
"If you run into any issues, please download the latest version at {}",
2023-08-29 21:53:47 +02:00
github::latest_release_url(GH_OWNER, GH_REPO)
2023-08-29 06:47:40 +02:00
);
let update_binary = PathBuf::from("alterware-launcher-update.exe");
let file_path = working_dir.join(&update_binary);
if update_binary.exists() {
fs::remove_file(&update_binary).unwrap();
}
2023-09-25 18:51:43 +02:00
let launcher_name = if cfg!(target_arch = "x86") {
"alterware-launcher-x86.exe"
} else {
"alterware-launcher.exe"
};
2023-11-06 22:27:09 +01:00
2023-08-29 06:47:40 +02:00
http::download_file(
&format!(
2023-09-25 18:51:43 +02:00
"{}/download/{}",
github::latest_release_url(GH_OWNER, GH_REPO),
launcher_name
2023-08-29 06:47:40 +02:00
),
&file_path,
);
if !file_path.exists() {
println!("Failed to download launcher update.");
return;
}
self_replace::self_replace("alterware-launcher-update.exe").unwrap();
fs::remove_file(&file_path).unwrap();
2023-11-06 03:20:46 +01:00
// restarting spawns a new console, automation should manually restart on exit code 201
2023-08-29 06:47:40 +02:00
if !update_only {
2023-11-06 22:27:09 +01:00
let restart_error = restart().to_string();
2023-11-06 03:20:46 +01:00
println!("Failed to restart launcher: {}", restart_error);
println!("Please restart the launcher manually.");
2023-10-06 08:30:29 +02:00
misc::stdin();
2023-08-29 06:47:40 +02:00
}
std::process::exit(201);
}
}