diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-04-25 03:50:17 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-25 03:50:17 +0000 |
| commit | dac943e3d79def7d2c322450790f16f027735941 (patch) | |
| tree | 3beafa3c8035c69a33b7d0f12779236bf786cf36 /azalea-protocol/src/mc_buf | |
| parent | b7641ff308aab7840d2a2253ae50f8ee496b2a97 (diff) | |
| parent | f4dd3a9293367fa8f3d839a184e8055b22595204 (diff) | |
| download | azalea-drasl-dac943e3d79def7d2c322450790f16f027735941.tar.xz | |
Merge pull request #3 from mat-1/auth
Auth
Diffstat (limited to 'azalea-protocol/src/mc_buf')
| -rwxr-xr-x | azalea-protocol/src/mc_buf/mod.rs | 20 | ||||
| -rwxr-xr-x | azalea-protocol/src/mc_buf/read.rs | 18 | ||||
| -rwxr-xr-x | azalea-protocol/src/mc_buf/write.rs | 10 |
3 files changed, 43 insertions, 5 deletions
diff --git a/azalea-protocol/src/mc_buf/mod.rs b/azalea-protocol/src/mc_buf/mod.rs index 4ecb65d1..3ba6ac3e 100755 --- a/azalea-protocol/src/mc_buf/mod.rs +++ b/azalea-protocol/src/mc_buf/mod.rs @@ -5,11 +5,31 @@ mod write; pub use read::{McBufReadable, McBufVarintReadable, Readable}; pub use write::{McBufVarintWritable, McBufWritable, Writable}; +use std::ops::Deref; // const DEFAULT_NBT_QUOTA: u32 = 2097152; const MAX_STRING_LENGTH: u16 = 32767; // const MAX_COMPONENT_STRING_LENGTH: u32 = 262144; + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct ByteArray(Vec<u8>); + +impl Deref for ByteArray { + type Target = Vec<u8>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl From<Vec<u8>> for ByteArray { + fn from(vec: Vec<u8>) -> 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 1e031916..3d50e5aa 100755 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -5,6 +5,7 @@ use azalea_core::{ }; use serde::Deserialize; use tokio::io::{AsyncRead, AsyncReadExt}; +use crate::mc_buf::ByteArray; use super::MAX_STRING_LENGTH; @@ -14,7 +15,7 @@ pub trait Readable { async fn read_varint(&mut self) -> Result<i32, String>; 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<Vec<u8>, String>; + async fn read_byte_array(&mut self) -> Result<ByteArray, String>; async fn read_bytes_with_len(&mut self, n: usize) -> Result<Vec<u8>, String>; async fn read_bytes(&mut self) -> Result<Vec<u8>, String>; async fn read_utf(&mut self) -> Result<String, String>; @@ -80,9 +81,9 @@ where 10 } - async fn read_byte_array(&mut self) -> Result<Vec<u8>, String> { + async fn read_byte_array(&mut self) -> Result<ByteArray, String> { let length = self.read_varint().await? as usize; - Ok(self.read_bytes_with_len(length).await?) + Ok(ByteArray(self.read_bytes_with_len(length).await?)) } async fn read_bytes_with_len(&mut self, n: usize) -> Result<Vec<u8>, String> { @@ -251,6 +252,17 @@ impl McBufReadable for Vec<u8> { } } + +#[async_trait] +impl McBufReadable for ByteArray { + async fn read_into<R>(buf: &mut R) -> Result<Self, String> + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_byte_array().await + } +} + // string #[async_trait] impl McBufReadable for String { diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index 05f613d8..f1362402 100755 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -1,3 +1,5 @@ +use super::MAX_STRING_LENGTH; +use crate::mc_buf::ByteArray; use async_trait::async_trait; use azalea_chat::component::Component; use azalea_core::{ @@ -6,8 +8,6 @@ use azalea_core::{ use byteorder::{BigEndian, WriteBytesExt}; use std::io::Write; -use super::MAX_STRING_LENGTH; - #[async_trait] pub trait Writable { fn write_list<F, T>(&mut self, list: &[T], writer: F) -> Result<(), std::io::Error> @@ -193,6 +193,12 @@ impl McBufWritable for Vec<u8> { } } +impl McBufWritable for ByteArray { + fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + buf.write_byte_array(&self) + } +} + // string impl McBufWritable for String { fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { |
