5 Commits

6 changed files with 163 additions and 52 deletions

View File

@@ -1,7 +1,8 @@
[package]
name = "town-of-us-updater"
version = "0.1.0"
version = "1.0.0"
edition = "2021"
build = "src/build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -12,3 +13,8 @@ fs_extra = "1.2.0"
dirs = "4.0.0"
reqwest = {version = "0.11.11", features = ["blocking"]}
iui = "0.3.0"
serde_json = "1.0"
tokio = {version="1", features=["full"]}
[build-dependencies]
embed-resource = "1.6"

BIN
assets/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB

BIN
assets/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

1
assets/main.rc Normal file
View File

@@ -0,0 +1 @@
1 ICON "icon.ico"

5
src/build.rs Normal file
View File

@@ -0,0 +1,5 @@
extern crate embed_resource;
fn main() {
embed_resource::compile("assets/main.rc");
}

View File

@@ -1,5 +1,6 @@
use std::path::{Path, PathBuf};
use std::str;
use std::thread;
use std::*;
use fs_extra::{copy_items, dir};
@@ -9,6 +10,7 @@ use regex::Regex;
static AMONG_US_APPID: &'static str = "945360";
#[derive(Ord, PartialOrd, Eq, PartialEq)]
struct AmongUsVersion {
year: i32,
month: i32,
@@ -61,7 +63,61 @@ fn unmod_among_us_folder(folder_path: &Path) {
fs::remove_dir_all(mono_path).unwrap_or_default();
}
fn main() {
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 body =
reqwest::get("https://git.dormedas.com/api/v1/repos/dormedas/town-of-us-updater/releases");
let root: serde_json::Value = serde_json::from_str(&body.await?.text().await?).unwrap();
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!");
}
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!");
}
println!("{}", i["tag_name"]);
}
}
Ok((String::from("no"), String::from("yes")))
}
#[tokio::main]
async fn main() {
let version = env!("CARGO_PKG_VERSION");
println!("Updater Version: {}", version);
get_latest_updater_version().await.unwrap();
//let _version_check_thread_handle = thread::spawn(move || get_latest_updater_version());
// CREATE PROGRAM DIRECTORY
let mut data_path = dirs::data_dir().unwrap();
data_path.push("town-of-us-updater");
@@ -79,28 +135,42 @@ fn main() {
vbox.set_padded(&ui, true);
let mut button = Button::new(&ui, "Button");
button.on_clicked(&ui, {
let ui = ui.clone();
move |btn| {
let folder_opt = detect_among_us_folder();
if folder_opt.is_some() {
btn.set_text(&ui, "Amogus");
ui.quit();
}
}
});
// let mut button = Button::new(&ui, "Button");
// button.on_clicked(&ui, {
// let ui = ui.clone();
// move |btn| {
// let folder_opt = detect_among_us_folder();
// if folder_opt.is_some() {
// btn.set_text(&ui, "Amogus");
// ui.quit();
// }
// }
// });
// DETERMINE AMONG US VERSION
let folder_opt = detect_among_us_folder();
let mut among_us_folder = path::PathBuf::new();
if folder_opt.is_some() {
among_us_folder.push(folder_opt.unwrap());
let mut existing_file_path = data_path.clone();
existing_file_path.push("existing_among_us_dir.txt");
let existing_found_folder = fs::read_to_string(existing_file_path.clone());
if existing_found_folder.is_ok() {
among_us_folder.push(existing_found_folder.unwrap());
} else {
win.modal_msg(&ui, "Find Among Us", "On the following Open File dialog, navigate to your Among Us folder and select Among Us.exe");
let open_window = win.open_file(&ui);
// println!("{}", open_window.unwrap().to_str().unwrap());
among_us_folder = open_window.unwrap().clone();
among_us_folder.pop();
let folder_opt = detect_among_us_folder();
if folder_opt.is_some() {
among_us_folder.push(folder_opt.unwrap());
} else {
win.modal_msg(&ui, "Find Among Us", "On the following Open File dialog, navigate to your Among Us folder and select Among Us.exe");
let open_window = win.open_file(&ui);
// println!("{}", open_window.unwrap().to_str().unwrap());
among_us_folder = open_window.unwrap().clone();
// Pop the selected file off the end of the path
among_us_folder.pop();
}
fs::write(existing_file_path, among_us_folder.to_str().unwrap()).unwrap();
}
println!("Among Us Folder: {}", among_us_folder.to_str().unwrap());
@@ -108,15 +178,22 @@ fn main() {
let among_us_version =
determine_among_us_version(String::from(among_us_folder.to_str().unwrap())).unwrap();
let ver_url: (String, String, bool) =
determine_town_of_us_url(among_us_version.clone()).unwrap();
determine_town_of_us_url(among_us_version.to_string().clone())
.await
.unwrap();
let version_smash = format!("{}-{}", among_us_version.clone(), ver_url.0.clone());
let version_smash = format!(
"{}-{}",
among_us_version.to_string().clone(),
ver_url.0.clone()
);
let new_installed_path: path::PathBuf =
[installs_path.to_str().unwrap(), version_smash.as_str()]
.iter()
.collect();
if !path::Path::exists(&new_installed_path) {
println!("Copying Among Us to separate location...");
copy_folder_to_target(
among_us_folder.to_str().unwrap(),
installs_path.to_str().unwrap(),
@@ -134,7 +211,7 @@ fn main() {
unmod_among_us_folder(&among_us_path);
println!(
"Rename {} to {}",
"Renaming {} to {}",
among_us_path.to_str().unwrap(),
new_installed_path.to_str().unwrap()
);
@@ -149,8 +226,8 @@ fn main() {
download_path.push(downloaded_filename.clone());
if !path::Path::exists(&download_path) {
println!("{:?}", download_path);
println!("Downloading Town of Us [{}]", ver_url.1.clone());
// println!("{:?}", download_path);
println!("Downloading Town of Us... [{}]", ver_url.1.clone());
let zip = reqwest::blocking::get(ver_url.1.clone())
.unwrap()
.bytes()
@@ -186,12 +263,22 @@ fn main() {
match install_iter {
Ok(iter) => {
let mut collection: Vec<Result<fs::DirEntry, io::Error>> = iter.collect();
let mut collection: Vec<Result<fs::DirEntry, std::io::Error>> = iter.collect();
collection.reverse();
for i in collection {
// for i in iter {
let existing_ver_smash = i.unwrap().file_name();
let mut button = Button::new(&ui, existing_ver_smash.clone().to_str().unwrap());
let mut ver_smash_split = existing_ver_smash.to_str().unwrap().split("-");
let auv = ver_smash_split.next().unwrap();
let button_string: String = format!(
"Among Us {} Town of Us {}",
auv,
ver_smash_split.next().unwrap()
);
let mut group_vbox = VerticalBox::new(&ui);
let mut group = Group::new(&ui, auv.clone());
let mut button = Button::new(&ui, button_string.as_str());
button.on_clicked(&ui, {
let ui = ui.clone();
let installs_path = installs_path.clone();
@@ -201,16 +288,28 @@ fn main() {
new_path.push(existing_ver_smash.clone());
println!("{}", new_path.clone().to_str().unwrap());
attempt_run_among_us(&new_path);
btn.set_text(&ui, "Launching...");
btn.set_text(&ui, "Launching Among Us...");
}
});
vbox.append(&ui, button, LayoutStrategy::Stretchy);
group_vbox.append(&ui, button, LayoutStrategy::Stretchy);
group.set_child(&ui, group_vbox);
vbox.append(&ui, group, LayoutStrategy::Stretchy);
println!("{}", existing_ver_smash.clone().to_str().unwrap());
}
}
_ => (),
}
// println!("Checking for updater updates...");
// let vals: (String, String) = version_check_thread_handle.join().unwrap();
// let mut update_button = Button::new(&ui, "Check for New Version");
// update_button.on_clicked(&ui, {
// let ui = ui.clone();
// move |btn| {}
// });
// vbox.append(&ui, update_button, LayoutStrategy::Stretchy);
win.set_child(&ui, vbox);
win.show(&ui);
ui.main();
@@ -231,21 +330,22 @@ fn main() {
}
// Returns (Version, URL)
fn determine_town_of_us_url(among_us_version: String) -> Option<(String, String, bool)> {
let markdown = reqwest::blocking::get(
"https://raw.githubusercontent.com/eDonnes124/Town-Of-Us-R/master/README.md",
)
.unwrap()
.text()
.unwrap();
async fn determine_town_of_us_url(among_us_version: String) -> Option<(String, String, bool)> {
let markdown =
reqwest::get("https://raw.githubusercontent.com/eDonnes124/Town-Of-Us-R/master/README.md")
.await
.unwrap()
.text()
.await
.unwrap();
let mut line = markdown.find(&among_us_version);
let mut line_offset = 0;
let mut official_compatibility = false;
if line.is_some() {
println!("Found official version!");
println!("Found sanctioned version!");
official_compatibility = true;
} else {
println!("Official version cannot be determined, installing experimental latest...");
println!("Sanctioned version cannot be determined, installing experimental latest...");
line = markdown.find("[Download]");
line_offset = 15;
// println!("At this point, there are two options:");
@@ -273,7 +373,7 @@ fn determine_town_of_us_url(among_us_version: String) -> Option<(String, String,
let captures = url_regex.captures(splits.1).unwrap();
let capture = captures.get(captures.len() - 1).unwrap();
let url = splits.1.get(capture.start()..capture.end()).unwrap();
println!("Official URL is: {}", url);
println!("Mod URL is: {}", url);
let ver_regex = Regex::new(r#"\| (v\d\.\d\.\d) \|"#).unwrap();
let ver_captures = ver_regex.captures(splits.1).unwrap();
let ver_capture = ver_captures.get(ver_captures.len() - 1).unwrap();
@@ -281,11 +381,11 @@ fn determine_town_of_us_url(among_us_version: String) -> Option<(String, String,
.1
.get(ver_capture.start()..ver_capture.end())
.unwrap();
println!("Matching version is: {}", ver);
println!("Installing version: {}", ver);
Some((String::from(ver), String::from(url), official_compatibility))
}
fn determine_among_us_version(folder_root: String) -> Option<String> {
fn determine_among_us_version(folder_root: String) -> Option<AmongUsVersion> {
let asset_file = format!("{}\\Among Us_Data\\globalgamemanagers", folder_root);
const TARGET_BYTES_LEN: usize = 16;
@@ -324,20 +424,18 @@ fn determine_among_us_version(folder_root: String) -> Option<String> {
// offset -= 1;
// }
println!(
"|{}|",
str::from_utf8(file_bytes.get(file_index..file_index + offset).unwrap()).unwrap()
);
// println!(
// "|{}|",
// str::from_utf8(file_bytes.get(file_index..file_index + offset).unwrap()).unwrap()
// );
let ver = AmongUsVersion::from(
str::from_utf8(file_bytes.get(file_index..file_index + offset).unwrap()).unwrap(),
);
println!("AmongUsVersion: {}", ver);
Some(String::from(
str::from_utf8(file_bytes.get(file_index..file_index + offset).unwrap()).unwrap(),
))
//println!("{}", bytes_str);
//None
Some(ver)
// Some(String::from(
// str::from_utf8(file_bytes.get(file_index..file_index + offset).unwrap()).unwrap(),
// ))
}
fn copy_folder_to_target<T: AsRef<path::Path>>(source: T, dest: T) {
@@ -358,6 +456,7 @@ fn copy_folder_contents_to_target<T: AsRef<path::Path>>(source: T, dest: T) {
copy_opts.content_only = true;
fs_extra::dir::copy(source, dest, &copy_opts).unwrap();
}
fn detect_among_us_folder() -> Option<String> {
let library_folder =
fs::read_to_string("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf");