From 290790243129b4eda12c4732c64bb6874449c9ea Mon Sep 17 00:00:00 2001 From: EightFactorial <29801334+EightFactorial@users.noreply.github.com> Date: Sat, 1 Apr 2023 16:14:03 -0700 Subject: Damage_Event unsigned subtract from zero (#86) --- .../src/packets/game/clientbound_damage_event_packet.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/src/packets/game/clientbound_damage_event_packet.rs b/azalea-protocol/src/packets/game/clientbound_damage_event_packet.rs index 93731445..941ed01f 100644 --- a/azalea-protocol/src/packets/game/clientbound_damage_event_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_damage_event_packet.rs @@ -16,15 +16,20 @@ pub struct ClientboundDamageEventPacket { } #[derive(Clone, Debug)] -pub struct OptionalEntityId(pub u32); +pub struct OptionalEntityId(pub Option); impl McBufReadable for OptionalEntityId { fn read_from(buf: &mut Cursor<&[u8]>) -> Result { - Ok(OptionalEntityId(u32::var_read_from(buf)? - 1)) + match u32::var_read_from(buf)? { + 0 => Ok(OptionalEntityId(None)), + id => Ok(OptionalEntityId(Some(id - 1))), + } } } impl McBufWritable for OptionalEntityId { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - (self.0 + 1).var_write_into(buf)?; - Ok(()) + match self.0 { + Some(id) => (id + 1).var_write_into(buf), + None => 0u32.var_write_into(buf), + } } } -- cgit v1.2.3 From 16903b73bf58a6f5b7fa19f52f265c50dadf0c2b Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 12 Apr 2023 03:26:37 +0000 Subject: fix tests --- azalea-nbt/src/encode.rs | 159 ++++++++++++--------- .../src/packets/game/clientbound_login_packet.rs | 2 +- 2 files changed, 91 insertions(+), 70 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 7912eabb..76b70b6e 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -2,7 +2,7 @@ use crate::tag::*; use azalea_buf::McBufWritable; use byteorder::{WriteBytesExt, BE}; use flate2::write::{GzEncoder, ZlibEncoder}; -use packed_simd_2::{i32x16, i32x2, i32x4, i32x8, i64x2, i64x4, i64x8}; +// use packed_simd_2::{i32x16, i32x2, i32x4, i32x8, i64x2, i64x4, i64x8}; use std::io::Write; #[inline] @@ -140,81 +140,102 @@ fn write_byte_array(writer: &mut impl Write, value: &[u8]) { } #[inline] -fn write_int_array(writer: &mut impl Write, l: &[i32]) { - writer.write_i32::(l.len() as i32).unwrap(); - // flip the bits to big endian with simd - let mut position = 0; - // x16 - while l.len() - position >= 16 { - let l = unsafe { i32x16::from_slice_unaligned_unchecked(&l[position..]) }; - l.to_be(); - let l = unsafe { std::mem::transmute::(l) }; - writer.write_all(&l).unwrap(); - position += 16; - } - // x8 - if l.len() - position >= 8 { - let l = unsafe { i32x8::from_slice_unaligned_unchecked(&l[position..]) }; - l.to_be(); - let l = unsafe { std::mem::transmute::(l) }; - writer.write_all(&l).unwrap(); - position += 8; - } - // x4 - if l.len() - position >= 4 { - let l = unsafe { i32x4::from_slice_unaligned_unchecked(&l[position..]) }; - l.to_be(); - let l = unsafe { std::mem::transmute::(l) }; - writer.write_all(&l).unwrap(); - position += 4; - } - // x2 - if l.len() - position >= 2 { - let l = unsafe { i32x2::from_slice_unaligned_unchecked(&l[position..]) }; - l.to_be(); - let l = unsafe { std::mem::transmute::(l) }; - writer.write_all(&l).unwrap(); - position += 2; - } - // x1 ... just a normal write_i32 - if l.len() - position >= 1 { - writer.write_i32::(l[position]).unwrap(); +fn write_int_array(writer: &mut impl Write, array: &[i32]) { + writer.write_i32::(array.len() as i32).unwrap(); + + for &item in array { + writer.write_i32::(item).unwrap(); } + + // (disabled for now since i realized packed_simd to_be does not work as + // expected) // flip the bits to big endian with simd + // let mut position = 0; + // // x16 + // while array.len() - position >= 16 { + // let l = unsafe { + // i32x16::from_slice_unaligned_unchecked(&array[position..]) }; let + // l = l.to_be(); let l = unsafe { std::mem::transmute::(l) }; writer.write_all(&l).unwrap(); + // position += 16; + // } + // // x8 + // if array.len() - position >= 8 { + // let l = unsafe { + // i32x8::from_slice_unaligned_unchecked(&array[position..]) }; + // let l = l.to_be(); + // let l = unsafe { std::mem::transmute::(l) }; + // writer.write_all(&l).unwrap(); + // position += 8; + // } + // // x4 + // if array.len() - position >= 4 { + // let l = unsafe { + // i32x4::from_slice_unaligned_unchecked(&array[position..]) }; + // let l = l.to_be(); + // let l = unsafe { std::mem::transmute::(l) }; + // writer.write_all(&l).unwrap(); + // position += 4; + // } + // // x2 + // if array.len() - position >= 2 { + // let l = unsafe { + // i32x2::from_slice_unaligned_unchecked(&array[position..]) }; + // let l = l.to_be(); + // let l = l.swap_bytes(); + // let l = unsafe { std::mem::transmute::(l) }; + // writer.write_all(&l).unwrap(); + // position += 2; + // } + // // x1 ... just a normal write_i32 + // if array.len() - position >= 1 { + // writer.write_i32::(array[position]).unwrap(); + // } } #[inline] fn write_long_array(writer: &mut impl Write, l: &[i64]) { writer.write_i32::(l.len() as i32).unwrap(); - // flip the bits to big endian with simd - let mut position = 0; - // x16 - while l.len() - position >= 8 { - let l = unsafe { i64x8::from_slice_unaligned_unchecked(&l[position..]) }; - l.to_be(); - let l = unsafe { std::mem::transmute::(l) }; - writer.write_all(&l).unwrap(); - position += 8; - } - // x4 - if l.len() - position >= 4 { - let l = unsafe { i64x4::from_slice_unaligned_unchecked(&l[position..]) }; - l.to_be(); - let l = unsafe { std::mem::transmute::(l) }; - writer.write_all(&l).unwrap(); - position += 4; - } - // x2 - if l.len() - position >= 2 { - let l = unsafe { i64x2::from_slice_unaligned_unchecked(&l[position..]) }; - l.to_be(); - let l = unsafe { std::mem::transmute::(l) }; - writer.write_all(&l).unwrap(); - position += 2; - } - // x1 ... just a normal write_i32 - if l.len() - position >= 1 { - writer.write_i64::(l[position]).unwrap(); + + for &item in l { + writer.write_i64::(item).unwrap(); } + + // (disabled for now since i realized packed_simd to_be does not work as + // expected) + + // // flip the bits to big endian with simd + // let mut position = 0; + // // x16 + // while l.len() - position >= 8 { + // let l = unsafe { + // i64x8::from_slice_unaligned_unchecked(&l[position..]) }; + // l.to_be(); + // let l = unsafe { std::mem::transmute::(l) }; + // writer.write_all(&l).unwrap(); + // position += 8; + // } + // // x4 + // if l.len() - position >= 4 { + // let l = unsafe { + // i64x4::from_slice_unaligned_unchecked(&l[position..]) }; + // l.to_be(); + // let l = unsafe { std::mem::transmute::(l) }; + // writer.write_all(&l).unwrap(); + // position += 4; + // } + // // x2 + // if l.len() - position >= 2 { + // let l = unsafe { + // i64x2::from_slice_unaligned_unchecked(&l[position..]) }; + // l.to_be(); + // let l = unsafe { std::mem::transmute::(l) }; + // writer.write_all(&l).unwrap(); + // position += 2; + // } + // // x1 ... just a normal write_i32 + // if l.len() - position >= 1 { + // writer.write_i64::(l[position]).unwrap(); + // } } impl Nbt { diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs index 42f93c40..a35951a7 100755 --- a/azalea-protocol/src/packets/game/clientbound_login_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -515,7 +515,7 @@ mod tests { .unwrap() .as_compound() .unwrap(); - let dimension_type = dimension.get("type").unwrap().as_string().unwrap(); + let dimension_type = dimension.get("type").unwrap().as_string().unwrap().as_str(); assert!(dimension_type == "minecraft:dimension_type"); } } -- cgit v1.2.3