4 Commits

Author SHA1 Message Date
9132afe917 Add .gitignore 2022-08-29 11:32:40 -07:00
ebad7cb893 Fix blocking reqwest crash 2022-08-29 11:31:21 -07:00
b4c22cdc48 Migrated to druid gui 2022-08-21 10:50:48 -07:00
54b2b8b117 Updated log message 2022-08-16 08:55:26 -07:00
4 changed files with 142 additions and 72 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/target /target
Cargo.lock

View File

@@ -15,6 +15,7 @@ reqwest = {version = "0.11.11", features = ["blocking"]}
iui = "0.3.0" 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"
[build-dependencies] [build-dependencies]
embed-resource = "1.6" embed-resource = "1.6"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 42 KiB

View File

@@ -1,13 +1,24 @@
use std::cell::RefCell;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::str; use std::str;
use std::thread;
use std::*; 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 fs_extra::{copy_items, dir};
use iui::controls::{Button, Group, Label, VerticalBox};
use iui::prelude::*;
use regex::Regex; use regex::Regex;
struct Delegate;
#[derive(Data, Clone)]
struct AppData {
pub among_us_path: Rc<RefCell<String>>,
}
static AMONG_US_APPID: &'static str = "945360"; static AMONG_US_APPID: &'static str = "945360";
#[derive(Ord, PartialOrd, Eq, PartialEq)] #[derive(Ord, PartialOrd, Eq, PartialEq)]
@@ -111,9 +122,39 @@ async fn get_latest_updater_version() -> Result<(String, String), reqwest::Error
Ok((String::from("no"), String::from("yes"))) 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] #[tokio::main]
async fn main() { async fn main() {
let version = env!("CARGO_PKG_VERSION"); let version = env!("CARGO_PKG_VERSION");
let title_string: String = format!("Town of Us Updater - {}", version);
println!("Updater Version: {}", version); println!("Updater Version: {}", version);
get_latest_updater_version().await.unwrap(); get_latest_updater_version().await.unwrap();
//let _version_check_thread_handle = thread::spawn(move || get_latest_updater_version()); //let _version_check_thread_handle = thread::spawn(move || get_latest_updater_version());
@@ -127,26 +168,6 @@ async fn main() {
installs_path.push("installs"); installs_path.push("installs");
fs::create_dir(installs_path.clone()).unwrap_or(()); fs::create_dir(installs_path.clone()).unwrap_or(());
let ui = UI::init().expect("UI failed to init");
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();
// }
// }
// });
// DETERMINE AMONG US VERSION // DETERMINE AMONG US VERSION
let mut among_us_folder = path::PathBuf::new(); let mut among_us_folder = path::PathBuf::new();
@@ -162,13 +183,37 @@ async fn main() {
if folder_opt.is_some() { if folder_opt.is_some() {
among_us_folder.push(folder_opt.unwrap()); among_us_folder.push(folder_opt.unwrap());
} else { } 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 path_rc = Rc::new(RefCell::new(String::from("")));
let open_window = win.open_file(&ui); 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()); // println!("{}", open_window.unwrap().to_str().unwrap());
among_us_folder = open_window.unwrap().clone(); // among_us_folder = open_window.unwrap().clone();
// Pop the selected file off the end of the path // Pop the selected file off the end of the path
among_us_folder.pop(); // among_us_folder.pop();
} }
fs::write(existing_file_path, among_us_folder.to_str().unwrap()).unwrap(); fs::write(existing_file_path, among_us_folder.to_str().unwrap()).unwrap();
} }
@@ -228,9 +273,11 @@ async fn main() {
if !path::Path::exists(&download_path) { if !path::Path::exists(&download_path) {
// println!("{:?}", download_path); // println!("{:?}", download_path);
println!("Downloading Town of Us... [{}]", ver_url.1.clone()); println!("Downloading Town of Us... [{}]", ver_url.1.clone());
let zip = reqwest::blocking::get(ver_url.1.clone()) let zip = reqwest::get(ver_url.1.clone())
.await
.unwrap() .unwrap()
.bytes() .bytes()
.await
.unwrap(); .unwrap();
fs::write(download_path.clone(), zip).unwrap(); fs::write(download_path.clone(), zip).unwrap();
} }
@@ -260,59 +307,80 @@ async fn main() {
// Find existing installs, make buttons for them // Find existing installs, make buttons for them
let install_iter = fs::read_dir(installs_path.clone()); let install_iter = fs::read_dir(installs_path.clone());
let mut root_column: druid::widget::Flex<u32> = druid::widget::Flex::column();
match install_iter { if let Ok(iter) = install_iter {
Ok(iter) => { let mut collection: Vec<Result<fs::DirEntry, std::io::Error>> = iter.collect();
let mut collection: Vec<Result<fs::DirEntry, std::io::Error>> = iter.collect(); collection.reverse();
collection.reverse();
for i in collection {
// for i in iter {
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!(
"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()); // Iterate first to find labels:
button.on_clicked(&ui, {
let ui = ui.clone(); let mut flexbox_array: Vec<druid::widget::Flex<u32>> = Vec::new();
let installs_path = installs_path.clone(); let mut auv_array: Vec<String> = Vec::new();
let existing_ver_smash = existing_ver_smash.clone();
move |btn| { for i in &collection {
let mut new_path = installs_path.clone(); let existing_ver_smash = i.as_ref().unwrap().file_name();
new_path.push(existing_ver_smash.clone()); let mut ver_smash_split = existing_ver_smash.to_str().unwrap().split("-");
println!("{}", new_path.clone().to_str().unwrap()); let auv = ver_smash_split.next().unwrap();
attempt_run_among_us(&new_path); if !auv_array.contains(&auv.to_string()) {
btn.set_text(&ui, "Launching Among Us..."); let label_text = format!("Among Us {}", auv);
} let flex: druid::widget::Flex<u32> = druid::widget::Flex::column()
}); .with_flex_child(
group_vbox.append(&ui, button, LayoutStrategy::Stretchy); druid::widget::Label::new(label_text.as_str()).with_text_size(24.0),
group.set_child(&ui, group_vbox); 1.0,
vbox.append(&ui, group, LayoutStrategy::Stretchy); )
println!("{}", existing_ver_smash.clone().to_str().unwrap()); .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);
}
} }
// println!("Checking for updater updates..."); // println!("Checking for updater updates...");
// let vals: (String, String) = version_check_thread_handle.join().unwrap(); // 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); 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();
win.set_child(&ui, vbox);
win.show(&ui);
ui.main();
// let mut install_folder_path = installs_path.clone(); // let mut install_folder_path = installs_path.clone();
// install_folder_path.push(version_smash); // install_folder_path.push(version_smash);
// fs::create_dir(install_folder_path.clone()).unwrap_or(()); // fs::create_dir(install_folder_path.clone()).unwrap_or(());
@@ -381,7 +449,7 @@ async fn determine_town_of_us_url(among_us_version: String) -> Option<(String, S
.1 .1
.get(ver_capture.start()..ver_capture.end()) .get(ver_capture.start()..ver_capture.end())
.unwrap(); .unwrap();
println!("Installing version: {}", ver); println!("Installing Town of Us version: {}", ver);
Some((String::from(ver), String::from(url), official_compatibility)) Some((String::from(ver), String::from(url), official_compatibility))
} }