Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
4a4f2e2a1c | ||
|
31041b8fe2 | ||
|
f19f598abe | ||
|
6a069edaff | ||
|
09f95625ef | ||
|
05cf9c6e6b | ||
|
024786e618 | ||
|
22bdb26a9c | ||
|
70bfd01848 | ||
|
f733fabedc | ||
|
b70968bd84 | ||
|
7b6b7e4921 | ||
|
b4a6f81e11 | ||
|
41c185fcb1 |
@ -1,7 +1,7 @@
|
|||||||
minecraft-protocol
|
minecraft-protocol
|
||||||
============
|
============
|
||||||
[](https://crates.io/crates/minecraft-protocol)
|
[](https://crates.io/crates/minecraft-protocol)
|
||||||
[](https://travis-ci.com/eihwaz/minecraft-protocol)
|
[](https://app.travis-ci.com/github/eihwaz/minecraft-protocol)
|
||||||
[](https://codecov.io/gh/eihwaz/minecraft-protocol)
|
[](https://codecov.io/gh/eihwaz/minecraft-protocol)
|
||||||
|
|
||||||
Library for decoding and encoding Minecraft packets
|
Library for decoding and encoding Minecraft packets
|
||||||
|
44
protocol-derive/src/error.rs
Normal file
44
protocol-derive/src/error.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
use syn::Error as SynError;
|
||||||
|
|
||||||
|
/// Possible errors while deriving.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) enum DeriveInputParserError {
|
||||||
|
/// Derive attribute must be placed on a structure or enum.
|
||||||
|
UnsupportedData,
|
||||||
|
/// Data fields must be named.
|
||||||
|
UnnamedDataFields,
|
||||||
|
/// Possible errors while parsing attributes.
|
||||||
|
AttributeError { attribute_error: AttributeError },
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Possible errors while parsing attributes.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) enum AttributeError {
|
||||||
|
/// Failed to parse field meta due incorrect syntax.
|
||||||
|
BadAttributeSyntax { syn_error: SynError },
|
||||||
|
/// Unsupported field attribute type.
|
||||||
|
UnsupportedAttribute,
|
||||||
|
/// Field meta has wrong value type.
|
||||||
|
/// For example an int was expected, but a string was supplied.
|
||||||
|
AttributeWrongValueType,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<AttributeError> for DeriveInputParserError {
|
||||||
|
fn from(attribute_error: AttributeError) -> Self {
|
||||||
|
DeriveInputParserError::AttributeError { attribute_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SynError> for DeriveInputParserError {
|
||||||
|
fn from(syn_error: SynError) -> Self {
|
||||||
|
DeriveInputParserError::AttributeError {
|
||||||
|
attribute_error: AttributeError::BadAttributeSyntax { syn_error },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SynError> for AttributeError {
|
||||||
|
fn from(syn_error: SynError) -> Self {
|
||||||
|
AttributeError::BadAttributeSyntax { syn_error }
|
||||||
|
}
|
||||||
|
}
|
@ -1,186 +1,42 @@
|
|||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
|
|
||||||
use proc_macro::TokenStream as TokenStream1;
|
use crate::parse::{parse_derive_input, DeriveInputParseResult};
|
||||||
use proc_macro2::Ident;
|
use crate::render::decoder::{render_enum_decoder, render_struct_decoder};
|
||||||
use proc_macro2::TokenStream as TokenStream2;
|
use crate::render::encoder::{render_enum_encoder, render_struct_encoder};
|
||||||
use quote::{quote, TokenStreamExt};
|
use proc_macro::TokenStream;
|
||||||
use std::iter::FromIterator;
|
use syn::parse_macro_input;
|
||||||
use syn::export::Span;
|
use syn::DeriveInput;
|
||||||
use syn::{parse_macro_input, Data, DeriveInput, Field, Fields, Lit, Meta, NestedMeta};
|
|
||||||
|
|
||||||
#[proc_macro_derive(Packet, attributes(packet))]
|
mod error;
|
||||||
pub fn derive_packet(input: proc_macro::TokenStream) -> TokenStream1 {
|
mod parse;
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
mod render;
|
||||||
let name = &input.ident;
|
|
||||||
|
|
||||||
match input.data {
|
#[proc_macro_derive(Encoder, attributes(data_type))]
|
||||||
Data::Struct(data) => {
|
pub fn derive_encoder(tokens: TokenStream) -> TokenStream {
|
||||||
let fields = &data.fields;
|
let input = parse_macro_input!(tokens as DeriveInput);
|
||||||
|
let derive_parse_result = parse_derive_input(&input).expect("Failed to parse derive input");
|
||||||
|
|
||||||
let encoder = impl_encoder_trait(name, fields);
|
TokenStream::from(match derive_parse_result {
|
||||||
let decoder = impl_decoder_trait(name, fields);
|
DeriveInputParseResult::Struct { name, fields } => render_struct_encoder(name, &fields),
|
||||||
|
DeriveInputParseResult::Enum {
|
||||||
TokenStream1::from(quote! {
|
name,
|
||||||
#encoder
|
discriminant_type,
|
||||||
|
variants,
|
||||||
#decoder
|
} => render_enum_encoder(name, &discriminant_type, &variants),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => panic!("Expected only structures"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn impl_encoder_trait(name: &Ident, fields: &Fields) -> TokenStream2 {
|
#[proc_macro_derive(Decoder, attributes(data_type))]
|
||||||
let encode = quote_field(fields, |field| {
|
pub fn derive_decoder(tokens: TokenStream) -> TokenStream {
|
||||||
let name = &field.ident;
|
let input = parse_macro_input!(tokens as DeriveInput);
|
||||||
|
let derive_parse_result = parse_derive_input(&input).expect("Failed to parse derive input");
|
||||||
|
|
||||||
let unparsed_meta = get_packet_field_meta(field);
|
TokenStream::from(match derive_parse_result {
|
||||||
let parsed_meta = parse_packet_field_meta(&unparsed_meta);
|
DeriveInputParseResult::Struct { name, fields } => render_struct_decoder(name, &fields),
|
||||||
|
DeriveInputParseResult::Enum {
|
||||||
// This is special case because max length are used only for strings.
|
name,
|
||||||
if let Some(max_length) = parsed_meta.max_length {
|
discriminant_type,
|
||||||
return quote! {
|
variants,
|
||||||
crate::EncoderWriteExt::write_string(writer, &self.#name, #max_length)?;
|
} => render_enum_decoder(name, &discriminant_type, &variants),
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let module = parsed_meta.module.as_deref().unwrap_or("Encoder");
|
|
||||||
let module_ident = Ident::new(&module, Span::call_site());
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
crate::#module_ident::encode(&self.#name, writer)?;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
#[automatically_derived]
|
|
||||||
impl crate::Encoder for #name {
|
|
||||||
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::EncodeError> {
|
|
||||||
#encode
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn impl_decoder_trait(name: &Ident, fields: &Fields) -> TokenStream2 {
|
|
||||||
let decode = quote_field(fields, |field| {
|
|
||||||
let name = &field.ident;
|
|
||||||
let ty = &field.ty;
|
|
||||||
|
|
||||||
let unparsed_meta = get_packet_field_meta(field);
|
|
||||||
let parsed_meta = parse_packet_field_meta(&unparsed_meta);
|
|
||||||
|
|
||||||
// This is special case because max length are used only for strings.
|
|
||||||
if let Some(max_length) = parsed_meta.max_length {
|
|
||||||
return quote! {
|
|
||||||
let #name = crate::DecoderReadExt::read_string(reader, #max_length)?;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
match parsed_meta.module {
|
|
||||||
Some(module) => {
|
|
||||||
let module_ident = Ident::new(&module, Span::call_site());
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
let #name = crate::#module_ident::decode(reader)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
quote! {
|
|
||||||
let #name = <#ty as crate::Decoder>::decode(reader)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let create = quote_field(fields, |field| {
|
|
||||||
let name = &field.ident;
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
#name,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
#[automatically_derived]
|
|
||||||
impl crate::Decoder for #name {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::DecodeError> {
|
|
||||||
#decode
|
|
||||||
|
|
||||||
Ok(#name {
|
|
||||||
#create
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct PacketFieldMeta {
|
|
||||||
module: Option<String>,
|
|
||||||
max_length: Option<u16>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_packet_field_meta(meta_list: &Vec<NestedMeta>) -> PacketFieldMeta {
|
|
||||||
let mut module = None;
|
|
||||||
let mut max_length = None;
|
|
||||||
|
|
||||||
for meta in meta_list {
|
|
||||||
match meta {
|
|
||||||
NestedMeta::Meta(Meta::NameValue(named_meta)) => match &named_meta.path {
|
|
||||||
path if path.is_ident("with") => match &named_meta.lit {
|
|
||||||
Lit::Str(lit_str) => module = Some(lit_str.value()),
|
|
||||||
_ => panic!("\"with\" attribute value must be string"),
|
|
||||||
},
|
|
||||||
path if path.is_ident("max_length") => match &named_meta.lit {
|
|
||||||
Lit::Int(lit_int) => {
|
|
||||||
max_length = Some(
|
|
||||||
lit_int
|
|
||||||
.base10_parse::<u16>()
|
|
||||||
.expect("Failed to parse max length attribute"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
_ => panic!("\"max_length\" attribute value must be integer"),
|
|
||||||
},
|
|
||||||
path => panic!(
|
|
||||||
"Received unrecognized attribute : \"{}\"",
|
|
||||||
path.get_ident().unwrap()
|
|
||||||
),
|
|
||||||
},
|
|
||||||
_ => panic!("Expected only named meta values"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
PacketFieldMeta { module, max_length }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_packet_field_meta(field: &Field) -> Vec<NestedMeta> {
|
|
||||||
field
|
|
||||||
.attrs
|
|
||||||
.iter()
|
|
||||||
.filter(|a| a.path.is_ident("packet"))
|
|
||||||
.map(|a| a.parse_meta().expect("Failed to parse field attribute"))
|
|
||||||
.map(|m| match m {
|
|
||||||
Meta::List(meta_list) => Vec::from_iter(meta_list.nested),
|
|
||||||
_ => panic!("Expected only list attributes"),
|
|
||||||
})
|
|
||||||
.flatten()
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn quote_field<F: Fn(&Field) -> TokenStream2>(fields: &Fields, func: F) -> TokenStream2 {
|
|
||||||
let mut output = quote!();
|
|
||||||
|
|
||||||
match fields {
|
|
||||||
Fields::Named(named_fields) => {
|
|
||||||
output.append_all(named_fields.named.iter().map(|f| func(f)))
|
|
||||||
}
|
|
||||||
_ => panic!("Expected only for named fields"),
|
|
||||||
}
|
|
||||||
|
|
||||||
output
|
|
||||||
}
|
|
||||||
|
291
protocol-derive/src/parse.rs
Normal file
291
protocol-derive/src/parse.rs
Normal file
@ -0,0 +1,291 @@
|
|||||||
|
use crate::error::{AttributeError, DeriveInputParserError};
|
||||||
|
use proc_macro2::Ident;
|
||||||
|
use std::iter::FromIterator;
|
||||||
|
use syn::punctuated::Punctuated;
|
||||||
|
use syn::{
|
||||||
|
Attribute, Data, DeriveInput, ExprLit, Field, Fields, FieldsNamed, Lit, Meta, NestedMeta, Type,
|
||||||
|
};
|
||||||
|
use syn::{Error as SynError, Variant};
|
||||||
|
use syn::{Expr, Token};
|
||||||
|
|
||||||
|
pub(crate) enum DeriveInputParseResult<'a> {
|
||||||
|
Struct {
|
||||||
|
name: &'a Ident,
|
||||||
|
fields: Vec<FieldData<'a>>,
|
||||||
|
},
|
||||||
|
Enum {
|
||||||
|
name: &'a Ident,
|
||||||
|
discriminant_type: DiscriminantType,
|
||||||
|
variants: Vec<VariantData<'a>>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct VariantData<'a> {
|
||||||
|
pub(crate) discriminant: usize,
|
||||||
|
pub(crate) name: &'a Ident,
|
||||||
|
pub(crate) fields: Vec<FieldData<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct FieldData<'a> {
|
||||||
|
pub(crate) name: &'a Ident,
|
||||||
|
pub(crate) ty: &'a Type,
|
||||||
|
pub(crate) attribute: AttributeData,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
pub(crate) enum AttributeData {
|
||||||
|
With { module: String },
|
||||||
|
MaxLength { length: usize },
|
||||||
|
Bitfield { idx: u8, position: BitfieldPosition },
|
||||||
|
Empty,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
pub(crate) enum DiscriminantType {
|
||||||
|
UnsignedByte,
|
||||||
|
VarInt,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
pub(crate) enum BitfieldPosition {
|
||||||
|
Start,
|
||||||
|
Intermediate,
|
||||||
|
End,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn parse_derive_input(
|
||||||
|
input: &DeriveInput,
|
||||||
|
) -> Result<DeriveInputParseResult, DeriveInputParserError> {
|
||||||
|
let name = &input.ident;
|
||||||
|
|
||||||
|
match &input.data {
|
||||||
|
Data::Struct(data_struct) => match &data_struct.fields {
|
||||||
|
Fields::Named(named_fields) => {
|
||||||
|
let fields = parse_fields(named_fields)?;
|
||||||
|
|
||||||
|
Ok(DeriveInputParseResult::Struct { name, fields })
|
||||||
|
}
|
||||||
|
_ => Err(DeriveInputParserError::UnnamedDataFields),
|
||||||
|
},
|
||||||
|
Data::Enum(data_enum) => {
|
||||||
|
let variants = parse_variants(&data_enum.variants)?;
|
||||||
|
let discriminant_type = parse_discriminant_type(&input.attrs)?;
|
||||||
|
|
||||||
|
Ok(DeriveInputParseResult::Enum {
|
||||||
|
name,
|
||||||
|
discriminant_type,
|
||||||
|
variants,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_ => Err(DeriveInputParserError::UnsupportedData),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_discriminant_type(
|
||||||
|
attributes: &Vec<Attribute>,
|
||||||
|
) -> Result<DiscriminantType, DeriveInputParserError> {
|
||||||
|
let nested_metas = parse_attributes_nested_metas(attributes)?;
|
||||||
|
let attribute = parse_attribute(nested_metas, None, 0)?;
|
||||||
|
|
||||||
|
match attribute {
|
||||||
|
AttributeData::With { module } if module == "var_int" => Ok(DiscriminantType::VarInt),
|
||||||
|
_ => Ok(DiscriminantType::UnsignedByte),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_variants(
|
||||||
|
variants: &Punctuated<Variant, Token![,]>,
|
||||||
|
) -> Result<Vec<VariantData>, DeriveInputParserError> {
|
||||||
|
variants
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(idx, v)| parse_variant(idx, v))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_variant(idx: usize, variant: &Variant) -> Result<VariantData, DeriveInputParserError> {
|
||||||
|
let discriminant = parse_variant_discriminant(variant).unwrap_or(idx);
|
||||||
|
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 {
|
||||||
|
discriminant,
|
||||||
|
name,
|
||||||
|
fields,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_variant_discriminant(variant: &Variant) -> Option<usize> {
|
||||||
|
variant
|
||||||
|
.discriminant
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|(_, expr)| match expr {
|
||||||
|
Expr::Lit(ExprLit {
|
||||||
|
lit: Lit::Int(lit_int),
|
||||||
|
..
|
||||||
|
}) => lit_int.base10_parse().ok(),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_fields(named_fields: &FieldsNamed) -> Result<Vec<FieldData>, DeriveInputParserError> {
|
||||||
|
let mut fields_data = Vec::new();
|
||||||
|
let mut current_bitfield_idx = 0;
|
||||||
|
|
||||||
|
let fields: Vec<&Field> = named_fields.named.iter().collect();
|
||||||
|
|
||||||
|
for (idx, field) in fields.iter().enumerate() {
|
||||||
|
let name = field.ident.as_ref().unwrap();
|
||||||
|
let ty = &field.ty;
|
||||||
|
|
||||||
|
let nested_metas = parse_attributes_nested_metas(&field.attrs)?;
|
||||||
|
|
||||||
|
let next_field_opt = fields.get(idx + 1);
|
||||||
|
let next_nested_metas_opt = next_field_opt
|
||||||
|
.and_then(|next_field| parse_attributes_nested_metas(&next_field.attrs).ok());
|
||||||
|
|
||||||
|
let attribute = parse_attribute(nested_metas, next_nested_metas_opt, current_bitfield_idx)?;
|
||||||
|
|
||||||
|
match attribute {
|
||||||
|
AttributeData::Bitfield { .. } => current_bitfield_idx += 1,
|
||||||
|
_ => current_bitfield_idx = 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
fields_data.push(FieldData {
|
||||||
|
name,
|
||||||
|
ty,
|
||||||
|
attribute,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(fields_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_attributes_nested_metas(
|
||||||
|
attributes: &Vec<Attribute>,
|
||||||
|
) -> Result<Vec<NestedMeta>, DeriveInputParserError> {
|
||||||
|
let parsed_metas = attributes
|
||||||
|
.iter()
|
||||||
|
.filter(|a| a.path.is_ident("data_type"))
|
||||||
|
.map(|a| a.parse_meta())
|
||||||
|
.collect::<Result<Vec<Meta>, SynError>>()?;
|
||||||
|
|
||||||
|
let nested_metas = parsed_metas
|
||||||
|
.into_iter()
|
||||||
|
.map(|m| match m {
|
||||||
|
Meta::List(meta_list) => Ok(Vec::from_iter(meta_list.nested)),
|
||||||
|
_ => Err(AttributeError::UnsupportedAttribute),
|
||||||
|
})
|
||||||
|
.collect::<Result<Vec<Vec<NestedMeta>>, AttributeError>>()?;
|
||||||
|
|
||||||
|
Ok(nested_metas.into_iter().flatten().collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_attribute(
|
||||||
|
nested_metas: Vec<NestedMeta>,
|
||||||
|
next_nested_metas_opt: Option<Vec<NestedMeta>>,
|
||||||
|
current_bitfield_idx: u8,
|
||||||
|
) -> Result<AttributeData, DeriveInputParserError> {
|
||||||
|
let simple_attribute_parsers: Vec<fn(&NestedMeta) -> Result<AttributeData, AttributeError>> =
|
||||||
|
vec![get_module_attribute, get_max_length_attribute];
|
||||||
|
|
||||||
|
for nested_meta in nested_metas.iter() {
|
||||||
|
let bitfield_attribute =
|
||||||
|
get_bitfield_attribute(current_bitfield_idx, nested_meta, &next_nested_metas_opt);
|
||||||
|
|
||||||
|
if bitfield_attribute != AttributeData::Empty {
|
||||||
|
return Ok(bitfield_attribute);
|
||||||
|
}
|
||||||
|
|
||||||
|
for attribute_parser in simple_attribute_parsers.iter() {
|
||||||
|
let attribute = attribute_parser(nested_meta)?;
|
||||||
|
|
||||||
|
if attribute != AttributeData::Empty {
|
||||||
|
return Ok(attribute);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(AttributeData::Empty)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_module_attribute(nested_meta: &NestedMeta) -> Result<AttributeData, AttributeError> {
|
||||||
|
if let NestedMeta::Meta(Meta::NameValue(named_meta)) = nested_meta {
|
||||||
|
if matches!(&named_meta.path, path if path.is_ident("with")) {
|
||||||
|
return match &named_meta.lit {
|
||||||
|
Lit::Str(lit_str) => Ok(AttributeData::With {
|
||||||
|
module: lit_str.value(),
|
||||||
|
}),
|
||||||
|
_ => Err(AttributeError::AttributeWrongValueType),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(AttributeData::Empty)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_max_length_attribute(nested_meta: &NestedMeta) -> Result<AttributeData, AttributeError> {
|
||||||
|
if let NestedMeta::Meta(Meta::NameValue(named_meta)) = nested_meta {
|
||||||
|
if matches!(&named_meta.path, path if path.is_ident("max_length")) {
|
||||||
|
return match &named_meta.lit {
|
||||||
|
Lit::Int(lit_int) => Ok(AttributeData::MaxLength {
|
||||||
|
length: lit_int.base10_parse()?,
|
||||||
|
}),
|
||||||
|
_ => Err(AttributeError::AttributeWrongValueType),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(AttributeData::Empty)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_bitfield_attribute(
|
||||||
|
current_bitfield_idx: u8,
|
||||||
|
nested_meta: &NestedMeta,
|
||||||
|
next_nested_metas_opt: &Option<Vec<NestedMeta>>,
|
||||||
|
) -> AttributeData {
|
||||||
|
if is_bitfield_attribute(nested_meta) {
|
||||||
|
let position = calc_bitfield_position(current_bitfield_idx, next_nested_metas_opt);
|
||||||
|
|
||||||
|
AttributeData::Bitfield {
|
||||||
|
idx: current_bitfield_idx,
|
||||||
|
position,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
AttributeData::Empty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calc_bitfield_position(
|
||||||
|
current_bitfield_idx: u8,
|
||||||
|
next_nested_metas_opt: &Option<Vec<NestedMeta>>,
|
||||||
|
) -> BitfieldPosition {
|
||||||
|
fn next_has_bitfield_attribute(next_nested_metas: &Vec<NestedMeta>) -> bool {
|
||||||
|
next_nested_metas
|
||||||
|
.iter()
|
||||||
|
.any(|nested_meta| is_bitfield_attribute(nested_meta))
|
||||||
|
}
|
||||||
|
|
||||||
|
match next_nested_metas_opt {
|
||||||
|
Some(next_nested_metas) if (next_has_bitfield_attribute(&next_nested_metas)) => {
|
||||||
|
if current_bitfield_idx == 0 {
|
||||||
|
BitfieldPosition::Start
|
||||||
|
} else {
|
||||||
|
BitfieldPosition::Intermediate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => BitfieldPosition::End,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_bitfield_attribute(nested_meta: &NestedMeta) -> bool {
|
||||||
|
match nested_meta {
|
||||||
|
NestedMeta::Meta(Meta::Path(path)) => path.is_ident("bitfield"),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
185
protocol-derive/src/render/decoder.rs
Normal file
185
protocol-derive/src/render/decoder.rs
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
use crate::parse::{AttributeData, BitfieldPosition, DiscriminantType, FieldData, VariantData};
|
||||||
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
|
use proc_macro2::{Ident, Span};
|
||||||
|
use quote::quote;
|
||||||
|
use syn::Type;
|
||||||
|
|
||||||
|
pub(crate) fn render_struct_decoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStream2 {
|
||||||
|
let field_names_joined_comma = render_field_names_joined_comma(fields);
|
||||||
|
let render_fields = render_fields(fields);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#[automatically_derived]
|
||||||
|
impl crate::decoder::Decoder for #name {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
|
||||||
|
#render_fields
|
||||||
|
|
||||||
|
Ok(#name {
|
||||||
|
#field_names_joined_comma
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn render_enum_decoder(
|
||||||
|
name: &Ident,
|
||||||
|
discriminant_type: &DiscriminantType,
|
||||||
|
variants: &Vec<VariantData>,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
let render_variants = render_variants(discriminant_type, variants);
|
||||||
|
let render_discriminant_type = render_discriminant_type(discriminant_type);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#[automatically_derived]
|
||||||
|
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 = #render_discriminant_type;
|
||||||
|
|
||||||
|
match type_id {
|
||||||
|
#render_variants
|
||||||
|
_ => Err(DecodeError::UnknownEnumType { type_id: type_id as usize, }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_variants(
|
||||||
|
discriminant_type: &DiscriminantType,
|
||||||
|
variants: &Vec<VariantData>,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
variants
|
||||||
|
.iter()
|
||||||
|
.map(|v| render_variant(discriminant_type, v))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_variant(discriminant_type: &DiscriminantType, variant: &VariantData) -> TokenStream2 {
|
||||||
|
if variant.fields.is_empty() {
|
||||||
|
render_unit_variant(discriminant_type, variant)
|
||||||
|
} else {
|
||||||
|
render_struct_variant(discriminant_type, variant)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_unit_variant(
|
||||||
|
discriminant_type: &DiscriminantType,
|
||||||
|
variant: &VariantData,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
let discriminant = render_discriminant(discriminant_type, variant.discriminant);
|
||||||
|
let name = variant.name;
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#discriminant => Ok(Self::#name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_struct_variant(
|
||||||
|
discriminant_type: &DiscriminantType,
|
||||||
|
variant: &VariantData,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
let discriminant = render_discriminant(discriminant_type, variant.discriminant);
|
||||||
|
let name = variant.name;
|
||||||
|
let fields = &variant.fields;
|
||||||
|
|
||||||
|
let field_names_joined_comma = render_field_names_joined_comma(fields);
|
||||||
|
let render_fields = render_fields(fields);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#discriminant => {
|
||||||
|
#render_fields
|
||||||
|
|
||||||
|
Ok(Self::#name {
|
||||||
|
#field_names_joined_comma
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_discriminant_type(discriminant_type: &DiscriminantType) -> TokenStream2 {
|
||||||
|
match discriminant_type {
|
||||||
|
DiscriminantType::UnsignedByte => {
|
||||||
|
quote!(reader.read_u8()?;)
|
||||||
|
}
|
||||||
|
DiscriminantType::VarInt => {
|
||||||
|
quote!(reader.read_var_i32()?;)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_discriminant(discriminant_type: &DiscriminantType, discriminant: usize) -> TokenStream2 {
|
||||||
|
match discriminant_type {
|
||||||
|
DiscriminantType::UnsignedByte => {
|
||||||
|
let u8 = discriminant as u8;
|
||||||
|
quote!(#u8)
|
||||||
|
}
|
||||||
|
DiscriminantType::VarInt => {
|
||||||
|
let i32 = discriminant as i32;
|
||||||
|
quote!(#i32)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
fields.iter().map(|f| render_field(f)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_field(field: &FieldData) -> TokenStream2 {
|
||||||
|
let name = field.name;
|
||||||
|
let ty = field.ty;
|
||||||
|
|
||||||
|
match &field.attribute {
|
||||||
|
AttributeData::With { module } => render_with_field(name, module),
|
||||||
|
AttributeData::MaxLength { length } => render_max_length_field(name, *length as u16),
|
||||||
|
AttributeData::Bitfield { idx, position } => render_bitfield(name, *idx, position),
|
||||||
|
AttributeData::Empty => render_simple_field(name, ty),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_simple_field(name: &Ident, ty: &Type) -> TokenStream2 {
|
||||||
|
quote! {
|
||||||
|
let #name = <#ty as crate::decoder::Decoder>::decode(reader)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_with_field(name: &Ident, module: &str) -> TokenStream2 {
|
||||||
|
let module_ident = Ident::new(module, Span::call_site());
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
let #name = crate::decoder::#module_ident::decode(reader)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_max_length_field(name: &Ident, max_length: u16) -> TokenStream2 {
|
||||||
|
quote! {
|
||||||
|
let #name = crate::decoder::DecoderReadExt::read_string(reader, #max_length)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_bitfield(name: &Ident, idx: u8, position: &BitfieldPosition) -> TokenStream2 {
|
||||||
|
let mask = 1u8 << idx;
|
||||||
|
|
||||||
|
let render_mask = quote! {
|
||||||
|
let #name = flags & #mask > 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
match position {
|
||||||
|
BitfieldPosition::Start => {
|
||||||
|
quote! {
|
||||||
|
let flags = reader.read_u8()?;
|
||||||
|
|
||||||
|
#render_mask
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => render_mask,
|
||||||
|
}
|
||||||
|
}
|
191
protocol-derive/src/render/encoder.rs
Normal file
191
protocol-derive/src/render/encoder.rs
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
use crate::parse::{AttributeData, BitfieldPosition, DiscriminantType, FieldData, VariantData};
|
||||||
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
|
use proc_macro2::{Ident, Span};
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
pub(crate) fn render_struct_encoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStream2 {
|
||||||
|
let render_fields = render_fields(fields, true);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#[automatically_derived]
|
||||||
|
impl crate::encoder::Encoder for #name {
|
||||||
|
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::error::EncodeError> {
|
||||||
|
#render_fields
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn render_enum_encoder(
|
||||||
|
name: &Ident,
|
||||||
|
discriminant_type: &DiscriminantType,
|
||||||
|
variants: &Vec<VariantData>,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
let render_variants = render_variants(discriminant_type, 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
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_variants(
|
||||||
|
discriminant_type: &DiscriminantType,
|
||||||
|
variants: &Vec<VariantData>,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
variants
|
||||||
|
.iter()
|
||||||
|
.map(|v| render_variant(discriminant_type, v))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_variant(discriminant_type: &DiscriminantType, variant: &VariantData) -> TokenStream2 {
|
||||||
|
if variant.fields.is_empty() {
|
||||||
|
render_unit_variant(discriminant_type, variant)
|
||||||
|
} else {
|
||||||
|
render_struct_variant(discriminant_type, variant)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_unit_variant(
|
||||||
|
discriminant_type: &DiscriminantType,
|
||||||
|
variant: &VariantData,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
let discriminant = variant.discriminant;
|
||||||
|
let name = variant.name;
|
||||||
|
|
||||||
|
let render_discriminant_type = render_discriminant_type(discriminant_type, discriminant);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
Self::#name => {
|
||||||
|
#render_discriminant_type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_struct_variant(
|
||||||
|
discriminant_type: &DiscriminantType,
|
||||||
|
variant: &VariantData,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
let discriminant = variant.discriminant;
|
||||||
|
let name = variant.name;
|
||||||
|
let fields = &variant.fields;
|
||||||
|
|
||||||
|
let render_discriminant_type = render_discriminant_type(discriminant_type, discriminant);
|
||||||
|
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
|
||||||
|
} => {
|
||||||
|
#render_discriminant_type
|
||||||
|
|
||||||
|
#render_fields
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_discriminant_type(
|
||||||
|
discriminant_type: &DiscriminantType,
|
||||||
|
discriminant: usize,
|
||||||
|
) -> TokenStream2 {
|
||||||
|
match discriminant_type {
|
||||||
|
DiscriminantType::UnsignedByte => {
|
||||||
|
let u8 = discriminant as u8;
|
||||||
|
|
||||||
|
quote!(writer.write_u8(#u8)?;)
|
||||||
|
}
|
||||||
|
DiscriminantType::VarInt => {
|
||||||
|
let var_i32 = discriminant as i32;
|
||||||
|
|
||||||
|
quote!(writer.write_var_i32(#var_i32)?;)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
match &field.attribute {
|
||||||
|
AttributeData::With { module } => render_with_field(name, module, with_self),
|
||||||
|
AttributeData::MaxLength { length } => {
|
||||||
|
render_max_length_field(name, *length as u16, with_self)
|
||||||
|
}
|
||||||
|
AttributeData::Bitfield { idx, position } => render_bitfield(name, *idx, position),
|
||||||
|
AttributeData::Empty => render_simple_field(name, with_self),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_simple_field(name: &Ident, with_self: bool) -> TokenStream2 {
|
||||||
|
render_with_field(name, "Encoder", with_self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_with_field(name: &Ident, module: &str, with_self: bool) -> TokenStream2 {
|
||||||
|
let module_ident = Ident::new(module, Span::call_site());
|
||||||
|
let final_name = get_field_final_name(name, with_self);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
crate::encoder::#module_ident::encode(#final_name, writer)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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! {
|
||||||
|
crate::encoder::EncoderWriteExt::write_string(writer, #final_name, #max_length)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_bitfield(name: &Ident, idx: u8, position: &BitfieldPosition) -> TokenStream2 {
|
||||||
|
let mask = 1u8 << idx;
|
||||||
|
|
||||||
|
let render_mask = quote! {
|
||||||
|
if self.#name {
|
||||||
|
flags |= #mask;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match position {
|
||||||
|
BitfieldPosition::Start => quote!(
|
||||||
|
let mut flags = 0;
|
||||||
|
|
||||||
|
#render_mask
|
||||||
|
),
|
||||||
|
BitfieldPosition::Intermediate => render_mask,
|
||||||
|
BitfieldPosition::End => {
|
||||||
|
quote! {
|
||||||
|
#render_mask
|
||||||
|
|
||||||
|
writer.write_u8(flags)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_field_final_name(name: &Ident, with_self: bool) -> TokenStream2 {
|
||||||
|
if with_self {
|
||||||
|
quote!(&self.#name)
|
||||||
|
} else {
|
||||||
|
quote!(#name)
|
||||||
|
}
|
||||||
|
}
|
2
protocol-derive/src/render/mod.rs
Normal file
2
protocol-derive/src/render/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub(crate) mod decoder;
|
||||||
|
pub(crate) mod encoder;
|
@ -16,6 +16,4 @@ byteorder = "1"
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
uuid = { version = "0.7", features = ["v4", "serde"] }
|
uuid = { version = "0.7", features = ["v4", "serde"] }
|
||||||
num-traits = "0.2"
|
named-binary-tag = "0.6"
|
||||||
num-derive = "0.2"
|
|
||||||
named-binary-tag = "0.2"
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
//! ## Serialize
|
//! ## Serialize
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use minecraft_protocol::chat::{Payload, Color, MessageBuilder};
|
//! use minecraft_protocol::data::chat::{MessageBuilder, Payload, Color};
|
||||||
//!
|
//!
|
||||||
//! let message = MessageBuilder::builder(Payload::text("Hello"))
|
//! let message = MessageBuilder::builder(Payload::text("Hello"))
|
||||||
//! .color(Color::Yellow)
|
//! .color(Color::Yellow)
|
||||||
@ -25,7 +25,7 @@
|
|||||||
//! ## Deserialize
|
//! ## Deserialize
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use minecraft_protocol::chat::{MessageBuilder, Color, Payload, Message};
|
//! use minecraft_protocol::data::chat::{MessageBuilder, Payload, Message, Color};
|
||||||
//!
|
//!
|
||||||
//! let json = r#"
|
//! let json = r#"
|
||||||
//! {
|
//! {
|
||||||
@ -68,7 +68,7 @@ use serde::{
|
|||||||
};
|
};
|
||||||
use serde_json::Error;
|
use serde_json::Error;
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum Color {
|
pub enum Color {
|
||||||
Black,
|
Black,
|
||||||
DarkBlue,
|
DarkBlue,
|
||||||
@ -93,7 +93,7 @@ pub enum Color {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use minecraft_protocol::chat::Color;
|
/// use minecraft_protocol::data::chat::Color;
|
||||||
///
|
///
|
||||||
/// let color = Color::Hex("#f98aff".into());
|
/// let color = Color::Hex("#f98aff".into());
|
||||||
/// ```
|
/// ```
|
||||||
@ -175,7 +175,7 @@ impl<'de> Deserialize<'de> for Color {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum ClickAction {
|
pub enum ClickAction {
|
||||||
OpenUrl,
|
OpenUrl,
|
||||||
@ -184,7 +184,7 @@ pub enum ClickAction {
|
|||||||
ChangePage,
|
ChangePage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct ClickEvent {
|
pub struct ClickEvent {
|
||||||
pub action: ClickAction,
|
pub action: ClickAction,
|
||||||
pub value: String,
|
pub value: String,
|
||||||
@ -199,7 +199,7 @@ impl ClickEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum HoverAction {
|
pub enum HoverAction {
|
||||||
ShowText,
|
ShowText,
|
||||||
@ -207,7 +207,7 @@ pub enum HoverAction {
|
|||||||
ShowEntity,
|
ShowEntity,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct HoverEvent {
|
pub struct HoverEvent {
|
||||||
pub action: HoverAction,
|
pub action: HoverAction,
|
||||||
pub value: String,
|
pub value: String,
|
||||||
@ -222,7 +222,7 @@ impl HoverEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum Payload {
|
pub enum Payload {
|
||||||
Text {
|
Text {
|
||||||
@ -280,7 +280,7 @@ impl Payload {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
@ -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)
|
||||||
}
|
}
|
||||||
@ -444,7 +448,7 @@ fn test_serialize_text_hello_world() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/text_hello_world.json")
|
include_str!("../../test/chat/text_hello_world.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -463,7 +467,7 @@ fn test_deserialize_text_hello_world() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/text_hello_world.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/text_hello_world.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -474,7 +478,7 @@ fn test_serialize_translate_opped_steve() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/translate_opped_steve.json")
|
include_str!("../../test/chat/translate_opped_steve.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -485,7 +489,7 @@ fn test_deserialize_translate_opped_steve() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/translate_opped_steve.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/translate_opped_steve.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -503,7 +507,7 @@ fn test_serialize_keybind_jump() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/keybind_jump.json")
|
include_str!("../../test/chat/keybind_jump.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -521,7 +525,7 @@ fn test_deserialize_keybind_jump() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/keybind_jump.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/keybind_jump.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -535,7 +539,7 @@ fn test_serialize_click_open_url() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/click_open_url.json")
|
include_str!("../../test/chat/click_open_url.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -549,7 +553,7 @@ fn test_deserialize_click_open_url() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/click_open_url.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/click_open_url.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -563,7 +567,7 @@ fn test_serialize_click_run_command() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/click_run_command.json")
|
include_str!("../../test/chat/click_run_command.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -577,7 +581,7 @@ fn test_deserialize_click_run_command() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/click_run_command.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/click_run_command.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -591,7 +595,7 @@ fn test_serialize_click_suggest_command() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/click_suggest_command.json")
|
include_str!("../../test/chat/click_suggest_command.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -605,7 +609,7 @@ fn test_deserialize_click_suggest_command() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/click_suggest_command.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/click_suggest_command.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -619,7 +623,7 @@ fn test_serialize_click_change_page() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/click_change_page.json")
|
include_str!("../../test/chat/click_change_page.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -633,7 +637,7 @@ fn test_deserialize_click_change_page() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/click_change_page.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/click_change_page.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -647,7 +651,7 @@ fn test_serialize_hover_show_text() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/hover_show_text.json")
|
include_str!("../../test/chat/hover_show_text.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -661,7 +665,7 @@ fn test_deserialize_hover_show_text() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/hover_show_text.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/hover_show_text.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -675,7 +679,7 @@ fn test_serialize_hover_show_item() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/hover_show_item.json")
|
include_str!("../../test/chat/hover_show_item.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -689,7 +693,7 @@ fn test_deserialize_hover_show_item() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/hover_show_item.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/hover_show_item.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -703,7 +707,7 @@ fn test_serialize_hover_show_entity() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/hover_show_entity.json")
|
include_str!("../../test/chat/hover_show_entity.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -717,7 +721,7 @@ fn test_deserialize_hover_show_entity() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expected_message,
|
expected_message,
|
||||||
Message::from_json(include_str!("../test/chat/hover_show_entity.json")).unwrap()
|
Message::from_json(include_str!("../../test/chat/hover_show_entity.json")).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -729,7 +733,7 @@ fn test_serialize_hex_color() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.to_json().unwrap(),
|
message.to_json().unwrap(),
|
||||||
include_str!("../test/chat/hex_color.json")
|
include_str!("../../test/chat/hex_color.json")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -740,7 +744,7 @@ fn test_deserialize_hex_color() {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Message::from_json(include_str!("../test/chat/hex_color.json")).unwrap(),
|
Message::from_json(include_str!("../../test/chat/hex_color.json")).unwrap(),
|
||||||
expected_message
|
expected_message
|
||||||
);
|
);
|
||||||
}
|
}
|
2
protocol/src/data/mod.rs
Normal file
2
protocol/src/data/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod chat;
|
||||||
|
pub mod server_status;
|
33
protocol/src/data/server_status.rs
Normal file
33
protocol/src/data/server_status.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use crate::data::chat::Message;
|
||||||
|
use crate::impl_json_encoder_decoder;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
|
pub struct ServerStatus {
|
||||||
|
pub version: ServerVersion,
|
||||||
|
pub players: OnlinePlayers,
|
||||||
|
pub description: Message,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
|
pub struct ServerVersion {
|
||||||
|
pub name: String,
|
||||||
|
pub protocol: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
|
pub struct OnlinePlayers {
|
||||||
|
pub max: u32,
|
||||||
|
pub online: u32,
|
||||||
|
#[serde(default)]
|
||||||
|
pub sample: Vec<OnlinePlayer>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||||
|
pub struct OnlinePlayer {
|
||||||
|
pub name: String,
|
||||||
|
pub id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_json_encoder_decoder!(ServerStatus);
|
291
protocol/src/decoder.rs
Normal file
291
protocol/src/decoder.rs
Normal file
@ -0,0 +1,291 @@
|
|||||||
|
use crate::error::DecodeError;
|
||||||
|
use byteorder::{BigEndian, ReadBytesExt};
|
||||||
|
use nbt::CompoundTag;
|
||||||
|
use std::io::Read;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub trait Decoder {
|
||||||
|
type Output;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait adds additional helper methods for `Read` to read protocol data.
|
||||||
|
pub trait DecoderReadExt {
|
||||||
|
fn read_bool(&mut self) -> Result<bool, DecodeError>;
|
||||||
|
|
||||||
|
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError>;
|
||||||
|
|
||||||
|
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError>;
|
||||||
|
|
||||||
|
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError>;
|
||||||
|
|
||||||
|
fn read_var_i32(&mut self) -> Result<i32, DecodeError>;
|
||||||
|
|
||||||
|
fn read_var_i64(&mut self) -> Result<i64, DecodeError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! read_signed_var_int (
|
||||||
|
($type: ident, $name: ident, $max_bytes: expr) => (
|
||||||
|
fn $name(&mut self) -> Result<$type, DecodeError> {
|
||||||
|
let mut bytes = 0;
|
||||||
|
let mut output = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let byte = self.read_u8()?;
|
||||||
|
let value = (byte & 0b01111111) as $type;
|
||||||
|
|
||||||
|
output |= value << 7 * bytes;
|
||||||
|
bytes += 1;
|
||||||
|
|
||||||
|
if bytes > $max_bytes {
|
||||||
|
return Err(DecodeError::VarIntTooLong { max_bytes: $max_bytes })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byte & 0b10000000) == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
impl<R: Read> DecoderReadExt for R {
|
||||||
|
fn read_bool(&mut self) -> Result<bool, DecodeError> {
|
||||||
|
match self.read_u8()? {
|
||||||
|
0 => Ok(false),
|
||||||
|
1 => Ok(true),
|
||||||
|
_ => Err(DecodeError::NonBoolValue),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError> {
|
||||||
|
let length = self.read_var_i32()? as usize;
|
||||||
|
|
||||||
|
if length as u16 > max_length {
|
||||||
|
return Err(DecodeError::StringTooLong { length, max_length });
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut buf = vec![0; length as usize];
|
||||||
|
self.read_exact(&mut buf)?;
|
||||||
|
|
||||||
|
Ok(String::from_utf8(buf)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError> {
|
||||||
|
let length = self.read_var_i32()?;
|
||||||
|
|
||||||
|
let mut buf = vec![0; length as usize];
|
||||||
|
self.read_exact(&mut buf)?;
|
||||||
|
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError> {
|
||||||
|
Ok(nbt::decode::read_compound_tag(self)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
read_signed_var_int!(i32, read_var_i32, 5);
|
||||||
|
read_signed_var_int!(i64, read_var_i64, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for u8 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_u8()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for i16 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_i16::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for i32 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_i32::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for u16 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_u16::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for u32 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_u32::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for i64 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_i64::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for u64 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_u64::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_string(32_768)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for bool {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_bool()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for Vec<u8> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_byte_array()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for Uuid {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
let mut buf = [0; 16];
|
||||||
|
reader.read_exact(&mut buf)?;
|
||||||
|
|
||||||
|
Ok(Uuid::from_bytes(buf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for CompoundTag {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_compound_tag()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for Vec<CompoundTag> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
let length = reader.read_var_i32()? as usize;
|
||||||
|
let mut vec = Vec::with_capacity(length);
|
||||||
|
|
||||||
|
for _ in 0..length {
|
||||||
|
let compound_tag = reader.read_compound_tag()?;
|
||||||
|
vec.push(compound_tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(vec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod var_int {
|
||||||
|
use crate::decoder::DecoderReadExt;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(reader: &mut R) -> Result<i32, DecodeError> {
|
||||||
|
Ok(reader.read_var_i32()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod var_long {
|
||||||
|
use crate::decoder::DecoderReadExt;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(reader: &mut R) -> Result<i64, DecodeError> {
|
||||||
|
Ok(reader.read_var_i64()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod rest {
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(reader: &mut R) -> Result<Vec<u8>, DecodeError> {
|
||||||
|
let mut data = Vec::new();
|
||||||
|
reader.read_to_end(data.as_mut())?;
|
||||||
|
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod uuid_hyp_str {
|
||||||
|
use crate::decoder::DecoderReadExt;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use std::io::Read;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(reader: &mut R) -> Result<Uuid, DecodeError> {
|
||||||
|
let uuid_hyphenated_string = reader.read_string(36)?;
|
||||||
|
let uuid = Uuid::parse_str(&uuid_hyphenated_string)?;
|
||||||
|
|
||||||
|
Ok(uuid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::decoder::DecoderReadExt;
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_variable_i32_2_bytes_value() {
|
||||||
|
let mut cursor = Cursor::new(vec![0b10101100, 0b00000010]);
|
||||||
|
let value = cursor.read_var_i32().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(value, 300);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_variable_i32_5_bytes_value() {
|
||||||
|
let mut cursor = Cursor::new(vec![0xff, 0xff, 0xff, 0xff, 0x07]);
|
||||||
|
let value = cursor.read_var_i32().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(value, 2147483647);
|
||||||
|
}
|
||||||
|
}
|
255
protocol/src/encoder.rs
Normal file
255
protocol/src/encoder.rs
Normal file
@ -0,0 +1,255 @@
|
|||||||
|
use crate::error::EncodeError;
|
||||||
|
use byteorder::{BigEndian, WriteBytesExt};
|
||||||
|
use nbt::CompoundTag;
|
||||||
|
use std::io::Write;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub trait Encoder {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait adds additional helper methods for `Write` to write protocol data.
|
||||||
|
pub trait EncoderWriteExt {
|
||||||
|
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_var_i32(&mut self, value: i32) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_var_i64(&mut self, value: i64) -> Result<(), EncodeError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! write_signed_var_int (
|
||||||
|
($type: ident, $name: ident) => (
|
||||||
|
fn $name(&mut self, mut value: $type) -> Result<(), EncodeError> {
|
||||||
|
loop {
|
||||||
|
let mut byte = (value & 0b01111111) as u8;
|
||||||
|
value = value >> 7;
|
||||||
|
|
||||||
|
if value != 0 {
|
||||||
|
byte |= 0b10000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.write_u8(byte)?;
|
||||||
|
|
||||||
|
if value == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
impl<W: Write> EncoderWriteExt for W {
|
||||||
|
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError> {
|
||||||
|
if value {
|
||||||
|
self.write_u8(1)?;
|
||||||
|
} else {
|
||||||
|
self.write_u8(0)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError> {
|
||||||
|
let length = value.len();
|
||||||
|
|
||||||
|
if length > max_length as usize {
|
||||||
|
return Err(EncodeError::StringTooLong { length, max_length });
|
||||||
|
}
|
||||||
|
|
||||||
|
self.write_var_i32(value.len() as i32)?;
|
||||||
|
self.write_all(value.as_bytes())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError> {
|
||||||
|
self.write_var_i32(value.len() as i32)?;
|
||||||
|
self.write_all(value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError> {
|
||||||
|
nbt::encode::write_compound_tag(self, &value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
write_signed_var_int!(i32, write_var_i32);
|
||||||
|
write_signed_var_int!(i64, write_var_i64);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for u8 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_u8(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for i16 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_i16::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for i32 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_i32::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for u16 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_u16::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for u32 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_u32::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for i64 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_i64::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for u64 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_u64::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_string(self, 32_768)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for bool {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_bool(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for Vec<u8> {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_byte_array(self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for Uuid {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_all(self.as_bytes())?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for CompoundTag {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_compound_tag(self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for Vec<CompoundTag> {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
writer.write_var_i32(self.len() as i32)?;
|
||||||
|
|
||||||
|
for compound_tag in self {
|
||||||
|
writer.write_compound_tag(&compound_tag)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod var_int {
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use crate::error::EncodeError;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
pub fn encode<W: Write>(value: &i32, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
writer.write_var_i32(*value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod var_long {
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use crate::error::EncodeError;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
pub fn encode<W: Write>(value: &i64, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
writer.write_var_i64(*value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod rest {
|
||||||
|
use crate::error::EncodeError;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
pub fn encode<W: Write>(value: &[u8], writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
writer.write_all(value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod uuid_hyp_str {
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use crate::error::EncodeError;
|
||||||
|
use std::io::Write;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub fn encode<W: Write>(value: &Uuid, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
let uuid_hyphenated_string = value.to_hyphenated().to_string();
|
||||||
|
writer.write_string(&uuid_hyphenated_string, 36)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_write_variable_i32_2_bytes_value() {
|
||||||
|
let mut cursor = Cursor::new(Vec::with_capacity(5));
|
||||||
|
cursor.write_var_i32(300).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.into_inner(), vec![0b10101100, 0b00000010]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_write_variable_i32_5_bytes_value() {
|
||||||
|
let mut cursor = Cursor::new(Vec::with_capacity(5));
|
||||||
|
cursor.write_var_i32(2147483647).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.into_inner(), vec![0xff, 0xff, 0xff, 0xff, 0x07]);
|
||||||
|
}
|
||||||
|
}
|
106
protocol/src/error.rs
Normal file
106
protocol/src/error.rs
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
use nbt::decode::TagDecodeError;
|
||||||
|
use serde_json::error::Error as JsonError;
|
||||||
|
use std::io::Error as IoError;
|
||||||
|
use std::string::FromUtf8Error;
|
||||||
|
use uuid::parser::ParseError as UuidParseError;
|
||||||
|
|
||||||
|
/// Possible errors while encoding packet.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum EncodeError {
|
||||||
|
/// String length can't be more than provided value.
|
||||||
|
StringTooLong {
|
||||||
|
/// String length.
|
||||||
|
length: usize,
|
||||||
|
/// Max string length.
|
||||||
|
max_length: u16,
|
||||||
|
},
|
||||||
|
IOError {
|
||||||
|
io_error: IoError,
|
||||||
|
},
|
||||||
|
JsonError {
|
||||||
|
json_error: JsonError,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<IoError> for EncodeError {
|
||||||
|
fn from(io_error: IoError) -> Self {
|
||||||
|
EncodeError::IOError { io_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<JsonError> for EncodeError {
|
||||||
|
fn from(json_error: JsonError) -> Self {
|
||||||
|
EncodeError::JsonError { json_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Possible errors while decoding packet.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum DecodeError {
|
||||||
|
/// Packet was not recognized. Invalid data or wrong protocol version.
|
||||||
|
UnknownPacketType {
|
||||||
|
type_id: u8,
|
||||||
|
},
|
||||||
|
/// String length can't be more than provided value.
|
||||||
|
StringTooLong {
|
||||||
|
/// String length.
|
||||||
|
length: usize,
|
||||||
|
/// Max string length.
|
||||||
|
max_length: u16,
|
||||||
|
},
|
||||||
|
IOError {
|
||||||
|
io_error: IoError,
|
||||||
|
},
|
||||||
|
JsonError {
|
||||||
|
json_error: JsonError,
|
||||||
|
},
|
||||||
|
/// Byte array was not recognized as valid UTF-8 string.
|
||||||
|
Utf8Error {
|
||||||
|
utf8_error: FromUtf8Error,
|
||||||
|
},
|
||||||
|
/// Boolean are parsed from byte. Valid byte value are 0 or 1.
|
||||||
|
NonBoolValue,
|
||||||
|
UuidParseError {
|
||||||
|
uuid_parse_error: UuidParseError,
|
||||||
|
},
|
||||||
|
/// Type id was not parsed as valid enum value.
|
||||||
|
UnknownEnumType {
|
||||||
|
type_id: usize,
|
||||||
|
},
|
||||||
|
TagDecodeError {
|
||||||
|
tag_decode_error: TagDecodeError,
|
||||||
|
},
|
||||||
|
VarIntTooLong {
|
||||||
|
max_bytes: usize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<IoError> for DecodeError {
|
||||||
|
fn from(io_error: IoError) -> Self {
|
||||||
|
DecodeError::IOError { io_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<JsonError> for DecodeError {
|
||||||
|
fn from(json_error: JsonError) -> Self {
|
||||||
|
DecodeError::JsonError { json_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<FromUtf8Error> for DecodeError {
|
||||||
|
fn from(utf8_error: FromUtf8Error) -> Self {
|
||||||
|
DecodeError::Utf8Error { utf8_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<UuidParseError> for DecodeError {
|
||||||
|
fn from(uuid_parse_error: UuidParseError) -> Self {
|
||||||
|
DecodeError::UuidParseError { uuid_parse_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TagDecodeError> for DecodeError {
|
||||||
|
fn from(tag_decode_error: TagDecodeError) -> Self {
|
||||||
|
DecodeError::TagDecodeError { tag_decode_error }
|
||||||
|
}
|
||||||
|
}
|
@ -1,650 +1,35 @@
|
|||||||
//! This crate implements Minecraft protocol.
|
//! This crate implements Minecraft protocol.
|
||||||
//!
|
//!
|
||||||
//! Information about protocol can be found at https://wiki.vg/Protocol.
|
//! Information about protocol can be found at https://wiki.vg/Protocol.
|
||||||
use io::Error as IoError;
|
pub mod data;
|
||||||
use std::io;
|
pub mod decoder;
|
||||||
use std::io::{Cursor, Read, Write};
|
pub mod encoder;
|
||||||
use std::string::FromUtf8Error;
|
pub mod error;
|
||||||
|
pub mod version;
|
||||||
|
|
||||||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
|
||||||
use serde_json::error::Error as JsonError;
|
|
||||||
use uuid::parser::ParseError as UuidParseError;
|
|
||||||
|
|
||||||
use crate::chat::Message;
|
|
||||||
use nbt::decode::TagDecodeError;
|
|
||||||
use nbt::CompoundTag;
|
|
||||||
use num_traits::{FromPrimitive, ToPrimitive};
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod chat;
|
|
||||||
pub mod game;
|
|
||||||
pub mod login;
|
|
||||||
pub mod status;
|
|
||||||
|
|
||||||
/// Current supported protocol version.
|
|
||||||
pub const PROTOCOL_VERSION: u32 = 498;
|
|
||||||
/// Protocol limits maximum string length.
|
/// Protocol limits maximum string length.
|
||||||
const STRING_MAX_LENGTH: u16 = 32_768;
|
const STRING_MAX_LENGTH: u16 = 32_768;
|
||||||
const HYPHENATED_UUID_LENGTH: u16 = 36;
|
|
||||||
|
|
||||||
/// Possible errors while encoding packet.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum EncodeError {
|
|
||||||
/// String length can't be more than provided value.
|
|
||||||
StringTooLong {
|
|
||||||
/// String length.
|
|
||||||
length: usize,
|
|
||||||
/// Max string length.
|
|
||||||
max_length: u16,
|
|
||||||
},
|
|
||||||
IOError {
|
|
||||||
io_error: IoError,
|
|
||||||
},
|
|
||||||
JsonError {
|
|
||||||
json_error: JsonError,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<IoError> for EncodeError {
|
|
||||||
fn from(io_error: IoError) -> Self {
|
|
||||||
EncodeError::IOError { io_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<JsonError> for EncodeError {
|
|
||||||
fn from(json_error: JsonError) -> Self {
|
|
||||||
EncodeError::JsonError { json_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Possible errors while decoding packet.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum DecodeError {
|
|
||||||
/// Packet was not recognized. Invalid data or wrong protocol version.
|
|
||||||
UnknownPacketType {
|
|
||||||
type_id: u8,
|
|
||||||
},
|
|
||||||
/// String length can't be more than provided value.
|
|
||||||
StringTooLong {
|
|
||||||
/// String length.
|
|
||||||
length: usize,
|
|
||||||
/// Max string length.
|
|
||||||
max_length: u16,
|
|
||||||
},
|
|
||||||
IOError {
|
|
||||||
io_error: IoError,
|
|
||||||
},
|
|
||||||
JsonError {
|
|
||||||
json_error: JsonError,
|
|
||||||
},
|
|
||||||
/// Byte array was not recognized as valid UTF-8 string.
|
|
||||||
Utf8Error {
|
|
||||||
utf8_error: FromUtf8Error,
|
|
||||||
},
|
|
||||||
/// Boolean are parsed from byte. Valid byte value are 0 or 1.
|
|
||||||
NonBoolValue,
|
|
||||||
UuidParseError {
|
|
||||||
uuid_parse_error: UuidParseError,
|
|
||||||
},
|
|
||||||
// Type id was not parsed as valid enum value.
|
|
||||||
UnknownEnumType {
|
|
||||||
type_id: u8,
|
|
||||||
},
|
|
||||||
TagDecodeError {
|
|
||||||
tag_decode_error: TagDecodeError,
|
|
||||||
},
|
|
||||||
VarIntTooLong {
|
|
||||||
max_bytes: usize,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<IoError> for DecodeError {
|
|
||||||
fn from(io_error: IoError) -> Self {
|
|
||||||
DecodeError::IOError { io_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<JsonError> for DecodeError {
|
|
||||||
fn from(json_error: JsonError) -> Self {
|
|
||||||
DecodeError::JsonError { json_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<FromUtf8Error> for DecodeError {
|
|
||||||
fn from(utf8_error: FromUtf8Error) -> Self {
|
|
||||||
DecodeError::Utf8Error { utf8_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<UuidParseError> for DecodeError {
|
|
||||||
fn from(uuid_parse_error: UuidParseError) -> Self {
|
|
||||||
DecodeError::UuidParseError { uuid_parse_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<TagDecodeError> for DecodeError {
|
|
||||||
fn from(tag_decode_error: TagDecodeError) -> Self {
|
|
||||||
DecodeError::TagDecodeError { tag_decode_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trait Encoder {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
trait Decoder {
|
|
||||||
type Output;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! write_signed_var_int (
|
|
||||||
($type: ident, $name: ident) => (
|
|
||||||
fn $name(&mut self, mut value: $type) -> Result<(), EncodeError> {
|
|
||||||
loop {
|
|
||||||
let mut byte = (value & 0b01111111) as u8;
|
|
||||||
value = value >> 7;
|
|
||||||
|
|
||||||
if value != 0 {
|
|
||||||
byte |= 0b10000000;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.write_u8(byte)?;
|
|
||||||
|
|
||||||
if value == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
macro_rules! read_signed_var_int (
|
|
||||||
($type: ident, $name: ident, $max_bytes: expr) => (
|
|
||||||
fn $name(&mut self) -> Result<$type, DecodeError> {
|
|
||||||
let mut bytes = 0;
|
|
||||||
let mut output = 0;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let byte = self.read_u8()?;
|
|
||||||
let value = (byte & 0b01111111) as $type;
|
|
||||||
|
|
||||||
output |= value << 7 * bytes;
|
|
||||||
bytes += 1;
|
|
||||||
|
|
||||||
if bytes > $max_bytes {
|
|
||||||
return Err(DecodeError::VarIntTooLong { max_bytes: $max_bytes })
|
|
||||||
}
|
|
||||||
|
|
||||||
if (byte & 0b10000000) == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(output)
|
|
||||||
}
|
|
||||||
);
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Trait adds additional helper methods for `Write` to write protocol data.
|
|
||||||
trait EncoderWriteExt {
|
|
||||||
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_chat_message(&mut self, value: &Message) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_enum<T: ToPrimitive>(&mut self, value: &T) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_var_i32(&mut self, value: i32) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_var_i64(&mut self, value: i64) -> Result<(), EncodeError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Trait adds additional helper methods for `Read` to read protocol data.
|
|
||||||
trait DecoderReadExt {
|
|
||||||
fn read_bool(&mut self) -> Result<bool, DecodeError>;
|
|
||||||
|
|
||||||
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError>;
|
|
||||||
|
|
||||||
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError>;
|
|
||||||
|
|
||||||
fn read_chat_message(&mut self) -> Result<Message, DecodeError>;
|
|
||||||
|
|
||||||
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError>;
|
|
||||||
|
|
||||||
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError>;
|
|
||||||
|
|
||||||
fn read_var_i32(&mut self) -> Result<i32, DecodeError>;
|
|
||||||
|
|
||||||
fn read_var_i64(&mut self) -> Result<i64, DecodeError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: Write> EncoderWriteExt for W {
|
|
||||||
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError> {
|
|
||||||
if value {
|
|
||||||
self.write_u8(1)?;
|
|
||||||
} else {
|
|
||||||
self.write_u8(0)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError> {
|
|
||||||
let length = value.len();
|
|
||||||
|
|
||||||
if length > max_length as usize {
|
|
||||||
return Err(EncodeError::StringTooLong { length, max_length });
|
|
||||||
}
|
|
||||||
|
|
||||||
self.write_var_i32(value.len() as i32)?;
|
|
||||||
self.write_all(value.as_bytes())?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError> {
|
|
||||||
self.write_var_i32(value.len() as i32)?;
|
|
||||||
self.write_all(value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_chat_message(&mut self, value: &Message) -> Result<(), EncodeError> {
|
|
||||||
self.write_string(&value.to_json()?, STRING_MAX_LENGTH)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_enum<T: ToPrimitive>(&mut self, value: &T) -> Result<(), EncodeError> {
|
|
||||||
let type_value = ToPrimitive::to_u8(value).unwrap();
|
|
||||||
self.write_u8(type_value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError> {
|
|
||||||
nbt::encode::write_compound_tag(self, value.clone())?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
write_signed_var_int!(i32, write_var_i32);
|
|
||||||
write_signed_var_int!(i64, write_var_i64);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: Read> DecoderReadExt for R {
|
|
||||||
fn read_bool(&mut self) -> Result<bool, DecodeError> {
|
|
||||||
match self.read_u8()? {
|
|
||||||
0 => Ok(false),
|
|
||||||
1 => Ok(true),
|
|
||||||
_ => Err(DecodeError::NonBoolValue),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError> {
|
|
||||||
let length = self.read_var_i32()? as usize;
|
|
||||||
|
|
||||||
if length as u16 > max_length {
|
|
||||||
return Err(DecodeError::StringTooLong { length, max_length });
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut buf = vec![0; length as usize];
|
|
||||||
self.read_exact(&mut buf)?;
|
|
||||||
|
|
||||||
Ok(String::from_utf8(buf)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError> {
|
|
||||||
let length = self.read_var_i32()?;
|
|
||||||
|
|
||||||
let mut buf = vec![0; length as usize];
|
|
||||||
self.read_exact(&mut buf)?;
|
|
||||||
|
|
||||||
Ok(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_chat_message(&mut self) -> Result<Message, DecodeError> {
|
|
||||||
let json = self.read_string(STRING_MAX_LENGTH)?;
|
|
||||||
let message = Message::from_json(&json)?;
|
|
||||||
|
|
||||||
Ok(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError> {
|
|
||||||
let type_id = self.read_u8()?;
|
|
||||||
let result = FromPrimitive::from_u8(type_id);
|
|
||||||
|
|
||||||
result.ok_or_else(|| DecodeError::UnknownEnumType { type_id })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError> {
|
|
||||||
Ok(nbt::decode::read_compound_tag(self)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
read_signed_var_int!(i32, read_var_i32, 5);
|
|
||||||
read_signed_var_int!(i64, read_var_i64, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for u8 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_u8(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for u8 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_u8()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for i32 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_i32::<BigEndian>(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for i32 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_i32::<BigEndian>()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for u32 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_u32::<BigEndian>(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for u32 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_u32::<BigEndian>()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for i64 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_i64::<BigEndian>(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for i64 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_i64::<BigEndian>()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for u64 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_u64::<BigEndian>(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for u64 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_u64::<BigEndian>()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for String {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_string(self, STRING_MAX_LENGTH)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for String {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_string(STRING_MAX_LENGTH)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for bool {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_bool(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for bool {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_bool()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for Vec<u8> {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_byte_array(self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for Vec<u8> {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_byte_array()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for Uuid {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_all(self.as_bytes())?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for Uuid {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
let mut buf = [0; 16];
|
|
||||||
reader.read_exact(&mut buf)?;
|
|
||||||
|
|
||||||
Ok(Uuid::from_bytes(buf))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for CompoundTag {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_compound_tag(self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for CompoundTag {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_compound_tag()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for Vec<CompoundTag> {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
writer.write_var_i32(self.len() as i32)?;
|
|
||||||
|
|
||||||
for compound_tag in self {
|
|
||||||
writer.write_compound_tag(&compound_tag)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for Vec<CompoundTag> {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
let length = reader.read_var_i32()? as usize;
|
|
||||||
let mut vec = Vec::with_capacity(length);
|
|
||||||
|
|
||||||
for _ in 0..length {
|
|
||||||
let compound_tag = reader.read_compound_tag()?;
|
|
||||||
vec.push(compound_tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(vec)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! impl_enum_encoder_decoder (
|
|
||||||
($ty: ident) => (
|
|
||||||
impl crate::Encoder for $ty {
|
|
||||||
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::EncodeError> {
|
|
||||||
Ok(crate::EncoderWriteExt::write_enum(writer, self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl crate::Decoder for $ty {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::DecodeError> {
|
|
||||||
Ok(crate::DecoderReadExt::read_enum(reader)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
);
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! impl_json_encoder_decoder (
|
macro_rules! impl_json_encoder_decoder (
|
||||||
($ty: ident) => (
|
($ty: ident) => (
|
||||||
impl crate::Encoder for $ty {
|
impl crate::encoder::Encoder for $ty {
|
||||||
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::EncodeError> {
|
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::error::EncodeError> {
|
||||||
let json = serde_json::to_string(self)?;
|
let json = serde_json::to_string(self)?;
|
||||||
crate::EncoderWriteExt::write_string(writer, &json, crate::STRING_MAX_LENGTH)?;
|
crate::encoder::EncoderWriteExt::write_string(writer, &json, crate::STRING_MAX_LENGTH)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::Decoder for $ty {
|
impl crate::decoder::Decoder for $ty {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::DecodeError> {
|
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
|
||||||
let json = crate::DecoderReadExt::read_string(reader, crate::STRING_MAX_LENGTH)?;
|
let json = crate::decoder::DecoderReadExt::read_string(reader, crate::STRING_MAX_LENGTH)?;
|
||||||
|
|
||||||
Ok(serde_json::from_str(&json)?)
|
Ok(serde_json::from_str(&json)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
);
|
);
|
||||||
|
|
||||||
mod var_int {
|
|
||||||
use crate::{DecodeError, EncodeError};
|
|
||||||
use crate::{DecoderReadExt, EncoderWriteExt};
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
|
|
||||||
pub fn encode<W: Write>(value: &i32, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
writer.write_var_i32(*value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode<R: Read>(reader: &mut R) -> Result<i32, DecodeError> {
|
|
||||||
Ok(reader.read_var_i32()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod var_long {
|
|
||||||
use crate::{DecodeError, EncodeError};
|
|
||||||
use crate::{DecoderReadExt, EncoderWriteExt};
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
|
|
||||||
pub fn encode<W: Write>(value: &i64, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
writer.write_var_i64(*value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode<R: Read>(reader: &mut R) -> Result<i64, DecodeError> {
|
|
||||||
Ok(reader.read_var_i64()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod rest {
|
|
||||||
use crate::{DecodeError, EncodeError};
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
|
|
||||||
pub fn encode<W: Write>(value: &[u8], writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
writer.write_all(value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode<R: Read>(reader: &mut R) -> Result<Vec<u8>, DecodeError> {
|
|
||||||
let mut data = Vec::new();
|
|
||||||
reader.read_to_end(data.as_mut())?;
|
|
||||||
|
|
||||||
Ok(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod uuid_hyp_str {
|
|
||||||
use crate::{
|
|
||||||
DecodeError, DecoderReadExt, EncodeError, EncoderWriteExt, HYPHENATED_UUID_LENGTH,
|
|
||||||
};
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub fn encode<W: Write>(value: &Uuid, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
let uuid_hyphenated_string = value.to_hyphenated().to_string();
|
|
||||||
writer.write_string(&uuid_hyphenated_string, HYPHENATED_UUID_LENGTH)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode<R: Read>(reader: &mut R) -> Result<Uuid, DecodeError> {
|
|
||||||
let uuid_hyphenated_string = reader.read_string(HYPHENATED_UUID_LENGTH)?;
|
|
||||||
let uuid = Uuid::parse_str(&uuid_hyphenated_string)?;
|
|
||||||
|
|
||||||
Ok(uuid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_read_variable_i32_2_bytes_value() {
|
|
||||||
let mut cursor = Cursor::new(vec![0b10101100, 0b00000010]);
|
|
||||||
let value = cursor.read_var_i32().unwrap();
|
|
||||||
|
|
||||||
assert_eq!(value, 300);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_read_variable_i32_5_bytes_value() {
|
|
||||||
let mut cursor = Cursor::new(vec![0xff, 0xff, 0xff, 0xff, 0x07]);
|
|
||||||
let value = cursor.read_var_i32().unwrap();
|
|
||||||
|
|
||||||
assert_eq!(value, 2147483647);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_write_variable_i32_2_bytes_value() {
|
|
||||||
let mut cursor = Cursor::new(Vec::with_capacity(5));
|
|
||||||
cursor.write_var_i32(300).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(cursor.into_inner(), vec![0b10101100, 0b00000010]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_write_variable_i32_5_bytes_value() {
|
|
||||||
let mut cursor = Cursor::new(Vec::with_capacity(5));
|
|
||||||
cursor.write_var_i32(2147483647).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(cursor.into_inner(), vec![0xff, 0xff, 0xff, 0xff, 0x07]);
|
|
||||||
}
|
|
||||||
|
1
protocol/src/version/mod.rs
Normal file
1
protocol/src/version/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod v1_14_4;
|
@ -1,16 +1,18 @@
|
|||||||
use num_derive::{FromPrimitive, ToPrimitive};
|
use crate::data::chat::Message;
|
||||||
|
use crate::decoder::Decoder;
|
||||||
use crate::chat::Message;
|
use crate::decoder::DecoderReadExt;
|
||||||
use crate::impl_enum_encoder_decoder;
|
use crate::encoder::EncoderWriteExt;
|
||||||
use crate::DecodeError;
|
use crate::error::DecodeError;
|
||||||
use crate::Decoder;
|
use byteorder::{ReadBytesExt, WriteBytesExt};
|
||||||
use minecraft_protocol_derive::Packet;
|
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),
|
||||||
ServerBoundKeepAlive(ServerBoundKeepAlive),
|
ServerBoundKeepAlive(ServerBoundKeepAlive),
|
||||||
|
ServerBoundAbilities(ServerBoundAbilities),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum GameClientBoundPacket {
|
pub enum GameClientBoundPacket {
|
||||||
@ -19,6 +21,8 @@ pub enum GameClientBoundPacket {
|
|||||||
ClientBoundKeepAlive(ClientBoundKeepAlive),
|
ClientBoundKeepAlive(ClientBoundKeepAlive),
|
||||||
ChunkData(ChunkData),
|
ChunkData(ChunkData),
|
||||||
GameDisconnect(GameDisconnect),
|
GameDisconnect(GameDisconnect),
|
||||||
|
BossBar(BossBar),
|
||||||
|
EntityAction(EntityAction),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameServerBoundPacket {
|
impl GameServerBoundPacket {
|
||||||
@ -26,6 +30,7 @@ impl GameServerBoundPacket {
|
|||||||
match self {
|
match self {
|
||||||
GameServerBoundPacket::ServerBoundChatMessage(_) => 0x03,
|
GameServerBoundPacket::ServerBoundChatMessage(_) => 0x03,
|
||||||
GameServerBoundPacket::ServerBoundKeepAlive(_) => 0x0F,
|
GameServerBoundPacket::ServerBoundKeepAlive(_) => 0x0F,
|
||||||
|
GameServerBoundPacket::ServerBoundAbilities(_) => 0x19,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,6 +59,8 @@ impl GameClientBoundPacket {
|
|||||||
GameClientBoundPacket::ClientBoundKeepAlive(_) => 0x20,
|
GameClientBoundPacket::ClientBoundKeepAlive(_) => 0x20,
|
||||||
GameClientBoundPacket::ChunkData(_) => 0x21,
|
GameClientBoundPacket::ChunkData(_) => 0x21,
|
||||||
GameClientBoundPacket::JoinGame(_) => 0x25,
|
GameClientBoundPacket::JoinGame(_) => 0x25,
|
||||||
|
GameClientBoundPacket::BossBar(_) => 0x0D,
|
||||||
|
GameClientBoundPacket::EntityAction(_) => 0x1B,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,9 +96,9 @@ impl GameClientBoundPacket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct ServerBoundChatMessage {
|
pub struct ServerBoundChatMessage {
|
||||||
#[packet(max_length = 256)]
|
#[data_type(max_length = 256)]
|
||||||
pub message: String,
|
pub message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,21 +110,19 @@ impl ServerBoundChatMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct ClientBoundChatMessage {
|
pub struct ClientBoundChatMessage {
|
||||||
pub message: Message,
|
pub message: Message,
|
||||||
pub position: MessagePosition,
|
pub position: MessagePosition,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, FromPrimitive, ToPrimitive)]
|
#[derive(Encoder, Decoder, Debug, Eq, PartialEq)]
|
||||||
pub enum MessagePosition {
|
pub enum MessagePosition {
|
||||||
Chat,
|
Chat,
|
||||||
System,
|
System,
|
||||||
HotBar,
|
HotBar,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_enum_encoder_decoder!(MessagePosition);
|
|
||||||
|
|
||||||
impl ClientBoundChatMessage {
|
impl ClientBoundChatMessage {
|
||||||
pub fn new(message: Message, position: MessagePosition) -> GameClientBoundPacket {
|
pub fn new(message: Message, position: MessagePosition) -> GameClientBoundPacket {
|
||||||
let chat_message = ClientBoundChatMessage { message, position };
|
let chat_message = ClientBoundChatMessage { message, position };
|
||||||
@ -126,20 +131,20 @@ impl ClientBoundChatMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct JoinGame {
|
pub struct JoinGame {
|
||||||
pub entity_id: u32,
|
pub entity_id: u32,
|
||||||
pub game_mode: GameMode,
|
pub game_mode: GameMode,
|
||||||
pub dimension: i32,
|
pub dimension: i32,
|
||||||
pub max_players: u8,
|
pub max_players: u8,
|
||||||
#[packet(max_length = 16)]
|
#[data_type(max_length = 16)]
|
||||||
pub level_type: String,
|
pub level_type: String,
|
||||||
#[packet(with = "var_int")]
|
#[data_type(with = "var_int")]
|
||||||
pub view_distance: i32,
|
pub view_distance: i32,
|
||||||
pub reduced_debug_info: bool,
|
pub reduced_debug_info: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, FromPrimitive, ToPrimitive)]
|
#[derive(Encoder, Decoder, Debug, Eq, PartialEq)]
|
||||||
pub enum GameMode {
|
pub enum GameMode {
|
||||||
Survival = 0,
|
Survival = 0,
|
||||||
Creative = 1,
|
Creative = 1,
|
||||||
@ -148,8 +153,6 @@ pub enum GameMode {
|
|||||||
Hardcore = 8,
|
Hardcore = 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_enum_encoder_decoder!(GameMode);
|
|
||||||
|
|
||||||
impl JoinGame {
|
impl JoinGame {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
entity_id: u32,
|
entity_id: u32,
|
||||||
@ -174,7 +177,7 @@ impl JoinGame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet)]
|
#[derive(Encoder, Decoder)]
|
||||||
pub struct ServerBoundKeepAlive {
|
pub struct ServerBoundKeepAlive {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
}
|
}
|
||||||
@ -187,7 +190,7 @@ impl ServerBoundKeepAlive {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet)]
|
#[derive(Encoder, Decoder)]
|
||||||
pub struct ClientBoundKeepAlive {
|
pub struct ClientBoundKeepAlive {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
}
|
}
|
||||||
@ -200,12 +203,12 @@ impl ClientBoundKeepAlive {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct ChunkData {
|
pub struct ChunkData {
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub z: i32,
|
pub z: i32,
|
||||||
pub full: bool,
|
pub full: bool,
|
||||||
#[packet(with = "var_int")]
|
#[data_type(with = "var_int")]
|
||||||
pub primary_mask: i32,
|
pub primary_mask: i32,
|
||||||
pub heights: CompoundTag,
|
pub heights: CompoundTag,
|
||||||
pub data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
@ -236,7 +239,7 @@ impl ChunkData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct GameDisconnect {
|
pub struct GameDisconnect {
|
||||||
pub reason: Message,
|
pub reason: Message,
|
||||||
}
|
}
|
||||||
@ -249,17 +252,114 @@ 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(Encoder, Decoder, Debug, PartialEq)]
|
||||||
|
pub enum BossBarColor {
|
||||||
|
Pink,
|
||||||
|
Blue,
|
||||||
|
Red,
|
||||||
|
Green,
|
||||||
|
Yellow,
|
||||||
|
Purple,
|
||||||
|
White,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug, PartialEq)]
|
||||||
|
pub enum BossBarDivision {
|
||||||
|
None,
|
||||||
|
Notches6,
|
||||||
|
Notches10,
|
||||||
|
Notches12,
|
||||||
|
Notches20,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BossBar {
|
||||||
|
pub fn new(id: Uuid, action: BossBarAction) -> GameClientBoundPacket {
|
||||||
|
let boss_bar = BossBar { id, action };
|
||||||
|
|
||||||
|
GameClientBoundPacket::BossBar(boss_bar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug, PartialEq)]
|
||||||
|
pub struct EntityAction {
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub entity_id: i32,
|
||||||
|
pub action_id: EntityActionId,
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub jump_boost: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug, PartialEq)]
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub enum EntityActionId {
|
||||||
|
StartSneaking,
|
||||||
|
StopSneaking,
|
||||||
|
LeaveBad,
|
||||||
|
StartSprinting,
|
||||||
|
StopSprinting,
|
||||||
|
StartJumpWithHorse,
|
||||||
|
StopJumpWithHorse,
|
||||||
|
OpenHorseInventory,
|
||||||
|
StartFlyingWithElytra,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug, PartialEq)]
|
||||||
|
pub struct ServerBoundAbilities {
|
||||||
|
#[data_type(bitfield)]
|
||||||
|
pub invulnerable: bool,
|
||||||
|
#[data_type(bitfield)]
|
||||||
|
pub allow_flying: bool,
|
||||||
|
#[data_type(bitfield)]
|
||||||
|
pub flying: bool,
|
||||||
|
#[data_type(bitfield)]
|
||||||
|
pub creative_mode: bool,
|
||||||
|
pub fly_speed: f32,
|
||||||
|
pub walk_speed: f32,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::chat::{Message, Payload};
|
use crate::data::chat::Payload;
|
||||||
use crate::game::{
|
use crate::decoder::Decoder;
|
||||||
ChunkData, ClientBoundChatMessage, ClientBoundKeepAlive, GameDisconnect, GameMode,
|
use crate::encoder::Encoder;
|
||||||
JoinGame, MessagePosition, ServerBoundChatMessage, ServerBoundKeepAlive,
|
use crate::encoder::EncoderWriteExt;
|
||||||
};
|
use crate::error::{DecodeError, EncodeError};
|
||||||
use crate::{DecodeError, Encoder, EncoderWriteExt, STRING_MAX_LENGTH};
|
use crate::version::v1_14_4::game::*;
|
||||||
use crate::{Decoder, EncodeError};
|
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() {
|
||||||
@ -272,14 +372,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/game/server_bound_chat_message.dat").to_vec()
|
include_bytes!("../../../test/packet/game/server_bound_chat_message.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_server_bound_chat_message_decode() {
|
fn test_server_bound_chat_message_decode() {
|
||||||
let mut cursor = Cursor::new(
|
let mut cursor = Cursor::new(
|
||||||
include_bytes!("../test/packet/game/server_bound_chat_message.dat").to_vec(),
|
include_bytes!("../../../test/packet/game/server_bound_chat_message.dat").to_vec(),
|
||||||
);
|
);
|
||||||
let chat_message = ServerBoundChatMessage::decode(&mut cursor).unwrap();
|
let chat_message = ServerBoundChatMessage::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
@ -342,14 +442,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/game/client_bound_chat_message.dat").to_vec()
|
include_bytes!("../../../test/packet/game/client_bound_chat_message.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_client_bound_chat_message_decode() {
|
fn test_client_bound_chat_message_decode() {
|
||||||
let mut cursor = Cursor::new(
|
let mut cursor = Cursor::new(
|
||||||
include_bytes!("../test/packet/game/client_bound_chat_message.dat").to_vec(),
|
include_bytes!("../../../test/packet/game/client_bound_chat_message.dat").to_vec(),
|
||||||
);
|
);
|
||||||
let chat_message = ClientBoundChatMessage::decode(&mut cursor).unwrap();
|
let chat_message = ClientBoundChatMessage::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
@ -370,14 +470,15 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/game/server_bound_keep_alive.dat").to_vec()
|
include_bytes!("../../../test/packet/game/server_bound_keep_alive.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_server_bound_keep_alive_decode() {
|
fn test_server_bound_keep_alive_decode() {
|
||||||
let mut cursor =
|
let mut cursor = Cursor::new(
|
||||||
Cursor::new(include_bytes!("../test/packet/game/server_bound_keep_alive.dat").to_vec());
|
include_bytes!("../../../test/packet/game/server_bound_keep_alive.dat").to_vec(),
|
||||||
|
);
|
||||||
let keep_alive = ServerBoundKeepAlive::decode(&mut cursor).unwrap();
|
let keep_alive = ServerBoundKeepAlive::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(keep_alive.id, 31122019);
|
assert_eq!(keep_alive.id, 31122019);
|
||||||
@ -392,14 +493,15 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/game/client_bound_keep_alive.dat").to_vec()
|
include_bytes!("../../../test/packet/game/client_bound_keep_alive.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_client_bound_keep_alive_decode() {
|
fn test_client_bound_keep_alive_decode() {
|
||||||
let mut cursor =
|
let mut cursor = Cursor::new(
|
||||||
Cursor::new(include_bytes!("../test/packet/game/client_bound_keep_alive.dat").to_vec());
|
include_bytes!("../../../test/packet/game/client_bound_keep_alive.dat").to_vec(),
|
||||||
|
);
|
||||||
let keep_alive = ClientBoundKeepAlive::decode(&mut cursor).unwrap();
|
let keep_alive = ClientBoundKeepAlive::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(keep_alive.id, 240714);
|
assert_eq!(keep_alive.id, 240714);
|
||||||
@ -409,7 +511,7 @@ mod tests {
|
|||||||
fn test_join_game_encode() {
|
fn test_join_game_encode() {
|
||||||
let join_game = JoinGame {
|
let join_game = JoinGame {
|
||||||
entity_id: 27,
|
entity_id: 27,
|
||||||
game_mode: GameMode::Spectator,
|
game_mode: GameMode::Hardcore,
|
||||||
dimension: 23,
|
dimension: 23,
|
||||||
max_players: 100,
|
max_players: 100,
|
||||||
level_type: String::from("default"),
|
level_type: String::from("default"),
|
||||||
@ -422,17 +524,18 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/game/join_game.dat").to_vec()
|
include_bytes!("../../../test/packet/game/join_game.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_join_game_decode() {
|
fn test_join_game_decode() {
|
||||||
let mut cursor = Cursor::new(include_bytes!("../test/packet/game/join_game.dat").to_vec());
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/game/join_game.dat").to_vec());
|
||||||
let join_game = JoinGame::decode(&mut cursor).unwrap();
|
let join_game = JoinGame::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(join_game.entity_id, 27);
|
assert_eq!(join_game.entity_id, 27);
|
||||||
assert_eq!(join_game.game_mode, GameMode::Spectator);
|
assert_eq!(join_game.game_mode, GameMode::Hardcore);
|
||||||
assert_eq!(join_game.dimension, 23);
|
assert_eq!(join_game.dimension, 23);
|
||||||
assert_eq!(join_game.max_players, 100);
|
assert_eq!(join_game.max_players, 100);
|
||||||
assert_eq!(join_game.level_type, String::from("default"));
|
assert_eq!(join_game.level_type, String::from("default"));
|
||||||
@ -457,13 +560,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/game/chunk_data.dat").to_vec()
|
include_bytes!("../../../test/packet/game/chunk_data.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_chunk_data_decode() {
|
fn test_chunk_data_decode() {
|
||||||
let mut cursor = Cursor::new(include_bytes!("../test/packet/game/chunk_data.dat").to_vec());
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/game/chunk_data.dat").to_vec());
|
||||||
let chunk_data = ChunkData::decode(&mut cursor).unwrap();
|
let chunk_data = ChunkData::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(chunk_data.x, -2);
|
assert_eq!(chunk_data.x, -2);
|
||||||
@ -486,14 +590,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/game/game_disconnect.dat").to_vec()
|
include_bytes!("../../../test/packet/game/game_disconnect.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_game_disconnect_decode() {
|
fn test_game_disconnect_decode() {
|
||||||
let mut cursor =
|
let mut cursor =
|
||||||
Cursor::new(include_bytes!("../test/packet/game/game_disconnect.dat").to_vec());
|
Cursor::new(include_bytes!("../../../test/packet/game/game_disconnect.dat").to_vec());
|
||||||
let game_disconnect = GameDisconnect::decode(&mut cursor).unwrap();
|
let game_disconnect = GameDisconnect::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -501,4 +605,130 @@ 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_entity_action_encode() {
|
||||||
|
let entity_action = EntityAction {
|
||||||
|
entity_id: 12345,
|
||||||
|
action_id: EntityActionId::StartFlyingWithElytra,
|
||||||
|
jump_boost: i32::MAX,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
entity_action.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/entity_action.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_entity_action_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/game/entity_action.dat").to_vec());
|
||||||
|
let entity_action = EntityAction::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
entity_action,
|
||||||
|
EntityAction {
|
||||||
|
entity_id: 12345,
|
||||||
|
action_id: EntityActionId::StartFlyingWithElytra,
|
||||||
|
jump_boost: i32::MAX,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_serverbound_abilities_encode() {
|
||||||
|
let abilities = ServerBoundAbilities {
|
||||||
|
invulnerable: true,
|
||||||
|
flying: true,
|
||||||
|
allow_flying: false,
|
||||||
|
creative_mode: true,
|
||||||
|
fly_speed: 0.0,
|
||||||
|
walk_speed: 0.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
abilities.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(vec, [13, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_serverbound_abilities_decode() {
|
||||||
|
let vec = [13, 0, 0, 0, 0, 0, 0, 0, 0].to_vec();
|
||||||
|
let mut cursor = Cursor::new(vec);
|
||||||
|
|
||||||
|
let abilities = ServerBoundAbilities::decode(&mut cursor).unwrap();
|
||||||
|
assert!(abilities.invulnerable);
|
||||||
|
assert!(!abilities.allow_flying);
|
||||||
|
assert!(abilities.flying);
|
||||||
|
assert!(abilities.creative_mode);
|
||||||
|
}
|
||||||
}
|
}
|
55
protocol/src/version/v1_14_4/handshake.rs
Normal file
55
protocol/src/version/v1_14_4/handshake.rs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
use crate::decoder::Decoder;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use minecraft_protocol_derive::{Decoder, Encoder};
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
pub enum HandshakeServerBoundPacket {
|
||||||
|
Handshake(Handshake),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HandshakeServerBoundPacket {
|
||||||
|
pub fn get_type_id(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
HandshakeServerBoundPacket::Handshake(_) => 0x00,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
|
||||||
|
match type_id {
|
||||||
|
0x00 => {
|
||||||
|
let handshake = Handshake::decode(reader)?;
|
||||||
|
Ok(HandshakeServerBoundPacket::Handshake(handshake))
|
||||||
|
}
|
||||||
|
_ => Err(DecodeError::UnknownPacketType { type_id }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct Handshake {
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub protocol_version: i32,
|
||||||
|
#[data_type(max_length = 255)]
|
||||||
|
pub server_addr: String,
|
||||||
|
pub server_port: u16,
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub next_state: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Handshake {
|
||||||
|
pub fn new(
|
||||||
|
protocol_version: i32,
|
||||||
|
server_addr: String,
|
||||||
|
server_port: u16,
|
||||||
|
next_state: i32,
|
||||||
|
) -> HandshakeServerBoundPacket {
|
||||||
|
let handshake = Handshake {
|
||||||
|
protocol_version,
|
||||||
|
server_addr,
|
||||||
|
server_port,
|
||||||
|
next_state,
|
||||||
|
};
|
||||||
|
|
||||||
|
HandshakeServerBoundPacket::Handshake(handshake)
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,10 @@
|
|||||||
use crate::chat::Message;
|
|
||||||
use crate::DecodeError;
|
|
||||||
use crate::Decoder;
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use minecraft_protocol_derive::Packet;
|
use crate::data::chat::Message;
|
||||||
|
use crate::decoder::Decoder;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use minecraft_protocol_derive::{Decoder, Encoder};
|
||||||
|
|
||||||
pub enum LoginServerBoundPacket {
|
pub enum LoginServerBoundPacket {
|
||||||
LoginStart(LoginStart),
|
LoginStart(LoginStart),
|
||||||
@ -102,7 +102,7 @@ impl LoginClientBoundPacket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct LoginStart {
|
pub struct LoginStart {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
@ -115,7 +115,7 @@ impl LoginStart {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct EncryptionResponse {
|
pub struct EncryptionResponse {
|
||||||
pub shared_secret: Vec<u8>,
|
pub shared_secret: Vec<u8>,
|
||||||
pub verify_token: Vec<u8>,
|
pub verify_token: Vec<u8>,
|
||||||
@ -132,12 +132,12 @@ impl EncryptionResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct LoginPluginResponse {
|
pub struct LoginPluginResponse {
|
||||||
#[packet(with = "var_int")]
|
#[data_type(with = "var_int")]
|
||||||
pub message_id: i32,
|
pub message_id: i32,
|
||||||
pub successful: bool,
|
pub successful: bool,
|
||||||
#[packet(with = "rest")]
|
#[data_type(with = "rest")]
|
||||||
pub data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,7 +153,7 @@ impl LoginPluginResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct LoginDisconnect {
|
pub struct LoginDisconnect {
|
||||||
pub reason: Message,
|
pub reason: Message,
|
||||||
}
|
}
|
||||||
@ -166,9 +166,9 @@ impl LoginDisconnect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct EncryptionRequest {
|
pub struct EncryptionRequest {
|
||||||
#[packet(max_length = 20)]
|
#[data_type(max_length = 20)]
|
||||||
pub server_id: String,
|
pub server_id: String,
|
||||||
pub public_key: Vec<u8>,
|
pub public_key: Vec<u8>,
|
||||||
pub verify_token: Vec<u8>,
|
pub verify_token: Vec<u8>,
|
||||||
@ -190,11 +190,11 @@ impl EncryptionRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct LoginSuccess {
|
pub struct LoginSuccess {
|
||||||
#[packet(with = "uuid_hyp_str")]
|
#[data_type(with = "uuid_hyp_str")]
|
||||||
pub uuid: Uuid,
|
pub uuid: Uuid,
|
||||||
#[packet(max_length = 16)]
|
#[data_type(max_length = 16)]
|
||||||
pub username: String,
|
pub username: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,9 +206,9 @@ impl LoginSuccess {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct SetCompression {
|
pub struct SetCompression {
|
||||||
#[packet(with = "var_int")]
|
#[data_type(with = "var_int")]
|
||||||
pub threshold: i32,
|
pub threshold: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,12 +220,12 @@ impl SetCompression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct LoginPluginRequest {
|
pub struct LoginPluginRequest {
|
||||||
#[packet(with = "var_int")]
|
#[data_type(with = "var_int")]
|
||||||
pub message_id: i32,
|
pub message_id: i32,
|
||||||
pub channel: String,
|
pub channel: String,
|
||||||
#[packet(with = "rest")]
|
#[data_type(with = "rest")]
|
||||||
pub data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,12 +243,10 @@ impl LoginPluginRequest {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::chat::{Message, Payload};
|
use crate::data::chat::Payload;
|
||||||
use crate::login::{EncryptionRequest, LoginDisconnect, LoginPluginRequest, SetCompression};
|
use crate::decoder::Decoder;
|
||||||
use crate::login::{EncryptionResponse, LoginPluginResponse};
|
use crate::encoder::Encoder;
|
||||||
use crate::login::{LoginStart, LoginSuccess};
|
use crate::version::v1_14_4::login::*;
|
||||||
use crate::Decoder;
|
|
||||||
use crate::Encoder;
|
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -263,14 +261,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/login/login_start.dat").to_vec()
|
include_bytes!("../../../test/packet/login/login_start.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_login_start_packet_decode() {
|
fn test_login_start_packet_decode() {
|
||||||
let mut cursor =
|
let mut cursor =
|
||||||
Cursor::new(include_bytes!("../test/packet/login/login_start.dat").to_vec());
|
Cursor::new(include_bytes!("../../../test/packet/login/login_start.dat").to_vec());
|
||||||
let login_start = LoginStart::decode(&mut cursor).unwrap();
|
let login_start = LoginStart::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(login_start.name, String::from("Username"));
|
assert_eq!(login_start.name, String::from("Username"));
|
||||||
@ -288,14 +286,15 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/login/encryption_response.dat").to_vec()
|
include_bytes!("../../../test/packet/login/encryption_response.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_encryption_response_decode() {
|
fn test_encryption_response_decode() {
|
||||||
let mut cursor =
|
let mut cursor = Cursor::new(
|
||||||
Cursor::new(include_bytes!("../test/packet/login/encryption_response.dat").to_vec());
|
include_bytes!("../../../test/packet/login/encryption_response.dat").to_vec(),
|
||||||
|
);
|
||||||
let encryption_response = EncryptionResponse::decode(&mut cursor).unwrap();
|
let encryption_response = EncryptionResponse::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -318,14 +317,15 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/login/login_plugin_response.dat").to_vec()
|
include_bytes!("../../../test/packet/login/login_plugin_response.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_login_plugin_response_decode() {
|
fn test_login_plugin_response_decode() {
|
||||||
let mut cursor =
|
let mut cursor = Cursor::new(
|
||||||
Cursor::new(include_bytes!("../test/packet/login/login_plugin_response.dat").to_vec());
|
include_bytes!("../../../test/packet/login/login_plugin_response.dat").to_vec(),
|
||||||
|
);
|
||||||
let login_plugin_response = LoginPluginResponse::decode(&mut cursor).unwrap();
|
let login_plugin_response = LoginPluginResponse::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(login_plugin_response.message_id, 55);
|
assert_eq!(login_plugin_response.message_id, 55);
|
||||||
@ -347,14 +347,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/login/login_disconnect.dat").to_vec()
|
include_bytes!("../../../test/packet/login/login_disconnect.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_login_disconnect_decode() {
|
fn test_login_disconnect_decode() {
|
||||||
let mut cursor =
|
let mut cursor =
|
||||||
Cursor::new(include_bytes!("../test/packet/login/login_disconnect.dat").to_vec());
|
Cursor::new(include_bytes!("../../../test/packet/login/login_disconnect.dat").to_vec());
|
||||||
let login_disconnect = LoginDisconnect::decode(&mut cursor).unwrap();
|
let login_disconnect = LoginDisconnect::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -376,14 +376,15 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/login/encryption_request.dat").to_vec()
|
include_bytes!("../../../test/packet/login/encryption_request.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_encryption_request_decode() {
|
fn test_encryption_request_decode() {
|
||||||
let mut cursor =
|
let mut cursor = Cursor::new(
|
||||||
Cursor::new(include_bytes!("../test/packet/login/encryption_request.dat").to_vec());
|
include_bytes!("../../../test/packet/login/encryption_request.dat").to_vec(),
|
||||||
|
);
|
||||||
let encryption_request = EncryptionRequest::decode(&mut cursor).unwrap();
|
let encryption_request = EncryptionRequest::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(encryption_request.server_id, String::from("ServerID"));
|
assert_eq!(encryption_request.server_id, String::from("ServerID"));
|
||||||
@ -406,14 +407,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/login/login_success.dat").to_vec()
|
include_bytes!("../../../test/packet/login/login_success.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_login_success_decode() {
|
fn test_login_success_decode() {
|
||||||
let mut cursor =
|
let mut cursor =
|
||||||
Cursor::new(include_bytes!("../test/packet/login/login_success.dat").to_vec());
|
Cursor::new(include_bytes!("../../../test/packet/login/login_success.dat").to_vec());
|
||||||
let login_success = LoginSuccess::decode(&mut cursor).unwrap();
|
let login_success = LoginSuccess::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(login_success.username, String::from("Username"));
|
assert_eq!(login_success.username, String::from("Username"));
|
||||||
@ -433,14 +434,15 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/login/login_set_compression.dat").to_vec()
|
include_bytes!("../../../test/packet/login/login_set_compression.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_set_compression_decode() {
|
fn test_set_compression_decode() {
|
||||||
let mut cursor =
|
let mut cursor = Cursor::new(
|
||||||
Cursor::new(include_bytes!("../test/packet/login/login_set_compression.dat").to_vec());
|
include_bytes!("../../../test/packet/login/login_set_compression.dat").to_vec(),
|
||||||
|
);
|
||||||
let set_compression = SetCompression::decode(&mut cursor).unwrap();
|
let set_compression = SetCompression::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(set_compression.threshold, 1);
|
assert_eq!(set_compression.threshold, 1);
|
||||||
@ -459,14 +461,15 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/login/login_plugin_request.dat").to_vec()
|
include_bytes!("../../../test/packet/login/login_plugin_request.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_login_plugin_request_decode() {
|
fn test_login_plugin_request_decode() {
|
||||||
let mut cursor =
|
let mut cursor = Cursor::new(
|
||||||
Cursor::new(include_bytes!("../test/packet/login/login_plugin_request.dat").to_vec());
|
include_bytes!("../../../test/packet/login/login_plugin_request.dat").to_vec(),
|
||||||
|
);
|
||||||
let login_plugin_request = LoginPluginRequest::decode(&mut cursor).unwrap();
|
let login_plugin_request = LoginPluginRequest::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(login_plugin_request.message_id, 55);
|
assert_eq!(login_plugin_request.message_id, 55);
|
4
protocol/src/version/v1_14_4/mod.rs
Normal file
4
protocol/src/version/v1_14_4/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
pub mod game;
|
||||||
|
pub mod handshake;
|
||||||
|
pub mod login;
|
||||||
|
pub mod status;
|
@ -1,11 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use crate::data::server_status::*;
|
||||||
use uuid::Uuid;
|
use crate::decoder::Decoder;
|
||||||
|
use crate::error::DecodeError;
|
||||||
use crate::chat::Message;
|
use minecraft_protocol_derive::{Decoder, Encoder};
|
||||||
use crate::impl_json_encoder_decoder;
|
|
||||||
use crate::DecodeError;
|
|
||||||
use crate::Decoder;
|
|
||||||
use minecraft_protocol_derive::Packet;
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
pub enum StatusServerBoundPacket {
|
pub enum StatusServerBoundPacket {
|
||||||
@ -48,7 +44,7 @@ impl StatusClientBoundPacket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct PingRequest {
|
pub struct PingRequest {
|
||||||
pub time: u64,
|
pub time: u64,
|
||||||
}
|
}
|
||||||
@ -61,7 +57,7 @@ impl PingRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct PingResponse {
|
pub struct PingResponse {
|
||||||
pub time: u64,
|
pub time: u64,
|
||||||
}
|
}
|
||||||
@ -74,39 +70,11 @@ impl PingResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
pub struct ServerStatus {
|
|
||||||
pub version: ServerVersion,
|
|
||||||
pub players: OnlinePlayers,
|
|
||||||
pub description: Message,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct ServerVersion {
|
|
||||||
pub name: String,
|
|
||||||
pub protocol: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct OnlinePlayers {
|
|
||||||
pub max: u32,
|
|
||||||
pub online: u32,
|
|
||||||
pub sample: Vec<OnlinePlayer>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
|
||||||
pub struct OnlinePlayer {
|
|
||||||
pub name: String,
|
|
||||||
pub id: Uuid,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
|
||||||
pub struct StatusResponse {
|
pub struct StatusResponse {
|
||||||
pub server_status: ServerStatus,
|
pub server_status: ServerStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_json_encoder_decoder!(ServerStatus);
|
|
||||||
|
|
||||||
impl StatusResponse {
|
impl StatusResponse {
|
||||||
pub fn new(server_status: ServerStatus) -> StatusClientBoundPacket {
|
pub fn new(server_status: ServerStatus) -> StatusClientBoundPacket {
|
||||||
let status_response = StatusResponse { server_status };
|
let status_response = StatusResponse { server_status };
|
||||||
@ -117,13 +85,10 @@ impl StatusResponse {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::chat::{Message, Payload};
|
use crate::data::chat::{Message, Payload};
|
||||||
use crate::status::{
|
use crate::decoder::Decoder;
|
||||||
OnlinePlayer, OnlinePlayers, PingRequest, PingResponse, ServerStatus, ServerVersion,
|
use crate::encoder::Encoder;
|
||||||
StatusResponse,
|
use crate::version::v1_14_4::status::*;
|
||||||
};
|
|
||||||
use crate::Decoder;
|
|
||||||
use crate::Encoder;
|
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -138,14 +103,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/status/ping_request.dat").to_vec()
|
include_bytes!("../../../test/packet/status/ping_request.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_status_ping_request_decode() {
|
fn test_status_ping_request_decode() {
|
||||||
let mut cursor =
|
let mut cursor =
|
||||||
Cursor::new(include_bytes!("../test/packet/status/ping_request.dat").to_vec());
|
Cursor::new(include_bytes!("../../../test/packet/status/ping_request.dat").to_vec());
|
||||||
let ping_request = PingRequest::decode(&mut cursor).unwrap();
|
let ping_request = PingRequest::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(ping_request.time, 1577735845610);
|
assert_eq!(ping_request.time, 1577735845610);
|
||||||
@ -162,14 +127,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/status/ping_response.dat").to_vec()
|
include_bytes!("../../../test/packet/status/ping_response.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_status_ping_response_decode() {
|
fn test_status_ping_response_decode() {
|
||||||
let mut cursor =
|
let mut cursor =
|
||||||
Cursor::new(include_bytes!("../test/packet/status/ping_response.dat").to_vec());
|
Cursor::new(include_bytes!("../../../test/packet/status/ping_response.dat").to_vec());
|
||||||
let ping_response = PingResponse::decode(&mut cursor).unwrap();
|
let ping_response = PingResponse::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
assert_eq!(ping_response.time, 1577735845610);
|
assert_eq!(ping_response.time, 1577735845610);
|
||||||
@ -206,14 +171,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
vec,
|
vec,
|
||||||
include_bytes!("../test/packet/status/status_response.dat").to_vec()
|
include_bytes!("../../../test/packet/status/status_response.dat").to_vec()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_status_response_decode() {
|
fn test_status_response_decode() {
|
||||||
let mut cursor =
|
let mut cursor =
|
||||||
Cursor::new(include_bytes!("../test/packet/status/status_response.dat").to_vec());
|
Cursor::new(include_bytes!("../../../test/packet/status/status_response.dat").to_vec());
|
||||||
let status_response = StatusResponse::decode(&mut cursor).unwrap();
|
let status_response = StatusResponse::decode(&mut cursor).unwrap();
|
||||||
let server_status = status_response.server_status;
|
let server_status = status_response.server_status;
|
||||||
|
|
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>ܢ
|
1
protocol/test/packet/game/entity_action.dat
Normal file
1
protocol/test/packet/game/entity_action.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
筦<08><><EFBFBD><EFBFBD>
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user