1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

Initialize worldborder packet

Also add varlong and replace #[varint] with #[var]
This commit is contained in:
mat 2022-05-07 11:58:00 -05:00
parent 79bf577130
commit b9c31efc01
23 changed files with 130 additions and 60 deletions

View file

@ -14,3 +14,6 @@ members = [
[profile.release] [profile.release]
debug = true debug = true
[profile.dev.package.azalea-crypto]
opt-level = 3

View file

@ -258,6 +258,9 @@ impl Client {
GamePacket::ClientboundAddPlayerPacket(p) => { GamePacket::ClientboundAddPlayerPacket(p) => {
println!("Got add player packet {:?}", p); println!("Got add player packet {:?}", p);
} }
GamePacket::ClientboundInitializeBorderPacket(p) => {
println!("Got initialize border packet {:?}", p);
}
_ => panic!("Unexpected packet {:?}", packet), _ => panic!("Unexpected packet {:?}", packet),
} }
println!(); println!();

View file

@ -23,9 +23,9 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt
// if it's a string, use buf.write_string // if it's a string, use buf.write_string
match field_type { match field_type {
syn::Type::Path(_) => { 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! { quote! {
let #field_name = crate::mc_buf::McBufVarintReadable::varint_read_into(buf)?; let #field_name = crate::mc_buf::McBufVarReadable::var_read_into(buf)?;
} }
} else { } else {
quote! { 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 // if it's a string, use buf.write_string
match field_type { match field_type {
syn::Type::Path(_) => { 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! { quote! {
crate::mc_buf::McBufVarintWritable::varint_write_into(&self.#field_name, buf)?; crate::mc_buf::McBufVarWritable::var_write_into(&self.#field_name, buf)?;
} }
} else { } else {
quote! { 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 { pub fn derive_mcbufreadable(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input); let DeriveInput { ident, data, .. } = parse_macro_input!(input);
create_impl_mcbufreadable(&ident, &data).into() 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 { pub fn derive_mcbufwritable(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input); 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() contents.into()
} }
#[proc_macro_derive(GamePacket, attributes(varint))] #[proc_macro_derive(GamePacket, attributes(var))]
pub fn derive_game_packet(input: TokenStream) -> TokenStream { pub fn derive_game_packet(input: TokenStream) -> TokenStream {
as_packet_derive(input, quote! {crate::packets::game::GamePacket}) 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 { pub fn derive_handshake_packet(input: TokenStream) -> TokenStream {
as_packet_derive(input, quote! {crate::packets::handshake::HandshakePacket}) 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 { pub fn derive_login_packet(input: TokenStream) -> TokenStream {
as_packet_derive(input, quote! {crate::packets::login::LoginPacket}) 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 { pub fn derive_status_packet(input: TokenStream) -> TokenStream {
as_packet_derive(input, quote! {crate::packets::status::StatusPacket}) as_packet_derive(input, quote! {crate::packets::status::StatusPacket})
} }

View file

@ -4,9 +4,9 @@ mod read;
mod write; mod write;
use packet_macros::{McBufReadable, McBufWritable}; 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; use std::ops::Deref;
pub use write::{McBufVarintWritable, McBufWritable, Writable}; pub use write::{McBufVarWritable, McBufWritable, Writable};
// const DEFAULT_NBT_QUOTA: u32 = 2097152; // const DEFAULT_NBT_QUOTA: u32 = 2097152;
const MAX_STRING_LENGTH: u16 = 32767; const MAX_STRING_LENGTH: u16 = 32767;

View file

@ -236,11 +236,11 @@ where
fn read_into(buf: &mut impl Read) -> Result<Self, String>; fn read_into(buf: &mut impl Read) -> Result<Self, String>;
} }
pub trait McBufVarintReadable pub trait McBufVarReadable
where where
Self: Sized, Self: Sized,
{ {
fn varint_read_into(buf: &mut impl Read) -> Result<Self, String>; fn var_read_into(buf: &mut impl Read) -> Result<Self, String>;
} }
impl McBufReadable for i32 { impl McBufReadable for i32 {
@ -249,12 +249,34 @@ impl McBufReadable for i32 {
} }
} }
impl McBufVarintReadable for i32 { impl McBufVarReadable for i32 {
fn varint_read_into(buf: &mut impl Read) -> Result<Self, String> { fn var_read_into(buf: &mut impl Read) -> Result<Self, String> {
buf.read_varint() 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<Self, String> {
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<Self, String> {
i64::var_read_into(buf).map(|i| i as u64)
}
}
impl McBufReadable for UnsizedByteArray { impl McBufReadable for UnsizedByteArray {
fn read_into(buf: &mut impl Read) -> Result<Self, String> { fn read_into(buf: &mut impl Read) -> Result<Self, String> {
Ok(UnsizedByteArray(buf.read_bytes()?)) Ok(UnsizedByteArray(buf.read_bytes()?))
@ -300,8 +322,8 @@ impl McBufReadable for u32 {
} }
// u32 varint // u32 varint
impl McBufVarintReadable for u32 { impl McBufVarReadable for u32 {
fn varint_read_into(buf: &mut impl Read) -> Result<Self, String> { fn var_read_into(buf: &mut impl Read) -> Result<Self, String> {
buf.read_varint().map(|i| i as u32) buf.read_varint().map(|i| i as u32)
} }
} }
@ -321,8 +343,8 @@ impl McBufReadable for i16 {
} }
// u16 varint // u16 varint
impl McBufVarintReadable for u16 { impl McBufVarReadable for u16 {
fn varint_read_into(buf: &mut impl Read) -> Result<Self, String> { fn var_read_into(buf: &mut impl Read) -> Result<Self, String> {
buf.read_varint().map(|i| i as u16) buf.read_varint().map(|i| i as u16)
} }
} }

View file

@ -146,8 +146,8 @@ pub trait McBufWritable {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>; fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>;
} }
pub trait McBufVarintWritable { pub trait McBufVarWritable {
fn varint_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>; fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error>;
} }
impl McBufWritable for i32 { impl McBufWritable for i32 {
@ -156,8 +156,8 @@ impl McBufWritable for i32 {
} }
} }
impl McBufVarintWritable for i32 { impl McBufVarWritable for i32 {
fn varint_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
buf.write_varint(*self) buf.write_varint(*self)
} }
} }
@ -202,9 +202,32 @@ impl McBufWritable for u32 {
} }
// u32 varint // u32 varint
impl McBufVarintWritable for u32 { impl McBufVarWritable for u32 {
fn varint_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i32::varint_write_into(&(*self as i32), buf) 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 // u16 varint
impl McBufVarintWritable for u16 { impl McBufVarWritable for u16 {
fn varint_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i32::varint_write_into(&(*self as i32), buf) i32::var_write_into(&(*self as i32), buf)
} }
} }

View file

@ -3,11 +3,11 @@ use uuid::Uuid;
#[derive(Clone, Debug, GamePacket)] #[derive(Clone, Debug, GamePacket)]
pub struct ClientboundAddEntityPacket { pub struct ClientboundAddEntityPacket {
#[varint] #[var]
pub id: i32, pub id: i32,
pub uuid: Uuid, pub uuid: Uuid,
// TODO: have an entity type struct // TODO: have an entity type struct
#[varint] #[var]
pub entity_type: i32, pub entity_type: i32,
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,

View file

@ -3,11 +3,11 @@ use uuid::Uuid;
#[derive(Clone, Debug, GamePacket)] #[derive(Clone, Debug, GamePacket)]
pub struct ClientboundAddMobPacket { pub struct ClientboundAddMobPacket {
#[varint] #[var]
pub id: i32, pub id: i32,
pub uuid: Uuid, pub uuid: Uuid,
// TODO: have an entity type struct // TODO: have an entity type struct
#[varint] #[var]
pub entity_type: i32, pub entity_type: i32,
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,

View file

@ -3,7 +3,7 @@ use uuid::Uuid;
#[derive(Clone, Debug, GamePacket)] #[derive(Clone, Debug, GamePacket)]
pub struct ClientboundAddPlayerPacket { pub struct ClientboundAddPlayerPacket {
#[varint] #[var]
pub id: i32, pub id: i32,
pub uuid: Uuid, pub uuid: Uuid,
pub x: f64, pub x: f64,

View file

@ -2,7 +2,7 @@ use packet_macros::GamePacket;
#[derive(Clone, Debug, GamePacket)] #[derive(Clone, Debug, GamePacket)]
pub struct ClientboundEntityVelocityPacket { pub struct ClientboundEntityVelocityPacket {
#[varint] #[var]
pub entity_id: u32, pub entity_id: u32,
pub x_vel: i16, pub x_vel: i16,
pub y_vel: i16, pub y_vel: i16,

View file

@ -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,
}

View file

@ -22,7 +22,7 @@ pub struct ClientboundLevelChunkPacketData {
pub struct BlockEntity { pub struct BlockEntity {
packed_xz: u8, packed_xz: u8,
y: u16, y: u16,
#[varint] #[var]
type_: i32, type_: i32,
data: azalea_nbt::Tag, data: azalea_nbt::Tag,
} }

View file

@ -12,11 +12,11 @@ pub struct ClientboundLoginPacket {
pub dimension_type: azalea_nbt::Tag, pub dimension_type: azalea_nbt::Tag,
pub dimension: ResourceLocation, pub dimension: ResourceLocation,
pub seed: i64, pub seed: i64,
#[varint] #[var]
pub max_players: i32, pub max_players: i32,
#[varint] #[var]
pub chunk_radius: i32, pub chunk_radius: i32,
#[varint] #[var]
pub simulation_distance: i32, pub simulation_distance: i32,
pub reduced_debug_info: bool, pub reduced_debug_info: bool,
pub show_death_screen: bool, pub show_death_screen: bool,

View file

@ -30,9 +30,9 @@ pub struct AddPlayer {
uuid: Uuid, uuid: Uuid,
name: String, name: String,
properties: Vec<PlayerProperty>, properties: Vec<PlayerProperty>,
#[varint] #[var]
gamemode: u32, gamemode: u32,
#[varint] #[var]
ping: i32, ping: i32,
display_name: Option<Component>, display_name: Option<Component>,
} }
@ -40,14 +40,14 @@ pub struct AddPlayer {
#[derive(Clone, Debug, McBufReadable, McBufWritable)] #[derive(Clone, Debug, McBufReadable, McBufWritable)]
pub struct UpdateGameMode { pub struct UpdateGameMode {
uuid: Uuid, uuid: Uuid,
#[varint] #[var]
gamemode: u32, gamemode: u32,
} }
#[derive(Clone, Debug, McBufReadable, McBufWritable)] #[derive(Clone, Debug, McBufReadable, McBufWritable)]
pub struct UpdateLatency { pub struct UpdateLatency {
uuid: Uuid, uuid: Uuid,
#[varint] #[var]
ping: i32, ping: i32,
} }

View file

@ -12,7 +12,7 @@ pub struct ClientboundPlayerPositionPacket {
pub relative_arguments: RelativeArguments, pub relative_arguments: RelativeArguments,
/// Client should confirm this packet with Teleport Confirm containing the /// Client should confirm this packet with Teleport Confirm containing the
/// same Teleport ID. /// same Teleport ID.
#[varint] #[var]
pub id: i32, pub id: i32,
pub dismount_vehicle: bool, pub dismount_vehicle: bool,
} }

View file

@ -2,8 +2,8 @@ use packet_macros::GamePacket;
#[derive(Clone, Debug, GamePacket)] #[derive(Clone, Debug, GamePacket)]
pub struct ClientboundSetChunkCacheCenterPacket { pub struct ClientboundSetChunkCacheCenterPacket {
#[varint] #[var]
pub x: i32, pub x: i32,
#[varint] #[var]
pub y: i32, pub y: i32,
} }

View file

@ -10,7 +10,7 @@ use uuid::Uuid;
#[derive(Clone, Debug, GamePacket)] #[derive(Clone, Debug, GamePacket)]
pub struct ClientboundSetEntityDataPacket { pub struct ClientboundSetEntityDataPacket {
#[varint] #[var]
pub id: i32, pub id: i32,
pub metadata: Vec<EntityDataItem>, pub metadata: Vec<EntityDataItem>,
} }
@ -142,17 +142,17 @@ pub enum Pose {
#[derive(Debug, Clone, McBufReadable, McBufWritable)] #[derive(Debug, Clone, McBufReadable, McBufWritable)]
pub struct VillagerData { pub struct VillagerData {
#[varint] #[var]
type_: u32, type_: u32,
#[varint] #[var]
profession: u32, profession: u32,
#[varint] #[var]
level: u32, level: u32,
} }
#[derive(Debug, Clone, McBufReadable, McBufWritable)] #[derive(Debug, Clone, McBufReadable, McBufWritable)]
pub struct Particle { pub struct Particle {
#[varint] #[var]
pub id: i32, pub id: i32,
pub data: ParticleData, pub data: ParticleData,
} }
@ -251,7 +251,7 @@ pub enum ParticleData {
#[derive(Debug, Clone, McBufReadable, McBufWritable)] #[derive(Debug, Clone, McBufReadable, McBufWritable)]
pub struct BlockParticle { pub struct BlockParticle {
#[varint] #[var]
pub block_state: i32, pub block_state: i32,
} }
#[derive(Debug, Clone, McBufReadable, McBufWritable)] #[derive(Debug, Clone, McBufReadable, McBufWritable)]
@ -294,9 +294,9 @@ pub struct VibrationParticle {
pub origin: BlockPos, pub origin: BlockPos,
pub position_type: String, pub position_type: String,
pub block_position: BlockPos, pub block_position: BlockPos,
#[varint] #[var]
pub entity_id: u32, pub entity_id: u32,
#[varint] #[var]
pub ticks: u32, pub ticks: u32,
} }

View file

@ -6,7 +6,7 @@ use uuid::Uuid;
#[derive(Clone, Debug, GamePacket)] #[derive(Clone, Debug, GamePacket)]
pub struct ClientboundUpdateAttributesPacket { pub struct ClientboundUpdateAttributesPacket {
#[varint] #[var]
pub entity_id: u32, pub entity_id: u32,
pub attributes: Vec<AttributeSnapshot>, pub attributes: Vec<AttributeSnapshot>,
} }

View file

@ -74,7 +74,7 @@ pub struct CookingRecipe {
ingredient: Ingredient, ingredient: Ingredient,
result: Slot, result: Slot,
experience: f32, experience: f32,
#[varint] #[var]
cooking_time: u32, cooking_time: u32,
} }
#[derive(Clone, Debug, McBufReadable, McBufWritable)] #[derive(Clone, Debug, McBufReadable, McBufWritable)]

View file

@ -2,6 +2,6 @@ use packet_macros::GamePacket;
#[derive(Clone, Debug, GamePacket)] #[derive(Clone, Debug, GamePacket)]
pub struct ClientboundUpdateViewDistancePacket { pub struct ClientboundUpdateViewDistancePacket {
#[varint] #[var]
pub view_distance: i32, pub view_distance: i32,
} }

View file

@ -7,6 +7,7 @@ pub mod clientbound_declare_commands_packet;
pub mod clientbound_disconnect_packet; pub mod clientbound_disconnect_packet;
pub mod clientbound_entity_event_packet; pub mod clientbound_entity_event_packet;
pub mod clientbound_entity_velocity_packet; pub mod clientbound_entity_velocity_packet;
pub mod clientbound_initialize_border_packet;
pub mod clientbound_level_chunk_with_light_packet; pub mod clientbound_level_chunk_with_light_packet;
pub mod clientbound_light_update_packet; pub mod clientbound_light_update_packet;
pub mod clientbound_login_packet; pub mod clientbound_login_packet;
@ -40,6 +41,7 @@ declare_state_packets!(
0x1a: clientbound_disconnect_packet::ClientboundDisconnectPacket, 0x1a: clientbound_disconnect_packet::ClientboundDisconnectPacket,
0x1b: clientbound_entity_event_packet::ClientboundEntityEventPacket, 0x1b: clientbound_entity_event_packet::ClientboundEntityEventPacket,
0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket,
0x20: clientbound_initialize_border_packet::ClientboundInitializeBorderPacket,
0x22: clientbound_level_chunk_with_light_packet::ClientboundLevelChunkWithLightPacket, 0x22: clientbound_level_chunk_with_light_packet::ClientboundLevelChunkWithLightPacket,
0x25: clientbound_light_update_packet::ClientboundLightUpdatePacket, 0x25: clientbound_light_update_packet::ClientboundLightUpdatePacket,
0x26: clientbound_login_packet::ClientboundLoginPacket, 0x26: clientbound_login_packet::ClientboundLoginPacket,

View file

@ -4,7 +4,7 @@ use std::hash::Hash;
#[derive(Hash, Clone, Debug, HandshakePacket)] #[derive(Hash, Clone, Debug, HandshakePacket)]
pub struct ClientIntentionPacket { pub struct ClientIntentionPacket {
#[varint] #[var]
pub protocol_version: u32, pub protocol_version: u32,
pub hostname: String, pub hostname: String,
pub port: u16, pub port: u16,

View file

@ -5,7 +5,7 @@ use std::hash::Hash;
#[derive(Hash, Clone, Debug, LoginPacket)] #[derive(Hash, Clone, Debug, LoginPacket)]
pub struct ClientboundCustomQueryPacket { pub struct ClientboundCustomQueryPacket {
#[varint] #[var]
pub transaction_id: u32, pub transaction_id: u32,
pub identifier: ResourceLocation, pub identifier: ResourceLocation,
pub data: UnsizedByteArray, pub data: UnsizedByteArray,