Compare commits
3 Commits
master
...
protodef-g
Author | SHA1 | Date | |
---|---|---|---|
|
2d8174678b | ||
|
5a2b7e81fa | ||
|
e7eace3ef5 |
@ -3,4 +3,5 @@
|
|||||||
members = [
|
members = [
|
||||||
"protocol",
|
"protocol",
|
||||||
"protocol-derive",
|
"protocol-derive",
|
||||||
|
"protocol-generator",
|
||||||
]
|
]
|
@ -16,3 +16,7 @@ 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"
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
|
|
||||||
use proc_macro::TokenStream as TokenStream1;
|
use proc_macro::TokenStream as TokenStream1;
|
||||||
use proc_macro2::Ident;
|
|
||||||
use proc_macro2::TokenStream as TokenStream2;
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
|
use proc_macro2::{Ident, Span};
|
||||||
use quote::{quote, TokenStreamExt};
|
use quote::{quote, TokenStreamExt};
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
use syn::export::Span;
|
use syn::{parse_macro_input, Data, DeriveInput, Field, Fields, Lit, Meta, NestedMeta, Type};
|
||||||
use syn::{parse_macro_input, Data, DeriveInput, Field, Fields, Lit, Meta, NestedMeta};
|
|
||||||
|
|
||||||
#[proc_macro_derive(Packet, attributes(packet))]
|
#[proc_macro_derive(Packet, attributes(packet))]
|
||||||
pub fn derive_packet(input: proc_macro::TokenStream) -> TokenStream1 {
|
pub fn derive_packet(input: proc_macro::TokenStream) -> TokenStream1 {
|
||||||
@ -20,6 +19,8 @@ pub fn derive_packet(input: proc_macro::TokenStream) -> TokenStream1 {
|
|||||||
let encoder = impl_encoder_trait(name, fields);
|
let encoder = impl_encoder_trait(name, fields);
|
||||||
let decoder = impl_decoder_trait(name, fields);
|
let decoder = impl_decoder_trait(name, fields);
|
||||||
|
|
||||||
|
println!("{}", decoder);
|
||||||
|
|
||||||
TokenStream1::from(quote! {
|
TokenStream1::from(quote! {
|
||||||
#encoder
|
#encoder
|
||||||
|
|
||||||
@ -65,6 +66,10 @@ fn impl_encoder_trait(name: &Ident, fields: &Fields) -> TokenStream2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn impl_decoder_trait(name: &Ident, fields: &Fields) -> TokenStream2 {
|
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 decode = quote_field(fields, |field| {
|
||||||
let name = &field.ident;
|
let name = &field.ident;
|
||||||
let ty = &field.ty;
|
let ty = &field.ty;
|
||||||
@ -72,27 +77,57 @@ fn impl_decoder_trait(name: &Ident, fields: &Fields) -> TokenStream2 {
|
|||||||
let unparsed_meta = get_packet_field_meta(field);
|
let unparsed_meta = get_packet_field_meta(field);
|
||||||
let parsed_meta = parse_packet_field_meta(&unparsed_meta);
|
let parsed_meta = parse_packet_field_meta(&unparsed_meta);
|
||||||
|
|
||||||
// This is special case because max length are used only for strings.
|
let mut result = quote!();
|
||||||
if let Some(max_length) = parsed_meta.max_length {
|
|
||||||
return quote! {
|
// When we encounter a bitfield, we skip writing until we get
|
||||||
let #name = crate::DecoderReadExt::read_string(reader, #max_length)?;
|
// 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!()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
match parsed_meta.module {
|
// This is special case because max length are used only for strings.
|
||||||
Some(module) => {
|
if let Some(max_length) = parsed_meta.max_length {
|
||||||
let module_ident = Ident::new(&module, Span::call_site());
|
result.append_all(quote! {
|
||||||
|
let #name = crate::DecoderReadExt::read_string(reader, #max_length)?;
|
||||||
|
});
|
||||||
|
|
||||||
quote! {
|
return result;
|
||||||
let #name = crate::#module_ident::decode(reader)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 => {
|
None => {
|
||||||
quote! {
|
result.append_all(quote! {
|
||||||
let #name = <#ty as crate::Decoder>::decode(reader)?;
|
let #name = <#ty as crate::Decoder>::decode(reader)?;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return result;
|
||||||
});
|
});
|
||||||
|
|
||||||
let create = quote_field(fields, |field| {
|
let create = quote_field(fields, |field| {
|
||||||
@ -123,11 +158,148 @@ fn impl_decoder_trait(name: &Ident, fields: &Fields) -> TokenStream2 {
|
|||||||
struct PacketFieldMeta {
|
struct PacketFieldMeta {
|
||||||
module: Option<String>,
|
module: Option<String>,
|
||||||
max_length: Option<u16>,
|
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 {
|
fn parse_packet_field_meta(meta_list: &Vec<NestedMeta>) -> PacketFieldMeta {
|
||||||
let mut module = None;
|
let mut module = None;
|
||||||
let mut max_length = None;
|
let mut max_length = None;
|
||||||
|
let mut bitfield = None;
|
||||||
|
|
||||||
for meta in meta_list {
|
for meta in meta_list {
|
||||||
match meta {
|
match meta {
|
||||||
@ -148,14 +320,46 @@ fn parse_packet_field_meta(meta_list: &Vec<NestedMeta>) -> PacketFieldMeta {
|
|||||||
},
|
},
|
||||||
path => panic!(
|
path => panic!(
|
||||||
"Received unrecognized attribute : \"{}\"",
|
"Received unrecognized attribute : \"{}\"",
|
||||||
path.get_ident().unwrap()
|
path.get_ident().expect("Failed to get ident")
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
_ => panic!("Expected only named meta values"),
|
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"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PacketFieldMeta { module, max_length }
|
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> {
|
fn get_packet_field_meta(field: &Field) -> Vec<NestedMeta> {
|
||||||
@ -172,7 +376,7 @@ fn get_packet_field_meta(field: &Field) -> Vec<NestedMeta> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn quote_field<F: Fn(&Field) -> TokenStream2>(fields: &Fields, func: F) -> TokenStream2 {
|
fn quote_field<F: FnMut(&Field) -> TokenStream2>(fields: &Fields, mut func: F) -> TokenStream2 {
|
||||||
let mut output = quote!();
|
let mut output = quote!();
|
||||||
|
|
||||||
match fields {
|
match fields {
|
||||||
|
60
protocol-derive/tests/bitfield_abilities.rs
Normal file
60
protocol-derive/tests/bitfield_abilities.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#[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);
|
||||||
|
}
|
||||||
|
}
|
107
protocol-derive/tests/bitfield_half.rs
Normal file
107
protocol-derive/tests/bitfield_half.rs
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
#[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);
|
||||||
|
}
|
||||||
|
}
|
54
protocol-derive/tests/bitfield_position.rs
Normal file
54
protocol-derive/tests/bitfield_position.rs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#[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);
|
||||||
|
}
|
||||||
|
}
|
17
protocol-generator/Cargo.toml
Normal file
17
protocol-generator/Cargo.toml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
[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"
|
12
protocol-generator/src/error.rs
Normal file
12
protocol-generator/src/error.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
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 }
|
||||||
|
}
|
||||||
|
}
|
138
protocol-generator/src/frontend.rs
Normal file
138
protocol-generator/src/frontend.rs
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
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"));
|
||||||
|
}
|
||||||
|
}
|
5
protocol-generator/src/main.rs
Normal file
5
protocol-generator/src/main.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
mod error;
|
||||||
|
mod frontend;
|
||||||
|
mod templates;
|
||||||
|
|
||||||
|
pub fn main() {}
|
87
protocol-generator/src/templates.rs
Normal file
87
protocol-generator/src/templates.rs
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
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(())
|
||||||
|
}
|
19
protocol-generator/templates/packet_struct.hbs
Normal file
19
protocol-generator/templates/packet_struct.hbs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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}}
|
||||||
|
}
|
27
protocol-generator/test/packet_struct.txt
Normal file
27
protocol-generator/test/packet_struct.txt
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
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,
|
||||||
|
}
|
@ -6,7 +6,7 @@
|
|||||||
//! ## Serialize
|
//! ## Serialize
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use minecraft_protocol::chat::{Payload, Color, MessageBuilder};
|
//! use minecraft_protocol::data::chat::{MessageBuilder, Payload, Color};
|
||||||
//!
|
//!
|
||||||
//! let message = MessageBuilder::builder(Payload::text("Hello"))
|
//! let message = MessageBuilder::builder(Payload::text("Hello"))
|
||||||
//! .color(Color::Yellow)
|
//! .color(Color::Yellow)
|
||||||
@ -25,7 +25,7 @@
|
|||||||
//! ## Deserialize
|
//! ## Deserialize
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use minecraft_protocol::chat::{MessageBuilder, Color, Payload, Message};
|
//! use minecraft_protocol::data::chat::{MessageBuilder, Color, Payload, Message};
|
||||||
//!
|
//!
|
||||||
//! let json = r#"
|
//! let json = r#"
|
||||||
//! {
|
//! {
|
||||||
@ -61,7 +61,6 @@
|
|||||||
//! 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,
|
||||||
@ -93,7 +92,7 @@ pub enum Color {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use minecraft_protocol::chat::Color;
|
/// use minecraft_protocol::data::chat::Color;
|
||||||
///
|
///
|
||||||
/// let color = Color::Hex("#f98aff".into());
|
/// let color = Color::Hex("#f98aff".into());
|
||||||
/// ```
|
/// ```
|
||||||
@ -333,8 +332,6 @@ impl Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_json_encoder_decoder!(Message);
|
|
||||||
|
|
||||||
pub struct MessageBuilder {
|
pub struct MessageBuilder {
|
||||||
current: Message,
|
current: Message,
|
||||||
root: Option<Message>,
|
root: Option<Message>,
|
||||||
@ -429,8 +426,12 @@ impl MessageBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_serialize_text_hello_world() {
|
mod tests {
|
||||||
|
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)
|
||||||
@ -446,10 +447,10 @@ fn test_serialize_text_hello_world() {
|
|||||||
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)
|
||||||
@ -465,10 +466,10 @@ fn test_deserialize_text_hello_world() {
|
|||||||
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));
|
||||||
|
|
||||||
@ -476,10 +477,10 @@ fn test_serialize_translate_opped_steve() {
|
|||||||
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));
|
||||||
|
|
||||||
@ -487,10 +488,10 @@ fn test_deserialize_translate_opped_steve() {
|
|||||||
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)
|
||||||
@ -505,10 +506,10 @@ fn test_serialize_keybind_jump() {
|
|||||||
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)
|
||||||
@ -523,10 +524,10 @@ fn test_deserialize_keybind_jump() {
|
|||||||
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)
|
||||||
@ -537,10 +538,10 @@ fn test_serialize_click_open_url() {
|
|||||||
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)
|
||||||
@ -551,10 +552,10 @@ fn test_deserialize_click_open_url() {
|
|||||||
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)
|
||||||
@ -565,10 +566,10 @@ fn test_serialize_click_run_command() {
|
|||||||
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)
|
||||||
@ -579,10 +580,10 @@ fn test_deserialize_click_run_command() {
|
|||||||
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)
|
||||||
@ -593,10 +594,10 @@ fn test_serialize_click_suggest_command() {
|
|||||||
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)
|
||||||
@ -607,10 +608,10 @@ fn test_deserialize_click_suggest_command() {
|
|||||||
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)
|
||||||
@ -621,10 +622,10 @@ fn test_serialize_click_change_page() {
|
|||||||
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)
|
||||||
@ -635,10 +636,10 @@ fn test_deserialize_click_change_page() {
|
|||||||
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)
|
||||||
@ -649,10 +650,10 @@ fn test_serialize_hover_show_text() {
|
|||||||
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)
|
||||||
@ -663,10 +664,10 @@ fn test_deserialize_hover_show_text() {
|
|||||||
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)
|
||||||
@ -677,10 +678,10 @@ fn test_serialize_hover_show_item() {
|
|||||||
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)
|
||||||
@ -691,10 +692,10 @@ fn test_deserialize_hover_show_item() {
|
|||||||
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)
|
||||||
@ -705,10 +706,10 @@ fn test_serialize_hover_show_entity() {
|
|||||||
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)
|
||||||
@ -719,10 +720,10 @@ fn test_deserialize_hover_show_entity() {
|
|||||||
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();
|
||||||
@ -731,10 +732,10 @@ fn test_serialize_hex_color() {
|
|||||||
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();
|
||||||
@ -743,4 +744,5 @@ fn test_deserialize_hex_color() {
|
|||||||
Message::from_json(include_str!("../test/chat/hex_color.json")).unwrap(),
|
Message::from_json(include_str!("../test/chat/hex_color.json")).unwrap(),
|
||||||
expected_message
|
expected_message
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
2
protocol/src/data/mod.rs
Normal file
2
protocol/src/data/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod chat;
|
||||||
|
pub mod server_status;
|
29
protocol/src/data/server_status.rs
Normal file
29
protocol/src/data/server_status.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
use crate::data::chat::Message;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct ServerStatus {
|
||||||
|
pub version: ServerVersion,
|
||||||
|
pub players: OnlinePlayers,
|
||||||
|
pub description: Message,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct ServerVersion {
|
||||||
|
pub name: String,
|
||||||
|
pub protocol: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct OnlinePlayers {
|
||||||
|
pub max: u32,
|
||||||
|
pub online: u32,
|
||||||
|
pub sample: Vec<OnlinePlayer>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||||
|
pub struct OnlinePlayer {
|
||||||
|
pub name: String,
|
||||||
|
pub id: Uuid,
|
||||||
|
}
|
285
protocol/src/decoder.rs
Normal file
285
protocol/src/decoder.rs
Normal file
@ -0,0 +1,285 @@
|
|||||||
|
use crate::error::DecodeError;
|
||||||
|
use byteorder::{BigEndian, ReadBytesExt};
|
||||||
|
use nbt::CompoundTag;
|
||||||
|
use num_traits::FromPrimitive;
|
||||||
|
use std::io::Read;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub trait Decoder {
|
||||||
|
type Output;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait adds additional helper methods for `Read` to read protocol data.
|
||||||
|
trait DecoderReadExt {
|
||||||
|
fn read_bool(&mut self) -> Result<bool, DecodeError>;
|
||||||
|
|
||||||
|
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError>;
|
||||||
|
|
||||||
|
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError>;
|
||||||
|
|
||||||
|
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError>;
|
||||||
|
|
||||||
|
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError>;
|
||||||
|
|
||||||
|
fn read_var_i32(&mut self) -> Result<i32, DecodeError>;
|
||||||
|
|
||||||
|
fn read_var_i64(&mut self) -> Result<i64, DecodeError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! read_signed_var_int (
|
||||||
|
($type: ident, $name: ident, $max_bytes: expr) => (
|
||||||
|
fn $name(&mut self) -> Result<$type, DecodeError> {
|
||||||
|
let mut bytes = 0;
|
||||||
|
let mut output = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let byte = self.read_u8()?;
|
||||||
|
let value = (byte & 0b01111111) as $type;
|
||||||
|
|
||||||
|
output |= value << 7 * bytes;
|
||||||
|
bytes += 1;
|
||||||
|
|
||||||
|
if bytes > $max_bytes {
|
||||||
|
return Err(DecodeError::VarIntTooLong { max_bytes: $max_bytes })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byte & 0b10000000) == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
impl<R: Read> DecoderReadExt for R {
|
||||||
|
fn read_bool(&mut self) -> Result<bool, DecodeError> {
|
||||||
|
match self.read_u8()? {
|
||||||
|
0 => Ok(false),
|
||||||
|
1 => Ok(true),
|
||||||
|
_ => Err(DecodeError::NonBoolValue),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError> {
|
||||||
|
let length = self.read_var_i32()? as usize;
|
||||||
|
|
||||||
|
if length as u16 > max_length {
|
||||||
|
return Err(DecodeError::StringTooLong { length, max_length });
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut buf = vec![0; length as usize];
|
||||||
|
self.read_exact(&mut buf)?;
|
||||||
|
|
||||||
|
Ok(String::from_utf8(buf)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError> {
|
||||||
|
let length = self.read_var_i32()?;
|
||||||
|
|
||||||
|
let mut buf = vec![0; length as usize];
|
||||||
|
self.read_exact(&mut buf)?;
|
||||||
|
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError> {
|
||||||
|
let type_id = self.read_u8()?;
|
||||||
|
let result = FromPrimitive::from_u8(type_id);
|
||||||
|
|
||||||
|
result.ok_or_else(|| DecodeError::UnknownEnumType { type_id })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError> {
|
||||||
|
Ok(nbt::decode::read_compound_tag(self)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
read_signed_var_int!(i32, read_var_i32, 5);
|
||||||
|
read_signed_var_int!(i64, read_var_i64, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for u8 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_u8()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for i16 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_i16::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for i32 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_i32::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for u16 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_u16::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for u32 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_u32::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for i64 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_i64::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for u64 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_u64::<BigEndian>()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for String {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_string(32_768)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for bool {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_bool()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for Vec<u8> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_byte_array()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for Uuid {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
let mut buf = [0; 16];
|
||||||
|
reader.read_exact(&mut buf)?;
|
||||||
|
|
||||||
|
Ok(Uuid::from_bytes(buf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for CompoundTag {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
Ok(reader.read_compound_tag()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for Vec<CompoundTag> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||||
|
let length = reader.read_var_i32()? as usize;
|
||||||
|
let mut vec = Vec::with_capacity(length);
|
||||||
|
|
||||||
|
for _ in 0..length {
|
||||||
|
let compound_tag = reader.read_compound_tag()?;
|
||||||
|
vec.push(compound_tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(vec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod var_int {
|
||||||
|
use crate::decoder::DecoderReadExt;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(reader: &mut R) -> Result<i32, DecodeError> {
|
||||||
|
Ok(reader.read_var_i32()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod var_long {
|
||||||
|
use crate::decoder::DecoderReadExt;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(reader: &mut R) -> Result<i64, DecodeError> {
|
||||||
|
Ok(reader.read_var_i64()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod rest {
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(reader: &mut R) -> Result<Vec<u8>, DecodeError> {
|
||||||
|
let mut data = Vec::new();
|
||||||
|
reader.read_to_end(data.as_mut())?;
|
||||||
|
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod uuid_hyp_str {
|
||||||
|
use crate::decoder::DecoderReadExt;
|
||||||
|
use crate::error::DecodeError;
|
||||||
|
use std::io::Read;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub fn decode<R: Read>(reader: &mut R) -> Result<Uuid, DecodeError> {
|
||||||
|
let uuid_hyphenated_string = reader.read_string(36)?;
|
||||||
|
let uuid = Uuid::parse_str(&uuid_hyphenated_string)?;
|
||||||
|
|
||||||
|
Ok(uuid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::decoder::DecoderReadExt;
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_variable_i32_2_bytes_value() {
|
||||||
|
let mut cursor = Cursor::new(vec![0b10101100, 0b00000010]);
|
||||||
|
let value = cursor.read_var_i32().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(value, 300);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_variable_i32_5_bytes_value() {
|
||||||
|
let mut cursor = Cursor::new(vec![0xff, 0xff, 0xff, 0xff, 0x07]);
|
||||||
|
let value = cursor.read_var_i32().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(value, 2147483647);
|
||||||
|
}
|
||||||
|
}
|
253
protocol/src/encoder.rs
Normal file
253
protocol/src/encoder.rs
Normal file
@ -0,0 +1,253 @@
|
|||||||
|
use crate::error::EncodeError;
|
||||||
|
use byteorder::{BigEndian, WriteBytesExt};
|
||||||
|
use nbt::CompoundTag;
|
||||||
|
use num_traits::ToPrimitive;
|
||||||
|
use std::io::Write;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub trait Encoder {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait adds additional helper methods for `Write` to write protocol data.
|
||||||
|
trait EncoderWriteExt {
|
||||||
|
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_enum<T: ToPrimitive>(&mut self, value: &T) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_var_i32(&mut self, value: i32) -> Result<(), EncodeError>;
|
||||||
|
|
||||||
|
fn write_var_i64(&mut self, value: i64) -> Result<(), EncodeError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! write_signed_var_int (
|
||||||
|
($type: ident, $name: ident) => (
|
||||||
|
fn $name(&mut self, mut value: $type) -> Result<(), EncodeError> {
|
||||||
|
loop {
|
||||||
|
let mut byte = (value & 0b01111111) as u8;
|
||||||
|
value = value >> 7;
|
||||||
|
|
||||||
|
if value != 0 {
|
||||||
|
byte |= 0b10000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.write_u8(byte)?;
|
||||||
|
|
||||||
|
if value == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
impl<W: Write> EncoderWriteExt for W {
|
||||||
|
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError> {
|
||||||
|
if value {
|
||||||
|
self.write_u8(1)?;
|
||||||
|
} else {
|
||||||
|
self.write_u8(0)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError> {
|
||||||
|
let length = value.len();
|
||||||
|
|
||||||
|
if length > max_length as usize {
|
||||||
|
return Err(EncodeError::StringTooLong { length, max_length });
|
||||||
|
}
|
||||||
|
|
||||||
|
self.write_var_i32(value.len() as i32)?;
|
||||||
|
self.write_all(value.as_bytes())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError> {
|
||||||
|
self.write_var_i32(value.len() as i32)?;
|
||||||
|
self.write_all(value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_enum<T: ToPrimitive>(&mut self, value: &T) -> Result<(), EncodeError> {
|
||||||
|
let type_value = ToPrimitive::to_u8(value).unwrap();
|
||||||
|
self.write_u8(type_value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError> {
|
||||||
|
nbt::encode::write_compound_tag(self, value.clone())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
write_signed_var_int!(i32, write_var_i32);
|
||||||
|
write_signed_var_int!(i64, write_var_i64);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for u8 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_u8(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for i16 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_i16::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for i32 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_i32::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for u16 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_u16::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for u32 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_u32::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for i64 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_i64::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for u64 {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_u64::<BigEndian>(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for String {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_string(self, 32_768)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for bool {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_bool(*self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for Vec<u8> {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_byte_array(self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for Uuid {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_all(self.as_bytes())?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for CompoundTag {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
Ok(writer.write_compound_tag(self)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encoder for Vec<CompoundTag> {
|
||||||
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
writer.write_var_i32(self.len() as i32)?;
|
||||||
|
|
||||||
|
for compound_tag in self {
|
||||||
|
writer.write_compound_tag(&compound_tag)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod var_int {
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use crate::error::EncodeError;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
pub fn encode<W: Write>(value: &i32, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
writer.write_var_i32(*value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod var_long {
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use crate::error::EncodeError;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
pub fn encode<W: Write>(value: &i64, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
writer.write_var_i64(*value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod rest {
|
||||||
|
use crate::error::EncodeError;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
pub fn encode<W: Write>(value: &[u8], writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
writer.write_all(value)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod uuid_hyp_str {
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use crate::error::EncodeError;
|
||||||
|
use std::io::Write;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub fn encode<W: Write>(value: &Uuid, writer: &mut W) -> Result<(), EncodeError> {
|
||||||
|
let uuid_hyphenated_string = value.to_hyphenated().to_string();
|
||||||
|
writer.write_string(&uuid_hyphenated_string, 36)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::encoder::EncoderWriteExt;
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_write_variable_i32_2_bytes_value() {
|
||||||
|
let mut cursor = Cursor::new(Vec::with_capacity(5));
|
||||||
|
cursor.write_var_i32(300).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.into_inner(), vec![0b10101100, 0b00000010]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_write_variable_i32_5_bytes_value() {
|
||||||
|
let mut cursor = Cursor::new(Vec::with_capacity(5));
|
||||||
|
cursor.write_var_i32(2147483647).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.into_inner(), vec![0xff, 0xff, 0xff, 0xff, 0x07]);
|
||||||
|
}
|
||||||
|
}
|
106
protocol/src/error.rs
Normal file
106
protocol/src/error.rs
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
use nbt::decode::TagDecodeError;
|
||||||
|
use serde_json::error::Error as JsonError;
|
||||||
|
use std::io::Error as IoError;
|
||||||
|
use std::string::FromUtf8Error;
|
||||||
|
use uuid::parser::ParseError as UuidParseError;
|
||||||
|
|
||||||
|
/// Possible errors while encoding packet.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum EncodeError {
|
||||||
|
/// String length can't be more than provided value.
|
||||||
|
StringTooLong {
|
||||||
|
/// String length.
|
||||||
|
length: usize,
|
||||||
|
/// Max string length.
|
||||||
|
max_length: u16,
|
||||||
|
},
|
||||||
|
IOError {
|
||||||
|
io_error: IoError,
|
||||||
|
},
|
||||||
|
JsonError {
|
||||||
|
json_error: JsonError,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<IoError> for EncodeError {
|
||||||
|
fn from(io_error: IoError) -> Self {
|
||||||
|
EncodeError::IOError { io_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<JsonError> for EncodeError {
|
||||||
|
fn from(json_error: JsonError) -> Self {
|
||||||
|
EncodeError::JsonError { json_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Possible errors while decoding packet.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum DecodeError {
|
||||||
|
/// Packet was not recognized. Invalid data or wrong protocol version.
|
||||||
|
UnknownPacketType {
|
||||||
|
type_id: u8,
|
||||||
|
},
|
||||||
|
/// String length can't be more than provided value.
|
||||||
|
StringTooLong {
|
||||||
|
/// String length.
|
||||||
|
length: usize,
|
||||||
|
/// Max string length.
|
||||||
|
max_length: u16,
|
||||||
|
},
|
||||||
|
IOError {
|
||||||
|
io_error: IoError,
|
||||||
|
},
|
||||||
|
JsonError {
|
||||||
|
json_error: JsonError,
|
||||||
|
},
|
||||||
|
/// Byte array was not recognized as valid UTF-8 string.
|
||||||
|
Utf8Error {
|
||||||
|
utf8_error: FromUtf8Error,
|
||||||
|
},
|
||||||
|
/// Boolean are parsed from byte. Valid byte value are 0 or 1.
|
||||||
|
NonBoolValue,
|
||||||
|
UuidParseError {
|
||||||
|
uuid_parse_error: UuidParseError,
|
||||||
|
},
|
||||||
|
/// Type id was not parsed as valid enum value.
|
||||||
|
UnknownEnumType {
|
||||||
|
type_id: u8,
|
||||||
|
},
|
||||||
|
TagDecodeError {
|
||||||
|
tag_decode_error: TagDecodeError,
|
||||||
|
},
|
||||||
|
VarIntTooLong {
|
||||||
|
max_bytes: usize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<IoError> for DecodeError {
|
||||||
|
fn from(io_error: IoError) -> Self {
|
||||||
|
DecodeError::IOError { io_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<JsonError> for DecodeError {
|
||||||
|
fn from(json_error: JsonError) -> Self {
|
||||||
|
DecodeError::JsonError { json_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<FromUtf8Error> for DecodeError {
|
||||||
|
fn from(utf8_error: FromUtf8Error) -> Self {
|
||||||
|
DecodeError::Utf8Error { utf8_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<UuidParseError> for DecodeError {
|
||||||
|
fn from(uuid_parse_error: UuidParseError) -> Self {
|
||||||
|
DecodeError::UuidParseError { uuid_parse_error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TagDecodeError> for DecodeError {
|
||||||
|
fn from(tag_decode_error: TagDecodeError) -> Self {
|
||||||
|
DecodeError::TagDecodeError { tag_decode_error }
|
||||||
|
}
|
||||||
|
}
|
@ -1,504 +0,0 @@
|
|||||||
use num_derive::{FromPrimitive, ToPrimitive};
|
|
||||||
|
|
||||||
use crate::chat::Message;
|
|
||||||
use crate::impl_enum_encoder_decoder;
|
|
||||||
use crate::DecodeError;
|
|
||||||
use crate::Decoder;
|
|
||||||
use minecraft_protocol_derive::Packet;
|
|
||||||
use nbt::CompoundTag;
|
|
||||||
use std::io::Read;
|
|
||||||
|
|
||||||
pub enum GameServerBoundPacket {
|
|
||||||
ServerBoundChatMessage(ServerBoundChatMessage),
|
|
||||||
ServerBoundKeepAlive(ServerBoundKeepAlive),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum GameClientBoundPacket {
|
|
||||||
ClientBoundChatMessage(ClientBoundChatMessage),
|
|
||||||
JoinGame(JoinGame),
|
|
||||||
ClientBoundKeepAlive(ClientBoundKeepAlive),
|
|
||||||
ChunkData(ChunkData),
|
|
||||||
GameDisconnect(GameDisconnect),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GameServerBoundPacket {
|
|
||||||
pub fn get_type_id(&self) -> u8 {
|
|
||||||
match self {
|
|
||||||
GameServerBoundPacket::ServerBoundChatMessage(_) => 0x03,
|
|
||||||
GameServerBoundPacket::ServerBoundKeepAlive(_) => 0x0F,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(Packet, Debug)]
|
|
||||||
pub struct ServerBoundChatMessage {
|
|
||||||
#[packet(max_length = 256)]
|
|
||||||
pub message: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ServerBoundChatMessage {
|
|
||||||
pub fn new(message: String) -> GameServerBoundPacket {
|
|
||||||
let chat_message = ServerBoundChatMessage { message };
|
|
||||||
|
|
||||||
GameServerBoundPacket::ServerBoundChatMessage(chat_message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
|
||||||
pub struct ClientBoundChatMessage {
|
|
||||||
pub message: Message,
|
|
||||||
pub position: MessagePosition,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, FromPrimitive, ToPrimitive)]
|
|
||||||
pub enum MessagePosition {
|
|
||||||
Chat,
|
|
||||||
System,
|
|
||||||
HotBar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_enum_encoder_decoder!(MessagePosition);
|
|
||||||
|
|
||||||
impl ClientBoundChatMessage {
|
|
||||||
pub fn new(message: Message, position: MessagePosition) -> GameClientBoundPacket {
|
|
||||||
let chat_message = ClientBoundChatMessage { message, position };
|
|
||||||
|
|
||||||
GameClientBoundPacket::ClientBoundChatMessage(chat_message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
|
||||||
pub struct JoinGame {
|
|
||||||
pub entity_id: u32,
|
|
||||||
pub game_mode: GameMode,
|
|
||||||
pub dimension: i32,
|
|
||||||
pub max_players: u8,
|
|
||||||
#[packet(max_length = 16)]
|
|
||||||
pub level_type: String,
|
|
||||||
#[packet(with = "var_int")]
|
|
||||||
pub view_distance: i32,
|
|
||||||
pub reduced_debug_info: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, FromPrimitive, ToPrimitive)]
|
|
||||||
pub enum GameMode {
|
|
||||||
Survival = 0,
|
|
||||||
Creative = 1,
|
|
||||||
Adventure = 2,
|
|
||||||
Spectator = 3,
|
|
||||||
Hardcore = 8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_enum_encoder_decoder!(GameMode);
|
|
||||||
|
|
||||||
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(Packet)]
|
|
||||||
pub struct ServerBoundKeepAlive {
|
|
||||||
pub id: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ServerBoundKeepAlive {
|
|
||||||
pub fn new(id: u64) -> GameServerBoundPacket {
|
|
||||||
let keep_alive = ServerBoundKeepAlive { id };
|
|
||||||
|
|
||||||
GameServerBoundPacket::ServerBoundKeepAlive(keep_alive)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Packet)]
|
|
||||||
pub struct ClientBoundKeepAlive {
|
|
||||||
pub id: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ClientBoundKeepAlive {
|
|
||||||
pub fn new(id: u64) -> GameClientBoundPacket {
|
|
||||||
let keep_alive = ClientBoundKeepAlive { id };
|
|
||||||
|
|
||||||
GameClientBoundPacket::ClientBoundKeepAlive(keep_alive)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
|
||||||
pub struct ChunkData {
|
|
||||||
pub x: i32,
|
|
||||||
pub z: i32,
|
|
||||||
pub full: bool,
|
|
||||||
#[packet(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(Packet, Debug)]
|
|
||||||
pub struct GameDisconnect {
|
|
||||||
pub reason: Message,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GameDisconnect {
|
|
||||||
pub fn new(reason: Message) -> GameClientBoundPacket {
|
|
||||||
let game_disconnect = GameDisconnect { reason };
|
|
||||||
|
|
||||||
GameClientBoundPacket::GameDisconnect(game_disconnect)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::chat::{Message, Payload};
|
|
||||||
use crate::game::{
|
|
||||||
ChunkData, ClientBoundChatMessage, ClientBoundKeepAlive, GameDisconnect, GameMode,
|
|
||||||
JoinGame, MessagePosition, ServerBoundChatMessage, ServerBoundKeepAlive,
|
|
||||||
};
|
|
||||||
use crate::{DecodeError, Encoder, EncoderWriteExt, STRING_MAX_LENGTH};
|
|
||||||
use crate::{Decoder, EncodeError};
|
|
||||||
use nbt::CompoundTag;
|
|
||||||
use std::io::Cursor;
|
|
||||||
|
|
||||||
#[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::Spectator,
|
|
||||||
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::Spectator);
|
|
||||||
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"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,650 +1,8 @@
|
|||||||
//! This crate implements Minecraft protocol.
|
//! This crate implements Minecraft protocol.
|
||||||
//!
|
//!
|
||||||
//! Information about protocol can be found at https://wiki.vg/Protocol.
|
//! Information about protocol can be found at https://wiki.vg/Protocol.
|
||||||
use io::Error as IoError;
|
#[cfg(feature = "data")]
|
||||||
use std::io;
|
pub mod data;
|
||||||
use std::io::{Cursor, Read, Write};
|
pub mod decoder;
|
||||||
use std::string::FromUtf8Error;
|
pub mod encoder;
|
||||||
|
pub mod error;
|
||||||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
|
||||||
use serde_json::error::Error as JsonError;
|
|
||||||
use uuid::parser::ParseError as UuidParseError;
|
|
||||||
|
|
||||||
use crate::chat::Message;
|
|
||||||
use nbt::decode::TagDecodeError;
|
|
||||||
use nbt::CompoundTag;
|
|
||||||
use num_traits::{FromPrimitive, ToPrimitive};
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod chat;
|
|
||||||
pub mod game;
|
|
||||||
pub mod login;
|
|
||||||
pub mod status;
|
|
||||||
|
|
||||||
/// Current supported protocol version.
|
|
||||||
pub const PROTOCOL_VERSION: u32 = 498;
|
|
||||||
/// Protocol limits maximum string length.
|
|
||||||
const STRING_MAX_LENGTH: u16 = 32_768;
|
|
||||||
const HYPHENATED_UUID_LENGTH: u16 = 36;
|
|
||||||
|
|
||||||
/// Possible errors while encoding packet.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum EncodeError {
|
|
||||||
/// String length can't be more than provided value.
|
|
||||||
StringTooLong {
|
|
||||||
/// String length.
|
|
||||||
length: usize,
|
|
||||||
/// Max string length.
|
|
||||||
max_length: u16,
|
|
||||||
},
|
|
||||||
IOError {
|
|
||||||
io_error: IoError,
|
|
||||||
},
|
|
||||||
JsonError {
|
|
||||||
json_error: JsonError,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<IoError> for EncodeError {
|
|
||||||
fn from(io_error: IoError) -> Self {
|
|
||||||
EncodeError::IOError { io_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<JsonError> for EncodeError {
|
|
||||||
fn from(json_error: JsonError) -> Self {
|
|
||||||
EncodeError::JsonError { json_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Possible errors while decoding packet.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum DecodeError {
|
|
||||||
/// Packet was not recognized. Invalid data or wrong protocol version.
|
|
||||||
UnknownPacketType {
|
|
||||||
type_id: u8,
|
|
||||||
},
|
|
||||||
/// String length can't be more than provided value.
|
|
||||||
StringTooLong {
|
|
||||||
/// String length.
|
|
||||||
length: usize,
|
|
||||||
/// Max string length.
|
|
||||||
max_length: u16,
|
|
||||||
},
|
|
||||||
IOError {
|
|
||||||
io_error: IoError,
|
|
||||||
},
|
|
||||||
JsonError {
|
|
||||||
json_error: JsonError,
|
|
||||||
},
|
|
||||||
/// Byte array was not recognized as valid UTF-8 string.
|
|
||||||
Utf8Error {
|
|
||||||
utf8_error: FromUtf8Error,
|
|
||||||
},
|
|
||||||
/// Boolean are parsed from byte. Valid byte value are 0 or 1.
|
|
||||||
NonBoolValue,
|
|
||||||
UuidParseError {
|
|
||||||
uuid_parse_error: UuidParseError,
|
|
||||||
},
|
|
||||||
// Type id was not parsed as valid enum value.
|
|
||||||
UnknownEnumType {
|
|
||||||
type_id: u8,
|
|
||||||
},
|
|
||||||
TagDecodeError {
|
|
||||||
tag_decode_error: TagDecodeError,
|
|
||||||
},
|
|
||||||
VarIntTooLong {
|
|
||||||
max_bytes: usize,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<IoError> for DecodeError {
|
|
||||||
fn from(io_error: IoError) -> Self {
|
|
||||||
DecodeError::IOError { io_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<JsonError> for DecodeError {
|
|
||||||
fn from(json_error: JsonError) -> Self {
|
|
||||||
DecodeError::JsonError { json_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<FromUtf8Error> for DecodeError {
|
|
||||||
fn from(utf8_error: FromUtf8Error) -> Self {
|
|
||||||
DecodeError::Utf8Error { utf8_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<UuidParseError> for DecodeError {
|
|
||||||
fn from(uuid_parse_error: UuidParseError) -> Self {
|
|
||||||
DecodeError::UuidParseError { uuid_parse_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<TagDecodeError> for DecodeError {
|
|
||||||
fn from(tag_decode_error: TagDecodeError) -> Self {
|
|
||||||
DecodeError::TagDecodeError { tag_decode_error }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trait Encoder {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
trait Decoder {
|
|
||||||
type Output;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! write_signed_var_int (
|
|
||||||
($type: ident, $name: ident) => (
|
|
||||||
fn $name(&mut self, mut value: $type) -> Result<(), EncodeError> {
|
|
||||||
loop {
|
|
||||||
let mut byte = (value & 0b01111111) as u8;
|
|
||||||
value = value >> 7;
|
|
||||||
|
|
||||||
if value != 0 {
|
|
||||||
byte |= 0b10000000;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.write_u8(byte)?;
|
|
||||||
|
|
||||||
if value == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
macro_rules! read_signed_var_int (
|
|
||||||
($type: ident, $name: ident, $max_bytes: expr) => (
|
|
||||||
fn $name(&mut self) -> Result<$type, DecodeError> {
|
|
||||||
let mut bytes = 0;
|
|
||||||
let mut output = 0;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let byte = self.read_u8()?;
|
|
||||||
let value = (byte & 0b01111111) as $type;
|
|
||||||
|
|
||||||
output |= value << 7 * bytes;
|
|
||||||
bytes += 1;
|
|
||||||
|
|
||||||
if bytes > $max_bytes {
|
|
||||||
return Err(DecodeError::VarIntTooLong { max_bytes: $max_bytes })
|
|
||||||
}
|
|
||||||
|
|
||||||
if (byte & 0b10000000) == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(output)
|
|
||||||
}
|
|
||||||
);
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Trait adds additional helper methods for `Write` to write protocol data.
|
|
||||||
trait EncoderWriteExt {
|
|
||||||
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_chat_message(&mut self, value: &Message) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_enum<T: ToPrimitive>(&mut self, value: &T) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_var_i32(&mut self, value: i32) -> Result<(), EncodeError>;
|
|
||||||
|
|
||||||
fn write_var_i64(&mut self, value: i64) -> Result<(), EncodeError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Trait adds additional helper methods for `Read` to read protocol data.
|
|
||||||
trait DecoderReadExt {
|
|
||||||
fn read_bool(&mut self) -> Result<bool, DecodeError>;
|
|
||||||
|
|
||||||
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError>;
|
|
||||||
|
|
||||||
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError>;
|
|
||||||
|
|
||||||
fn read_chat_message(&mut self) -> Result<Message, DecodeError>;
|
|
||||||
|
|
||||||
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError>;
|
|
||||||
|
|
||||||
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError>;
|
|
||||||
|
|
||||||
fn read_var_i32(&mut self) -> Result<i32, DecodeError>;
|
|
||||||
|
|
||||||
fn read_var_i64(&mut self) -> Result<i64, DecodeError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: Write> EncoderWriteExt for W {
|
|
||||||
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError> {
|
|
||||||
if value {
|
|
||||||
self.write_u8(1)?;
|
|
||||||
} else {
|
|
||||||
self.write_u8(0)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError> {
|
|
||||||
let length = value.len();
|
|
||||||
|
|
||||||
if length > max_length as usize {
|
|
||||||
return Err(EncodeError::StringTooLong { length, max_length });
|
|
||||||
}
|
|
||||||
|
|
||||||
self.write_var_i32(value.len() as i32)?;
|
|
||||||
self.write_all(value.as_bytes())?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError> {
|
|
||||||
self.write_var_i32(value.len() as i32)?;
|
|
||||||
self.write_all(value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_chat_message(&mut self, value: &Message) -> Result<(), EncodeError> {
|
|
||||||
self.write_string(&value.to_json()?, STRING_MAX_LENGTH)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_enum<T: ToPrimitive>(&mut self, value: &T) -> Result<(), EncodeError> {
|
|
||||||
let type_value = ToPrimitive::to_u8(value).unwrap();
|
|
||||||
self.write_u8(type_value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError> {
|
|
||||||
nbt::encode::write_compound_tag(self, value.clone())?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
write_signed_var_int!(i32, write_var_i32);
|
|
||||||
write_signed_var_int!(i64, write_var_i64);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: Read> DecoderReadExt for R {
|
|
||||||
fn read_bool(&mut self) -> Result<bool, DecodeError> {
|
|
||||||
match self.read_u8()? {
|
|
||||||
0 => Ok(false),
|
|
||||||
1 => Ok(true),
|
|
||||||
_ => Err(DecodeError::NonBoolValue),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError> {
|
|
||||||
let length = self.read_var_i32()? as usize;
|
|
||||||
|
|
||||||
if length as u16 > max_length {
|
|
||||||
return Err(DecodeError::StringTooLong { length, max_length });
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut buf = vec![0; length as usize];
|
|
||||||
self.read_exact(&mut buf)?;
|
|
||||||
|
|
||||||
Ok(String::from_utf8(buf)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError> {
|
|
||||||
let length = self.read_var_i32()?;
|
|
||||||
|
|
||||||
let mut buf = vec![0; length as usize];
|
|
||||||
self.read_exact(&mut buf)?;
|
|
||||||
|
|
||||||
Ok(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_chat_message(&mut self) -> Result<Message, DecodeError> {
|
|
||||||
let json = self.read_string(STRING_MAX_LENGTH)?;
|
|
||||||
let message = Message::from_json(&json)?;
|
|
||||||
|
|
||||||
Ok(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError> {
|
|
||||||
let type_id = self.read_u8()?;
|
|
||||||
let result = FromPrimitive::from_u8(type_id);
|
|
||||||
|
|
||||||
result.ok_or_else(|| DecodeError::UnknownEnumType { type_id })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError> {
|
|
||||||
Ok(nbt::decode::read_compound_tag(self)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
read_signed_var_int!(i32, read_var_i32, 5);
|
|
||||||
read_signed_var_int!(i64, read_var_i64, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for u8 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_u8(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for u8 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_u8()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for i32 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_i32::<BigEndian>(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for i32 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_i32::<BigEndian>()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for u32 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_u32::<BigEndian>(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for u32 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_u32::<BigEndian>()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for i64 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_i64::<BigEndian>(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for i64 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_i64::<BigEndian>()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for u64 {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_u64::<BigEndian>(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for u64 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_u64::<BigEndian>()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for String {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_string(self, STRING_MAX_LENGTH)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for String {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_string(STRING_MAX_LENGTH)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for bool {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_bool(*self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for bool {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_bool()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for Vec<u8> {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_byte_array(self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for Vec<u8> {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_byte_array()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for Uuid {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_all(self.as_bytes())?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for Uuid {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
let mut buf = [0; 16];
|
|
||||||
reader.read_exact(&mut buf)?;
|
|
||||||
|
|
||||||
Ok(Uuid::from_bytes(buf))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for CompoundTag {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
Ok(writer.write_compound_tag(self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for CompoundTag {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
Ok(reader.read_compound_tag()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encoder for Vec<CompoundTag> {
|
|
||||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
writer.write_var_i32(self.len() as i32)?;
|
|
||||||
|
|
||||||
for compound_tag in self {
|
|
||||||
writer.write_compound_tag(&compound_tag)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decoder for Vec<CompoundTag> {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
||||||
let length = reader.read_var_i32()? as usize;
|
|
||||||
let mut vec = Vec::with_capacity(length);
|
|
||||||
|
|
||||||
for _ in 0..length {
|
|
||||||
let compound_tag = reader.read_compound_tag()?;
|
|
||||||
vec.push(compound_tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(vec)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! impl_enum_encoder_decoder (
|
|
||||||
($ty: ident) => (
|
|
||||||
impl crate::Encoder for $ty {
|
|
||||||
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::EncodeError> {
|
|
||||||
Ok(crate::EncoderWriteExt::write_enum(writer, self)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl crate::Decoder for $ty {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::DecodeError> {
|
|
||||||
Ok(crate::DecoderReadExt::read_enum(reader)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
);
|
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! impl_json_encoder_decoder (
|
|
||||||
($ty: ident) => (
|
|
||||||
impl crate::Encoder for $ty {
|
|
||||||
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::EncodeError> {
|
|
||||||
let json = serde_json::to_string(self)?;
|
|
||||||
crate::EncoderWriteExt::write_string(writer, &json, crate::STRING_MAX_LENGTH)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl crate::Decoder for $ty {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::DecodeError> {
|
|
||||||
let json = crate::DecoderReadExt::read_string(reader, crate::STRING_MAX_LENGTH)?;
|
|
||||||
|
|
||||||
Ok(serde_json::from_str(&json)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
);
|
|
||||||
|
|
||||||
mod var_int {
|
|
||||||
use crate::{DecodeError, EncodeError};
|
|
||||||
use crate::{DecoderReadExt, EncoderWriteExt};
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
|
|
||||||
pub fn encode<W: Write>(value: &i32, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
writer.write_var_i32(*value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode<R: Read>(reader: &mut R) -> Result<i32, DecodeError> {
|
|
||||||
Ok(reader.read_var_i32()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod var_long {
|
|
||||||
use crate::{DecodeError, EncodeError};
|
|
||||||
use crate::{DecoderReadExt, EncoderWriteExt};
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
|
|
||||||
pub fn encode<W: Write>(value: &i64, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
writer.write_var_i64(*value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode<R: Read>(reader: &mut R) -> Result<i64, DecodeError> {
|
|
||||||
Ok(reader.read_var_i64()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod rest {
|
|
||||||
use crate::{DecodeError, EncodeError};
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
|
|
||||||
pub fn encode<W: Write>(value: &[u8], writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
writer.write_all(value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode<R: Read>(reader: &mut R) -> Result<Vec<u8>, DecodeError> {
|
|
||||||
let mut data = Vec::new();
|
|
||||||
reader.read_to_end(data.as_mut())?;
|
|
||||||
|
|
||||||
Ok(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod uuid_hyp_str {
|
|
||||||
use crate::{
|
|
||||||
DecodeError, DecoderReadExt, EncodeError, EncoderWriteExt, HYPHENATED_UUID_LENGTH,
|
|
||||||
};
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub fn encode<W: Write>(value: &Uuid, writer: &mut W) -> Result<(), EncodeError> {
|
|
||||||
let uuid_hyphenated_string = value.to_hyphenated().to_string();
|
|
||||||
writer.write_string(&uuid_hyphenated_string, HYPHENATED_UUID_LENGTH)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode<R: Read>(reader: &mut R) -> Result<Uuid, DecodeError> {
|
|
||||||
let uuid_hyphenated_string = reader.read_string(HYPHENATED_UUID_LENGTH)?;
|
|
||||||
let uuid = Uuid::parse_str(&uuid_hyphenated_string)?;
|
|
||||||
|
|
||||||
Ok(uuid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_read_variable_i32_2_bytes_value() {
|
|
||||||
let mut cursor = Cursor::new(vec![0b10101100, 0b00000010]);
|
|
||||||
let value = cursor.read_var_i32().unwrap();
|
|
||||||
|
|
||||||
assert_eq!(value, 300);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_read_variable_i32_5_bytes_value() {
|
|
||||||
let mut cursor = Cursor::new(vec![0xff, 0xff, 0xff, 0xff, 0x07]);
|
|
||||||
let value = cursor.read_var_i32().unwrap();
|
|
||||||
|
|
||||||
assert_eq!(value, 2147483647);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_write_variable_i32_2_bytes_value() {
|
|
||||||
let mut cursor = Cursor::new(Vec::with_capacity(5));
|
|
||||||
cursor.write_var_i32(300).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(cursor.into_inner(), vec![0b10101100, 0b00000010]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_write_variable_i32_5_bytes_value() {
|
|
||||||
let mut cursor = Cursor::new(Vec::with_capacity(5));
|
|
||||||
cursor.write_var_i32(2147483647).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(cursor.into_inner(), vec![0xff, 0xff, 0xff, 0xff, 0x07]);
|
|
||||||
}
|
|
||||||
|
@ -1,479 +0,0 @@
|
|||||||
use crate::chat::Message;
|
|
||||||
use crate::DecodeError;
|
|
||||||
use crate::Decoder;
|
|
||||||
use std::io::Read;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
use minecraft_protocol_derive::Packet;
|
|
||||||
|
|
||||||
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(Packet, 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(Packet, 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(Packet, Debug)]
|
|
||||||
pub struct LoginPluginResponse {
|
|
||||||
#[packet(with = "var_int")]
|
|
||||||
pub message_id: i32,
|
|
||||||
pub successful: bool,
|
|
||||||
#[packet(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(Packet, 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(Packet, Debug)]
|
|
||||||
pub struct EncryptionRequest {
|
|
||||||
#[packet(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(Packet, Debug)]
|
|
||||||
pub struct LoginSuccess {
|
|
||||||
#[packet(with = "uuid_hyp_str")]
|
|
||||||
pub uuid: Uuid,
|
|
||||||
#[packet(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(Packet, Debug)]
|
|
||||||
pub struct SetCompression {
|
|
||||||
#[packet(with = "var_int")]
|
|
||||||
pub threshold: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SetCompression {
|
|
||||||
pub fn new(threshold: i32) -> LoginClientBoundPacket {
|
|
||||||
let set_compression = SetCompression { threshold };
|
|
||||||
|
|
||||||
LoginClientBoundPacket::SetCompression(set_compression)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
|
||||||
pub struct LoginPluginRequest {
|
|
||||||
#[packet(with = "var_int")]
|
|
||||||
pub message_id: i32,
|
|
||||||
pub channel: String,
|
|
||||||
#[packet(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::chat::{Message, Payload};
|
|
||||||
use crate::login::{EncryptionRequest, LoginDisconnect, LoginPluginRequest, SetCompression};
|
|
||||||
use crate::login::{EncryptionResponse, LoginPluginResponse};
|
|
||||||
use crate::login::{LoginStart, LoginSuccess};
|
|
||||||
use crate::Decoder;
|
|
||||||
use crate::Encoder;
|
|
||||||
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]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,235 +0,0 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
use crate::chat::Message;
|
|
||||||
use crate::impl_json_encoder_decoder;
|
|
||||||
use crate::DecodeError;
|
|
||||||
use crate::Decoder;
|
|
||||||
use minecraft_protocol_derive::Packet;
|
|
||||||
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(Packet, 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(Packet, 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(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct ServerStatus {
|
|
||||||
pub version: ServerVersion,
|
|
||||||
pub players: OnlinePlayers,
|
|
||||||
pub description: Message,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct ServerVersion {
|
|
||||||
pub name: String,
|
|
||||||
pub protocol: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct OnlinePlayers {
|
|
||||||
pub max: u32,
|
|
||||||
pub online: u32,
|
|
||||||
pub sample: Vec<OnlinePlayer>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
|
||||||
pub struct OnlinePlayer {
|
|
||||||
pub name: String,
|
|
||||||
pub id: Uuid,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Packet, Debug)]
|
|
||||||
pub struct StatusResponse {
|
|
||||||
pub server_status: ServerStatus,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_json_encoder_decoder!(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::chat::{Message, Payload};
|
|
||||||
use crate::status::{
|
|
||||||
OnlinePlayer, OnlinePlayers, PingRequest, PingResponse, ServerStatus, ServerVersion,
|
|
||||||
StatusResponse,
|
|
||||||
};
|
|
||||||
use crate::Decoder;
|
|
||||||
use crate::Encoder;
|
|
||||||
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"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
Binary file not shown.
@ -1 +0,0 @@
|
|||||||
{"text":"hello client!"}
|
|
Binary file not shown.
@ -1 +0,0 @@
|
|||||||
{"text":"Message"}
|
|
Binary file not shown.
@ -1 +0,0 @@
|
|||||||
hello server!
|
|
Binary file not shown.
@ -1,3 +0,0 @@
|
|||||||
ServerID
|
|
||||||
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
@ -1 +0,0 @@
|
|||||||
{"text":"Message"}
|
|
@ -1 +0,0 @@
|
|||||||
7Channel
|
|
@ -1 +0,0 @@
|
|||||||
7
|
|
@ -1 +0,0 @@
|
|||||||
|
|
@ -1 +0,0 @@
|
|||||||
Username
|
|
@ -1 +0,0 @@
|
|||||||
$35ee313b-d89a-41b8-b25e-d32e8aff0389Username
|
|
Binary file not shown.
Binary file not shown.
@ -1 +0,0 @@
|
|||||||
¾{"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