Move all encode functions into an impl for FuncParams

This commit is contained in:
endernon 2025-01-11 00:43:10 +00:00
parent bde76d6548
commit 3d00b8306a
2 changed files with 260 additions and 259 deletions

View file

@ -14,52 +14,53 @@ use idmangler_lib::{
use idmangler_lib::encoding::DataEncoder; use idmangler_lib::encoding::DataEncoder;
use std::collections::HashMap; use std::collections::HashMap;
pub fn encode_startdata(general_params: &mut FuncParams) -> Result<(), Errorfr> { impl FuncParams<'_> {
if *general_params.fr_debug_mode { pub fn encode_startdata(&mut self) -> Result<(), Errorfr> {
if *self.fr_debug_mode {
println!("Encoding StartData") println!("Encoding StartData")
} }
// ENCODE: StartData // ENCODE: StartData
StartData(general_params.fr_ver) StartData(self.fr_ver)
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
pub fn encode_typedata(general_params: &mut FuncParams, item_type_deser: ItemTypeDeser) -> Result<(), Errorfr> { pub fn encode_typedata(&mut self, item_type_deser: ItemTypeDeser) -> Result<(), Errorfr> {
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!("Encoding TypeData: {:?}", item_type_deser); println!("Encoding TypeData: {:?}", item_type_deser);
} }
// ENCODE: TypeData // ENCODE: TypeData
TypeData(ItemType::from(item_type_deser)) TypeData(ItemType::from(item_type_deser))
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
pub fn encode_typedata_custom(general_params: &mut FuncParams, crafted_type: &str) -> Result<(), Errorfr> { pub fn encode_typedata_custom(&mut self, crafted_type: &str) -> Result<(), Errorfr> {
let frfr_type = CraftedTypesFr::try_from(crafted_type)?; let frfr_type = CraftedTypesFr::try_from(crafted_type)?;
match frfr_type { match frfr_type {
CraftedTypesFr::Gear(a) => { CraftedTypesFr::Gear(a) => {
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!("Encoding CustomTypeData: Gear"); println!("Encoding CustomTypeData: Gear");
} }
CraftedGearTypeData(a) CraftedGearTypeData(a)
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap() .unwrap()
} }
CraftedTypesFr::Consu(a) => { CraftedTypesFr::Consu(a) => {
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!("Encoding CustomTypeData: Consumable"); println!("Encoding CustomTypeData: Consumable");
} }
CraftedConsumableTypeData(a) CraftedConsumableTypeData(a)
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap() .unwrap()
} }
} }
Ok(()) Ok(())
} }
pub fn encode_duradata(general_params: &mut FuncParams, real_dura: &Durability) -> Result<(), Errorfr> { 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. 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 { if let Some(effstr) = real_dura.effect_strength {
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!("Encoding DurabilityData: Defined"); println!("Encoding DurabilityData: Defined");
} }
effect_strength_fr = effstr effect_strength_fr = effstr
@ -94,7 +95,7 @@ pub fn encode_duradata(general_params: &mut FuncParams, real_dura: &Durability)
return Err(Errorfr::JsonDuraOutOfRange); return Err(Errorfr::JsonDuraOutOfRange);
} }
} }
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!( println!(
"Encoding DurabilityData.effect_strenght: {}", "Encoding DurabilityData.effect_strenght: {}",
effect_strength_fr effect_strength_fr
@ -107,12 +108,12 @@ pub fn encode_duradata(general_params: &mut FuncParams, real_dura: &Durability)
current: real_dura.dura_cur, current: real_dura.dura_cur,
max: real_dura.dura_max, max: real_dura.dura_max,
} }
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
pub fn encode_reqdata(general_params: &mut FuncParams, real_reqdata: RequirementsDeser) -> Result<(), Errorfr> { pub fn encode_reqdata(&mut self, real_reqdata: RequirementsDeser) -> Result<(), Errorfr> {
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!( println!(
"Encoding RequirementData.Level: {:?}", "Encoding RequirementData.Level: {:?}",
real_reqdata.level.clone() real_reqdata.level.clone()
@ -121,10 +122,10 @@ pub fn encode_reqdata(general_params: &mut FuncParams, real_reqdata: Requirement
let mut fr_class: Option<ClassType> = None; let mut fr_class: Option<ClassType> = None;
if let Some(actualclass) = real_reqdata.class { if let Some(actualclass) = real_reqdata.class {
fr_class = Some(ClassType::from(actualclass)); fr_class = Some(ClassType::from(actualclass));
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!("Encoding RequirementData.Class: {:?}", actualclass.clone()) println!("Encoding RequirementData.Class: {:?}", actualclass.clone())
} }
} else if *general_params.fr_debug_mode { } else if *self.fr_debug_mode {
println!("Encoding RequirementData.Class: Undefined"); println!("Encoding RequirementData.Class: Undefined");
} }
@ -132,7 +133,7 @@ pub fn encode_reqdata(general_params: &mut FuncParams, real_reqdata: Requirement
Some(real_sp) => {Vec::<(SkillType, i32)>::from(real_sp)}, Some(real_sp) => {Vec::<(SkillType, i32)>::from(real_sp)},
None => {Vec::new()} None => {Vec::new()}
}; };
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!("Encoding RequirementData.Skills: {:?}", spvec.clone()) println!("Encoding RequirementData.Skills: {:?}", spvec.clone())
} }
RequirementsData { RequirementsData {
@ -140,21 +141,21 @@ pub fn encode_reqdata(general_params: &mut FuncParams, real_reqdata: Requirement
class: fr_class, class: fr_class,
skills: spvec, skills: spvec,
} }
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
pub fn encode_namedata(general_params: &mut FuncParams, real_name: &str) -> Result<(), Errorfr> { pub fn encode_namedata(&mut self, real_name: &str) -> Result<(), Errorfr> {
// ENCODE: NameData // ENCODE: NameData
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!("Encoding NameData: {:?}", &real_name) println!("Encoding NameData: {:?}", &real_name)
} }
NameData(real_name.trim().to_string()) NameData(real_name.trim().to_string())
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
pub fn encode_iddata(general_params: &mut FuncParams, real_ids: &Vec<Identificationer>, idsmap: HashMap<String, u8>) -> Result<(), Errorfr> { pub fn encode_iddata(&mut self, real_ids: &Vec<Identificationer>, idsmap: HashMap<String, u8>) -> Result<(), Errorfr> {
let mut idvec = Vec::new(); let mut idvec = Vec::new();
for eachid in real_ids { for eachid in real_ids {
let id_id = idsmap.get(eachid.id.trim()); let id_id = idsmap.get(eachid.id.trim());
@ -177,7 +178,7 @@ pub fn encode_iddata(general_params: &mut FuncParams, real_ids: &Vec<Identificat
// println!("{:?} {:?} {:?}",id_id,id_base,id_roll) // println!("{:?} {:?} {:?}",id_id,id_base,id_roll)
} }
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!("Encoding IdentificationData: {:?}", &idvec) println!("Encoding IdentificationData: {:?}", &idvec)
} }
// ENCODE: IdentificationsData // ENCODE: IdentificationsData
@ -185,11 +186,11 @@ pub fn encode_iddata(general_params: &mut FuncParams, real_ids: &Vec<Identificat
identifications: idvec, identifications: idvec,
extended_encoding: true, extended_encoding: true,
} }
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
pub fn encode_powderdata(general_params: &mut FuncParams, real_powders: &Vec<PowderFr>) -> Result<(), Errorfr> { pub fn encode_powderdata(&mut self, real_powders: &Vec<PowderFr>) -> Result<(), Errorfr> {
let mut powdervec = Vec::new(); let mut powdervec = Vec::new();
for eachpowder in real_powders { for eachpowder in real_powders {
let powderamount: u8 = eachpowder.amount.unwrap_or(1); let powderamount: u8 = eachpowder.amount.unwrap_or(1);
@ -203,7 +204,7 @@ pub fn encode_powderdata(general_params: &mut FuncParams, real_powders: &Vec<Pow
'a' => Element::Air, 'a' => Element::Air,
_ => {return Err(Errorfr::JsonUnknownPowderElement)}, _ => {return Err(Errorfr::JsonUnknownPowderElement)},
}; };
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
dbg!(eletype); dbg!(eletype);
} }
powdervec.push( powdervec.push(
@ -211,7 +212,7 @@ pub fn encode_powderdata(general_params: &mut FuncParams, real_powders: &Vec<Pow
); // 6 is the tier. Wynntils ONLY really uses tier 6 so theres no point keeping others. ); // 6 is the tier. Wynntils ONLY really uses tier 6 so theres no point keeping others.
} }
} }
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
dbg!(&powdervec); dbg!(&powdervec);
} }
@ -224,23 +225,23 @@ pub fn encode_powderdata(general_params: &mut FuncParams, real_powders: &Vec<Pow
powder_slots: powderlimitfr, powder_slots: powderlimitfr,
powders: powdervec, powders: powdervec,
} }
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
pub fn encode_rerolldata(general_params: &mut FuncParams, rerollcount: u8) -> Result<(), Errorfr> { pub fn encode_rerolldata(&mut self, rerollcount: u8) -> Result<(), Errorfr> {
if rerollcount != 0 { if rerollcount != 0 {
// ENCODE: RerollData if applicable // ENCODE: RerollData if applicable
RerollData(rerollcount) RerollData(rerollcount)
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
dbg!(rerollcount); dbg!(rerollcount);
} }
} }
Ok(()) Ok(())
} }
pub fn encode_shinydata(general_params: &mut FuncParams, shiny: &Shinyjson, json_shiny: &Vec<Shinystruct>) -> Result<(), Errorfr> { pub fn encode_shinydata(&mut self, shiny: &Shinyjson, json_shiny: &Vec<Shinystruct>) -> Result<(), Errorfr> {
let mut realshinykey: u8; let mut realshinykey: u8;
let _shinykey = &shiny.key; let _shinykey = &shiny.key;
let shinyvalue = shiny.value; let shinyvalue = shiny.value;
@ -248,12 +249,12 @@ pub fn encode_shinydata(general_params: &mut FuncParams, shiny: &Shinyjson, json
for i in json_shiny { for i in json_shiny {
if i.key == shiny.key { if i.key == shiny.key {
realshinykey = i.id; realshinykey = i.id;
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
dbg!(&shiny.key); dbg!(&shiny.key);
} }
} }
} }
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
dbg!(&realshinykey); dbg!(&realshinykey);
dbg!(&shinyvalue); dbg!(&shinyvalue);
} }
@ -262,17 +263,18 @@ pub fn encode_shinydata(general_params: &mut FuncParams, shiny: &Shinyjson, json
id: realshinykey, id: realshinykey,
val: shinyvalue, val: shinyvalue,
} }
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
Ok(()) Ok(())
} }
pub fn encode_enddata(general_params: &mut FuncParams) -> Result<(), Errorfr> { pub fn encode_enddata(&mut self) -> Result<(), Errorfr> {
if *general_params.fr_debug_mode { if *self.fr_debug_mode {
println!("Encoding EndData") println!("Encoding EndData")
} }
// ENCODE: EndData // ENCODE: EndData
EndData EndData
.encode(general_params.fr_ver, general_params.fr_out) .encode(self.fr_ver, self.fr_out)
.unwrap(); .unwrap();
Ok(()) Ok(())
}
} }

View file

@ -8,7 +8,6 @@ mod encode;
mod errorfr; mod errorfr;
mod jsondl; mod jsondl;
mod jsonstruct; mod jsonstruct;
use crate::encode::*;
use crate::errorfr::Errorfr; use crate::errorfr::Errorfr;
use crate::jsondl::*; use crate::jsondl::*;
use crate::jsonstruct::*; use crate::jsonstruct::*;
@ -126,14 +125,14 @@ fn cook(
}; };
// ENCODE: StartData and TypeData, ALWAYS // ENCODE: StartData and TypeData, ALWAYS
encode_startdata(&mut fr_params)?; fr_params.encode_startdata()?;
encode_typedata(&mut fr_params, json_config.item_type)?; fr_params.encode_typedata(json_config.item_type)?;
// ENCODE: CustomGearTypeData / CustomConsumableTypeData // ENCODE: CustomGearTypeData / CustomConsumableTypeData
match json_config.item_type { match json_config.item_type {
ItemTypeDeser::CraftedGear | ItemTypeDeser::CraftedConsu => { ItemTypeDeser::CraftedGear | ItemTypeDeser::CraftedConsu => {
if let Some(real_crafted_type) = &json_config.crafted_type { if let Some(real_crafted_type) = &json_config.crafted_type {
encode_typedata_custom(&mut fr_params, real_crafted_type)?; fr_params.encode_typedata_custom(real_crafted_type)?;
} else { } else {
return Err(JsonNotFoundCraftedType); return Err(JsonNotFoundCraftedType);
} }
@ -145,7 +144,7 @@ fn cook(
match json_config.item_type { match json_config.item_type {
ItemTypeDeser::Gear | ItemTypeDeser::Tome | ItemTypeDeser::Charm => { ItemTypeDeser::Gear | ItemTypeDeser::Tome | ItemTypeDeser::Charm => {
if let Some(real_name) = &json_config.name { if let Some(real_name) = &json_config.name {
encode_namedata(&mut fr_params, real_name)? fr_params.encode_namedata(real_name)?
} else { } else {
return Err(Errorfr::JsonNotFoundName); return Err(Errorfr::JsonNotFoundName);
} }
@ -157,7 +156,7 @@ fn cook(
match json_config.item_type { match json_config.item_type {
ItemTypeDeser::Gear | ItemTypeDeser::Tome | ItemTypeDeser::Charm => { ItemTypeDeser::Gear | ItemTypeDeser::Tome | ItemTypeDeser::Charm => {
if let Some(real_ids) = &json_config.ids { if let Some(real_ids) = &json_config.ids {
encode_iddata(&mut fr_params, real_ids, idsmap)? fr_params.encode_iddata(real_ids, idsmap)?
} }
} }
_ => {} _ => {}
@ -167,7 +166,7 @@ fn cook(
match json_config.item_type { match json_config.item_type {
ItemTypeDeser::CraftedGear => { ItemTypeDeser::CraftedGear => {
if let Some(real_dura) = &json_config.durability { if let Some(real_dura) = &json_config.durability {
encode_duradata(&mut fr_params, real_dura)?; fr_params.encode_duradata(real_dura)?;
} else { } else {
return Err(Errorfr::JsonNotFoundDura); return Err(Errorfr::JsonNotFoundDura);
} }
@ -179,7 +178,7 @@ fn cook(
match json_config.item_type { match json_config.item_type {
ItemTypeDeser::CraftedGear | ItemTypeDeser::CraftedConsu => { ItemTypeDeser::CraftedGear | ItemTypeDeser::CraftedConsu => {
if let Some(real_reqs) = json_config.requirements { if let Some(real_reqs) = json_config.requirements {
encode_reqdata(&mut fr_params, real_reqs)? fr_params.encode_reqdata(real_reqs)?
} else { } else {
return Err(Errorfr::JsonNotFoundReqs); return Err(Errorfr::JsonNotFoundReqs);
} }
@ -191,7 +190,7 @@ fn cook(
match json_config.item_type { match json_config.item_type {
ItemTypeDeser::Gear | ItemTypeDeser::CraftedGear => { ItemTypeDeser::Gear | ItemTypeDeser::CraftedGear => {
if let Some(real_powders) = &json_config.powders { if let Some(real_powders) = &json_config.powders {
encode_powderdata(&mut fr_params, real_powders)? fr_params.encode_powderdata(real_powders)?
} }
} }
_ => {} _ => {}
@ -202,7 +201,7 @@ fn cook(
ItemTypeDeser::Gear | ItemTypeDeser::Tome | ItemTypeDeser::Charm => { ItemTypeDeser::Gear | ItemTypeDeser::Tome | ItemTypeDeser::Charm => {
if let Some(rerollcount) = json_config.rerolls { if let Some(rerollcount) = json_config.rerolls {
// rerolldata // rerolldata
encode_rerolldata(&mut fr_params, rerollcount)? fr_params.encode_rerolldata(rerollcount)?
} }
} }
_ => {} _ => {}
@ -212,14 +211,14 @@ fn cook(
match json_config.item_type { match json_config.item_type {
ItemTypeDeser::Gear => { ItemTypeDeser::Gear => {
if let Some(shiny) = &json_config.shiny { if let Some(shiny) = &json_config.shiny {
encode_shinydata(&mut fr_params, shiny, &json_shiny)? fr_params.encode_shinydata(shiny, &json_shiny)?
} }
} }
_ => {} _ => {}
} }
// ENCODE: EndData, ALWAYS // ENCODE: EndData, ALWAYS
encode_enddata(&mut fr_params)?; fr_params.encode_enddata()?;
let mut final_string: String = encode_string(out); let mut final_string: String = encode_string(out);