aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/mc_buf
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-protocol/src/mc_buf')
-rwxr-xr-xazalea-protocol/src/mc_buf/mod.rs231
-rwxr-xr-xazalea-protocol/src/mc_buf/read.rs459
-rwxr-xr-xazalea-protocol/src/mc_buf/write.rs341
3 files changed, 1031 insertions, 0 deletions
diff --git a/azalea-protocol/src/mc_buf/mod.rs b/azalea-protocol/src/mc_buf/mod.rs
new file mode 100755
index 00000000..4ecb65d1
--- /dev/null
+++ b/azalea-protocol/src/mc_buf/mod.rs
@@ -0,0 +1,231 @@
+//! Utilities for reading and writing for the Minecraft protocol
+
+mod read;
+mod write;
+
+pub use read::{McBufReadable, McBufVarintReadable, Readable};
+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;
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use azalea_core::resource_location::ResourceLocation;
+ use std::{collections::HashMap, io::Cursor};
+ use tokio::io::BufReader;
+
+ #[test]
+ fn test_write_varint() {
+ let mut buf = Vec::new();
+ buf.write_varint(0).unwrap();
+ assert_eq!(buf, vec![0]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(1).unwrap();
+ assert_eq!(buf, vec![1]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(2).unwrap();
+ assert_eq!(buf, vec![2]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(127).unwrap();
+ assert_eq!(buf, vec![127]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(128).unwrap();
+ assert_eq!(buf, vec![128, 1]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(255).unwrap();
+ assert_eq!(buf, vec![255, 1]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(25565).unwrap();
+ assert_eq!(buf, vec![221, 199, 1]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(2097151).unwrap();
+ assert_eq!(buf, vec![255, 255, 127]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(2147483647).unwrap();
+ assert_eq!(buf, vec![255, 255, 255, 255, 7]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(-1).unwrap();
+ assert_eq!(buf, vec![255, 255, 255, 255, 15]);
+
+ let mut buf = Vec::new();
+ buf.write_varint(-2147483648).unwrap();
+ assert_eq!(buf, vec![128, 128, 128, 128, 8]);
+ }
+
+ #[tokio::test]
+ async fn test_read_varint() {
+ let mut buf = BufReader::new(Cursor::new(vec![0]));
+ assert_eq!(buf.read_varint().await.unwrap(), 0);
+ assert_eq!(buf.get_varint_size(0), 1);
+
+ let mut buf = BufReader::new(Cursor::new(vec![1]));
+ assert_eq!(buf.read_varint().await.unwrap(), 1);
+ assert_eq!(buf.get_varint_size(1), 1);
+
+ let mut buf = BufReader::new(Cursor::new(vec![2]));
+ assert_eq!(buf.read_varint().await.unwrap(), 2);
+ assert_eq!(buf.get_varint_size(2), 1);
+
+ let mut buf = BufReader::new(Cursor::new(vec![127]));
+ assert_eq!(buf.read_varint().await.unwrap(), 127);
+ assert_eq!(buf.get_varint_size(127), 1);
+
+ let mut buf = BufReader::new(Cursor::new(vec![128, 1]));
+ assert_eq!(buf.read_varint().await.unwrap(), 128);
+ assert_eq!(buf.get_varint_size(128), 2);
+
+ let mut buf = BufReader::new(Cursor::new(vec![255, 1]));
+ assert_eq!(buf.read_varint().await.unwrap(), 255);
+ assert_eq!(buf.get_varint_size(255), 2);
+
+ let mut buf = BufReader::new(Cursor::new(vec![221, 199, 1]));
+ assert_eq!(buf.read_varint().await.unwrap(), 25565);
+ assert_eq!(buf.get_varint_size(25565), 3);
+
+ let mut buf = BufReader::new(Cursor::new(vec![255, 255, 127]));
+ assert_eq!(buf.read_varint().await.unwrap(), 2097151);
+ assert_eq!(buf.get_varint_size(2097151), 3);
+
+ let mut buf = BufReader::new(Cursor::new(vec![255, 255, 255, 255, 7]));
+ assert_eq!(buf.read_varint().await.unwrap(), 2147483647);
+ assert_eq!(buf.get_varint_size(2147483647), 5);
+
+ let mut buf = BufReader::new(Cursor::new(vec![255, 255, 255, 255, 15]));
+ assert_eq!(buf.read_varint().await.unwrap(), -1);
+ assert_eq!(buf.get_varint_size(-1), 5);
+
+ let mut buf = BufReader::new(Cursor::new(vec![128, 128, 128, 128, 8]));
+ assert_eq!(buf.read_varint().await.unwrap(), -2147483648);
+ assert_eq!(buf.get_varint_size(-2147483648), 5);
+ }
+
+ #[tokio::test]
+ async fn test_read_varint_longer() {
+ let mut buf = BufReader::new(Cursor::new(vec![138, 56, 0, 135, 56, 123]));
+ assert_eq!(buf.read_varint().await.unwrap(), 7178);
+ }
+
+ #[tokio::test]
+ async fn test_list() {
+ let mut buf = Vec::new();
+ buf.write_list(&vec!["a", "bc", "def"], |buf, s| buf.write_utf(s))
+ .unwrap();
+
+ // there's no read_list because idk how to do it in rust
+ let mut buf = BufReader::new(Cursor::new(buf));
+
+ let mut result = Vec::new();
+ let length = buf.read_varint().await.unwrap();
+ for _ in 0..length {
+ result.push(buf.read_utf().await.unwrap());
+ }
+
+ assert_eq!(result, vec!["a", "bc", "def"]);
+ }
+
+ #[tokio::test]
+ async fn test_int_id_list() {
+ let mut buf = Vec::new();
+ buf.write_list(&vec![1, 2, 3], |buf, i| buf.write_varint(*i))
+ .unwrap();
+
+ let mut buf = BufReader::new(Cursor::new(buf));
+
+ let result = buf.read_int_id_list().await.unwrap();
+ assert_eq!(result, vec![1, 2, 3]);
+ }
+
+ #[tokio::test]
+ async fn test_map() {
+ let mut buf = Vec::new();
+ buf.write_map(
+ vec![("a", 1), ("bc", 23), ("def", 456)],
+ Vec::write_utf,
+ Vec::write_varint,
+ )
+ .unwrap();
+
+ let mut buf = BufReader::new(Cursor::new(buf));
+
+ let mut result = Vec::new();
+ let length = buf.read_varint().await.unwrap();
+ for _ in 0..length {
+ result.push((
+ buf.read_utf().await.unwrap(),
+ buf.read_varint().await.unwrap(),
+ ));
+ }
+
+ assert_eq!(
+ result,
+ vec![
+ ("a".to_string(), 1),
+ ("bc".to_string(), 23),
+ ("def".to_string(), 456)
+ ]
+ );
+ }
+
+ #[tokio::test]
+ async fn test_nbt() {
+ let mut buf = Vec::new();
+ buf.write_nbt(&azalea_nbt::Tag::Compound(HashMap::from_iter(vec![(
+ "hello world".to_string(),
+ azalea_nbt::Tag::Compound(HashMap::from_iter(vec![(
+ "name".to_string(),
+ azalea_nbt::Tag::String("Bananrama".to_string()),
+ )])),
+ )])))
+ .unwrap();
+
+ let mut buf = BufReader::new(Cursor::new(buf));
+
+ let result = buf.read_nbt().await.unwrap();
+ assert_eq!(
+ result,
+ azalea_nbt::Tag::Compound(HashMap::from_iter(vec![(
+ "hello world".to_string(),
+ azalea_nbt::Tag::Compound(HashMap::from_iter(vec![(
+ "name".to_string(),
+ azalea_nbt::Tag::String("Bananrama".to_string()),
+ )])),
+ )]))
+ );
+ }
+
+ #[tokio::test]
+ async fn test_long() {
+ let mut buf = Vec::new();
+ buf.write_long(123456).unwrap();
+
+ let mut buf = BufReader::new(Cursor::new(buf));
+
+ assert_eq!(buf.read_long().await.unwrap(), 123456);
+ }
+
+ #[tokio::test]
+ async fn test_resource_location() {
+ let mut buf = Vec::new();
+ buf.write_resource_location(&ResourceLocation::new("minecraft:dirt").unwrap())
+ .unwrap();
+
+ let mut buf = BufReader::new(Cursor::new(buf));
+
+ assert_eq!(
+ buf.read_resource_location().await.unwrap(),
+ ResourceLocation::new("minecraft:dirt").unwrap()
+ );
+ }
+}
diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs
new file mode 100755
index 00000000..1e031916
--- /dev/null
+++ b/azalea-protocol/src/mc_buf/read.rs
@@ -0,0 +1,459 @@
+use async_trait::async_trait;
+use azalea_chat::component::Component;
+use azalea_core::{
+ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
+};
+use serde::Deserialize;
+use tokio::io::{AsyncRead, AsyncReadExt};
+
+use super::MAX_STRING_LENGTH;
+
+#[async_trait]
+pub trait Readable {
+ async fn read_int_id_list(&mut self) -> Result<Vec<i32>, String>;
+ 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_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>;
+ async fn read_utf_with_len(&mut self, max_length: u32) -> Result<String, String>;
+ async fn read_byte(&mut self) -> Result<u8, String>;
+ async fn read_int(&mut self) -> Result<i32, String>;
+ async fn read_boolean(&mut self) -> Result<bool, String>;
+ async fn read_nbt(&mut self) -> Result<azalea_nbt::Tag, String>;
+ async fn read_long(&mut self) -> Result<i64, String>;
+ async fn read_resource_location(&mut self) -> Result<ResourceLocation, String>;
+ async fn read_short(&mut self) -> Result<i16, String>;
+ async fn read_float(&mut self) -> Result<f32, String>;
+}
+
+#[async_trait]
+impl<R> Readable for R
+where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+{
+ async fn read_int_id_list(&mut self) -> Result<Vec<i32>, String> {
+ let len = self.read_varint().await?;
+ let mut list = Vec::with_capacity(len as usize);
+ for _ in 0..len {
+ list.push(self.read_varint().await?);
+ }
+ Ok(list)
+ }
+
+ // fast varints modified from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L67
+ /// Read a single varint from the reader and return the value, along with the number of bytes read
+ async fn read_varint(&mut self) -> Result<i32, String> {
+ let mut buffer = [0];
+ let mut ans = 0;
+ for i in 0..5 {
+ self.read_exact(&mut buffer)
+ .await
+ .map_err(|_| "Invalid VarInt".to_string())?;
+ ans |= ((buffer[0] & 0b0111_1111) as i32) << (7 * i);
+ if buffer[0] & 0b1000_0000 == 0 {
+ return Ok(ans);
+ }
+ }
+ Ok(ans)
+ }
+
+ fn get_varint_size(&mut self, value: i32) -> u8 {
+ for i in 1..5 {
+ if (value & -1 << (i * 7)) != 0 {
+ continue;
+ }
+ return i;
+ }
+ 5
+ }
+
+ fn get_varlong_size(&mut self, value: i32) -> u8 {
+ for i in 1..10 {
+ if (value & -1 << (i * 7)) != 0 {
+ continue;
+ }
+ return i;
+ }
+ 10
+ }
+
+ async fn read_byte_array(&mut self) -> Result<Vec<u8>, String> {
+ let length = self.read_varint().await? as usize;
+ Ok(self.read_bytes_with_len(length).await?)
+ }
+
+ async fn read_bytes_with_len(&mut self, n: usize) -> Result<Vec<u8>, String> {
+ let mut bytes = vec![0; n];
+ match AsyncReadExt::read_exact(self, &mut bytes).await {
+ Ok(_) => Ok(bytes),
+ Err(_) => Err("Error reading bytes".to_string()),
+ }
+ }
+
+ async fn read_bytes(&mut self) -> Result<Vec<u8>, String> {
+ // read to end of the buffer
+ let mut bytes = vec![];
+ AsyncReadExt::read_to_end(self, &mut bytes)
+ .await
+ .map_err(|_| "Error reading bytes".to_string())?;
+ Ok(bytes)
+ }
+
+ async fn read_utf(&mut self) -> Result<String, String> {
+ self.read_utf_with_len(MAX_STRING_LENGTH.into()).await
+ }
+
+ async fn read_utf_with_len(&mut self, max_length: u32) -> Result<String, String> {
+ let length = self.read_varint().await?;
+ // i don't know why it's multiplied by 4 but it's like that in mojang's code so
+ if length < 0 {
+ return Err(
+ "The received encoded string buffer length is less than zero! Weird string!"
+ .to_string(),
+ );
+ }
+ if length as u32 > max_length * 4 {
+ return Err(format!(
+ "The received encoded string buffer length is longer than maximum allowed ({} > {})",
+ length,
+ max_length * 4
+ ));
+ }
+
+ // this is probably quite inefficient, idk how to do it better
+ let mut string = String::new();
+ let mut buffer = vec![0; length as usize];
+ self.read_exact(&mut buffer)
+ .await
+ .map_err(|_| "Invalid UTF-8".to_string())?;
+
+ string.push_str(std::str::from_utf8(&buffer).unwrap());
+ if string.len() > length as usize {
+ return Err(format!(
+ "The received string length is longer than maximum allowed ({} > {})",
+ length, max_length
+ ));
+ }
+
+ Ok(string)
+ }
+
+ /// Read a single byte from the reader
+ async fn read_byte(&mut self) -> Result<u8, String> {
+ match AsyncReadExt::read_u8(self).await {
+ Ok(r) => Ok(r),
+ Err(_) => Err("Error reading byte".to_string()),
+ }
+ }
+
+ async fn read_int(&mut self) -> Result<i32, String> {
+ match AsyncReadExt::read_i32(self).await {
+ Ok(r) => Ok(r),
+ Err(_) => Err("Error reading int".to_string()),
+ }
+ }
+
+ async fn read_boolean(&mut self) -> Result<bool, String> {
+ match self.read_byte().await {
+ Ok(0) => Ok(false),
+ Ok(1) => Ok(true),
+ _ => Err("Error reading boolean".to_string()),
+ }
+ }
+
+ async fn read_nbt(&mut self) -> Result<azalea_nbt::Tag, String> {
+ match azalea_nbt::Tag::read(self).await {
+ Ok(r) => Ok(r),
+ // Err(e) => Err(e.to_string()),
+ Err(e) => Err(e.to_string()).unwrap(),
+ }
+ }
+
+ async fn read_long(&mut self) -> Result<i64, String> {
+ match AsyncReadExt::read_i64(self).await {
+ Ok(r) => Ok(r),
+ Err(_) => Err("Error reading long".to_string()),
+ }
+ }
+
+ async fn read_resource_location(&mut self) -> Result<ResourceLocation, String> {
+ // get the resource location from the string
+ let location_string = self.read_utf().await?;
+ let location = ResourceLocation::new(&location_string)?;
+ Ok(location)
+ }
+
+ async fn read_short(&mut self) -> Result<i16, String> {
+ match AsyncReadExt::read_i16(self).await {
+ Ok(r) => Ok(r),
+ Err(_) => Err("Error reading short".to_string()),
+ }
+ }
+
+ async fn read_float(&mut self) -> Result<f32, String> {
+ match AsyncReadExt::read_f32(self).await {
+ Ok(r) => Ok(r),
+ Err(_) => Err("Error reading float".to_string()),
+ }
+ }
+}
+
+#[async_trait]
+pub trait McBufReadable
+where
+ Self: Sized,
+{
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send;
+}
+
+#[async_trait]
+pub trait McBufVarintReadable
+where
+ Self: Sized,
+{
+ async fn varint_read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send;
+}
+
+#[async_trait]
+impl McBufReadable for i32 {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_int().await
+ }
+}
+
+#[async_trait]
+impl McBufVarintReadable for i32 {
+ async fn varint_read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_varint().await
+ }
+}
+
+#[async_trait]
+impl McBufReadable for Vec<u8> {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_bytes().await
+ }
+}
+
+// string
+#[async_trait]
+impl McBufReadable for String {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_utf().await
+ }
+}
+
+// ResourceLocation
+#[async_trait]
+impl McBufReadable for ResourceLocation {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_resource_location().await
+ }
+}
+
+// u32
+#[async_trait]
+impl McBufReadable for u32 {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_int().await.map(|i| i as u32)
+ }
+}
+
+// u32 varint
+#[async_trait]
+impl McBufVarintReadable for u32 {
+ async fn varint_read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_varint().await.map(|i| i as u32)
+ }
+}
+
+// u16
+#[async_trait]
+impl McBufReadable for u16 {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_short().await.map(|i| i as u16)
+ }
+}
+
+// u16 varint
+#[async_trait]
+impl McBufVarintReadable for u16 {
+ async fn varint_read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ 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
+ }
+}
+
+// u8
+#[async_trait]
+impl McBufReadable for u8 {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_byte().await
+ }
+}
+
+// i8
+#[async_trait]
+impl McBufReadable for i8 {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_byte().await.map(|i| i as i8)
+ }
+}
+
+// f32
+#[async_trait]
+impl McBufReadable for f32 {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ buf.read_float().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
+ }
+}
+
+// Difficulty
+#[async_trait]
+impl McBufReadable for Difficulty {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ Ok(Difficulty::by_id(u8::read_into(buf).await?))
+ }
+}
+
+// Component
+#[async_trait]
+impl McBufReadable for Component {
+ async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ where
+ R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ {
+ let string = buf.read_utf().await?;
+ let json: serde_json::Value = serde_json::from_str(string.as_str())
+ .map_err(|e| "Component isn't valid JSON".to_string())?;
+ let component = Component::deserialize(json).map_err(|e| e.to_string())?;
+ Ok(component)
+ }
+}
diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs
new file mode 100755
index 00000000..05f613d8
--- /dev/null
+++ b/azalea-protocol/src/mc_buf/write.rs
@@ -0,0 +1,341 @@
+use async_trait::async_trait;
+use azalea_chat::component::Component;
+use azalea_core::{
+ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
+};
+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>
+ where
+ F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy,
+ T: Sized,
+ Self: Sized;
+ fn write_int_id_list(&mut self, list: &Vec<i32>) -> Result<(), std::io::Error>;
+ fn write_map<KF, VF, KT, VT>(
+ &mut self,
+ map: Vec<(KT, VT)>,
+ key_writer: KF,
+ value_writer: VF,
+ ) -> Result<(), std::io::Error>
+ where
+ KF: Fn(&mut Self, KT) -> Result<(), std::io::Error> + Copy,
+ VF: Fn(&mut Self, VT) -> Result<(), std::io::Error> + Copy,
+ Self: Sized;
+
+ fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error>;
+ fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>;
+ fn write_varint(&mut self, value: i32) -> Result<(), std::io::Error>;
+ fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error>;
+ fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error>;
+ fn write_short(&mut self, n: i16) -> Result<(), std::io::Error>;
+ fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>;
+ fn write_int(&mut self, n: i32) -> Result<(), std::io::Error>;
+ fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error>;
+ fn write_nbt(&mut self, nbt: &azalea_nbt::Tag) -> Result<(), std::io::Error>;
+ fn write_long(&mut self, n: i64) -> Result<(), std::io::Error>;
+ fn write_resource_location(
+ &mut self,
+ location: &ResourceLocation,
+ ) -> Result<(), std::io::Error>;
+ fn write_float(&mut self, n: f32) -> Result<(), std::io::Error>;
+}
+
+#[async_trait]
+impl Writable for Vec<u8> {
+ fn write_list<F, T>(&mut self, list: &[T], writer: F) -> Result<(), std::io::Error>
+ where
+ F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy,
+ Self: Sized,
+ {
+ self.write_varint(list.len() as i32)?;
+ for item in list {
+ writer(self, item)?;
+ }
+ Ok(())
+ }
+
+ fn write_int_id_list(&mut self, list: &Vec<i32>) -> Result<(), std::io::Error> {
+ self.write_list(&list, |buf, n| buf.write_varint(*n))
+ }
+
+ fn write_map<KF, VF, KT, VT>(
+ &mut self,
+ map: Vec<(KT, VT)>,
+ key_writer: KF,
+ value_writer: VF,
+ ) -> Result<(), std::io::Error>
+ where
+ KF: Fn(&mut Self, KT) -> Result<(), std::io::Error> + Copy,
+ VF: Fn(&mut Self, VT) -> Result<(), std::io::Error> + Copy,
+ Self: Sized,
+ {
+ self.write_varint(map.len() as i32)?;
+ for (key, value) in map {
+ key_writer(self, key)?;
+ value_writer(self, value)?;
+ }
+ Ok(())
+ }
+
+ fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error> {
+ WriteBytesExt::write_u8(self, n)
+ }
+
+ fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> {
+ self.extend_from_slice(bytes);
+ Ok(())
+ }
+
+ fn write_varint(&mut self, mut value: i32) -> Result<(), std::io::Error> {
+ let mut buffer = [0];
+ if value == 0 {
+ self.write_all(&buffer).unwrap();
+ }
+ while value != 0 {
+ buffer[0] = (value & 0b0111_1111) as u8;
+ value = (value >> 7) & (i32::max_value() >> 6);
+ if value != 0 {
+ buffer[0] |= 0b1000_0000;
+ }
+ self.write_all(&buffer)?;
+ }
+ Ok(())
+ }
+
+ fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error> {
+ if string.len() > len {
+ panic!(
+ "String too big (was {} bytes encoded, max {})",
+ string.len(),
+ len
+ );
+ }
+ self.write_varint(string.len() as i32)?;
+ self.write_bytes(string.as_bytes())
+ }
+
+ fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error> {
+ self.write_utf_with_len(string, MAX_STRING_LENGTH.into())
+ }
+
+ fn write_short(&mut self, n: i16) -> Result<(), std::io::Error> {
+ WriteBytesExt::write_i16::<BigEndian>(self, n)
+ }
+
+ fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> {
+ self.write_varint(bytes.len() as i32)?;
+ self.write_bytes(bytes)
+ }
+
+ fn write_int(&mut self, n: i32) -> Result<(), std::io::Error> {
+ WriteBytesExt::write_i32::<BigEndian>(self, n)
+ }
+
+ fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error> {
+ self.write_byte(if b { 1 } else { 0 })
+ }
+
+ fn write_nbt(&mut self, nbt: &azalea_nbt::Tag) -> Result<(), std::io::Error> {
+ nbt.write(self)
+ .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
+ }
+
+ fn write_long(&mut self, n: i64) -> Result<(), std::io::Error> {
+ WriteBytesExt::write_i64::<BigEndian>(self, n)
+ }
+
+ fn write_float(&mut self, n: f32) -> Result<(), std::io::Error> {
+ WriteBytesExt::write_f32::<BigEndian>(self, n)
+ }
+
+ fn write_resource_location(
+ &mut self,
+ location: &ResourceLocation,
+ ) -> Result<(), std::io::Error> {
+ self.write_utf(&location.to_string())
+ }
+}
+
+pub trait McBufWritable
+where
+ Self: Sized,
+{
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error>;
+}
+
+pub trait McBufVarintWritable
+where
+ Self: Sized,
+{
+ fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error>;
+}
+
+impl McBufWritable for i32 {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ Writable::write_int(buf, *self)
+ }
+}
+
+impl McBufVarintWritable for i32 {
+ fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ buf.write_varint(*self)
+ }
+}
+
+impl McBufWritable for Vec<u8> {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ buf.write_bytes(self)
+ }
+}
+
+// string
+impl McBufWritable for String {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ buf.write_utf(self)
+ }
+}
+
+// ResourceLocation
+impl McBufWritable for ResourceLocation {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ buf.write_resource_location(self)
+ }
+}
+
+// u32
+impl McBufWritable for u32 {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ i16::write_into(&(*self as i16), buf)
+ }
+}
+
+// u32 varint
+impl McBufVarintWritable for u32 {
+ fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ 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> {
+ i16::write_into(&(*self as i16), buf)
+ }
+}
+
+// u16 varint
+impl McBufVarintWritable for u16 {
+ fn varint_write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ 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)
+ }
+}
+
+// i16
+impl McBufWritable for i16 {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ Writable::write_short(buf, *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)
+ }
+}
+
+// i8
+impl McBufWritable for i8 {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ buf.write_byte(*self as u8)
+ }
+}
+
+// f32
+impl McBufWritable for f32 {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ buf.write_float(*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)
+ }
+}
+
+// Difficulty
+impl McBufWritable for Difficulty {
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ u8::write_into(&self.id(), buf)
+ }
+}
+
+// Component
+#[async_trait]
+impl McBufWritable for Component {
+ // async fn read_into<R>(buf: &mut R) -> Result<Self, String>
+ // where
+ // R: AsyncRead + std::marker::Unpin + std::marker::Send,
+ // {
+ // let string = buf.read_utf().await?;
+ // let json: serde_json::Value = serde_json::from_str(string.as_str())
+ // .map_err(|e| "Component isn't valid JSON".to_string())?;
+ // let component = Component::deserialize(json).map_err(|e| e.to_string())?;
+ // Ok(component)
+ // }
+ fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
+ // component doesn't have serialize implemented yet
+ todo!()
+ }
+}