diff --git a/src/main.rs b/src/main.rs index 987f726..1539fc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ use indexmap::IndexMap; -use log::{error, info, LevelFilter}; +use log::{debug, error, info, LevelFilter}; use serde_derive::Deserialize; use simple_logger::SimpleLogger; +use std::env; #[cfg(target_os = "windows")] use std::io; use std::io::Error; @@ -43,25 +44,35 @@ pub struct ChampInfo { const USER_AGENT_KEY: &str = "User-Agent"; const USER_AGENT_VALUE: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0"; -const LOL_CHAMPS_DIR: &str = ".\\champs"; +const DEFAULT_LOL_CHAMPS_DIR: &str = ".\\champs"; +const REG_KEY_LOL_RADS: &str = r"SOFTWARE\WOW6432Node\Riot Games\RADS"; +const REG_KEY_LOL_INC: &str = r"SOFTWARE\WOW6432Node\Riot Games, Inc\League of Legends"; +const REG_KEY_WIN_64_UNINSTALL: &str = + r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; +const REG_KEY_WIN_UNINSTALL: &str = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"; fn main() -> Result<(), Box> { + let args: Vec = env::args().collect(); + let mut level = LevelFilter::Info; + for s in &args { + if s.eq_ignore_ascii_case("-v") || s.eq_ignore_ascii_case("--verbose") { + level = LevelFilter::Debug; + } + } SimpleLogger::new() - .with_level(LevelFilter::Info) + .with_level(level) .with_module_level("ureq", LevelFilter::Error) .init()?; - info!("CGG Item Sets"); let lol_champs_dir = match lol_champ_dir() { Ok(x) => x, - Err(_e) => PathBuf::from(LOL_CHAMPS_DIR), + Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR), }; info!( "LoL Champs Folder: {}", - lol_champs_dir.to_str().unwrap_or(LOL_CHAMPS_DIR) + lol_champs_dir.to_str().unwrap_or(DEFAULT_LOL_CHAMPS_DIR) ); - let client = ureq::AgentBuilder::new() .timeout(Duration::from_secs(10)) .build(); @@ -139,55 +150,61 @@ fn get_champ_from_key(champs: &Champion, key: &str) -> Option { #[cfg(target_os = "windows")] fn lol_champ_dir() -> Result { let hklm = RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE); - - let node = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Riot Games\RADS"); - if node.is_ok() { - let val: String = node?.get_value("LocalRootFolder")?; - return Ok(PathBuf::from(val).parent().unwrap().to_path_buf()); + let path = if let Ok(node) = hklm.open_subkey(REG_KEY_LOL_RADS) { + debug!( + "Use registry key {} for relative champ directory", + REG_KEY_LOL_RADS + ); + let val: String = node.get_value("LocalRootFolder")?; + // TODO: remplacer ce .unwrap() + PathBuf::from(val).parent().unwrap().to_path_buf() + } else if let Ok(node) = hklm.open_subkey(REG_KEY_LOL_INC) { + debug!( + "Use registry key {} for relative champ directory", + REG_KEY_LOL_INC + ); + let val: String = node.get_value("Location")?; + PathBuf::from(val) + } else if let Ok(node) = + find_subnode_from_path(hklm, REG_KEY_WIN_64_UNINSTALL, "League of Legends") + { + debug!( + "Use registry key {} for relative champ directory", + REG_KEY_WIN_64_UNINSTALL + ); + let val: String = node.get_value("InstallLocation")?; + PathBuf::from(val) + } else if let Ok(node) = find_subnode_from_path( + RegKey::predef(winreg::enums::HKEY_CURRENT_USER), + REG_KEY_WIN_UNINSTALL, + "Riot Game league_of_legends.live", + ) { + debug!( + "Use registry key {} for relative champ directory", + REG_KEY_WIN_UNINSTALL + ); + let val: String = node.get_value("InstallLocation")?; + PathBuf::from(val) } else { - let node = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Riot Games, Inc\League of Legends"); - if node.is_ok() { - let val: String = node?.get_value("Location")?; - return Ok(PathBuf::from(val)); - } - } - - let node = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"); - let mut sub_node: io::Result = Err(Error::from(ErrorKind::NotFound)); - - if let Ok(n) = node { - let key = n - .enum_keys() - .map(|x| x.unwrap()) - .find(|x| x.starts_with("League of Legends")); - if let Some(k) = key { - sub_node = n.open_subkey(k); - } - } - - if sub_node.is_err() { - let hkcu = RegKey::predef(winreg::enums::HKEY_CURRENT_USER); - let node = hkcu.open_subkey(r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"); - if let Ok(n) = node { - let key = n - .enum_keys() - .map(|x| x.unwrap()) - .find(|x| x == "Riot Game league_of_legends.live"); - if let Some(k) = key { - sub_node = n.open_subkey(k); - } - } - } - - if sub_node.is_ok() { - let val: String = sub_node?.get_value("InstallLocation")?; - return Ok(PathBuf::from(val).join("Config").join("Champions")); - } - - Err(Error::from(ErrorKind::NotFound)) + return Err(Error::from(ErrorKind::NotFound)); + }; + Ok(PathBuf::from(path).join("Config").join("Champions")) } #[cfg(not(target_os = "windows"))] fn lol_champ_dir() -> Result { - Ok(PathBuf::from(LOL_CHAMPS_DIR)) + Ok(PathBuf::from(DEFAULT_LOL_CHAMPS_DIR)) +} + +fn find_subnode_from_path(reg: RegKey, path: &str, key: &str) -> io::Result { + if let Ok(node) = reg.open_subkey(path) { + if let Some(k) = node + .enum_keys() + .map(|x| x.unwrap()) + .find(|x| x.starts_with(key)) + { + return node.open_subkey(k); + } + } + Err(Error::from(ErrorKind::NotFound)) }