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

add a couple more packets

This commit is contained in:
mat 2022-01-01 19:44:51 -06:00
parent 1a961d968b
commit e81b85dd5b
7 changed files with 93 additions and 23 deletions

View file

@ -64,6 +64,12 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> {
GamePacket::ClientboundLoginPacket(p) => { GamePacket::ClientboundLoginPacket(p) => {
println!("Got login packet {:?}", p); println!("Got login packet {:?}", p);
} }
GamePacket::ClientboundUpdateViewDistancePacket(p) => {
println!("Got view distance packet {:?}", p);
}
GamePacket::ClientboundCustomPayloadPacket(p) => {
println!("Got custom payload packet {:?}", p);
}
// GamePacket::ClientboundKeepAlivePacket(p) => { // GamePacket::ClientboundKeepAlivePacket(p) => {
// println!("Got keep alive packet {:?}", p.keep_alive_id); // println!("Got keep alive packet {:?}", p.keep_alive_id);
// } // }

View file

@ -166,7 +166,8 @@ pub trait Readable {
fn get_varint_size(&mut self, value: i32) -> u8; fn get_varint_size(&mut self, value: i32) -> u8;
fn get_varlong_size(&mut self, value: i32) -> u8; fn get_varlong_size(&mut self, value: i32) -> u8;
async fn read_byte_array(&mut self) -> Result<Vec<u8>, String>; async fn read_byte_array(&mut self) -> Result<Vec<u8>, String>;
async fn read_bytes(&mut self, n: usize) -> Result<Vec<u8>, String>; async fn read_bytes_with_len(&mut self, n: usize) -> Result<Vec<u8>, String>;
async fn read_bytes(&mut self) -> Result<Vec<u8>, String>;
async fn read_utf(&mut self) -> Result<String, String>; async fn read_utf(&mut self) -> Result<String, String>;
async fn read_utf_with_len(&mut self, max_length: u32) -> Result<String, String>; async fn read_utf_with_len(&mut self, max_length: u32) -> Result<String, String>;
async fn read_byte(&mut self) -> Result<u8, String>; async fn read_byte(&mut self) -> Result<u8, String>;
@ -230,10 +231,10 @@ where
async fn read_byte_array(&mut self) -> Result<Vec<u8>, String> { async fn read_byte_array(&mut self) -> Result<Vec<u8>, String> {
let length = self.read_varint().await? as usize; let length = self.read_varint().await? as usize;
Ok(self.read_bytes(length).await?) Ok(self.read_bytes_with_len(length).await?)
} }
async fn read_bytes(&mut self, n: usize) -> Result<Vec<u8>, String> { async fn read_bytes_with_len(&mut self, n: usize) -> Result<Vec<u8>, String> {
let mut bytes = vec![0; n]; let mut bytes = vec![0; n];
match AsyncReadExt::read_exact(self, &mut bytes).await { match AsyncReadExt::read_exact(self, &mut bytes).await {
Ok(_) => Ok(bytes), Ok(_) => Ok(bytes),
@ -241,6 +242,15 @@ where
} }
} }
async fn read_bytes(&mut self) -> Result<Vec<u8>, String> {
// read to end of the buffer
let mut bytes = vec![];
AsyncReadExt::read_to_end(self, &mut bytes)
.await
.map_err(|_| "Error reading bytes".to_string())?;
Ok(bytes)
}
async fn read_utf(&mut self) -> Result<String, String> { async fn read_utf(&mut self) -> Result<String, String> {
self.read_utf_with_len(MAX_STRING_LENGTH.into()).await self.read_utf_with_len(MAX_STRING_LENGTH.into()).await
} }

View file

@ -0,0 +1,30 @@
use super::GamePacket;
use crate::mc_buf::{Readable, Writable};
use azalea_core::{game_type::GameType, resource_location::ResourceLocation};
#[derive(Clone, Debug)]
pub struct ClientboundCustomPayloadPacket {
pub identifier: ResourceLocation,
pub data: Vec<u8>,
}
impl ClientboundCustomPayloadPacket {
pub fn get(self) -> GamePacket {
GamePacket::ClientboundCustomPayloadPacket(self)
}
pub fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_resource_location(&self.identifier)?;
buf.write_bytes(&self.data)?;
Ok(())
}
pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
buf: &mut T,
) -> Result<GamePacket, String> {
let identifier = buf.read_resource_location().await?;
let data = buf.read_bytes().await?;
Ok(ClientboundCustomPayloadPacket { identifier, data }.get())
}
}

View file

@ -4,23 +4,6 @@ use azalea_core::{game_type::GameType, resource_location::ResourceLocation};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ClientboundLoginPacket { pub struct ClientboundLoginPacket {
// private final int playerId;
// private final boolean hardcore;
// private final GameType gameType;
// @Nullable
// private final GameType previousGameType;
// private final Set<ResourceKey<Level>> levels;
// private final RegistryAccess.RegistryHolder registryHolder;
// private final DimensionType dimensionType;
// private final ResourceKey<Level> dimension;
// private final long seed;
// private final int maxPlayers;
// private final int chunkRadius;
// private final int simulationDistance;
// private final boolean reducedDebugInfo;
// private final boolean showDeathScreen;
// private final boolean isDebug;
// private final boolean isFlat;
pub player_id: i32, pub player_id: i32,
pub hardcore: bool, pub hardcore: bool,
pub game_type: GameType, pub game_type: GameType,

View file

@ -0,0 +1,28 @@
// i don't know the actual name of this packet, i couldn't find it in the source code!
use super::GamePacket;
use crate::mc_buf::{Readable, Writable};
#[derive(Clone, Debug)]
pub struct ClientboundUpdateViewDistancePacket {
pub view_distance: i32,
}
impl ClientboundUpdateViewDistancePacket {
pub fn get(self) -> GamePacket {
GamePacket::ClientboundUpdateViewDistancePacket(self)
}
pub fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_varint(self.view_distance)?;
Ok(())
}
pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
buf: &mut T,
) -> Result<GamePacket, String> {
let view_distance = buf.read_varint().await?;
Ok(ClientboundUpdateViewDistancePacket { view_distance }.get())
}
}

View file

@ -1,4 +1,6 @@
pub mod clientbound_custom_payload_packet;
pub mod clientbound_login_packet; pub mod clientbound_login_packet;
pub mod clientbound_update_view_distance_packet;
use super::ProtocolPacket; use super::ProtocolPacket;
use crate::connect::PacketFlow; use crate::connect::PacketFlow;
@ -10,13 +12,21 @@ where
Self: Sized, Self: Sized,
{ {
ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket), ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket),
ClientboundUpdateViewDistancePacket(
clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket,
),
ClientboundCustomPayloadPacket(
clientbound_custom_payload_packet::ClientboundCustomPayloadPacket,
),
} }
#[async_trait] #[async_trait]
impl ProtocolPacket for GamePacket { impl ProtocolPacket for GamePacket {
fn id(&self) -> u32 { fn id(&self) -> u32 {
match self { match self {
GamePacket::ClientboundLoginPacket(_packet) => 0x00, GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18,
GamePacket::ClientboundLoginPacket(_packet) => 0x26,
GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a,
} }
} }
@ -33,8 +43,11 @@ impl ProtocolPacket for GamePacket {
{ {
Ok(match flow { Ok(match flow {
PacketFlow::ServerToClient => match id { PacketFlow::ServerToClient => match id {
0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?,
0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?,
0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket
::read(buf)
.await?,
_ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)),
}, },
PacketFlow::ClientToServer => match id { PacketFlow::ClientToServer => match id {

View file

@ -26,7 +26,7 @@ impl ClientboundCustomQueryPacket {
) -> Result<LoginPacket, String> { ) -> Result<LoginPacket, String> {
let transaction_id = buf.read_varint().await? as u32; let transaction_id = buf.read_varint().await? as u32;
let identifier = ResourceLocation::new(&buf.read_utf().await?)?; let identifier = ResourceLocation::new(&buf.read_utf().await?)?;
let data = buf.read_bytes(1048576).await?; let data = buf.read_bytes_with_len(1048576).await?;
Ok(ClientboundCustomQueryPacket { Ok(ClientboundCustomQueryPacket {
transaction_id, transaction_id,
identifier, identifier,