diff options
| author | mat <github@matdoes.dev> | 2021-12-06 00:28:40 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-06 00:28:40 -0600 |
| commit | 5029a09963b5753c1f9b7f777f28e1c0951343e7 (patch) | |
| tree | 2e0e37029bf031adc3e28713828e7d4be7336ccb /minecraft-protocol/src/friendly_byte_buf.rs | |
| download | azalea-drasl-5029a09963b5753c1f9b7f777f28e1c0951343e7.tar.xz | |
Initial commit
Diffstat (limited to 'minecraft-protocol/src/friendly_byte_buf.rs')
| -rw-r--r-- | minecraft-protocol/src/friendly_byte_buf.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/minecraft-protocol/src/friendly_byte_buf.rs b/minecraft-protocol/src/friendly_byte_buf.rs new file mode 100644 index 00000000..2babe398 --- /dev/null +++ b/minecraft-protocol/src/friendly_byte_buf.rs @@ -0,0 +1,56 @@ +//! Minecraft calls it a "friendly byte buffer". + +use byteorder::{BigEndian, WriteBytesExt}; +// use std::io::Write; + +const MAX_VARINT_SIZE: u32 = 5; +const MAX_VARLONG_SIZE: u32 = 10; +const DEFAULT_NBT_QUOTA: u32 = 2097152; +const MAX_STRING_LENGTH: u16 = 32767; +const MAX_COMPONENT_STRING_LENGTH: u32 = 262144; + +pub struct FriendlyByteBuf<'a> { + source: &'a mut Vec<u8>, +} + +impl FriendlyByteBuf<'_> { + pub fn write_byte(&mut self, n: u8) { + self.source.write_u8(n).unwrap(); + println!("write_byte: {}", n); + } + + pub fn write_bytes(&mut self, bytes: &[u8]) { + self.source.extend_from_slice(bytes); + } + + pub fn write_varint(&mut self, mut n: u32) { + loop { + if (n & 0xFFFFFF80) == 0 { + self.write_byte(n as u8); + return (); + } + self.write_byte((n & 0x7F | 0x80) as u8); + n >>= 7; + } + } + + pub fn write_utf_with_len(&mut self, string: &String, len: usize) { + if string.len() > len { + panic!( + "String too big (was {} bytes encoded, max {})", + string.len(), + len + ); + } + self.write_varint(string.len() as u32); + self.write_bytes(string.as_bytes()); + } + + pub fn write_utf(&mut self, string: &String) { + self.write_utf_with_len(string, MAX_STRING_LENGTH as usize); + } + + pub fn write_short(&mut self, n: u16) { + self.source.write_u16::<BigEndian>(n).unwrap(); + } +} |
