Skip to content

Commit

Permalink
Inline some new type size functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gtker committed Mar 30, 2024
1 parent c1ff0b7 commit b3ece7a
Show file tree
Hide file tree
Showing 40 changed files with 245 additions and 1,328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -795,14 +795,14 @@ impl CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag {
1 // inner
+ {
if let Some(s) = &self.pin {
s.size()
20
} else {
0
}
}
+ {
if let Some(s) = &self.matrix_card {
s.size()
12
} else {
0
}
Expand All @@ -816,13 +816,6 @@ pub struct CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_Pin {
pub pin_salt: [u8; 16],
}

impl CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_Pin {
pub(crate) const fn size(&self) -> usize {
4 // pin_grid_seed: u32
+ 16 // pin_salt: u8[16]
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_MatrixCard {
pub challenge_count: u8,
Expand All @@ -832,16 +825,6 @@ pub struct CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_MatrixCard {
pub width: u8,
}

impl CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_MatrixCard {
pub(crate) const fn size(&self) -> usize {
1 // challenge_count: u8
+ 1 // digit_count: u8
+ 1 // height: u8
+ 8 // seed: u64
+ 1 // width: u8
}
}

impl Default for CMD_AUTH_LOGON_CHALLENGE_Server {
fn default() -> Self {
// First enumerator without any fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,14 @@ impl CMD_AUTH_LOGON_PROOF_Client_SecurityFlag {
1 // inner
+ {
if let Some(s) = &self.pin {
s.size()
36
} else {
0
}
}
+ {
if let Some(s) = &self.matrix_card {
s.size()
20
} else {
0
}
Expand All @@ -579,24 +579,11 @@ pub struct CMD_AUTH_LOGON_PROOF_Client_SecurityFlag_Pin {
pub pin_salt: [u8; 16],
}

impl CMD_AUTH_LOGON_PROOF_Client_SecurityFlag_Pin {
pub(crate) const fn size(&self) -> usize {
20 // pin_hash: u8[20]
+ 16 // pin_salt: u8[16]
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct CMD_AUTH_LOGON_PROOF_Client_SecurityFlag_MatrixCard {
pub matrix_card_proof: [u8; 20],
}

impl CMD_AUTH_LOGON_PROOF_Client_SecurityFlag_MatrixCard {
pub(crate) const fn size(&self) -> usize {
20 // matrix_card_proof: u8[20]
}
}

#[cfg(test)]
mod test_version_5 {
#![allow(clippy::missing_const_for_fn)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,21 +881,21 @@ impl CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag {
1 // inner
+ {
if let Some(s) = &self.pin {
s.size()
20
} else {
0
}
}
+ {
if let Some(s) = &self.matrix_card {
s.size()
12
} else {
0
}
}
+ {
if let Some(s) = &self.authenticator {
s.size()
1
} else {
0
}
Expand All @@ -909,13 +909,6 @@ pub struct CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_Pin {
pub pin_salt: [u8; 16],
}

impl CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_Pin {
pub(crate) const fn size(&self) -> usize {
4 // pin_grid_seed: u32
+ 16 // pin_salt: u8[16]
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_MatrixCard {
pub challenge_count: u8,
Expand All @@ -925,27 +918,11 @@ pub struct CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_MatrixCard {
pub width: u8,
}

impl CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_MatrixCard {
pub(crate) const fn size(&self) -> usize {
1 // challenge_count: u8
+ 1 // digit_count: u8
+ 1 // height: u8
+ 8 // seed: u64
+ 1 // width: u8
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_Authenticator {
pub required: u8,
}

impl CMD_AUTH_LOGON_CHALLENGE_Server_SecurityFlag_Authenticator {
pub(crate) const fn size(&self) -> usize {
1 // required: u8
}
}

impl Default for CMD_AUTH_LOGON_CHALLENGE_Server {
fn default() -> Self {
// First enumerator without any fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,14 @@ impl CMD_AUTH_LOGON_PROOF_Client_SecurityFlag {
1 // inner
+ {
if let Some(s) = &self.pin {
s.size()
36
} else {
0
}
}
+ {
if let Some(s) = &self.matrix_card {
s.size()
20
} else {
0
}
Expand All @@ -680,24 +680,11 @@ pub struct CMD_AUTH_LOGON_PROOF_Client_SecurityFlag_Pin {
pub pin_salt: [u8; 16],
}

impl CMD_AUTH_LOGON_PROOF_Client_SecurityFlag_Pin {
pub(crate) const fn size(&self) -> usize {
20 // pin_hash: u8[20]
+ 16 // pin_salt: u8[16]
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct CMD_AUTH_LOGON_PROOF_Client_SecurityFlag_MatrixCard {
pub matrix_card_proof: [u8; 20],
}

impl CMD_AUTH_LOGON_PROOF_Client_SecurityFlag_MatrixCard {
pub(crate) const fn size(&self) -> usize {
20 // matrix_card_proof: u8[20]
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct CMD_AUTH_LOGON_PROOF_Client_SecurityFlag_Authenticator {
pub authenticator: String,
Expand Down
8 changes: 1 addition & 7 deletions wow_login_messages/src/logon/version_8/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ impl Realm_RealmFlag {
1 // inner
+ {
if let Some(s) = &self.specify_build {
s.size()
5
} else {
0
}
Expand All @@ -485,9 +485,3 @@ pub struct Realm_RealmFlag_SpecifyBuild {
pub version: Version,
}

impl Realm_RealmFlag_SpecifyBuild {
pub(crate) const fn size(&self) -> usize {
5 // version: Version
}
}

13 changes: 13 additions & 0 deletions wow_message_parser/src/rust_printer/rust_view/rust_enumerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ impl RustEnumerator {

v
}
pub(crate) fn is_constant(&self) -> Option<i128> {
let mut size = 0;
for m in self.members() {
if let Some(i) = m.is_constant() {
size += i;
} else {
return None;
}
}

Some(size)
}

pub(crate) fn name(&self) -> &str {
&self.name
}
Expand Down
3 changes: 3 additions & 0 deletions wow_message_parser/src/rust_printer/rust_view/rust_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ impl RustMember {
v.push(self);
v
}
pub(crate) fn is_constant(&self) -> Option<i128> {
self.ty().is_constant()
}

pub(crate) fn is_single_rust_definer(&self) -> bool {
matches!(
Expand Down
37 changes: 33 additions & 4 deletions wow_message_parser/src/rust_printer/rust_view/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,37 @@ pub(crate) enum RustType {
}

impl RustType {
pub(crate) fn is_constant(&self) -> Option<i128> {
match self {
RustType::Enum {
int_ty,
enumerators,
..
}
| RustType::Flag {
int_ty,
enumerators,
..
} => {
let mut size = 0;

for enumerator in enumerators {
if let Some(i) = enumerator.is_constant() {
if i > size {
size = i;
}
} else {
return None;
}
}

Some(size + int_ty.sizes().is_constant().unwrap())
}
RustType::Struct { sizes, .. } => sizes.is_constant(),
_ => self.to_type().sizes().is_constant(),
}
}

pub(crate) fn str(&self) -> String {
match self {
RustType::Array { array, .. } => array.str(),
Expand Down Expand Up @@ -119,11 +150,9 @@ impl RustType {
RustType::IpAddress => Type::IpAddress,
RustType::Seconds => Type::Seconds,
RustType::Milliseconds => Type::Milliseconds,
RustType::Array { array, .. } => Type::Array(array.clone()),

RustType::Array { .. }
| RustType::Enum { .. }
| RustType::Flag { .. }
| RustType::Struct { .. } => {
RustType::Enum { .. } | RustType::Flag { .. } | RustType::Struct { .. } => {
panic!("invalid conversion")
}
RustType::Population => Type::Population,
Expand Down
22 changes: 14 additions & 8 deletions wow_message_parser/src/rust_printer/structs/print_new_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ fn print_size_for_new_flag(s: &mut Writer, rd: &RustDefiner) {
name = enumerator.name().to_lowercase()
),
|s| {
s.wln("s.size()");
if let Some(size) = enumerator.is_constant() {
s.wln(size.to_string());
} else {
s.wln("s.size()");
}
},
|s| {
s.wln("0");
Expand Down Expand Up @@ -335,13 +339,15 @@ fn print_types_for_new_flag(s: &mut Writer, rd: &RustDefiner) {
}
});

let const_fn = enumerator
.members_in_struct()
.iter()
.all(|a| a.ty().size_is_const_fn());
variable_size(s, &new_type_name, "size", const_fn, |s| {
print_rust_members_sizes(s, enumerator.members(), None, "self.");
});
if enumerator.is_constant().is_none() {
let const_fn = enumerator
.members_in_struct()
.iter()
.all(|a| a.ty().size_is_const_fn());
variable_size(s, &new_type_name, "size", const_fn, |s| {
print_rust_members_sizes(s, enumerator.members(), None, "self.");
});
}
}
}

Expand Down
18 changes: 3 additions & 15 deletions wow_message_parser/tests/flag_if_else_if.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl FlagIfElseIf_SimpleFlag {
1 // inner
+ {
if let Some(s) = &self.one {
s.size()
1
} else {
0
}
Expand All @@ -255,12 +255,6 @@ pub struct FlagIfElseIf_SimpleFlag_One {
pub if_one: u8,
}

impl FlagIfElseIf_SimpleFlag_One {
pub(crate) const fn size(&self) -> usize {
1 // if_one: u8
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum FlagIfElseIf_UpdateFlag_OneIf {
OneIf {
Expand Down Expand Up @@ -412,14 +406,14 @@ impl FlagIfElseIf_UpdateFlag {
1 // inner
+ {
if let Some(s) = &self.one_if {
s.size()
3
} else {
0
}
}
+ {
if let Some(s) = &self.eight_if {
s.size()
1
} else {
0
}
Expand All @@ -432,9 +426,3 @@ pub struct FlagIfElseIf_UpdateFlag_EightIf {
pub last_if: u8,
}

impl FlagIfElseIf_UpdateFlag_EightIf {
pub(crate) const fn size(&self) -> usize {
1 // last_if: u8
}
}

Loading

0 comments on commit b3ece7a

Please sign in to comment.