From 361b24b5e0b194bc0b2fe6ca1ccc4aef1725323b Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Thu, 7 Mar 2024 22:34:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0NbtReader=E7=9A=84=E6=9E=84?= =?UTF-8?q?=E9=80=A0=E5=87=BD=E6=95=B0=E5=92=8C=E6=96=B9=E6=B3=95=E7=AD=BE?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shen-nbt5/src/lib.rs | 18 ++++++++++++++---- shen-nbt5/src/tests.rs | 29 +++++++++++++++++++---------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/shen-nbt5/src/lib.rs b/shen-nbt5/src/lib.rs index 0d0db11..77d62ca 100644 --- a/shen-nbt5/src/lib.rs +++ b/shen-nbt5/src/lib.rs @@ -9,7 +9,7 @@ mod tests; /// 用于读取 NBT 数据 pub struct NbtReader<'data> { /// NBT 数据 - pub data: &'data [u8], + pub data: &'data mut [u8], /// 当前读取的位置 pub cursor: usize, // be/le @@ -39,7 +39,7 @@ macro_rules! read { }; } impl NbtReader<'_> { - pub fn new(data: &[u8]) -> NbtReader { + pub fn new(data: &mut [u8]) -> NbtReader { NbtReader { data, cursor: 0, @@ -84,15 +84,25 @@ impl NbtReader<'_> { } pub fn read_int_array(&mut self, len: usize) -> &[i32] { unsafe { - let value = std::slice::from_raw_parts(self.data[self.cursor..].as_ptr() as *const i32, len); + println!("data: {:?}", self.data); + let value = std::slice::from_raw_parts_mut(self.data[self.cursor..].as_ptr() as *mut i32, len); + for n in &mut *value { + *n = n.to_be(); + } self.cursor += len * 4; + println!("data: {:?}", self.data); value } } pub fn read_long_array(&mut self, len: usize) -> &[i64] { unsafe { - let value = std::slice::from_raw_parts(self.data[self.cursor..].as_ptr() as *const i64, len); + println!("data: {:?}", self.data); + let value = std::slice::from_raw_parts_mut(self.data[self.cursor..].as_ptr() as *mut i64, len); + for n in &mut *value { + *n = n.to_be(); + } self.cursor += len * 8; + println!("data: {:?}", self.data); value } } diff --git a/shen-nbt5/src/tests.rs b/shen-nbt5/src/tests.rs index 89efdf7..3969d6b 100644 --- a/shen-nbt5/src/tests.rs +++ b/shen-nbt5/src/tests.rs @@ -2,17 +2,18 @@ use crate::{NbtReader, NbtValue}; #[test] fn basic_init() { - let data = vec![0x01, 0x02, 0x03, 0x04]; - let reader = NbtReader::new(&data); + let mut data = vec![0x01, 0x02, 0x03, 0x04]; + let reader = NbtReader::new(&mut data); assert_eq!(reader.cursor, 0); - assert_eq!(reader.data, &data); + let same_data = vec![0x01, 0x02, 0x03, 0x04]; + assert_eq!(reader.data, &same_data); } #[test] fn read_i8() { - let data = vec![0x01, 0x02, 0x03, 0x04]; - let mut reader = NbtReader::new(&data); + let mut data = vec![0x01, 0x02, 0x03, 0x04]; + let mut reader = NbtReader::new(&mut data); assert_eq!(reader.read_i8(), 0x01); assert_eq!(reader.cursor, 1); assert_eq!(reader.read_i8(), 0x02); @@ -21,18 +22,26 @@ fn read_i8() { #[test] fn read_array() { - let data = vec![0x01, 0x02, 0x03, 0x04]; - let mut reader = NbtReader::new(&data); + let mut data = vec![0x01, 0x02, 0x03, 0x04]; + let mut reader = NbtReader::new(&mut data); assert_eq!(reader.read_u8_array(2), &[0x01, 0x02]); assert_eq!(reader.cursor, 2); assert_eq!(reader.read_i8_array(2), &[0x03, 0x04]); assert_eq!(reader.cursor, 4); } +#[test] +fn read_int_array() { + let mut value = 1234567890_i32.to_be_bytes(); + let mut reader = NbtReader::new(&mut value); + assert_eq!(reader.read_int_array(1), &[1234567890_i32]); + assert_eq!(reader.cursor, 4); +} + #[test] fn read_long_array() { - let data = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]; - let mut reader = NbtReader::new(&data); - assert_eq!(reader.read_long_array(1), &[i64::from_ne_bytes([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])]); + let mut value = 1234567890_i64.to_be_bytes(); + let mut reader = NbtReader::new(&mut value); + assert_eq!(reader.read_long_array(1), &[1234567890_i64]); assert_eq!(reader.cursor, 8); }