Added a semver class

Auto updater improvements, not yet ready

Registry and Epic Games auto-detection started
This commit is contained in:
2022-09-03 18:56:03 -07:00
parent 81e8eaad0f
commit 762d96baee
3 changed files with 91 additions and 39 deletions

View File

@@ -1,15 +1,27 @@
// Modules
mod among_us_version;
mod semver;
// Uses
use among_us_version::*;
use std::{cell::RefCell, fs, io, path::Path, path::PathBuf, process, rc::Rc, str};
use semver::*;
use std::{
cell::RefCell,
fs, io,
path::{Path, PathBuf},
process,
rc::Rc,
str,
};
use fs_extra::{copy_items, dir};
use regex::Regex;
use registry::Hive;
// GUI stuff
use druid::{
commands, widget::*, AppDelegate, AppLauncher, Application, Data, DelegateCtx, Env,
@@ -60,10 +72,7 @@ fn unmod_among_us_folder(folder_path: &Path) {
async fn get_latest_updater_version() -> Result<(String, String), reqwest::Error> {
let version = env!("CARGO_PKG_VERSION");
let mut cur_vers = version.split('.');
let major_ver = cur_vers.next().unwrap().parse::<i32>().unwrap();
let minor_ver = cur_vers.next().unwrap().parse::<i32>().unwrap();
let patch_ver = cur_vers.next().unwrap().parse::<i32>().unwrap();
let local_sem_ver = SemVer::from(version);
let body =
reqwest::get("https://git.dormedas.com/api/v1/repos/dormedas/town-of-us-updater/releases");
@@ -72,33 +81,33 @@ async fn get_latest_updater_version() -> Result<(String, String), reqwest::Error
for i in root.as_array().unwrap() {
let tag_name = i["tag_name"].as_str().unwrap();
if let Some(trimmed_str) = tag_name.strip_prefix('v') {
let mut vers = trimmed_str.split('.');
let tag_major_ver = vers.next().unwrap().parse::<i32>().unwrap();
let tag_minor_ver = vers.next().unwrap().parse::<i32>().unwrap();
let tag_patch_ver = vers.next().unwrap().parse::<i32>().unwrap();
if tag_major_ver > major_ver
|| (tag_major_ver == major_ver && tag_minor_ver > minor_ver)
|| (tag_major_ver == major_ver
&& tag_minor_ver == minor_ver
&& tag_patch_ver > patch_ver)
{
println!("New version of the updater detected!");
let remote_sem_ver = SemVer::from(trimmed_str);
if remote_sem_ver > local_sem_ver {
for j in i["assets"].as_array().unwrap() {
if j["name"].as_str().unwrap().contains(".exe") {
// let url = j["browser_download_url"].as_str().unwrap();
// let data = reqwest::get(url).await?.bytes().await?;
// let mut exe_dir = std::env::current_dir().unwrap();
// exe_dir.push("town-of-us-updater-new.exe");
// fs::write(exe_dir, data).unwrap();
}
}
println!("New version ({}) of the updater detected!", remote_sem_ver);
}
println!("{}", trimmed_str);
} else {
let mut vers = tag_name.split('.');
let tag_major_ver = vers.next().unwrap().parse::<i32>().unwrap();
let tag_minor_ver = vers.next().unwrap().parse::<i32>().unwrap();
let tag_patch_ver = vers.next().unwrap().parse::<i32>().unwrap();
if tag_major_ver > major_ver
|| (tag_major_ver == major_ver && tag_minor_ver > minor_ver)
|| (tag_major_ver == major_ver
&& tag_minor_ver == minor_ver
&& tag_patch_ver > patch_ver)
{
println!("New version of the updater detected!");
let remote_sem_ver = SemVer::from(tag_name);
if remote_sem_ver > local_sem_ver {
for j in i["assets"].as_array().unwrap() {
if j["name"].as_str().unwrap().contains(".exe") {
// let url = j["browser_download_url"].as_str().unwrap();
// let data = reqwest::get(url).await?.bytes().await?;
// let mut exe_dir = std::env::current_dir().unwrap();
// exe_dir.push("town-of-us-updater-new.exe");
// fs::write(exe_dir, data).unwrap();
}
}
println!("New version ({}) of the updater detected!", remote_sem_ver);
}
println!("{}", i["tag_name"]);
}
}
@@ -262,11 +271,13 @@ async fn main() {
fs::write(download_path.clone(), zip).unwrap();
}
let opened_zip = fs::File::open(download_path).unwrap();
let opened_zip = fs::File::open(download_path.clone()).unwrap();
println!("Extracting mod zip file...");
let mut archive = zip::ZipArchive::new(opened_zip).unwrap();
archive.extract(data_path.clone()).unwrap();
fs::remove_file(download_path).unwrap();
let mut root_folder_path = String::new();
for i in archive.file_names() {
root_folder_path = String::from(i.split('/').next().unwrap());
@@ -489,7 +500,25 @@ fn copy_folder_contents_to_target<T: AsRef<Path>>(source: T, dest: T) {
fs_extra::dir::copy(source, dest, &copy_opts).unwrap();
}
fn detect_steam() -> Option<String> {
None
}
fn detect_epic() -> Option<String> {
let default_folder = String::from("C:\\Program Files\\Epic Games\\AmongUs");
None
}
fn detect_among_us_folder() -> Option<String> {
if let Ok(steam_regkey) = Hive::LocalMachine.open(
r"SOFTWARE\WOW6432Node\Valve\Steam",
registry::Security::Read,
) {
if let Ok(steam_folder) = steam_regkey.value("InstallPath") {
println!("{:?}", steam_folder);
}
}
let library_folder =
fs::read_to_string("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf");
@@ -503,20 +532,11 @@ fn detect_among_us_folder() -> Option<String> {
} else {
library_folder_string.truncate(appid_index.unwrap());
}
//println!("{}", library_folder_string);
let path_regex = Regex::new(r#"path"\s+"([Z-a\w\d\s\\\(\):]+)""#).unwrap();
let caps: regex::CaptureMatches = path_regex.captures_iter(&library_folder_string);
let last_path = caps.last().unwrap();
let start = last_path.get(last_path.len() - 1).unwrap();
/*
println!(
"{}",
library_folder_string
.get(start.start()..start.end())
.unwrap()
);
*/
return Some(format!(
"{}\\\\steamapps\\\\common\\\\Among Us\\\\",