From 446951ea0f64966a3efa3bf6d13d8ccf4c2b6ec7 Mon Sep 17 00:00:00 2001 From: endernon Date: Sat, 18 Jan 2025 13:18:55 +0000 Subject: [PATCH] major cleanup --- src/encode.rs | 274 +++++++++++++++++++++++++--------------------- src/gearjson.rs | 2 + src/jsonstruct.rs | 8 -- src/main.rs | 23 ++-- 4 files changed, 162 insertions(+), 145 deletions(-) diff --git a/src/encode.rs b/src/encode.rs index c86b3f5..8e2b83a 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -1,29 +1,156 @@ use crate::errorfr::Errorfr; -use crate::jsonstruct::{CraftedTypesFr, Durability, FuncParams, Identificationer, ItemTypeDeser, PowderFr, RequirementsDeser, Shinyjson, Shinystruct}; +use crate::jsonstruct::{CraftedTypesFr, Durability, Identificationer, ItemTypeDeser, PowderFr, RequirementsDeser, Shinyjson, Shinystruct}; use idmangler_lib::encoding::DataEncoder; use idmangler_lib::{ block::{CraftedConsumableTypeData, CraftedGearTypeData, DurabilityData, EndData, IdentificationData, NameData, PowderData, RequirementsData, RerollData, ShinyData, StartData, TypeData}, types::{ClassType, Element, ItemType, Powder, RollType, SkillType, Stat}, }; use std::collections::HashMap; +use idmangler_lib::types::EncodingVersion; +/// FuncParams struct, used for the three most important parameters for encoding. +/// Also, all the encode functions are stored here, seeing as I require these three params most of the time when encoding. +#[derive(PartialEq, Eq, Hash, Debug)] +pub struct FuncParams<'a> { + pub fr_out: &'a mut Vec, + pub fr_debug_mode: &'a bool, + pub fr_ver: EncodingVersion, +} impl FuncParams<'_> { + /// ENCODE: StartData + /// (REQUIRED) pub fn encode_startdata(&mut self) -> Result<(), Errorfr> { if *self.fr_debug_mode { println!("Encoding StartData") } - // ENCODE: StartData StartData(self.fr_ver).encode(self.fr_ver, self.fr_out).unwrap(); Ok(()) } + /// ENCODE: TypeData + /// (REQUIRED) pub fn encode_typedata(&mut self, item_type_deser: ItemTypeDeser) -> Result<(), Errorfr> { if *self.fr_debug_mode { println!("Encoding TypeData: {:?}", item_type_deser); } - // ENCODE: TypeData TypeData(ItemType::from(item_type_deser)).encode(self.fr_ver, self.fr_out).unwrap(); Ok(()) } + /// ENCODE: NameData + pub fn encode_namedata(&mut self, real_name: &str) -> Result<(), Errorfr> { + if *self.fr_debug_mode { + println!("Encoding NameData: {:?}", &real_name) + } + NameData(real_name.trim().to_string()).encode(self.fr_ver, self.fr_out).unwrap(); + Ok(()) + } + /// ENCODE: IdentificationData + pub fn encode_iddata(&mut self, real_ids: &Vec, idsmap: HashMap) -> Result<(), Errorfr> { + let mut idvec = Vec::new(); + for eachid in real_ids { + let id_id = idsmap.get(eachid.id.trim()); + let id_base = eachid.base; + let id_roll = eachid.roll; + + idvec.push(Stat { + kind: match id_id { + Some(ide) => *ide, + None => std::panic!("There is a mismatched ID, and this message has replaced where the line is meant to be"), + }, + base: Some(id_base), + roll: match id_roll { + Some(rolle) => RollType::Value(rolle), + None => RollType::PreIdentified, + }, + }); + } + if *self.fr_debug_mode { + println!("Encoding IdentificationData: {:?}", &idvec) + } + IdentificationData { + identifications: idvec, + extended_encoding: true, + } + .encode(self.fr_ver, self.fr_out) + .unwrap(); + Ok(()) + } + /// ENCODE: PowderData + pub fn encode_powderdata(&mut self, real_powders: &Vec) -> Result<(), Errorfr> { + let mut powdervec = Vec::new(); + for eachpowder in real_powders { + let powderamount: u8 = eachpowder.amount.unwrap_or(1); + // match for the powder type + for _ in 0..powderamount { + let eletype = match eachpowder.r#type.to_ascii_lowercase() { + 'e' => Element::Earth, + 't' => Element::Thunder, + 'w' => Element::Water, + 'f' => Element::Fire, + 'a' => Element::Air, + _ => return Err(Errorfr::JsonUnknownPowderElement), + }; + if *self.fr_debug_mode { + dbg!(eletype); + } + powdervec.push(Powder::new(eletype, 6).map_err(|_| Errorfr::JsonUnknownPowderTier)?); + // 6 is the tier. Wynntils ONLY really uses tier 6 so theres no point keeping others. + } + } + if *self.fr_debug_mode { + dbg!(&powdervec); + } + + let powderlimitfr: u8 = powdervec.len() as u8; // min of the current number of powders and 255 (if you have over 255 powders stuff breaks) + + PowderData { + powder_slots: powderlimitfr, + powders: powdervec, + } + .encode(self.fr_ver, self.fr_out) + .unwrap(); + Ok(()) + } + /// ENCODE: RerollData + pub fn encode_rerolldata(&mut self, rerollcount: u8) -> Result<(), Errorfr> { + if rerollcount != 0 { + RerollData(rerollcount).encode(self.fr_ver, self.fr_out).unwrap(); + if *self.fr_debug_mode { + dbg!(rerollcount); + } + } + Ok(()) + } + /// ENCODE: ShinyData + pub fn encode_shinydata(&mut self, shiny: &Shinyjson, json_shiny: &Vec) -> Result<(), Errorfr> { + let mut realshinykey: u8; + let _shinykey = &shiny.key; + let shinyvalue = shiny.value; + realshinykey = 1; + for i in json_shiny { + if i.key == shiny.key { + realshinykey = i.id; + if *self.fr_debug_mode { + dbg!(&shiny.key); + } + } + } + if *self.fr_debug_mode { + dbg!(&realshinykey); + dbg!(&shinyvalue); + } + ShinyData { id: realshinykey, val: shinyvalue }.encode(self.fr_ver, self.fr_out).unwrap(); + Ok(()) + } + /// ENCODE: EndData + /// (REQUIRED) + pub fn encode_enddata(&mut self) -> Result<(), Errorfr> { + if *self.fr_debug_mode { + println!("Encoding EndData") + } + EndData.encode(self.fr_ver, self.fr_out).unwrap(); + Ok(()) + } + /// ENCODE: CraftedConsumableTypeData or CraftedGearTypeData based on Type pub fn encode_typedata_custom(&mut self, crafted_type: &str) -> Result<(), Errorfr> { let frfr_type = CraftedTypesFr::try_from(crafted_type)?; match frfr_type { @@ -42,6 +169,18 @@ impl FuncParams<'_> { } Ok(()) } + /// ENCODE: DamageData + /// (CRAFTED ONLY) + pub fn encode_damagedata(&mut self) -> Result<(), Errorfr> { + unimplemented!(); + } + /// ENCODE: DefenseData + /// (CRAFTED ONLY) + pub fn encode_defensedata(&mut self) -> Result<(), Errorfr> { + unimplemented!(); + } + /// ENCODE: DurabilityData + /// (CRAFTED ONLY) pub fn encode_duradata(&mut self, real_dura: &Durability) -> Result<(), Errorfr> { let effect_strength_fr: u8; // but actually it should be 0 to 100, not 0 to 255. But i dunno how to use u7 data type. if let Some(effstr) = real_dura.effect_strength { @@ -98,10 +237,11 @@ impl FuncParams<'_> { current: real_dura.dura_cur, max: real_dura.dura_max, } - .encode(self.fr_ver, self.fr_out) - .unwrap(); + .encode(self.fr_ver, self.fr_out) + .unwrap(); Ok(()) } + /// ENCODE: RequirementsData pub fn encode_reqdata(&mut self, real_reqdata: RequirementsDeser) -> Result<(), Errorfr> { if *self.fr_debug_mode { println!("Encoding RequirementData.Level: {:?}", real_reqdata.level.clone()) @@ -128,129 +268,9 @@ impl FuncParams<'_> { class: fr_class, skills: spvec, } - .encode(self.fr_ver, self.fr_out) - .unwrap(); + .encode(self.fr_ver, self.fr_out) + .unwrap(); Ok(()) } - pub fn encode_namedata(&mut self, real_name: &str) -> Result<(), Errorfr> { - // ENCODE: NameData - if *self.fr_debug_mode { - println!("Encoding NameData: {:?}", &real_name) - } - NameData(real_name.trim().to_string()).encode(self.fr_ver, self.fr_out).unwrap(); - Ok(()) - } - pub fn encode_iddata(&mut self, real_ids: &Vec, idsmap: HashMap) -> Result<(), Errorfr> { - let mut idvec = Vec::new(); - for eachid in real_ids { - let id_id = idsmap.get(eachid.id.trim()); - let id_base = eachid.base; - let id_roll = eachid.roll; - idvec.push(Stat { - kind: match id_id { - Some(ide) => *ide, - None => std::panic!("There is a mismatched ID, and this message has replaced where the line is meant to be"), - }, - base: Some(id_base), - roll: match id_roll { - Some(rolle) => RollType::Value(rolle), - None => RollType::PreIdentified, - }, - }); - - // println!("{:?} {:?} {:?}",id_id,id_base,id_roll) - } - if *self.fr_debug_mode { - println!("Encoding IdentificationData: {:?}", &idvec) - } - // ENCODE: IdentificationsData - IdentificationData { - identifications: idvec, - extended_encoding: true, - } - .encode(self.fr_ver, self.fr_out) - .unwrap(); - Ok(()) - } - pub fn encode_powderdata(&mut self, real_powders: &Vec) -> Result<(), Errorfr> { - let mut powdervec = Vec::new(); - for eachpowder in real_powders { - let powderamount: u8 = eachpowder.amount.unwrap_or(1); - // match for the powder type - for _ in 0..powderamount { - let eletype = match eachpowder.r#type.to_ascii_lowercase() { - 'e' => Element::Earth, - 't' => Element::Thunder, - 'w' => Element::Water, - 'f' => Element::Fire, - 'a' => Element::Air, - _ => return Err(Errorfr::JsonUnknownPowderElement), - }; - if *self.fr_debug_mode { - dbg!(eletype); - } - powdervec.push(Powder::new(eletype, 6).map_err(|_| Errorfr::JsonUnknownPowderTier)?); - // 6 is the tier. Wynntils ONLY really uses tier 6 so theres no point keeping others. - } - } - if *self.fr_debug_mode { - dbg!(&powdervec); - } - - let powderlimitfr: u8 = powdervec.len() as u8; // min of the current number of powders and 255 (if you have over 255 powders stuff breaks) - - // ENCODE: PowderData - // only occurs if the powders array is present and the powder limit is also present - // - PowderData { - powder_slots: powderlimitfr, - powders: powdervec, - } - .encode(self.fr_ver, self.fr_out) - .unwrap(); - Ok(()) - } - pub fn encode_rerolldata(&mut self, rerollcount: u8) -> Result<(), Errorfr> { - if rerollcount != 0 { - // ENCODE: RerollData if applicable - RerollData(rerollcount).encode(self.fr_ver, self.fr_out).unwrap(); - if *self.fr_debug_mode { - dbg!(rerollcount); - } - } - Ok(()) - } - pub fn encode_shinydata(&mut self, shiny: &Shinyjson, json_shiny: &Vec) -> Result<(), Errorfr> { - let mut realshinykey: u8; - let _shinykey = &shiny.key; - let shinyvalue = shiny.value; - realshinykey = 1; - for i in json_shiny { - if i.key == shiny.key { - realshinykey = i.id; - if *self.fr_debug_mode { - dbg!(&shiny.key); - } - } - } - if *self.fr_debug_mode { - dbg!(&realshinykey); - dbg!(&shinyvalue); - } - // ENCODE: ShinyData (if applicable) - ShinyData { id: realshinykey, val: shinyvalue }.encode(self.fr_ver, self.fr_out).unwrap(); - Ok(()) - } - pub fn encode_enddata(&mut self) -> Result<(), Errorfr> { - if *self.fr_debug_mode { - println!("Encoding EndData") - } - // ENCODE: EndData - EndData.encode(self.fr_ver, self.fr_out).unwrap(); - Ok(()) - } - pub fn encode_damagedata(&mut self) -> Result<(), Errorfr> { - unimplemented!(); - } } diff --git a/src/gearjson.rs b/src/gearjson.rs index 2d5aaac..b6e1179 100644 --- a/src/gearjson.rs +++ b/src/gearjson.rs @@ -25,6 +25,8 @@ pub struct GearJsonItemInnerStruct { raw: i32 } +/// Function to generate a perfect item. +/// It returns Result\, Errorfr\> pub fn gen_perfect(name: &str, frjson: &HashMap) -> Result, Errorfr> { let mut a: Vec = Vec::new(); let name = name.to_lowercase(); diff --git a/src/jsonstruct.rs b/src/jsonstruct.rs index f0b31fd..be48e81 100644 --- a/src/jsonstruct.rs +++ b/src/jsonstruct.rs @@ -203,14 +203,6 @@ pub struct Shinyjson { pub value: i64, } -// struct for general parameters for most encode functions -#[derive(PartialEq, Eq, Hash, Debug)] -pub struct FuncParams<'a> { - pub fr_out: &'a mut Vec, - pub fr_debug_mode: &'a bool, - pub fr_ver: EncodingVersion, -} - #[derive(Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone)] pub struct DamageDeser { pub attack_speed: AttackSpeed, diff --git a/src/main.rs b/src/main.rs index 5e243fc..ac9493b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,15 @@ mod gearjson; mod jsondl; mod jsonstruct; use crate::errorfr::Errorfr; +use crate::encode::FuncParams; use crate::jsondl::*; use crate::jsonstruct::*; +use crate::gearjson::gen_perfect; use clap::Parser; use idmangler_lib::{encoding::string::encode_string, types::EncodingVersion}; use reqwest::Url; use std::{collections::HashMap, env, fs, io::Write, path::PathBuf}; -use crate::gearjson::gen_perfect; + #[derive(Parser, Debug, Clone)] #[command(version, about, long_about = None, arg_required_else_help(true))] @@ -122,11 +124,16 @@ fn main_2() -> Result<(), Errorfr> { let ver = EncodingVersion::Version1; let mut loaded_config_clone = loaded_config.clone(); - + + let mut funcparamsfr: FuncParams = FuncParams { + fr_out: &mut out, + fr_debug_mode: &debug_mode, + fr_ver: ver, + }; // ENCODE: A Lot Of Stuff // Also print any mapped errors - let cooking = cook(&mut out, &debug_mode, ver, &mut loaded_config_clone, loaded_idkeys, loaded_shinystats, namefr, executable_path); + let cooking = cook(&mut funcparamsfr, &mut loaded_config_clone, loaded_idkeys, loaded_shinystats, namefr, executable_path); if let Err(e) = cooking { println!("{}", e); // print error if there is an error } else { @@ -137,12 +144,8 @@ fn main_2() -> Result<(), Errorfr> { Ok(()) } -fn cook(out: &mut Vec, debug_mode: &bool, ver: EncodingVersion, json_config: &mut Jsonconfig, idsmap: HashMap, json_shiny: Vec, namefr: String, executable_path: &str) -> Result { - let mut fr_params = FuncParams { - fr_out: out, - fr_debug_mode: debug_mode, - fr_ver: ver, - }; +fn cook(fr_params: &mut FuncParams, json_config: &mut Jsonconfig, idsmap: HashMap, json_shiny: Vec, namefr: String, executable_path: &str) -> Result { + // ENCODE: StartData and TypeData, ALWAYS fr_params.encode_startdata()?; @@ -249,7 +252,7 @@ fn cook(out: &mut Vec, debug_mode: &bool, ver: EncodingVersion, json_config: // ENCODE: EndData, ALWAYS fr_params.encode_enddata()?; - let mut final_string: String = encode_string(out); + let mut final_string: String = encode_string(fr_params.fr_out); // add NameAfter. not actually an encode. match json_config.item_type {