更新NbtReader的构造函数和方法签名

This commit is contained in:
shenjack 2024-03-07 22:34:51 +08:00
parent 35a38e1ffc
commit 361b24b5e0
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 33 additions and 14 deletions

View File

@ -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
}
}

View File

@ -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);
}