From d6e3a7133bc8f626a62f70cb796bd3323adcb2e1 Mon Sep 17 00:00:00 2001 From: Ian Mason Date: Mon, 29 Aug 2022 18:41:03 -0700 Subject: [PATCH] Move AmongUsVersion to its own file. Concatenate and reduce imports. --- src/among_us_version.rs | 38 +++++++++++++++++++ src/main.rs | 84 +++++++++++++++-------------------------- 2 files changed, 69 insertions(+), 53 deletions(-) create mode 100644 src/among_us_version.rs diff --git a/src/among_us_version.rs b/src/among_us_version.rs new file mode 100644 index 0000000..82473e8 --- /dev/null +++ b/src/among_us_version.rs @@ -0,0 +1,38 @@ +use std::fmt; +#[derive(Ord, PartialOrd, Eq, PartialEq)] +pub struct AmongUsVersion { + year: i32, + month: i32, + day: i32, +} + +impl fmt::Display for AmongUsVersion { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}.{}.{}", self.year, self.month, self.day) + } +} + +impl From<&str> for AmongUsVersion { + fn from(s: &str) -> AmongUsVersion { + // Ignore a prepending "v" + let tmp_str = s.replace("v", ""); + + let v: Vec<&str> = tmp_str.split(".").collect(); + + AmongUsVersion { + year: i32::from_str_radix(v[0], 10).unwrap(), + month: i32::from_str_radix(v[1], 10).unwrap(), + day: i32::from_str_radix(v[2], 10).unwrap(), + } + } +} + +impl From<(i32, i32, i32)> for AmongUsVersion { + fn from(s: (i32, i32, i32)) -> AmongUsVersion { + AmongUsVersion { + year: s.0, + month: s.1, + day: s.2, + } + } +} diff --git a/src/main.rs b/src/main.rs index 3931984..d4a7226 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,21 @@ -use std::cell::RefCell; -use std::path::{Path, PathBuf}; -use std::rc::Rc; -use std::str; -use std::*; +// Modules +mod among_us_version; + +// Uses +use among_us_version::*; + +use std::{cell::RefCell, fs, io, path::Path, path::PathBuf, process, rc::Rc, str}; -use druid::widget::*; -use druid::{ - commands, AppDelegate, AppLauncher, Application, Data, DelegateCtx, Env, FileDialogOptions, - Handled, Target, WidgetExt, WindowDesc, -}; use fs_extra::{copy_items, dir}; + use regex::Regex; +// GUI stuff +use druid::{ + commands, widget::*, AppDelegate, AppLauncher, Application, Data, DelegateCtx, Env, + FileDialogOptions, Handled, Target, WidgetExt, WindowDesc, +}; + struct Delegate; #[derive(Data, Clone)] @@ -21,36 +25,12 @@ struct AppData { static AMONG_US_APPID: &'static str = "945360"; -#[derive(Ord, PartialOrd, Eq, PartialEq)] -struct AmongUsVersion { - year: i32, - month: i32, - day: i32, -} - -impl fmt::Display for AmongUsVersion { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}.{}.{}", self.year, self.month, self.day) - } -} - -impl From<&str> for AmongUsVersion { - fn from(s: &str) -> AmongUsVersion { - let v: Vec<&str> = s.split(".").collect(); - AmongUsVersion { - year: i32::from_str_radix(v[0], 10).unwrap(), - month: i32::from_str_radix(v[1], 10).unwrap(), - day: i32::from_str_radix(v[2], 10).unwrap(), - } - } -} - fn attempt_run_among_us(install_path: &Path) { - let executable_path: path::PathBuf = [install_path.to_str().unwrap(), "Among Us.exe"] + let executable_path: PathBuf = [install_path.to_str().unwrap(), "Among Us.exe"] .iter() .collect(); - std::process::Command::new(executable_path.to_str().unwrap()) + process::Command::new(executable_path.to_str().unwrap()) .spawn() .unwrap(); } @@ -170,7 +150,7 @@ async fn main() { // DETERMINE AMONG US VERSION - let mut among_us_folder = path::PathBuf::new(); + let mut among_us_folder = PathBuf::new(); let mut existing_file_path = data_path.clone(); existing_file_path.push("existing_among_us_dir.txt"); @@ -203,7 +183,7 @@ async fn main() { .launch(data) .unwrap_or_default(); - among_us_folder = path::PathBuf::from(Rc::try_unwrap(path_rc).unwrap().take()); + among_us_folder = PathBuf::from(Rc::try_unwrap(path_rc).unwrap().take()); println!("{}", among_us_folder.to_str().unwrap()); @@ -232,19 +212,18 @@ async fn main() { 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(); + let new_installed_path: PathBuf = [installs_path.to_str().unwrap(), version_smash.as_str()] + .iter() + .collect(); - if !path::Path::exists(&new_installed_path) { + if !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(), ); - let among_us_path: path::PathBuf = [installs_path.to_str().unwrap(), "Among Us\\"] + let among_us_path: PathBuf = [installs_path.to_str().unwrap(), "Among Us\\"] .iter() .collect(); @@ -270,7 +249,7 @@ async fn main() { let downloaded_filename = ver_url.1.rsplit("/").nth(0).unwrap(); download_path.push(downloaded_filename.clone()); - if !path::Path::exists(&download_path) { + if !Path::exists(&download_path) { // println!("{:?}", download_path); println!("Downloading Town of Us... [{}]", ver_url.1.clone()); let zip = reqwest::get(ver_url.1.clone()) @@ -292,10 +271,9 @@ async fn main() { root_folder_path = String::from(i.split("/").nth(0).unwrap()); break; } - let extracted_path: path::PathBuf = - [data_path.to_str().unwrap(), root_folder_path.as_str(), "."] - .iter() - .collect(); + let extracted_path: PathBuf = [data_path.to_str().unwrap(), root_folder_path.as_str(), "."] + .iter() + .collect(); println!("{}", extracted_path.to_str().unwrap()); copy_folder_contents_to_target( extracted_path.to_str().unwrap(), @@ -310,7 +288,7 @@ async fn main() { let mut root_column: druid::widget::Flex = druid::widget::Flex::column(); if let Ok(iter) = install_iter { - let mut collection: Vec> = iter.collect(); + let mut collection: Vec> = iter.collect(); collection.reverse(); // Iterate first to find labels: @@ -392,7 +370,7 @@ async fn main() { // .iter() // .collect(); - // std::process::Command::new(executable_path.to_str().unwrap()) + // process::Command::new(executable_path.to_str().unwrap()) // .spawn() // .unwrap(); } @@ -506,7 +484,7 @@ fn determine_among_us_version(folder_root: String) -> Option { // )) } -fn copy_folder_to_target>(source: T, dest: T) { +fn copy_folder_to_target>(source: T, dest: T) { let mut copy_opts = dir::CopyOptions::new(); copy_opts.overwrite = true; copy_opts.skip_exist = true; @@ -516,7 +494,7 @@ fn copy_folder_to_target>(source: T, dest: T) { copy_items(&from_paths, dest, ©_opts).unwrap(); } -fn copy_folder_contents_to_target>(source: T, dest: T) { +fn copy_folder_contents_to_target>(source: T, dest: T) { let mut copy_opts = dir::CopyOptions::new(); copy_opts.overwrite = true; copy_opts.skip_exist = true;