use indexmap::IndexMap; use log::{error, info, LevelFilter}; use serde_derive::Deserialize; use simple_logger::SimpleLogger; 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)] struct ChampInfo { name: String, } const USER_AGENT_KEY: &str = "User-Agent"; const USER_AGENT_VALUE: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0"; const LOL_CHAMPS_DIR: &str = ".\\champs"; fn main() { SimpleLogger::new() .with_level(LevelFilter::Info) .with_module_level("ureq", LevelFilter::Error) .init() .unwrap(); info!("CGG Item Sets"); let lol_champs_dir = match lol_champ_dir() { Ok(x) => x, Err(_e) => PathBuf::from(LOL_CHAMPS_DIR), }; info!("LoL Champs Folder: {}", lol_champs_dir.to_str().unwrap()); let client = ureq::AgentBuilder::new() .timeout(Duration::from_secs(10)) .build(); let realm: Realm = client .get("https://ddragon.leagueoflegends.com/realms/euw.json") .set(USER_AGENT_KEY, USER_AGENT_VALUE) .call() .unwrap() .into_json() .unwrap(); info!("LoL version: {}", realm.v); let 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(); info!("LoL numbers of champs: {}", champion.data.len()); let data_sources: [Box; 2] = [ Box::new(PBDataSource), // Box::new(CGGDataSource), Box::new(KBDataSource::new(&client)), ]; for data_source in data_sources.iter() { 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 { if champion.data.contains_key(id) { let path = lol_champs_dir.join(&id).join("Recommended"); fs::create_dir_all(&path).unwrap(); for pos in positions { data_source.write_item_set( &id, &champion.data.get(id).unwrap().name, &pos, &patch, &path, &client, ); thread::sleep(Duration::from_millis(300)); } } else { error!("{} not found in LoL champs", &id); } } } } #[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"); let path: PathBuf; if node.is_ok() { let val: String = node?.get_value("LocalRootFolder")?; path = PathBuf::from(val).parent().unwrap().to_path_buf(); } 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")?; path = PathBuf::from(val); } else { let mut node = hklm .open_subkey(r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall")?; let key = node .enum_keys() .map(|x| x.unwrap()) .find(|x| x.starts_with("League of Legends")); if key == None { return Err(Error::new(ErrorKind::NotFound, "")); } node = node.open_subkey(key.unwrap())?; let val: String = node.get_value("InstallLocation")?; path = PathBuf::from(val); } } Ok(path.join("Config").join("Champions")) } #[cfg(not(target_os = "windows"))] fn lol_champ_dir() -> Result { Ok(PathBuf::from("./champs")) }