Add struct variant derive (#12)
This commit is contained in:
parent
70bfd01848
commit
22bdb26a9c
@ -1,8 +1,8 @@
|
|||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
|
|
||||||
use crate::parse::parse_derive_input;
|
use crate::parse::{parse_derive_input, DeriveInputParseResult};
|
||||||
use crate::render::decoder::render_decoder;
|
use crate::render::decoder::{render_struct_decoder, render_struct_variant_decoder};
|
||||||
use crate::render::encoder::render_encoder;
|
use crate::render::encoder::{render_struct_encoder, render_struct_variant_encoder};
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use syn::parse_macro_input;
|
use syn::parse_macro_input;
|
||||||
use syn::DeriveInput;
|
use syn::DeriveInput;
|
||||||
@ -14,15 +14,25 @@ mod render;
|
|||||||
#[proc_macro_derive(Encoder, attributes(data_type))]
|
#[proc_macro_derive(Encoder, attributes(data_type))]
|
||||||
pub fn derive_encoder(tokens: TokenStream) -> TokenStream {
|
pub fn derive_encoder(tokens: TokenStream) -> TokenStream {
|
||||||
let input = parse_macro_input!(tokens as DeriveInput);
|
let input = parse_macro_input!(tokens as DeriveInput);
|
||||||
let (name, fields) = parse_derive_input(&input).expect("Failed to parse derive input");
|
let derive_parse_result = parse_derive_input(&input).expect("Failed to parse derive input");
|
||||||
|
|
||||||
TokenStream::from(render_encoder(name, &fields))
|
TokenStream::from(match derive_parse_result {
|
||||||
|
DeriveInputParseResult::Struct { name, fields } => render_struct_encoder(name, &fields),
|
||||||
|
DeriveInputParseResult::StructVariant { name, variants } => {
|
||||||
|
render_struct_variant_encoder(name, &variants)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(Decoder, attributes(data_type))]
|
#[proc_macro_derive(Decoder, attributes(data_type))]
|
||||||
pub fn derive_decoder(tokens: TokenStream) -> TokenStream {
|
pub fn derive_decoder(tokens: TokenStream) -> TokenStream {
|
||||||
let input = parse_macro_input!(tokens as DeriveInput);
|
let input = parse_macro_input!(tokens as DeriveInput);
|
||||||
let (name, fields) = parse_derive_input(&input).expect("Failed to parse derive input");
|
let derive_parse_result = parse_derive_input(&input).expect("Failed to parse derive input");
|
||||||
|
|
||||||
TokenStream::from(render_decoder(name, &fields))
|
TokenStream::from(match derive_parse_result {
|
||||||
|
DeriveInputParseResult::Struct { name, fields } => render_struct_decoder(name, &fields),
|
||||||
|
DeriveInputParseResult::StructVariant { name, variants } => {
|
||||||
|
render_struct_variant_decoder(name, &variants)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,27 @@
|
|||||||
use crate::error::{DeriveInputParserError, FieldError};
|
use crate::error::{DeriveInputParserError, FieldError};
|
||||||
use proc_macro2::Ident;
|
use proc_macro2::Ident;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
use syn::Error as SynError;
|
use syn::punctuated::Punctuated;
|
||||||
|
use syn::Token;
|
||||||
use syn::{Data, DeriveInput, Field, Fields, FieldsNamed, Lit, Meta, NestedMeta, Type};
|
use syn::{Data, DeriveInput, Field, Fields, FieldsNamed, Lit, Meta, NestedMeta, Type};
|
||||||
|
use syn::{Error as SynError, Variant};
|
||||||
|
|
||||||
|
pub(crate) enum DeriveInputParseResult<'a> {
|
||||||
|
Struct {
|
||||||
|
name: &'a Ident,
|
||||||
|
fields: Vec<FieldData<'a>>,
|
||||||
|
},
|
||||||
|
StructVariant {
|
||||||
|
name: &'a Ident,
|
||||||
|
variants: Vec<VariantData<'a>>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct VariantData<'a> {
|
||||||
|
pub(crate) idx: u8,
|
||||||
|
pub(crate) name: &'a Ident,
|
||||||
|
pub(crate) fields: Vec<FieldData<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) struct FieldData<'a> {
|
pub(crate) struct FieldData<'a> {
|
||||||
pub(crate) name: &'a Ident,
|
pub(crate) name: &'a Ident,
|
||||||
@ -19,18 +38,49 @@ pub(crate) enum Attribute {
|
|||||||
|
|
||||||
pub(crate) fn parse_derive_input(
|
pub(crate) fn parse_derive_input(
|
||||||
input: &DeriveInput,
|
input: &DeriveInput,
|
||||||
) -> Result<(&Ident, Vec<FieldData>), DeriveInputParserError> {
|
) -> Result<DeriveInputParseResult, DeriveInputParserError> {
|
||||||
let name = &input.ident;
|
let name = &input.ident;
|
||||||
|
|
||||||
match &input.data {
|
match &input.data {
|
||||||
Data::Struct(data) => match &data.fields {
|
Data::Struct(data_struct) => match &data_struct.fields {
|
||||||
Fields::Named(named_fields) => Ok((name, parse_fields(named_fields)?)),
|
Fields::Named(named_fields) => {
|
||||||
|
let fields = parse_fields(named_fields)?;
|
||||||
|
|
||||||
|
Ok(DeriveInputParseResult::Struct { name, fields })
|
||||||
|
}
|
||||||
_ => Err(DeriveInputParserError::UnnamedDataFields),
|
_ => Err(DeriveInputParserError::UnnamedDataFields),
|
||||||
},
|
},
|
||||||
|
Data::Enum(data_enum) => {
|
||||||
|
let variants = parse_variants(&data_enum.variants)?;
|
||||||
|
|
||||||
|
Ok(DeriveInputParseResult::StructVariant { name, variants })
|
||||||
|
}
|
||||||
_ => Err(DeriveInputParserError::UnsupportedData),
|
_ => Err(DeriveInputParserError::UnsupportedData),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_variants(
|
||||||
|
variants: &Punctuated<Variant, Token![,]>,
|
||||||
|
) -> Result<Vec<VariantData>, DeriveInputParserError> {
|
||||||
|
variants
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(idx, v)| parse_variant(idx as u8, v))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_variant(idx: u8, variant: &Variant) -> Result<VariantData, DeriveInputParserError> {
|
||||||
|
let name = &variant.ident;
|
||||||
|
|
||||||
|
let fields = match &variant.fields {
|
||||||
|
Fields::Named(named_fields) => parse_fields(named_fields),
|
||||||
|
Fields::Unit => Ok(Vec::new()),
|
||||||
|
_ => Err(DeriveInputParserError::UnnamedDataFields),
|
||||||
|
}?;
|
||||||
|
|
||||||
|
Ok(VariantData { idx, name, fields })
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_fields(named_fields: &FieldsNamed) -> Result<Vec<FieldData>, DeriveInputParserError> {
|
fn parse_fields(named_fields: &FieldsNamed) -> Result<Vec<FieldData>, DeriveInputParserError> {
|
||||||
let mut fields_data = Vec::new();
|
let mut fields_data = Vec::new();
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
use crate::parse::{Attribute, FieldData};
|
use crate::parse::{Attribute, FieldData, VariantData};
|
||||||
use proc_macro2::TokenStream as TokenStream2;
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
use proc_macro2::{Ident, Span};
|
use proc_macro2::{Ident, Span};
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::Type;
|
use syn::Type;
|
||||||
|
|
||||||
pub(crate) fn render_decoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStream2 {
|
pub(crate) fn render_struct_decoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStream2 {
|
||||||
let struct_create = render_struct_create(name, fields);
|
let field_names_joined_comma = render_field_names_joined_comma(fields);
|
||||||
let render_fields = render_fields(fields);
|
let render_fields = render_fields(fields);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
@ -16,28 +16,72 @@ pub(crate) fn render_decoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStre
|
|||||||
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
|
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
|
||||||
#render_fields
|
#render_fields
|
||||||
|
|
||||||
Ok(#struct_create)
|
Ok(#name {
|
||||||
|
#field_names_joined_comma
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_struct_create(name: &Ident, fields: &Vec<FieldData>) -> TokenStream2 {
|
pub(crate) fn render_struct_variant_decoder(
|
||||||
let struct_fields = fields
|
name: &Ident,
|
||||||
.iter()
|
variants: &Vec<VariantData>,
|
||||||
.map(|f| f.name)
|
) -> TokenStream2 {
|
||||||
.map(|n| quote!(#n,))
|
let render_variants = render_variants(variants);
|
||||||
.collect::<TokenStream2>();
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#name {
|
#[automatically_derived]
|
||||||
#struct_fields
|
impl crate::decoder::Decoder for #name {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
|
||||||
|
let type_id = reader.read_u8()?;
|
||||||
|
|
||||||
|
match type_id {
|
||||||
|
#render_variants
|
||||||
|
_ => Err(DecodeError::UnknownEnumType { type_id }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_variants(variants: &Vec<VariantData>) -> TokenStream2 {
|
||||||
|
variants.iter().map(|v| render_variant(v)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_variant(variant: &VariantData) -> TokenStream2 {
|
||||||
|
let idx = variant.idx;
|
||||||
|
let name = variant.name;
|
||||||
|
let fields = &variant.fields;
|
||||||
|
|
||||||
|
if fields.is_empty() {
|
||||||
|
quote! {
|
||||||
|
#idx => Ok(Self::#name),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let field_names_joined_comma = render_field_names_joined_comma(fields);
|
||||||
|
let render_fields = render_fields(fields);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#idx => {
|
||||||
|
#render_fields
|
||||||
|
|
||||||
|
Ok(Self::#name {
|
||||||
|
#field_names_joined_comma
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_field_names_joined_comma(fields: &Vec<FieldData>) -> TokenStream2 {
|
||||||
|
fields.iter().map(|f| f.name).map(|n| quote!(#n,)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
fn render_fields(fields: &Vec<FieldData>) -> TokenStream2 {
|
fn render_fields(fields: &Vec<FieldData>) -> TokenStream2 {
|
||||||
fields.iter().map(|f| render_field(f)).flatten().collect()
|
fields.iter().map(|f| render_field(f)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_field(field: &FieldData) -> TokenStream2 {
|
fn render_field(field: &FieldData) -> TokenStream2 {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
use crate::parse::{Attribute, FieldData};
|
use crate::parse::{Attribute, FieldData, VariantData};
|
||||||
use proc_macro2::TokenStream as TokenStream2;
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
use proc_macro2::{Ident, Span};
|
use proc_macro2::{Ident, Span};
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
pub(crate) fn render_encoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStream2 {
|
pub(crate) fn render_struct_encoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStream2 {
|
||||||
let render_fields = render_fields(fields);
|
let render_fields = render_fields(fields, true);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
@ -18,34 +18,100 @@ pub(crate) fn render_encoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStre
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_fields(fields: &Vec<FieldData>) -> TokenStream2 {
|
pub(crate) fn render_struct_variant_encoder(
|
||||||
fields.iter().map(|f| render_field(f)).flatten().collect()
|
name: &Ident,
|
||||||
|
variants: &Vec<VariantData>,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
let render_variants = render_variants(variants);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#[automatically_derived]
|
||||||
|
impl crate::encoder::Encoder for #name {
|
||||||
|
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::error::EncodeError> {
|
||||||
|
match self {
|
||||||
|
#render_variants
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_field(field: &FieldData) -> TokenStream2 {
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_variants(variants: &Vec<VariantData>) -> TokenStream2 {
|
||||||
|
variants.iter().map(|v| render_variant(v)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_variant(variant: &VariantData) -> TokenStream2 {
|
||||||
|
let idx = variant.idx;
|
||||||
|
let name = variant.name;
|
||||||
|
let fields = &variant.fields;
|
||||||
|
|
||||||
|
if fields.is_empty() {
|
||||||
|
quote! {
|
||||||
|
Self::#name => {
|
||||||
|
writer.write_u8(#idx)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let field_names_joined_comma = render_field_names_joined_comma(fields);
|
||||||
|
let render_fields = render_fields(fields, false);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
Self::#name {
|
||||||
|
#field_names_joined_comma
|
||||||
|
} => {
|
||||||
|
writer.write_u8(#idx)?;
|
||||||
|
|
||||||
|
#render_fields
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_field_names_joined_comma(fields: &Vec<FieldData>) -> TokenStream2 {
|
||||||
|
fields.iter().map(|f| f.name).map(|n| quote!(#n,)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_fields(fields: &Vec<FieldData>, with_self: bool) -> TokenStream2 {
|
||||||
|
fields.iter().map(|f| render_field(f, with_self)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_field(field: &FieldData, with_self: bool) -> TokenStream2 {
|
||||||
let name = field.name;
|
let name = field.name;
|
||||||
|
|
||||||
match &field.attribute {
|
match &field.attribute {
|
||||||
Attribute::With { module } => render_with_field(name, module),
|
Attribute::With { module } => render_with_field(name, module, with_self),
|
||||||
Attribute::MaxLength { length } => render_max_length_field(name, *length as u16),
|
Attribute::MaxLength { length } => render_max_length_field(name, *length as u16, with_self),
|
||||||
Attribute::Empty => render_simple_field(name),
|
Attribute::Empty => render_simple_field(name, with_self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_simple_field(name: &Ident) -> TokenStream2 {
|
fn render_simple_field(name: &Ident, with_self: bool) -> TokenStream2 {
|
||||||
render_with_field(name, "Encoder")
|
render_with_field(name, "Encoder", with_self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_with_field(name: &Ident, module: &str) -> TokenStream2 {
|
fn render_with_field(name: &Ident, module: &str, with_self: bool) -> TokenStream2 {
|
||||||
let module_ident = Ident::new(module, Span::call_site());
|
let module_ident = Ident::new(module, Span::call_site());
|
||||||
|
let final_name = get_field_final_name(name, with_self);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
crate::encoder::#module_ident::encode(&self.#name, writer)?;
|
crate::encoder::#module_ident::encode(#final_name, writer)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_max_length_field(name: &Ident, max_length: u16) -> TokenStream2 {
|
fn render_max_length_field(name: &Ident, max_length: u16, with_self: bool) -> TokenStream2 {
|
||||||
|
let final_name = get_field_final_name(name, with_self);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
crate::encoder::EncoderWriteExt::write_string(writer, &self.#name, #max_length)?;
|
crate::encoder::EncoderWriteExt::write_string(writer, #final_name, #max_length)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_field_final_name(name: &Ident, with_self: bool) -> TokenStream2 {
|
||||||
|
if with_self {
|
||||||
|
quote!(&self.#name)
|
||||||
|
} else {
|
||||||
|
quote!(#name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -324,6 +324,10 @@ impl Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_str(text: &str) -> Message {
|
||||||
|
Message::new(Payload::text(text))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_json(json: &str) -> Result<Self, Error> {
|
pub fn from_json(json: &str) -> Result<Self, Error> {
|
||||||
serde_json::from_str(json)
|
serde_json::from_str(json)
|
||||||
}
|
}
|
||||||
|
@ -157,6 +157,22 @@ impl Decoder for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Decoder for f32 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_f32::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for f64 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_f64::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Decoder for String {
|
impl Decoder for String {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
|
@ -139,6 +139,18 @@ impl Encoder for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Encoder for f32 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_f32::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for f64 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_f64::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Encoder for String {
|
impl Encoder for String {
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
Ok(writer.write_string(self, 32_768)?)
|
Ok(writer.write_string(self, 32_768)?)
|
||||||
|
@ -4,9 +4,11 @@ use crate::data::chat::Message;
|
|||||||
use crate::decoder::Decoder;
|
use crate::decoder::Decoder;
|
||||||
use crate::error::DecodeError;
|
use crate::error::DecodeError;
|
||||||
use crate::impl_enum_encoder_decoder;
|
use crate::impl_enum_encoder_decoder;
|
||||||
|
use byteorder::{ReadBytesExt, WriteBytesExt};
|
||||||
use minecraft_protocol_derive::{Decoder, Encoder};
|
use minecraft_protocol_derive::{Decoder, Encoder};
|
||||||
use nbt::CompoundTag;
|
use nbt::CompoundTag;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub enum GameServerBoundPacket {
|
pub enum GameServerBoundPacket {
|
||||||
ServerBoundChatMessage(ServerBoundChatMessage),
|
ServerBoundChatMessage(ServerBoundChatMessage),
|
||||||
@ -19,6 +21,7 @@ pub enum GameClientBoundPacket {
|
|||||||
ClientBoundKeepAlive(ClientBoundKeepAlive),
|
ClientBoundKeepAlive(ClientBoundKeepAlive),
|
||||||
ChunkData(ChunkData),
|
ChunkData(ChunkData),
|
||||||
GameDisconnect(GameDisconnect),
|
GameDisconnect(GameDisconnect),
|
||||||
|
BossBar(BossBar),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameServerBoundPacket {
|
impl GameServerBoundPacket {
|
||||||
@ -54,6 +57,7 @@ impl GameClientBoundPacket {
|
|||||||
GameClientBoundPacket::ClientBoundKeepAlive(_) => 0x20,
|
GameClientBoundPacket::ClientBoundKeepAlive(_) => 0x20,
|
||||||
GameClientBoundPacket::ChunkData(_) => 0x21,
|
GameClientBoundPacket::ChunkData(_) => 0x21,
|
||||||
GameClientBoundPacket::JoinGame(_) => 0x25,
|
GameClientBoundPacket::JoinGame(_) => 0x25,
|
||||||
|
GameClientBoundPacket::BossBar(_) => 0x0D,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,6 +253,69 @@ impl GameDisconnect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug, PartialEq)]
|
||||||
|
pub struct BossBar {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub action: BossBarAction,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug, PartialEq)]
|
||||||
|
pub enum BossBarAction {
|
||||||
|
Add {
|
||||||
|
title: Message,
|
||||||
|
health: f32,
|
||||||
|
color: BossBarColor,
|
||||||
|
division: BossBarDivision,
|
||||||
|
flags: u8,
|
||||||
|
},
|
||||||
|
Remove,
|
||||||
|
UpdateHealth {
|
||||||
|
health: f32,
|
||||||
|
},
|
||||||
|
UpdateTitle {
|
||||||
|
title: Message,
|
||||||
|
},
|
||||||
|
UpdateStyle {
|
||||||
|
color: BossBarColor,
|
||||||
|
division: BossBarDivision,
|
||||||
|
},
|
||||||
|
UpdateFlags {
|
||||||
|
flags: u8,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)]
|
||||||
|
pub enum BossBarColor {
|
||||||
|
Pink,
|
||||||
|
Blue,
|
||||||
|
Red,
|
||||||
|
Green,
|
||||||
|
Yellow,
|
||||||
|
Purple,
|
||||||
|
White,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_enum_encoder_decoder!(BossBarColor);
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)]
|
||||||
|
pub enum BossBarDivision {
|
||||||
|
None,
|
||||||
|
Notches6,
|
||||||
|
Notches10,
|
||||||
|
Notches12,
|
||||||
|
Notches20,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_enum_encoder_decoder!(BossBarDivision);
|
||||||
|
|
||||||
|
impl BossBar {
|
||||||
|
pub fn new(id: Uuid, action: BossBarAction) -> GameClientBoundPacket {
|
||||||
|
let boss_bar = BossBar { id, action };
|
||||||
|
|
||||||
|
GameClientBoundPacket::BossBar(boss_bar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::data::chat::Payload;
|
use crate::data::chat::Payload;
|
||||||
@ -260,6 +327,7 @@ mod tests {
|
|||||||
use crate::STRING_MAX_LENGTH;
|
use crate::STRING_MAX_LENGTH;
|
||||||
use nbt::CompoundTag;
|
use nbt::CompoundTag;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_server_bound_chat_message_encode() {
|
fn test_server_bound_chat_message_encode() {
|
||||||
@ -505,4 +573,68 @@ mod tests {
|
|||||||
Message::new(Payload::text("Message"))
|
Message::new(Payload::text("Message"))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_boss_bar_add_encode() {
|
||||||
|
let boss_bar_add = create_boss_bar_add_packet();
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
boss_bar_add.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/boss_bar_add.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_boss_bar_add_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/game/boss_bar_add.dat").to_vec());
|
||||||
|
let boss_bar_add = BossBar::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(boss_bar_add, create_boss_bar_add_packet());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_boss_bar_add_packet() -> BossBar {
|
||||||
|
BossBar {
|
||||||
|
id: Uuid::from_str("afa32ac8-d3bf-47f3-99eb-294d60b3dca2").unwrap(),
|
||||||
|
action: BossBarAction::Add {
|
||||||
|
title: Message::from_str("Boss title"),
|
||||||
|
health: 123.45,
|
||||||
|
color: BossBarColor::Yellow,
|
||||||
|
division: BossBarDivision::Notches10,
|
||||||
|
flags: 7,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_boss_bar_remove_encode() {
|
||||||
|
let boss_bar_remove = create_boss_bar_remove_packet();
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
boss_bar_remove.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/boss_bar_remove.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_boss_bar_remove_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/game/boss_bar_remove.dat").to_vec());
|
||||||
|
let boss_bar_remove = BossBar::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(boss_bar_remove, create_boss_bar_remove_packet());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_boss_bar_remove_packet() -> BossBar {
|
||||||
|
BossBar {
|
||||||
|
id: Uuid::from_str("afa32ac8-d3bf-47f3-99eb-294d60b3dca2").unwrap(),
|
||||||
|
action: BossBarAction::Remove,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
BIN
protocol/test/packet/game/boss_bar_add.dat
Normal file
BIN
protocol/test/packet/game/boss_bar_add.dat
Normal file
Binary file not shown.
1
protocol/test/packet/game/boss_bar_remove.dat
Normal file
1
protocol/test/packet/game/boss_bar_remove.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
<EFBFBD><EFBFBD>*<2A>ӿG<D3BF><47><EFBFBD>)M`<60>ܢ
|
Loading…
x
Reference in New Issue
Block a user