Find lol dir with reg key

This commit is contained in:
nyyu 2018-06-09 08:31:15 +02:00
parent dba1caabcb
commit 452ff61525
3 changed files with 41 additions and 19 deletions

10
Cargo.lock generated
View File

@ -93,6 +93,7 @@ dependencies = [
"serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
"simple_logger 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winreg 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1183,6 +1184,14 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winreg"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ws2_32-sys"
version = "0.2.1"
@ -1332,4 +1341,5 @@ dependencies = [
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
"checksum winreg 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a27a759395c1195c4cc5cda607ef6f8f6498f64e78f7900f5de0a127a424704a"
"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"

View File

@ -13,3 +13,4 @@ reqwest = "0.8"
select = { git = "https://github.com/utkarshkukreti/select.rs" }
regex = "1.0"
lazy_static = "1.0"
winreg = "0.5"

View File

@ -7,16 +7,18 @@ extern crate simple_logger;
extern crate select;
extern crate regex;
#[macro_use] extern crate lazy_static;
extern crate winreg;
use std::collections::BTreeMap;
use std::{fs, thread, time};
use std::path::{Path, PathBuf};
use std::{io, fs, thread, time};
use std::path::PathBuf;
use reqwest::header::{Headers, UserAgent};
use select::document::Document;
use select::predicate::{Class, Name};
use regex::Regex;
use serde_json::Value;
use time::Duration;
use winreg::RegKey;
#[derive(Deserialize)]
struct Realm {
@ -49,23 +51,23 @@ const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gec
const LOL_CHAMPS_DIR: &str = ".\\champs";
const CONSUMABLES: [u32; 9] = [2003, 2004, 2055, 2031, 2032, 2033, 2138, 2140, 2139];
const TRINKETS: [u32; 3] = [3340, 3364, 3363];
lazy_static! {
static ref ITEM_TYPES: BTreeMap<&'static str, [&'static str; 2]> = {
let mut m = BTreeMap::new();
m.insert("1. Most Frequent Starters", ["firstItems", "mostGames"]);
m.insert("2. Highest Win % Starters", ["firstItems", "highestWinPercent"]);
m.insert("3. Most Frequent Core Build", ["items", "mostGames"]);
m.insert("4. Highest Win % Core Build", ["firstItems", "highestWinPercent"]);
m
};
}
const ITEM_TYPES: &'static [(&str, [&str; 2]); 4] = &[
("Most Frequent Starters", ["firstItems", "mostGames"]),
("Highest Win % Starters", ["firstItems", "highestWinPercent"]),
("Most Frequent Core Build", ["items", "mostGames"]),
("Highest Win % Core Build", ["firstItems", "highestWinPercent"])
];
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)
};
let mut headers = Headers::new();
headers.set(UserAgent::new(USER_AGENT));
@ -87,7 +89,7 @@ fn main() {
for (id, positions) in &champs {
if champion.data.contains_key(id) {
let path = Path::new(LOL_CHAMPS_DIR).join(&id).join("Recommended");
let path = lol_champs_dir.join(&id).join("Recommended");
fs::create_dir_all(&path).unwrap();
for pos in positions {
write_item_set(&id, &pos, &patch, &path, &client);
@ -134,10 +136,12 @@ fn make_item_set(data: &Value, label: &str) -> Value {
})
}
fn make_item_set_from_list(list: Vec<u32>, label: &str, key: &str, data: &Value) -> Value {
fn make_item_set_from_list(list: &Vec<u32>, label: &str, key: &str, data: &Value) -> Value {
json!({
"items": list.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::<Vec<Value>>(),
"type": format!("{} {}", label, data["skills"][key]["order"].as_array().unwrap().iter().map(|x| data["skills"]["skillInfo"].as_array().unwrap()[x.as_str().unwrap().parse::<usize>().unwrap()-1]["key"].as_str().unwrap()).collect::<Vec<&str>>().join("."))
"type": format!("{} {}", label, data["skills"][key]["order"].as_array().unwrap().iter()
.map(|x| data["skills"]["skillInfo"].as_array().unwrap()[x.as_str().unwrap().parse::<usize>().unwrap()-1]["key"].as_str().unwrap())
.collect::<Vec<&str>>().join("."))
})
}
@ -163,8 +167,8 @@ fn write_item_set(id: &str, pos: &str, ver: &str, path: &PathBuf, client: &reqwe
}
}
item_set.blocks.push(make_item_set_from_list(CONSUMABLES.to_vec(), "Consumables | Frequent:", "mostGames", &data));
item_set.blocks.push(make_item_set_from_list(TRINKETS.to_vec(), "Trinkets | Wins:", "highestWinPercent", &data));
item_set.blocks.push(make_item_set_from_list(&CONSUMABLES.to_vec(), "Consumables | Frequent:", "mostGames", &data));
item_set.blocks.push(make_item_set_from_list(&TRINKETS.to_vec(), "Trinkets | Wins:", "highestWinPercent", &data));
info!("Writing item set for {} at {}", id, pos);
fs::write(path.join(format!("CGG_{}_{}.json", id, pos)), serde_json::to_string_pretty(&item_set).unwrap()).unwrap();
@ -181,3 +185,10 @@ fn find_champ_data(text: &str) -> String {
}
RE.captures(text).unwrap()[1].to_string()
}
fn lol_champ_dir() -> Result<PathBuf, io::Error> {
let hklm = RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE);
let node = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Riot Games\RADS")?;
let dir: String = node.get_value("LocalRootFolder")?;
Ok(PathBuf::from(&dir).parent().unwrap().join("Config").join("Champions"))
}