From 08958c2278b15ebeac8a964f392ebb792e479b61 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Wed, 27 Nov 2024 19:31:40 -0600 Subject: Refactor azalea-protocol (#190) * start updating to 1.21.4 * fix block codegen and stop using block data from burger * rename packet related modules and structs to be simpler * ItemSlot -> ItemStack for more consistency with mojmap * .get() -> .into_packet() * simplify declare_state_packets by removing packet ids * rename read_from and write_into to azalea_read and azalea_write * rename McBufReadable and McBufWritable to AzaleaRead and AzaleaWrite * McBuf -> AzBuf * remove most uses of into_variant * update codegen and use resourcelocation names for packets * implement #[limit(i)] attribute for AzBuf derive macro * fixes for 1.21.4 * fix examples * update some physics code and fix ChatType * remove unused imports in codegen * re-add some things to migrate.py and update +mc version numbers automatically * downgrade to 1.21.3 lol --- azalea-protocol/src/write.rs | 126 ++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 61 deletions(-) (limited to 'azalea-protocol/src/write.rs') diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs index 0ec4308b..512d08ad 100755 --- a/azalea-protocol/src/write.rs +++ b/azalea-protocol/src/write.rs @@ -1,8 +1,11 @@ //! Write packets to a stream. -use std::{fmt::Debug, io::Read}; +use std::{ + fmt::Debug, + io::{self, Read}, +}; -use azalea_buf::McBufVarWritable; +use azalea_buf::AzaleaWriteVar; use azalea_crypto::Aes128CfbEnc; use flate2::{bufread::ZlibEncoder, Compression}; use thiserror::Error; @@ -11,31 +14,26 @@ use tracing::trace; use crate::{packets::ProtocolPacket, read::MAXIMUM_UNCOMPRESSED_LENGTH}; -/// Prepend the length of the packet to it. -fn frame_prepender(mut data: Vec) -> Result, std::io::Error> { - let mut buf = Vec::new(); - (data.len() as u32).var_write_into(&mut buf)?; - buf.append(&mut data); - Ok(buf) -} - -#[derive(Error, Debug)] -pub enum PacketEncodeError { - #[error("{0}")] - Io(#[from] std::io::Error), - #[error("Packet too big (is {actual} bytes, should be less than {maximum}): {packet_string}")] - TooBig { - actual: usize, - maximum: usize, - packet_string: String, - }, +pub async fn write_packet( + packet: &P, + stream: &mut W, + compression_threshold: Option, + cipher: &mut Option, +) -> io::Result<()> +where + P: ProtocolPacket + Debug, + W: AsyncWrite + Unpin + Send, +{ + trace!("Sending packet: {packet:?}"); + let raw_packet = serialize_packet(packet).unwrap(); + write_raw_packet(&raw_packet, stream, compression_threshold, cipher).await } pub fn serialize_packet( packet: &P, ) -> Result, PacketEncodeError> { let mut buf = Vec::new(); - packet.id().var_write_into(&mut buf)?; + packet.id().azalea_write_var(&mut buf)?; packet.write(&mut buf)?; if buf.len() > MAXIMUM_UNCOMPRESSED_LENGTH as usize { return Err(PacketEncodeError::TooBig { @@ -47,10 +45,26 @@ pub fn serialize_packet( Ok(buf) } -#[derive(Error, Debug)] -pub enum PacketCompressError { - #[error("{0}")] - Io(#[from] std::io::Error), +pub async fn write_raw_packet( + raw_packet: &[u8], + stream: &mut W, + compression_threshold: Option, + cipher: &mut Option, +) -> io::Result<()> +where + W: AsyncWrite + Unpin + Send, +{ + trace!("Writing raw packet: {raw_packet:?}"); + let mut raw_packet = raw_packet.to_vec(); + if let Some(threshold) = compression_threshold { + raw_packet = compression_encoder(&raw_packet, threshold).unwrap(); + } + raw_packet = frame_prepender(raw_packet).unwrap(); + // if we were given a cipher, encrypt the packet + if let Some(cipher) = cipher { + azalea_crypto::encrypt_packet(cipher, &mut raw_packet); + } + stream.write_all(&raw_packet).await } pub fn compression_encoder( @@ -61,58 +75,48 @@ pub 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(); - 0.var_write_into(&mut buf)?; - std::io::Write::write_all(&mut buf, data)?; + 0_u32.azalea_write_var(&mut buf)?; + io::Write::write_all(&mut buf, data)?; Ok(buf) } else { // otherwise, compress let mut deflater = ZlibEncoder::new(data, Compression::default()); + // write deflated data to buf let mut compressed_data = Vec::new(); deflater.read_to_end(&mut compressed_data)?; // prepend the length let mut len_prepended_compressed_data = Vec::new(); - (data.len() as u32).var_write_into(&mut len_prepended_compressed_data)?; + (data.len() as u32).azalea_write_var(&mut len_prepended_compressed_data)?; len_prepended_compressed_data.append(&mut compressed_data); Ok(len_prepended_compressed_data) } } -pub async fn write_packet( - packet: &P, - stream: &mut W, - compression_threshold: Option, - cipher: &mut Option, -) -> std::io::Result<()> -where - P: ProtocolPacket + Debug, - W: AsyncWrite + Unpin + Send, -{ - trace!("Sending packet: {packet:?}"); - let raw_packet = serialize_packet(packet).unwrap(); - write_raw_packet(&raw_packet, stream, compression_threshold, cipher).await +/// Prepend the length of the packet to it. +fn frame_prepender(mut data: Vec) -> Result, io::Error> { + let mut buf = Vec::new(); + (data.len() as u32).azalea_write_var(&mut buf)?; + buf.append(&mut data); + Ok(buf) } -pub async fn write_raw_packet( - raw_packet: &[u8], - stream: &mut W, - compression_threshold: Option, - cipher: &mut Option, -) -> std::io::Result<()> -where - W: AsyncWrite + Unpin + Send, -{ - trace!("Writing raw packet: {raw_packet:?}"); - let mut raw_packet = raw_packet.to_vec(); - if let Some(threshold) = compression_threshold { - raw_packet = compression_encoder(&raw_packet, threshold).unwrap(); - } - raw_packet = frame_prepender(raw_packet).unwrap(); - // if we were given a cipher, encrypt the packet - if let Some(cipher) = cipher { - azalea_crypto::encrypt_packet(cipher, &mut raw_packet); - } - stream.write_all(&raw_packet).await +#[derive(Error, Debug)] +pub enum PacketEncodeError { + #[error("{0}")] + Io(#[from] io::Error), + #[error("Packet too big (is {actual} bytes, should be less than {maximum}): {packet_string}")] + TooBig { + actual: usize, + maximum: usize, + packet_string: String, + }, +} + +#[derive(Error, Debug)] +pub enum PacketCompressError { + #[error("{0}")] + Io(#[from] io::Error), } -- cgit v1.2.3