From e5fcfa119309877ab515b921f8ada5f1b1ec4c30 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 26 Apr 2022 15:33:41 +0000 Subject: [PATCH] default implementation for read and write Vec --- azalea-client/src/connect.rs | 3 ++ azalea-protocol/src/mc_buf/mod.rs | 11 +++-- azalea-protocol/src/mc_buf/read.rs | 38 ++++++---------- azalea-protocol/src/mc_buf/write.rs | 20 +++------ .../game/clientbound_custom_payload_packet.rs | 3 +- .../game/clientbound_update_recipes_packet.rs | 44 ------------------- .../game/clientbound_update_tags_packet.rs | 27 ------------ .../login/clientbound_custom_query_packet.rs | 3 +- .../packets/login/clientbound_hello_packet.rs | 6 +-- .../packets/login/serverbound_key_packet.rs | 6 +-- 10 files changed, 37 insertions(+), 124 deletions(-) diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index 44eff33f..f441f2f3 100755 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -114,6 +114,9 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { GamePacket::ClientboundDisconnectPacket(p) => { println!("Got login disconnect packet {:?}", p); } + GamePacket::ClientboundUpdateRecipesPacket(p) => { + println!("Got update recipes packet {:?}", p); + } }, Err(e) => { panic!("Error: {:?}", e); diff --git a/azalea-protocol/src/mc_buf/mod.rs b/azalea-protocol/src/mc_buf/mod.rs index 3ba6ac3e..3ab6e761 100755 --- a/azalea-protocol/src/mc_buf/mod.rs +++ b/azalea-protocol/src/mc_buf/mod.rs @@ -4,18 +4,18 @@ mod read; mod write; pub use read::{McBufReadable, McBufVarintReadable, Readable}; -pub use write::{McBufVarintWritable, McBufWritable, Writable}; use std::ops::Deref; +pub use write::{McBufVarintWritable, McBufWritable, Writable}; // const DEFAULT_NBT_QUOTA: u32 = 2097152; const MAX_STRING_LENGTH: u16 = 32767; // const MAX_COMPONENT_STRING_LENGTH: u32 = 262144; - +/// A Vec that isn't prefixed by a VarInt with the size. #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct ByteArray(Vec); +pub struct UnsizedByteArray(Vec); -impl Deref for ByteArray { +impl Deref for UnsizedByteArray { type Target = Vec; fn deref(&self) -> &Self::Target { @@ -23,13 +23,12 @@ impl Deref for ByteArray { } } -impl From> for ByteArray { +impl From> for UnsizedByteArray { fn from(vec: Vec) -> Self { Self(vec) } } - #[cfg(test)] mod tests { use super::*; diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 1e7db1dd..4c126b7e 100755 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -1,4 +1,3 @@ -use crate::mc_buf::ByteArray; use async_trait::async_trait; use azalea_chat::component::Component; use azalea_core::{ @@ -8,7 +7,7 @@ use azalea_core::{ use serde::Deserialize; use tokio::io::{AsyncRead, AsyncReadExt}; -use super::MAX_STRING_LENGTH; +use super::{UnsizedByteArray, MAX_STRING_LENGTH}; #[async_trait] pub trait Readable { @@ -16,7 +15,7 @@ pub trait Readable { async fn read_varint(&mut self) -> Result; fn get_varint_size(&mut self, value: i32) -> u8; fn get_varlong_size(&mut self, value: i32) -> u8; - async fn read_byte_array(&mut self) -> Result; + async fn read_byte_array(&mut self) -> Result, String>; async fn read_bytes_with_len(&mut self, n: usize) -> Result, String>; async fn read_bytes(&mut self) -> Result, String>; async fn read_utf(&mut self) -> Result; @@ -82,9 +81,9 @@ where 10 } - async fn read_byte_array(&mut self) -> Result { + async fn read_byte_array(&mut self) -> Result, String> { let length = self.read_varint().await? as usize; - Ok(ByteArray(self.read_bytes_with_len(length).await?)) + self.read_bytes_with_len(length).await } async fn read_bytes_with_len(&mut self, n: usize) -> Result, String> { @@ -244,22 +243,27 @@ impl McBufVarintReadable for i32 { } #[async_trait] -impl McBufReadable for Vec { +impl McBufReadable for UnsizedByteArray { async fn read_into(buf: &mut R) -> Result where R: AsyncRead + std::marker::Unpin + std::marker::Send, { - buf.read_bytes().await + Ok(UnsizedByteArray(buf.read_bytes().await?)) } } #[async_trait] -impl McBufReadable for ByteArray { +impl McBufReadable for Vec { async fn read_into(buf: &mut R) -> Result where R: AsyncRead + std::marker::Unpin + std::marker::Send, { - buf.read_byte_array().await + let length = buf.read_varint().await? as usize; + let mut contents = Vec::with_capacity(length); + for _ in 0..length { + contents.push(T::read_into(buf).await?); + } + Ok(contents) } } @@ -417,22 +421,6 @@ impl McBufReadable for Option { } } -// Vec -#[async_trait] -impl McBufReadable for Vec { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let mut vec = Vec::new(); - let length = buf.read_varint().await?; - for _ in 0..length { - vec.push(buf.read_resource_location().await?); - } - Ok(vec) - } -} - // azalea_nbt::Tag #[async_trait] impl McBufReadable for azalea_nbt::Tag { diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index f1362402..e8845f25 100755 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -1,5 +1,4 @@ -use super::MAX_STRING_LENGTH; -use crate::mc_buf::ByteArray; +use super::{UnsizedByteArray, MAX_STRING_LENGTH}; use async_trait::async_trait; use azalea_chat::component::Component; use azalea_core::{ @@ -187,15 +186,17 @@ impl McBufVarintWritable for i32 { } } -impl McBufWritable for Vec { +impl McBufWritable for UnsizedByteArray { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { buf.write_bytes(self) } } -impl McBufWritable for ByteArray { +// TODO: use specialization when that gets stabilized into rust +// to optimize for Vec byte arrays +impl McBufWritable for Vec { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_byte_array(&self) + buf.write_list(self, |buf, i| T::write_into(i, buf)) } } @@ -304,15 +305,6 @@ impl McBufWritable for Option { } } -// Vec -impl McBufWritable for Vec { - fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_list(self, |buf, resource_location| { - buf.write_resource_location(resource_location) - }) - } -} - // azalea_nbt::Tag impl McBufWritable for azalea_nbt::Tag { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { diff --git a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs index 134a3109..2c56ea2b 100755 --- a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs @@ -1,8 +1,9 @@ +use crate::mc_buf::UnsizedByteArray; use azalea_core::resource_location::ResourceLocation; use packet_macros::GamePacket; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundCustomPayloadPacket { pub identifier: ResourceLocation, - pub data: Vec, + pub data: UnsizedByteArray, } 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 558b74c7..2e8532df 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs @@ -88,47 +88,3 @@ impl McBufReadable for Ingredient { Ok(ingredient) } } - -impl McBufWritable for Vec { - fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(self.len() as i32)?; - for recipe in self { - recipe.write_into(buf)?; - } - Ok(()) - } -} -#[async_trait] -impl McBufReadable for Vec { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let recipe_count = buf.read_varint().await?; - let mut recipes = Vec::with_capacity(recipe_count as usize); - for _ in 0..recipe_count { - recipes.push(Recipe::read_into(buf).await?); - } - Ok(recipes) - } -} - -impl McBufWritable for Vec { - fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(self.len() as i32)?; - for ingredient in self { - ingredient.write_into(buf)?; - } - Ok(()) - } -} - -#[async_trait] -impl McBufReadable for Vec { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - todo!() - } -} 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 a2f17370..66eee4b6 100755 --- a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs @@ -54,33 +54,6 @@ impl McBufWritable for HashMap> { Ok(()) } } - -#[async_trait] -impl McBufReadable for Vec { - async fn read_into(buf: &mut R) -> Result - where - R: AsyncRead + std::marker::Unpin + std::marker::Send, - { - let tags_count = buf.read_varint().await? as usize; - let mut tags_vec = Vec::with_capacity(tags_count); - for _ in 0..tags_count { - let tags = Tags::read_into(buf).await?; - tags_vec.push(tags); - } - Ok(tags_vec) - } -} - -impl McBufWritable for Vec { - fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(self.len() as i32)?; - for tag in self { - tag.write_into(buf)?; - } - Ok(()) - } -} - #[async_trait] impl McBufReadable for Tags { async fn read_into(buf: &mut R) -> Result diff --git a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs index 3138106e..9e1e1df5 100755 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -1,3 +1,4 @@ +use crate::mc_buf::UnsizedByteArray; use azalea_core::resource_location::ResourceLocation; use packet_macros::LoginPacket; use std::hash::Hash; @@ -7,5 +8,5 @@ pub struct ClientboundCustomQueryPacket { #[varint] pub transaction_id: u32, pub identifier: ResourceLocation, - pub data: Vec, + pub data: UnsizedByteArray, } diff --git a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs index d36f1335..20af1bec 100755 --- a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs @@ -1,13 +1,13 @@ use std::hash::Hash; use super::LoginPacket; -use crate::mc_buf::{ByteArray, Readable}; +use crate::mc_buf::Readable; #[derive(Hash, Clone, Debug)] pub struct ClientboundHelloPacket { pub server_id: String, - pub public_key: ByteArray, - pub nonce: ByteArray, + pub public_key: Vec, + pub nonce: Vec, } impl ClientboundHelloPacket { diff --git a/azalea-protocol/src/packets/login/serverbound_key_packet.rs b/azalea-protocol/src/packets/login/serverbound_key_packet.rs index 3750331f..f402d357 100644 --- a/azalea-protocol/src/packets/login/serverbound_key_packet.rs +++ b/azalea-protocol/src/packets/login/serverbound_key_packet.rs @@ -1,10 +1,10 @@ use super::LoginPacket; -use crate::mc_buf::{ByteArray, Writable}; +use crate::mc_buf::Writable; use packet_macros::LoginPacket; use std::hash::Hash; #[derive(Hash, Clone, Debug, LoginPacket)] pub struct ServerboundKeyPacket { - pub shared_secret: ByteArray, - pub nonce: ByteArray, + pub shared_secret: Vec, + pub nonce: Vec, }