From 5ca49e680ed8519456dc9a9af84321d4b69dcbb3 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 23 Jun 2022 15:12:17 -0500 Subject: azalea-buf --- azalea-crypto/src/lib.rs | 2 ++ azalea-crypto/src/signing.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) (limited to 'azalea-crypto/src') diff --git a/azalea-crypto/src/lib.rs b/azalea-crypto/src/lib.rs index a5e797e8..85705883 100644 --- a/azalea-crypto/src/lib.rs +++ b/azalea-crypto/src/lib.rs @@ -79,6 +79,8 @@ pub fn decrypt_packet(cipher: &mut Aes128CfbDec, packet: &mut [u8]) { cipher.decrypt_blocks_inout_mut(chunks); } + + #[cfg(test)] mod tests { use super::*; diff --git a/azalea-crypto/src/signing.rs b/azalea-crypto/src/signing.rs index 21cd813a..a5280a18 100644 --- a/azalea-crypto/src/signing.rs +++ b/azalea-crypto/src/signing.rs @@ -3,3 +3,19 @@ pub struct SaltSignaturePair { pub salt: u64, pub signature: Vec, } + +impl McBufReadable for SaltSignaturePair { + fn read_into(buf: &mut impl Read) -> Result { + let salt = u64::read_into(buf)?; + let signature = Vec::::read_into(buf)?; + Ok(SaltSignaturePair { salt, signature }) + } +} + +impl McBufWritable for SaltSignaturePair { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + self.salt.write_into(buf)?; + self.signature.write_into(buf)?; + Ok(()) + } +} -- cgit v1.2.3 From ba399c4440c2d182f55cba7fab068e1118bcc2b7 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 23 Jun 2022 15:28:33 -0500 Subject: switch things to use azalea-buf --- azalea-buf/Cargo.toml | 2 +- azalea-buf/buf-macros/src/lib.rs | 20 ++++++++++---------- azalea-buf/src/lib.rs | 3 ++- azalea-chat/src/component.rs | 5 ++++- azalea-core/src/delta.rs | 2 ++ azalea-core/src/particle/mod.rs | 2 ++ azalea-crypto/src/signing.rs | 3 +++ azalea-nbt/src/decode.rs | 3 ++- azalea-nbt/src/encode.rs | 1 + azalea-nbt/src/lib.rs | 9 +++++---- 10 files changed, 32 insertions(+), 18 deletions(-) (limited to 'azalea-crypto/src') diff --git a/azalea-buf/Cargo.toml b/azalea-buf/Cargo.toml index 79f9d64d..7497f947 100644 --- a/azalea-buf/Cargo.toml +++ b/azalea-buf/Cargo.toml @@ -1,6 +1,6 @@ [package] edition = "2021" -name = "azalea-buf" +name = "azalea_buf" version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/azalea-buf/buf-macros/src/lib.rs b/azalea-buf/buf-macros/src/lib.rs index 3afeaeed..4cc397a2 100644 --- a/azalea-buf/buf-macros/src/lib.rs +++ b/azalea-buf/buf-macros/src/lib.rs @@ -21,11 +21,11 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt syn::Type::Path(_) => { if f.attrs.iter().any(|a| a.path.is_ident("var")) { quote! { - let #field_name = crate::McBufVarReadable::var_read_into(buf)?; + let #field_name = azalea_buf::McBufVarReadable::var_read_into(buf)?; } } else { quote! { - let #field_name = crate::McBufReadable::read_into(buf)?; + let #field_name = azalea_buf::McBufReadable::read_into(buf)?; } } } @@ -40,7 +40,7 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt let read_field_names = named.iter().map(|f| &f.ident).collect::>(); quote! { - impl crate::McBufReadable for #ident { + impl azalea_buf::McBufReadable for #ident { fn read_into(buf: &mut impl std::io::Read) -> Result { #(#read_fields)* Ok(#ident { @@ -75,10 +75,10 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt } quote! { - impl crate::McBufReadable for #ident { + impl azalea_buf::McBufReadable for #ident { fn read_into(buf: &mut impl std::io::Read) -> Result { - let id = crate::McBufVarReadable::var_read_into(buf)?; + let id = azalea_buf::McBufVarReadable::var_read_into(buf)?; match id { #match_contents _ => Err(format!("Unknown enum variant {}", id)), @@ -110,11 +110,11 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt syn::Type::Path(_) => { if f.attrs.iter().any(|attr| attr.path.is_ident("var")) { quote! { - crate::McBufVarWritable::var_write_into(&self.#field_name, buf)?; + azalea_buf::McBufVarWritable::var_write_into(&self.#field_name, buf)?; } } else { quote! { - crate::McBufWritable::write_into(&self.#field_name, buf)?; + azalea_buf::McBufWritable::write_into(&self.#field_name, buf)?; } } } @@ -128,7 +128,7 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt .collect::>(); quote! { - impl crate::McBufWritable for #ident { + impl azalea_buf::McBufWritable for #ident { fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { #(#write_fields)* Ok(()) @@ -138,9 +138,9 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt } syn::Data::Enum(syn::DataEnum { .. }) => { quote! { - impl crate::McBufWritable for #ident { + impl azalea_buf::McBufWritable for #ident { fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { - crate::Writable::write_varint(buf, *self as i32) + azalea_buf::Writable::write_varint(buf, *self as i32) } } } diff --git a/azalea-buf/src/lib.rs b/azalea-buf/src/lib.rs index 2ba17ac2..bd9f43be 100644 --- a/azalea-buf/src/lib.rs +++ b/azalea-buf/src/lib.rs @@ -7,6 +7,7 @@ mod definitions; mod read; mod write; +pub use buf_macros::*; pub use definitions::*; pub use read::{read_varint_async, McBufReadable, McBufVarReadable, Readable}; pub use write::{McBufVarWritable, McBufWritable, Writable}; @@ -18,7 +19,7 @@ const MAX_STRING_LENGTH: u16 = 32767; #[cfg(test)] mod tests { use super::*; - use std::{collections::HashMap, io::Cursor}; + use std::io::Cursor; #[test] fn test_write_varint() { diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index 872a0a5c..d307bcbc 100755 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -1,3 +1,6 @@ +use std::io::{Read, Write}; + +use azalea_buf::{McBufReadable, McBufWritable}; use serde::{de, Deserialize, Deserializer}; use crate::{ @@ -267,7 +270,7 @@ impl<'de> Deserialize<'de> for Component { impl McBufReadable for Component { fn read_into(buf: &mut impl Read) -> Result { - let string = buf.read_utf()?; + let string = String::read_into(buf)?; let json: serde_json::Value = serde_json::from_str(string.as_str()) .map_err(|_| "Component isn't valid JSON".to_string())?; let component = Component::deserialize(json).map_err(|e| e.to_string())?; diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index 32517e0d..a4d02907 100644 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -1,3 +1,5 @@ +pub use azalea_buf::McBuf; + /// Only works for up to 8 blocks #[derive(Clone, Debug, McBuf)] pub struct PositionDelta { diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs index fc815a0b..5ca8ac8e 100644 --- a/azalea-core/src/particle/mod.rs +++ b/azalea-core/src/particle/mod.rs @@ -1,3 +1,5 @@ +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; #[derive(Debug, Clone, McBuf)] pub struct Particle { diff --git a/azalea-crypto/src/signing.rs b/azalea-crypto/src/signing.rs index a5280a18..4fc07d30 100644 --- a/azalea-crypto/src/signing.rs +++ b/azalea-crypto/src/signing.rs @@ -1,3 +1,6 @@ +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + #[derive(Debug, Clone)] pub struct SaltSignaturePair { pub salt: u64, diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs index 73cd613e..1c011839 100755 --- a/azalea-nbt/src/decode.rs +++ b/azalea-nbt/src/decode.rs @@ -1,5 +1,6 @@ use crate::Error; use crate::Tag; +use azalea_buf::McBufReadable; use byteorder::{ReadBytesExt, BE}; use flate2::read::{GzDecoder, ZlibDecoder}; use std::collections::HashMap; @@ -139,7 +140,7 @@ impl Tag { impl McBufReadable for Tag { fn read_into(buf: &mut impl Read) -> Result { - match Tag::read(self) { + match Tag::read(buf) { Ok(r) => Ok(r), // Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()).unwrap(), diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 17d20270..3763ffd1 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -1,5 +1,6 @@ use crate::Error; use crate::Tag; +use azalea_buf::McBufWritable; use byteorder::{WriteBytesExt, BE}; use flate2::write::{GzEncoder, ZlibEncoder}; use std::collections::HashMap; diff --git a/azalea-nbt/src/lib.rs b/azalea-nbt/src/lib.rs index 8cca1f2b..cb2eb28e 100755 --- a/azalea-nbt/src/lib.rs +++ b/azalea-nbt/src/lib.rs @@ -9,23 +9,24 @@ pub use tag::Tag; #[cfg(test)] mod tests { use super::*; + use azalea_buf::{McBufReadable, McBufWritable}; use std::{collections::HashMap, io::Cursor}; #[test] fn mcbuf_nbt() { let mut buf = Vec::new(); - buf.write_nbt(&Tag::Compound(HashMap::from_iter(vec![( + let tag = Tag::Compound(HashMap::from_iter(vec![( "hello world".to_string(), Tag::Compound(HashMap::from_iter(vec![( "name".to_string(), Tag::String("Bananrama".to_string()), )])), - )]))) - .unwrap(); + )])); + tag.write_into(&mut buf).unwrap(); let mut buf = Cursor::new(buf); - let result = buf.read_nbt().unwrap(); + let result = Tag::read_into(&mut buf).unwrap(); assert_eq!( result, Tag::Compound(HashMap::from_iter(vec![( -- cgit v1.2.3 From ce834aeca5d732a547b66554590232321e3d1829 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 23 Jun 2022 23:45:23 -0500 Subject: Fixes --- azalea-client/src/movement.rs | 2 +- azalea-crypto/src/lib.rs | 2 -- azalea-protocol/src/packets/game/mod.rs | 2 +- codegen/lib/code/packet.py | 3 ++- 4 files changed, 4 insertions(+), 5 deletions(-) (limited to 'azalea-crypto/src') diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index 9f5cd27c..c9cd62e9 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -1,5 +1,5 @@ -use azalea_core::EntityPos; use crate::Client; +use azalea_core::EntityPos; use azalea_protocol::packets::game::serverbound_move_player_packet_pos_rot::ServerboundMovePlayerPacketPosRot; impl Client { diff --git a/azalea-crypto/src/lib.rs b/azalea-crypto/src/lib.rs index 85705883..a5e797e8 100644 --- a/azalea-crypto/src/lib.rs +++ b/azalea-crypto/src/lib.rs @@ -79,8 +79,6 @@ pub fn decrypt_packet(cipher: &mut Aes128CfbDec, packet: &mut [u8]) { cipher.decrypt_blocks_inout_mut(chunks); } - - #[cfg(test)] mod tests { use super::*; diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 80cada9b..d3d6650c 100755 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -105,13 +105,13 @@ declare_state_packets!( 0x3c: clientbound_rotate_head_packet::ClientboundRotateHeadPacket, 0x3d: clientbound_section_blocks_update_packet::ClientboundSectionBlocksUpdatePacket, 0x3f: clientbound_server_data_packet::ClientboundServerDataPacket, - 0x44: clientbound_set_entity_link_packet::ClientboundSetEntityLinkPacket, 0x47: clientbound_set_carried_item_packet::ClientboundSetCarriedItemPacket, 0x48: clientbound_set_chunk_cache_center_packet::ClientboundSetChunkCacheCenterPacket, 0x49: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, 0x4a: clientbound_set_default_spawn_position_packet::ClientboundSetDefaultSpawnPositionPacket, 0x4b: clientbound_set_display_chat_preview_packet::ClientboundSetDisplayChatPreviewPacket, 0x4d: clientbound_set_entity_data_packet::ClientboundSetEntityDataPacket, + 0x4e: clientbound_set_entity_link_packet::ClientboundSetEntityLinkPacket, 0x4f: clientbound_entity_velocity_packet::ClientboundEntityVelocityPacket, 0x50: clientbound_set_equipment_packet::ClientboundSetEquipmentPacket, 0x51: clientbound_set_experience_packet::ClientboundSetExperiencePacket, diff --git a/codegen/lib/code/packet.py b/codegen/lib/code/packet.py index c282bdb4..b4ca8be7 100644 --- a/codegen/lib/code/packet.py +++ b/codegen/lib/code/packet.py @@ -29,7 +29,8 @@ def generate_packet(burger_packets, mappings: Mappings, target_packet_id, target uses = set() generated_packet_code.append( f'#[derive(Clone, Debug, McBuf, {to_camel_case(state)}Packet)]') - uses.add(f'packet_macros::{{{to_camel_case(state)}Packet, McBuf}}') + uses.add(f'packet_macros::{to_camel_case(state)}Packet') + uses.add(f'packet_buf::McBuf') obfuscated_class_name = packet['class'].split('.')[0] class_name = mappings.get_class( -- cgit v1.2.3