use indexmap::IndexMap; use lazy_static::lazy_static; #[cfg(target_os = "windows")] use log::debug; use log::{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; #[cfg(target_os = "windows")] use std::io::ErrorKind; use std::path::PathBuf; use std::{fs, thread, time}; use time::Duration; #[cfg(target_os = "windows")] use winreg::RegKey; mod cgg_data_source; mod data_source; mod kb_data_source; mod pb_data_source; use cgg_data_source::CGGDataSource; use data_source::DataSource; use kb_data_source::KBDataSource; use pb_data_source::PBDataSource; #[derive(Deserialize)] struct Realm { v: String, } #[derive(Deserialize)] struct Champion { data: IndexMap, } #[derive(Deserialize)] pub struct ChampInfo { id: String, name: String, key: String, } 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 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> { 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(level) .with_module_level("ureq", LevelFilter::Error) .init()?; info!("CGG Item Sets"); lazy_static! { static ref LOL_CHAMPS_DIR: PathBuf = match lol_champ_dir() { Ok(x) => x, Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR), }; static ref CLIENT: ureq::Agent = ureq::AgentBuilder::new() .timeout(Duration::from_secs(10)) .build(); static ref REALM: Realm = CLIENT .get("https://ddragon.leagueoflegends.com/realms/euw.json") .set(USER_AGENT_KEY, USER_AGENT_VALUE) .call() .unwrap() .into_json() .unwrap(); static ref CHAMPION: Champion = CLIENT .get(&format!( "https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json", REALM.v )) .set(USER_AGENT_KEY, USER_AGENT_VALUE) .call() .unwrap() .into_json() .unwrap(); static ref DATA_SOURCES: [Box; 3] = [ Box::new(PBDataSource), Box::new(CGGDataSource), Box::new(KBDataSource::new(&CLIENT)), ]; } info!( "LoL Champs Folder: {}", LOL_CHAMPS_DIR.to_str().unwrap() ); info!("LoL version: {}", REALM.v); info!("LoL numbers of champs: {}", CHAMPION.data.len()); let mut threads = vec![]; for data_source in DATA_SOURCES.iter() { threads.push(thread::spawn(move || { execute_data_source(&data_source, &CLIENT, &CHAMPION, &LOL_CHAMPS_DIR) })); } for child in threads { let _ = child.join(); } Ok(()) } fn get_champ_from_key(champs: &Champion, key: &str) -> Option { for champ in champs.data.values() { if key == champ.key { return Some(champ.id.to_owned()); } } None } fn execute_data_source( data_source: &Box, client: &ureq::Agent, champion: &Champion, lol_champs_dir: &PathBuf, ) { let (champs, patch) = data_source.get_champs_with_positions_and_patch(&client); info!("{} version: {}", data_source.get_alias(), patch); info!( "{} numbers of champs: {}", data_source.get_alias(), champs.len() ); for (id, positions) in &champs { let mut champ_id: String = id.to_owned(); if !champion.data.contains_key(&champ_id) { if let Some(c_id) = get_champ_from_key(&champion, &champ_id) { champ_id = c_id; } } if let Some(champ) = champion.data.get(&champ_id) { if positions.is_empty() { error!("{} missing positions", &champ_id); } else { let path = lol_champs_dir.join(&champ_id).join("Recommended"); fs::create_dir_all(&path).unwrap(); for pos in positions { data_source.write_item_set(&champ, &pos, &patch, &path, &client); thread::sleep(Duration::from_millis(data_source.get_timeout())); } } } else { error!("{} not found in LoL champs", &champ_id); } } } #[cfg(target_os = "windows")] fn lol_champ_dir() -> Result { let hklm = RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE); 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 { return Err(Error::from(ErrorKind::NotFound)); }; Ok(path.join("Config").join("Champions")) } #[cfg(not(target_os = "windows"))] fn lol_champ_dir() -> Result { Ok(PathBuf::from(DEFAULT_LOL_CHAMPS_DIR)) } #[cfg(target_os = "windows")] 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)) }