Move AmongUsVersion to its own file. Concatenate and reduce imports.
This commit is contained in:
38
src/among_us_version.rs
Normal file
38
src/among_us_version.rs
Normal file
@@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
84
src/main.rs
84
src/main.rs
@@ -1,17 +1,21 @@
|
|||||||
use std::cell::RefCell;
|
// Modules
|
||||||
use std::path::{Path, PathBuf};
|
mod among_us_version;
|
||||||
use std::rc::Rc;
|
|
||||||
use std::str;
|
// Uses
|
||||||
use std::*;
|
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 fs_extra::{copy_items, dir};
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
// GUI stuff
|
||||||
|
use druid::{
|
||||||
|
commands, widget::*, AppDelegate, AppLauncher, Application, Data, DelegateCtx, Env,
|
||||||
|
FileDialogOptions, Handled, Target, WidgetExt, WindowDesc,
|
||||||
|
};
|
||||||
|
|
||||||
struct Delegate;
|
struct Delegate;
|
||||||
|
|
||||||
#[derive(Data, Clone)]
|
#[derive(Data, Clone)]
|
||||||
@@ -21,36 +25,12 @@ struct AppData {
|
|||||||
|
|
||||||
static AMONG_US_APPID: &'static str = "945360";
|
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) {
|
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()
|
.iter()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
std::process::Command::new(executable_path.to_str().unwrap())
|
process::Command::new(executable_path.to_str().unwrap())
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
@@ -170,7 +150,7 @@ async fn main() {
|
|||||||
|
|
||||||
// DETERMINE AMONG US VERSION
|
// 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();
|
let mut existing_file_path = data_path.clone();
|
||||||
existing_file_path.push("existing_among_us_dir.txt");
|
existing_file_path.push("existing_among_us_dir.txt");
|
||||||
@@ -203,7 +183,7 @@ async fn main() {
|
|||||||
.launch(data)
|
.launch(data)
|
||||||
.unwrap_or_default();
|
.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());
|
println!("{}", among_us_folder.to_str().unwrap());
|
||||||
|
|
||||||
@@ -232,19 +212,18 @@ async fn main() {
|
|||||||
among_us_version.to_string().clone(),
|
among_us_version.to_string().clone(),
|
||||||
ver_url.0.clone()
|
ver_url.0.clone()
|
||||||
);
|
);
|
||||||
let new_installed_path: path::PathBuf =
|
let new_installed_path: PathBuf = [installs_path.to_str().unwrap(), version_smash.as_str()]
|
||||||
[installs_path.to_str().unwrap(), version_smash.as_str()]
|
.iter()
|
||||||
.iter()
|
.collect();
|
||||||
.collect();
|
|
||||||
|
|
||||||
if !path::Path::exists(&new_installed_path) {
|
if !Path::exists(&new_installed_path) {
|
||||||
println!("Copying Among Us to separate location...");
|
println!("Copying Among Us to separate location...");
|
||||||
copy_folder_to_target(
|
copy_folder_to_target(
|
||||||
among_us_folder.to_str().unwrap(),
|
among_us_folder.to_str().unwrap(),
|
||||||
installs_path.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()
|
.iter()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@@ -270,7 +249,7 @@ async fn main() {
|
|||||||
let downloaded_filename = ver_url.1.rsplit("/").nth(0).unwrap();
|
let downloaded_filename = ver_url.1.rsplit("/").nth(0).unwrap();
|
||||||
download_path.push(downloaded_filename.clone());
|
download_path.push(downloaded_filename.clone());
|
||||||
|
|
||||||
if !path::Path::exists(&download_path) {
|
if !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::get(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());
|
root_folder_path = String::from(i.split("/").nth(0).unwrap());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let extracted_path: path::PathBuf =
|
let extracted_path: PathBuf = [data_path.to_str().unwrap(), root_folder_path.as_str(), "."]
|
||||||
[data_path.to_str().unwrap(), root_folder_path.as_str(), "."]
|
.iter()
|
||||||
.iter()
|
.collect();
|
||||||
.collect();
|
|
||||||
println!("{}", extracted_path.to_str().unwrap());
|
println!("{}", extracted_path.to_str().unwrap());
|
||||||
copy_folder_contents_to_target(
|
copy_folder_contents_to_target(
|
||||||
extracted_path.to_str().unwrap(),
|
extracted_path.to_str().unwrap(),
|
||||||
@@ -310,7 +288,7 @@ async fn main() {
|
|||||||
let mut root_column: druid::widget::Flex<u32> = druid::widget::Flex::column();
|
let mut root_column: druid::widget::Flex<u32> = druid::widget::Flex::column();
|
||||||
|
|
||||||
if let Ok(iter) = install_iter {
|
if let Ok(iter) = install_iter {
|
||||||
let mut collection: Vec<Result<fs::DirEntry, std::io::Error>> = iter.collect();
|
let mut collection: Vec<Result<fs::DirEntry, io::Error>> = iter.collect();
|
||||||
collection.reverse();
|
collection.reverse();
|
||||||
|
|
||||||
// Iterate first to find labels:
|
// Iterate first to find labels:
|
||||||
@@ -392,7 +370,7 @@ async fn main() {
|
|||||||
// .iter()
|
// .iter()
|
||||||
// .collect();
|
// .collect();
|
||||||
|
|
||||||
// std::process::Command::new(executable_path.to_str().unwrap())
|
// process::Command::new(executable_path.to_str().unwrap())
|
||||||
// .spawn()
|
// .spawn()
|
||||||
// .unwrap();
|
// .unwrap();
|
||||||
}
|
}
|
||||||
@@ -506,7 +484,7 @@ fn determine_among_us_version(folder_root: String) -> Option<AmongUsVersion> {
|
|||||||
// ))
|
// ))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_folder_to_target<T: AsRef<path::Path>>(source: T, dest: T) {
|
fn copy_folder_to_target<T: AsRef<Path>>(source: T, dest: T) {
|
||||||
let mut copy_opts = dir::CopyOptions::new();
|
let mut copy_opts = dir::CopyOptions::new();
|
||||||
copy_opts.overwrite = true;
|
copy_opts.overwrite = true;
|
||||||
copy_opts.skip_exist = true;
|
copy_opts.skip_exist = true;
|
||||||
@@ -516,7 +494,7 @@ fn copy_folder_to_target<T: AsRef<path::Path>>(source: T, dest: T) {
|
|||||||
copy_items(&from_paths, dest, ©_opts).unwrap();
|
copy_items(&from_paths, dest, ©_opts).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_folder_contents_to_target<T: AsRef<path::Path>>(source: T, dest: T) {
|
fn copy_folder_contents_to_target<T: AsRef<Path>>(source: T, dest: T) {
|
||||||
let mut copy_opts = dir::CopyOptions::new();
|
let mut copy_opts = dir::CopyOptions::new();
|
||||||
copy_opts.overwrite = true;
|
copy_opts.overwrite = true;
|
||||||
copy_opts.skip_exist = true;
|
copy_opts.skip_exist = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user