Compare commits
14 Commits
protodef-g
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
4a4f2e2a1c | ||
|
31041b8fe2 | ||
|
f19f598abe | ||
|
6a069edaff | ||
|
09f95625ef | ||
|
05cf9c6e6b | ||
|
024786e618 | ||
|
22bdb26a9c | ||
|
70bfd01848 | ||
|
f733fabedc | ||
|
b70968bd84 | ||
|
7b6b7e4921 | ||
|
b4a6f81e11 | ||
|
41c185fcb1 |
@ -3,5 +3,4 @@
|
|||||||
members = [
|
members = [
|
||||||
"protocol",
|
"protocol",
|
||||||
"protocol-derive",
|
"protocol-derive",
|
||||||
"protocol-generator",
|
|
||||||
]
|
]
|
@ -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
|
||||||
|
@ -16,7 +16,3 @@ proc-macro = true
|
|||||||
proc-macro2 = "1.0"
|
proc-macro2 = "1.0"
|
||||||
syn = "1.0"
|
syn = "1.0"
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
minecraft-protocol = { version = "0.1.0", path = "../protocol" }
|
|
||||||
byteorder = "1"
|
|
||||||
|
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,390 +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::TokenStream as TokenStream2;
|
use crate::render::decoder::{render_enum_decoder, render_struct_decoder};
|
||||||
use proc_macro2::{Ident, Span};
|
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::{parse_macro_input, Data, DeriveInput, Field, Fields, Lit, Meta, NestedMeta, Type};
|
use syn::DeriveInput;
|
||||||
|
|
||||||
#[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 {
|
||||||
println!("{}", decoder);
|
name,
|
||||||
|
discriminant_type,
|
||||||
TokenStream1::from(quote! {
|
variants,
|
||||||
#encoder
|
} => render_enum_encoder(name, &discriminant_type, &variants),
|
||||||
|
|
||||||
#decoder
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
_ => 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 mut bitfields = vec![];
|
|
||||||
let total_fields = fields.iter().count();
|
|
||||||
let mut current = 0;
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
let mut result = quote!();
|
|
||||||
|
|
||||||
// When we encounter a bitfield, we skip writing until we get
|
|
||||||
// to the last bit field or the last field.
|
|
||||||
match &parsed_meta.bitfield {
|
|
||||||
Some(bitfield) => {
|
|
||||||
bitfields.push((field.clone(), bitfield.clone()));
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
result.append_all(quote_decoder_bitfield(&bitfields));
|
|
||||||
bitfields.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
current += 1;
|
|
||||||
|
|
||||||
if !bitfields.is_empty() {
|
|
||||||
return if current == total_fields {
|
|
||||||
result.append_all(quote_decoder_bitfield(&bitfields));
|
|
||||||
bitfields.clear();
|
|
||||||
result
|
|
||||||
} else {
|
|
||||||
quote!()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is special case because max length are used only for strings.
|
|
||||||
if let Some(max_length) = parsed_meta.max_length {
|
|
||||||
result.append_all(quote! {
|
|
||||||
let #name = crate::DecoderReadExt::read_string(reader, #max_length)?;
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
match &parsed_meta.module {
|
|
||||||
Some(module) => {
|
|
||||||
let module_ident = Ident::new(module, Span::call_site());
|
|
||||||
|
|
||||||
result.append_all(quote! {
|
|
||||||
let #name = crate::#module_ident::decode(reader)?;
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
None => {
|
|
||||||
result.append_all(quote! {
|
|
||||||
let #name = <#ty as crate::Decoder>::decode(reader)?;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
});
|
|
||||||
|
|
||||||
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>,
|
|
||||||
bitfield: Option<Bitfield>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
struct Bitfield {
|
|
||||||
size: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! decode_bitmask_value (
|
|
||||||
($value: expr, $name: ident) => (
|
|
||||||
fn $name(
|
|
||||||
result: &mut TokenStream2,
|
|
||||||
name: &Option<Ident>,
|
|
||||||
ty: &Type,
|
|
||||||
bitfield_size: usize,
|
|
||||||
current: usize,
|
|
||||||
signed: bool,
|
|
||||||
) {
|
|
||||||
let mask = (2 << (bitfield_size - 1)) - $value;
|
|
||||||
let bool = match ty {
|
|
||||||
Type::Path(type_path) => type_path
|
|
||||||
.path
|
|
||||||
.get_ident()
|
|
||||||
.expect("Failed to get ident")
|
|
||||||
.to_string() == "bool",
|
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
|
|
||||||
if bool {
|
|
||||||
if current > 0 {
|
|
||||||
result.append_all(quote! {
|
|
||||||
let mut #name = ((encoded >> #current) & #mask) != 0;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
result.append_all(quote! {
|
|
||||||
let mut #name = (encoded & #mask) != 0;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if current > 0 {
|
|
||||||
result.append_all(quote! {
|
|
||||||
let mut #name = ((encoded >> #current) & #mask) as #ty;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
result.append_all(quote! {
|
|
||||||
let mut #name = (encoded & #mask) as #ty;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if signed {
|
|
||||||
let subtract = mask + 1;
|
|
||||||
let overflow = subtract >> 1;
|
|
||||||
|
|
||||||
result.append_all(quote! {
|
|
||||||
if #name > #overflow as #ty {
|
|
||||||
#name -= #subtract as #ty;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
decode_bitmask_value!(1i64, decode_i64_bitmask_value);
|
|
||||||
decode_bitmask_value!(1i32, decode_i32_bitmask_value);
|
|
||||||
decode_bitmask_value!(1i16, decode_i16_bitmask_value);
|
|
||||||
decode_bitmask_value!(1i8, decode_i8_bitmask_value);
|
|
||||||
|
|
||||||
fn quote_decoder_bitfield(bitfields: &Vec<(Field, Bitfield)>) -> TokenStream2 {
|
|
||||||
if bitfields.is_empty() {
|
|
||||||
return quote!();
|
|
||||||
}
|
|
||||||
|
|
||||||
let total_size: usize = bitfields.iter().map(|(_, b)| b.size).sum();
|
|
||||||
|
|
||||||
let mut result = read_decoder_bitfield_quote(total_size);
|
|
||||||
let mut current = total_size;
|
|
||||||
|
|
||||||
for (field, bitfield) in bitfields {
|
|
||||||
current -= bitfield.size;
|
|
||||||
|
|
||||||
let name = &field.ident;
|
|
||||||
let ty = &field.ty;
|
|
||||||
|
|
||||||
let signed = match ty {
|
|
||||||
Type::Path(type_path) => type_path
|
|
||||||
.path
|
|
||||||
.get_ident()
|
|
||||||
.expect("Failed to get ident")
|
|
||||||
.to_string()
|
|
||||||
.starts_with("i"),
|
|
||||||
_ => panic!("Unexpected bitfield type"),
|
|
||||||
};
|
|
||||||
|
|
||||||
match total_size {
|
|
||||||
_ if total_size == 64 => {
|
|
||||||
decode_i64_bitmask_value(&mut result, name, ty, bitfield.size, current, signed)
|
|
||||||
}
|
|
||||||
_ if total_size == 32 => {
|
|
||||||
decode_i32_bitmask_value(&mut result, name, ty, bitfield.size, current, signed)
|
|
||||||
}
|
|
||||||
_ if total_size == 16 => {
|
|
||||||
decode_i16_bitmask_value(&mut result, name, ty, bitfield.size, current, signed)
|
|
||||||
}
|
|
||||||
_ => decode_i8_bitmask_value(&mut result, name, ty, bitfield.size, current, signed),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_decoder_bitfield_quote(total_size: usize) -> TokenStream2 {
|
|
||||||
match total_size {
|
|
||||||
_ if total_size == 64 => {
|
|
||||||
quote! {
|
|
||||||
let encoded = ::byteorder::ReadBytesExt::read_i64::<::byteorder::BigEndian>(reader)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ if total_size == 32 => {
|
|
||||||
quote! {
|
|
||||||
let encoded = ::byteorder::ReadBytesExt::read_i32::<::byteorder::BigEndian>(reader)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ if total_size == 16 => {
|
|
||||||
quote! {
|
|
||||||
let encoded = ::byteorder::ReadBytesExt::read_i16::<::byteorder::BigEndian>(reader)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ if total_size == 8 => {
|
|
||||||
quote! {
|
|
||||||
let encoded = ::byteorder::ReadBytesExt::read_i8(reader)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => panic!("Bitfield size must be aligned to 8, 16, 32 or 64"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_packet_field_meta(meta_list: &Vec<NestedMeta>) -> PacketFieldMeta {
|
|
||||||
let mut module = None;
|
|
||||||
let mut max_length = None;
|
|
||||||
let mut bitfield = 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().expect("Failed to get ident")
|
|
||||||
),
|
|
||||||
},
|
|
||||||
NestedMeta::Meta(Meta::List(meta_list)) if meta_list.path.is_ident("bitfield") => {
|
|
||||||
let mut size = None;
|
|
||||||
|
|
||||||
for meta in &meta_list.nested {
|
|
||||||
match meta {
|
|
||||||
NestedMeta::Meta(Meta::NameValue(named_meta)) => match &named_meta.path {
|
|
||||||
path if path.is_ident("size") => match &named_meta.lit {
|
|
||||||
Lit::Int(lit_int) => {
|
|
||||||
size = Some(
|
|
||||||
lit_int
|
|
||||||
.base10_parse::<usize>()
|
|
||||||
.expect("Failed to parse size attribute"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
_ => panic!("\"size\" attribute value must be integer"),
|
|
||||||
},
|
|
||||||
path => panic!(
|
|
||||||
"Received unrecognized attribute : \"{}\"",
|
|
||||||
path.get_ident().expect("Failed to get ident")
|
|
||||||
),
|
|
||||||
},
|
|
||||||
_ => panic!("Unexpected field meta"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let size = size.unwrap_or_else(|| panic!("Size must be specified in bitfield"));
|
|
||||||
bitfield = Some(Bitfield { size })
|
|
||||||
}
|
|
||||||
_ => panic!("Unexpected field meta"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
PacketFieldMeta {
|
|
||||||
module,
|
|
||||||
max_length,
|
|
||||||
bitfield,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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: FnMut(&Field) -> TokenStream2>(fields: &Fields, mut 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;
|
@ -1,60 +0,0 @@
|
|||||||
#[macro_use]
|
|
||||||
extern crate minecraft_protocol_derive;
|
|
||||||
|
|
||||||
use minecraft_protocol::decoder::Decoder;
|
|
||||||
use minecraft_protocol::encoder::Encoder;
|
|
||||||
use minecraft_protocol::error::{DecodeError, EncodeError};
|
|
||||||
|
|
||||||
#[derive(Packet)]
|
|
||||||
pub struct Abilities {
|
|
||||||
#[packet(bitfield(size = 4))]
|
|
||||||
pub _unused: u8,
|
|
||||||
#[packet(bitfield(size = 1))]
|
|
||||||
pub creative_mode: bool,
|
|
||||||
#[packet(bitfield(size = 1))]
|
|
||||||
pub allow_flying: bool,
|
|
||||||
#[packet(bitfield(size = 1))]
|
|
||||||
pub flying: bool,
|
|
||||||
#[packet(bitfield(size = 1))]
|
|
||||||
pub invulnerable: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::Abilities;
|
|
||||||
use minecraft_protocol::decoder::Decoder;
|
|
||||||
use minecraft_protocol::encoder::Encoder;
|
|
||||||
use minecraft_protocol::error::{DecodeError, EncodeError};
|
|
||||||
use std::io::Cursor;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_encode_abilities_i8_bitfield() {
|
|
||||||
let abilities = Abilities {
|
|
||||||
_unused: 0,
|
|
||||||
creative_mode: true,
|
|
||||||
allow_flying: true,
|
|
||||||
flying: true,
|
|
||||||
invulnerable: true,
|
|
||||||
};
|
|
||||||
let mut vec = Vec::new();
|
|
||||||
|
|
||||||
abilities
|
|
||||||
.encode(&mut vec)
|
|
||||||
.expect("Failed to encode abilities");
|
|
||||||
assert_eq!(vec, [15]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_decode_abilities_i8_bitfield() {
|
|
||||||
let value = 15i8;
|
|
||||||
|
|
||||||
let vec = value.to_be_bytes().to_vec();
|
|
||||||
let mut cursor = Cursor::new(vec);
|
|
||||||
|
|
||||||
let abilities = Abilities::decode(&mut cursor).expect("Failed to decode abilities");
|
|
||||||
assert!(abilities.invulnerable);
|
|
||||||
assert!(abilities.flying);
|
|
||||||
assert!(abilities.allow_flying);
|
|
||||||
assert!(abilities.creative_mode);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,107 +0,0 @@
|
|||||||
#[macro_use]
|
|
||||||
extern crate minecraft_protocol_derive;
|
|
||||||
|
|
||||||
use minecraft_protocol::decoder::Decoder;
|
|
||||||
use minecraft_protocol::encoder::Encoder;
|
|
||||||
use minecraft_protocol::error::{DecodeError, EncodeError};
|
|
||||||
|
|
||||||
#[derive(Packet)]
|
|
||||||
pub struct HalfLong {
|
|
||||||
#[packet(bitfield(size = 32))]
|
|
||||||
pub _unused: u32,
|
|
||||||
#[packet(bitfield(size = 32))]
|
|
||||||
pub value: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Packet)]
|
|
||||||
pub struct HalfInt {
|
|
||||||
#[packet(bitfield(size = 16))]
|
|
||||||
pub _unused: u16,
|
|
||||||
#[packet(bitfield(size = 16))]
|
|
||||||
pub value: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Packet)]
|
|
||||||
pub struct HalfShort {
|
|
||||||
#[packet(bitfield(size = 8))]
|
|
||||||
pub _unused: u8,
|
|
||||||
#[packet(bitfield(size = 8))]
|
|
||||||
pub value: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::{HalfInt, HalfLong, HalfShort};
|
|
||||||
use minecraft_protocol::decoder::Decoder;
|
|
||||||
use minecraft_protocol::encoder::Encoder;
|
|
||||||
use minecraft_protocol::error::{DecodeError, EncodeError};
|
|
||||||
use std::io::Cursor;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_encode_half_long_u64_bitfield() {
|
|
||||||
let half = HalfLong {
|
|
||||||
_unused: 0,
|
|
||||||
value: u32::MAX,
|
|
||||||
};
|
|
||||||
let mut vec = Vec::new();
|
|
||||||
|
|
||||||
half.encode(&mut vec).expect("Failed to encode half");
|
|
||||||
assert_eq!(vec, u32::MAX.to_be_bytes().to_vec());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_decode_half_long_u64_bitfield() {
|
|
||||||
let value = u32::MAX as u64;
|
|
||||||
|
|
||||||
let vec = value.to_be_bytes().to_vec();
|
|
||||||
let mut cursor = Cursor::new(vec);
|
|
||||||
|
|
||||||
let half = HalfLong::decode(&mut cursor).expect("Failed to decode half");
|
|
||||||
assert_eq!(half.value, u32::MAX);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_encode_half_int_u32_bitfield() {
|
|
||||||
let half = HalfInt {
|
|
||||||
_unused: 0,
|
|
||||||
value: u16::MAX,
|
|
||||||
};
|
|
||||||
let mut vec = Vec::new();
|
|
||||||
|
|
||||||
half.encode(&mut vec).expect("Failed to encode half");
|
|
||||||
assert_eq!(vec, u16::MAX.to_be_bytes().to_vec());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_decode_half_int_u32_bitfield() {
|
|
||||||
let value = u16::MAX as u32;
|
|
||||||
|
|
||||||
let vec = value.to_be_bytes().to_vec();
|
|
||||||
let mut cursor = Cursor::new(vec);
|
|
||||||
|
|
||||||
let half = HalfInt::decode(&mut cursor).expect("Failed to decode half");
|
|
||||||
assert_eq!(half.value, u16::MAX);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_encode_half_short_u8_bitfield() {
|
|
||||||
let half = HalfShort {
|
|
||||||
_unused: 0,
|
|
||||||
value: u8::MAX,
|
|
||||||
};
|
|
||||||
let mut vec = Vec::new();
|
|
||||||
|
|
||||||
half.encode(&mut vec).expect("Failed to encode half");
|
|
||||||
assert_eq!(vec, u8::MAX.to_be_bytes().to_vec());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_decode_half_short_u8_bitfield() {
|
|
||||||
let value = u8::MAX as u16;
|
|
||||||
let vec = value.to_be_bytes().to_vec();
|
|
||||||
let mut cursor = Cursor::new(vec);
|
|
||||||
|
|
||||||
let half = HalfShort::decode(&mut cursor).expect("Failed to decode half");
|
|
||||||
assert_eq!(half.value, u8::MAX);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
#[macro_use]
|
|
||||||
extern crate minecraft_protocol_derive;
|
|
||||||
|
|
||||||
use minecraft_protocol::decoder::Decoder;
|
|
||||||
use minecraft_protocol::encoder::Encoder;
|
|
||||||
use minecraft_protocol::error::{DecodeError, EncodeError};
|
|
||||||
|
|
||||||
#[derive(Packet)]
|
|
||||||
pub struct Position {
|
|
||||||
#[packet(bitfield(size = 26))]
|
|
||||||
pub x: i32,
|
|
||||||
#[packet(bitfield(size = 26))]
|
|
||||||
pub z: i32,
|
|
||||||
#[packet(bitfield(size = 12))]
|
|
||||||
pub y: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::Position;
|
|
||||||
use minecraft_protocol::decoder::Decoder;
|
|
||||||
use minecraft_protocol::encoder::Encoder;
|
|
||||||
use minecraft_protocol::error::{DecodeError, EncodeError};
|
|
||||||
use std::io::Cursor;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_encode_position_i64_bitfield() {
|
|
||||||
let position = Position {
|
|
||||||
x: 1000,
|
|
||||||
y: 64,
|
|
||||||
z: -1000,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut vec = Vec::new();
|
|
||||||
|
|
||||||
position
|
|
||||||
.encode(&mut vec)
|
|
||||||
.expect("Failed to encode position");
|
|
||||||
|
|
||||||
assert_eq!(vec, 275152780755008i64.to_be_bytes().to_vec());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_decode_position_i64_bitfield() {
|
|
||||||
let value = -137164079660992i64;
|
|
||||||
let vec = value.to_be_bytes().to_vec();
|
|
||||||
let mut cursor = Cursor::new(vec);
|
|
||||||
let position = Position::decode(&mut cursor).expect("Failed to decode position");
|
|
||||||
|
|
||||||
assert_eq!(position.x, -500);
|
|
||||||
assert_eq!(position.y, 64);
|
|
||||||
assert_eq!(position.z, -1000);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "minecraft-protocol-generator"
|
|
||||||
version = "0.0.0"
|
|
||||||
authors = ["vagola <vladislavs.golubs@yandex.ru>"]
|
|
||||||
edition = "2018"
|
|
||||||
description = "Minecraft protocol generator"
|
|
||||||
license = "MIT"
|
|
||||||
homepage = "https://github.com/eihwaz/minecraft-protocol"
|
|
||||||
repository = "https://github.com/eihwaz/minecraft-protocol"
|
|
||||||
keywords = ["minecraft", "protocol", "packet", "io"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
serde = "1.0.120"
|
|
||||||
serde_json = "1.0"
|
|
||||||
handlebars = "3.5.2"
|
|
||||||
heck = "0.3.2"
|
|
||||||
protodef-parser = "0.1.0"
|
|
@ -1,12 +0,0 @@
|
|||||||
use handlebars::RenderError;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum FrontendError {
|
|
||||||
TemplateRenderError { render_error: RenderError },
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<RenderError> for FrontendError {
|
|
||||||
fn from(render_error: RenderError) -> Self {
|
|
||||||
FrontendError::TemplateRenderError { render_error }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,138 +0,0 @@
|
|||||||
use crate::error::FrontendError;
|
|
||||||
use handlebars::Handlebars;
|
|
||||||
use serde::Serialize;
|
|
||||||
use std::io::Write;
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Serialize)]
|
|
||||||
#[serde(tag = "type")]
|
|
||||||
pub enum DataType {
|
|
||||||
#[serde(rename(serialize = "bool"))]
|
|
||||||
Boolean,
|
|
||||||
#[serde(rename(serialize = "i8"))]
|
|
||||||
Byte,
|
|
||||||
#[serde(rename(serialize = "u8"))]
|
|
||||||
UnsignedByte,
|
|
||||||
#[serde(rename(serialize = "i16"))]
|
|
||||||
Short,
|
|
||||||
#[serde(rename(serialize = "u16"))]
|
|
||||||
UnsignedShort,
|
|
||||||
#[serde(rename(serialize = "i32"))]
|
|
||||||
Int {
|
|
||||||
var_int: bool,
|
|
||||||
},
|
|
||||||
#[serde(rename(serialize = "u32"))]
|
|
||||||
UnsignedInt,
|
|
||||||
#[serde(rename(serialize = "i64"))]
|
|
||||||
Long {
|
|
||||||
var_long: bool,
|
|
||||||
},
|
|
||||||
#[serde(rename(serialize = "u64"))]
|
|
||||||
UnsignedLong,
|
|
||||||
#[serde(rename(serialize = "f32"))]
|
|
||||||
Float,
|
|
||||||
#[serde(rename(serialize = "f64"))]
|
|
||||||
Double,
|
|
||||||
String {
|
|
||||||
max_length: u16,
|
|
||||||
},
|
|
||||||
Uuid {
|
|
||||||
hyphenated: bool,
|
|
||||||
},
|
|
||||||
#[serde(rename(serialize = "Vec<u8>"))]
|
|
||||||
ByteArray {
|
|
||||||
rest: bool,
|
|
||||||
},
|
|
||||||
CompoundTag,
|
|
||||||
RefType {
|
|
||||||
ref_name: String,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Serialize)]
|
|
||||||
pub struct PacketStruct {
|
|
||||||
pub name: String,
|
|
||||||
pub fields: Vec<Field>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PacketStruct {
|
|
||||||
pub fn new(name: impl ToString, fields: Vec<Field>) -> PacketStruct {
|
|
||||||
PacketStruct {
|
|
||||||
name: name.to_string(),
|
|
||||||
fields,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Serialize)]
|
|
||||||
pub struct Field {
|
|
||||||
pub name: String,
|
|
||||||
#[serde(flatten)]
|
|
||||||
pub data_type: DataType,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Field {
|
|
||||||
pub fn new(name: impl ToString, data_type: DataType) -> Field {
|
|
||||||
Field {
|
|
||||||
name: name.to_string(),
|
|
||||||
data_type,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_packet_struct<W: Write>(
|
|
||||||
template_engine: &Handlebars,
|
|
||||||
packet_struct: PacketStruct,
|
|
||||||
write: &mut W,
|
|
||||||
) -> Result<(), FrontendError> {
|
|
||||||
template_engine.render_to_write("packet_struct", &packet_struct, write)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::frontend::{write_packet_struct, DataType, Field, PacketStruct};
|
|
||||||
use crate::templates;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_write_packet_struct() {
|
|
||||||
let template_engine = templates::create_template_engine("templates");
|
|
||||||
|
|
||||||
let fields = vec![
|
|
||||||
Field::new("boolean", DataType::Boolean),
|
|
||||||
Field::new("byte", DataType::Byte),
|
|
||||||
Field::new("unsigned_byte", DataType::UnsignedByte),
|
|
||||||
Field::new("short", DataType::Short),
|
|
||||||
Field::new("unsigned_short", DataType::UnsignedShort),
|
|
||||||
Field::new("int", DataType::Int { var_int: false }),
|
|
||||||
Field::new("varint", DataType::Int { var_int: true }),
|
|
||||||
Field::new("unsigned_int", DataType::UnsignedInt),
|
|
||||||
Field::new("long", DataType::Long { var_long: false }),
|
|
||||||
Field::new("varlong", DataType::Long { var_long: true }),
|
|
||||||
Field::new("unsigned_long", DataType::UnsignedLong),
|
|
||||||
Field::new("float", DataType::Float),
|
|
||||||
Field::new("double", DataType::Double),
|
|
||||||
Field::new("string", DataType::String { max_length: 20 }),
|
|
||||||
Field::new("uuid", DataType::Uuid { hyphenated: false }),
|
|
||||||
Field::new("hyphenated", DataType::Uuid { hyphenated: true }),
|
|
||||||
Field::new("byte_array", DataType::ByteArray { rest: false }),
|
|
||||||
Field::new("rest", DataType::ByteArray { rest: true }),
|
|
||||||
Field::new("compound_tag", DataType::CompoundTag),
|
|
||||||
Field::new(
|
|
||||||
"ref",
|
|
||||||
DataType::RefType {
|
|
||||||
ref_name: "Chat".to_string(),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
];
|
|
||||||
let packet_struct = PacketStruct::new("TestPacket", fields);
|
|
||||||
let mut vec = vec![];
|
|
||||||
|
|
||||||
write_packet_struct(&template_engine, packet_struct, &mut vec)
|
|
||||||
.expect("Failed to write packet struct");
|
|
||||||
|
|
||||||
let result = String::from_utf8(vec).expect("Failed to convert vec to string");
|
|
||||||
|
|
||||||
assert_eq!(result, include_str!("../test/packet_struct.txt"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
mod error;
|
|
||||||
mod frontend;
|
|
||||||
mod templates;
|
|
||||||
|
|
||||||
pub fn main() {}
|
|
@ -1,87 +0,0 @@
|
|||||||
use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError};
|
|
||||||
use heck::SnakeCase;
|
|
||||||
|
|
||||||
pub fn create_template_engine(templates_folder: &str) -> Handlebars<'static> {
|
|
||||||
let mut template_engine = Handlebars::new();
|
|
||||||
|
|
||||||
template_engine.register_helper("snake_case", Box::new(format_snake_case));
|
|
||||||
template_engine.register_helper("packet_id", Box::new(format_packet_id));
|
|
||||||
template_engine.register_helper(
|
|
||||||
"protocol_version_module",
|
|
||||||
Box::new(format_protocol_version_module),
|
|
||||||
);
|
|
||||||
template_engine.register_escape_fn(|s| s.to_owned());
|
|
||||||
|
|
||||||
register_template_file(&mut template_engine, templates_folder, "packet_struct");
|
|
||||||
|
|
||||||
template_engine
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register_template_file(template_engine: &mut Handlebars, templates_folder: &str, name: &str) {
|
|
||||||
let tpl_path = format!("{}/{}.hbs", templates_folder, name);
|
|
||||||
|
|
||||||
template_engine
|
|
||||||
.register_template_file(name, tpl_path)
|
|
||||||
.expect("Failed to register template");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn format_snake_case(
|
|
||||||
h: &Helper,
|
|
||||||
_: &Handlebars,
|
|
||||||
_: &Context,
|
|
||||||
_: &mut RenderContext,
|
|
||||||
out: &mut dyn Output,
|
|
||||||
) -> Result<(), RenderError> {
|
|
||||||
let str = h
|
|
||||||
.param(0)
|
|
||||||
.and_then(|v| v.value().as_str())
|
|
||||||
.ok_or(RenderError::new(
|
|
||||||
"Param 0 with str type is required for snake case helper.",
|
|
||||||
))? as &str;
|
|
||||||
|
|
||||||
let snake_case_str = str.to_snake_case();
|
|
||||||
|
|
||||||
out.write(snake_case_str.as_ref())?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn format_packet_id(
|
|
||||||
h: &Helper,
|
|
||||||
_: &Handlebars,
|
|
||||||
_: &Context,
|
|
||||||
_: &mut RenderContext,
|
|
||||||
out: &mut dyn Output,
|
|
||||||
) -> Result<(), RenderError> {
|
|
||||||
let id = h
|
|
||||||
.param(0)
|
|
||||||
.and_then(|v| v.value().as_u64())
|
|
||||||
.ok_or(RenderError::new(
|
|
||||||
"Param 0 with u64 type is required for packet id helper.",
|
|
||||||
))? as u64;
|
|
||||||
|
|
||||||
let packet_id_str = format!("{:#04X}", id);
|
|
||||||
|
|
||||||
out.write(packet_id_str.as_ref())?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn format_protocol_version_module(
|
|
||||||
h: &Helper,
|
|
||||||
_: &Handlebars,
|
|
||||||
_: &Context,
|
|
||||||
_: &mut RenderContext,
|
|
||||||
out: &mut dyn Output,
|
|
||||||
) -> Result<(), RenderError> {
|
|
||||||
let version = h
|
|
||||||
.param(0)
|
|
||||||
.and_then(|v| v.value().as_str())
|
|
||||||
.ok_or(RenderError::new(
|
|
||||||
"Param 0 with str type is required for packet id helper.",
|
|
||||||
))? as &str;
|
|
||||||
|
|
||||||
let formatted_protocol_module_version =
|
|
||||||
format!("v_{}", version.replace(".", "_").replace("-", "_"));
|
|
||||||
|
|
||||||
out.write(formatted_protocol_module_version.as_ref())?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
pub struct {{name}} {
|
|
||||||
{{~#each fields as |f|}}
|
|
||||||
{{~#if f.var_int}}
|
|
||||||
#[packet(with = "var_int")]{{/if}}
|
|
||||||
{{~#if f.var_long}}
|
|
||||||
#[packet(with = "var_long")]{{/if}}
|
|
||||||
{{~#if f.rest}}
|
|
||||||
#[packet(with = "rest")]{{/if}}
|
|
||||||
{{~#if f.hyphenated}}
|
|
||||||
#[packet(with = "uuid_hyp_str")]{{/if}}
|
|
||||||
{{~#if f.max_length}}
|
|
||||||
#[packet(max_length = {{f.max_length}})]{{/if}}
|
|
||||||
{{~#if (ne f.type "RefType")}}
|
|
||||||
pub {{f.name}}: {{f.type}},
|
|
||||||
{{~else}}
|
|
||||||
pub {{f.name}}: {{f.ref_name}},
|
|
||||||
{{~/if}}
|
|
||||||
{{~/each}}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
pub struct TestPacket {
|
|
||||||
pub boolean: bool,
|
|
||||||
pub byte: i8,
|
|
||||||
pub unsigned_byte: u8,
|
|
||||||
pub short: i16,
|
|
||||||
pub unsigned_short: u16,
|
|
||||||
pub int: i32,
|
|
||||||
#[packet(with = "var_int")]
|
|
||||||
pub varint: i32,
|
|
||||||
pub unsigned_int: u32,
|
|
||||||
pub long: i64,
|
|
||||||
#[packet(with = "var_long")]
|
|
||||||
pub varlong: i64,
|
|
||||||
pub unsigned_long: u64,
|
|
||||||
pub float: f32,
|
|
||||||
pub double: f64,
|
|
||||||
#[packet(max_length = 20)]
|
|
||||||
pub string: String,
|
|
||||||
pub uuid: Uuid,
|
|
||||||
#[packet(with = "uuid_hyp_str")]
|
|
||||||
pub hyphenated: Uuid,
|
|
||||||
pub byte_array: Vec<u8>,
|
|
||||||
#[packet(with = "rest")]
|
|
||||||
pub rest: Vec<u8>,
|
|
||||||
pub compound_tag: CompoundTag,
|
|
||||||
pub ref: Chat,
|
|
||||||
}
|
|
@ -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"
|
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
//! ## Deserialize
|
//! ## Deserialize
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use minecraft_protocol::data::chat::{MessageBuilder, Color, Payload, Message};
|
//! use minecraft_protocol::data::chat::{MessageBuilder, Payload, Message, Color};
|
||||||
//!
|
//!
|
||||||
//! let json = r#"
|
//! let json = r#"
|
||||||
//! {
|
//! {
|
||||||
@ -61,13 +61,14 @@
|
|||||||
//! assert_eq!(expected_message, Message::from_json(json).unwrap());
|
//! assert_eq!(expected_message, Message::from_json(json).unwrap());
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
use crate::impl_json_encoder_decoder;
|
||||||
use serde::{
|
use serde::{
|
||||||
de::{self, Visitor},
|
de::{self, Visitor},
|
||||||
Deserialize, Serialize,
|
Deserialize, Serialize,
|
||||||
};
|
};
|
||||||
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,
|
||||||
@ -174,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,
|
||||||
@ -183,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,
|
||||||
@ -198,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,
|
||||||
@ -206,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,
|
||||||
@ -221,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 {
|
||||||
@ -279,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")]
|
||||||
@ -323,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)
|
||||||
}
|
}
|
||||||
@ -332,6 +337,8 @@ impl Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_json_encoder_decoder!(Message);
|
||||||
|
|
||||||
pub struct MessageBuilder {
|
pub struct MessageBuilder {
|
||||||
current: Message,
|
current: Message,
|
||||||
root: Option<Message>,
|
root: Option<Message>,
|
||||||
@ -426,12 +433,8 @@ impl MessageBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[test]
|
||||||
mod tests {
|
fn test_serialize_text_hello_world() {
|
||||||
use crate::data::chat::{Color, Message, MessageBuilder, Payload};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_serialize_text_hello_world() {
|
|
||||||
let message = MessageBuilder::builder(Payload::text("Hello"))
|
let message = MessageBuilder::builder(Payload::text("Hello"))
|
||||||
.color(Color::Yellow)
|
.color(Color::Yellow)
|
||||||
.bold(true)
|
.bold(true)
|
||||||
@ -445,12 +448,12 @@ mod tests {
|
|||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_text_hello_world() {
|
fn test_deserialize_text_hello_world() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("Hello"))
|
let expected_message = MessageBuilder::builder(Payload::text("Hello"))
|
||||||
.color(Color::Yellow)
|
.color(Color::Yellow)
|
||||||
.bold(true)
|
.bold(true)
|
||||||
@ -464,34 +467,34 @@ mod tests {
|
|||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_translate_opped_steve() {
|
fn test_serialize_translate_opped_steve() {
|
||||||
let with = vec![Message::new(Payload::text("Steve"))];
|
let with = vec![Message::new(Payload::text("Steve"))];
|
||||||
let message = Message::new(Payload::translation("Opped %s", with));
|
let message = Message::new(Payload::translation("Opped %s", with));
|
||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_translate_opped_steve() {
|
fn test_deserialize_translate_opped_steve() {
|
||||||
let with = vec![Message::new(Payload::text("Steve"))];
|
let with = vec![Message::new(Payload::text("Steve"))];
|
||||||
let expected_message = Message::new(Payload::translation("Opped %s", with));
|
let expected_message = Message::new(Payload::translation("Opped %s", with));
|
||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_keybind_jump() {
|
fn test_serialize_keybind_jump() {
|
||||||
let message = MessageBuilder::builder(Payload::text("Press \""))
|
let message = MessageBuilder::builder(Payload::text("Press \""))
|
||||||
.color(Color::Yellow)
|
.color(Color::Yellow)
|
||||||
.bold(true)
|
.bold(true)
|
||||||
@ -504,12 +507,12 @@ mod tests {
|
|||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_keybind_jump() {
|
fn test_deserialize_keybind_jump() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("Press \""))
|
let expected_message = MessageBuilder::builder(Payload::text("Press \""))
|
||||||
.color(Color::Yellow)
|
.color(Color::Yellow)
|
||||||
.bold(true)
|
.bold(true)
|
||||||
@ -522,12 +525,12 @@ mod tests {
|
|||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_click_open_url() {
|
fn test_serialize_click_open_url() {
|
||||||
let message = MessageBuilder::builder(Payload::text("click me"))
|
let message = MessageBuilder::builder(Payload::text("click me"))
|
||||||
.color(Color::Yellow)
|
.color(Color::Yellow)
|
||||||
.bold(true)
|
.bold(true)
|
||||||
@ -536,12 +539,12 @@ mod tests {
|
|||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_click_open_url() {
|
fn test_deserialize_click_open_url() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
||||||
.color(Color::Yellow)
|
.color(Color::Yellow)
|
||||||
.bold(true)
|
.bold(true)
|
||||||
@ -550,12 +553,12 @@ mod tests {
|
|||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_click_run_command() {
|
fn test_serialize_click_run_command() {
|
||||||
let message = MessageBuilder::builder(Payload::text("click me"))
|
let message = MessageBuilder::builder(Payload::text("click me"))
|
||||||
.color(Color::LightPurple)
|
.color(Color::LightPurple)
|
||||||
.italic(true)
|
.italic(true)
|
||||||
@ -564,12 +567,12 @@ mod tests {
|
|||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_click_run_command() {
|
fn test_deserialize_click_run_command() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
||||||
.color(Color::LightPurple)
|
.color(Color::LightPurple)
|
||||||
.italic(true)
|
.italic(true)
|
||||||
@ -578,12 +581,12 @@ mod tests {
|
|||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_click_suggest_command() {
|
fn test_serialize_click_suggest_command() {
|
||||||
let message = MessageBuilder::builder(Payload::text("click me"))
|
let message = MessageBuilder::builder(Payload::text("click me"))
|
||||||
.color(Color::Blue)
|
.color(Color::Blue)
|
||||||
.obfuscated(true)
|
.obfuscated(true)
|
||||||
@ -592,12 +595,12 @@ mod tests {
|
|||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_click_suggest_command() {
|
fn test_deserialize_click_suggest_command() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
||||||
.color(Color::Blue)
|
.color(Color::Blue)
|
||||||
.obfuscated(true)
|
.obfuscated(true)
|
||||||
@ -606,12 +609,12 @@ mod tests {
|
|||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_click_change_page() {
|
fn test_serialize_click_change_page() {
|
||||||
let message = MessageBuilder::builder(Payload::text("click me"))
|
let message = MessageBuilder::builder(Payload::text("click me"))
|
||||||
.color(Color::DarkGray)
|
.color(Color::DarkGray)
|
||||||
.underlined(true)
|
.underlined(true)
|
||||||
@ -620,12 +623,12 @@ mod tests {
|
|||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_click_change_page() {
|
fn test_deserialize_click_change_page() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
||||||
.color(Color::DarkGray)
|
.color(Color::DarkGray)
|
||||||
.underlined(true)
|
.underlined(true)
|
||||||
@ -634,12 +637,12 @@ mod tests {
|
|||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_hover_show_text() {
|
fn test_serialize_hover_show_text() {
|
||||||
let message = MessageBuilder::builder(Payload::text("hover at me"))
|
let message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||||
.color(Color::DarkPurple)
|
.color(Color::DarkPurple)
|
||||||
.bold(true)
|
.bold(true)
|
||||||
@ -648,12 +651,12 @@ mod tests {
|
|||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_hover_show_text() {
|
fn test_deserialize_hover_show_text() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("hover at me"))
|
let expected_message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||||
.color(Color::DarkPurple)
|
.color(Color::DarkPurple)
|
||||||
.bold(true)
|
.bold(true)
|
||||||
@ -662,12 +665,12 @@ mod tests {
|
|||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_hover_show_item() {
|
fn test_serialize_hover_show_item() {
|
||||||
let message = MessageBuilder::builder(Payload::text("hover at me"))
|
let message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||||
.color(Color::DarkRed)
|
.color(Color::DarkRed)
|
||||||
.italic(true)
|
.italic(true)
|
||||||
@ -676,12 +679,12 @@ mod tests {
|
|||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_hover_show_item() {
|
fn test_deserialize_hover_show_item() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("hover at me"))
|
let expected_message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||||
.color(Color::DarkRed)
|
.color(Color::DarkRed)
|
||||||
.italic(true)
|
.italic(true)
|
||||||
@ -690,12 +693,12 @@ mod tests {
|
|||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_hover_show_entity() {
|
fn test_serialize_hover_show_entity() {
|
||||||
let message = MessageBuilder::builder(Payload::text("hover at me"))
|
let message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||||
.color(Color::DarkAqua)
|
.color(Color::DarkAqua)
|
||||||
.obfuscated(true)
|
.obfuscated(true)
|
||||||
@ -704,12 +707,12 @@ mod tests {
|
|||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_hover_show_entity() {
|
fn test_deserialize_hover_show_entity() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("hover at me"))
|
let expected_message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||||
.color(Color::DarkAqua)
|
.color(Color::DarkAqua)
|
||||||
.obfuscated(true)
|
.obfuscated(true)
|
||||||
@ -718,31 +721,30 @@ mod tests {
|
|||||||
|
|
||||||
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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_hex_color() {
|
fn test_serialize_hex_color() {
|
||||||
let message = MessageBuilder::builder(Payload::text("Hello"))
|
let message = MessageBuilder::builder(Payload::text("Hello"))
|
||||||
.color(Color::Hex("#ffffff".into()))
|
.color(Color::Hex("#ffffff".into()))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_hex_color() {
|
fn test_deserialize_hex_color() {
|
||||||
let expected_message = MessageBuilder::builder(Payload::text("Hello"))
|
let expected_message = MessageBuilder::builder(Payload::text("Hello"))
|
||||||
.color(Color::Hex("#ffffff".into()))
|
.color(Color::Hex("#ffffff".into()))
|
||||||
.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
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,33 @@
|
|||||||
use crate::data::chat::Message;
|
use crate::data::chat::Message;
|
||||||
|
use crate::impl_json_encoder_decoder;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
pub struct ServerStatus {
|
pub struct ServerStatus {
|
||||||
pub version: ServerVersion,
|
pub version: ServerVersion,
|
||||||
pub players: OnlinePlayers,
|
pub players: OnlinePlayers,
|
||||||
pub description: Message,
|
pub description: Message,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
pub struct ServerVersion {
|
pub struct ServerVersion {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub protocol: u32,
|
pub protocol: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
pub struct OnlinePlayers {
|
pub struct OnlinePlayers {
|
||||||
pub max: u32,
|
pub max: u32,
|
||||||
pub online: u32,
|
pub online: u32,
|
||||||
|
#[serde(default)]
|
||||||
pub sample: Vec<OnlinePlayer>,
|
pub sample: Vec<OnlinePlayer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||||
pub struct OnlinePlayer {
|
pub struct OnlinePlayer {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_json_encoder_decoder!(ServerStatus);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use crate::error::DecodeError;
|
use crate::error::DecodeError;
|
||||||
use byteorder::{BigEndian, ReadBytesExt};
|
use byteorder::{BigEndian, ReadBytesExt};
|
||||||
use nbt::CompoundTag;
|
use nbt::CompoundTag;
|
||||||
use num_traits::FromPrimitive;
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -12,15 +11,13 @@ pub trait Decoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Trait adds additional helper methods for `Read` to read protocol data.
|
/// Trait adds additional helper methods for `Read` to read protocol data.
|
||||||
trait DecoderReadExt {
|
pub trait DecoderReadExt {
|
||||||
fn read_bool(&mut self) -> Result<bool, DecodeError>;
|
fn read_bool(&mut self) -> Result<bool, DecodeError>;
|
||||||
|
|
||||||
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError>;
|
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError>;
|
||||||
|
|
||||||
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError>;
|
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError>;
|
||||||
|
|
||||||
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError>;
|
|
||||||
|
|
||||||
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError>;
|
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError>;
|
||||||
|
|
||||||
fn read_var_i32(&mut self) -> Result<i32, DecodeError>;
|
fn read_var_i32(&mut self) -> Result<i32, DecodeError>;
|
||||||
@ -86,13 +83,6 @@ impl<R: Read> DecoderReadExt for R {
|
|||||||
Ok(buf)
|
Ok(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError> {
|
||||||
Ok(nbt::decode::read_compound_tag(self)?)
|
Ok(nbt::decode::read_compound_tag(self)?)
|
||||||
}
|
}
|
||||||
@ -157,6 +147,22 @@ impl Decoder for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Decoder for f32 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_f32::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for f64 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_f64::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Decoder for String {
|
impl Decoder for String {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
@ -216,7 +222,7 @@ impl Decoder for Vec<CompoundTag> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod var_int {
|
pub mod var_int {
|
||||||
use crate::decoder::DecoderReadExt;
|
use crate::decoder::DecoderReadExt;
|
||||||
use crate::error::DecodeError;
|
use crate::error::DecodeError;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
@ -226,7 +232,7 @@ mod var_int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod var_long {
|
pub mod var_long {
|
||||||
use crate::decoder::DecoderReadExt;
|
use crate::decoder::DecoderReadExt;
|
||||||
use crate::error::DecodeError;
|
use crate::error::DecodeError;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
@ -236,7 +242,7 @@ mod var_long {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod rest {
|
pub mod rest {
|
||||||
use crate::error::DecodeError;
|
use crate::error::DecodeError;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
@ -248,7 +254,7 @@ mod rest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod uuid_hyp_str {
|
pub mod uuid_hyp_str {
|
||||||
use crate::decoder::DecoderReadExt;
|
use crate::decoder::DecoderReadExt;
|
||||||
use crate::error::DecodeError;
|
use crate::error::DecodeError;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use crate::error::EncodeError;
|
use crate::error::EncodeError;
|
||||||
use byteorder::{BigEndian, WriteBytesExt};
|
use byteorder::{BigEndian, WriteBytesExt};
|
||||||
use nbt::CompoundTag;
|
use nbt::CompoundTag;
|
||||||
use num_traits::ToPrimitive;
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -10,15 +9,13 @@ pub trait Encoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Trait adds additional helper methods for `Write` to write protocol data.
|
/// Trait adds additional helper methods for `Write` to write protocol data.
|
||||||
trait EncoderWriteExt {
|
pub trait EncoderWriteExt {
|
||||||
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError>;
|
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
fn write_string(&mut self, value: &str, max_length: u16) -> 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_byte_array(&mut self, value: &[u8]) -> 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_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
fn write_var_i32(&mut self, value: i32) -> Result<(), EncodeError>;
|
fn write_var_i32(&mut self, value: i32) -> Result<(), EncodeError>;
|
||||||
@ -80,15 +77,8 @@ impl<W: Write> EncoderWriteExt for W {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError> {
|
||||||
nbt::encode::write_compound_tag(self, value.clone())?;
|
nbt::encode::write_compound_tag(self, &value)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -139,6 +129,18 @@ impl Encoder for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Encoder for f32 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_f32::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for f64 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_f64::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Encoder for String {
|
impl Encoder for String {
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
Ok(writer.write_string(self, 32_768)?)
|
Ok(writer.write_string(self, 32_768)?)
|
||||||
@ -181,7 +183,7 @@ impl Encoder for Vec<CompoundTag> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod var_int {
|
pub mod var_int {
|
||||||
use crate::encoder::EncoderWriteExt;
|
use crate::encoder::EncoderWriteExt;
|
||||||
use crate::error::EncodeError;
|
use crate::error::EncodeError;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@ -193,7 +195,7 @@ mod var_int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod var_long {
|
pub mod var_long {
|
||||||
use crate::encoder::EncoderWriteExt;
|
use crate::encoder::EncoderWriteExt;
|
||||||
use crate::error::EncodeError;
|
use crate::error::EncodeError;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@ -205,7 +207,7 @@ mod var_long {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod rest {
|
pub mod rest {
|
||||||
use crate::error::EncodeError;
|
use crate::error::EncodeError;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
@ -216,7 +218,7 @@ mod rest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod uuid_hyp_str {
|
pub mod uuid_hyp_str {
|
||||||
use crate::encoder::EncoderWriteExt;
|
use crate::encoder::EncoderWriteExt;
|
||||||
use crate::error::EncodeError;
|
use crate::error::EncodeError;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
@ -65,7 +65,7 @@ pub enum DecodeError {
|
|||||||
},
|
},
|
||||||
/// Type id was not parsed as valid enum value.
|
/// Type id was not parsed as valid enum value.
|
||||||
UnknownEnumType {
|
UnknownEnumType {
|
||||||
type_id: u8,
|
type_id: usize,
|
||||||
},
|
},
|
||||||
TagDecodeError {
|
TagDecodeError {
|
||||||
tag_decode_error: TagDecodeError,
|
tag_decode_error: TagDecodeError,
|
||||||
|
@ -1,8 +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.
|
||||||
#[cfg(feature = "data")]
|
|
||||||
pub mod data;
|
pub mod data;
|
||||||
pub mod decoder;
|
pub mod decoder;
|
||||||
pub mod encoder;
|
pub mod encoder;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
pub mod version;
|
||||||
|
|
||||||
|
/// Protocol limits maximum string length.
|
||||||
|
const STRING_MAX_LENGTH: u16 = 32_768;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! impl_json_encoder_decoder (
|
||||||
|
($ty: ident) => (
|
||||||
|
impl crate::encoder::Encoder for $ty {
|
||||||
|
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::error::EncodeError> {
|
||||||
|
let json = serde_json::to_string(self)?;
|
||||||
|
crate::encoder::EncoderWriteExt::write_string(writer, &json, crate::STRING_MAX_LENGTH)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl crate::decoder::Decoder for $ty {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
|
||||||
|
let json = crate::decoder::DecoderReadExt::read_string(reader, crate::STRING_MAX_LENGTH)?;
|
||||||
|
|
||||||
|
Ok(serde_json::from_str(&json)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
);
|
||||||
|
1
protocol/src/version/mod.rs
Normal file
1
protocol/src/version/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod v1_14_4;
|
734
protocol/src/version/v1_14_4/game.rs
Normal file
734
protocol/src/version/v1_14_4/game.rs
Normal file
@ -0,0 +1,734 @@
|
|||||||
|
use crate::data::chat::Message;
|
||||||
|
use crate::decoder::Decoder;
|
||||||
|
use crate::decoder::DecoderReadExt;
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use byteorder::{ReadBytesExt, WriteBytesExt};
|
||||||
|
use minecraft_protocol_derive::{Decoder, Encoder};
|
||||||
|
use nbt::CompoundTag;
|
||||||
|
use std::io::Read;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub enum GameServerBoundPacket {
|
||||||
|
ServerBoundChatMessage(ServerBoundChatMessage),
|
||||||
|
ServerBoundKeepAlive(ServerBoundKeepAlive),
|
||||||
|
ServerBoundAbilities(ServerBoundAbilities),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum GameClientBoundPacket {
|
||||||
|
ClientBoundChatMessage(ClientBoundChatMessage),
|
||||||
|
JoinGame(JoinGame),
|
||||||
|
ClientBoundKeepAlive(ClientBoundKeepAlive),
|
||||||
|
ChunkData(ChunkData),
|
||||||
|
GameDisconnect(GameDisconnect),
|
||||||
|
BossBar(BossBar),
|
||||||
|
EntityAction(EntityAction),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GameServerBoundPacket {
|
||||||
|
pub fn get_type_id(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
GameServerBoundPacket::ServerBoundChatMessage(_) => 0x03,
|
||||||
|
GameServerBoundPacket::ServerBoundKeepAlive(_) => 0x0F,
|
||||||
|
GameServerBoundPacket::ServerBoundAbilities(_) => 0x19,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
|
||||||
|
match type_id {
|
||||||
|
0x03 => {
|
||||||
|
let chat_message = ServerBoundChatMessage::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(GameServerBoundPacket::ServerBoundChatMessage(chat_message))
|
||||||
|
}
|
||||||
|
0x0F => {
|
||||||
|
let keep_alive = ServerBoundKeepAlive::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(GameServerBoundPacket::ServerBoundKeepAlive(keep_alive))
|
||||||
|
}
|
||||||
|
_ => Err(DecodeError::UnknownPacketType { type_id }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GameClientBoundPacket {
|
||||||
|
pub fn get_type_id(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
GameClientBoundPacket::ClientBoundChatMessage(_) => 0x0E,
|
||||||
|
GameClientBoundPacket::GameDisconnect(_) => 0x1A,
|
||||||
|
GameClientBoundPacket::ClientBoundKeepAlive(_) => 0x20,
|
||||||
|
GameClientBoundPacket::ChunkData(_) => 0x21,
|
||||||
|
GameClientBoundPacket::JoinGame(_) => 0x25,
|
||||||
|
GameClientBoundPacket::BossBar(_) => 0x0D,
|
||||||
|
GameClientBoundPacket::EntityAction(_) => 0x1B,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
|
||||||
|
match type_id {
|
||||||
|
0x0E => {
|
||||||
|
let chat_message = ClientBoundChatMessage::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(GameClientBoundPacket::ClientBoundChatMessage(chat_message))
|
||||||
|
}
|
||||||
|
0x1A => {
|
||||||
|
let game_disconnect = GameDisconnect::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(GameClientBoundPacket::GameDisconnect(game_disconnect))
|
||||||
|
}
|
||||||
|
0x20 => {
|
||||||
|
let keep_alive = ClientBoundKeepAlive::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(GameClientBoundPacket::ClientBoundKeepAlive(keep_alive))
|
||||||
|
}
|
||||||
|
0x21 => {
|
||||||
|
let chunk_data = ChunkData::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(GameClientBoundPacket::ChunkData(chunk_data))
|
||||||
|
}
|
||||||
|
0x25 => {
|
||||||
|
let join_game = JoinGame::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(GameClientBoundPacket::JoinGame(join_game))
|
||||||
|
}
|
||||||
|
_ => Err(DecodeError::UnknownPacketType { type_id }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct ServerBoundChatMessage {
|
||||||
|
#[data_type(max_length = 256)]
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ServerBoundChatMessage {
|
||||||
|
pub fn new(message: String) -> GameServerBoundPacket {
|
||||||
|
let chat_message = ServerBoundChatMessage { message };
|
||||||
|
|
||||||
|
GameServerBoundPacket::ServerBoundChatMessage(chat_message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct ClientBoundChatMessage {
|
||||||
|
pub message: Message,
|
||||||
|
pub position: MessagePosition,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug, Eq, PartialEq)]
|
||||||
|
pub enum MessagePosition {
|
||||||
|
Chat,
|
||||||
|
System,
|
||||||
|
HotBar,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ClientBoundChatMessage {
|
||||||
|
pub fn new(message: Message, position: MessagePosition) -> GameClientBoundPacket {
|
||||||
|
let chat_message = ClientBoundChatMessage { message, position };
|
||||||
|
|
||||||
|
GameClientBoundPacket::ClientBoundChatMessage(chat_message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct JoinGame {
|
||||||
|
pub entity_id: u32,
|
||||||
|
pub game_mode: GameMode,
|
||||||
|
pub dimension: i32,
|
||||||
|
pub max_players: u8,
|
||||||
|
#[data_type(max_length = 16)]
|
||||||
|
pub level_type: String,
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub view_distance: i32,
|
||||||
|
pub reduced_debug_info: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug, Eq, PartialEq)]
|
||||||
|
pub enum GameMode {
|
||||||
|
Survival = 0,
|
||||||
|
Creative = 1,
|
||||||
|
Adventure = 2,
|
||||||
|
Spectator = 3,
|
||||||
|
Hardcore = 8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl JoinGame {
|
||||||
|
pub fn new(
|
||||||
|
entity_id: u32,
|
||||||
|
game_mode: GameMode,
|
||||||
|
dimension: i32,
|
||||||
|
max_players: u8,
|
||||||
|
level_type: String,
|
||||||
|
view_distance: i32,
|
||||||
|
reduced_debug_info: bool,
|
||||||
|
) -> GameClientBoundPacket {
|
||||||
|
let join_game = JoinGame {
|
||||||
|
entity_id,
|
||||||
|
game_mode,
|
||||||
|
dimension,
|
||||||
|
max_players,
|
||||||
|
level_type,
|
||||||
|
view_distance,
|
||||||
|
reduced_debug_info,
|
||||||
|
};
|
||||||
|
|
||||||
|
GameClientBoundPacket::JoinGame(join_game)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder)]
|
||||||
|
pub struct ServerBoundKeepAlive {
|
||||||
|
pub id: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ServerBoundKeepAlive {
|
||||||
|
pub fn new(id: u64) -> GameServerBoundPacket {
|
||||||
|
let keep_alive = ServerBoundKeepAlive { id };
|
||||||
|
|
||||||
|
GameServerBoundPacket::ServerBoundKeepAlive(keep_alive)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder)]
|
||||||
|
pub struct ClientBoundKeepAlive {
|
||||||
|
pub id: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ClientBoundKeepAlive {
|
||||||
|
pub fn new(id: u64) -> GameClientBoundPacket {
|
||||||
|
let keep_alive = ClientBoundKeepAlive { id };
|
||||||
|
|
||||||
|
GameClientBoundPacket::ClientBoundKeepAlive(keep_alive)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct ChunkData {
|
||||||
|
pub x: i32,
|
||||||
|
pub z: i32,
|
||||||
|
pub full: bool,
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub primary_mask: i32,
|
||||||
|
pub heights: CompoundTag,
|
||||||
|
pub data: Vec<u8>,
|
||||||
|
pub tiles: Vec<CompoundTag>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChunkData {
|
||||||
|
pub fn new(
|
||||||
|
x: i32,
|
||||||
|
z: i32,
|
||||||
|
full: bool,
|
||||||
|
primary_mask: i32,
|
||||||
|
heights: CompoundTag,
|
||||||
|
data: Vec<u8>,
|
||||||
|
tiles: Vec<CompoundTag>,
|
||||||
|
) -> GameClientBoundPacket {
|
||||||
|
let chunk_data = ChunkData {
|
||||||
|
x,
|
||||||
|
z,
|
||||||
|
full,
|
||||||
|
primary_mask,
|
||||||
|
heights,
|
||||||
|
data,
|
||||||
|
tiles,
|
||||||
|
};
|
||||||
|
|
||||||
|
GameClientBoundPacket::ChunkData(chunk_data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct GameDisconnect {
|
||||||
|
pub reason: Message,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GameDisconnect {
|
||||||
|
pub fn new(reason: Message) -> GameClientBoundPacket {
|
||||||
|
let game_disconnect = GameDisconnect { reason };
|
||||||
|
|
||||||
|
GameClientBoundPacket::GameDisconnect(game_disconnect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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)]
|
||||||
|
mod tests {
|
||||||
|
use crate::data::chat::Payload;
|
||||||
|
use crate::decoder::Decoder;
|
||||||
|
use crate::encoder::Encoder;
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use crate::error::{DecodeError, EncodeError};
|
||||||
|
use crate::version::v1_14_4::game::*;
|
||||||
|
use crate::STRING_MAX_LENGTH;
|
||||||
|
use nbt::CompoundTag;
|
||||||
|
use std::io::Cursor;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_server_bound_chat_message_encode() {
|
||||||
|
let chat_message = ServerBoundChatMessage {
|
||||||
|
message: String::from("hello server!"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
chat_message.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/server_bound_chat_message.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_server_bound_chat_message_decode() {
|
||||||
|
let mut cursor = Cursor::new(
|
||||||
|
include_bytes!("../../../test/packet/game/server_bound_chat_message.dat").to_vec(),
|
||||||
|
);
|
||||||
|
let chat_message = ServerBoundChatMessage::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(chat_message.message, "hello server!");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_server_bound_chat_message_encode_invalid_length() {
|
||||||
|
let chat_message = ServerBoundChatMessage {
|
||||||
|
message: "abc".repeat(100),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
|
||||||
|
let encode_error = chat_message
|
||||||
|
.encode(&mut vec)
|
||||||
|
.err()
|
||||||
|
.expect("Expected error `StringTooLong` because message has invalid length");
|
||||||
|
|
||||||
|
match encode_error {
|
||||||
|
EncodeError::StringTooLong { length, max_length } => {
|
||||||
|
assert_eq!(length, 300);
|
||||||
|
assert_eq!(max_length, 256);
|
||||||
|
}
|
||||||
|
_ => panic!("Expected `StringTooLong` but got `{:?}`", encode_error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_server_bound_chat_message_decode_invalid_length() {
|
||||||
|
let message = "abc".repeat(100);
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
vec.write_string(&message, STRING_MAX_LENGTH).unwrap();
|
||||||
|
|
||||||
|
let mut cursor = Cursor::new(vec);
|
||||||
|
|
||||||
|
let decode_error = ServerBoundChatMessage::decode(&mut cursor)
|
||||||
|
.err()
|
||||||
|
.expect("Expected error `StringTooLong` because message has invalid length");
|
||||||
|
|
||||||
|
match decode_error {
|
||||||
|
DecodeError::StringTooLong { length, max_length } => {
|
||||||
|
assert_eq!(length, 300);
|
||||||
|
assert_eq!(max_length, 256);
|
||||||
|
}
|
||||||
|
_ => panic!("Expected `StringTooLong` but got `{:?}`", decode_error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_client_bound_chat_message_encode() {
|
||||||
|
let chat_message = ClientBoundChatMessage {
|
||||||
|
message: Message::new(Payload::text("hello client!")),
|
||||||
|
position: MessagePosition::System,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
chat_message.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/client_bound_chat_message.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_client_bound_chat_message_decode() {
|
||||||
|
let mut cursor = Cursor::new(
|
||||||
|
include_bytes!("../../../test/packet/game/client_bound_chat_message.dat").to_vec(),
|
||||||
|
);
|
||||||
|
let chat_message = ClientBoundChatMessage::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
chat_message.message,
|
||||||
|
Message::new(Payload::text("hello client!"))
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(chat_message.position, MessagePosition::System);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_server_bound_keep_alive_encode() {
|
||||||
|
let keep_alive = ServerBoundKeepAlive { id: 31122019 };
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
keep_alive.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/server_bound_keep_alive.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_server_bound_keep_alive_decode() {
|
||||||
|
let mut cursor = Cursor::new(
|
||||||
|
include_bytes!("../../../test/packet/game/server_bound_keep_alive.dat").to_vec(),
|
||||||
|
);
|
||||||
|
let keep_alive = ServerBoundKeepAlive::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(keep_alive.id, 31122019);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_client_bound_keep_alive_encode() {
|
||||||
|
let keep_alive = ClientBoundKeepAlive { id: 240714 };
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
keep_alive.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/client_bound_keep_alive.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_client_bound_keep_alive_decode() {
|
||||||
|
let mut cursor = Cursor::new(
|
||||||
|
include_bytes!("../../../test/packet/game/client_bound_keep_alive.dat").to_vec(),
|
||||||
|
);
|
||||||
|
let keep_alive = ClientBoundKeepAlive::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(keep_alive.id, 240714);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_join_game_encode() {
|
||||||
|
let join_game = JoinGame {
|
||||||
|
entity_id: 27,
|
||||||
|
game_mode: GameMode::Hardcore,
|
||||||
|
dimension: 23,
|
||||||
|
max_players: 100,
|
||||||
|
level_type: String::from("default"),
|
||||||
|
view_distance: 10,
|
||||||
|
reduced_debug_info: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
join_game.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/join_game.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_join_game_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/game/join_game.dat").to_vec());
|
||||||
|
let join_game = JoinGame::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(join_game.entity_id, 27);
|
||||||
|
assert_eq!(join_game.game_mode, GameMode::Hardcore);
|
||||||
|
assert_eq!(join_game.dimension, 23);
|
||||||
|
assert_eq!(join_game.max_players, 100);
|
||||||
|
assert_eq!(join_game.level_type, String::from("default"));
|
||||||
|
assert_eq!(join_game.view_distance, 10);
|
||||||
|
assert!(join_game.reduced_debug_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_chunk_data_encode() {
|
||||||
|
let chunk_data = ChunkData {
|
||||||
|
x: -2,
|
||||||
|
z: 5,
|
||||||
|
full: true,
|
||||||
|
primary_mask: 65535,
|
||||||
|
heights: CompoundTag::named("HeightMaps"),
|
||||||
|
data: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
||||||
|
tiles: vec![CompoundTag::named("TileEntity")],
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
chunk_data.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/chunk_data.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_chunk_data_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/game/chunk_data.dat").to_vec());
|
||||||
|
let chunk_data = ChunkData::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(chunk_data.x, -2);
|
||||||
|
assert_eq!(chunk_data.z, 5);
|
||||||
|
assert!(chunk_data.full);
|
||||||
|
assert_eq!(chunk_data.heights.name, Some(String::from("HeightMaps")));
|
||||||
|
assert_eq!(chunk_data.data, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||||
|
assert_eq!(chunk_data.primary_mask, 65535);
|
||||||
|
assert_eq!(chunk_data.tiles[0].name, Some(String::from("TileEntity")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_game_disconnect_encode() {
|
||||||
|
let game_disconnect = GameDisconnect {
|
||||||
|
reason: Message::new(Payload::text("Message")),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
game_disconnect.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/game/game_disconnect.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_game_disconnect_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/game/game_disconnect.dat").to_vec());
|
||||||
|
let game_disconnect = GameDisconnect::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
game_disconnect.reason,
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
482
protocol/src/version/v1_14_4/login.rs
Normal file
482
protocol/src/version/v1_14_4/login.rs
Normal file
@ -0,0 +1,482 @@
|
|||||||
|
use std::io::Read;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::data::chat::Message;
|
||||||
|
use crate::decoder::Decoder;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use minecraft_protocol_derive::{Decoder, Encoder};
|
||||||
|
|
||||||
|
pub enum LoginServerBoundPacket {
|
||||||
|
LoginStart(LoginStart),
|
||||||
|
EncryptionResponse(EncryptionResponse),
|
||||||
|
LoginPluginResponse(LoginPluginResponse),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum LoginClientBoundPacket {
|
||||||
|
LoginDisconnect(LoginDisconnect),
|
||||||
|
EncryptionRequest(EncryptionRequest),
|
||||||
|
LoginSuccess(LoginSuccess),
|
||||||
|
SetCompression(SetCompression),
|
||||||
|
LoginPluginRequest(LoginPluginRequest),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoginServerBoundPacket {
|
||||||
|
pub fn get_type_id(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
LoginServerBoundPacket::LoginStart(_) => 0x00,
|
||||||
|
LoginServerBoundPacket::EncryptionResponse(_) => 0x01,
|
||||||
|
LoginServerBoundPacket::LoginPluginResponse(_) => 0x02,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
|
||||||
|
match type_id {
|
||||||
|
0x00 => {
|
||||||
|
let login_start = LoginStart::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(LoginServerBoundPacket::LoginStart(login_start))
|
||||||
|
}
|
||||||
|
0x01 => {
|
||||||
|
let encryption_response = EncryptionResponse::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(LoginServerBoundPacket::EncryptionResponse(
|
||||||
|
encryption_response,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
0x02 => {
|
||||||
|
let login_plugin_response = LoginPluginResponse::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(LoginServerBoundPacket::LoginPluginResponse(
|
||||||
|
login_plugin_response,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
_ => Err(DecodeError::UnknownPacketType { type_id }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoginClientBoundPacket {
|
||||||
|
pub fn get_type_id(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
LoginClientBoundPacket::LoginDisconnect(_) => 0x00,
|
||||||
|
LoginClientBoundPacket::EncryptionRequest(_) => 0x01,
|
||||||
|
LoginClientBoundPacket::LoginSuccess(_) => 0x02,
|
||||||
|
LoginClientBoundPacket::SetCompression(_) => 0x03,
|
||||||
|
LoginClientBoundPacket::LoginPluginRequest(_) => 0x04,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
|
||||||
|
match type_id {
|
||||||
|
0x00 => {
|
||||||
|
let login_disconnect = LoginDisconnect::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(LoginClientBoundPacket::LoginDisconnect(login_disconnect))
|
||||||
|
}
|
||||||
|
0x01 => {
|
||||||
|
let encryption_request = EncryptionRequest::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(LoginClientBoundPacket::EncryptionRequest(
|
||||||
|
encryption_request,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
0x02 => {
|
||||||
|
let login_success = LoginSuccess::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(LoginClientBoundPacket::LoginSuccess(login_success))
|
||||||
|
}
|
||||||
|
0x03 => {
|
||||||
|
let set_compression = SetCompression::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(LoginClientBoundPacket::SetCompression(set_compression))
|
||||||
|
}
|
||||||
|
0x04 => {
|
||||||
|
let login_plugin_request = LoginPluginRequest::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(LoginClientBoundPacket::LoginPluginRequest(
|
||||||
|
login_plugin_request,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
_ => Err(DecodeError::UnknownPacketType { type_id }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct LoginStart {
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoginStart {
|
||||||
|
pub fn new(name: String) -> LoginServerBoundPacket {
|
||||||
|
let login_start = LoginStart { name };
|
||||||
|
|
||||||
|
LoginServerBoundPacket::LoginStart(login_start)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct EncryptionResponse {
|
||||||
|
pub shared_secret: Vec<u8>,
|
||||||
|
pub verify_token: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EncryptionResponse {
|
||||||
|
pub fn new(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> LoginServerBoundPacket {
|
||||||
|
let encryption_response = EncryptionResponse {
|
||||||
|
shared_secret,
|
||||||
|
verify_token,
|
||||||
|
};
|
||||||
|
|
||||||
|
LoginServerBoundPacket::EncryptionResponse(encryption_response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct LoginPluginResponse {
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub message_id: i32,
|
||||||
|
pub successful: bool,
|
||||||
|
#[data_type(with = "rest")]
|
||||||
|
pub data: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoginPluginResponse {
|
||||||
|
pub fn new(message_id: i32, successful: bool, data: Vec<u8>) -> LoginServerBoundPacket {
|
||||||
|
let login_plugin_response = LoginPluginResponse {
|
||||||
|
message_id,
|
||||||
|
successful,
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
|
||||||
|
LoginServerBoundPacket::LoginPluginResponse(login_plugin_response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct LoginDisconnect {
|
||||||
|
pub reason: Message,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoginDisconnect {
|
||||||
|
pub fn new(reason: Message) -> LoginClientBoundPacket {
|
||||||
|
let login_disconnect = LoginDisconnect { reason };
|
||||||
|
|
||||||
|
LoginClientBoundPacket::LoginDisconnect(login_disconnect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct EncryptionRequest {
|
||||||
|
#[data_type(max_length = 20)]
|
||||||
|
pub server_id: String,
|
||||||
|
pub public_key: Vec<u8>,
|
||||||
|
pub verify_token: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EncryptionRequest {
|
||||||
|
pub fn new(
|
||||||
|
server_id: String,
|
||||||
|
public_key: Vec<u8>,
|
||||||
|
verify_token: Vec<u8>,
|
||||||
|
) -> LoginClientBoundPacket {
|
||||||
|
let encryption_request = EncryptionRequest {
|
||||||
|
server_id,
|
||||||
|
public_key,
|
||||||
|
verify_token,
|
||||||
|
};
|
||||||
|
|
||||||
|
LoginClientBoundPacket::EncryptionRequest(encryption_request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct LoginSuccess {
|
||||||
|
#[data_type(with = "uuid_hyp_str")]
|
||||||
|
pub uuid: Uuid,
|
||||||
|
#[data_type(max_length = 16)]
|
||||||
|
pub username: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoginSuccess {
|
||||||
|
pub fn new(uuid: Uuid, username: String) -> LoginClientBoundPacket {
|
||||||
|
let login_success = LoginSuccess { uuid, username };
|
||||||
|
|
||||||
|
LoginClientBoundPacket::LoginSuccess(login_success)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct SetCompression {
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub threshold: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SetCompression {
|
||||||
|
pub fn new(threshold: i32) -> LoginClientBoundPacket {
|
||||||
|
let set_compression = SetCompression { threshold };
|
||||||
|
|
||||||
|
LoginClientBoundPacket::SetCompression(set_compression)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct LoginPluginRequest {
|
||||||
|
#[data_type(with = "var_int")]
|
||||||
|
pub message_id: i32,
|
||||||
|
pub channel: String,
|
||||||
|
#[data_type(with = "rest")]
|
||||||
|
pub data: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoginPluginRequest {
|
||||||
|
pub fn new(message_id: i32, channel: String, data: Vec<u8>) -> LoginClientBoundPacket {
|
||||||
|
let login_plugin_request = LoginPluginRequest {
|
||||||
|
message_id,
|
||||||
|
channel,
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
|
||||||
|
LoginClientBoundPacket::LoginPluginRequest(login_plugin_request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::data::chat::Payload;
|
||||||
|
use crate::decoder::Decoder;
|
||||||
|
use crate::encoder::Encoder;
|
||||||
|
use crate::version::v1_14_4::login::*;
|
||||||
|
use std::io::Cursor;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_start_packet_encode() {
|
||||||
|
let login_start = LoginStart {
|
||||||
|
name: String::from("Username"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
login_start.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/login/login_start.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_start_packet_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/login/login_start.dat").to_vec());
|
||||||
|
let login_start = LoginStart::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(login_start.name, String::from("Username"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_encryption_response_encode() {
|
||||||
|
let encryption_response = EncryptionResponse {
|
||||||
|
shared_secret: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
||||||
|
verify_token: vec![1, 2, 3, 4],
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
encryption_response.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/login/encryption_response.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_encryption_response_decode() {
|
||||||
|
let mut cursor = Cursor::new(
|
||||||
|
include_bytes!("../../../test/packet/login/encryption_response.dat").to_vec(),
|
||||||
|
);
|
||||||
|
let encryption_response = EncryptionResponse::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
encryption_response.shared_secret,
|
||||||
|
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
);
|
||||||
|
assert_eq!(encryption_response.verify_token, vec![1, 2, 3, 4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_plugin_response_encode() {
|
||||||
|
let login_plugin_response = LoginPluginResponse {
|
||||||
|
message_id: 55,
|
||||||
|
successful: true,
|
||||||
|
data: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
login_plugin_response.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/login/login_plugin_response.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_plugin_response_decode() {
|
||||||
|
let mut cursor = Cursor::new(
|
||||||
|
include_bytes!("../../../test/packet/login/login_plugin_response.dat").to_vec(),
|
||||||
|
);
|
||||||
|
let login_plugin_response = LoginPluginResponse::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(login_plugin_response.message_id, 55);
|
||||||
|
assert!(login_plugin_response.successful);
|
||||||
|
assert_eq!(
|
||||||
|
login_plugin_response.data,
|
||||||
|
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_disconnect_encode() {
|
||||||
|
let login_disconnect = LoginDisconnect {
|
||||||
|
reason: Message::new(Payload::text("Message")),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
login_disconnect.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/login/login_disconnect.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_disconnect_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/login/login_disconnect.dat").to_vec());
|
||||||
|
let login_disconnect = LoginDisconnect::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
login_disconnect.reason,
|
||||||
|
Message::new(Payload::text("Message"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_encryption_request_encode() {
|
||||||
|
let encryption_request = EncryptionRequest {
|
||||||
|
server_id: String::from("ServerID"),
|
||||||
|
public_key: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
||||||
|
verify_token: vec![1, 2, 3, 4],
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
encryption_request.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/login/encryption_request.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_encryption_request_decode() {
|
||||||
|
let mut cursor = Cursor::new(
|
||||||
|
include_bytes!("../../../test/packet/login/encryption_request.dat").to_vec(),
|
||||||
|
);
|
||||||
|
let encryption_request = EncryptionRequest::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(encryption_request.server_id, String::from("ServerID"));
|
||||||
|
assert_eq!(
|
||||||
|
encryption_request.public_key,
|
||||||
|
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
);
|
||||||
|
assert_eq!(encryption_request.verify_token, vec![1, 2, 3, 4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_success_encode() {
|
||||||
|
let login_success = LoginSuccess {
|
||||||
|
uuid: Uuid::parse_str("35ee313b-d89a-41b8-b25e-d32e8aff0389").unwrap(),
|
||||||
|
username: String::from("Username"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
login_success.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/login/login_success.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_success_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/login/login_success.dat").to_vec());
|
||||||
|
let login_success = LoginSuccess::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(login_success.username, String::from("Username"));
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
login_success.uuid,
|
||||||
|
Uuid::parse_str("35ee313b-d89a-41b8-b25e-d32e8aff0389").unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_set_compression_encode() {
|
||||||
|
let set_compression = SetCompression { threshold: 1 };
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
set_compression.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/login/login_set_compression.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_set_compression_decode() {
|
||||||
|
let mut cursor = Cursor::new(
|
||||||
|
include_bytes!("../../../test/packet/login/login_set_compression.dat").to_vec(),
|
||||||
|
);
|
||||||
|
let set_compression = SetCompression::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(set_compression.threshold, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_plugin_request_encode() {
|
||||||
|
let login_plugin_request = LoginPluginRequest {
|
||||||
|
message_id: 55,
|
||||||
|
channel: String::from("Channel"),
|
||||||
|
data: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
login_plugin_request.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/login/login_plugin_request.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_login_plugin_request_decode() {
|
||||||
|
let mut cursor = Cursor::new(
|
||||||
|
include_bytes!("../../../test/packet/login/login_plugin_request.dat").to_vec(),
|
||||||
|
);
|
||||||
|
let login_plugin_request = LoginPluginRequest::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(login_plugin_request.message_id, 55);
|
||||||
|
assert_eq!(login_plugin_request.channel, String::from("Channel"));
|
||||||
|
assert_eq!(
|
||||||
|
login_plugin_request.data,
|
||||||
|
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
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;
|
200
protocol/src/version/v1_14_4/status.rs
Normal file
200
protocol/src/version/v1_14_4/status.rs
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
use crate::data::server_status::*;
|
||||||
|
use crate::decoder::Decoder;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use minecraft_protocol_derive::{Decoder, Encoder};
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
pub enum StatusServerBoundPacket {
|
||||||
|
StatusRequest,
|
||||||
|
PingRequest(PingRequest),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum StatusClientBoundPacket {
|
||||||
|
StatusResponse(StatusResponse),
|
||||||
|
PingResponse(PingResponse),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StatusServerBoundPacket {
|
||||||
|
pub fn get_type_id(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
StatusServerBoundPacket::StatusRequest => 0x00,
|
||||||
|
StatusServerBoundPacket::PingRequest(_) => 0x01,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
|
||||||
|
match type_id {
|
||||||
|
0x00 => Ok(StatusServerBoundPacket::StatusRequest),
|
||||||
|
0x01 => {
|
||||||
|
let ping_request = PingRequest::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(StatusServerBoundPacket::PingRequest(ping_request))
|
||||||
|
}
|
||||||
|
_ => Err(DecodeError::UnknownPacketType { type_id }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StatusClientBoundPacket {
|
||||||
|
pub fn get_type_id(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
StatusClientBoundPacket::StatusResponse(_) => 0x00,
|
||||||
|
StatusClientBoundPacket::PingResponse(_) => 0x01,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct PingRequest {
|
||||||
|
pub time: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PingRequest {
|
||||||
|
pub fn new(time: u64) -> StatusServerBoundPacket {
|
||||||
|
let ping_request = PingRequest { time };
|
||||||
|
|
||||||
|
StatusServerBoundPacket::PingRequest(ping_request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct PingResponse {
|
||||||
|
pub time: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PingResponse {
|
||||||
|
pub fn new(time: u64) -> StatusClientBoundPacket {
|
||||||
|
let ping_response = PingResponse { time };
|
||||||
|
|
||||||
|
StatusClientBoundPacket::PingResponse(ping_response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Encoder, Decoder, Debug)]
|
||||||
|
pub struct StatusResponse {
|
||||||
|
pub server_status: ServerStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StatusResponse {
|
||||||
|
pub fn new(server_status: ServerStatus) -> StatusClientBoundPacket {
|
||||||
|
let status_response = StatusResponse { server_status };
|
||||||
|
|
||||||
|
StatusClientBoundPacket::StatusResponse(status_response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::data::chat::{Message, Payload};
|
||||||
|
use crate::decoder::Decoder;
|
||||||
|
use crate::encoder::Encoder;
|
||||||
|
use crate::version::v1_14_4::status::*;
|
||||||
|
use std::io::Cursor;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ping_request_encode() {
|
||||||
|
let ping_request = PingRequest {
|
||||||
|
time: 1577735845610,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
ping_request.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/status/ping_request.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_status_ping_request_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/status/ping_request.dat").to_vec());
|
||||||
|
let ping_request = PingRequest::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(ping_request.time, 1577735845610);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ping_response_encode() {
|
||||||
|
let ping_response = PingResponse {
|
||||||
|
time: 1577735845610,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
ping_response.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/status/ping_response.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_status_ping_response_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/status/ping_response.dat").to_vec());
|
||||||
|
let ping_response = PingResponse::decode(&mut cursor).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(ping_response.time, 1577735845610);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_status_response_encode() {
|
||||||
|
let version = ServerVersion {
|
||||||
|
name: String::from("1.15.1"),
|
||||||
|
protocol: 575,
|
||||||
|
};
|
||||||
|
|
||||||
|
let player = OnlinePlayer {
|
||||||
|
id: Uuid::parse_str("2a1e1912-7103-4add-80fc-91ebc346cbce").unwrap(),
|
||||||
|
name: String::from("Username"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let players = OnlinePlayers {
|
||||||
|
online: 10,
|
||||||
|
max: 100,
|
||||||
|
sample: vec![player],
|
||||||
|
};
|
||||||
|
|
||||||
|
let server_status = ServerStatus {
|
||||||
|
version,
|
||||||
|
description: Message::new(Payload::text("Description")),
|
||||||
|
players,
|
||||||
|
};
|
||||||
|
|
||||||
|
let status_response = StatusResponse { server_status };
|
||||||
|
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
status_response.encode(&mut vec).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vec,
|
||||||
|
include_bytes!("../../../test/packet/status/status_response.dat").to_vec()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_status_response_decode() {
|
||||||
|
let mut cursor =
|
||||||
|
Cursor::new(include_bytes!("../../../test/packet/status/status_response.dat").to_vec());
|
||||||
|
let status_response = StatusResponse::decode(&mut cursor).unwrap();
|
||||||
|
let server_status = status_response.server_status;
|
||||||
|
|
||||||
|
let player = OnlinePlayer {
|
||||||
|
id: Uuid::parse_str("2a1e1912-7103-4add-80fc-91ebc346cbce").unwrap(),
|
||||||
|
name: String::from("Username"),
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(server_status.version.name, String::from("1.15.1"));
|
||||||
|
assert_eq!(server_status.version.protocol, 575);
|
||||||
|
assert_eq!(server_status.players.max, 100);
|
||||||
|
assert_eq!(server_status.players.online, 10);
|
||||||
|
assert_eq!(server_status.players.sample, vec![player]);
|
||||||
|
assert_eq!(
|
||||||
|
server_status.description,
|
||||||
|
Message::new(Payload::text("Description"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
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>ܢ
|
BIN
protocol/test/packet/game/chunk_data.dat
Normal file
BIN
protocol/test/packet/game/chunk_data.dat
Normal file
Binary file not shown.
1
protocol/test/packet/game/client_bound_chat_message.dat
Normal file
1
protocol/test/packet/game/client_bound_chat_message.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"text":"hello client!"}
|
BIN
protocol/test/packet/game/client_bound_keep_alive.dat
Normal file
BIN
protocol/test/packet/game/client_bound_keep_alive.dat
Normal file
Binary file not shown.
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>
|
1
protocol/test/packet/game/game_disconnect.dat
Normal file
1
protocol/test/packet/game/game_disconnect.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"text":"Message"}
|
BIN
protocol/test/packet/game/join_game.dat
Normal file
BIN
protocol/test/packet/game/join_game.dat
Normal file
Binary file not shown.
1
protocol/test/packet/game/server_bound_chat_message.dat
Normal file
1
protocol/test/packet/game/server_bound_chat_message.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
hello server!
|
BIN
protocol/test/packet/game/server_bound_keep_alive.dat
Normal file
BIN
protocol/test/packet/game/server_bound_keep_alive.dat
Normal file
Binary file not shown.
3
protocol/test/packet/login/encryption_request.dat
Normal file
3
protocol/test/packet/login/encryption_request.dat
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
ServerID
|
||||||
|
|
||||||
|
|
3
protocol/test/packet/login/encryption_response.dat
Normal file
3
protocol/test/packet/login/encryption_response.dat
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
1
protocol/test/packet/login/login_disconnect.dat
Normal file
1
protocol/test/packet/login/login_disconnect.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"text":"Message"}
|
1
protocol/test/packet/login/login_plugin_request.dat
Normal file
1
protocol/test/packet/login/login_plugin_request.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
7Channel
|
1
protocol/test/packet/login/login_plugin_response.dat
Normal file
1
protocol/test/packet/login/login_plugin_response.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
7
|
1
protocol/test/packet/login/login_set_compression.dat
Normal file
1
protocol/test/packet/login/login_set_compression.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
1
protocol/test/packet/login/login_start.dat
Normal file
1
protocol/test/packet/login/login_start.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
Username
|
1
protocol/test/packet/login/login_success.dat
Normal file
1
protocol/test/packet/login/login_success.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
$35ee313b-d89a-41b8-b25e-d32e8aff0389Username
|
BIN
protocol/test/packet/status/ping_request.dat
Normal file
BIN
protocol/test/packet/status/ping_request.dat
Normal file
Binary file not shown.
BIN
protocol/test/packet/status/ping_response.dat
Normal file
BIN
protocol/test/packet/status/ping_response.dat
Normal file
Binary file not shown.
1
protocol/test/packet/status/status_response.dat
Normal file
1
protocol/test/packet/status/status_response.dat
Normal file
@ -0,0 +1 @@
|
|||||||
|
¾{"version":{"name":"1.15.1","protocol":575},"players":{"max":100,"online":10,"sample":[{"name":"Username","id":"2a1e1912-7103-4add-80fc-91ebc346cbce"}]},"description":{"text":"Description"}}
|
Loading…
x
Reference in New Issue
Block a user