From 567c6f4f2c39976d170111b816806453636f8241 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 1 May 2022 21:54:03 -0500 Subject: Reduce usage of AsyncRead We already receive everything from the server when it tells us the length, so we can actually just treat the stream as a Read instead of an AsyncRead. --- .../game/clientbound_declare_commands_packet.rs | 98 +++++++--------------- .../game/clientbound_player_abilities_packet.rs | 12 +-- .../packets/game/clientbound_player_info_packet.rs | 23 ++--- .../game/clientbound_player_position_packet.rs | 12 +-- .../src/packets/game/clientbound_recipe_packet.rs | 12 +-- .../game/clientbound_set_entity_data_packet.rs | 88 ++++++++----------- .../game/clientbound_update_attributes_packet.rs | 16 ++-- .../game/clientbound_update_recipes_packet.rs | 47 +++++------ .../packets/game/clientbound_update_tags_packet.rs | 32 +++---- .../clientbound_update_view_distance_packet.rs | 2 - .../game/serverbound_custom_payload_packet.rs | 2 - .../packets/handshake/client_intention_packet.rs | 24 +----- .../login/clientbound_game_profile_packet.rs | 16 ++-- .../src/packets/login/clientbound_hello_packet.rs | 12 ++- .../login/clientbound_login_compression_packet.rs | 8 +- azalea-protocol/src/packets/mod.rs | 21 ++--- .../status/clientbound_status_response_packet.rs | 8 +- 17 files changed, 146 insertions(+), 287 deletions(-) (limited to 'azalea-protocol/src/packets') 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 afaa7fdd..703811c0 100755 --- a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs @@ -1,9 +1,7 @@ use super::GamePacket; use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; -use async_trait::async_trait; use azalea_core::resource_location::ResourceLocation; -use std::hash::Hash; -use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt}; +use std::{hash::Hash, io::Read}; #[derive(Hash, Clone, Debug)] pub struct ClientboundDeclareCommandsPacket { @@ -20,16 +18,14 @@ impl ClientboundDeclareCommandsPacket { panic!("ClientboundDeclareCommandsPacket::write not implemented") } - pub async fn read( - buf: &mut T, - ) -> Result { - let node_count = buf.read_varint().await?; + pub fn read(buf: &mut T) -> Result { + let node_count = buf.read_varint()?; let mut nodes = Vec::with_capacity(node_count as usize); for _ in 0..node_count { - let node = BrigadierNodeStub::read_into(buf).await?; + let node = BrigadierNodeStub::read_into(buf)?; nodes.push(node); } - let root_index = buf.read_varint().await?; + let root_index = buf.read_varint()?; Ok(GamePacket::ClientboundDeclareCommandsPacket( ClientboundDeclareCommandsPacket { entries: nodes, @@ -47,20 +43,16 @@ pub struct BrigadierNumber { min: Option, max: Option, } -#[async_trait] -impl McBufReadable for BrigadierNumber { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let flags = buf.read_byte().await?; +impl McBufReadable for BrigadierNumber { + fn read_into(buf: &mut impl Read) -> Result { + let flags = buf.read_byte()?; let min = if flags & 0x01 != 0 { - Some(T::read_into(buf).await?) + Some(T::read_into(buf)?) } else { None }; let max = if flags & 0x02 != 0 { - Some(T::read_into(buf).await?) + Some(T::read_into(buf)?) } else { None }; @@ -97,13 +89,9 @@ pub enum BrigadierString { GreedyPhrase = 2, } -#[async_trait] impl McBufReadable for BrigadierString { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let id = buf.read_byte().await?; + fn read_into(buf: &mut impl Read) -> Result { + let id = buf.read_byte()?; Ok(match id { 0 => BrigadierString::SingleWord, 1 => BrigadierString::QuotablePhrase, @@ -171,38 +159,24 @@ pub enum BrigadierParser { Resource { registry_key: ResourceLocation }, } -#[async_trait] impl McBufReadable for BrigadierParser { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let parser = buf.read_resource_location().await?; + fn read_into(buf: &mut impl Read) -> Result { + let parser = buf.read_resource_location()?; if parser == ResourceLocation::new("brigadier:bool")? { Ok(BrigadierParser::Bool) } else if parser == ResourceLocation::new("brigadier:double")? { - Ok(BrigadierParser::Double( - BrigadierNumber::read_into(buf).await?, - )) + Ok(BrigadierParser::Double(BrigadierNumber::read_into(buf)?)) } else if parser == ResourceLocation::new("brigadier:float")? { - Ok(BrigadierParser::Float( - BrigadierNumber::read_into(buf).await?, - )) + Ok(BrigadierParser::Float(BrigadierNumber::read_into(buf)?)) } else if parser == ResourceLocation::new("brigadier:integer")? { - Ok(BrigadierParser::Integer( - BrigadierNumber::read_into(buf).await?, - )) + Ok(BrigadierParser::Integer(BrigadierNumber::read_into(buf)?)) } else if parser == ResourceLocation::new("brigadier:long")? { - Ok(BrigadierParser::Long( - BrigadierNumber::read_into(buf).await?, - )) + Ok(BrigadierParser::Long(BrigadierNumber::read_into(buf)?)) } else if parser == ResourceLocation::new("brigadier:string")? { - Ok(BrigadierParser::String( - BrigadierString::read_into(buf).await?, - )) + Ok(BrigadierParser::String(BrigadierString::read_into(buf)?)) } else if parser == ResourceLocation::new("minecraft:entity")? { - let flags = buf.read_byte().await?; + let flags = buf.read_byte()?; Ok(BrigadierParser::Entity { single: flags & 0x01 != 0, players_only: flags & 0x02 != 0, @@ -250,7 +224,7 @@ impl McBufReadable for BrigadierParser { } else if parser == ResourceLocation::new("minecraft:scoreboard_slot")? { Ok(BrigadierParser::ScoreboardSlot) } else if parser == ResourceLocation::new("minecraft:score_holder")? { - let flags = buf.read_byte().await?; + let flags = buf.read_byte()?; Ok(BrigadierParser::ScoreHolder { allows_multiple: flags & 0x01 != 0, }) @@ -270,7 +244,7 @@ impl McBufReadable for BrigadierParser { Ok(BrigadierParser::EntityAnchor) } else if parser == ResourceLocation::new("minecraft:range")? { Ok(BrigadierParser::Range { - decimals_allowed: buf.read_boolean().await?, + decimals_allowed: buf.read_boolean()?, }) } else if parser == ResourceLocation::new("minecraft:int_range")? { Ok(BrigadierParser::IntRange) @@ -292,11 +266,11 @@ impl McBufReadable for BrigadierParser { Ok(BrigadierParser::Time) } else if parser == ResourceLocation::new("minecraft:resource_or_tag")? { Ok(BrigadierParser::ResourceOrTag { - registry_key: buf.read_resource_location().await?, + registry_key: buf.read_resource_location()?, }) } else if parser == ResourceLocation::new("minecraft:resource")? { Ok(BrigadierParser::Resource { - registry_key: buf.read_resource_location().await?, + registry_key: buf.read_resource_location()?, }) } else { panic!("Unknown Brigadier parser: {}", parser) @@ -305,13 +279,9 @@ impl McBufReadable for BrigadierParser { } // azalea_brigadier::tree::CommandNode -#[async_trait] impl McBufReadable for BrigadierNodeStub { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let flags = u8::read_into(buf).await?; + fn read_into(buf: &mut impl Read) -> Result { + let flags = u8::read_into(buf)?; if flags > 31 { println!( "Warning: The flags from a Brigadier node are over 31. This is probably a bug." @@ -323,21 +293,17 @@ impl McBufReadable for BrigadierNodeStub { let has_redirect = flags & 0x08 != 0; let has_suggestions_type = flags & 0x10 != 0; - let children = buf.read_int_id_list().await?; - let redirect_node = if has_redirect { - buf.read_varint().await? - } else { - 0 - }; + let children = buf.read_int_id_list()?; + let redirect_node = if has_redirect { buf.read_varint()? } else { 0 }; // argument node if node_type == 2 { - let name = buf.read_utf().await?; + let name = buf.read_utf()?; - let parser = BrigadierParser::read_into(buf).await?; + let parser = BrigadierParser::read_into(buf)?; let suggestions_type = if has_suggestions_type { - Some(buf.read_resource_location().await?) + Some(buf.read_resource_location()?) } else { None }; @@ -345,7 +311,7 @@ impl McBufReadable for BrigadierNodeStub { } // literal node if node_type == 1 { - let name = buf.read_utf().await?; + let name = buf.read_utf()?; return Ok(BrigadierNodeStub {}); } Ok(BrigadierNodeStub {}) 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 f4f528cf..3ca1ac85 100755 --- a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs @@ -1,9 +1,7 @@ -// i don't know the actual name of this packet, i couldn't find it in the source code +use std::io::Read; use crate::mc_buf::{McBufReadable, McBufWritable, Readable}; -use async_trait::async_trait; use packet_macros::GamePacket; -use tokio::io::AsyncRead; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundPlayerAbilitiesPacket { @@ -21,13 +19,9 @@ pub struct PlayerAbilitiesFlags { pub instant_break: bool, } -#[async_trait] impl McBufReadable for PlayerAbilitiesFlags { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let byte = buf.read_byte().await?; + fn read_into(buf: &mut impl Read) -> Result { + let byte = buf.read_byte()?; Ok(PlayerAbilitiesFlags { invulnerable: byte & 1 != 0, flying: byte & 2 != 0, 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 3d4c3ac7..97b68259 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs @@ -1,10 +1,7 @@ -// i don't know the actual name of this packet, i couldn't find it in the source code - use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; -use async_trait::async_trait; use azalea_chat::component::Component; use packet_macros::{GamePacket, McBufReadable, McBufWritable}; -use tokio::io::AsyncRead; +use std::io::Read; use uuid::Uuid; #[derive(Clone, Debug, GamePacket)] @@ -64,19 +61,15 @@ pub struct RemovePlayer { uuid: Uuid, } -#[async_trait] impl McBufReadable for Action { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let id = buf.read_byte().await?; + fn read_into(buf: &mut impl Read) -> Result { + let id = buf.read_byte()?; Ok(match id { - 0 => Action::AddPlayer(Vec::::read_into(buf).await?), - 1 => Action::UpdateGameMode(Vec::::read_into(buf).await?), - 2 => Action::UpdateLatency(Vec::::read_into(buf).await?), - 3 => Action::UpdateDisplayName(Vec::::read_into(buf).await?), - 4 => Action::RemovePlayer(Vec::::read_into(buf).await?), + 0 => Action::AddPlayer(Vec::::read_into(buf)?), + 1 => Action::UpdateGameMode(Vec::::read_into(buf)?), + 2 => Action::UpdateLatency(Vec::::read_into(buf)?), + 3 => Action::UpdateDisplayName(Vec::::read_into(buf)?), + 4 => Action::RemovePlayer(Vec::::read_into(buf)?), _ => panic!("Unknown player info action id: {}", id), }) } 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 5bb40d84..e47ca9e1 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs @@ -1,9 +1,7 @@ -// i don't know the actual name of this packet, i couldn't find it in the source code +use std::io::Read; use crate::mc_buf::{McBufReadable, McBufWritable, Readable}; -use async_trait::async_trait; use packet_macros::GamePacket; -use tokio::io::AsyncRead; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundPlayerPositionPacket { @@ -29,13 +27,9 @@ pub struct RelativeArguments { pub x_rot: bool, } -#[async_trait] impl McBufReadable for RelativeArguments { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let byte = buf.read_byte().await?; + fn read_into(buf: &mut impl Read) -> Result { + let byte = buf.read_byte()?; Ok(RelativeArguments { x: byte & 0b1 != 0, y: byte & 0b10 != 0, diff --git a/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs b/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs index 69f26ddc..4847bbf8 100644 --- a/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs @@ -1,8 +1,6 @@ -use async_trait::async_trait; -use azalea_chat::component::Component; use azalea_core::{resource_location::ResourceLocation, Slot}; use packet_macros::{GamePacket, McBufReadable, McBufWritable}; -use tokio::io::AsyncRead; +use std::io::Read; use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; @@ -42,13 +40,9 @@ impl McBufWritable for State { Ok(()) } } -#[async_trait] impl McBufReadable for State { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let state = buf.read_varint().await?.try_into().unwrap(); + fn read_into(buf: &mut impl Read) -> Result { + let state = buf.read_varint()?.try_into().unwrap(); Ok(match state { 0 => State::Init, 1 => State::Add, diff --git a/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs index 4cc456f3..302d832a 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs @@ -1,12 +1,12 @@ +use std::io::Read; + use crate::{ mc_buf::{Readable, Writable}, packets::{McBufReadable, McBufWritable}, }; -use async_trait::async_trait; use azalea_chat::component::Component; use azalea_core::{BlockPos, Direction, Slot}; use packet_macros::{GamePacket, McBufReadable, McBufWritable}; -use tokio::io::AsyncRead; use uuid::Uuid; #[derive(Clone, Debug, GamePacket)] @@ -24,19 +24,15 @@ pub struct EntityDataItem { pub value: EntityDataValue, } -#[async_trait] impl McBufReadable for Vec { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { + fn read_into(buf: &mut impl Read) -> Result { let mut metadata = Vec::new(); loop { - let index = buf.read_byte().await?; + let index = buf.read_byte()?; if index == 0xff { break; } - let value = EntityDataValue::read_into(buf).await?; + let value = EntityDataValue::read_into(buf)?; metadata.push(EntityDataItem { index, value }); } Ok(metadata) @@ -81,51 +77,47 @@ pub enum EntityDataValue { Pose(Pose), } -#[async_trait] impl McBufReadable for EntityDataValue { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let type_ = buf.read_varint().await?; + fn read_into(buf: &mut impl Read) -> Result { + let type_ = buf.read_varint()?; Ok(match type_ { - 0 => EntityDataValue::Byte(buf.read_byte().await?), - 1 => EntityDataValue::Int(buf.read_varint().await?), - 2 => EntityDataValue::Float(buf.read_float().await?), - 3 => EntityDataValue::String(buf.read_utf().await?), - 4 => EntityDataValue::Component(Component::read_into(buf).await?), - 5 => EntityDataValue::OptionalComponent(Option::::read_into(buf).await?), - 6 => EntityDataValue::ItemStack(Slot::read_into(buf).await?), - 7 => EntityDataValue::Boolean(buf.read_boolean().await?), + 0 => EntityDataValue::Byte(buf.read_byte()?), + 1 => EntityDataValue::Int(buf.read_varint()?), + 2 => EntityDataValue::Float(buf.read_float()?), + 3 => EntityDataValue::String(buf.read_utf()?), + 4 => EntityDataValue::Component(Component::read_into(buf)?), + 5 => EntityDataValue::OptionalComponent(Option::::read_into(buf)?), + 6 => EntityDataValue::ItemStack(Slot::read_into(buf)?), + 7 => EntityDataValue::Boolean(buf.read_boolean()?), 8 => EntityDataValue::Rotations { - x: buf.read_float().await?, - y: buf.read_float().await?, - z: buf.read_float().await?, + x: buf.read_float()?, + y: buf.read_float()?, + z: buf.read_float()?, }, - 9 => EntityDataValue::BlockPos(BlockPos::read_into(buf).await?), - 10 => EntityDataValue::OptionalBlockPos(Option::::read_into(buf).await?), - 11 => EntityDataValue::Direction(Direction::read_into(buf).await?), - 12 => EntityDataValue::OptionalUuid(Option::::read_into(buf).await?), + 9 => EntityDataValue::BlockPos(BlockPos::read_into(buf)?), + 10 => EntityDataValue::OptionalBlockPos(Option::::read_into(buf)?), + 11 => EntityDataValue::Direction(Direction::read_into(buf)?), + 12 => EntityDataValue::OptionalUuid(Option::::read_into(buf)?), 13 => EntityDataValue::OptionalBlockState({ - let val = i32::read_into(buf).await?; + let val = i32::read_into(buf)?; if val == 0 { None } else { Some(val) } }), - 14 => EntityDataValue::CompoundTag(azalea_nbt::Tag::read_into(buf).await?), - 15 => EntityDataValue::Particle(Particle::read_into(buf).await?), - 16 => EntityDataValue::VillagerData(VillagerData::read_into(buf).await?), + 14 => EntityDataValue::CompoundTag(azalea_nbt::Tag::read_into(buf)?), + 15 => EntityDataValue::Particle(Particle::read_into(buf)?), + 16 => EntityDataValue::VillagerData(VillagerData::read_into(buf)?), 17 => EntityDataValue::OptionalUnsignedInt({ - let val = buf.read_varint().await?; + let val = buf.read_varint()?; if val == 0 { None } else { Some((val - 1) as u32) } }), - 18 => EntityDataValue::Pose(Pose::read_into(buf).await?), + 18 => EntityDataValue::Pose(Pose::read_into(buf)?), _ => return Err(format!("Unknown entity data type: {}", type_)), }) } @@ -309,18 +301,14 @@ pub struct VibrationParticle { pub ticks: u32, } -#[async_trait] impl McBufReadable for ParticleData { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let id = buf.read_varint().await?; + fn read_into(buf: &mut impl Read) -> Result { + let id = buf.read_varint()?; Ok(match id { 0 => ParticleData::AmbientEntityEffect, 1 => ParticleData::AngryVillager, - 2 => ParticleData::Block(BlockParticle::read_into(buf).await?), - 3 => ParticleData::BlockMarker(BlockParticle::read_into(buf).await?), + 2 => ParticleData::Block(BlockParticle::read_into(buf)?), + 3 => ParticleData::BlockMarker(BlockParticle::read_into(buf)?), 4 => ParticleData::Bubble, 5 => ParticleData::Cloud, 6 => ParticleData::Crit, @@ -331,10 +319,8 @@ impl McBufReadable for ParticleData { 11 => ParticleData::LandingLava, 12 => ParticleData::DrippingWater, 13 => ParticleData::FallingWater, - 14 => ParticleData::Dust(DustParticle::read_into(buf).await?), - 15 => ParticleData::DustColorTransition( - DustColorTransitionParticle::read_into(buf).await?, - ), + 14 => ParticleData::Dust(DustParticle::read_into(buf)?), + 15 => ParticleData::DustColorTransition(DustColorTransitionParticle::read_into(buf)?), 16 => ParticleData::Effect, 17 => ParticleData::ElderGuardian, 18 => ParticleData::EnchantedHit, @@ -343,7 +329,7 @@ impl McBufReadable for ParticleData { 21 => ParticleData::EntityEffect, 22 => ParticleData::ExplosionEmitter, 23 => ParticleData::Explosion, - 24 => ParticleData::FallingDust(BlockParticle::read_into(buf).await?), + 24 => ParticleData::FallingDust(BlockParticle::read_into(buf)?), 25 => ParticleData::Firework, 26 => ParticleData::Fishing, 27 => ParticleData::Flame, @@ -354,8 +340,8 @@ impl McBufReadable for ParticleData { 32 => ParticleData::Composter, 33 => ParticleData::Heart, 34 => ParticleData::InstantEffect, - 35 => ParticleData::Item(ItemParticle::read_into(buf).await?), - 36 => ParticleData::Vibration(VibrationParticle::read_into(buf).await?), + 35 => ParticleData::Item(ItemParticle::read_into(buf)?), + 36 => ParticleData::Vibration(VibrationParticle::read_into(buf)?), 37 => ParticleData::ItemSlime, 38 => ParticleData::ItemSnowball, 39 => ParticleData::LargeSmoke, 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 d7f86931..6e0aae29 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs @@ -1,11 +1,9 @@ -use async_trait::async_trait; -use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; +use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_core::resource_location::ResourceLocation; use packet_macros::{GamePacket, McBufReadable, McBufWritable}; -use tokio::io::AsyncRead; +use std::io::Read; use uuid::Uuid; -use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; - #[derive(Clone, Debug, GamePacket)] pub struct ClientboundUpdateAttributesPacket { #[varint] @@ -34,13 +32,9 @@ enum Operation { MultiplyTotal = 2, } -#[async_trait] impl McBufReadable for Operation { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - match buf.read_byte().await? { + fn read_into(buf: &mut impl Read) -> Result { + match buf.read_byte()? { 0 => Ok(Operation::Addition), 1 => Ok(Operation::MultiplyBase), 2 => Ok(Operation::MultiplyTotal), 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 5ae06a6f..4b1a322a 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs @@ -1,8 +1,7 @@ -use async_trait::async_trait; -use azalea_chat::component::Component; +use std::io::Read; + use azalea_core::{resource_location::ResourceLocation, Slot}; use packet_macros::{GamePacket, McBufReadable, McBufWritable}; -use tokio::io::AsyncRead; use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; @@ -48,20 +47,16 @@ impl McBufWritable for ShapedRecipe { Ok(()) } } -#[async_trait] impl McBufReadable for ShapedRecipe { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let width = buf.read_varint().await?.try_into().unwrap(); - let height = buf.read_varint().await?.try_into().unwrap(); - let group = buf.read_utf().await?; + fn read_into(buf: &mut impl Read) -> Result { + let width = buf.read_varint()?.try_into().unwrap(); + let height = buf.read_varint()?.try_into().unwrap(); + let group = buf.read_utf()?; let mut ingredients = Vec::with_capacity(width * height); for _ in 0..width * height { - ingredients.push(Ingredient::read_into(buf).await?); + ingredients.push(Ingredient::read_into(buf)?); } - let result = Slot::read_into(buf).await?; + let result = Slot::read_into(buf)?; Ok(ShapedRecipe { width, @@ -132,22 +127,18 @@ impl McBufWritable for Recipe { } } -#[async_trait] impl McBufReadable for Recipe { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let recipe_type = buf.read_resource_location().await?; - let identifier = buf.read_resource_location().await?; + fn read_into(buf: &mut impl Read) -> Result { + let recipe_type = buf.read_resource_location()?; + let identifier = buf.read_resource_location()?; // rust doesn't let us match ResourceLocation so we have to do a big // if-else chain :( let data = if recipe_type == ResourceLocation::new("minecraft:crafting_shapeless").unwrap() { - RecipeData::CraftingShapeless(ShapelessRecipe::read_into(buf).await?) + RecipeData::CraftingShapeless(ShapelessRecipe::read_into(buf)?) } else if recipe_type == ResourceLocation::new("minecraft:crafting_shaped").unwrap() { - RecipeData::CraftingShaped(ShapedRecipe::read_into(buf).await?) + RecipeData::CraftingShaped(ShapedRecipe::read_into(buf)?) } else if recipe_type == ResourceLocation::new("minecraft:crafting_special_armordye").unwrap() { @@ -205,17 +196,17 @@ impl McBufReadable for Recipe { { RecipeData::CraftingSpecialSuspiciousStew } else if recipe_type == ResourceLocation::new("minecraft:smelting").unwrap() { - RecipeData::Smelting(CookingRecipe::read_into(buf).await?) + RecipeData::Smelting(CookingRecipe::read_into(buf)?) } else if recipe_type == ResourceLocation::new("minecraft:blasting").unwrap() { - RecipeData::Blasting(CookingRecipe::read_into(buf).await?) + RecipeData::Blasting(CookingRecipe::read_into(buf)?) } else if recipe_type == ResourceLocation::new("minecraft:smoking").unwrap() { - RecipeData::Smoking(CookingRecipe::read_into(buf).await?) + RecipeData::Smoking(CookingRecipe::read_into(buf)?) } else if recipe_type == ResourceLocation::new("minecraft:campfire_cooking").unwrap() { - RecipeData::CampfireCooking(CookingRecipe::read_into(buf).await?) + RecipeData::CampfireCooking(CookingRecipe::read_into(buf)?) } else if recipe_type == ResourceLocation::new("minecraft:stonecutting").unwrap() { - RecipeData::Stonecutting(StoneCuttingRecipe::read_into(buf).await?) + RecipeData::Stonecutting(StoneCuttingRecipe::read_into(buf)?) } else if recipe_type == ResourceLocation::new("minecraft:smithing").unwrap() { - RecipeData::Smithing(SmithingRecipe::read_into(buf).await?) + RecipeData::Smithing(SmithingRecipe::read_into(buf)?) } else { panic!("Unknown recipe type sent by server: {}", recipe_type); }; 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 b6046948..4646c2d3 100755 --- a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs @@ -1,11 +1,7 @@ -use std::collections::HashMap; - -use async_trait::async_trait; +use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_core::resource_location::ResourceLocation; use packet_macros::GamePacket; -use tokio::io::AsyncRead; - -use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use std::{collections::HashMap, io::Read}; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundUpdateTagsPacket { @@ -18,20 +14,16 @@ pub struct Tags { pub elements: Vec, } -#[async_trait] impl McBufReadable for HashMap> { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let length = buf.read_varint().await? as usize; + fn read_into(buf: &mut impl Read) -> Result { + let length = buf.read_varint()? as usize; let mut data = HashMap::with_capacity(length); for _ in 0..length { - let tag_type = buf.read_resource_location().await?; - let tags_count = buf.read_varint().await? as usize; + let tag_type = buf.read_resource_location()?; + let tags_count = buf.read_varint()? as usize; let mut tags_vec = Vec::with_capacity(tags_count); for _ in 0..tags_count { - let tags = Tags::read_into(buf).await?; + let tags = Tags::read_into(buf)?; tags_vec.push(tags); } data.insert(tag_type, tags_vec); @@ -50,14 +42,10 @@ impl McBufWritable for HashMap> { Ok(()) } } -#[async_trait] impl McBufReadable for Tags { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let name = buf.read_resource_location().await?; - let elements = buf.read_int_id_list().await?; + fn read_into(buf: &mut impl Read) -> Result { + let name = buf.read_resource_location()?; + let elements = buf.read_int_id_list()?; Ok(Tags { name, elements }) } } diff --git a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs index 8301c089..1f988fe5 100755 --- a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs @@ -1,5 +1,3 @@ -// i don't know the actual name of this packet, i couldn't find it in the source code - use packet_macros::GamePacket; #[derive(Clone, Debug, GamePacket)] diff --git a/azalea-protocol/src/packets/game/serverbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/serverbound_custom_payload_packet.rs index 43ddb700..eefafdd1 100644 --- a/azalea-protocol/src/packets/game/serverbound_custom_payload_packet.rs +++ b/azalea-protocol/src/packets/game/serverbound_custom_payload_packet.rs @@ -1,5 +1,3 @@ -// i don't know the actual name of this packet, i couldn't find it in the source code - use crate::mc_buf::UnsizedByteArray; use azalea_core::resource_location::ResourceLocation; use packet_macros::GamePacket; diff --git a/azalea-protocol/src/packets/handshake/client_intention_packet.rs b/azalea-protocol/src/packets/handshake/client_intention_packet.rs index a92d65f6..6216ddc4 100755 --- a/azalea-protocol/src/packets/handshake/client_intention_packet.rs +++ b/azalea-protocol/src/packets/handshake/client_intention_packet.rs @@ -1,9 +1,7 @@ -use crate::{mc_buf::Writable, packets::ConnectionProtocol}; +use crate::packets::ConnectionProtocol; use packet_macros::HandshakePacket; use std::hash::Hash; -use super::HandshakePacket; - #[derive(Hash, Clone, Debug, HandshakePacket)] pub struct ClientIntentionPacket { #[varint] @@ -12,23 +10,3 @@ pub struct ClientIntentionPacket { pub port: u16, pub intention: ConnectionProtocol, } - -// impl ClientIntentionPacket { -// pub fn get(self) -> HandshakePacket { -// HandshakePacket::ClientIntentionPacket(self) -// } - -// pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { -// buf.write_varint(self.protocol_version as i32)?; -// buf.write_utf(&self.hostname)?; -// buf.write_short(self.port as i16)?; -// buf.write_varint(self.intention as i32)?; -// Ok(()) -// } - -// pub async fn read( -// buf: &mut T, -// ) -> Result { -// todo!() -// } -// } 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 ccf0f482..bcdcd105 100755 --- a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs @@ -1,3 +1,5 @@ +use std::io::Read; + use super::LoginPacket; use crate::mc_buf::{Readable, Writable}; use azalea_auth::game_profile::GameProfile; @@ -23,17 +25,15 @@ impl ClientboundGameProfilePacket { Ok(()) } - pub async fn read( - buf: &mut T, - ) -> Result { + pub fn read(buf: &mut impl Read) -> Result { // TODO: we have a thing to read from the uuid now let uuid = Uuid::from_int_array([ - buf.read_int().await? as u32, - buf.read_int().await? as u32, - buf.read_int().await? as u32, - buf.read_int().await? as u32, + buf.read_int()? as u32, + buf.read_int()? as u32, + buf.read_int()? as u32, + buf.read_int()? as u32, ]); - let name = buf.read_utf_with_len(16).await?; + let name = buf.read_utf_with_len(16)?; Ok(ClientboundGameProfilePacket { game_profile: GameProfile::new(uuid, name), } diff --git a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs index 20af1bec..06f346c2 100755 --- a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs @@ -1,4 +1,4 @@ -use std::hash::Hash; +use std::{hash::Hash, io::Read}; use super::LoginPacket; use crate::mc_buf::Readable; @@ -19,12 +19,10 @@ impl ClientboundHelloPacket { panic!("ClientboundHelloPacket::write not implemented") } - pub async fn read( - buf: &mut T, - ) -> Result { - let server_id = buf.read_utf_with_len(20).await?; - let public_key = buf.read_byte_array().await?; - let nonce = buf.read_byte_array().await?; + pub fn read(buf: &mut impl Read) -> Result { + let server_id = buf.read_utf_with_len(20)?; + let public_key = buf.read_byte_array()?; + let nonce = buf.read_byte_array()?; Ok(ClientboundHelloPacket { server_id, 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 a88c6cbf..a5ab78bb 100755 --- a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs @@ -1,4 +1,4 @@ -use std::hash::Hash; +use std::{hash::Hash, io::Read}; use crate::mc_buf::{Readable, Writable}; @@ -19,10 +19,8 @@ impl ClientboundLoginCompressionPacket { Ok(()) } - pub async fn read( - buf: &mut T, - ) -> Result { - let compression_threshold = buf.read_varint().await?; + pub fn read(buf: &mut impl Read) -> Result { + let compression_threshold = buf.read_varint()?; Ok(ClientboundLoginCompressionPacket { compression_threshold, diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index 98741a75..67fa92e9 100755 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -3,14 +3,14 @@ pub mod handshake; pub mod login; pub mod status; +use std::io::Read; + use crate::{ connect::PacketFlow, mc_buf::{McBufReadable, McBufWritable, Readable, Writable}, }; -use async_trait::async_trait; use num_derive::FromPrimitive; use num_traits::FromPrimitive; -use tokio::io::AsyncRead; pub const PROTOCOL_VERSION: u32 = 758; @@ -31,7 +31,6 @@ pub enum Packet { } /// An enum of packets for a certain protocol -#[async_trait] pub trait ProtocolPacket where Self: Sized, @@ -39,24 +38,14 @@ where fn id(&self) -> u32; /// Read a packet by its id, ConnectionProtocol, and flow - async fn read( - id: u32, - flow: &PacketFlow, - buf: &mut T, - ) -> Result - where - Self: Sized; + fn read(id: u32, flow: &PacketFlow, buf: &mut impl Read) -> Result; fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error>; } -#[async_trait] impl McBufReadable for ConnectionProtocol { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - ConnectionProtocol::from_i32(buf.read_varint().await?) + fn read_into(buf: &mut impl Read) -> Result { + ConnectionProtocol::from_i32(buf.read_varint()?) .ok_or_else(|| "Invalid intention".to_string()) } } 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 884cf408..f5789ae3 100755 --- a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -1,3 +1,5 @@ +use std::io::Read; + use azalea_chat::component::Component; use serde::Deserialize; use serde_json::Value; @@ -43,10 +45,8 @@ impl ClientboundStatusResponsePacket { Ok(()) } - pub async fn read( - buf: &mut T, - ) -> Result { - let status_string = buf.read_utf().await?; + pub fn read(buf: &mut impl Read) -> Result { + 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"); -- cgit v1.2.3