Implement more v1.17.1 packets [WIP]

This commit is contained in:
timvisee 2021-11-13 22:33:25 +01:00
parent 39751ce604
commit a4fc2bcf7b
No known key found for this signature in database
GPG Key ID: B8DB720BC383E172
3 changed files with 98 additions and 64 deletions

View File

@ -222,6 +222,22 @@ impl Decoder for Vec<CompoundTag> {
} }
} }
impl Decoder for Vec<String> {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
let length = reader.read_var_i32()? as usize;
let mut vec = Vec::with_capacity(length);
for _ in 0..length {
let identifier = reader.read_string(32767)?;
vec.push(identifier);
}
Ok(vec)
}
}
pub mod var_int { pub mod var_int {
use crate::decoder::DecoderReadExt; use crate::decoder::DecoderReadExt;
use crate::error::DecodeError; use crate::error::DecodeError;
@ -261,8 +277,17 @@ pub mod uuid_hyp_str {
use uuid::Uuid; use uuid::Uuid;
pub fn decode<R: Read>(reader: &mut R) -> Result<Uuid, DecodeError> { pub fn decode<R: Read>(reader: &mut R) -> Result<Uuid, DecodeError> {
let uuid_hyphenated_string = reader.read_string(36)?; // TODO(timvisee): use custom encoder for this, rather than putting this in uuid_hyp_str
let uuid = Uuid::parse_str(&uuid_hyphenated_string)?;
let data = [reader.read_var_i64()?, reader.read_var_i64()?];
// TODO(timvisee): remove unsafe
let raw = unsafe { std::mem::transmute(data) };
let uuid = Uuid::from_bytes(raw);
// let uuid_hyphenated_string = reader.read_string(36)?;
// let uuid = Uuid::parse_str(&uuid_hyphenated_string)?;
Ok(uuid) Ok(uuid)
} }

View File

@ -183,7 +183,7 @@ impl Encoder for Vec<CompoundTag> {
} }
} }
// TODO: identifier encoder, we might want a custom type // TODO(timvisee): identifier encoder, we might want a custom type
impl Encoder for Vec<String> { impl Encoder for Vec<String> {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> { fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
writer.write_var_i32(self.len() as i32)?; writer.write_var_i32(self.len() as i32)?;
@ -196,19 +196,6 @@ impl Encoder for Vec<String> {
} }
} }
// TODO: BitSet encoder, implement this
// TODO: impl Encoder for Vec<i64> {
// TODO: fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
// TODO: writer.write_var_i32(self.len() as i32)?;
// TODO: for mask in self {
// TODO: writer.write_i64::<BigEndian>(*mask)?
// TODO: }
// TODO: Ok(())
// TODO: }
// TODO: }
pub mod var_int { pub mod var_int {
use crate::encoder::EncoderWriteExt; use crate::encoder::EncoderWriteExt;
use crate::error::EncodeError; use crate::error::EncodeError;
@ -251,7 +238,7 @@ pub mod uuid_hyp_str {
use uuid::{Uuid, Version}; use uuid::{Uuid, Version};
pub fn encode<W: Write>(value: &Uuid, writer: &mut W) -> Result<(), EncodeError> { pub fn encode<W: Write>(value: &Uuid, writer: &mut W) -> Result<(), EncodeError> {
// TODO: use custom encoder for this, rather than putting this in uuid_hyp_str // TODO(timvisee): use custom encoder for this, rather than putting this in uuid_hyp_str
match value.get_version() { match value.get_version() {
Some(Version::Md5) => { Some(Version::Md5) => {
writer.write_all(value.as_bytes())?; writer.write_all(value.as_bytes())?;

View File

@ -27,8 +27,13 @@ pub enum GameClientBoundPacket {
BossBar(BossBar), BossBar(BossBar),
EntityAction(EntityAction), EntityAction(EntityAction),
Respawn(Respawn),
PlayerPositionAndLook(PlayerPositionAndLook), PlayerPositionAndLook(PlayerPositionAndLook),
SpawnPosition(SpawnPosition),
SetTitleSubtitle(SetTitleSubtitle),
SetTitleText(SetTitleText),
TimeUpdate(TimeUpdate), TimeUpdate(TimeUpdate),
SetTitleTimes(SetTitleTimes),
} }
impl GameServerBoundPacket { impl GameServerBoundPacket {
@ -68,7 +73,12 @@ impl GameClientBoundPacket {
GameClientBoundPacket::BossBar(_) => 0x0D, GameClientBoundPacket::BossBar(_) => 0x0D,
GameClientBoundPacket::EntityAction(_) => 0x1B, GameClientBoundPacket::EntityAction(_) => 0x1B,
GameClientBoundPacket::PlayerPositionAndLook(_) => 0x38, GameClientBoundPacket::PlayerPositionAndLook(_) => 0x38,
GameClientBoundPacket::Respawn(_) => 0x3D,
GameClientBoundPacket::SpawnPosition(_) => 0x4B,
GameClientBoundPacket::SetTitleSubtitle(_) => 0x57,
GameClientBoundPacket::TimeUpdate(_) => 0x58, GameClientBoundPacket::TimeUpdate(_) => 0x58,
GameClientBoundPacket::SetTitleText(_) => 0x59,
GameClientBoundPacket::SetTitleTimes(_) => 0x5A,
} }
} }
@ -94,31 +104,56 @@ impl GameClientBoundPacket {
Ok(GameClientBoundPacket::ChunkData(chunk_data)) Ok(GameClientBoundPacket::ChunkData(chunk_data))
} }
// TODO: 0x25 => { 0x25 => {
// TODO: let join_game = JoinGame::decode(reader)?; let join_game = JoinGame::decode(reader)?;
// TODO: Ok(GameClientBoundPacket::JoinGame(join_game)) Ok(GameClientBoundPacket::JoinGame(join_game))
// TODO: } }
// TODO: 0x38 => { 0x3D => {
// TODO: let player_position = PlayerPositionAndLook::decode(reader)?; let respawn = Respawn::decode(reader)?;
// TODO: Ok(GameClientBoundPacket::PlayerPositionAndLook( Ok(GameClientBoundPacket::Respawn(respawn))
// TODO: player_position, }
// TODO: )) 0x38 => {
// TODO: } let player_position = PlayerPositionAndLook::decode(reader)?;
// TODO: 0x58 => {
// TODO: let time_update = TimeUpdate::decode(reader)?;
// TODO: Ok(GameClientBoundPacket::TimeUpdate(time_update)) Ok(GameClientBoundPacket::PlayerPositionAndLook(
// TODO: } player_position,
))
}
0x4B => {
let spawn_position = SpawnPosition::decode(reader)?;
Ok(GameClientBoundPacket::SpawnPosition(spawn_position))
}
0x57 => {
let title_subtitle = SetTitleSubtitle::decode(reader)?;
Ok(GameClientBoundPacket::SetTitleSubtitle(title_subtitle))
}
0x58 => {
let time_update = TimeUpdate::decode(reader)?;
Ok(GameClientBoundPacket::TimeUpdate(time_update))
}
0x59 => {
let title_text = SetTitleText::decode(reader)?;
Ok(GameClientBoundPacket::SetTitleText(title_text))
}
0x5A => {
let title_times = SetTitleTimes::decode(reader)?;
Ok(GameClientBoundPacket::SetTitleTimes(title_times))
}
_ => Err(DecodeError::UnknownPacketType { type_id }), _ => Err(DecodeError::UnknownPacketType { type_id }),
} }
} }
} }
// TODO: implement decoder // TODO(timvisee): implement new()
// TODO: implement new() // TODO(timvisee): remove clone?
#[derive(Encoder, Debug)] #[derive(Clone, Encoder, Decoder, Debug)]
pub struct JoinGame { pub struct JoinGame {
pub entity_id: u32, pub entity_id: u32,
pub hardcore: bool, pub hardcore: bool,
@ -139,9 +174,21 @@ pub struct JoinGame {
pub is_flat: bool, pub is_flat: bool,
} }
// TODO: implement decoder // TODO(timvisee): implement new()
// TODO: implement new() #[derive(Encoder, Decoder, Debug)]
#[derive(Encoder, Debug)] pub struct Respawn {
pub dimension: CompoundTag,
pub world_name: String,
pub hashed_seed: i64,
pub game_mode: u8,
pub previous_game_mode: u8,
pub is_debug: bool,
pub is_flat: bool,
pub copy_metadata: bool,
}
// TODO(timvisee): implement new()
#[derive(Encoder, Decoder, Debug)]
pub struct PlayerPositionAndLook { pub struct PlayerPositionAndLook {
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,
@ -154,9 +201,8 @@ pub struct PlayerPositionAndLook {
pub dismount_vehicle: bool, pub dismount_vehicle: bool,
} }
// TODO: implement decoder // TODO(timvisee): implement new()
// TODO: implement new() #[derive(Encoder, Decoder, Debug)]
#[derive(Encoder, Debug)]
pub struct TimeUpdate { pub struct TimeUpdate {
pub world_age: i64, pub world_age: i64,
pub time_of_day: i64, pub time_of_day: i64,
@ -184,27 +230,3 @@ pub struct SpawnPosition {
pub position: u64, pub position: u64,
pub angle: f32, pub angle: f32,
} }
// TODO: implement decoder
// TODO: implement new()
// #[derive(Encoder, Debug)]
// pub struct ChunkData {
// pub x: i32,
// pub z: i32,
// // TODO: should be BitSet type!
// pub primary_mask: Vec<i64>,
// pub heightmaps: CompoundTag,
// #[data_type(with = "var_int_array")]
// pub biomes: Vec<i32>,
// // TODO: extract this from block_entities with sized datatype
// #[data_type(with = "var_int")]
// pub data_size: i32,
// pub data: Vec<u8>,
// // TODO: extract this from block_entities with sized datatype
// #[data_type(with = "var_int")]
// pub block_entities_size: i32,
// pub block_entities: Vec<u8>,
// }