From 9b78d7ed805436c3a3e44811e4b19825e8240f24 Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 27 Sep 2021 23:06:21 -0600 Subject: [PATCH] add some Defaults --- src/entity/account.rs | 22 ++++++++++++++++++++- src/entity/character.rs | 42 +++++++++++++++++++++++++---------------- src/lib.rs | 1 + 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/entity/account.rs b/src/entity/account.rs index 9223797..3958412 100644 --- a/src/entity/account.rs +++ b/src/entity/account.rs @@ -5,7 +5,7 @@ use libpso::character::guildcard; pub const USERFLAG_NEWCHAR: u32 = 0x00000001; pub const USERFLAG_DRESSINGROOM: u32 = 0x00000002; -#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)] pub struct UserAccountId(pub u32); #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct UserSettingsId(pub u32); @@ -59,6 +59,26 @@ pub struct UserAccountEntity { pub at_ship: bool, } +impl Default for UserAccountEntity { + fn default() -> UserAccountEntity { + UserAccountEntity { + id: UserAccountId(0), + username: "".into(), + password: "".into(), + guildcard: 0xFFFFFFFF, + team_id: None, + banned_until: None, + muted_until: None, + created_at: chrono::Utc::now(), + flags: 0, + activated: false, + at_login: false, + at_character: false, + at_ship: false, + } + } +} + impl UserAccountEntity { pub fn is_currently_online(&self) -> bool { self.at_login | self.at_character | self.at_ship diff --git a/src/entity/character.rs b/src/entity/character.rs index 6785322..117fddb 100644 --- a/src/entity/character.rs +++ b/src/entity/character.rs @@ -7,8 +7,9 @@ use libpso::character::character::{DEFAULT_PALETTE_CONFIG, DEFAULT_TECH_MENU}; use crate::entity::item::tech::Technique; use crate::entity::account::UserAccountId; -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, enum_utils::FromStr, derive_more::Display, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, enum_utils::FromStr, derive_more::Display, Serialize, Deserialize, Default)] pub enum CharacterClass { + #[default] HUmar, HUnewearl, HUcast, @@ -90,8 +91,9 @@ impl CharacterClass { } -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, enum_utils::FromStr, derive_more::Display, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, enum_utils::FromStr, derive_more::Display, Serialize, Deserialize, Default)] pub enum SectionID { + #[default] Viridia, Greenill, Skyly, @@ -163,13 +165,15 @@ pub struct CharacterTechniques { pub techs: HashMap } -impl CharacterTechniques { - fn new() -> CharacterTechniques { +impl Default for CharacterTechniques { + fn default() -> CharacterTechniques { CharacterTechniques { techs: HashMap::new(), } } +} +impl CharacterTechniques { pub fn set_tech(&mut self, tech: Technique, level: TechLevel) { self.techs.insert(tech, TechLevel(level.0 - 1)); } @@ -192,13 +196,15 @@ pub struct CharacterConfig { pub raw_data: [u8; 0xE8], } -impl CharacterConfig { - fn new() -> CharacterConfig { +impl Default for CharacterConfig { + fn default() -> CharacterConfig { CharacterConfig { raw_data: DEFAULT_PALETTE_CONFIG, } } +} +impl CharacterConfig { pub fn update(&mut self, new_config: &UpdateConfig) { self.raw_data = new_config.config; } @@ -213,13 +219,15 @@ pub struct CharacterInfoboard { pub board: [u16; 172], } -impl CharacterInfoboard { - fn new() -> CharacterInfoboard { +impl Default for CharacterInfoboard { + fn default() -> CharacterInfoboard { CharacterInfoboard { board: [0; 172] } } +} +impl CharacterInfoboard { pub fn as_bytes(&self) -> [u16; 172] { self.board } @@ -239,13 +247,15 @@ pub struct CharacterTechMenu { pub tech_menu: [u8; 40], } -impl CharacterTechMenu { - fn new() -> CharacterTechMenu { +impl Default for CharacterTechMenu { + fn default() -> CharacterTechMenu { CharacterTechMenu { tech_menu: DEFAULT_TECH_MENU, } } +} +impl CharacterTechMenu { pub fn as_bytes(&self) -> [u8; 40] { self.tech_menu } @@ -262,7 +272,7 @@ pub struct CharacterMaterials { pub tp: u32, } -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)] pub struct CharacterEntityId(pub u32); #[derive(Clone)] @@ -299,12 +309,12 @@ impl NewCharacterEntity { char_class: CharacterClass::HUmar, section_id: SectionID::Viridia, appearance: CharacterAppearance::default(), - techs: CharacterTechniques::new(), - config: CharacterConfig::new(), - info_board: CharacterInfoboard::new(), + techs: CharacterTechniques::default(), + config: CharacterConfig::default(), + info_board: CharacterInfoboard::default(), guildcard: CharacterGuildCard::default(), materials: CharacterMaterials::default(), - tech_menu: CharacterTechMenu::new(), + tech_menu: CharacterTechMenu::default(), meseta: 0, bank_meseta: 0, option_flags: 0, @@ -312,7 +322,7 @@ impl NewCharacterEntity { } } -#[derive(Clone)] +#[derive(Clone, Default)] pub struct CharacterEntity { pub id: CharacterEntityId, pub user_id: UserAccountId, diff --git a/src/lib.rs b/src/lib.rs index dbf3afa..cb41223 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ #![feature(maybe_uninit_extra)] #![feature(inline_const)] #![feature(drain_filter)] +#![feature(derive_default_enum)]