mirror of
https://github.com/alterware/alterware-launcher.git
synced 2025-12-04 15:27:48 +00:00
cleanup misc
This commit is contained in:
@@ -13,7 +13,7 @@ use structs::*;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate simple_log;
|
extern crate simple_log;
|
||||||
|
|
||||||
use colored::*;
|
use colored::Colorize;
|
||||||
use indicatif::ProgressBar;
|
use indicatif::ProgressBar;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use mslnk::ShellLink;
|
use mslnk::ShellLink;
|
||||||
|
|||||||
113
src/misc.rs
113
src/misc.rs
@@ -1,18 +1,18 @@
|
|||||||
use indicatif::{ProgressBar, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
use std::{
|
use std::{
|
||||||
fs::{self, OpenOptions},
|
fs::{self, File},
|
||||||
io::Write,
|
io::Read,
|
||||||
path::Path,
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{global, structs};
|
use crate::{global, structs};
|
||||||
|
|
||||||
pub fn file_blake3(file: &std::path::Path) -> std::io::Result<String> {
|
pub fn file_blake3(file: &Path) -> std::io::Result<String> {
|
||||||
let mut blake3 = blake3::Hasher::new();
|
let mut blake3 = blake3::Hasher::new();
|
||||||
let mut file = std::fs::File::open(file)?;
|
let mut file = File::open(file)?;
|
||||||
let mut buffer = [0; 1024];
|
let mut buffer = [0; 1024];
|
||||||
loop {
|
loop {
|
||||||
let n = std::io::Read::read(&mut file, &mut buffer)?;
|
let n = file.read(&mut buffer)?;
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -52,14 +52,14 @@ pub fn human_readable_bytes(bytes: u64) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn pb_style_download(pb: &ProgressBar, state: bool) {
|
pub fn pb_style_download(pb: &ProgressBar, state: bool) {
|
||||||
if state {
|
let style = if state {
|
||||||
pb.set_style(
|
ProgressStyle::with_template(
|
||||||
ProgressStyle::with_template("{spinner:.magenta} {msg:.magenta} > {bytes}/{total_bytes} | {bytes_per_sec} | {eta}")
|
"{spinner:.magenta} {msg:.magenta} > {bytes}/{total_bytes} | {bytes_per_sec} | {eta}",
|
||||||
.unwrap(),
|
)
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
pb.set_style(ProgressStyle::with_template("{spinner:.magenta} {msg}").unwrap());
|
ProgressStyle::with_template("{spinner:.magenta} {msg}")
|
||||||
}
|
};
|
||||||
|
pb.set_style(style.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cute_path(path: &Path) -> String {
|
pub fn cute_path(path: &Path) -> String {
|
||||||
@@ -68,15 +68,16 @@ pub fn cute_path(path: &Path) -> String {
|
|||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub fn is_program_in_path(program: &str) -> bool {
|
pub fn is_program_in_path(program: &str) -> bool {
|
||||||
if let Ok(path) = std::env::var("PATH") {
|
std::env::var_os("PATH")
|
||||||
for p in path.split(':') {
|
.map(|paths| {
|
||||||
let p_str = format!("{p}/{program}");
|
paths.to_str().map(|paths| {
|
||||||
if fs::metadata(p_str).is_ok() {
|
paths
|
||||||
return true;
|
.split(':')
|
||||||
}
|
.any(|dir| fs::metadata(format!("{}/{}", dir, program)).is_ok())
|
||||||
}
|
})
|
||||||
}
|
})
|
||||||
false
|
.flatten()
|
||||||
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
@@ -99,22 +100,20 @@ macro_rules! println_error {
|
|||||||
fn install_dependency(path: &Path, args: &[&str]) {
|
fn install_dependency(path: &Path, args: &[&str]) {
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
match runas::Command::new(path).args(args).status() {
|
match runas::Command::new(path).args(args).status() {
|
||||||
|
Ok(status) if status.success() || matches!(status.code(), Some(1638) | Some(3010)) => {
|
||||||
|
info!("{} installed successfully", path.display());
|
||||||
|
}
|
||||||
Ok(status) => {
|
Ok(status) => {
|
||||||
if !status.success() && !matches!(status.code(), Some(1638) | Some(3010)) {
|
println_error!("Error installing dependency {}, {status}", path.display());
|
||||||
println_error!("Error installing dependency {}, {status}", path.display());
|
}
|
||||||
} else {
|
Err(e) if e.raw_os_error() == Some(740) => {
|
||||||
info!("{} installed successfully", path.display());
|
println_error!(
|
||||||
}
|
"Error: Process requires elevation. Please run the launcher as administrator or install {} manually",
|
||||||
|
path.display()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if let Some(740) = e.raw_os_error() {
|
println_error!("Error running file {}: {e}", path.display());
|
||||||
println_error!(
|
|
||||||
"Error: Process requires elevation. Please run the launcher as administrator or install {} manually",
|
|
||||||
path.display()
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
println_error!("Error running file {}: {e}", path.display());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -127,17 +126,17 @@ async fn download_and_install_dependency(url: &str, path: &Path, args: &[&str])
|
|||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
info!("Downloading {} from {url}", path.display());
|
info!("Downloading {} from {url}", path.display());
|
||||||
if let Some(parent) = path.parent() {
|
if let Some(parent) = path.parent() {
|
||||||
match fs::create_dir_all(parent) {
|
if let Err(e) = fs::create_dir_all(parent) {
|
||||||
Ok(_) => (),
|
println_error!("Error creating directory {}: {e}", parent.display());
|
||||||
Err(e) => {
|
return;
|
||||||
println_error!("Error creating directory {}: {e}", parent.display());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match crate::http_async::download_file(url, &std::path::PathBuf::from(path)).await {
|
match crate::http_async::download_file(url, &PathBuf::from(path)).await {
|
||||||
Ok(_) => info!("Downloaded {}", path.display()),
|
Ok(_) => info!("Downloaded {}", path.display()),
|
||||||
Err(e) => println_error!("Error downloading {}: {e}", path.display()),
|
Err(e) => {
|
||||||
|
println_error!("Error downloading {}: {e}", path.display());
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
install_dependency(path, args);
|
install_dependency(path, args);
|
||||||
@@ -170,29 +169,19 @@ pub fn prefix(tag_name: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_cache(dir: &Path) -> structs::Cache {
|
pub fn get_cache(dir: &Path) -> structs::Cache {
|
||||||
let mut cache_file = OpenOptions::new()
|
let cache_path = dir.join("awcache.json");
|
||||||
.read(true)
|
let cache_content = fs::read_to_string(cache_path).unwrap_or_default();
|
||||||
.create(true)
|
if cache_content.trim().is_empty() {
|
||||||
.append(true)
|
|
||||||
.open(dir.join("awcache.json"))
|
|
||||||
.unwrap();
|
|
||||||
let mut cache_s = String::new();
|
|
||||||
std::io::Read::read_to_string(&mut cache_file, &mut cache_s).unwrap();
|
|
||||||
let cache: structs::Cache = if cache_s.trim().is_empty() {
|
|
||||||
structs::Cache::default()
|
structs::Cache::default()
|
||||||
} else {
|
} else {
|
||||||
serde_json::from_str(&cache_s).unwrap_or(structs::Cache::default())
|
serde_json::from_str(&cache_content).unwrap_or_default()
|
||||||
};
|
}
|
||||||
cache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_cache(dir: &Path, cache: structs::Cache) {
|
pub fn save_cache(dir: &Path, cache: structs::Cache) {
|
||||||
let mut cache_file = OpenOptions::new()
|
let cache_path = dir.join("awcache.json");
|
||||||
.write(true)
|
|
||||||
.create(true)
|
|
||||||
.truncate(true)
|
|
||||||
.open(dir.join("awcache.json"))
|
|
||||||
.unwrap();
|
|
||||||
let cache_serialized = serde_json::to_string_pretty(&cache).unwrap();
|
let cache_serialized = serde_json::to_string_pretty(&cache).unwrap();
|
||||||
cache_file.write_all(cache_serialized.as_bytes()).unwrap();
|
fs::write(cache_path, cache_serialized).unwrap_or_else(|e| {
|
||||||
|
println_error!("Failed to save cache: {}", e);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use colored::*;
|
use colored::ColoredString;
|
||||||
use std::{collections::HashMap, path::Path};
|
use std::{collections::HashMap, path::Path};
|
||||||
|
|
||||||
#[derive(serde::Deserialize, serde::Serialize, Clone)]
|
#[derive(serde::Deserialize, serde::Serialize, Clone)]
|
||||||
|
|||||||
Reference in New Issue
Block a user