From 762d96baeeefa68caac3dfcd64fb08409d1d5166 Mon Sep 17 00:00:00 2001 From: Ian Mason Date: Sat, 3 Sep 2022 18:56:03 -0700 Subject: [PATCH] Added a semver class Auto updater improvements, not yet ready Registry and Epic Games auto-detection started --- Cargo.toml | 1 + src/main.rs | 98 +++++++++++++++++++++++++++++++-------------------- src/semver.rs | 31 ++++++++++++++++ 3 files changed, 91 insertions(+), 39 deletions(-) create mode 100644 src/semver.rs diff --git a/Cargo.toml b/Cargo.toml index fdc24de..281f19d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ iui = "0.3.0" serde_json = "1.0" tokio = {version="1", features=["full"]} druid = "0.7.0" +registry = "1" [build-dependencies] embed-resource = "1.6" diff --git a/src/main.rs b/src/main.rs index 1637d77..11fa434 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::().unwrap(); - let minor_ver = cur_vers.next().unwrap().parse::().unwrap(); - let patch_ver = cur_vers.next().unwrap().parse::().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::().unwrap(); - let tag_minor_ver = vers.next().unwrap().parse::().unwrap(); - let tag_patch_ver = vers.next().unwrap().parse::().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::().unwrap(); - let tag_minor_ver = vers.next().unwrap().parse::().unwrap(); - let tag_patch_ver = vers.next().unwrap().parse::().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>(source: T, dest: T) { fs_extra::dir::copy(source, dest, ©_opts).unwrap(); } +fn detect_steam() -> Option { + None +} + +fn detect_epic() -> Option { + let default_folder = String::from("C:\\Program Files\\Epic Games\\AmongUs"); + None +} + fn detect_among_us_folder() -> Option { + 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 { } 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\\\\", diff --git a/src/semver.rs b/src/semver.rs new file mode 100644 index 0000000..95eedbe --- /dev/null +++ b/src/semver.rs @@ -0,0 +1,31 @@ +//! # SemVer + +//! A simple Semantic Versioning struct to handle comparisons and ordering + +// Uses +use std::fmt; + +#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)] +pub struct SemVer { + pub major: i32, + pub minor: i32, + pub patch: i32, +} + +impl fmt::Display for SemVer { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}.{}.{}", self.major, self.minor, self.patch) + } +} + +impl From<&str> for SemVer { + fn from(s: &str) -> SemVer { + let v: Vec<&str> = s.split('.').collect(); + + SemVer { + major: v[0].parse().unwrap(), + minor: v[1].parse().unwrap(), + patch: v[2].parse().unwrap(), + } + } +}