major cleanup

This commit is contained in:
endernon 2025-01-18 13:18:55 +00:00
parent 03b87c64f6
commit 446951ea0f
4 changed files with 162 additions and 145 deletions

View file

@ -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<u8>,
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<Identificationer>, idsmap: HashMap<String, u8>) -> 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<PowderFr>) -> 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<Shinystruct>) -> 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<Identificationer>, idsmap: HashMap<String, u8>) -> 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<PowderFr>) -> 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<Shinystruct>) -> 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!();
}
}

View file

@ -25,6 +25,8 @@ pub struct GearJsonItemInnerStruct {
raw: i32
}
/// Function to generate a perfect item.
/// It returns Result\<Vec\<Identificationer\>, Errorfr\>
pub fn gen_perfect(name: &str, frjson: &HashMap<String, GearJsonItem>) -> Result<Vec<Identificationer>, Errorfr> {
let mut a: Vec<Identificationer> = Vec::new();
let name = name.to_lowercase();

View file

@ -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<u8>,
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,

View file

@ -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))]
@ -123,10 +125,15 @@ fn main_2() -> Result<(), Errorfr> {
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<u8>, debug_mode: &bool, ver: EncodingVersion, json_config: &mut Jsonconfig, idsmap: HashMap<String, u8>, json_shiny: Vec<Shinystruct>, namefr: String, executable_path: &str) -> Result<String, Errorfr> {
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<String, u8>, json_shiny: Vec<Shinystruct>, namefr: String, executable_path: &str) -> Result<String, Errorfr> {
// ENCODE: StartData and TypeData, ALWAYS
fr_params.encode_startdata()?;
@ -249,7 +252,7 @@ fn cook(out: &mut Vec<u8>, 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 {