diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-09-02 12:11:14 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-02 12:11:14 -0500 |
| commit | cfb190d00c70f1b09789e23f89a3c67840e0fd87 (patch) | |
| tree | 9bdc021943753d60bf437526c4c294275eae13ac /azalea-protocol/src | |
| parent | 32458d743f757da3193717fe5554f490703640c0 (diff) | |
| download | azalea-drasl-cfb190d00c70f1b09789e23f89a3c67840e0fd87.tar.xz | |
get rid of Readable & Writable (#21)
Diffstat (limited to 'azalea-protocol/src')
12 files changed, 59 insertions, 58 deletions
diff --git a/azalea-protocol/src/packets/game/clientbound_commands_packet.rs b/azalea-protocol/src/packets/game/clientbound_commands_packet.rs index f3ae4ab9..1f0aa24f 100644 --- a/azalea-protocol/src/packets/game/clientbound_commands_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_commands_packet.rs @@ -1,7 +1,7 @@ use azalea_buf::BufReadError; use azalea_buf::McBuf; use azalea_buf::McBufVarReadable; -use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_buf::{McBufReadable, McBufWritable}; use azalea_core::ResourceLocation; use packet_macros::ClientboundGamePacket; use std::{ @@ -26,7 +26,7 @@ pub struct BrigadierNumber<T> { } impl<T: McBufReadable> McBufReadable for BrigadierNumber<T> { fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { - let flags = buf.read_byte()?; + let flags = u8::read_from(buf)?; let min = if flags & 0x01 != 0 { Some(T::read_from(buf)?) } else { @@ -42,14 +42,14 @@ impl<T: McBufReadable> McBufReadable for BrigadierNumber<T> { } impl<T: McBufWritable> McBufWritable for BrigadierNumber<T> { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - let mut flags = 0; + let mut flags: u8 = 0; if self.min.is_some() { flags |= 0x01; } if self.max.is_some() { flags |= 0x02; } - buf.write_byte(flags)?; + flags.write_into(buf)?; if let Some(min) = &self.min { min.write_into(buf)?; } @@ -135,7 +135,7 @@ impl McBufReadable for BrigadierParser { 4 => Ok(BrigadierParser::Long(BrigadierNumber::read_from(buf)?)), 5 => Ok(BrigadierParser::String(BrigadierString::read_from(buf)?)), 6 => { - let flags = buf.read_byte()?; + let flags = u8::read_from(buf)?; Ok(BrigadierParser::Entity { single: flags & 0x01 != 0, players_only: flags & 0x02 != 0, @@ -164,7 +164,7 @@ impl McBufReadable for BrigadierParser { 27 => Ok(BrigadierParser::Rotation), 28 => Ok(BrigadierParser::ScoreboardSlot), 29 => { - let flags = buf.read_byte()?; + let flags = u8::read_from(buf)?; Ok(BrigadierParser::ScoreHolder { allows_multiple: flags & 0x01 != 0, }) @@ -213,12 +213,16 @@ impl McBufReadable for BrigadierNodeStub { let has_redirect = flags & 0x08 != 0; let has_suggestions_type = flags & 0x10 != 0; - let _children = buf.read_int_id_list()?; - let _redirect_node = if has_redirect { buf.read_varint()? } else { 0 }; + let _children = Vec::<i32>::var_read_from(buf)?; + let _redirect_node = if has_redirect { + i32::var_read_from(buf)? + } else { + 0 + }; // argument node if node_type == 2 { - let _name = buf.read_utf()?; + let _name = String::read_from(buf)?; let _parser = BrigadierParser::read_from(buf)?; let _suggestions_type = if has_suggestions_type { Some(ResourceLocation::read_from(buf)?) @@ -229,7 +233,7 @@ impl McBufReadable for BrigadierNodeStub { } // literal node if node_type == 1 { - let _name = buf.read_utf()?; + let _name = String::read_from(buf)?; 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 5ab187ae..997b8ed7 100755 --- a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::{BufReadError, McBuf}; -use azalea_buf::{McBufReadable, McBufWritable, Readable}; +use azalea_buf::{McBufReadable, McBufWritable}; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -21,7 +21,7 @@ pub struct PlayerAbilitiesFlags { impl McBufReadable for PlayerAbilitiesFlags { fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { - let byte = buf.read_byte()?; + let byte = u8::read_from(buf)?; 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 4c61d5c7..85e285b6 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs @@ -1,6 +1,6 @@ use crate::packets::login::serverbound_hello_packet::ProfilePublicKeyData; use azalea_buf::{BufReadError, McBuf}; -use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_buf::{McBufReadable, McBufWritable}; use azalea_chat::component::Component; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -66,7 +66,7 @@ pub struct RemovePlayer { impl McBufReadable for Action { fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { - let id = buf.read_byte()?; + let id = u8::read_from(buf)?; Ok(match id { 0 => Action::AddPlayer(Vec::<AddPlayer>::read_from(buf)?), 1 => Action::UpdateGameMode(Vec::<UpdateGameMode>::read_from(buf)?), @@ -79,13 +79,14 @@ impl McBufReadable for Action { } impl McBufWritable for Action { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_byte(match self { + let id: u8 = match self { Action::AddPlayer(_) => 0, Action::UpdateGameMode(_) => 1, Action::UpdateLatency(_) => 2, Action::UpdateDisplayName(_) => 3, Action::RemovePlayer(_) => 4, - })?; + }; + id.write_into(buf)?; match self { Action::AddPlayer(players) => players.write_into(buf)?, Action::UpdateGameMode(players) => players.write_into(buf)?, 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 d4c9dcb5..8396b0ef 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::{BufReadError, McBuf}; -use azalea_buf::{McBufReadable, McBufWritable, Readable}; +use azalea_buf::{McBufReadable, McBufWritable}; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -29,7 +29,7 @@ pub struct RelativeArguments { impl McBufReadable for RelativeArguments { fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { - let byte = buf.read_byte()?; + let byte = u8::read_from(buf)?; Ok(RelativeArguments { x: byte & 0b1 != 0, y: byte & 0b10 != 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 6bb41a81..518b402c 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::{BufReadError, McBuf}; -use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_buf::{McBufReadable, McBufWritable}; use azalea_core::ResourceLocation; use packet_macros::ClientboundGamePacket; use std::io::{Read, Write}; @@ -35,7 +35,7 @@ enum Operation { impl McBufReadable for Operation { fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { - match buf.read_byte()? { + match u8::read_from(buf)? { 0 => Ok(Operation::Addition), 1 => Ok(Operation::MultiplyBase), 2 => Ok(Operation::MultiplyTotal), @@ -46,7 +46,7 @@ impl McBufReadable for Operation { impl McBufWritable for Operation { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_byte(*self as u8)?; + (*self as u8).write_into(buf)?; Ok(()) } } 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 1b10b221..b48877fd 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs @@ -1,10 +1,9 @@ -use std::io::{Read, Write}; - -use azalea_buf::{BufReadError, McBuf}; +use azalea_buf::{ + BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable, +}; use azalea_core::{ResourceLocation, Slot}; use packet_macros::ClientboundGamePacket; - -use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use std::io::{Read, Write}; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] pub struct ClientboundUpdateRecipesPacket { @@ -37,9 +36,9 @@ pub struct ShapedRecipe { impl McBufWritable for ShapedRecipe { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_varint(self.width.try_into().unwrap())?; - buf.write_varint(self.height.try_into().unwrap())?; - buf.write_utf(&self.group)?; + (self.width as u32).var_write_into(buf)?; + (self.height as u32).var_write_into(buf)?; + self.group.write_into(buf)?; for ingredient in &self.ingredients { ingredient.write_into(buf)?; } @@ -50,9 +49,9 @@ impl McBufWritable for ShapedRecipe { } impl McBufReadable for ShapedRecipe { 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()?; + let width = u32::var_read_from(buf)?.try_into().unwrap(); + let height = u32::var_read_from(buf)?.try_into().unwrap(); + let group = String::read_from(buf)?; let mut ingredients = Vec::with_capacity(width * height); for _ in 0..width * height { ingredients.push(Ingredient::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 6f0fe778..f698a22b 100755 --- a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs @@ -1,5 +1,5 @@ -use azalea_buf::{BufReadError, McBuf}; -use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_buf::{BufReadError, McBuf, McBufVarReadable, McBufVarWritable}; +use azalea_buf::{McBufReadable, McBufWritable}; use azalea_core::ResourceLocation; use packet_macros::ClientboundGamePacket; use std::ops::Deref; @@ -24,11 +24,11 @@ pub struct TagMap(HashMap<ResourceLocation, Vec<Tags>>); impl McBufReadable for TagMap { fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { - let length = buf.read_varint()? as usize; + let length = u32::var_read_from(buf)? as usize; let mut data = HashMap::with_capacity(length); for _ in 0..length { let tag_type = ResourceLocation::read_from(buf)?; - let tags_count = buf.read_varint()? as usize; + let tags_count = i32::var_read_from(buf)? as usize; let mut tags_vec = Vec::with_capacity(tags_count); for _ in 0..tags_count { let tags = Tags::read_from(buf)?; @@ -42,7 +42,7 @@ impl McBufReadable for TagMap { impl McBufWritable for TagMap { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_varint(self.len() as i32)?; + (self.len() as u32).write_into(buf)?; for (k, v) in &self.0 { k.write_into(buf)?; v.write_into(buf)?; @@ -53,7 +53,7 @@ impl McBufWritable for TagMap { impl McBufReadable for Tags { fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let name = ResourceLocation::read_from(buf)?; - let elements = buf.read_int_id_list()?; + let elements = Vec::<i32>::var_read_from(buf)?; Ok(Tags { name, elements }) } } @@ -61,7 +61,7 @@ impl McBufReadable for Tags { impl McBufWritable for Tags { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { self.name.write_into(buf)?; - buf.write_int_id_list(&self.elements)?; + self.elements.var_write_into(buf)?; Ok(()) } } 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 26321f34..61c281f5 100755 --- a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs @@ -1,12 +1,10 @@ +use super::ClientboundLoginPacket; +use azalea_buf::{BufReadError, McBufVarReadable, McBufVarWritable}; use std::{ hash::Hash, io::{Read, Write}, }; -use azalea_buf::{BufReadError, Readable, Writable}; - -use super::ClientboundLoginPacket; - #[derive(Hash, Clone, Debug)] pub struct ClientboundLoginCompressionPacket { pub compression_threshold: i32, @@ -18,12 +16,12 @@ impl ClientboundLoginCompressionPacket { } pub fn write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_varint(self.compression_threshold).unwrap(); + self.compression_threshold.var_write_into(buf)?; Ok(()) } pub fn read(buf: &mut impl Read) -> Result<ClientboundLoginPacket, BufReadError> { - let compression_threshold = buf.read_varint()?; + let compression_threshold = i32::var_read_from(buf)?; Ok(ClientboundLoginCompressionPacket { compression_threshold, diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index da1631a0..06647540 100644 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -4,7 +4,7 @@ pub mod login; pub mod status; use crate::read::ReadPacketError; -use azalea_buf::{BufReadError, McBufWritable, Readable, Writable}; +use azalea_buf::{BufReadError, McBufVarReadable, McBufVarWritable, McBufWritable}; use std::io::{Read, Write}; pub const PROTOCOL_VERSION: u32 = 760; @@ -44,13 +44,13 @@ where impl azalea_buf::McBufReadable for ConnectionProtocol { fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { - let id = buf.read_varint()?; + let id = i32::var_read_from(buf)?; ConnectionProtocol::from_i32(id).ok_or(BufReadError::UnexpectedEnumVariant { id }) } } impl McBufWritable for ConnectionProtocol { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_varint(*self as i32) + (*self as i32).var_write_into(buf) } } 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 0fa0f4b9..5382f195 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::{BufReadError, Readable}; +use azalea_buf::{BufReadError, McBufReadable}; use azalea_chat::component::Component; use serde::Deserialize; use serde_json::Value; @@ -44,7 +44,7 @@ impl ClientboundStatusResponsePacket { } pub fn read(buf: &mut impl Read) -> Result<ClientboundStatusPacket, BufReadError> { - let status_string = buf.read_utf()?; + let status_string = String::read_from(buf)?; let status_json: Value = serde_json::from_str(status_string.as_str())?; let packet = ClientboundStatusResponsePacket::deserialize(status_json)?.get(); diff --git a/azalea-protocol/src/read.rs b/azalea-protocol/src/read.rs index e7466c12..9ca102c1 100755 --- a/azalea-protocol/src/read.rs +++ b/azalea-protocol/src/read.rs @@ -1,6 +1,6 @@ use crate::packets::ProtocolPacket; use azalea_buf::McBufVarReadable; -use azalea_buf::{read_varint_async, BufReadError, Readable}; +use azalea_buf::{read_varint_async, BufReadError}; use azalea_crypto::Aes128CfbDec; use flate2::read::ZlibDecoder; use std::{ @@ -78,9 +78,8 @@ where fn packet_decoder<P: ProtocolPacket>(stream: &mut impl Read) -> Result<P, ReadPacketError> { // Packet ID - let packet_id = stream - .read_varint() - .map_err(|e| ReadPacketError::ReadPacketId { source: e })?; + let packet_id = + u32::var_read_from(stream).map_err(|e| ReadPacketError::ReadPacketId { source: e })?; P::read(packet_id.try_into().unwrap(), stream) } diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs index 30710f8b..b2ae2810 100755 --- a/azalea-protocol/src/write.rs +++ b/azalea-protocol/src/write.rs @@ -1,6 +1,6 @@ use crate::{packets::ProtocolPacket, read::MAXIMUM_UNCOMPRESSED_LENGTH}; use async_compression::tokio::bufread::ZlibEncoder; -use azalea_buf::Writable; +use azalea_buf::McBufVarWritable; use azalea_crypto::Aes128CfbEnc; use std::fmt::Debug; use thiserror::Error; @@ -8,7 +8,7 @@ use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt}; fn frame_prepender(data: &mut Vec<u8>) -> Result<Vec<u8>, std::io::Error> { let mut buf = Vec::new(); - buf.write_varint(data.len() as i32)?; + (data.len() as u32).var_write_into(&mut buf)?; buf.append(data); Ok(buf) } @@ -29,7 +29,7 @@ fn packet_encoder<P: ProtocolPacket + std::fmt::Debug>( packet: &P, ) -> Result<Vec<u8>, PacketEncodeError> { let mut buf = Vec::new(); - buf.write_varint(packet.id() as i32)?; + (packet.id() as u32).var_write_into(&mut buf)?; packet.write(&mut buf)?; if buf.len() > MAXIMUM_UNCOMPRESSED_LENGTH as usize { return Err(PacketEncodeError::TooBig { @@ -55,7 +55,7 @@ async fn compression_encoder( // if it's less than the compression threshold, don't compress if n < compression_threshold as usize { let mut buf = Vec::new(); - buf.write_varint(0)?; + 0.var_write_into(&mut buf)?; buf.write_all(data).await?; Ok(buf) } else { |
