diff options
| author | mat <github@matdoes.dev> | 2022-01-02 17:40:18 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-01-02 17:40:18 -0600 |
| commit | 45871fc01e212a50ac5e6268e4677f97f8fe5bb3 (patch) | |
| tree | 3a8d4190f15e93758eeb6caa3f9694be71f04cb4 /azalea-protocol/src/mc_buf | |
| parent | 094c210dada7c0ee83c19965739312d2d00e4099 (diff) | |
| download | azalea-drasl-45871fc01e212a50ac5e6268e4677f97f8fe5bb3.tar.xz | |
better parsing for entire login packet
Diffstat (limited to 'azalea-protocol/src/mc_buf')
| -rw-r--r-- | azalea-protocol/src/mc_buf/read.rs | 84 | ||||
| -rw-r--r-- | azalea-protocol/src/mc_buf/write.rs | 68 |
2 files changed, 146 insertions, 6 deletions
diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 374e5443..5127860e 100644 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use azalea_core::resource_location::ResourceLocation; +use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; use tokio::io::{AsyncRead, AsyncReadExt}; use super::MAX_STRING_LENGTH; @@ -304,3 +304,85 @@ impl McBufVarintReadable for u16 { buf.read_varint().await.map(|i| i as u16) } } + +// i64 +#[async_trait] +impl McBufReadable for i64 { + async fn read_into<R>(buf: &mut R) -> Result<Self, String> + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_long().await + } +} + +// u64 +#[async_trait] +impl McBufReadable for u64 { + async fn read_into<R>(buf: &mut R) -> Result<Self, String> + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + i64::read_into(buf).await.map(|i| i as u64) + } +} + +// bool +#[async_trait] +impl McBufReadable for bool { + async fn read_into<R>(buf: &mut R) -> Result<Self, String> + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_boolean().await + } +} + +// GameType +#[async_trait] +impl McBufReadable for GameType { + async fn read_into<R>(buf: &mut R) -> Result<Self, String> + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + GameType::from_id(buf.read_byte().await?) + } +} + +// Option<GameType> +#[async_trait] +impl McBufReadable for Option<GameType> { + async fn read_into<R>(buf: &mut R) -> Result<Self, String> + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + GameType::from_optional_id(buf.read_byte().await? as i8) + } +} + +// Vec<ResourceLocation> +#[async_trait] +impl McBufReadable for Vec<ResourceLocation> { + async fn read_into<R>(buf: &mut R) -> Result<Self, String> + 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 { + async fn read_into<R>(buf: &mut R) -> Result<Self, String> + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_nbt().await + } +} diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index f22b218a..14dac9d1 100644 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use azalea_core::resource_location::ResourceLocation; +use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; use byteorder::{BigEndian, WriteBytesExt}; use std::io::Write; @@ -202,27 +202,85 @@ impl McBufWritable for ResourceLocation { // u32 impl McBufWritable for u32 { fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { - buf.write_varint(*self as i32) + i32::varint_write_into(&(*self as i32), buf) } } // u32 varint impl McBufVarintWritable for u32 { fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { - buf.write_varint(*self as i32) + i32::varint_write_into(&(*self as i32), buf) } } // u16 impl McBufWritable for u16 { fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { - buf.write_varint(*self as i32) + i32::varint_write_into(&(*self as i32), buf) } } // u16 varint impl McBufVarintWritable for u16 { fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { - buf.write_varint(*self as i32) + i32::varint_write_into(&(*self as i32), buf) + } +} + +// u8 +impl McBufWritable for u8 { + fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + buf.write_byte(*self) + } +} + +// i64 +impl McBufWritable for i64 { + fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + Writable::write_long(buf, *self) + } +} + +// u64 +impl McBufWritable for u64 { + fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + i64::write_into(&(*self as i64), buf) + } +} + +// bool +impl McBufWritable for bool { + fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + buf.write_boolean(*self) + } +} + +// GameType +impl McBufWritable for GameType { + fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + u8::write_into(&self.to_id(), buf) + } +} + +// Option<GameType> +impl McBufWritable for Option<GameType> { + fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + buf.write_byte(GameType::to_optional_id(&self) as u8) + } +} + +// Vec<ResourceLocation> +impl McBufWritable for Vec<ResourceLocation> { + fn write_into(&self, buf: &mut Vec<u8>) -> 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<u8>) -> Result<(), std::io::Error> { + buf.write_nbt(self) } } |
