mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
fix errors in enums inconsistent w/ vanilla
This commit is contained in:
parent
3389f19e60
commit
dee991a7fc
3 changed files with 56 additions and 37 deletions
|
@ -14,6 +14,7 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
|
|||
.iter()
|
||||
.map(|f| {
|
||||
let field_name = &f.ident;
|
||||
let field_name_str = field_name.as_ref().unwrap().to_string();
|
||||
let field_type = &f.ty;
|
||||
// do a different buf.write_* for each field depending on the type
|
||||
// if it's a string, use buf.write_string
|
||||
|
@ -21,10 +22,12 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
|
|||
syn::Type::Path(_) | syn::Type::Array(_) => {
|
||||
if f.attrs.iter().any(|a| a.path.is_ident("var")) {
|
||||
quote! {
|
||||
println!("reading {}", #field_name_str);
|
||||
let #field_name = azalea_buf::McBufVarReadable::var_read_from(buf)?;
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
println!("reading {}", #field_name_str);
|
||||
let #field_name = azalea_buf::McBufReadable::read_from(buf)?;
|
||||
}
|
||||
}
|
||||
|
@ -54,6 +57,7 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
|
|||
let mut match_contents = quote!();
|
||||
let mut variant_discrim: u32 = 0;
|
||||
let mut first = true;
|
||||
let mut first_reader = None;
|
||||
for variant in variants {
|
||||
let variant_name = &variant.ident;
|
||||
match &variant.discriminant.as_ref() {
|
||||
|
@ -72,9 +76,7 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
|
|||
}
|
||||
}
|
||||
None => {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
if !first {
|
||||
variant_discrim += 1;
|
||||
}
|
||||
}
|
||||
|
@ -90,6 +92,10 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
|
|||
Ok(Self::#variant_name)
|
||||
},
|
||||
};
|
||||
if first {
|
||||
first_reader = Some(reader.clone());
|
||||
first = false;
|
||||
};
|
||||
|
||||
match_contents.extend(quote! {
|
||||
#variant_discrim => {
|
||||
|
@ -98,6 +104,8 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
|
|||
});
|
||||
}
|
||||
|
||||
let first_reader = first_reader.expect("There should be at least one variant");
|
||||
|
||||
quote! {
|
||||
impl azalea_buf::McBufReadable for #ident {
|
||||
fn read_from(buf: &mut impl std::io::Read) -> Result<Self, azalea_buf::BufReadError>
|
||||
|
@ -105,7 +113,8 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
|
|||
let id = azalea_buf::McBufVarReadable::var_read_from(buf)?;
|
||||
match id {
|
||||
#match_contents
|
||||
_ => Err(azalea_buf::BufReadError::UnexpectedEnumVariant { id: id as i32 }),
|
||||
// you'd THINK this throws an error, but mojang decided to make it default for some reason
|
||||
_ => #first_reader
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
use azalea_buf::{
|
||||
BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable,
|
||||
};
|
||||
use azalea_buf::McBuf;
|
||||
use azalea_chat::component::Component;
|
||||
use azalea_core::BitSet;
|
||||
use azalea_crypto::{MessageSignature, SignedMessageHeader};
|
||||
use azalea_protocol_macros::ClientboundGamePacket;
|
||||
use std::io::{Read, Write};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)]
|
||||
|
@ -82,48 +79,25 @@ pub struct ChatMessageContent {
|
|||
pub decorated: Option<Component>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, McBuf)]
|
||||
pub enum FilterMask {
|
||||
PassThrough,
|
||||
FullyFiltered,
|
||||
PartiallyFiltered(BitSet),
|
||||
}
|
||||
|
||||
impl McBufReadable for FilterMask {
|
||||
fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
|
||||
let filter_mask = u32::var_read_from(buf)?;
|
||||
match filter_mask {
|
||||
0 => Ok(FilterMask::PassThrough),
|
||||
1 => Ok(FilterMask::FullyFiltered),
|
||||
2 => Ok(FilterMask::PartiallyFiltered(BitSet::read_from(buf)?)),
|
||||
_ => Err(BufReadError::UnexpectedEnumVariant {
|
||||
id: filter_mask as i32,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl McBufWritable for FilterMask {
|
||||
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
|
||||
match self {
|
||||
FilterMask::PassThrough => 0u32.var_write_into(buf)?,
|
||||
FilterMask::FullyFiltered => 1u32.var_write_into(buf)?,
|
||||
FilterMask::PartiallyFiltered(bits) => {
|
||||
2u32.var_write_into(buf)?;
|
||||
bits.write_into(buf)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use azalea_buf::McBufReadable;
|
||||
|
||||
#[test]
|
||||
fn test_chat_type() {
|
||||
let chat_type_enum = ChatType::read_from(&mut &[0x06][..]).unwrap();
|
||||
assert_eq!(chat_type_enum, ChatType::EmoteCommand);
|
||||
assert!(ChatType::read_from(&mut &[0x07][..]).is_err());
|
||||
assert_eq!(
|
||||
ChatType::read_from(&mut &[0x07][..]).unwrap(),
|
||||
ChatType::Chat
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -217,3 +217,39 @@ where
|
|||
|
||||
Ok(packet)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::packets::game::{clientbound_player_chat_packet::ChatType, ClientboundGamePacket};
|
||||
use std::io::Cursor;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_packet() {
|
||||
let mut buf = Cursor::new(vec![
|
||||
51, 0, 12, 177, 250, 155, 132, 106, 60, 218, 161, 217, 90, 157, 105, 57, 206, 20, 0, 5,
|
||||
104, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 116,
|
||||
123, 34, 101, 120, 116, 114, 97, 34, 58, 91, 123, 34, 99, 111, 108, 111, 114, 34, 58,
|
||||
34, 103, 114, 97, 121, 34, 44, 34, 116, 101, 120, 116, 34, 58, 34, 91, 77, 69, 77, 66,
|
||||
69, 82, 93, 32, 112, 108, 97, 121, 101, 114, 49, 34, 125, 44, 123, 34, 116, 101, 120,
|
||||
116, 34, 58, 34, 32, 34, 125, 44, 123, 34, 99, 111, 108, 111, 114, 34, 58, 34, 103,
|
||||
114, 97, 121, 34, 44, 34, 116, 101, 120, 116, 34, 58, 34, 92, 117, 48, 48, 51, 101, 32,
|
||||
104, 101, 108, 108, 111, 34, 125, 93, 44, 34, 116, 101, 120, 116, 34, 58, 34, 34, 125,
|
||||
0, 7, 64, 123, 34, 101, 120, 116, 114, 97, 34, 58, 91, 123, 34, 99, 111, 108, 111, 114,
|
||||
34, 58, 34, 103, 114, 97, 121, 34, 44, 34, 116, 101, 120, 116, 34, 58, 34, 91, 77, 69,
|
||||
77, 66, 69, 82, 93, 32, 112, 108, 97, 121, 101, 114, 49, 34, 125, 93, 44, 34, 116, 101,
|
||||
120, 116, 34, 58, 34, 34, 125, 0,
|
||||
]);
|
||||
let packet = packet_decoder::<ClientboundGamePacket>(&mut buf).unwrap();
|
||||
match &packet {
|
||||
ClientboundGamePacket::PlayerChat(m) => {
|
||||
assert_eq!(
|
||||
m.chat_type.chat_type,
|
||||
ChatType::Chat,
|
||||
"Enums should default if they're invalid"
|
||||
);
|
||||
}
|
||||
_ => panic!("Wrong packet type"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue