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

@@ -16,6 +16,7 @@ iui = "0.3.0"
serde_json = "1.0" serde_json = "1.0"
tokio = {version="1", features=["full"]} tokio = {version="1", features=["full"]}
druid = "0.7.0" druid = "0.7.0"
registry = "1"
[build-dependencies] [build-dependencies]
embed-resource = "1.6" embed-resource = "1.6"

View File

@@ -1,15 +1,27 @@
// Modules // Modules
mod among_us_version; mod among_us_version;
mod semver;
// Uses // Uses
use among_us_version::*; 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 fs_extra::{copy_items, dir};
use regex::Regex; use regex::Regex;
use registry::Hive;
// GUI stuff // GUI stuff
use druid::{ use druid::{
commands, widget::*, AppDelegate, AppLauncher, Application, Data, DelegateCtx, Env, 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> { async fn get_latest_updater_version() -> Result<(String, String), reqwest::Error> {
let version = env!("CARGO_PKG_VERSION"); let version = env!("CARGO_PKG_VERSION");
let mut cur_vers = version.split('.'); let local_sem_ver = SemVer::from(version);
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 body = let body =
reqwest::get("https://git.dormedas.com/api/v1/repos/dormedas/town-of-us-updater/releases"); 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() { for i in root.as_array().unwrap() {
let tag_name = i["tag_name"].as_str().unwrap(); let tag_name = i["tag_name"].as_str().unwrap();
if let Some(trimmed_str) = tag_name.strip_prefix('v') { if let Some(trimmed_str) = tag_name.strip_prefix('v') {
let mut vers = trimmed_str.split('.'); let remote_sem_ver = SemVer::from(trimmed_str);
let tag_major_ver = vers.next().unwrap().parse::<i32>().unwrap(); if remote_sem_ver > local_sem_ver {
let tag_minor_ver = vers.next().unwrap().parse::<i32>().unwrap(); for j in i["assets"].as_array().unwrap() {
let tag_patch_ver = vers.next().unwrap().parse::<i32>().unwrap(); if j["name"].as_str().unwrap().contains(".exe") {
if tag_major_ver > major_ver // let url = j["browser_download_url"].as_str().unwrap();
|| (tag_major_ver == major_ver && tag_minor_ver > minor_ver) // let data = reqwest::get(url).await?.bytes().await?;
|| (tag_major_ver == major_ver // let mut exe_dir = std::env::current_dir().unwrap();
&& tag_minor_ver == minor_ver // exe_dir.push("town-of-us-updater-new.exe");
&& tag_patch_ver > patch_ver) // fs::write(exe_dir, data).unwrap();
{ }
println!("New version of the updater detected!"); }
println!("New version ({}) of the updater detected!", remote_sem_ver);
} }
println!("{}", trimmed_str);
} else { } else {
let mut vers = tag_name.split('.'); let remote_sem_ver = SemVer::from(tag_name);
let tag_major_ver = vers.next().unwrap().parse::<i32>().unwrap(); if remote_sem_ver > local_sem_ver {
let tag_minor_ver = vers.next().unwrap().parse::<i32>().unwrap(); for j in i["assets"].as_array().unwrap() {
let tag_patch_ver = vers.next().unwrap().parse::<i32>().unwrap(); if j["name"].as_str().unwrap().contains(".exe") {
if tag_major_ver > major_ver // let url = j["browser_download_url"].as_str().unwrap();
|| (tag_major_ver == major_ver && tag_minor_ver > minor_ver) // let data = reqwest::get(url).await?.bytes().await?;
|| (tag_major_ver == major_ver // let mut exe_dir = std::env::current_dir().unwrap();
&& tag_minor_ver == minor_ver // exe_dir.push("town-of-us-updater-new.exe");
&& tag_patch_ver > patch_ver) // fs::write(exe_dir, data).unwrap();
{ }
println!("New version of the updater detected!"); }
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(); 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..."); println!("Extracting mod zip file...");
let mut archive = zip::ZipArchive::new(opened_zip).unwrap(); let mut archive = zip::ZipArchive::new(opened_zip).unwrap();
archive.extract(data_path.clone()).unwrap(); archive.extract(data_path.clone()).unwrap();
fs::remove_file(download_path).unwrap();
let mut root_folder_path = String::new(); let mut root_folder_path = String::new();
for i in archive.file_names() { for i in archive.file_names() {
root_folder_path = String::from(i.split('/').next().unwrap()); 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(); 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> { 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 = let library_folder =
fs::read_to_string("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf"); fs::read_to_string("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf");
@@ -503,20 +532,11 @@ fn detect_among_us_folder() -> Option<String> {
} else { } else {
library_folder_string.truncate(appid_index.unwrap()); 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 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 caps: regex::CaptureMatches = path_regex.captures_iter(&library_folder_string);
let last_path = caps.last().unwrap(); let last_path = caps.last().unwrap();
let start = last_path.get(last_path.len() - 1).unwrap(); let start = last_path.get(last_path.len() - 1).unwrap();
/*
println!(
"{}",
library_folder_string
.get(start.start()..start.end())
.unwrap()
);
*/
return Some(format!( return Some(format!(
"{}\\\\steamapps\\\\common\\\\Among Us\\\\", "{}\\\\steamapps\\\\common\\\\Among Us\\\\",

31
src/semver.rs Normal file
View File

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