use indexmap::IndexMap; use log::{error, info}; use reqwest::header; use serde_derive::Deserialize; use simple_logger; use std::io::{Error, ErrorKind}; use std::path::PathBuf; use std::{fs, thread, time}; use time::Duration; 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: &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() { simple_logger::init_with_level(log::Level::Info).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 mut headers = header::HeaderMap::new(); headers.insert( header::USER_AGENT, header::HeaderValue::from_static(USER_AGENT), ); let client = reqwest::Client::builder() .default_headers(headers) .timeout(Duration::from_secs(10)) .build() .unwrap(); let realm: Realm = client .get("https://ddragon.leagueoflegends.com/realms/euw.json") .send() .unwrap() .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 )) .send() .unwrap() .json() .unwrap(); info!("LoL numbers of champs: {}", champion.data.len()); let data_sources: [Box; 3] = [ 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); } } } } 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")) }