Working importing projects

This commit is contained in:
Piotr Siuszko 2022-09-25 22:00:25 +02:00
parent 034f05eeba
commit d812491883
3 changed files with 67 additions and 9 deletions

View File

@ -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);
}
}
});
});
});

View File

@ -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 {

View File

@ -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<UnityProject> {
@ -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<UnityProject> {
pub fn try_get_project_at_path(path: &str) -> Option<UnityProject> {
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() {