diff --git a/rusty_hub/Cargo.lock b/rusty_hub/Cargo.lock index 4d379fb..b15ad7a 100644 --- a/rusty_hub/Cargo.lock +++ b/rusty_hub/Cargo.lock @@ -310,12 +310,26 @@ dependencies = [ "thiserror", ] +[[package]] +name = "registry" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83e4b158bf49b0d000013487636c92268de4cfd26cdbb629f020a612749f12c4" +dependencies = [ + "bitflags", + "log", + "thiserror", + "utfx", + "winapi", +] + [[package]] name = "rusty_hub" version = "0.1.0" dependencies = [ "confy", "exe", + "registry", "serde", "serde_derive", "walkdir", @@ -436,6 +450,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" +[[package]] +name = "utfx" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "133bf74f01486773317ddfcde8e2e20d2933cc3b68ab797e5d718bef996a81de" + [[package]] name = "version_check" version = "0.9.4" diff --git a/rusty_hub/Cargo.toml b/rusty_hub/Cargo.toml index e67c0b8..abda01b 100644 --- a/rusty_hub/Cargo.toml +++ b/rusty_hub/Cargo.toml @@ -18,4 +18,5 @@ confy = "^0.5.0" serde = "^1.0" serde_derive = "^1.0" walkdir = "^2.3.2" -exe = "^0.5.4" \ No newline at end of file +exe = "^0.5.4" +registry = "1.2.2" \ No newline at end of file diff --git a/rusty_hub/src/config.rs b/rusty_hub/src/config.rs index 07c4b32..9a7428b 100644 --- a/rusty_hub/src/config.rs +++ b/rusty_hub/src/config.rs @@ -1,11 +1,24 @@ use walkdir::{DirEntry, WalkDir}; +use crate::unity_editor::UnityEditor; + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Configuration { pub unity_search_paths: Vec, + pub editors_configurations: Vec, } impl Configuration { + pub fn rebuild(&mut self) { + self.editors_configurations = Vec::new(); + let paths = self.get_unity_paths(); + for path in &paths { + let editor = UnityEditor::new(&path); + if editor.is_some() { + self.editors_configurations.push(editor.unwrap()); + } + } + } pub fn get_unity_paths(&self) -> Vec { let mut paths = Vec::new(); @@ -56,8 +69,12 @@ impl Configuration { impl Default for Configuration { fn default() -> Self { - Self { + let mut default = Self { unity_search_paths: vec!["C:\\Program Files\\Unity\\Hub".to_string()], - } + editors_configurations: Vec::new(), + }; + default.rebuild(); + + default } } diff --git a/rusty_hub/src/main.rs b/rusty_hub/src/main.rs index e19373c..8ba45a1 100644 --- a/rusty_hub/src/main.rs +++ b/rusty_hub/src/main.rs @@ -1,5 +1,3 @@ -use unity_editor::UnityEditor; - use crate::config::Configuration; #[macro_use] extern crate serde_derive; @@ -7,14 +5,11 @@ extern crate confy; mod config; mod unity_editor; +mod unity_project; fn main() { let config = Configuration::default(); - let paths = config.get_unity_paths(); - for path in &paths { - let editor = UnityEditor::new(&path); - if editor.is_some() { - println!("{:#?}", editor.unwrap()); - } - } + println!("{:#?}", config.editors_configurations); + let projects = unity_project::UnityProject::get_projects_from_registry(); + println!("{:#?}", projects); } diff --git a/rusty_hub/src/unity_editor.rs b/rusty_hub/src/unity_editor.rs index d8c642e..d325f19 100644 --- a/rusty_hub/src/unity_editor.rs +++ b/rusty_hub/src/unity_editor.rs @@ -80,7 +80,9 @@ impl UnityEditor { } let f = path.unwrap(); if let Ok(result_string) = f.file_name().into_string() { - if let Some(value) = platform_names.get(&result_string.clone().to_lowercase().borrow()) { + if let Some(value) = + platform_names.get(&result_string.clone().to_lowercase().borrow()) + { platforms.push(value.to_string()); } else { platforms.push(result_string); diff --git a/rusty_hub/src/unity_project.rs b/rusty_hub/src/unity_project.rs new file mode 100644 index 0000000..9505f84 --- /dev/null +++ b/rusty_hub/src/unity_project.rs @@ -0,0 +1,71 @@ +use registry::{Hive, Security}; +use std::{path::Path, str}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct UnityProject { + pub path: String, + pub title: String, + pub version: String, +} + +impl UnityProject { + pub fn get_projects_from_registry() -> Vec { + let mut projects = Vec::new(); + + let key = Hive::CurrentUser + .open( + r"SOFTWARE\Unity Technologies\Unity Editor 5.x", + Security::Read, + ) + .unwrap(); + println!("{}", key.to_string()); + + for value in key.values() { + if value.is_err() { + continue; + } + let val = value.unwrap(); + let unwraped_name = val.name().to_string().unwrap(); + if !unwraped_name.contains("RecentlyUsedProjectPaths-") { + continue; + } + + 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) { + projects.push(result); + } + println!("\t{}: {}", unwraped_name, project_path); + } + } + projects + } + + fn get_project_at_path(path: &str) -> Option { + let path = path.trim_matches(char::from(0)); + let second_path = Path::new(&path.replace("/", "\\")).join("ProjectSettings"); + if std::fs::metadata(&second_path).is_err() { + println!( + "DUPA: {:#?}, {:#?}", + second_path.display(), + std::fs::metadata(&second_path).err() + ); + return None; + } + let project_version_file = std::fs::read_to_string(second_path.join("ProjectVersion.txt")); + if project_version_file.is_err() { + println!("DUPA2"); + return None; + } + let project_version_file = project_version_file.unwrap(); + let mut iter = project_version_file.split_whitespace(); + iter.next(); + let project_version = iter.next().unwrap().to_string(); + + Some(UnityProject { + path: path.to_string(), + title: path.to_string(), + version: project_version, + }) + } +}