diff --git a/Cargo.toml b/Cargo.toml index f7272ff7..e5673121 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,6 @@ members = [ [profile.release] debug = true + +[profile.dev.package.azalea-crypto] +opt-level = 3 diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index a7735e0c..6edb3d13 100755 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -258,6 +258,9 @@ impl Client { GamePacket::ClientboundAddPlayerPacket(p) => { println!("Got add player packet {:?}", p); } + GamePacket::ClientboundInitializeBorderPacket(p) => { + println!("Got initialize border packet {:?}", p); + } _ => panic!("Unexpected packet {:?}", packet), } println!(); diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 35abf642..a25492c6 100755 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -23,9 +23,9 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt // if it's a string, use buf.write_string match field_type { syn::Type::Path(_) => { - if f.attrs.iter().any(|a| a.path.is_ident("varint")) { + if f.attrs.iter().any(|a| a.path.is_ident("var")) { quote! { - let #field_name = crate::mc_buf::McBufVarintReadable::varint_read_into(buf)?; + let #field_name = crate::mc_buf::McBufVarReadable::var_read_into(buf)?; } } else { quote! { @@ -102,9 +102,9 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt // if it's a string, use buf.write_string match field_type { syn::Type::Path(_) => { - if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { + if f.attrs.iter().any(|attr| attr.path.is_ident("var")) { quote! { - crate::mc_buf::McBufVarintWritable::varint_write_into(&self.#field_name, buf)?; + crate::mc_buf::McBufVarWritable::var_write_into(&self.#field_name, buf)?; } } else { quote! { @@ -143,14 +143,14 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt } } -#[proc_macro_derive(McBufReadable, attributes(varint))] +#[proc_macro_derive(McBufReadable, attributes(var))] pub fn derive_mcbufreadable(input: TokenStream) -> TokenStream { let DeriveInput { ident, data, .. } = parse_macro_input!(input); create_impl_mcbufreadable(&ident, &data).into() } -#[proc_macro_derive(McBufWritable, attributes(varint))] +#[proc_macro_derive(McBufWritable, attributes(var))] pub fn derive_mcbufwritable(input: TokenStream) -> TokenStream { let DeriveInput { ident, data, .. } = parse_macro_input!(input); @@ -198,22 +198,22 @@ fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> Toke contents.into() } -#[proc_macro_derive(GamePacket, attributes(varint))] +#[proc_macro_derive(GamePacket, attributes(var))] pub fn derive_game_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::game::GamePacket}) } -#[proc_macro_derive(HandshakePacket, attributes(varint))] +#[proc_macro_derive(HandshakePacket, attributes(var))] pub fn derive_handshake_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::handshake::HandshakePacket}) } -#[proc_macro_derive(LoginPacket, attributes(varint))] +#[proc_macro_derive(LoginPacket, attributes(var))] pub fn derive_login_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::login::LoginPacket}) } -#[proc_macro_derive(StatusPacket, attributes(varint))] +#[proc_macro_derive(StatusPacket, attributes(var))] pub fn derive_status_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::status::StatusPacket}) } diff --git a/azalea-protocol/src/mc_buf/mod.rs b/azalea-protocol/src/mc_buf/mod.rs index a82334fb..1626d163 100755 --- a/azalea-protocol/src/mc_buf/mod.rs +++ b/azalea-protocol/src/mc_buf/mod.rs @@ -4,9 +4,9 @@ mod read; mod write; use packet_macros::{McBufReadable, McBufWritable}; -pub use read::{read_varint_async, McBufReadable, McBufVarintReadable, Readable}; +pub use read::{read_varint_async, McBufReadable, McBufVarReadable, Readable}; use std::ops::Deref; -pub use write::{McBufVarintWritable, McBufWritable, Writable}; +pub use write::{McBufVarWritable, McBufWritable, Writable}; // const DEFAULT_NBT_QUOTA: u32 = 2097152; const MAX_STRING_LENGTH: u16 = 32767; diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 68c9cb3f..72b99222 100755 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -236,11 +236,11 @@ where fn read_into(buf: &mut impl Read) -> Result; } -pub trait McBufVarintReadable +pub trait McBufVarReadable where Self: Sized, { - fn varint_read_into(buf: &mut impl Read) -> Result; + fn var_read_into(buf: &mut impl Read) -> Result; } impl McBufReadable for i32 { @@ -249,12 +249,34 @@ impl McBufReadable for i32 { } } -impl McBufVarintReadable for i32 { - fn varint_read_into(buf: &mut impl Read) -> Result { +impl McBufVarReadable for i32 { + fn var_read_into(buf: &mut impl Read) -> Result { buf.read_varint() } } +impl McBufVarReadable for i64 { + // fast varints modified from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L54 + fn var_read_into(buf: &mut impl Read) -> Result { + let mut buffer = [0]; + let mut ans = 0; + for i in 0..8 { + buf.read_exact(&mut buffer) + .map_err(|_| "Invalid VarLong".to_string())?; + ans |= ((buffer[0] & 0b0111_1111) as i64) << 7 * i; + if buffer[0] & 0b1000_0000 == 0 { + break; + } + } + Ok(ans) + } +} +impl McBufVarReadable for u64 { + fn var_read_into(buf: &mut impl Read) -> Result { + i64::var_read_into(buf).map(|i| i as u64) + } +} + impl McBufReadable for UnsizedByteArray { fn read_into(buf: &mut impl Read) -> Result { Ok(UnsizedByteArray(buf.read_bytes()?)) @@ -300,8 +322,8 @@ impl McBufReadable for u32 { } // u32 varint -impl McBufVarintReadable for u32 { - fn varint_read_into(buf: &mut impl Read) -> Result { +impl McBufVarReadable for u32 { + fn var_read_into(buf: &mut impl Read) -> Result { buf.read_varint().map(|i| i as u32) } } @@ -321,8 +343,8 @@ impl McBufReadable for i16 { } // u16 varint -impl McBufVarintReadable for u16 { - fn varint_read_into(buf: &mut impl Read) -> Result { +impl McBufVarReadable for u16 { + fn var_read_into(buf: &mut impl Read) -> Result { buf.read_varint().map(|i| i as u16) } } diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index c125bf0b..10271bf8 100755 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -146,8 +146,8 @@ pub trait McBufWritable { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>; } -pub trait McBufVarintWritable { - fn varint_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>; +pub trait McBufVarWritable { + fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>; } impl McBufWritable for i32 { @@ -156,8 +156,8 @@ impl McBufWritable for i32 { } } -impl McBufVarintWritable for i32 { - fn varint_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { +impl McBufVarWritable for i32 { + fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { buf.write_varint(*self) } } @@ -202,9 +202,32 @@ impl McBufWritable for u32 { } // u32 varint -impl McBufVarintWritable for u32 { - fn varint_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - i32::varint_write_into(&(*self as i32), buf) +impl McBufVarWritable for u32 { + fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + i32::var_write_into(&(*self as i32), buf) + } +} + +impl McBufVarWritable for i64 { + fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + let mut buffer = [0]; + let mut cnt = 0; + let mut value = *self; + while value != 0 { + buffer[0] = (value & 0b0111_1111) as u8; + value = (value >> 7) & (i64::max_value() >> 6); + if value != 0 { + buffer[0] |= 0b1000_0000; + } + cnt += buf.write(&mut buffer)?; + } + Ok(()) + } +} + +impl McBufVarWritable for u64 { + fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + i64::var_write_into(&(*self as i64), buf) } } @@ -216,9 +239,9 @@ impl McBufWritable for u16 { } // u16 varint -impl McBufVarintWritable for u16 { - fn varint_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - i32::varint_write_into(&(*self as i32), buf) +impl McBufVarWritable for u16 { + fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + i32::var_write_into(&(*self as i32), buf) } } diff --git a/azalea-protocol/src/packets/game/clientbound_add_entity_packet.rs b/azalea-protocol/src/packets/game/clientbound_add_entity_packet.rs index f45e71c9..55fbd2e1 100644 --- a/azalea-protocol/src/packets/game/clientbound_add_entity_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_add_entity_packet.rs @@ -3,11 +3,11 @@ use uuid::Uuid; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundAddEntityPacket { - #[varint] + #[var] pub id: i32, pub uuid: Uuid, // TODO: have an entity type struct - #[varint] + #[var] pub entity_type: i32, pub x: f64, pub y: f64, diff --git a/azalea-protocol/src/packets/game/clientbound_add_mob_packet.rs b/azalea-protocol/src/packets/game/clientbound_add_mob_packet.rs index 144e74c8..ab72eb59 100644 --- a/azalea-protocol/src/packets/game/clientbound_add_mob_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_add_mob_packet.rs @@ -3,11 +3,11 @@ use uuid::Uuid; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundAddMobPacket { - #[varint] + #[var] pub id: i32, pub uuid: Uuid, // TODO: have an entity type struct - #[varint] + #[var] pub entity_type: i32, pub x: f64, pub y: f64, diff --git a/azalea-protocol/src/packets/game/clientbound_add_player_packet.rs b/azalea-protocol/src/packets/game/clientbound_add_player_packet.rs index 9fe086cb..8f848c99 100644 --- a/azalea-protocol/src/packets/game/clientbound_add_player_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_add_player_packet.rs @@ -3,7 +3,7 @@ use uuid::Uuid; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundAddPlayerPacket { - #[varint] + #[var] pub id: i32, pub uuid: Uuid, pub x: f64, diff --git a/azalea-protocol/src/packets/game/clientbound_entity_velocity_packet.rs b/azalea-protocol/src/packets/game/clientbound_entity_velocity_packet.rs index 94061c51..4fc8f86f 100644 --- a/azalea-protocol/src/packets/game/clientbound_entity_velocity_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_entity_velocity_packet.rs @@ -2,7 +2,7 @@ use packet_macros::GamePacket; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundEntityVelocityPacket { - #[varint] + #[var] pub entity_id: u32, pub x_vel: i16, pub y_vel: i16, diff --git a/azalea-protocol/src/packets/game/clientbound_initialize_border_packet.rs b/azalea-protocol/src/packets/game/clientbound_initialize_border_packet.rs new file mode 100644 index 00000000..435e43cc --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_initialize_border_packet.rs @@ -0,0 +1,17 @@ +use packet_macros::GamePacket; + +#[derive(Clone, Debug, GamePacket)] +pub struct ClientboundInitializeBorderPacket { + pub new_center_x: f64, + pub new_center_z: f64, + pub old_size: f64, + pub new_size: f64, + #[var] + pub lerp_time: u64, + #[var] + pub new_absolute_max_size: u32, + #[var] + pub warning_blocks: u32, + #[var] + pub warning_time: u32, +} diff --git a/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs b/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs index 1d017c2a..22eedb05 100644 --- a/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs @@ -22,7 +22,7 @@ pub struct ClientboundLevelChunkPacketData { pub struct BlockEntity { packed_xz: u8, y: u16, - #[varint] + #[var] type_: i32, data: azalea_nbt::Tag, } diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs index 929b11cd..9c8b7df1 100755 --- a/azalea-protocol/src/packets/game/clientbound_login_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -12,11 +12,11 @@ pub struct ClientboundLoginPacket { pub dimension_type: azalea_nbt::Tag, pub dimension: ResourceLocation, pub seed: i64, - #[varint] + #[var] pub max_players: i32, - #[varint] + #[var] pub chunk_radius: i32, - #[varint] + #[var] pub simulation_distance: i32, pub reduced_debug_info: bool, pub show_death_screen: bool, diff --git a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs index 9c34d06e..8e7ce4fd 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs @@ -30,9 +30,9 @@ pub struct AddPlayer { uuid: Uuid, name: String, properties: Vec, - #[varint] + #[var] gamemode: u32, - #[varint] + #[var] ping: i32, display_name: Option, } @@ -40,14 +40,14 @@ pub struct AddPlayer { #[derive(Clone, Debug, McBufReadable, McBufWritable)] pub struct UpdateGameMode { uuid: Uuid, - #[varint] + #[var] gamemode: u32, } #[derive(Clone, Debug, McBufReadable, McBufWritable)] pub struct UpdateLatency { uuid: Uuid, - #[varint] + #[var] ping: i32, } diff --git a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs index cac4665d..86266e9f 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs @@ -12,7 +12,7 @@ pub struct ClientboundPlayerPositionPacket { pub relative_arguments: RelativeArguments, /// Client should confirm this packet with Teleport Confirm containing the /// same Teleport ID. - #[varint] + #[var] pub id: i32, pub dismount_vehicle: bool, } diff --git a/azalea-protocol/src/packets/game/clientbound_set_chunk_cache_center.rs b/azalea-protocol/src/packets/game/clientbound_set_chunk_cache_center.rs index 482dc0c7..f4d59e32 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_chunk_cache_center.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_chunk_cache_center.rs @@ -2,8 +2,8 @@ use packet_macros::GamePacket; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundSetChunkCacheCenterPacket { - #[varint] + #[var] pub x: i32, - #[varint] + #[var] pub y: i32, } diff --git a/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs index 5d288518..03df8e13 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs @@ -10,7 +10,7 @@ use uuid::Uuid; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundSetEntityDataPacket { - #[varint] + #[var] pub id: i32, pub metadata: Vec, } @@ -142,17 +142,17 @@ pub enum Pose { #[derive(Debug, Clone, McBufReadable, McBufWritable)] pub struct VillagerData { - #[varint] + #[var] type_: u32, - #[varint] + #[var] profession: u32, - #[varint] + #[var] level: u32, } #[derive(Debug, Clone, McBufReadable, McBufWritable)] pub struct Particle { - #[varint] + #[var] pub id: i32, pub data: ParticleData, } @@ -251,7 +251,7 @@ pub enum ParticleData { #[derive(Debug, Clone, McBufReadable, McBufWritable)] pub struct BlockParticle { - #[varint] + #[var] pub block_state: i32, } #[derive(Debug, Clone, McBufReadable, McBufWritable)] @@ -294,9 +294,9 @@ pub struct VibrationParticle { pub origin: BlockPos, pub position_type: String, pub block_position: BlockPos, - #[varint] + #[var] pub entity_id: u32, - #[varint] + #[var] pub ticks: u32, } diff --git a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs index 82f34f96..3d83e6fb 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs @@ -6,7 +6,7 @@ use uuid::Uuid; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundUpdateAttributesPacket { - #[varint] + #[var] pub entity_id: u32, pub attributes: Vec, } diff --git a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs index d15e10c3..1c87a472 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs @@ -74,7 +74,7 @@ pub struct CookingRecipe { ingredient: Ingredient, result: Slot, experience: f32, - #[varint] + #[var] cooking_time: u32, } #[derive(Clone, Debug, McBufReadable, McBufWritable)] diff --git a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs index 1f988fe5..fe65d048 100755 --- a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs @@ -2,6 +2,6 @@ use packet_macros::GamePacket; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundUpdateViewDistancePacket { - #[varint] + #[var] pub view_distance: i32, } diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 81980723..852a9525 100755 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -7,6 +7,7 @@ pub mod clientbound_declare_commands_packet; pub mod clientbound_disconnect_packet; pub mod clientbound_entity_event_packet; pub mod clientbound_entity_velocity_packet; +pub mod clientbound_initialize_border_packet; pub mod clientbound_level_chunk_with_light_packet; pub mod clientbound_light_update_packet; pub mod clientbound_login_packet; @@ -40,6 +41,7 @@ declare_state_packets!( 0x1a: clientbound_disconnect_packet::ClientboundDisconnectPacket, 0x1b: clientbound_entity_event_packet::ClientboundEntityEventPacket, 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, + 0x20: clientbound_initialize_border_packet::ClientboundInitializeBorderPacket, 0x22: clientbound_level_chunk_with_light_packet::ClientboundLevelChunkWithLightPacket, 0x25: clientbound_light_update_packet::ClientboundLightUpdatePacket, 0x26: clientbound_login_packet::ClientboundLoginPacket, diff --git a/azalea-protocol/src/packets/handshake/client_intention_packet.rs b/azalea-protocol/src/packets/handshake/client_intention_packet.rs index 6216ddc4..98caf34c 100755 --- a/azalea-protocol/src/packets/handshake/client_intention_packet.rs +++ b/azalea-protocol/src/packets/handshake/client_intention_packet.rs @@ -4,7 +4,7 @@ use std::hash::Hash; #[derive(Hash, Clone, Debug, HandshakePacket)] pub struct ClientIntentionPacket { - #[varint] + #[var] pub protocol_version: u32, pub hostname: String, pub port: u16, diff --git a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs index 9e1e1df5..fc5dd1a2 100755 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -5,7 +5,7 @@ use std::hash::Hash; #[derive(Hash, Clone, Debug, LoginPacket)] pub struct ClientboundCustomQueryPacket { - #[varint] + #[var] pub transaction_id: u32, pub identifier: ResourceLocation, pub data: UnsizedByteArray,