CGGItemSets/src/main.rs

152 lines
4.3 KiB
Rust
Raw Normal View History

2018-06-09 09:07:41 +02:00
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate log;
2018-06-04 22:51:18 +02:00
extern crate reqwest;
extern crate serde;
2018-06-09 09:07:41 +02:00
#[macro_use]
extern crate serde_json;
2018-06-08 22:41:01 +02:00
extern crate regex;
2018-06-09 09:07:41 +02:00
extern crate select;
extern crate simple_logger;
#[macro_use]
extern crate lazy_static;
2018-06-09 12:35:47 +02:00
extern crate indexmap;
2018-06-09 08:31:15 +02:00
extern crate winreg;
2018-06-04 22:51:18 +02:00
2018-06-16 10:48:46 +02:00
mod cgg_data_source;
mod data_source;
2018-06-16 18:13:24 +02:00
mod pb_data_source;
2018-06-16 10:48:46 +02:00
use cgg_data_source::CGGDataSource;
use data_source::DataSource;
2018-06-09 12:35:47 +02:00
use indexmap::IndexMap;
2018-06-16 18:13:24 +02:00
use pb_data_source::PBDataSource;
2019-04-21 22:25:22 +02:00
use reqwest::header;
2018-06-10 15:02:08 +02:00
use std::io::{Error, ErrorKind};
2018-06-09 09:07:41 +02:00
use std::path::PathBuf;
2018-06-10 15:02:08 +02:00
use std::{fs, thread, time};
2018-06-09 00:06:30 +02:00
use time::Duration;
2018-06-09 08:31:15 +02:00
use winreg::RegKey;
2018-06-04 22:51:18 +02:00
#[derive(Deserialize)]
struct Realm {
2018-06-09 09:07:41 +02:00
v: String,
2018-06-04 22:51:18 +02:00
}
#[derive(Deserialize)]
struct Champion {
2018-06-09 12:35:47 +02:00
data: IndexMap<String, ChampInfo>,
2018-06-04 22:51:18 +02:00
}
#[derive(Deserialize)]
struct ChampInfo {
2018-06-09 11:46:02 +02:00
name: String,
}
2018-06-09 09:07:41 +02:00
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";
2018-06-08 22:41:01 +02:00
2018-06-04 22:51:18 +02:00
fn main() {
simple_logger::init_with_level(log::Level::Info).unwrap();
info!("CGG Item Sets");
2018-06-09 08:31:15 +02:00
let lol_champs_dir = match lol_champ_dir() {
Ok(x) => x,
2018-06-09 09:07:41 +02:00
Err(_e) => PathBuf::from(LOL_CHAMPS_DIR),
2018-06-09 08:31:15 +02:00
};
2018-06-10 12:42:02 +02:00
info!("LoL Champs Folder: {}", lol_champs_dir.to_str().unwrap());
2018-06-09 08:31:15 +02:00
2019-04-21 22:25:22 +02:00
let mut headers = header::HeaderMap::new();
headers.insert(header::USER_AGENT, header::HeaderValue::from_static(USER_AGENT));
2018-06-04 22:51:18 +02:00
2018-06-09 00:06:30 +02:00
let client = reqwest::Client::builder()
.default_headers(headers)
.timeout(Duration::from_secs(10))
2018-06-09 09:07:41 +02:00
.build()
.unwrap();
2018-06-04 22:51:18 +02:00
2018-06-09 09:07:41 +02:00
let realm: Realm = client
.get("https://ddragon.leagueoflegends.com/realms/euw.json")
.send()
.unwrap()
.json()
.unwrap();
2018-06-04 22:51:18 +02:00
info!("LoL version: {}", realm.v);
2018-06-09 09:07:41 +02:00
let champion: Champion = client
.get(&format!(
"https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json",
realm.v
))
.send()
.unwrap()
.json()
.unwrap();
2018-06-04 22:51:18 +02:00
info!("LoL numbers of champs: {}", champion.data.len());
2018-06-16 18:13:24 +02:00
let data_sources: [Box<DataSource>; 2] = [Box::new(PBDataSource), Box::new(CGGDataSource)];
2018-06-16 12:48:14 +02:00
for data_source in data_sources.iter() {
let (champs, patch) = data_source.get_champs_with_positions_and_patch(&client);
info!("CGG version: {}", patch);
info!("CGG numbers of champs: {}", 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);
}
}
}
}
2018-06-10 15:02:08 +02:00
fn lol_champ_dir() -> Result<PathBuf, Error> {
2018-06-09 08:31:15 +02:00
let hklm = RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE);
2018-06-10 12:42:02 +02:00
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");
2018-06-10 15:02:08 +02:00
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);
}
2018-06-10 12:42:02 +02:00
}
Ok(path.join("Config").join("Champions"))
2018-06-09 08:31:15 +02:00
}