From 5a9fca0ca9cdb46f4b866781f219756c89e2293a Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sat, 6 Aug 2022 07:22:19 +0000 Subject: 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 --- .../login/clientbound_game_profile_packet.rs | 29 +++------------------- .../login/clientbound_login_compression_packet.rs | 4 +-- .../src/packets/login/serverbound_key_packet.rs | 4 +-- 3 files changed, 7 insertions(+), 30 deletions(-) (limited to 'azalea-protocol/src/packets/login') 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 { - 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 { + pub fn read(buf: &mut impl Read) -> Result { 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 { + fn read_from(buf: &mut impl Read) -> Result { let is_nonce = bool::read_from(buf)?; if is_nonce { Ok(NonceOrSaltSignature::Nonce(Vec::::read_from(buf)?)) -- cgit v1.2.3