Compare commits
9 Commits
v0.1.0
...
9132afe917
| Author | SHA1 | Date | |
|---|---|---|---|
| 9132afe917 | |||
| ebad7cb893 | |||
| b4c22cdc48 | |||
| 54b2b8b117 | |||
| 91579d333e | |||
| 0b940aea70 | |||
| 86d1e176d4 | |||
| b6221643e2 | |||
| 3150019b76 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
|
||||
@@ -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,9 @@ 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"]}
|
||||
druid = "0.7.0"
|
||||
|
||||
[build-dependencies]
|
||||
embed-resource = "1.6"
|
||||
|
||||
BIN
assets/icon.ico
Normal file
BIN
assets/icon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 142 KiB |
BIN
assets/logo.png
Normal file
BIN
assets/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 42 KiB |
1
assets/main.rc
Normal file
1
assets/main.rc
Normal file
@@ -0,0 +1 @@
|
||||
1 ICON "icon.ico"
|
||||
5
src/build.rs
Normal file
5
src/build.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
extern crate embed_resource;
|
||||
|
||||
fn main() {
|
||||
embed_resource::compile("assets/main.rc");
|
||||
}
|
||||
335
src/main.rs
335
src/main.rs
@@ -1,14 +1,27 @@
|
||||
use std::cell::RefCell;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::rc::Rc;
|
||||
use std::str;
|
||||
use std::*;
|
||||
|
||||
use druid::widget::*;
|
||||
use druid::{
|
||||
commands, AppDelegate, AppLauncher, Application, Data, DelegateCtx, Env, FileDialogOptions,
|
||||
Handled, Target, WidgetExt, WindowDesc,
|
||||
};
|
||||
use fs_extra::{copy_items, dir};
|
||||
use iui::controls::{Button, Group, Label, VerticalBox};
|
||||
use iui::prelude::*;
|
||||
use regex::Regex;
|
||||
|
||||
struct Delegate;
|
||||
|
||||
#[derive(Data, Clone)]
|
||||
struct AppData {
|
||||
pub among_us_path: Rc<RefCell<String>>,
|
||||
}
|
||||
|
||||
static AMONG_US_APPID: &'static str = "945360";
|
||||
|
||||
#[derive(Ord, PartialOrd, Eq, PartialEq)]
|
||||
struct AmongUsVersion {
|
||||
year: i32,
|
||||
month: i32,
|
||||
@@ -61,7 +74,91 @@ 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")))
|
||||
}
|
||||
|
||||
impl AppDelegate<AppData> for Delegate {
|
||||
fn command(
|
||||
&mut self,
|
||||
_ctx: &mut DelegateCtx,
|
||||
_target: Target,
|
||||
cmd: &druid::Command,
|
||||
data: &mut AppData,
|
||||
_env: &Env,
|
||||
) -> Handled {
|
||||
if let Some(file_info) = cmd.get(commands::OPEN_FILE) {
|
||||
println!("{:?}", file_info);
|
||||
let among_us_folder = file_info.path();
|
||||
let mut buf = among_us_folder.to_path_buf();
|
||||
buf.pop();
|
||||
let mut borrow = data.among_us_path.borrow_mut();
|
||||
*borrow = String::from(buf.to_str().unwrap());
|
||||
|
||||
// Pop the selected file off the end of the path
|
||||
// among_us_folder.pop();
|
||||
println!("{}", buf.to_str().unwrap());
|
||||
println!("Handled!");
|
||||
Application::global().quit();
|
||||
return Handled::Yes;
|
||||
}
|
||||
Handled::No
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let version = env!("CARGO_PKG_VERSION");
|
||||
let title_string: String = format!("Town of Us Updater - {}", 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");
|
||||
@@ -71,36 +168,54 @@ fn main() {
|
||||
installs_path.push("installs");
|
||||
fs::create_dir(installs_path.clone()).unwrap_or(());
|
||||
|
||||
let ui = UI::init().expect("UI failed to init");
|
||||
// DETERMINE AMONG US VERSION
|
||||
|
||||
let mut win = Window::new(&ui, "Town of Us Updater", 600, 400, WindowType::NoMenubar);
|
||||
|
||||
let mut vbox = VerticalBox::new(&ui);
|
||||
|
||||
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 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 {
|
||||
let path_rc = Rc::new(RefCell::new(String::from("")));
|
||||
let data: AppData = AppData {
|
||||
among_us_path: path_rc.clone(),
|
||||
};
|
||||
let open_button = Button::new("Locate Among Us").fix_height(45.0).on_click(
|
||||
move |ctx, _: &mut AppData, _| {
|
||||
ctx.submit_command(
|
||||
druid::commands::SHOW_OPEN_PANEL.with(FileDialogOptions::new()),
|
||||
);
|
||||
},
|
||||
);
|
||||
let flex: Flex<AppData> = Flex::column().with_flex_child(open_button, 1.0);
|
||||
let open_dialog = WindowDesc::new(|| flex)
|
||||
.title("Among Us Not Found!")
|
||||
.window_size((400.0, 400.0));
|
||||
AppLauncher::with_window(open_dialog)
|
||||
.delegate(Delegate)
|
||||
.launch(data)
|
||||
.unwrap_or_default();
|
||||
|
||||
among_us_folder = path::PathBuf::from(Rc::try_unwrap(path_rc).unwrap().take());
|
||||
|
||||
println!("{}", among_us_folder.to_str().unwrap());
|
||||
|
||||
// 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 +223,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 +256,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,11 +271,13 @@ 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());
|
||||
let zip = reqwest::blocking::get(ver_url.1.clone())
|
||||
// println!("{:?}", download_path);
|
||||
println!("Downloading Town of Us... [{}]", ver_url.1.clone());
|
||||
let zip = reqwest::get(ver_url.1.clone())
|
||||
.await
|
||||
.unwrap()
|
||||
.bytes()
|
||||
.await
|
||||
.unwrap();
|
||||
fs::write(download_path.clone(), zip).unwrap();
|
||||
}
|
||||
@@ -183,37 +307,80 @@ fn main() {
|
||||
|
||||
// Find existing installs, make buttons for them
|
||||
let install_iter = fs::read_dir(installs_path.clone());
|
||||
let mut root_column: druid::widget::Flex<u32> = druid::widget::Flex::column();
|
||||
|
||||
match install_iter {
|
||||
Ok(iter) => {
|
||||
let mut collection: Vec<Result<fs::DirEntry, 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());
|
||||
button.on_clicked(&ui, {
|
||||
let ui = ui.clone();
|
||||
let installs_path = installs_path.clone();
|
||||
let existing_ver_smash = existing_ver_smash.clone();
|
||||
move |btn| {
|
||||
let mut new_path = installs_path.clone();
|
||||
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...");
|
||||
}
|
||||
});
|
||||
vbox.append(&ui, button, LayoutStrategy::Stretchy);
|
||||
println!("{}", existing_ver_smash.clone().to_str().unwrap());
|
||||
if let Ok(iter) = install_iter {
|
||||
let mut collection: Vec<Result<fs::DirEntry, std::io::Error>> = iter.collect();
|
||||
collection.reverse();
|
||||
|
||||
// Iterate first to find labels:
|
||||
|
||||
let mut flexbox_array: Vec<druid::widget::Flex<u32>> = Vec::new();
|
||||
let mut auv_array: Vec<String> = Vec::new();
|
||||
|
||||
for i in &collection {
|
||||
let existing_ver_smash = i.as_ref().unwrap().file_name();
|
||||
let mut ver_smash_split = existing_ver_smash.to_str().unwrap().split("-");
|
||||
let auv = ver_smash_split.next().unwrap();
|
||||
if !auv_array.contains(&auv.to_string()) {
|
||||
let label_text = format!("Among Us {}", auv);
|
||||
let flex: druid::widget::Flex<u32> = druid::widget::Flex::column()
|
||||
.with_flex_child(
|
||||
druid::widget::Label::new(label_text.as_str()).with_text_size(24.0),
|
||||
1.0,
|
||||
)
|
||||
.with_default_spacer();
|
||||
flexbox_array.push(flex);
|
||||
auv_array.push(auv.to_string());
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
|
||||
for i in collection {
|
||||
let existing_ver_smash = i.unwrap().file_name();
|
||||
let mut ver_smash_split = existing_ver_smash.to_str().unwrap().split("-");
|
||||
let auv = ver_smash_split.next().unwrap();
|
||||
let button_string: String = format!("Town of Us {}", ver_smash_split.next().unwrap());
|
||||
|
||||
let mut index = 0;
|
||||
for j in &auv_array {
|
||||
if j == auv {
|
||||
let mut clone = installs_path.clone();
|
||||
clone.push(existing_ver_smash.clone());
|
||||
|
||||
let fybutton = druid::widget::Button::new(button_string.as_str())
|
||||
.fix_height(45.0)
|
||||
.on_click(move |_, _: &mut u32, _| {
|
||||
attempt_run_among_us(&clone);
|
||||
});
|
||||
|
||||
flexbox_array
|
||||
.get_mut(index)
|
||||
.unwrap()
|
||||
.add_flex_child(fybutton, 1.0);
|
||||
|
||||
println!("{}", existing_ver_smash.clone().to_str().unwrap());
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
for i in flexbox_array {
|
||||
root_column.add_flex_child(i, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
win.set_child(&ui, vbox);
|
||||
win.show(&ui);
|
||||
ui.main();
|
||||
// println!("Checking for updater updates...");
|
||||
// let vals: (String, String) = version_check_thread_handle.join().unwrap();
|
||||
|
||||
println!("Launching main window...");
|
||||
|
||||
let main_window = WindowDesc::new(|| root_column)
|
||||
.title(title_string)
|
||||
.window_size((400.0, 400.0));
|
||||
let app_launcher = AppLauncher::with_window(main_window);
|
||||
let external_handler = app_launcher.get_external_handle();
|
||||
app_launcher.launch(1).unwrap();
|
||||
// AppLauncher::with_window(main_window).launch(1).unwrap();
|
||||
|
||||
// let mut install_folder_path = installs_path.clone();
|
||||
// install_folder_path.push(version_smash);
|
||||
// fs::create_dir(install_folder_path.clone()).unwrap_or(());
|
||||
@@ -231,21 +398,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 +441,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 +449,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 Town of Us 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 +492,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 +524,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, ©_opts).unwrap();
|
||||
}
|
||||
|
||||
fn detect_among_us_folder() -> Option<String> {
|
||||
let library_folder =
|
||||
fs::read_to_string("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf");
|
||||
|
||||
Reference in New Issue
Block a user