diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-08-06 07:22:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-06 02:22:19 -0500 |
| commit | 5a9fca0ca9cdb46f4b866781f219756c89e2293a (patch) | |
| tree | b006e28b91a181734fb9702bb6ec510f5b2af3df /azalea-protocol/src/packets | |
| parent | 1d48c3fe34edd4e2295f54bd3d79f81f58c38a8e (diff) | |
| download | azalea-drasl-5a9fca0ca9cdb46f4b866781f219756c89e2293a.tar.xz | |
Better errors (#14)
* make reading use thiserror
* finish implementing all the error things
* clippy warnings related to ok_or
* fix some errors in other places
* thiserror in more places
* don't use closures in a couple places
* errors in writing packet
* rip backtraces
* change some BufReadError::Custom to UnexpectedEnumVariant
* Errors say what packet is bad
* error on leftover data and fix
it wasn't reading the properties for gameprofile
Diffstat (limited to 'azalea-protocol/src/packets')
18 files changed, 61 insertions, 77 deletions
diff --git a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs index f45946d5..622b9ec7 100755 --- a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs @@ -1,3 +1,4 @@ +use azalea_buf::BufReadError; use azalea_buf::McBuf; use azalea_buf::McBufVarReadable; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; @@ -24,7 +25,7 @@ pub struct BrigadierNumber<T> { max: Option<T>, } impl<T: McBufReadable> McBufReadable for BrigadierNumber<T> { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let flags = buf.read_byte()?; let min = if flags & 0x01 != 0 { Some(T::read_from(buf)?) @@ -123,7 +124,7 @@ pub enum BrigadierParser { } impl McBufReadable for BrigadierParser { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let parser_type = u32::var_read_from(buf)?; match parser_type { @@ -190,14 +191,16 @@ impl McBufReadable for BrigadierParser { 45 => Ok(BrigadierParser::TemplateMirror), 46 => Ok(BrigadierParser::TemplateRotation), 47 => Ok(BrigadierParser::Uuid), - _ => Err(format!("Unknown BrigadierParser type: {}", parser_type)), + _ => Err(BufReadError::UnexpectedEnumVariant { + id: parser_type as i32, + }), } } } // TODO: BrigadierNodeStub should have more stuff impl McBufReadable for BrigadierNodeStub { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let flags = u8::read_from(buf)?; if flags > 31 { println!( diff --git a/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs b/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs index 8b52b8cf..d85f39cb 100644 --- a/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::{McBufReadable, McBufVarReadable, McBufWritable}; +use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufWritable}; use azalea_core::ParticleData; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -20,7 +20,7 @@ pub struct ClientboundLevelParticlesPacket { } impl McBufReadable for ClientboundLevelParticlesPacket { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let particle_id = u32::var_read_from(buf)?; let override_limiter = bool::read_from(buf)?; let x = f64::read_from(buf)?; diff --git a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs index 9a312276..5ab187ae 100755 --- a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable}; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -20,7 +20,7 @@ pub struct PlayerAbilitiesFlags { } impl McBufReadable for PlayerAbilitiesFlags { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let byte = buf.read_byte()?; Ok(PlayerAbilitiesFlags { invulnerable: byte & 1 != 0, diff --git a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs index a2caae4b..7414e10c 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::{BitSet, McBuf, McBufReadable, McBufVarWritable}; +use azalea_buf::{BitSet, BufReadError, McBuf, McBufReadable, McBufVarWritable}; use azalea_buf::{McBufVarReadable, McBufWritable}; use azalea_chat::component::Component; use azalea_crypto::{MessageSignature, SignedMessageHeader}; @@ -74,13 +74,15 @@ pub enum FilterMask { } impl McBufReadable for FilterMask { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + 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("Invalid filter mask".to_string()), + _ => Err(BufReadError::UnexpectedEnumVariant { + id: filter_mask as i32, + }), } } } diff --git a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs index 614a351e..d53774b7 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_chat::component::Component; use packet_macros::ClientboundGamePacket; @@ -63,7 +63,7 @@ pub struct RemovePlayer { } impl McBufReadable for Action { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let id = buf.read_byte()?; Ok(match id { 0 => Action::AddPlayer(Vec::<AddPlayer>::read_from(buf)?), @@ -71,7 +71,7 @@ impl McBufReadable for Action { 2 => Action::UpdateLatency(Vec::<UpdateLatency>::read_from(buf)?), 3 => Action::UpdateDisplayName(Vec::<UpdateDisplayName>::read_from(buf)?), 4 => Action::RemovePlayer(Vec::<RemovePlayer>::read_from(buf)?), - _ => panic!("Unknown player info action id: {}", id), + _ => return Err(BufReadError::UnexpectedEnumVariant { id: id.into() }), }) } } diff --git a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs index 758e6bb0..d4c9dcb5 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable}; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -28,7 +28,7 @@ pub struct RelativeArguments { } impl McBufReadable for RelativeArguments { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let byte = buf.read_byte()?; Ok(RelativeArguments { x: byte & 0b1 != 0, diff --git a/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs b/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs index e6a5bf7f..05e96f69 100644 --- a/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_core::ResourceLocation; use packet_macros::ClientboundGamePacket; @@ -41,7 +41,7 @@ impl McBufWritable for State { } } impl McBufReadable for State { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let state = buf.read_varint()?; Ok(match state { 0 => State::Init, diff --git a/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs b/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs index 0a83edf0..5dee398e 100644 --- a/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable}; use azalea_core::{ChunkSectionBlockPos, ChunkSectionPos}; use packet_macros::ClientboundGamePacket; @@ -18,7 +18,7 @@ pub struct BlockStateWithPosition { } impl McBufReadable for BlockStateWithPosition { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let data = u64::var_read_from(buf)?; let position_part = data & 4095; let state = (data >> 12) as u32; diff --git a/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs index 102e90e1..35f0d251 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_core::Slot; use packet_macros::ClientboundGamePacket; @@ -17,13 +17,17 @@ pub struct EquipmentSlots { } impl McBufReadable for EquipmentSlots { - fn read_from(buf: &mut impl std::io::Read) -> Result<Self, String> { + fn read_from(buf: &mut impl std::io::Read) -> Result<Self, BufReadError> { let mut slots = vec![]; loop { let equipment_byte = u8::read_from(buf)?; - let equipment_slot = EquipmentSlot::from_byte(equipment_byte & 127) - .ok_or_else(|| format!("Invalid equipment slot byte {}", equipment_byte))?; + let equipment_slot = + EquipmentSlot::from_byte(equipment_byte & 127).ok_or_else(|| { + BufReadError::UnexpectedEnumVariant { + id: equipment_byte.into(), + } + })?; let item = Slot::read_from(buf)?; slots.push((equipment_slot, item)); if equipment_byte & 128 == 0 { diff --git a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs index 396c0fa4..88cb15e9 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::{McBuf, McBufReadable, McBufWritable}; +use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; use azalea_chat::component::Component; use azalea_core::{ResourceLocation, Slot}; use packet_macros::ClientboundGamePacket; @@ -45,7 +45,7 @@ pub struct DisplayFlags { } impl McBufReadable for DisplayFlags { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let data = u32::read_from(buf)?; Ok(DisplayFlags { background: (data & 0b1) != 0, diff --git a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs index b8785a66..6bb41a81 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_core::ResourceLocation; use packet_macros::ClientboundGamePacket; @@ -34,12 +34,12 @@ enum Operation { } impl McBufReadable for Operation { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { match buf.read_byte()? { 0 => Ok(Operation::Addition), 1 => Ok(Operation::MultiplyBase), 2 => Ok(Operation::MultiplyTotal), - op => Err(format!("Unknown operation: {}", op)), + id => Err(BufReadError::UnexpectedEnumVariant { id: id.into() }), } } } diff --git a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs index 7a96629b..da1485d0 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs @@ -1,6 +1,6 @@ use std::io::{Read, Write}; -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_core::{ResourceLocation, Slot}; use packet_macros::ClientboundGamePacket; @@ -49,7 +49,7 @@ impl McBufWritable for ShapedRecipe { } } impl McBufReadable for ShapedRecipe { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let width = buf.read_varint()?.try_into().unwrap(); let height = buf.read_varint()?.try_into().unwrap(); let group = buf.read_utf()?; @@ -129,7 +129,7 @@ impl McBufWritable for Recipe { } impl McBufReadable for Recipe { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let recipe_type = ResourceLocation::read_from(buf)?; let identifier = ResourceLocation::read_from(buf)?; diff --git a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs index 6d567cfa..6f0fe778 100755 --- a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_core::ResourceLocation; use packet_macros::ClientboundGamePacket; @@ -23,7 +23,7 @@ pub struct Tags { pub struct TagMap(HashMap<ResourceLocation, Vec<Tags>>); impl McBufReadable for TagMap { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let length = buf.read_varint()? as usize; let mut data = HashMap::with_capacity(length); for _ in 0..length { @@ -51,7 +51,7 @@ impl McBufWritable for TagMap { } } impl McBufReadable for Tags { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let name = ResourceLocation::read_from(buf)?; let elements = buf.read_int_id_list()?; Ok(Tags { name, elements }) diff --git a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs index 9d9ce35b..93421cc2 100755 --- a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs @@ -2,34 +2,11 @@ use std::io::{Read, Write}; use super::ClientboundLoginPacket; use azalea_auth::game_profile::GameProfile; -use azalea_buf::{McBufReadable, Readable, SerializableUuid, Writable}; +use azalea_buf::{BufReadError, McBuf, McBufReadable, Readable, SerializableUuid, Writable}; +use packet_macros::ClientboundLoginPacket; use uuid::Uuid; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, McBuf, ClientboundLoginPacket)] pub struct ClientboundGameProfilePacket { pub game_profile: GameProfile, } - -// TODO: add derives to GameProfile and have an impl McBufReadable/Writable for GameProfile -impl ClientboundGameProfilePacket { - pub fn get(self) -> ClientboundLoginPacket { - ClientboundLoginPacket::ClientboundGameProfilePacket(self) - } - - pub fn write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - for n in self.game_profile.uuid.to_int_array() { - buf.write_int(n as i32).unwrap(); - } - buf.write_utf(self.game_profile.name.as_str()).unwrap(); - Ok(()) - } - - pub fn read(buf: &mut impl Read) -> Result<ClientboundLoginPacket, String> { - let uuid = Uuid::read_from(buf)?; - let name = buf.read_utf_with_len(16)?; - Ok(ClientboundGameProfilePacket { - game_profile: GameProfile::new(uuid, name), - } - .get()) - } -} diff --git a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs index 39314041..26321f34 100755 --- a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs @@ -3,7 +3,7 @@ use std::{ io::{Read, Write}, }; -use azalea_buf::{Readable, Writable}; +use azalea_buf::{BufReadError, Readable, Writable}; use super::ClientboundLoginPacket; @@ -22,7 +22,7 @@ impl ClientboundLoginCompressionPacket { Ok(()) } - pub fn read(buf: &mut impl Read) -> Result<ClientboundLoginPacket, String> { + pub fn read(buf: &mut impl Read) -> Result<ClientboundLoginPacket, BufReadError> { let compression_threshold = buf.read_varint()?; Ok(ClientboundLoginCompressionPacket { diff --git a/azalea-protocol/src/packets/login/serverbound_key_packet.rs b/azalea-protocol/src/packets/login/serverbound_key_packet.rs index 62dc6252..54f268e7 100644 --- a/azalea-protocol/src/packets/login/serverbound_key_packet.rs +++ b/azalea-protocol/src/packets/login/serverbound_key_packet.rs @@ -1,4 +1,4 @@ -use azalea_buf::McBuf; +use azalea_buf::{BufReadError, McBuf}; use azalea_crypto::SaltSignaturePair; use packet_macros::ServerboundLoginPacket; use std::io::{Read, Write}; @@ -18,7 +18,7 @@ pub enum NonceOrSaltSignature { } impl McBufReadable for NonceOrSaltSignature { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let is_nonce = bool::read_from(buf)?; if is_nonce { Ok(NonceOrSaltSignature::Nonce(Vec::<u8>::read_from(buf)?)) diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index 228d7c74..da1631a0 100644 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -3,7 +3,8 @@ pub mod handshake; pub mod login; pub mod status; -use azalea_buf::{McBufWritable, Readable, Writable}; +use crate::read::ReadPacketError; +use azalea_buf::{BufReadError, McBufWritable, Readable, Writable}; use std::io::{Read, Write}; pub const PROTOCOL_VERSION: u32 = 760; @@ -36,15 +37,15 @@ where fn id(&self) -> u32; /// Read a packet by its id, ConnectionProtocol, and flow - fn read(id: u32, buf: &mut impl Read) -> Result<Self, String>; + fn read(id: u32, buf: &mut impl Read) -> Result<Self, ReadPacketError>; fn write(&self, buf: &mut impl Write) -> Result<(), std::io::Error>; } impl azalea_buf::McBufReadable for ConnectionProtocol { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { - ConnectionProtocol::from_i32(buf.read_varint()?) - .ok_or_else(|| "Invalid intention".to_string()) + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + let id = buf.read_varint()?; + ConnectionProtocol::from_i32(id).ok_or(BufReadError::UnexpectedEnumVariant { id }) } } diff --git a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs index 2e4116fa..8724816b 100755 --- a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -1,5 +1,5 @@ use super::ClientboundStatusPacket; -use azalea_buf::Readable; +use azalea_buf::{BufReadError, Readable}; use azalea_chat::component::Component; use serde::Deserialize; use serde_json::Value; @@ -42,14 +42,11 @@ impl ClientboundStatusResponsePacket { Ok(()) } - pub fn read(buf: &mut impl Read) -> Result<ClientboundStatusPacket, String> { + pub fn read(buf: &mut impl Read) -> Result<ClientboundStatusPacket, BufReadError> { let status_string = buf.read_utf()?; - let status_json: Value = - serde_json::from_str(status_string.as_str()).expect("Server status isn't valid JSON"); + let status_json: Value = serde_json::from_str(status_string.as_str())?; - let packet = ClientboundStatusResponsePacket::deserialize(status_json) - .map_err(|e| e.to_string())? - .get(); + let packet = ClientboundStatusResponsePacket::deserialize(status_json)?.get(); Ok(packet) } |
