From d812491883ca5b6d30f897e4875e31dc91f55463 Mon Sep 17 00:00:00 2001 From: Piotr Siuszko Date: Sun, 25 Sep 2022 22:00:25 +0200 Subject: [PATCH] Working importing projects --- egui_client/src/hub_client.rs | 34 ++++++++++++++++++++++++++++------ rusty_hub/src/hub.rs | 32 +++++++++++++++++++++++++++++++- rusty_hub/src/unity_project.rs | 10 ++++++++-- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/egui_client/src/hub_client.rs b/egui_client/src/hub_client.rs index b5f0cbf..d1a71fa 100644 --- a/egui_client/src/hub_client.rs +++ b/egui_client/src/hub_client.rs @@ -275,15 +275,37 @@ impl HubClient { body.rows(text_height, 1, |_, mut row| { row.col(|ui| { add_header(ui); - });row.col(|_ui| {}); + }); + row.col(|_ui| {}); row.col(|ui| { ui.vertical_centered_justified(|ui| { ui.add_space(VERTICAL_SPACING); - if ui - .button("Import") - .on_hover_text("Not implemented yet") - .clicked() - {} + if ui.button("Import").clicked() { + let directory = FileDialog::new().pick_folder(); + + if let Some(dir) = directory { + let amount = self.hub.search_for_projects_at_path(&dir); + let mut message = + rfd::MessageDialog::new().set_title("Search ended"); + + match amount { + 0 => { + message = message + .set_description("No new projects found.") + .set_level(rfd::MessageLevel::Warning) + } + 1 => message = message.set_description("Project founded!"), + _ => { + message = message.set_description(&format!( + "Founded {} projects.", + amount + )) + } + } + message.show(); + self.save_config(true); + } + } }); }); }); diff --git a/rusty_hub/src/hub.rs b/rusty_hub/src/hub.rs index 7bc3a2a..aa5125e 100644 --- a/rusty_hub/src/hub.rs +++ b/rusty_hub/src/hub.rs @@ -1,4 +1,6 @@ -use std::process::Command; +use std::{path::PathBuf, process::Command}; + +use walkdir::WalkDir; use crate::{config::Configuration, unity_editor::UnityEditor, unity_project::UnityProject}; @@ -40,6 +42,34 @@ impl Hub { .spawn() .expect("Failed to run project"); } + + pub fn search_for_projects_at_path(&mut self, path: &PathBuf) -> usize { + let path_exists = std::fs::metadata(path).is_ok(); + let mut result = 0; + if !path_exists { + return result; + } + for entry in WalkDir::new(path) + .max_depth(3) + .into_iter() + .filter_entry(|_| true) + { + let projects = self.projects.clone(); + if entry.is_err() { + continue; + } + + let entry_unwraped = entry.unwrap(); + let path_string = entry_unwraped.path().as_os_str().to_str(); + if let Some(project) = UnityProject::try_get_project_at_path(&path_string.unwrap()) { + if !projects.contains(&project) { + self.projects.push(project); + result = result + 1; + } + } + } + result + } } impl Default for Hub { fn default() -> Self { diff --git a/rusty_hub/src/unity_project.rs b/rusty_hub/src/unity_project.rs index 23da4ed..4b03dcf 100644 --- a/rusty_hub/src/unity_project.rs +++ b/rusty_hub/src/unity_project.rs @@ -7,6 +7,12 @@ pub struct UnityProject { pub version: String, } +impl PartialEq for UnityProject { + fn eq(&self, other: &Self) -> bool { + self.path == other.path + } +} + impl UnityProject { #[cfg(not(target_os = "windows"))] pub fn get_projects_from_registry() -> Vec { @@ -37,7 +43,7 @@ impl UnityProject { if let registry::value::Data::Binary(data) = &val.data() { let project_path = str::from_utf8(&data).unwrap().to_string(); - if let Some(result) = UnityProject::get_project_at_path(&project_path) { + if let Some(result) = UnityProject::try_get_project_at_path(&project_path) { projects.push(result); } println!("\t{}: {}", unwraped_name, project_path); @@ -46,7 +52,7 @@ impl UnityProject { projects } - fn get_project_at_path(path: &str) -> Option { + pub fn try_get_project_at_path(path: &str) -> Option { let path = path.trim_matches(char::from(0)).replace("/", "\\"); let second_path = Path::new(&path).join("ProjectSettings"); if std::fs::metadata(&second_path).is_err() {