fix: wrong path to champ directory #8

Merged
nyyu merged 2 commits from slany/CGGItemSets:fix/champ_path into master 2021-03-15 16:37:26 +01:00

View file

@ -1,7 +1,10 @@
use indexmap::IndexMap; use indexmap::IndexMap;
#[cfg(target_os = "windows")]
use log::debug;
use log::{error, info, LevelFilter}; use log::{error, info, LevelFilter};
use serde_derive::Deserialize; use serde_derive::Deserialize;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use std::env;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use std::io; use std::io;
use std::io::Error; use std::io::Error;
@ -43,25 +46,39 @@ pub struct ChampInfo {
const USER_AGENT_KEY: &str = "User-Agent"; const USER_AGENT_KEY: &str = "User-Agent";
const USER_AGENT_VALUE: &str = const USER_AGENT_VALUE: &str =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0"; "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";
#[cfg(target_os = "windows")]
const REG_KEY_LOL_RADS: &str = r"SOFTWARE\WOW6432Node\Riot Games\RADS";
#[cfg(target_os = "windows")]
const REG_KEY_LOL_INC: &str = r"SOFTWARE\WOW6432Node\Riot Games, Inc\League of Legends";
#[cfg(target_os = "windows")]
const REG_KEY_WIN_64_UNINSTALL: &str =
r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
#[cfg(target_os = "windows")]
const REG_KEY_WIN_UNINSTALL: &str = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\";
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = 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() SimpleLogger::new()
.with_level(LevelFilter::Info) .with_level(level)
.with_module_level("ureq", LevelFilter::Error) .with_module_level("ureq", LevelFilter::Error)
.init()?; .init()?;
info!("CGG Item Sets"); info!("CGG Item Sets");
let lol_champs_dir = match lol_champ_dir() { let lol_champs_dir = match lol_champ_dir() {
Ok(x) => x, Ok(x) => x,
Err(_e) => PathBuf::from(LOL_CHAMPS_DIR), Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR),
}; };
info!( info!(
"LoL Champs Folder: {}", "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() let client = ureq::AgentBuilder::new()
.timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(10))
.build(); .build();
@ -139,55 +156,62 @@ fn get_champ_from_key(champs: &Champion, key: &str) -> Option<String> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn lol_champ_dir() -> Result<PathBuf, Error> { fn lol_champ_dir() -> Result<PathBuf, Error> {
let hklm = RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE); let hklm = RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE);
let path = if let Ok(node) = hklm.open_subkey(REG_KEY_LOL_RADS) {
let node = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Riot Games\RADS"); debug!(
if node.is_ok() { "Use registry key {} for relative champ directory",
let val: String = node?.get_value("LocalRootFolder")?; REG_KEY_LOL_RADS
return Ok(PathBuf::from(val).parent().unwrap().to_path_buf()); );
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 { } else {
let node = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Riot Games, Inc\League of Legends"); return Err(Error::from(ErrorKind::NotFound));
if node.is_ok() { };
let val: String = node?.get_value("Location")?; Ok(PathBuf::from(path).join("Config").join("Champions"))
return Ok(PathBuf::from(val));
}
}
let node = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
let mut sub_node: io::Result<RegKey> = 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))
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
fn lol_champ_dir() -> Result<PathBuf, Error> { fn lol_champ_dir() -> Result<PathBuf, Error> {
Ok(PathBuf::from(LOL_CHAMPS_DIR)) Ok(PathBuf::from(DEFAULT_LOL_CHAMPS_DIR))
}
#[cfg(target_os = "windows")]
fn find_subnode_from_path(reg: RegKey, path: &str, key: &str) -> io::Result<RegKey> {
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))
} }