diff --git a/src/errorfr.rs b/src/errorfr.rs index d82b49e..cdcf8e0 100644 --- a/src/errorfr.rs +++ b/src/errorfr.rs @@ -31,10 +31,15 @@ pub enum Errorfr { #[error("Error 2.5: gear.json is missing. It's only required for the \"perfect mode\" item gen. \nYou should run \"--download Gear\" or \"--download All\".")] GearJsonMissing, - /// shiny data json is corrupt + /// gear data json is corrupt #[error("Error 2.6: gear.json is corrupt. It's only required for the \"perfect mode\" item gen. \nYou should run \"--download Gear\" or \"--download All\".\n{0:?}")] GearJsonCorrupt(serde_json5::Error), + /// gear data json cache could not be created + #[error("Error 2.7: unable to create file gear_cache.json.")] + GearJsonCacheCreateFail, + + /// could not download the file #[error("Error 3.1: Download request failed. Check your network settings.")] JsonDlReqFail, diff --git a/src/gearjson.rs b/src/gearjson.rs index 97071d5..9ab2db9 100644 --- a/src/gearjson.rs +++ b/src/gearjson.rs @@ -1,8 +1,8 @@ -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::collections::HashMap; // the struct for the stuff I need in in Hashmap gear.json. its a big ass pain -#[derive(Deserialize, PartialEq, Eq, Debug)] +#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)] pub struct GearJsonItem { identifications: Option< HashMap< @@ -10,15 +10,15 @@ pub struct GearJsonItem { > > } -#[derive(Deserialize, PartialEq, Eq, Debug)] +#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)] #[serde(untagged)] pub enum GearJsonItemInner { r#Struct(GearJsonItemInnerStruct), r#Int(i32) } -#[derive(Deserialize, PartialEq, Eq, Debug)] +#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)] pub struct GearJsonItemInnerStruct { - max: i16, - min: i16, - raw: i16 + max: i32, + min: i32, + raw: i32 } \ No newline at end of file diff --git a/src/jsondl.rs b/src/jsondl.rs index ef11deb..84aaf08 100644 --- a/src/jsondl.rs +++ b/src/jsondl.rs @@ -4,7 +4,8 @@ use crate::gearjson; use crate::jsonstruct::Shinystruct; use serde::Deserialize; use std::collections::HashMap; -use std::fs; +use std::{fs, io}; +use std::io::Write; pub fn load_idkeys(executable_path: &str) -> Result, Errorfr> { // id_keys.json @@ -20,14 +21,21 @@ pub fn load_shinystats(executable_path: &str) -> Result, Errorf .map_err(|_| Errorfr::ShinyJsonMissing)?) .map_err(|_| Errorfr::ShinyJsonCorrupt) } - pub fn load_gear(executable_path: &str) -> Result, Errorfr> { - // gear.json (ONLY FOR PERFECT ITEM FUNCTION GEN) + // gear.json (ONLY FOR DL gear.json) serde_json5::from_reader( &mut fs::File::open(executable_path.to_owned() + "/data/gear.json") .map_err(|_| Errorfr::GearJsonMissing)?) .map_err(Errorfr::GearJsonCorrupt) } +pub fn load_gear_cache(executable_path: &str) -> Result, Errorfr> { + // gear_cache.json (ONLY FOR PERFECT ITEM FUNCTION GEN) + serde_json5::from_reader( + &mut fs::File::open(executable_path.to_owned() + "/data/gear_cache.json") + .map_err(|_| Errorfr::GearJsonMissing)?) + .map_err(Errorfr::GearJsonCorrupt) +} + pub fn dl_json_fr(dlvalue: &String, executable_path: &str) { let jsons = DownloadJsons::from(dlvalue.clone()); if let Err(e) = fs::create_dir_all(format!("{}{}", executable_path, "/data/")) { @@ -53,12 +61,23 @@ pub fn dl_json_fr(dlvalue: &String, executable_path: &str) { } } if jsons == DownloadJsons::All || jsons == DownloadJsons::Gear { - if let Err(e) = dl_json( + match dl_json( "https://raw.githubusercontent.com/Wynntils/Static-Storage/main/Reference/gear.json".parse().unwrap(), format!("{}{}", executable_path, "/data/gear.json"), ) { - // error handling below - println!("{} Filename: {}", e, dlvalue) + Err(e) => { + // error handling below + println!("{} Filename: {}", e, dlvalue); + }, + Ok(t) => { + println!("Now generating gear_cache.json (otherwise, when running --perfect it will take ages each time!)"); + let frfrnocap = serde_json::to_vec( + &load_gear(executable_path) + .unwrap() + ).unwrap(); + let mut outer = fs::File::create(format!("{}{}",executable_path, "/data/gear.json")).map_err(|_| Errorfr::GearJsonCacheCreateFail).unwrap(); + outer.write_all(&frfrnocap).unwrap(); + } } } } diff --git a/src/main.rs b/src/main.rs index c6abdac..374fff7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -156,7 +156,7 @@ fn cook(out: &mut Vec, debug_mode: &bool, ver: EncodingVersion, json_config: ItemTypeDeser::Gear | ItemTypeDeser::Tome | ItemTypeDeser::Charm => { if namefr != *"" { println!("cool tree"); - let fr_gear = load_gear(executable_path)?; + let fr_gear = load_gear_cache(executable_path)?; } else if let Some(real_ids) = &json_config.ids { fr_params.encode_iddata(real_ids, idsmap)?