Fix test and packets (#60)

* Fix test and packets

* Fix bug, fix a couple more packets

* add tests and fix stuff

* fix warnings

Co-authored-by: Ubuntu <github@matdoes.dev>
This commit is contained in:
EightFactorial 2023-01-25 09:51:27 -08:00 committed by GitHub
parent 473c74175c
commit 9a66cb97a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 71 additions and 29 deletions

View file

@ -95,9 +95,8 @@ mod tests {
} }
] ]
}"#; }"#;
let profile = GameProfile::from( let profile =
serde_json::from_str::<SerializableProfilePropertyValue>(json).unwrap(), GameProfile::from(serde_json::from_str::<SerializableGameProfile>(json).unwrap());
);
assert_eq!( assert_eq!(
profile, profile,
GameProfile { GameProfile {
@ -106,9 +105,9 @@ mod tests {
properties: { properties: {
let mut map = HashMap::new(); let mut map = HashMap::new();
map.insert( map.insert(
"asdf".to_string(), "qwer".to_string(),
ProfilePropertyValue { ProfilePropertyValue {
value: "qwer".to_string(), value: "asdf".to_string(),
signature: Some("zxcv".to_string()), signature: Some("zxcv".to_string()),
}, },
); );

View file

@ -142,6 +142,21 @@ mod tests {
assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 7178); assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 7178);
} }
#[test]
fn test_write_varlong() {
let mut buf = Vec::new();
0u64.var_write_into(&mut buf).unwrap();
assert_eq!(buf, vec![0]);
let mut buf = Vec::new();
1u64.var_write_into(&mut buf).unwrap();
assert_eq!(buf, vec![1]);
let mut buf = Vec::new();
9223372036854775807u64.var_write_into(&mut buf).unwrap();
assert_eq!(buf, vec![255, 255, 255, 255, 255, 255, 255, 255, 127]);
}
#[test] #[test]
fn test_list() { fn test_list() {
let original_vec = vec!["a".to_string(), "bc".to_string(), "def".to_string()]; let original_vec = vec!["a".to_string(), "bc".to_string(), "def".to_string()];

View file

@ -132,15 +132,16 @@ impl McBufVarWritable for i64 {
fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut buffer = [0]; let mut buffer = [0];
let mut value = *self; let mut value = *self;
if value == 0 {
buf.write_all(&buffer).unwrap();
}
while value != 0 { while value != 0 {
buffer[0] = (value & 0b0111_1111) as u8; buffer[0] = (value & 0b0111_1111) as u8;
value = (value >> 7) & (i64::max_value() >> 6); value = (value >> 7) & (i64::max_value() >> 6);
if value != 0 { if value != 0 {
buffer[0] |= 0b1000_0000; buffer[0] |= 0b1000_0000;
} }
// this only writes a single byte, so write_all isn't necessary buf.write_all(&buffer)?;
// the let _ = is so clippy doesn't complain
let _ = buf.write(&buffer)?;
} }
Ok(()) Ok(())
} }

View file

@ -1,4 +1,4 @@
use std::io::{Cursor, Read, Write}; use std::io::{Cursor, Write};
use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
@ -106,13 +106,6 @@ impl BitSet {
pub fn set(&mut self, bit_index: usize) { pub fn set(&mut self, bit_index: usize) {
self.data[bit_index / 64] |= 1u64 << (bit_index % 64); self.data[bit_index / 64] |= 1u64 << (bit_index % 64);
} }
/// Read a BitSet with a known length.
pub fn read_fixed(buf: &mut Cursor<&[u8]>, length: usize) -> Result<Self, BufReadError> {
let mut data = vec![0; length.div_ceil(8)];
buf.read_exact(&mut data)?;
Ok(BitSet::from(data))
}
} }
impl From<Vec<u64>> for BitSet { impl From<Vec<u64>> for BitSet {

View file

@ -305,7 +305,7 @@ impl Connection<ClientboundLoginPacket, ServerboundLoginPacket> {
/// let e = azalea_crypto::encrypt(&p.public_key, &p.nonce).unwrap(); /// let e = azalea_crypto::encrypt(&p.public_key, &p.nonce).unwrap();
/// conn.authenticate( /// conn.authenticate(
/// &access_token, /// &access_token,
/// &Uuid::parse_str(&profile.id).expect("Invalid UUID"), /// &profile.id,
/// e.secret_key, /// e.secret_key,
/// &p /// &p
/// ).await?; /// ).await?;

View file

@ -1,7 +1,7 @@
use azalea_buf::McBuf; use azalea_buf::McBuf;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)] #[derive(Clone, Debug, ClientboundGamePacket, McBuf)]
pub struct ClientboundInitializeBorderPacket { pub struct ClientboundInitializeBorderPacket {
pub new_center_x: f64, pub new_center_x: f64,
pub new_center_z: f64, pub new_center_z: f64,

View file

@ -1,4 +1,4 @@
use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufWritable}; use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable};
use azalea_core::ParticleData; use azalea_core::ParticleData;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};
@ -51,7 +51,18 @@ impl McBufReadable for ClientboundLevelParticlesPacket {
} }
impl McBufWritable for ClientboundLevelParticlesPacket { impl McBufWritable for ClientboundLevelParticlesPacket {
fn write_into(&self, _buf: &mut impl Write) -> Result<(), std::io::Error> { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
todo!(); self.particle_id.var_write_into(buf)?;
self.override_limiter.write_into(buf)?;
self.x.write_into(buf)?;
self.y.write_into(buf)?;
self.z.write_into(buf)?;
self.x_dist.write_into(buf)?;
self.y_dist.write_into(buf)?;
self.z_dist.write_into(buf)?;
self.max_speed.write_into(buf)?;
self.count.write_into(buf)?;
self.data.write_without_id(buf)?;
Ok(())
} }
} }

View file

@ -3,7 +3,7 @@ use azalea_buf::{
BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable, BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable,
}; };
use azalea_chat::Component; use azalea_chat::Component;
use azalea_core::{BitSet, GameType}; use azalea_core::{FixedBitSet, GameType};
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -151,7 +151,7 @@ impl McBufWritable for ClientboundPlayerInfoUpdatePacket {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct ActionEnumSet { pub struct ActionEnumSet {
pub add_player: bool, pub add_player: bool,
pub initialize_chat: bool, pub initialize_chat: bool,
@ -163,7 +163,7 @@ pub struct ActionEnumSet {
impl McBufReadable for ActionEnumSet { impl McBufReadable for ActionEnumSet {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let set = BitSet::read_fixed(buf, 6)?; let set = FixedBitSet::<6>::read_from(buf)?;
Ok(ActionEnumSet { Ok(ActionEnumSet {
add_player: set.index(0), add_player: set.index(0),
initialize_chat: set.index(1), initialize_chat: set.index(1),
@ -177,7 +177,7 @@ impl McBufReadable for ActionEnumSet {
impl McBufWritable for ActionEnumSet { impl McBufWritable for ActionEnumSet {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = BitSet::new(6); let mut set = FixedBitSet::<6>::new();
if self.add_player { if self.add_player {
set.set(0); set.set(0);
} }
@ -196,6 +196,29 @@ impl McBufWritable for ActionEnumSet {
if self.update_display_name { if self.update_display_name {
set.set(5); set.set(5);
} }
set.write_into(buf) set.write_into(buf)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_action_enum_set() {
let data = ActionEnumSet {
add_player: true,
initialize_chat: false,
update_game_mode: true,
update_listed: false,
update_latency: true,
update_display_name: false,
};
let mut buf = Vec::new();
data.write_into(&mut buf).unwrap();
let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
let read_data = ActionEnumSet::read_from(&mut data_cursor).unwrap();
assert_eq!(read_data, data);
} }
} }

View file

@ -40,7 +40,7 @@ impl McBufReadable for TagMap {
impl McBufWritable for TagMap { impl McBufWritable for TagMap {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.len() as u32).write_into(buf)?; (self.len() as u32).var_write_into(buf)?;
for (k, v) in &self.0 { for (k, v) in &self.0 {
k.write_into(buf)?; k.write_into(buf)?;
v.write_into(buf)?; v.write_into(buf)?;

View file

@ -23,7 +23,7 @@ impl McBufWritable for ServerboundPlayerAbilitiesPacket {
if self.is_flying { if self.is_flying {
byte |= 2; byte |= 2;
} }
byte.write_into(buf)?; u8::write_into(&byte, buf)?;
Ok(()) Ok(())
} }
} }

View file

@ -32,7 +32,7 @@ impl McBufWritable for ServerboundPlayerInputPacket {
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
self.xxa.write_into(buf)?; self.xxa.write_into(buf)?;
self.zza.write_into(buf)?; self.zza.write_into(buf)?;
let mut byte = 0; let mut byte = 0u8;
if self.is_jumping { if self.is_jumping {
byte |= 1; byte |= 1;
} }