mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
rename Tag to Nbt
This commit is contained in:
parent
2a07962af9
commit
ecb3f2ffd7
19 changed files with 136 additions and 136 deletions
|
@ -1,7 +1,7 @@
|
|||
// TODO: have an azalea-inventory or azalea-container crate and put this there
|
||||
|
||||
use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
|
||||
use azalea_nbt::Tag;
|
||||
use azalea_nbt::Nbt;
|
||||
use std::io::{Cursor, Write};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
|
@ -16,7 +16,7 @@ pub struct SlotData {
|
|||
#[var]
|
||||
pub id: u32,
|
||||
pub count: u8,
|
||||
pub nbt: Tag,
|
||||
pub nbt: Nbt,
|
||||
}
|
||||
|
||||
impl McBufReadable for Slot {
|
||||
|
|
|
@ -5,18 +5,18 @@ A fast NBT serializer and deserializer.
|
|||
# Examples
|
||||
|
||||
```
|
||||
use azalea_nbt::{Tag, NbtCompound};
|
||||
use azalea_nbt::{Nbt, NbtCompound};
|
||||
use std::io::Cursor;
|
||||
|
||||
let buf = include_bytes!("../tests/hello_world.nbt");
|
||||
let tag = Tag::read(&mut Cursor::new(&buf[..])).unwrap();
|
||||
let tag = Nbt::read(&mut Cursor::new(&buf[..])).unwrap();
|
||||
assert_eq!(
|
||||
tag,
|
||||
Tag::Compound(NbtCompound::from_iter(vec![(
|
||||
Nbt::Compound(NbtCompound::from_iter(vec![(
|
||||
"hello world".into(),
|
||||
Tag::Compound(NbtCompound::from_iter(vec![(
|
||||
Nbt::Compound(NbtCompound::from_iter(vec![(
|
||||
"name".into(),
|
||||
Tag::String("Bananrama".into()),
|
||||
Nbt::String("Bananrama".into()),
|
||||
)]))
|
||||
)]))
|
||||
);
|
||||
|
|
|
@ -25,7 +25,7 @@ pub fn bench_read_file(filename: &str, c: &mut Criterion) {
|
|||
group.bench_function("azalea_parse", |b| {
|
||||
b.iter(|| {
|
||||
let input = black_box(input);
|
||||
let nbt = azalea_nbt::Tag::read(&mut Cursor::new(&input)).unwrap();
|
||||
let nbt = azalea_nbt::Nbt::read(&mut Cursor::new(&input)).unwrap();
|
||||
black_box(nbt);
|
||||
})
|
||||
});
|
||||
|
@ -48,7 +48,7 @@ pub fn bench_read_file(filename: &str, c: &mut Criterion) {
|
|||
|
||||
// // writing
|
||||
|
||||
let nbt = azalea_nbt::Tag::read_from(&mut Cursor::new(input)).unwrap();
|
||||
let nbt = azalea_nbt::Nbt::read_from(&mut Cursor::new(input)).unwrap();
|
||||
group.bench_function("azalea_write", |b| {
|
||||
b.iter(|| {
|
||||
let nbt = black_box(&nbt);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use azalea_nbt::Tag;
|
||||
use azalea_nbt::Nbt;
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
|
||||
use flate2::read::GzDecoder;
|
||||
use std::{
|
||||
|
@ -19,7 +19,7 @@ fn bench_file(filename: &str, c: &mut Criterion) {
|
|||
|
||||
let mut decoded_src_stream = Cursor::new(&decoded_src[..]);
|
||||
|
||||
let nbt = Tag::read(&mut decoded_src_stream).unwrap();
|
||||
let nbt = Nbt::read(&mut decoded_src_stream).unwrap();
|
||||
decoded_src_stream.set_position(0);
|
||||
|
||||
let mut group = c.benchmark_group(filename);
|
||||
|
@ -28,7 +28,7 @@ fn bench_file(filename: &str, c: &mut Criterion) {
|
|||
|
||||
group.bench_function("Decode", |b| {
|
||||
b.iter(|| {
|
||||
black_box(Tag::read(&mut decoded_src_stream).unwrap());
|
||||
black_box(Nbt::read(&mut decoded_src_stream).unwrap());
|
||||
decoded_src_stream.set_position(0);
|
||||
})
|
||||
});
|
||||
|
|
|
@ -169,7 +169,7 @@ fn read_compound(stream: &mut Cursor<&[u8]>) -> Result<NbtCompound, Error> {
|
|||
break;
|
||||
}
|
||||
let name = read_string(stream)?;
|
||||
let tag = Tag::read_known(stream, tag_id)?;
|
||||
let tag = Nbt::read_known(stream, tag_id)?;
|
||||
map.insert(name, tag);
|
||||
}
|
||||
Ok(map)
|
||||
|
@ -201,36 +201,36 @@ fn read_long_array(stream: &mut Cursor<&[u8]>) -> Result<NbtLongArray, Error> {
|
|||
Ok(longs)
|
||||
}
|
||||
|
||||
impl Tag {
|
||||
impl Nbt {
|
||||
/// Read the NBT data when you already know the ID of the tag. You usually
|
||||
/// want [`Tag::read`] if you're reading an NBT file.
|
||||
/// want [`Nbt::read`] if you're reading an NBT file.
|
||||
#[inline]
|
||||
fn read_known(stream: &mut Cursor<&[u8]>, id: u8) -> Result<Tag, Error> {
|
||||
fn read_known(stream: &mut Cursor<&[u8]>, id: u8) -> Result<Nbt, Error> {
|
||||
Ok(match id {
|
||||
// Signifies the end of a TAG_Compound. It is only ever used inside
|
||||
// a TAG_Compound, and is not named despite being in a TAG_Compound
|
||||
END_ID => Tag::End,
|
||||
END_ID => Nbt::End,
|
||||
// A single signed byte
|
||||
BYTE_ID => Tag::Byte(stream.read_i8()?),
|
||||
BYTE_ID => Nbt::Byte(stream.read_i8()?),
|
||||
// A single signed, big endian 16 bit integer
|
||||
SHORT_ID => Tag::Short(stream.read_i16::<BE>()?),
|
||||
SHORT_ID => Nbt::Short(stream.read_i16::<BE>()?),
|
||||
// A single signed, big endian 32 bit integer
|
||||
INT_ID => Tag::Int(stream.read_i32::<BE>()?),
|
||||
INT_ID => Nbt::Int(stream.read_i32::<BE>()?),
|
||||
// A single signed, big endian 64 bit integer
|
||||
LONG_ID => Tag::Long(stream.read_i64::<BE>()?),
|
||||
LONG_ID => Nbt::Long(stream.read_i64::<BE>()?),
|
||||
// A single, big endian IEEE-754 single-precision floating point
|
||||
// number (NaN possible)
|
||||
FLOAT_ID => Tag::Float(stream.read_f32::<BE>()?),
|
||||
FLOAT_ID => Nbt::Float(stream.read_f32::<BE>()?),
|
||||
// A single, big endian IEEE-754 double-precision floating point
|
||||
// number (NaN possible)
|
||||
DOUBLE_ID => Tag::Double(stream.read_f64::<BE>()?),
|
||||
DOUBLE_ID => Nbt::Double(stream.read_f64::<BE>()?),
|
||||
// A length-prefixed array of signed bytes. The prefix is a signed
|
||||
// integer (thus 4 bytes)
|
||||
BYTE_ARRAY_ID => Tag::ByteArray(read_byte_array(stream)?),
|
||||
BYTE_ARRAY_ID => Nbt::ByteArray(read_byte_array(stream)?),
|
||||
// A length-prefixed modified UTF-8 string. The prefix is an
|
||||
// unsigned short (thus 2 bytes) signifying the length of the
|
||||
// string in bytes
|
||||
STRING_ID => Tag::String(read_string(stream)?),
|
||||
STRING_ID => Nbt::String(read_string(stream)?),
|
||||
// A list of nameless tags, all of the same type. The list is
|
||||
// prefixed with the Type ID of the items it contains (thus 1
|
||||
// byte), and the length of the list as a signed integer (a further
|
||||
|
@ -239,57 +239,57 @@ impl Tag {
|
|||
// notchian implementation uses TAG_End in that situation, but
|
||||
// another reference implementation by Mojang uses 1 instead;
|
||||
// parsers should accept any type if the length is <= 0).
|
||||
LIST_ID => Tag::List(read_list(stream)?),
|
||||
LIST_ID => Nbt::List(read_list(stream)?),
|
||||
// Effectively a list of a named tags. Order is not guaranteed.
|
||||
COMPOUND_ID => Tag::Compound(read_compound(stream)?),
|
||||
COMPOUND_ID => Nbt::Compound(read_compound(stream)?),
|
||||
// A length-prefixed array of signed integers. The prefix is a
|
||||
// signed integer (thus 4 bytes) and indicates the number of 4 byte
|
||||
// integers.
|
||||
INT_ARRAY_ID => Tag::IntArray(read_int_array(stream)?),
|
||||
INT_ARRAY_ID => Nbt::IntArray(read_int_array(stream)?),
|
||||
// A length-prefixed array of signed longs. The prefix is a signed
|
||||
// integer (thus 4 bytes) and indicates the number of 8 byte longs.
|
||||
LONG_ARRAY_ID => Tag::LongArray(read_long_array(stream)?),
|
||||
LONG_ARRAY_ID => Nbt::LongArray(read_long_array(stream)?),
|
||||
_ => return Err(Error::InvalidTagType(id)),
|
||||
})
|
||||
}
|
||||
|
||||
/// Read the NBT data. This will return a compound tag with a single item.
|
||||
pub fn read(stream: &mut Cursor<&[u8]>) -> Result<Tag, Error> {
|
||||
pub fn read(stream: &mut Cursor<&[u8]>) -> Result<Nbt, Error> {
|
||||
// default to compound tag
|
||||
|
||||
// the parent compound only ever has one item
|
||||
let tag_id = stream.read_u8().unwrap_or(0);
|
||||
if tag_id == 0 {
|
||||
return Ok(Tag::End);
|
||||
return Ok(Nbt::End);
|
||||
}
|
||||
let name = read_string(stream)?;
|
||||
let tag = Tag::read_known(stream, tag_id)?;
|
||||
let tag = Nbt::read_known(stream, tag_id)?;
|
||||
let mut map = NbtCompound::with_capacity(1);
|
||||
map.insert(name, tag);
|
||||
|
||||
Ok(Tag::Compound(map))
|
||||
Ok(Nbt::Compound(map))
|
||||
}
|
||||
|
||||
/// Read the NBT data compressed wtih zlib.
|
||||
pub fn read_zlib(stream: &mut impl BufRead) -> Result<Tag, Error> {
|
||||
pub fn read_zlib(stream: &mut impl BufRead) -> Result<Nbt, Error> {
|
||||
let mut gz = ZlibDecoder::new(stream);
|
||||
let mut buf = Vec::new();
|
||||
gz.read_to_end(&mut buf)?;
|
||||
Tag::read(&mut Cursor::new(&buf))
|
||||
Nbt::read(&mut Cursor::new(&buf))
|
||||
}
|
||||
|
||||
/// Read the NBT data compressed wtih gzip.
|
||||
pub fn read_gzip(stream: &mut Cursor<Vec<u8>>) -> Result<Tag, Error> {
|
||||
pub fn read_gzip(stream: &mut Cursor<Vec<u8>>) -> Result<Nbt, Error> {
|
||||
let mut gz = GzDecoder::new(stream);
|
||||
let mut buf = Vec::new();
|
||||
gz.read_to_end(&mut buf)?;
|
||||
Tag::read(&mut Cursor::new(&buf))
|
||||
Nbt::read(&mut Cursor::new(&buf))
|
||||
}
|
||||
}
|
||||
|
||||
impl McBufReadable for Tag {
|
||||
impl McBufReadable for Nbt {
|
||||
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
|
||||
Ok(Tag::read(buf)?)
|
||||
Ok(Nbt::read(buf)?)
|
||||
}
|
||||
}
|
||||
impl From<Error> for BufReadError {
|
||||
|
|
|
@ -14,63 +14,63 @@ fn write_string(writer: &mut dyn Write, string: &str) {
|
|||
fn write_compound(writer: &mut dyn Write, value: &NbtCompound, end_tag: bool) {
|
||||
for (key, tag) in value.iter() {
|
||||
match tag {
|
||||
Tag::End => {}
|
||||
Tag::Byte(value) => {
|
||||
Nbt::End => {}
|
||||
Nbt::Byte(value) => {
|
||||
writer.write_u8(BYTE_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
writer.write_i8(*value).unwrap();
|
||||
}
|
||||
Tag::Short(value) => {
|
||||
Nbt::Short(value) => {
|
||||
writer.write_u8(SHORT_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
writer.write_i16::<BE>(*value).unwrap();
|
||||
}
|
||||
Tag::Int(value) => {
|
||||
Nbt::Int(value) => {
|
||||
writer.write_u8(INT_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
writer.write_i32::<BE>(*value).unwrap();
|
||||
}
|
||||
Tag::Long(value) => {
|
||||
Nbt::Long(value) => {
|
||||
writer.write_u8(LONG_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
writer.write_i64::<BE>(*value).unwrap();
|
||||
}
|
||||
Tag::Float(value) => {
|
||||
Nbt::Float(value) => {
|
||||
writer.write_u8(FLOAT_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
writer.write_f32::<BE>(*value).unwrap();
|
||||
}
|
||||
Tag::Double(value) => {
|
||||
Nbt::Double(value) => {
|
||||
writer.write_u8(DOUBLE_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
writer.write_f64::<BE>(*value).unwrap();
|
||||
}
|
||||
Tag::ByteArray(value) => {
|
||||
Nbt::ByteArray(value) => {
|
||||
writer.write_u8(BYTE_ARRAY_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
write_byte_array(writer, value);
|
||||
}
|
||||
Tag::String(value) => {
|
||||
Nbt::String(value) => {
|
||||
writer.write_u8(STRING_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
write_string(writer, value);
|
||||
}
|
||||
Tag::List(value) => {
|
||||
Nbt::List(value) => {
|
||||
writer.write_u8(LIST_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
write_list(writer, value);
|
||||
}
|
||||
Tag::Compound(value) => {
|
||||
Nbt::Compound(value) => {
|
||||
writer.write_u8(COMPOUND_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
write_compound(writer, value, true);
|
||||
}
|
||||
Tag::IntArray(value) => {
|
||||
Nbt::IntArray(value) => {
|
||||
writer.write_u8(INT_ARRAY_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
write_int_array(writer, value);
|
||||
}
|
||||
Tag::LongArray(value) => {
|
||||
Nbt::LongArray(value) => {
|
||||
writer.write_u8(LONG_ARRAY_ID).unwrap();
|
||||
write_string(writer, key);
|
||||
write_long_array(writer, value);
|
||||
|
@ -196,27 +196,27 @@ fn write_long_array(writer: &mut dyn Write, value: &Vec<i64>) {
|
|||
}
|
||||
}
|
||||
|
||||
impl Tag {
|
||||
impl Nbt {
|
||||
/// Write the tag as unnamed, uncompressed NBT data. If you're writing a
|
||||
/// compound tag and the length of the NBT is already known, use
|
||||
/// [`Tag::write`] to avoid the `End` tag (this is used when writing NBT to
|
||||
/// [`Nbt::write`] to avoid the `End` tag (this is used when writing NBT to
|
||||
/// a file).
|
||||
#[inline]
|
||||
pub fn write_without_end(&self, writer: &mut dyn Write) {
|
||||
match self {
|
||||
Tag::End => {}
|
||||
Tag::Byte(value) => writer.write_i8(*value).unwrap(),
|
||||
Tag::Short(value) => writer.write_i16::<BE>(*value).unwrap(),
|
||||
Tag::Int(value) => writer.write_i32::<BE>(*value).unwrap(),
|
||||
Tag::Long(value) => writer.write_i64::<BE>(*value).unwrap(),
|
||||
Tag::Float(value) => writer.write_f32::<BE>(*value).unwrap(),
|
||||
Tag::Double(value) => writer.write_f64::<BE>(*value).unwrap(),
|
||||
Tag::ByteArray(value) => write_byte_array(writer, value),
|
||||
Tag::String(value) => write_string(writer, value),
|
||||
Tag::List(value) => write_list(writer, value),
|
||||
Tag::Compound(value) => write_compound(writer, value, true),
|
||||
Tag::IntArray(value) => write_int_array(writer, value),
|
||||
Tag::LongArray(value) => write_long_array(writer, value),
|
||||
Nbt::End => {}
|
||||
Nbt::Byte(value) => writer.write_i8(*value).unwrap(),
|
||||
Nbt::Short(value) => writer.write_i16::<BE>(*value).unwrap(),
|
||||
Nbt::Int(value) => writer.write_i32::<BE>(*value).unwrap(),
|
||||
Nbt::Long(value) => writer.write_i64::<BE>(*value).unwrap(),
|
||||
Nbt::Float(value) => writer.write_f32::<BE>(*value).unwrap(),
|
||||
Nbt::Double(value) => writer.write_f64::<BE>(*value).unwrap(),
|
||||
Nbt::ByteArray(value) => write_byte_array(writer, value),
|
||||
Nbt::String(value) => write_string(writer, value),
|
||||
Nbt::List(value) => write_list(writer, value),
|
||||
Nbt::Compound(value) => write_compound(writer, value, true),
|
||||
Nbt::IntArray(value) => write_int_array(writer, value),
|
||||
Nbt::LongArray(value) => write_long_array(writer, value),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -227,10 +227,10 @@ impl Tag {
|
|||
/// Will panic if the tag is not a Compound or End tag.
|
||||
pub fn write(&self, writer: &mut impl Write) {
|
||||
match self {
|
||||
Tag::Compound(value) => {
|
||||
Nbt::Compound(value) => {
|
||||
write_compound(writer, value, false);
|
||||
}
|
||||
Tag::End => {
|
||||
Nbt::End => {
|
||||
END_ID.write_into(writer).unwrap();
|
||||
}
|
||||
_ => panic!("Not a compound tag"),
|
||||
|
@ -258,7 +258,7 @@ impl Tag {
|
|||
}
|
||||
}
|
||||
|
||||
impl McBufWritable for Tag {
|
||||
impl McBufWritable for Nbt {
|
||||
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
|
||||
self.write(buf);
|
||||
Ok(())
|
||||
|
|
|
@ -6,7 +6,7 @@ mod error;
|
|||
mod tag;
|
||||
|
||||
pub use error::Error;
|
||||
pub use tag::{NbtCompound, NbtList, Tag};
|
||||
pub use tag::{NbtCompound, NbtList, Nbt};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
@ -20,25 +20,25 @@ mod tests {
|
|||
#[test]
|
||||
fn mcbuf_nbt() {
|
||||
let mut buf = Vec::new();
|
||||
let tag = Tag::Compound(NbtCompound::from_iter(vec![(
|
||||
let tag = Nbt::Compound(NbtCompound::from_iter(vec![(
|
||||
"hello world".into(),
|
||||
Tag::Compound(NbtCompound::from_iter(vec![(
|
||||
Nbt::Compound(NbtCompound::from_iter(vec![(
|
||||
"name".into(),
|
||||
Tag::String("Bananrama".into()),
|
||||
Nbt::String("Bananrama".into()),
|
||||
)])),
|
||||
)]));
|
||||
tag.write_into(&mut buf).unwrap();
|
||||
|
||||
let mut buf = Cursor::new(&buf[..]);
|
||||
|
||||
let result = Tag::read_from(&mut buf).unwrap();
|
||||
let result = Nbt::read_from(&mut buf).unwrap();
|
||||
assert_eq!(
|
||||
result,
|
||||
Tag::Compound(NbtCompound::from_iter(vec![(
|
||||
Nbt::Compound(NbtCompound::from_iter(vec![(
|
||||
"hello world".into(),
|
||||
Tag::Compound(NbtCompound::from_iter(vec![(
|
||||
Nbt::Compound(NbtCompound::from_iter(vec![(
|
||||
"name".into(),
|
||||
Tag::String("Bananrama".into()),
|
||||
Nbt::String("Bananrama".into()),
|
||||
)])),
|
||||
)]))
|
||||
);
|
||||
|
|
|
@ -32,7 +32,7 @@ pub const LONG_ARRAY_ID: u8 = 12;
|
|||
#[derive(Clone, Debug, PartialEq, Default, EnumAsInner)]
|
||||
#[repr(u8)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(untagged))]
|
||||
pub enum Tag {
|
||||
pub enum Nbt {
|
||||
#[default]
|
||||
End = END_ID,
|
||||
Byte(NbtByte) = BYTE_ID,
|
||||
|
@ -69,7 +69,7 @@ pub enum NbtList {
|
|||
LongArray(Vec<NbtLongArray>) = LONG_ARRAY_ID,
|
||||
}
|
||||
|
||||
impl Tag {
|
||||
impl Nbt {
|
||||
/// Get the numerical ID of the tag type.
|
||||
#[inline]
|
||||
pub fn id(&self) -> u8 {
|
||||
|
@ -96,7 +96,7 @@ impl NbtList {
|
|||
#[derive(Debug, Clone, Default)]
|
||||
pub struct NbtCompound {
|
||||
sorted: bool,
|
||||
inner: Vec<(NbtString, Tag)>,
|
||||
inner: Vec<(NbtString, Nbt)>,
|
||||
}
|
||||
impl NbtCompound {
|
||||
#[inline]
|
||||
|
@ -117,7 +117,7 @@ impl NbtCompound {
|
|||
/// If you previously used [`Self::insert_unsorted`] without [`Self::sort`],
|
||||
/// this function may return incorrect results.
|
||||
#[inline]
|
||||
pub fn get(&mut self, key: &NbtString) -> Option<&Tag> {
|
||||
pub fn get(&mut self, key: &NbtString) -> Option<&Nbt> {
|
||||
if !self.sorted {
|
||||
self.sort()
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ impl NbtCompound {
|
|||
/// [`Self::insert_unsorted`] and then [`Self::sort`] after everything is
|
||||
/// inserted.
|
||||
#[inline]
|
||||
pub fn insert(&mut self, key: NbtString, value: Tag) {
|
||||
pub fn insert(&mut self, key: NbtString, value: Nbt) {
|
||||
self.inner.push((key, value));
|
||||
}
|
||||
|
||||
|
@ -142,7 +142,7 @@ impl NbtCompound {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn iter(&self) -> std::slice::Iter<'_, (CompactString, Tag)> {
|
||||
pub fn iter(&self) -> std::slice::Iter<'_, (CompactString, Nbt)> {
|
||||
self.inner.iter()
|
||||
}
|
||||
}
|
||||
|
@ -160,7 +160,7 @@ impl Serialize for NbtCompound {
|
|||
impl<'de> Deserialize<'de> for NbtCompound {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
use std::collections::BTreeMap;
|
||||
let map = <BTreeMap<NbtString, Tag> as Deserialize>::deserialize(deserializer)?;
|
||||
let map = <BTreeMap<NbtString, Nbt> as Deserialize>::deserialize(deserializer)?;
|
||||
Ok(Self {
|
||||
inner: map.into_iter().collect(),
|
||||
sorted: false,
|
||||
|
@ -168,8 +168,8 @@ impl<'de> Deserialize<'de> for NbtCompound {
|
|||
}
|
||||
}
|
||||
|
||||
impl FromIterator<(NbtString, Tag)> for NbtCompound {
|
||||
fn from_iter<T: IntoIterator<Item = (NbtString, Tag)>>(iter: T) -> Self {
|
||||
impl FromIterator<(NbtString, Nbt)> for NbtCompound {
|
||||
fn from_iter<T: IntoIterator<Item = (NbtString, Nbt)>>(iter: T) -> Self {
|
||||
let inner = iter.into_iter().collect::<Vec<_>>();
|
||||
Self {
|
||||
inner,
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
use azalea_nbt::{NbtCompound, NbtList, Tag};
|
||||
use azalea_nbt::{NbtCompound, NbtList, Nbt};
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn test_decode_hello_world() {
|
||||
// read hello_world.nbt
|
||||
let buf = include_bytes!("hello_world.nbt").to_vec();
|
||||
let tag = Tag::read(&mut Cursor::new(&buf[..])).unwrap();
|
||||
let tag = Nbt::read(&mut Cursor::new(&buf[..])).unwrap();
|
||||
assert_eq!(
|
||||
tag,
|
||||
Tag::Compound(NbtCompound::from_iter(vec![(
|
||||
Nbt::Compound(NbtCompound::from_iter(vec![(
|
||||
"hello world".into(),
|
||||
Tag::Compound(NbtCompound::from_iter(vec![(
|
||||
Nbt::Compound(NbtCompound::from_iter(vec![(
|
||||
"name".into(),
|
||||
Tag::String("Bananrama".into()),
|
||||
Nbt::String("Bananrama".into()),
|
||||
)]))
|
||||
)]))
|
||||
);
|
||||
|
@ -23,7 +23,7 @@ fn test_roundtrip_hello_world() {
|
|||
let original = include_bytes!("hello_world.nbt").to_vec();
|
||||
|
||||
let mut original_stream = Cursor::new(&original[..]);
|
||||
let tag = Tag::read(&mut original_stream).unwrap();
|
||||
let tag = Nbt::read(&mut original_stream).unwrap();
|
||||
|
||||
// write hello_world.nbt
|
||||
let mut result = Vec::new();
|
||||
|
@ -38,21 +38,21 @@ fn test_bigtest() {
|
|||
let original = include_bytes!("bigtest.nbt").to_vec();
|
||||
|
||||
let mut original_stream = Cursor::new(original);
|
||||
let original_tag = Tag::read_gzip(&mut original_stream).unwrap();
|
||||
let original_tag = Nbt::read_gzip(&mut original_stream).unwrap();
|
||||
|
||||
let mut result = Vec::new();
|
||||
original_tag.write(&mut result);
|
||||
|
||||
let decoded_tag = Tag::read(&mut Cursor::new(&result)).unwrap();
|
||||
let decoded_tag = Nbt::read(&mut Cursor::new(&result)).unwrap();
|
||||
|
||||
assert_eq!(decoded_tag, original_tag);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_stringtest() {
|
||||
let correct_tag = Tag::Compound(NbtCompound::from_iter(vec![(
|
||||
let correct_tag = Nbt::Compound(NbtCompound::from_iter(vec![(
|
||||
"😃".into(),
|
||||
Tag::List(NbtList::String(vec![
|
||||
Nbt::List(NbtList::String(vec![
|
||||
"asdfkghasfjgihsdfogjsndfg".into(),
|
||||
"jnabsfdgihsabguiqwrntgretqwejirhbiqw".into(),
|
||||
"asd".into(),
|
||||
|
@ -74,7 +74,7 @@ fn test_stringtest() {
|
|||
let original = include_bytes!("stringtest.nbt").to_vec();
|
||||
|
||||
let mut original_stream = Cursor::new(original);
|
||||
let original_tag = Tag::read_gzip(&mut original_stream).unwrap();
|
||||
let original_tag = Nbt::read_gzip(&mut original_stream).unwrap();
|
||||
|
||||
assert_eq!(original_tag, correct_tag);
|
||||
}
|
||||
|
@ -84,12 +84,12 @@ fn test_complex_player() {
|
|||
let original = include_bytes!("complex_player.dat").to_vec();
|
||||
|
||||
let mut original_stream = Cursor::new(original);
|
||||
let original_tag = Tag::read_gzip(&mut original_stream).unwrap();
|
||||
let original_tag = Nbt::read_gzip(&mut original_stream).unwrap();
|
||||
|
||||
let mut result = Vec::new();
|
||||
original_tag.write(&mut result);
|
||||
|
||||
let decoded_tag = Tag::read(&mut Cursor::new(&result)).unwrap();
|
||||
let decoded_tag = Nbt::read(&mut Cursor::new(&result)).unwrap();
|
||||
|
||||
assert_eq!(decoded_tag, original_tag);
|
||||
}
|
||||
|
@ -99,12 +99,12 @@ fn test_simple_player() {
|
|||
let original = include_bytes!("simple_player.dat").to_vec();
|
||||
|
||||
let mut original_stream = Cursor::new(original);
|
||||
let original_tag = Tag::read_gzip(&mut original_stream).unwrap();
|
||||
let original_tag = Nbt::read_gzip(&mut original_stream).unwrap();
|
||||
|
||||
let mut result = Vec::new();
|
||||
original_tag.write(&mut result);
|
||||
|
||||
let decoded_tag = Tag::read(&mut Cursor::new(&result)).unwrap();
|
||||
let decoded_tag = Nbt::read(&mut Cursor::new(&result)).unwrap();
|
||||
|
||||
assert_eq!(decoded_tag, original_tag);
|
||||
}
|
||||
|
|
|
@ -6,5 +6,5 @@ use azalea_protocol_macros::ClientboundGamePacket;
|
|||
pub struct ClientboundBlockEntityDataPacket {
|
||||
pub pos: BlockPos,
|
||||
pub block_entity_type: azalea_registry::BlockEntityKind,
|
||||
pub tag: azalea_nbt::Tag,
|
||||
pub tag: azalea_nbt::Nbt,
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ pub struct ClientboundLevelChunkWithLightPacket {
|
|||
|
||||
#[derive(Clone, Debug, McBuf)]
|
||||
pub struct ClientboundLevelChunkPacketData {
|
||||
pub heightmaps: azalea_nbt::Tag,
|
||||
pub heightmaps: azalea_nbt::Nbt,
|
||||
// we can't parse the data in azalea-protocol because it dependso on context from other packets
|
||||
pub data: Vec<u8>,
|
||||
pub block_entities: Vec<BlockEntity>,
|
||||
|
@ -25,5 +25,5 @@ pub struct BlockEntity {
|
|||
pub y: u16,
|
||||
#[var]
|
||||
pub type_: i32,
|
||||
pub data: azalea_nbt::Tag,
|
||||
pub data: azalea_nbt::Nbt,
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ pub mod registry {
|
|||
|
||||
use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
|
||||
use azalea_core::ResourceLocation;
|
||||
use azalea_nbt::Tag;
|
||||
use azalea_nbt::Nbt;
|
||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||
use std::{collections::HashMap, io::Cursor};
|
||||
|
||||
|
@ -58,32 +58,32 @@ pub mod registry {
|
|||
pub root: RegistryRoot,
|
||||
}
|
||||
|
||||
impl TryFrom<Tag> for RegistryHolder {
|
||||
impl TryFrom<Nbt> for RegistryHolder {
|
||||
type Error = serde_json::Error;
|
||||
|
||||
fn try_from(value: Tag) -> Result<Self, Self::Error> {
|
||||
fn try_from(value: Nbt) -> Result<Self, Self::Error> {
|
||||
serde_json::from_value(serde_json::to_value(value)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryInto<Tag> for RegistryHolder {
|
||||
impl TryInto<Nbt> for RegistryHolder {
|
||||
type Error = serde_json::Error;
|
||||
|
||||
fn try_into(self) -> Result<Tag, Self::Error> {
|
||||
fn try_into(self) -> Result<Nbt, Self::Error> {
|
||||
serde_json::from_value(serde_json::to_value(self)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl McBufReadable for RegistryHolder {
|
||||
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
|
||||
RegistryHolder::try_from(Tag::read_from(buf)?)
|
||||
RegistryHolder::try_from(Nbt::read_from(buf)?)
|
||||
.map_err(|e| BufReadError::Deserialization { source: e })
|
||||
}
|
||||
}
|
||||
|
||||
impl McBufWritable for RegistryHolder {
|
||||
fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
|
||||
TryInto::<Tag>::try_into(self.clone())?.write_into(buf)
|
||||
TryInto::<Nbt>::try_into(self.clone())?.write_into(buf)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -100,14 +100,14 @@ pub mod registry {
|
|||
pub trim_material: RegistryType<TrimMaterialElement>,
|
||||
#[cfg(not(feature = "strict_registry"))]
|
||||
#[serde(rename = "minecraft:trim_material")]
|
||||
pub trim_material: Tag,
|
||||
pub trim_material: Nbt,
|
||||
|
||||
#[cfg(feature = "strict_registry")]
|
||||
#[serde(rename = "minecraft:chat_type")]
|
||||
pub chat_type: RegistryType<ChatTypeElement>,
|
||||
#[cfg(not(feature = "strict_registry"))]
|
||||
#[serde(rename = "minecraft:chat_type")]
|
||||
pub chat_type: Tag,
|
||||
pub chat_type: Nbt,
|
||||
|
||||
#[serde(rename = "minecraft:dimension_type")]
|
||||
pub dimension_type: RegistryType<DimensionTypeElement>,
|
||||
|
@ -117,21 +117,21 @@ pub mod registry {
|
|||
pub world_type: RegistryType<WorldTypeElement>,
|
||||
#[cfg(not(feature = "strict_registry"))]
|
||||
#[serde(rename = "minecraft:worldgen/biome")]
|
||||
pub world_type: Tag,
|
||||
pub world_type: Nbt,
|
||||
|
||||
#[cfg(feature = "strict_registry")]
|
||||
#[serde(rename = "minecraft:trim_pattern")]
|
||||
pub trim_pattern: RegistryType<TrimPatternElement>,
|
||||
#[cfg(not(feature = "strict_registry"))]
|
||||
#[serde(rename = "minecraft:trim_pattern")]
|
||||
pub trim_pattern: Tag,
|
||||
pub trim_pattern: Nbt,
|
||||
|
||||
#[cfg(feature = "strict_registry")]
|
||||
#[serde(rename = "minecraft:damage_type")]
|
||||
pub damage_type: RegistryType<DamageTypeElement>,
|
||||
#[cfg(not(feature = "strict_registry"))]
|
||||
#[serde(rename = "minecraft:damage_type")]
|
||||
pub damage_type: Tag,
|
||||
pub damage_type: Nbt,
|
||||
}
|
||||
|
||||
/// A collection of values for a certain type of registry data.
|
||||
|
@ -481,27 +481,27 @@ pub mod registry {
|
|||
mod tests {
|
||||
use super::registry::{DimensionTypeElement, RegistryHolder, RegistryRoot, RegistryType};
|
||||
use azalea_core::ResourceLocation;
|
||||
use azalea_nbt::Tag;
|
||||
use azalea_nbt::Nbt;
|
||||
|
||||
#[test]
|
||||
fn test_convert() {
|
||||
// Do NOT use Tag::End, they should be Tag::Compound.
|
||||
// Do NOT use Nbt::End, they should be Nbt::Compound.
|
||||
// This is just for testing.
|
||||
let registry = RegistryHolder {
|
||||
root: RegistryRoot {
|
||||
trim_material: Tag::End,
|
||||
chat_type: Tag::End,
|
||||
trim_material: Nbt::End,
|
||||
chat_type: Nbt::End,
|
||||
dimension_type: RegistryType::<DimensionTypeElement> {
|
||||
kind: ResourceLocation::new("minecraft:dimension_type"),
|
||||
value: Vec::new(),
|
||||
},
|
||||
world_type: Tag::End,
|
||||
trim_pattern: Tag::End,
|
||||
damage_type: Tag::End,
|
||||
world_type: Nbt::End,
|
||||
trim_pattern: Nbt::End,
|
||||
damage_type: Nbt::End,
|
||||
},
|
||||
};
|
||||
|
||||
let tag: Tag = registry.try_into().unwrap();
|
||||
let tag: Nbt = registry.try_into().unwrap();
|
||||
let root = tag
|
||||
.as_compound()
|
||||
.unwrap()
|
||||
|
|
|
@ -5,5 +5,5 @@ use azalea_protocol_macros::ClientboundGamePacket;
|
|||
pub struct ClientboundTagQueryPacket {
|
||||
#[var]
|
||||
pub transaction_id: u32,
|
||||
pub tag: azalea_nbt::Tag,
|
||||
pub tag: azalea_nbt::Nbt,
|
||||
}
|
||||
|
|
|
@ -10,5 +10,5 @@ pub struct ClientboundUpdateMobEffectPacket {
|
|||
#[var]
|
||||
pub effect_duration_ticks: u32,
|
||||
pub flags: u8,
|
||||
pub factor_data: Option<azalea_nbt::Tag>,
|
||||
pub factor_data: Option<azalea_nbt::Nbt>,
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ pub struct Recipe {
|
|||
#[derive(Clone, Debug, McBuf)]
|
||||
pub struct ShapelessRecipe {
|
||||
/// Used to group similar recipes together in the recipe book.
|
||||
/// Tag is present in recipe JSON
|
||||
/// Nbt is present in recipe JSON
|
||||
pub group: String,
|
||||
pub category: CraftingBookCategory,
|
||||
pub ingredients: Vec<Ingredient>,
|
||||
|
|
|
@ -70,7 +70,7 @@ pub enum EntityDataValue {
|
|||
BlockState(azalea_block::BlockState),
|
||||
/// If this is air, that means it's absent,
|
||||
OptionalBlockState(azalea_block::BlockState),
|
||||
CompoundTag(azalea_nbt::Tag),
|
||||
CompoundTag(azalea_nbt::Nbt),
|
||||
Particle(Particle),
|
||||
VillagerData(VillagerData),
|
||||
// 0 for absent; 1 + actual value otherwise. Used for entity IDs.
|
||||
|
|
|
@ -6023,9 +6023,9 @@ pub struct PlayerModeCustomisation(pub u8);
|
|||
#[derive(Component, Deref, DerefMut, Clone)]
|
||||
pub struct PlayerMainHand(pub u8);
|
||||
#[derive(Component, Deref, DerefMut, Clone)]
|
||||
pub struct ShoulderLeft(pub azalea_nbt::Tag);
|
||||
pub struct ShoulderLeft(pub azalea_nbt::Nbt);
|
||||
#[derive(Component, Deref, DerefMut, Clone)]
|
||||
pub struct ShoulderRight(pub azalea_nbt::Tag);
|
||||
pub struct ShoulderRight(pub azalea_nbt::Nbt);
|
||||
#[derive(Component)]
|
||||
pub struct Player;
|
||||
impl Player {
|
||||
|
@ -6106,8 +6106,8 @@ impl Default for PlayerMetadataBundle {
|
|||
score: Score(0),
|
||||
player_mode_customisation: PlayerModeCustomisation(0),
|
||||
player_main_hand: PlayerMainHand(1),
|
||||
shoulder_left: ShoulderLeft(azalea_nbt::Tag::Compound(Default::default())),
|
||||
shoulder_right: ShoulderRight(azalea_nbt::Tag::Compound(Default::default())),
|
||||
shoulder_left: ShoulderLeft(azalea_nbt::Nbt::Compound(Default::default())),
|
||||
shoulder_right: ShoulderRight(azalea_nbt::Nbt::Compound(Default::default())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -399,7 +399,7 @@ impl From<EntityDataValue> for UpdateMetadataError {
|
|||
if default is None:
|
||||
# some types don't have Default implemented
|
||||
if type_name == 'CompoundTag':
|
||||
default = 'azalea_nbt::Tag::Compound(Default::default())'
|
||||
default = 'azalea_nbt::Nbt::Compound(Default::default())'
|
||||
elif type_name == 'CatVariant':
|
||||
default = 'azalea_registry::CatVariant::Tabby'
|
||||
elif type_name == 'PaintingVariant':
|
||||
|
@ -433,7 +433,7 @@ impl From<EntityDataValue> for UpdateMetadataError {
|
|||
elif type_name == 'OptionalFormattedText':
|
||||
default = f'Some({default})' if default != 'Empty' else 'None'
|
||||
elif type_name == 'CompoundTag':
|
||||
default = f'azalea_nbt::Tag::Compound({default})' if default != 'Empty' else 'azalea_nbt::Tag::Compound(Default::default())'
|
||||
default = f'azalea_nbt::Nbt::Compound({default})' if default != 'Empty' else 'azalea_nbt::Nbt::Compound(Default::default())'
|
||||
elif type_name == 'Quaternion':
|
||||
default = f'Quaternion {{ x: {float(default["x"])}, y: {float(default["y"])}, z: {float(default["z"])}, w: {float(default["w"])} }}'
|
||||
elif type_name == 'Vector3':
|
||||
|
|
|
@ -56,7 +56,7 @@ def burger_type_to_rust_type(burger_type, field_name: Optional[str] = None, inst
|
|||
field_type_rs = 'BlockPos'
|
||||
uses.add('azalea_core::BlockPos')
|
||||
elif burger_type == 'nbtcompound':
|
||||
field_type_rs = 'azalea_nbt::Tag'
|
||||
field_type_rs = 'azalea_nbt::Nbt'
|
||||
elif burger_type == 'itemstack':
|
||||
field_type_rs = 'Slot'
|
||||
uses.add('azalea_core::Slot')
|
||||
|
|
Loading…
Add table
Reference in a new issue