Compare commits

..

22 Commits

Author SHA1 Message Date
Vladislavs Golubs
a121323eed Add multi version support 2021-02-09 23:36:13 +03:00
Vladislavs Golubs
b043dbb07b WIP list field 2021-02-09 13:05:08 +03:00
Vladislavs Golubs
2d9b9dd94f Fix backend list parsing 2021-02-09 03:14:55 +03:00
Vladislavs Golubs
c6d1cdecc7 Refactor 2021-02-09 03:04:40 +03:00
Vladislavs Golubs
e8a061e672 Refactor 2021-02-08 23:37:10 +03:00
Vladislavs Golubs
ebd8f5ce22 Refactor 2021-02-08 23:37:01 +03:00
Vladislavs Golubs
5245cdf16d Add more types and improve debug println 2021-02-08 01:49:30 +03:00
Vladislavs Golubs
1d5f2112a7 Fix 2021-02-07 18:46:24 +03:00
Vladislavs Golubs
0a0835fd2d Add game packets 2021-02-07 18:40:11 +03:00
Vladislavs Golubs
3be31e8b81 Add ref types to login and remove code coverage 2021-02-07 17:20:01 +03:00
Vladislavs Golubs
87b1e7a250 Add field data type modification 2021-02-07 16:46:00 +03:00
Vladislavs Golubs
99a331c6ab Fix 2021-02-07 16:09:48 +03:00
Vladislavs Golubs
03267e3318 Add fields 2021-02-07 15:52:27 +03:00
Vladislavs Golubs
d589bd7956 Fix packet ids 2021-02-07 02:20:12 +03:00
Vladislavs Golubs
ab4d40e482 Rework input json parser 2021-02-06 23:18:39 +03:00
Vladislavs Golubs
3229d02418 WIP input parse 2021-02-06 18:59:51 +03:00
Vladislavs Golubs
3189f0c0d9 Add minecraft-data as git submodule 2021-01-31 22:46:50 +03:00
Vladislavs Golubs
f1c602092c Fix tests and rename to packet templates 2021-01-24 02:11:26 +03:00
Vladislavs Golubs
fca5351977 Refactor protocol and refactor imports 2021-01-24 02:06:31 +03:00
Vladislavs Golubs
02f2d6b860 Add protocol state structs and imports generation 2021-01-24 01:03:16 +03:00
Vladislavs Golubs
2849483562 Add protocol state enum generation 2021-01-23 18:40:28 +03:00
Vladislavs Golubs
ccd8647113 Fix derive 2021-01-23 18:39:59 +03:00
234 changed files with 122294 additions and 2859 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "protocol-generator/minecraft-data"]
path = protocol-generator/minecraft-data
url = https://github.com/PrismarineJS/minecraft-data

View File

@ -7,27 +7,3 @@ rust:
matrix:
allow_failures:
- rust: nightly
addons:
apt:
packages:
- libcurl4-openssl-dev
- libelf-dev
- libdw-dev
- cmake
- gcc
- binutils-dev
- libiberty-dev
after_success: |
wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz &&
tar xzf master.tar.gz &&
cd kcov-master &&
mkdir build &&
cd build &&
cmake .. &&
make &&
make install DESTDIR=../../kcov-build &&
cd ../.. &&
rm -rf kcov-master &&
for file in target/debug/minecraft_protocol*; do [ -x "${file}" ] || continue; mkdir -p "target/cov/$(basename $file)"; ./kcov-build/usr/local/bin/kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done &&
bash <(curl -s https://codecov.io/bash) &&
echo "Uploaded code coverage"

View File

@ -3,4 +3,5 @@
members = [
"protocol",
"protocol-derive",
]
"protocol-generator"
]

View File

@ -1,7 +1,6 @@
minecraft-protocol
============
[![crates.io](https://img.shields.io/crates/v/minecraft-protocol.svg)](https://crates.io/crates/minecraft-protocol)
[![Build Status](https://travis-ci.com/eihwaz/minecraft-protocol.svg?branch=master)](https://app.travis-ci.com/github/eihwaz/minecraft-protocol)
[![codecov](https://codecov.io/gh/eihwaz/minecraft-protocol/branch/master/graph/badge.svg)](https://codecov.io/gh/eihwaz/minecraft-protocol)
[![Build Status](https://travis-ci.com/eihwaz/minecraft-protocol.svg?branch=master)](https://travis-ci.com/eihwaz/minecraft-protocol)
Library for decoding and encoding Minecraft packets

4
generate.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
cargo run
cargo fmt

View File

@ -7,7 +7,7 @@ description = "Derive macro for reading and writing Minecraft packets"
license = "MIT"
homepage = "https://github.com/eihwaz/minecraft-protocol"
repository = "https://github.com/eihwaz/minecraft-protocol"
keywords = ["minecraft", "protocol", "packet", "io"]
keywords = ["minecraft", "derive", "protocol", "packet", "io"]
[lib]
proc-macro = true

View File

@ -1,44 +0,0 @@
use syn::Error as SynError;
/// Possible errors while deriving.
#[derive(Debug)]
pub(crate) enum DeriveInputParserError {
/// Derive attribute must be placed on a structure or enum.
UnsupportedData,
/// Data fields must be named.
UnnamedDataFields,
/// Possible errors while parsing attributes.
AttributeError { attribute_error: AttributeError },
}
/// Possible errors while parsing attributes.
#[derive(Debug)]
pub(crate) enum AttributeError {
/// Failed to parse field meta due incorrect syntax.
BadAttributeSyntax { syn_error: SynError },
/// Unsupported field attribute type.
UnsupportedAttribute,
/// Field meta has wrong value type.
/// For example an int was expected, but a string was supplied.
AttributeWrongValueType,
}
impl From<AttributeError> for DeriveInputParserError {
fn from(attribute_error: AttributeError) -> Self {
DeriveInputParserError::AttributeError { attribute_error }
}
}
impl From<SynError> for DeriveInputParserError {
fn from(syn_error: SynError) -> Self {
DeriveInputParserError::AttributeError {
attribute_error: AttributeError::BadAttributeSyntax { syn_error },
}
}
}
impl From<SynError> for AttributeError {
fn from(syn_error: SynError) -> Self {
AttributeError::BadAttributeSyntax { syn_error }
}
}

View File

@ -1,42 +1,186 @@
extern crate proc_macro;
use crate::parse::{parse_derive_input, DeriveInputParseResult};
use crate::render::decoder::{render_enum_decoder, render_struct_decoder};
use crate::render::encoder::{render_enum_encoder, render_struct_encoder};
use proc_macro::TokenStream;
use syn::parse_macro_input;
use syn::DeriveInput;
use proc_macro::TokenStream as TokenStream1;
use proc_macro2::Ident;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, TokenStreamExt};
use std::iter::FromIterator;
use syn::__private::Span;
use syn::{parse_macro_input, Data, DeriveInput, Field, Fields, Lit, Meta, NestedMeta};
mod error;
mod parse;
mod render;
#[proc_macro_derive(Packet, attributes(packet))]
pub fn derive_packet(input: proc_macro::TokenStream) -> TokenStream1 {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
#[proc_macro_derive(Encoder, attributes(data_type))]
pub fn derive_encoder(tokens: TokenStream) -> TokenStream {
let input = parse_macro_input!(tokens as DeriveInput);
let derive_parse_result = parse_derive_input(&input).expect("Failed to parse derive input");
match input.data {
Data::Struct(data) => {
let fields = &data.fields;
TokenStream::from(match derive_parse_result {
DeriveInputParseResult::Struct { name, fields } => render_struct_encoder(name, &fields),
DeriveInputParseResult::Enum {
name,
discriminant_type,
variants,
} => render_enum_encoder(name, &discriminant_type, &variants),
})
let encoder = impl_encoder_trait(name, fields);
let decoder = impl_decoder_trait(name, fields);
TokenStream1::from(quote! {
#encoder
#decoder
})
}
_ => panic!("Expected only structures"),
}
}
#[proc_macro_derive(Decoder, attributes(data_type))]
pub fn derive_decoder(tokens: TokenStream) -> TokenStream {
let input = parse_macro_input!(tokens as DeriveInput);
let derive_parse_result = parse_derive_input(&input).expect("Failed to parse derive input");
fn impl_encoder_trait(name: &Ident, fields: &Fields) -> TokenStream2 {
let encode = quote_field(fields, |field| {
let name = &field.ident;
TokenStream::from(match derive_parse_result {
DeriveInputParseResult::Struct { name, fields } => render_struct_decoder(name, &fields),
DeriveInputParseResult::Enum {
name,
discriminant_type,
variants,
} => render_enum_decoder(name, &discriminant_type, &variants),
})
let unparsed_meta = get_packet_field_meta(field);
let parsed_meta = parse_packet_field_meta(&unparsed_meta);
// This is special case because max length are used only for strings.
if let Some(max_length) = parsed_meta.max_length {
return quote! {
crate::EncoderWriteExt::write_string(writer, &self.#name, #max_length)?;
};
}
let module = parsed_meta.module.as_deref().unwrap_or("Encoder");
let module_ident = Ident::new(&module, Span::call_site());
quote! {
crate::#module_ident::encode(&self.#name, writer)?;
}
});
quote! {
#[automatically_derived]
impl crate::Encoder for #name {
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::EncodeError> {
#encode
Ok(())
}
}
}
}
fn impl_decoder_trait(name: &Ident, fields: &Fields) -> TokenStream2 {
let decode = quote_field(fields, |field| {
let name = &field.ident;
let ty = &field.ty;
let unparsed_meta = get_packet_field_meta(field);
let parsed_meta = parse_packet_field_meta(&unparsed_meta);
// This is special case because max length are used only for strings.
if let Some(max_length) = parsed_meta.max_length {
return quote! {
let #name = crate::DecoderReadExt::read_string(reader, #max_length)?;
};
}
match parsed_meta.module {
Some(module) => {
let module_ident = Ident::new(&module, Span::call_site());
quote! {
let #name = crate::#module_ident::decode(reader)?;
}
}
None => {
quote! {
let #name = <#ty as crate::Decoder>::decode(reader)?;
}
}
}
});
let create = quote_field(fields, |field| {
let name = &field.ident;
quote! {
#name,
}
});
quote! {
#[automatically_derived]
impl crate::Decoder for #name {
type Output = Self;
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::DecodeError> {
#decode
Ok(#name {
#create
})
}
}
}
}
#[derive(Debug)]
struct PacketFieldMeta {
module: Option<String>,
max_length: Option<u16>,
}
fn parse_packet_field_meta(meta_list: &Vec<NestedMeta>) -> PacketFieldMeta {
let mut module = None;
let mut max_length = None;
for meta in meta_list {
match meta {
NestedMeta::Meta(Meta::NameValue(named_meta)) => match &named_meta.path {
path if path.is_ident("with") => match &named_meta.lit {
Lit::Str(lit_str) => module = Some(lit_str.value()),
_ => panic!("\"with\" attribute value must be string"),
},
path if path.is_ident("max_length") => match &named_meta.lit {
Lit::Int(lit_int) => {
max_length = Some(
lit_int
.base10_parse::<u16>()
.expect("Failed to parse max length attribute"),
)
}
_ => panic!("\"max_length\" attribute value must be integer"),
},
path => panic!(
"Received unrecognized attribute : \"{}\"",
path.get_ident().unwrap()
),
},
_ => panic!("Expected only named meta values"),
}
}
PacketFieldMeta { module, max_length }
}
fn get_packet_field_meta(field: &Field) -> Vec<NestedMeta> {
field
.attrs
.iter()
.filter(|a| a.path.is_ident("packet"))
.map(|a| a.parse_meta().expect("Failed to parse field attribute"))
.map(|m| match m {
Meta::List(meta_list) => Vec::from_iter(meta_list.nested),
_ => panic!("Expected only list attributes"),
})
.flatten()
.collect()
}
fn quote_field<F: Fn(&Field) -> TokenStream2>(fields: &Fields, func: F) -> TokenStream2 {
let mut output = quote!();
match fields {
Fields::Named(named_fields) => {
output.append_all(named_fields.named.iter().map(|f| func(f)))
}
_ => panic!("Expected only for named fields"),
}
output
}

View File

@ -1,291 +0,0 @@
use crate::error::{AttributeError, DeriveInputParserError};
use proc_macro2::Ident;
use std::iter::FromIterator;
use syn::punctuated::Punctuated;
use syn::{
Attribute, Data, DeriveInput, ExprLit, Field, Fields, FieldsNamed, Lit, Meta, NestedMeta, Type,
};
use syn::{Error as SynError, Variant};
use syn::{Expr, Token};
pub(crate) enum DeriveInputParseResult<'a> {
Struct {
name: &'a Ident,
fields: Vec<FieldData<'a>>,
},
Enum {
name: &'a Ident,
discriminant_type: DiscriminantType,
variants: Vec<VariantData<'a>>,
},
}
pub(crate) struct VariantData<'a> {
pub(crate) discriminant: usize,
pub(crate) name: &'a Ident,
pub(crate) fields: Vec<FieldData<'a>>,
}
pub(crate) struct FieldData<'a> {
pub(crate) name: &'a Ident,
pub(crate) ty: &'a Type,
pub(crate) attribute: AttributeData,
}
#[derive(Debug, Eq, PartialEq)]
pub(crate) enum AttributeData {
With { module: String },
MaxLength { length: usize },
Bitfield { idx: u8, position: BitfieldPosition },
Empty,
}
#[derive(Debug, Eq, PartialEq)]
pub(crate) enum DiscriminantType {
UnsignedByte,
VarInt,
}
#[derive(Debug, Eq, PartialEq)]
pub(crate) enum BitfieldPosition {
Start,
Intermediate,
End,
}
pub(crate) fn parse_derive_input(
input: &DeriveInput,
) -> Result<DeriveInputParseResult, DeriveInputParserError> {
let name = &input.ident;
match &input.data {
Data::Struct(data_struct) => match &data_struct.fields {
Fields::Named(named_fields) => {
let fields = parse_fields(named_fields)?;
Ok(DeriveInputParseResult::Struct { name, fields })
}
_ => Err(DeriveInputParserError::UnnamedDataFields),
},
Data::Enum(data_enum) => {
let variants = parse_variants(&data_enum.variants)?;
let discriminant_type = parse_discriminant_type(&input.attrs)?;
Ok(DeriveInputParseResult::Enum {
name,
discriminant_type,
variants,
})
}
_ => Err(DeriveInputParserError::UnsupportedData),
}
}
fn parse_discriminant_type(
attributes: &Vec<Attribute>,
) -> Result<DiscriminantType, DeriveInputParserError> {
let nested_metas = parse_attributes_nested_metas(attributes)?;
let attribute = parse_attribute(nested_metas, None, 0)?;
match attribute {
AttributeData::With { module } if module == "var_int" => Ok(DiscriminantType::VarInt),
_ => Ok(DiscriminantType::UnsignedByte),
}
}
fn parse_variants(
variants: &Punctuated<Variant, Token![,]>,
) -> Result<Vec<VariantData>, DeriveInputParserError> {
variants
.iter()
.enumerate()
.map(|(idx, v)| parse_variant(idx, v))
.collect()
}
fn parse_variant(idx: usize, variant: &Variant) -> Result<VariantData, DeriveInputParserError> {
let discriminant = parse_variant_discriminant(variant).unwrap_or(idx);
let name = &variant.ident;
let fields = match &variant.fields {
Fields::Named(named_fields) => parse_fields(named_fields),
Fields::Unit => Ok(Vec::new()),
_ => Err(DeriveInputParserError::UnnamedDataFields),
}?;
Ok(VariantData {
discriminant,
name,
fields,
})
}
fn parse_variant_discriminant(variant: &Variant) -> Option<usize> {
variant
.discriminant
.as_ref()
.and_then(|(_, expr)| match expr {
Expr::Lit(ExprLit {
lit: Lit::Int(lit_int),
..
}) => lit_int.base10_parse().ok(),
_ => None,
})
}
fn parse_fields(named_fields: &FieldsNamed) -> Result<Vec<FieldData>, DeriveInputParserError> {
let mut fields_data = Vec::new();
let mut current_bitfield_idx = 0;
let fields: Vec<&Field> = named_fields.named.iter().collect();
for (idx, field) in fields.iter().enumerate() {
let name = field.ident.as_ref().unwrap();
let ty = &field.ty;
let nested_metas = parse_attributes_nested_metas(&field.attrs)?;
let next_field_opt = fields.get(idx + 1);
let next_nested_metas_opt = next_field_opt
.and_then(|next_field| parse_attributes_nested_metas(&next_field.attrs).ok());
let attribute = parse_attribute(nested_metas, next_nested_metas_opt, current_bitfield_idx)?;
match attribute {
AttributeData::Bitfield { .. } => current_bitfield_idx += 1,
_ => current_bitfield_idx = 0,
}
fields_data.push(FieldData {
name,
ty,
attribute,
})
}
Ok(fields_data)
}
fn parse_attributes_nested_metas(
attributes: &Vec<Attribute>,
) -> Result<Vec<NestedMeta>, DeriveInputParserError> {
let parsed_metas = attributes
.iter()
.filter(|a| a.path.is_ident("data_type"))
.map(|a| a.parse_meta())
.collect::<Result<Vec<Meta>, SynError>>()?;
let nested_metas = parsed_metas
.into_iter()
.map(|m| match m {
Meta::List(meta_list) => Ok(Vec::from_iter(meta_list.nested)),
_ => Err(AttributeError::UnsupportedAttribute),
})
.collect::<Result<Vec<Vec<NestedMeta>>, AttributeError>>()?;
Ok(nested_metas.into_iter().flatten().collect())
}
fn parse_attribute(
nested_metas: Vec<NestedMeta>,
next_nested_metas_opt: Option<Vec<NestedMeta>>,
current_bitfield_idx: u8,
) -> Result<AttributeData, DeriveInputParserError> {
let simple_attribute_parsers: Vec<fn(&NestedMeta) -> Result<AttributeData, AttributeError>> =
vec![get_module_attribute, get_max_length_attribute];
for nested_meta in nested_metas.iter() {
let bitfield_attribute =
get_bitfield_attribute(current_bitfield_idx, nested_meta, &next_nested_metas_opt);
if bitfield_attribute != AttributeData::Empty {
return Ok(bitfield_attribute);
}
for attribute_parser in simple_attribute_parsers.iter() {
let attribute = attribute_parser(nested_meta)?;
if attribute != AttributeData::Empty {
return Ok(attribute);
}
}
}
Ok(AttributeData::Empty)
}
fn get_module_attribute(nested_meta: &NestedMeta) -> Result<AttributeData, AttributeError> {
if let NestedMeta::Meta(Meta::NameValue(named_meta)) = nested_meta {
if matches!(&named_meta.path, path if path.is_ident("with")) {
return match &named_meta.lit {
Lit::Str(lit_str) => Ok(AttributeData::With {
module: lit_str.value(),
}),
_ => Err(AttributeError::AttributeWrongValueType),
};
}
}
Ok(AttributeData::Empty)
}
fn get_max_length_attribute(nested_meta: &NestedMeta) -> Result<AttributeData, AttributeError> {
if let NestedMeta::Meta(Meta::NameValue(named_meta)) = nested_meta {
if matches!(&named_meta.path, path if path.is_ident("max_length")) {
return match &named_meta.lit {
Lit::Int(lit_int) => Ok(AttributeData::MaxLength {
length: lit_int.base10_parse()?,
}),
_ => Err(AttributeError::AttributeWrongValueType),
};
}
}
Ok(AttributeData::Empty)
}
fn get_bitfield_attribute(
current_bitfield_idx: u8,
nested_meta: &NestedMeta,
next_nested_metas_opt: &Option<Vec<NestedMeta>>,
) -> AttributeData {
if is_bitfield_attribute(nested_meta) {
let position = calc_bitfield_position(current_bitfield_idx, next_nested_metas_opt);
AttributeData::Bitfield {
idx: current_bitfield_idx,
position,
}
} else {
AttributeData::Empty
}
}
fn calc_bitfield_position(
current_bitfield_idx: u8,
next_nested_metas_opt: &Option<Vec<NestedMeta>>,
) -> BitfieldPosition {
fn next_has_bitfield_attribute(next_nested_metas: &Vec<NestedMeta>) -> bool {
next_nested_metas
.iter()
.any(|nested_meta| is_bitfield_attribute(nested_meta))
}
match next_nested_metas_opt {
Some(next_nested_metas) if (next_has_bitfield_attribute(&next_nested_metas)) => {
if current_bitfield_idx == 0 {
BitfieldPosition::Start
} else {
BitfieldPosition::Intermediate
}
}
_ => BitfieldPosition::End,
}
}
fn is_bitfield_attribute(nested_meta: &NestedMeta) -> bool {
match nested_meta {
NestedMeta::Meta(Meta::Path(path)) => path.is_ident("bitfield"),
_ => false,
}
}

View File

@ -1,185 +0,0 @@
use crate::parse::{AttributeData, BitfieldPosition, DiscriminantType, FieldData, VariantData};
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::Type;
pub(crate) fn render_struct_decoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStream2 {
let field_names_joined_comma = render_field_names_joined_comma(fields);
let render_fields = render_fields(fields);
quote! {
#[automatically_derived]
impl crate::decoder::Decoder for #name {
type Output = Self;
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
#render_fields
Ok(#name {
#field_names_joined_comma
})
}
}
}
}
pub(crate) fn render_enum_decoder(
name: &Ident,
discriminant_type: &DiscriminantType,
variants: &Vec<VariantData>,
) -> TokenStream2 {
let render_variants = render_variants(discriminant_type, variants);
let render_discriminant_type = render_discriminant_type(discriminant_type);
quote! {
#[automatically_derived]
impl crate::decoder::Decoder for #name {
type Output = Self;
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
let type_id = #render_discriminant_type;
match type_id {
#render_variants
_ => Err(DecodeError::UnknownEnumType { type_id: type_id as usize, }),
}
}
}
}
}
fn render_variants(
discriminant_type: &DiscriminantType,
variants: &Vec<VariantData>,
) -> TokenStream2 {
variants
.iter()
.map(|v| render_variant(discriminant_type, v))
.collect()
}
fn render_variant(discriminant_type: &DiscriminantType, variant: &VariantData) -> TokenStream2 {
if variant.fields.is_empty() {
render_unit_variant(discriminant_type, variant)
} else {
render_struct_variant(discriminant_type, variant)
}
}
fn render_unit_variant(
discriminant_type: &DiscriminantType,
variant: &VariantData,
) -> TokenStream2 {
let discriminant = render_discriminant(discriminant_type, variant.discriminant);
let name = variant.name;
quote! {
#discriminant => Ok(Self::#name),
}
}
fn render_struct_variant(
discriminant_type: &DiscriminantType,
variant: &VariantData,
) -> TokenStream2 {
let discriminant = render_discriminant(discriminant_type, variant.discriminant);
let name = variant.name;
let fields = &variant.fields;
let field_names_joined_comma = render_field_names_joined_comma(fields);
let render_fields = render_fields(fields);
quote! {
#discriminant => {
#render_fields
Ok(Self::#name {
#field_names_joined_comma
})
}
}
}
fn render_discriminant_type(discriminant_type: &DiscriminantType) -> TokenStream2 {
match discriminant_type {
DiscriminantType::UnsignedByte => {
quote!(reader.read_u8()?;)
}
DiscriminantType::VarInt => {
quote!(reader.read_var_i32()?;)
}
}
}
fn render_discriminant(discriminant_type: &DiscriminantType, discriminant: usize) -> TokenStream2 {
match discriminant_type {
DiscriminantType::UnsignedByte => {
let u8 = discriminant as u8;
quote!(#u8)
}
DiscriminantType::VarInt => {
let i32 = discriminant as i32;
quote!(#i32)
}
}
}
fn render_field_names_joined_comma(fields: &Vec<FieldData>) -> TokenStream2 {
fields.iter().map(|f| f.name).map(|n| quote!(#n,)).collect()
}
fn render_fields(fields: &Vec<FieldData>) -> TokenStream2 {
fields.iter().map(|f| render_field(f)).collect()
}
fn render_field(field: &FieldData) -> TokenStream2 {
let name = field.name;
let ty = field.ty;
match &field.attribute {
AttributeData::With { module } => render_with_field(name, module),
AttributeData::MaxLength { length } => render_max_length_field(name, *length as u16),
AttributeData::Bitfield { idx, position } => render_bitfield(name, *idx, position),
AttributeData::Empty => render_simple_field(name, ty),
}
}
fn render_simple_field(name: &Ident, ty: &Type) -> TokenStream2 {
quote! {
let #name = <#ty as crate::decoder::Decoder>::decode(reader)?;
}
}
fn render_with_field(name: &Ident, module: &str) -> TokenStream2 {
let module_ident = Ident::new(module, Span::call_site());
quote! {
let #name = crate::decoder::#module_ident::decode(reader)?;
}
}
fn render_max_length_field(name: &Ident, max_length: u16) -> TokenStream2 {
quote! {
let #name = crate::decoder::DecoderReadExt::read_string(reader, #max_length)?;
}
}
fn render_bitfield(name: &Ident, idx: u8, position: &BitfieldPosition) -> TokenStream2 {
let mask = 1u8 << idx;
let render_mask = quote! {
let #name = flags & #mask > 0;
};
match position {
BitfieldPosition::Start => {
quote! {
let flags = reader.read_u8()?;
#render_mask
}
}
_ => render_mask,
}
}

View File

@ -1,191 +0,0 @@
use crate::parse::{AttributeData, BitfieldPosition, DiscriminantType, FieldData, VariantData};
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span};
use quote::quote;
pub(crate) fn render_struct_encoder(name: &Ident, fields: &Vec<FieldData>) -> TokenStream2 {
let render_fields = render_fields(fields, true);
quote! {
#[automatically_derived]
impl crate::encoder::Encoder for #name {
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::error::EncodeError> {
#render_fields
Ok(())
}
}
}
}
pub(crate) fn render_enum_encoder(
name: &Ident,
discriminant_type: &DiscriminantType,
variants: &Vec<VariantData>,
) -> TokenStream2 {
let render_variants = render_variants(discriminant_type, variants);
quote! {
#[automatically_derived]
impl crate::encoder::Encoder for #name {
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::error::EncodeError> {
match self {
#render_variants
}
Ok(())
}
}
}
}
fn render_variants(
discriminant_type: &DiscriminantType,
variants: &Vec<VariantData>,
) -> TokenStream2 {
variants
.iter()
.map(|v| render_variant(discriminant_type, v))
.collect()
}
fn render_variant(discriminant_type: &DiscriminantType, variant: &VariantData) -> TokenStream2 {
if variant.fields.is_empty() {
render_unit_variant(discriminant_type, variant)
} else {
render_struct_variant(discriminant_type, variant)
}
}
fn render_unit_variant(
discriminant_type: &DiscriminantType,
variant: &VariantData,
) -> TokenStream2 {
let discriminant = variant.discriminant;
let name = variant.name;
let render_discriminant_type = render_discriminant_type(discriminant_type, discriminant);
quote! {
Self::#name => {
#render_discriminant_type
}
}
}
fn render_struct_variant(
discriminant_type: &DiscriminantType,
variant: &VariantData,
) -> TokenStream2 {
let discriminant = variant.discriminant;
let name = variant.name;
let fields = &variant.fields;
let render_discriminant_type = render_discriminant_type(discriminant_type, discriminant);
let field_names_joined_comma = render_field_names_joined_comma(fields);
let render_fields = render_fields(fields, false);
quote! {
Self::#name {
#field_names_joined_comma
} => {
#render_discriminant_type
#render_fields
}
}
}
fn render_discriminant_type(
discriminant_type: &DiscriminantType,
discriminant: usize,
) -> TokenStream2 {
match discriminant_type {
DiscriminantType::UnsignedByte => {
let u8 = discriminant as u8;
quote!(writer.write_u8(#u8)?;)
}
DiscriminantType::VarInt => {
let var_i32 = discriminant as i32;
quote!(writer.write_var_i32(#var_i32)?;)
}
}
}
fn render_field_names_joined_comma(fields: &Vec<FieldData>) -> TokenStream2 {
fields.iter().map(|f| f.name).map(|n| quote!(#n,)).collect()
}
fn render_fields(fields: &Vec<FieldData>, with_self: bool) -> TokenStream2 {
fields.iter().map(|f| render_field(f, with_self)).collect()
}
fn render_field(field: &FieldData, with_self: bool) -> TokenStream2 {
let name = field.name;
match &field.attribute {
AttributeData::With { module } => render_with_field(name, module, with_self),
AttributeData::MaxLength { length } => {
render_max_length_field(name, *length as u16, with_self)
}
AttributeData::Bitfield { idx, position } => render_bitfield(name, *idx, position),
AttributeData::Empty => render_simple_field(name, with_self),
}
}
fn render_simple_field(name: &Ident, with_self: bool) -> TokenStream2 {
render_with_field(name, "Encoder", with_self)
}
fn render_with_field(name: &Ident, module: &str, with_self: bool) -> TokenStream2 {
let module_ident = Ident::new(module, Span::call_site());
let final_name = get_field_final_name(name, with_self);
quote! {
crate::encoder::#module_ident::encode(#final_name, writer)?;
}
}
fn render_max_length_field(name: &Ident, max_length: u16, with_self: bool) -> TokenStream2 {
let final_name = get_field_final_name(name, with_self);
quote! {
crate::encoder::EncoderWriteExt::write_string(writer, #final_name, #max_length)?;
}
}
fn render_bitfield(name: &Ident, idx: u8, position: &BitfieldPosition) -> TokenStream2 {
let mask = 1u8 << idx;
let render_mask = quote! {
if self.#name {
flags |= #mask;
}
};
match position {
BitfieldPosition::Start => quote!(
let mut flags = 0;
#render_mask
),
BitfieldPosition::Intermediate => render_mask,
BitfieldPosition::End => {
quote! {
#render_mask
writer.write_u8(flags)?;
}
}
}
}
fn get_field_final_name(name: &Ident, with_self: bool) -> TokenStream2 {
if with_self {
quote!(&self.#name)
} else {
quote!(#name)
}
}

View File

@ -1,2 +0,0 @@
pub(crate) mod decoder;
pub(crate) mod encoder;

View File

@ -0,0 +1,17 @@
[package]
name = "minecraft-protocol-generator"
version = "0.0.0"
authors = ["vagola <vladislavs.golubs@yandex.ru>"]
edition = "2018"
description = "CLI for generating Rust code with Minecraft packets"
license = "MIT"
homepage = "https://github.com/eihwaz/minecraft-protocol"
repository = "https://github.com/eihwaz/minecraft-protocol"
keywords = ["minecraft", "cli", "protocol", "packet", "io"]
[dependencies]
serde = "1.0.120"
serde_json = "1.0"
handlebars = "3.5.2"
heck = "0.3.2"
linked-hash-map = { version = "0.5.4", features = ["serde_impl"] }

@ -0,0 +1 @@
Subproject commit cc155f399397755abeb9275de40fc82b65a252f4

View File

@ -0,0 +1,101 @@
use linked_hash_map::LinkedHashMap;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct ProtocolHandler {
pub handshaking: Protocol,
pub status: Protocol,
pub login: Protocol,
#[serde(rename = "play")]
pub game: Protocol,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Protocol {
pub to_client: Packets,
pub to_server: Packets,
}
#[derive(Debug, Deserialize)]
pub struct Packets {
pub types: LinkedHashMap<String, Vec<Data>>,
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum Data {
Type(String),
Containers(Vec<Container>),
Container(Box<Container>),
Mapper {
#[serde(rename = "type")]
mappings_type: String,
mappings: LinkedHashMap<String, String>,
},
Switch(Switch),
List(Box<List>),
Bitfield(Vec<BitField>),
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum Container {
Value {
name: String,
#[serde(rename = "type")]
data: Data,
},
List {
name: Option<String>,
#[serde(rename = "type")]
data_vec: Vec<Data>,
},
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum Switch {
Value {
#[serde(rename = "compareTo")]
compare_to: String,
fields: LinkedHashMap<String, Data>,
},
List {
#[serde(rename = "compareTo")]
compare_to: String,
fields: LinkedHashMap<String, Vec<Data>>,
},
Empty {
#[serde(rename = "compareTo")]
compare_to: String,
},
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum List {
Value {
#[serde(rename = "countType")]
count_type: String,
#[serde(rename = "type")]
list_type: Data,
},
List {
#[serde(rename = "countType")]
count_type: String,
#[serde(rename = "type")]
list_type: Vec<Data>,
},
Empty {
#[serde(rename = "countType")]
count_type: String,
},
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
pub struct BitField {
name: String,
size: usize,
signed: bool,
}

View File

@ -0,0 +1,309 @@
use crate::mappings::Mappings;
use crate::{backend, frontend, transformers};
use handlebars::{Handlebars, TemplateRenderError};
use serde::Serialize;
use serde_json::json;
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::fmt::Display;
use std::fs::{create_dir_all, File};
use std::io::Write;
use std::path::Path;
#[derive(Debug)]
pub enum State {
Handshake,
Status,
Login,
Game,
}
impl State {
pub fn data_import(&self) -> &'static str {
match self {
State::Handshake => "crate::data::handshake::*",
State::Status => "crate::data::status::*",
State::Login => "crate::data::login::*",
State::Game => "crate::data::game::*",
}
}
}
impl Display for State {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
State::Handshake => "Handshake",
State::Status => "Status",
State::Login => "Login",
State::Game => "Game",
};
write!(f, "{}", name)
}
}
pub enum Bound {
Server,
Client,
}
impl Display for Bound {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
Bound::Server => "Server",
Bound::Client => "Client",
};
write!(f, "{}", name)
}
}
#[derive(Serialize, Debug)]
pub struct Packet {
pub id: u8,
pub name: String,
pub fields: Vec<Field>,
}
impl Packet {
pub fn new(id: u8, name: impl ToString, fields: Vec<Field>) -> Packet {
Packet {
id,
name: name.to_string(),
fields,
}
}
}
#[derive(Serialize, Debug)]
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,
}
}
pub fn change_type(&self, data_type: DataType) -> Field {
Field::new(&self.name, data_type)
}
}
#[derive(Serialize, Eq, PartialEq, Debug)]
#[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 = "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,
},
#[serde(rename(serialize = "Message"))]
Chat,
}
impl DataType {
pub fn import(&self, state: &State) -> Option<&'static str> {
match self {
DataType::Uuid { .. } => Some("uuid::Uuid"),
DataType::CompoundTag => Some("nbt::CompoundTag"),
DataType::RefType { .. } => Some(state.data_import()),
DataType::Chat => Some("crate::data::chat::Message"),
_ => None,
}
}
}
#[derive(Debug)]
pub struct Protocol {
pub server_bound_packets: Vec<Packet>,
pub client_bound_packets: Vec<Packet>,
}
impl Protocol {
pub fn new(server_bound_packets: Vec<Packet>, client_bound_packets: Vec<Packet>) -> Protocol {
Protocol {
server_bound_packets,
client_bound_packets,
}
}
pub fn data_type_imports(&self, state: &State) -> HashSet<&'static str> {
self.server_bound_packets
.iter()
.chain(self.client_bound_packets.iter())
.flat_map(|p| p.fields.iter())
.filter_map(|f| f.data_type.import(state))
.collect()
}
}
pub fn generate_rust_files<M: Mappings>(
versions_data: HashMap<String, File>,
template_engine: &Handlebars,
mappings: &M,
) -> Result<(), TemplateRenderError> {
generate_versions_module_file(template_engine, versions_data.keys().cloned().collect())?;
for (version, data_file) in versions_data.iter() {
println!("Generating protocol data for version {}", version);
let protocol_handler: backend::ProtocolHandler =
serde_json::from_reader(data_file).expect("Failed to parse protocol data");
let frontend_protocols =
transformers::transform_protocol_handler(mappings, &protocol_handler);
let formatted_version = version.replace(".", "_").replace("-", "_");
let folder_name = format!("protocol/src/version/v_{}", formatted_version);
let folder_path = Path::new(&folder_name);
generate_protocol_module_file(template_engine, &folder_path)?;
for (protocol, state) in frontend_protocols {
let file_name = format!("{}.rs", state.to_string().to_lowercase());
let mut file = File::create(folder_path.join(file_name))
.expect("Failed to create protocol enum file");
generate_protocol_enum_header(template_engine, &protocol, &state, &mut file)?;
generate_protocol_enum_content(
template_engine,
&protocol.server_bound_packets,
&state,
&Bound::Server,
&mut file,
)?;
generate_protocol_enum_content(
template_engine,
&protocol.client_bound_packets,
&state,
&Bound::Client,
&mut file,
)?;
generate_packets_structs(template_engine, &protocol.server_bound_packets, &mut file)?;
generate_packets_structs(template_engine, &protocol.client_bound_packets, &mut file)?;
}
}
Ok(())
}
fn generate_versions_module_file(
template_engine: &Handlebars,
versions: Vec<String>,
) -> Result<(), TemplateRenderError> {
let mut file =
File::create("protocol/src/version/mod.rs").expect("Failed to create versions module file");
let ctx = json!({ "versions": versions });
template_engine.render_to_write("protocol_versions_module", &ctx, &mut file)?;
Ok(())
}
fn generate_protocol_module_file(
template_engine: &Handlebars,
folder_path: &Path,
) -> Result<(), TemplateRenderError> {
generate_module_file(template_engine, folder_path, "protocol_module")
}
fn generate_module_file(
template_engine: &Handlebars,
folder_path: &Path,
name: &str,
) -> Result<(), TemplateRenderError> {
create_dir_all(folder_path).expect("Failed to create module folder");
let mut file = File::create(folder_path.join("mod.rs")).expect("Failed to create module file");
template_engine.render_to_write(name, &(), &mut file)?;
Ok(())
}
fn generate_protocol_enum_header<W: Write>(
template_engine: &Handlebars,
protocol: &frontend::Protocol,
state: &frontend::State,
write: &mut W,
) -> Result<(), TemplateRenderError> {
let imports = protocol.data_type_imports(state);
let ctx = json!({ "imports": imports });
template_engine.render_to_write("protocol_header", &ctx, write)?;
Ok(())
}
fn generate_protocol_enum_content<W: Write>(
template_engine: &Handlebars,
packets: &Vec<Packet>,
state: &frontend::State,
bound: &frontend::Bound,
write: &mut W,
) -> Result<(), TemplateRenderError> {
let protocol_enum_name = format!("{}Bound{}Packet", bound, state);
let ctx = json!({ "protocol_enum_name": protocol_enum_name, "packets": packets });
template_engine.render_to_write("protocol_enum", &ctx, write)?;
Ok(())
}
fn generate_packets_structs<W: Write>(
template_engine: &Handlebars,
packets: &Vec<Packet>,
write: &mut W,
) -> Result<(), TemplateRenderError> {
let ctx = json!({ "packets": packets });
template_engine.render_to_write("packets_structs", &ctx, write)?;
Ok(())
}

View File

@ -0,0 +1,57 @@
use std::fs::File;
use crate::mappings::CodeMappings;
use std::fs;
use std::io::Error;
use std::path::Path;
pub mod backend;
pub mod frontend;
pub mod mappings;
pub mod templates;
pub mod transformers;
pub fn main() {
let paths = fs::read_dir("protocol-generator/minecraft-data/data/pc/")
.expect("Failed to open data folder");
let versions_data = paths
.into_iter()
.map(|entry| {
entry
.expect("Failed to get dir entry")
.file_name()
.into_string()
.expect("Failed to get version string")
})
.filter(|version| match version.as_str() {
"0.30c" => false, // A very old version with a lot of incompatibility.
"1.7" => false, // Requires some fixes to support.
_ => true,
})
.filter_map(|version| {
let protocol_data_file_name = format!(
"protocol-generator/minecraft-data/data/pc/{}/protocol.json",
version
);
let protocol_data_file_path = Path::new(&protocol_data_file_name);
match protocol_data_file_path.exists() {
true => {
let protocol_data_file = File::open(protocol_data_file_path)
.expect("Failed to open protocol data file");
Some((version, protocol_data_file))
}
false => None,
}
})
.collect();
let template_engine = templates::create_template_engine();
let mappings = CodeMappings::new();
frontend::generate_rust_files(versions_data, &template_engine, &mappings)
.expect("Failed to generate rust files");
}

View File

@ -0,0 +1,71 @@
use crate::backend;
use crate::frontend;
pub trait Mappings {
fn rename_packet(
&self,
unformatted_name: &str,
name: &str,
bound: &frontend::Bound,
protocol: &backend::Protocol,
) -> String;
fn change_field_type(&self, packet_name: &str, field: frontend::Field) -> frontend::Field;
}
pub struct CodeMappings {}
impl CodeMappings {
pub fn new() -> CodeMappings {
CodeMappings {}
}
}
impl Mappings for CodeMappings {
fn rename_packet(
&self,
unformatted_name: &str,
name: &str,
bound: &frontend::Bound,
protocol: &backend::Protocol,
) -> String {
let new_name = match (name, bound) {
("EncryptionBegin", frontend::Bound::Server) => "EncryptionResponse",
("EncryptionBegin", frontend::Bound::Client) => "EncryptionRequest",
("PingStart", frontend::Bound::Server) => "StatusRequest",
("Ping", frontend::Bound::Server) => "PingRequest",
("ServerInfo", frontend::Bound::Client) => "StatusResponse",
("Ping", frontend::Bound::Client) => "PingResponse",
("Login", frontend::Bound::Client) => "JoinGame",
_ => name,
}
.to_owned();
if new_name == name
&& protocol.to_client.types.contains_key(unformatted_name)
&& protocol.to_server.types.contains_key(unformatted_name)
{
match bound {
frontend::Bound::Server => format!("ServerBound{}", name),
frontend::Bound::Client => format!("ClientBound{}", name),
}
} else {
new_name.to_owned()
}
}
fn change_field_type(&self, packet_name: &str, field: frontend::Field) -> frontend::Field {
match (packet_name, field.name.as_str()) {
// ("StatusResponse", "response") => field.change_type(frontend::DataType::RefType {
// ref_name: "ServerStatus".to_owned(),
// }),
// ("Success", "uuid") => field.change_type(frontend::DataType::Uuid { hyphenated: true }),
// ("Disconnect", "reason") => field.change_type(frontend::DataType::Chat),
// ("ClientBoundChat", "message") => field.change_type(frontend::DataType::Chat),
// ("ClientBoundChat", "position") => field.change_type(frontend::DataType::RefType {
// ref_name: "MessagePosition".to_owned(),
// }),
_ => field,
}
}
}

View File

@ -0,0 +1,89 @@
use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError};
use heck::SnakeCase;
pub fn create_template_engine() -> 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, "protocol_versions_module");
register_template_file(&mut template_engine, "protocol_module");
register_template_file(&mut template_engine, "protocol_enum");
register_template_file(&mut template_engine, "packets_structs");
register_template_file(&mut template_engine, "protocol_header");
template_engine
}
fn register_template_file(template_engine: &mut Handlebars, name: &str) {
template_engine
.register_template_file(name, format!("protocol-generator/templates/{}.hbs", name))
.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(())
}

View File

@ -0,0 +1,241 @@
use crate::mappings::Mappings;
use crate::{backend, frontend};
use heck::{CamelCase, SnakeCase};
use std::collections::HashMap;
pub fn transform_protocol_handler<M: Mappings>(
mappings: &M,
protocol_handler: &backend::ProtocolHandler,
) -> Vec<(frontend::Protocol, frontend::State)> {
vec![
(
transform_protocol::<M>(&mappings, &protocol_handler.handshaking),
frontend::State::Handshake,
),
(
transform_protocol::<M>(&mappings, &protocol_handler.status),
frontend::State::Status,
),
(
transform_protocol::<M>(&mappings, &protocol_handler.login),
frontend::State::Login,
),
(
transform_protocol::<M>(&mappings, &protocol_handler.game),
frontend::State::Game,
),
]
}
fn transform_protocol<M: Mappings>(
mappings: &M,
protocol: &backend::Protocol,
) -> frontend::Protocol {
let server_bound_packets = transform_packets(
mappings,
protocol,
&protocol.to_server,
frontend::Bound::Server,
);
let client_bound_packets = transform_packets(
mappings,
protocol,
&protocol.to_client,
frontend::Bound::Client,
);
frontend::Protocol {
server_bound_packets,
client_bound_packets,
}
}
fn get_packet_ids(packets: &backend::Packets) -> HashMap<String, u8> {
let reversed_packet_ids = packets
.types
.get("packet")
.and_then(|d| d.get(1))
.and_then(|d| match d {
backend::Data::Containers(data) => data.get(0),
_ => None,
})
.and_then(|c| match c {
backend::Container::List { data_vec, .. } => data_vec.get(1),
_ => None,
})
.and_then(|d| match d {
backend::Data::Mapper { mappings, .. } => Some(mappings),
_ => None,
})
.expect("Failed to get packet ids");
reversed_packet_ids
.into_iter()
.map(|(k, v)| {
(
v.clone(),
u8::from_str_radix(k.trim_start_matches("0x"), 16).expect("Invalid packet id"),
)
})
.collect()
}
fn transform_packets<M: Mappings>(
mappings: &M,
protocol: &backend::Protocol,
packets: &backend::Packets,
bound: frontend::Bound,
) -> Vec<frontend::Packet> {
let packet_ids = get_packet_ids(packets);
let mut output_packets = vec![];
for (unformatted_name, data_vec) in packets.types.iter() {
if !unformatted_name.starts_with("packet_")
|| unformatted_name == "packet_legacy_server_list_ping"
{
continue;
}
let no_prefix_unformatted = unformatted_name.trim_start_matches("packet_");
let id = *packet_ids
.get(no_prefix_unformatted)
.expect("Failed to get packet id");
let packet_name = mappings.rename_packet(
unformatted_name,
&no_prefix_unformatted.to_camel_case(),
&bound,
protocol,
);
let mut fields = vec![];
for data in data_vec {
if let backend::Data::Containers(container_vec) = data {
for container in container_vec {
match container {
backend::Container::Value { name, data } => {
match transform_value_field(&name, &data) {
Some(field) => {
fields.push(mappings.change_field_type(&packet_name, field))
}
None => println!(
"[{}] Field \"{}\" are skipped ({:?}",
packet_name, name, data
),
}
}
backend::Container::List { name, data_vec } => {
if let Some(name) = name {
match transform_list_field(&name, data_vec) {
Some(field) => {
fields.push(mappings.change_field_type(&packet_name, field))
}
None => {}
}
}
}
}
}
}
}
let packet = frontend::Packet {
id,
name: packet_name,
fields,
};
output_packets.push(packet);
}
output_packets
}
fn transform_value_field(
unformatted_field_name: &str,
data: &backend::Data,
) -> Option<frontend::Field> {
match data {
backend::Data::Type(name) => match transform_data_type(name) {
Some(data_type) => Some(frontend::Field {
name: format_field_name(unformatted_field_name),
data_type,
}),
None => None,
},
_ => None,
}
}
fn transform_list_field(
unformatted_field_name: &str,
data_vec: &Vec<backend::Data>,
) -> Option<frontend::Field> {
match &data_vec[0] {
backend::Data::Type(name) => match name.as_ref() {
"buffer" => Some(frontend::Field {
name: format_field_name(unformatted_field_name),
data_type: frontend::DataType::ByteArray { rest: false },
}),
"array" => None,
"switch" => None,
"particleData" => Some(frontend::Field {
name: format_field_name(unformatted_field_name),
data_type: frontend::DataType::RefType {
ref_name: "ParticleData".to_string(),
},
}),
"option" => transform_value_field(unformatted_field_name, &data_vec[1]),
_ => None,
},
_ => None,
}
}
fn transform_data_type(name: &str) -> Option<frontend::DataType> {
match name {
"bool" => Some(frontend::DataType::Boolean),
"i8" => Some(frontend::DataType::Byte),
"i16" => Some(frontend::DataType::Short),
"i32" => Some(frontend::DataType::Int { var_int: false }),
"i64" => Some(frontend::DataType::Long { var_long: false }),
"u8" => Some(frontend::DataType::UnsignedByte),
"u16" => Some(frontend::DataType::UnsignedShort),
"u32" => Some(frontend::DataType::UnsignedInt),
"f32" => Some(frontend::DataType::Float),
"f64" => Some(frontend::DataType::Double),
"varint" => Some(frontend::DataType::Int { var_int: true }),
"varlong" => Some(frontend::DataType::Long { var_long: true }),
"string" => Some(frontend::DataType::String { max_length: 0 }),
"nbt" | "optionalNbt" => Some(frontend::DataType::CompoundTag),
"UUID" => Some(frontend::DataType::Uuid { hyphenated: false }),
"restBuffer" => Some(frontend::DataType::ByteArray { rest: true }),
"position" => Some(frontend::DataType::RefType {
ref_name: "Position".to_string(),
}),
"slot" => Some(frontend::DataType::RefType {
ref_name: "Option<Slot>".to_string(),
}),
"entityMetadata" => Some(frontend::DataType::RefType {
ref_name: "Metadata".to_string(),
}),
"tags" => Some(frontend::DataType::RefType {
ref_name: "TagsMap".to_string(),
}),
_ => {
println!("Unknown data type \"{}\"", name);
None
}
}
}
fn format_field_name(unformatted_field_name: &str) -> String {
if unformatted_field_name == "type" {
String::from("type_")
} else {
unformatted_field_name.to_snake_case()
}
}

View File

@ -0,0 +1,24 @@
{{~#each packets as |p|}}
{{~#if p.fields}}
#[derive(Packet, Debug)]
pub struct {{p.name}} {
{{~#each p.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}}{{#unless @last}},{{/unless}}
{{~else}}
pub {{f.name}}: {{f.ref_name}}{{#unless @last}},{{/unless}}
{{~/if}}
{{~/each}}
}
{{/if}}
{{~/each}}

View File

@ -0,0 +1,51 @@
{{~#if packets}}
pub enum {{protocol_enum_name}} {
{{~#each packets as |p|}}
{{p.name}}{{#if p.fields}}({{p.name}}){{/if}},
{{~/each}}
}
impl {{protocol_enum_name}} {
pub fn get_type_id(&self) -> u8 {
match self {
{{~#each packets as |p|}}
Self::{{p.name}}{{#if p.fields}}(_){{/if}} => {{packet_id p.id}},
{{~/each}}
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
{{~#each packets as |p|}}
{{packet_id p.id}} => {
{{~#if p.fields}}
let {{snake_case p.name}} = {{p.name}}::decode(reader)?;
Ok(Self::{{p.name}}({{snake_case p.name}}))
{{~/if}}
{{~#unless p.fields}}
Ok(Self::{{p.name}})
{{~/unless}}
}
{{~/each}}
_ => Err(DecodeError::UnknownPacketType { type_id })
}
}
{{#each packets as |p|}}
pub fn {{snake_case p.name}}({{~#each p.fields as |f|}}{{f.name}}: {{#if (ne f.type "RefType")}}{{f.type}}{{~else}}{{f.ref_name}}{{/if}}{{#unless @last}}, {{/unless}}{{~/each}}) -> Self {
{{~#if p.fields}}
let {{snake_case p.name}} = {{p.name}} {
{{~#each p.fields as |f|}}
{{f.name}},
{{~/each}}
};
Self::{{p.name}}({{snake_case p.name}})
{{~/if}}
{{~#unless p.fields}}
Self::{{p.name}}
{{~/unless}}
}
{{/each~}}
}
{{~/if}}

View File

@ -0,0 +1,10 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use std::io::Read;
use minecraft_protocol_derive::Packet;
{{#each imports as |i|~}}
use {{i}};
{{/each~}}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod handshake;
pub mod status;
pub mod login;
pub mod game;

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
{{#each versions as |v|~}}
#[cfg(feature = "{{v}}")]
pub mod {{protocol_version_module v}};
{{/each~}}

View File

@ -16,4 +16,6 @@ byteorder = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
uuid = { version = "0.7", features = ["v4", "serde"] }
named-binary-tag = "0.6"
num-traits = "0.2"
num-derive = "0.2"
named-binary-tag = "0.2"

View File

@ -6,7 +6,7 @@
//! ## Serialize
//!
//! ```
//! use minecraft_protocol::data::chat::{MessageBuilder, Payload, Color};
//! use minecraft_protocol::data::chat::{Payload, Color, MessageBuilder};
//!
//! let message = MessageBuilder::builder(Payload::text("Hello"))
//! .color(Color::Yellow)
@ -25,7 +25,7 @@
//! ## Deserialize
//!
//! ```
//! use minecraft_protocol::data::chat::{MessageBuilder, Payload, Message, Color};
//! use minecraft_protocol::data::chat::{MessageBuilder, Color, Payload, Message};
//!
//! let json = r#"
//! {
@ -68,7 +68,7 @@ use serde::{
};
use serde_json::Error;
#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
pub enum Color {
Black,
DarkBlue,
@ -175,7 +175,7 @@ impl<'de> Deserialize<'de> for Color {
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ClickAction {
OpenUrl,
@ -184,7 +184,7 @@ pub enum ClickAction {
ChangePage,
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ClickEvent {
pub action: ClickAction,
pub value: String,
@ -199,7 +199,7 @@ impl ClickEvent {
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HoverAction {
ShowText,
@ -207,7 +207,7 @@ pub enum HoverAction {
ShowEntity,
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct HoverEvent {
pub action: HoverAction,
pub value: String,
@ -222,7 +222,7 @@ impl HoverEvent {
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Payload {
Text {
@ -280,7 +280,7 @@ impl Payload {
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Message {
#[serde(skip_serializing_if = "Option::is_none")]
@ -324,10 +324,6 @@ impl Message {
}
}
pub fn from_str(text: &str) -> Message {
Message::new(Payload::text(text))
}
pub fn from_json(json: &str) -> Result<Self, Error> {
serde_json::from_str(json)
}

View File

@ -3,31 +3,30 @@ use crate::impl_json_encoder_decoder;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct ServerVersion {
pub name: String,
pub protocol: u32,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct OnlinePlayer {
pub name: String,
pub id: Uuid,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct OnlinePlayers {
pub max: u32,
pub online: u32,
pub sample: Vec<OnlinePlayer>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ServerStatus {
pub version: ServerVersion,
pub players: OnlinePlayers,
pub description: Message,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ServerVersion {
pub name: String,
pub protocol: u32,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct OnlinePlayers {
pub max: u32,
pub online: u32,
#[serde(default)]
pub sample: Vec<OnlinePlayer>,
}
#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct OnlinePlayer {
pub name: String,
pub id: Uuid,
}
impl_json_encoder_decoder!(ServerStatus);

View File

@ -1,291 +0,0 @@
use crate::error::DecodeError;
use byteorder::{BigEndian, ReadBytesExt};
use nbt::CompoundTag;
use std::io::Read;
use uuid::Uuid;
pub trait Decoder {
type Output;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError>;
}
/// Trait adds additional helper methods for `Read` to read protocol data.
pub trait DecoderReadExt {
fn read_bool(&mut self) -> Result<bool, DecodeError>;
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError>;
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError>;
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError>;
fn read_var_i32(&mut self) -> Result<i32, DecodeError>;
fn read_var_i64(&mut self) -> Result<i64, DecodeError>;
}
macro_rules! read_signed_var_int (
($type: ident, $name: ident, $max_bytes: expr) => (
fn $name(&mut self) -> Result<$type, DecodeError> {
let mut bytes = 0;
let mut output = 0;
loop {
let byte = self.read_u8()?;
let value = (byte & 0b01111111) as $type;
output |= value << 7 * bytes;
bytes += 1;
if bytes > $max_bytes {
return Err(DecodeError::VarIntTooLong { max_bytes: $max_bytes })
}
if (byte & 0b10000000) == 0 {
break;
}
}
Ok(output)
}
);
);
impl<R: Read> DecoderReadExt for R {
fn read_bool(&mut self) -> Result<bool, DecodeError> {
match self.read_u8()? {
0 => Ok(false),
1 => Ok(true),
_ => Err(DecodeError::NonBoolValue),
}
}
fn read_string(&mut self, max_length: u16) -> Result<String, DecodeError> {
let length = self.read_var_i32()? as usize;
if length as u16 > max_length {
return Err(DecodeError::StringTooLong { length, max_length });
}
let mut buf = vec![0; length as usize];
self.read_exact(&mut buf)?;
Ok(String::from_utf8(buf)?)
}
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError> {
let length = self.read_var_i32()?;
let mut buf = vec![0; length as usize];
self.read_exact(&mut buf)?;
Ok(buf)
}
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError> {
Ok(nbt::decode::read_compound_tag(self)?)
}
read_signed_var_int!(i32, read_var_i32, 5);
read_signed_var_int!(i64, read_var_i64, 10);
}
impl Decoder for u8 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_u8()?)
}
}
impl Decoder for i16 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_i16::<BigEndian>()?)
}
}
impl Decoder for i32 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_i32::<BigEndian>()?)
}
}
impl Decoder for u16 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_u16::<BigEndian>()?)
}
}
impl Decoder for u32 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_u32::<BigEndian>()?)
}
}
impl Decoder for i64 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_i64::<BigEndian>()?)
}
}
impl Decoder for u64 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_u64::<BigEndian>()?)
}
}
impl Decoder for f32 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_f32::<BigEndian>()?)
}
}
impl Decoder for f64 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_f64::<BigEndian>()?)
}
}
impl Decoder for String {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_string(32_768)?)
}
}
impl Decoder for bool {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_bool()?)
}
}
impl Decoder for Vec<u8> {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_byte_array()?)
}
}
impl Decoder for Uuid {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
let mut buf = [0; 16];
reader.read_exact(&mut buf)?;
Ok(Uuid::from_bytes(buf))
}
}
impl Decoder for CompoundTag {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_compound_tag()?)
}
}
impl Decoder for Vec<CompoundTag> {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
let length = reader.read_var_i32()? as usize;
let mut vec = Vec::with_capacity(length);
for _ in 0..length {
let compound_tag = reader.read_compound_tag()?;
vec.push(compound_tag);
}
Ok(vec)
}
}
pub mod var_int {
use crate::decoder::DecoderReadExt;
use crate::error::DecodeError;
use std::io::Read;
pub fn decode<R: Read>(reader: &mut R) -> Result<i32, DecodeError> {
Ok(reader.read_var_i32()?)
}
}
pub mod var_long {
use crate::decoder::DecoderReadExt;
use crate::error::DecodeError;
use std::io::Read;
pub fn decode<R: Read>(reader: &mut R) -> Result<i64, DecodeError> {
Ok(reader.read_var_i64()?)
}
}
pub mod rest {
use crate::error::DecodeError;
use std::io::Read;
pub fn decode<R: Read>(reader: &mut R) -> Result<Vec<u8>, DecodeError> {
let mut data = Vec::new();
reader.read_to_end(data.as_mut())?;
Ok(data)
}
}
pub mod uuid_hyp_str {
use crate::decoder::DecoderReadExt;
use crate::error::DecodeError;
use std::io::Read;
use uuid::Uuid;
pub fn decode<R: Read>(reader: &mut R) -> Result<Uuid, DecodeError> {
let uuid_hyphenated_string = reader.read_string(36)?;
let uuid = Uuid::parse_str(&uuid_hyphenated_string)?;
Ok(uuid)
}
}
#[cfg(test)]
mod tests {
use crate::decoder::DecoderReadExt;
use std::io::Cursor;
#[test]
fn test_read_variable_i32_2_bytes_value() {
let mut cursor = Cursor::new(vec![0b10101100, 0b00000010]);
let value = cursor.read_var_i32().unwrap();
assert_eq!(value, 300);
}
#[test]
fn test_read_variable_i32_5_bytes_value() {
let mut cursor = Cursor::new(vec![0xff, 0xff, 0xff, 0xff, 0x07]);
let value = cursor.read_var_i32().unwrap();
assert_eq!(value, 2147483647);
}
}

View File

@ -1,255 +0,0 @@
use crate::error::EncodeError;
use byteorder::{BigEndian, WriteBytesExt};
use nbt::CompoundTag;
use std::io::Write;
use uuid::Uuid;
pub trait Encoder {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError>;
}
/// Trait adds additional helper methods for `Write` to write protocol data.
pub trait EncoderWriteExt {
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError>;
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError>;
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError>;
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError>;
fn write_var_i32(&mut self, value: i32) -> Result<(), EncodeError>;
fn write_var_i64(&mut self, value: i64) -> Result<(), EncodeError>;
}
macro_rules! write_signed_var_int (
($type: ident, $name: ident) => (
fn $name(&mut self, mut value: $type) -> Result<(), EncodeError> {
loop {
let mut byte = (value & 0b01111111) as u8;
value = value >> 7;
if value != 0 {
byte |= 0b10000000;
}
self.write_u8(byte)?;
if value == 0 {
break;
}
}
Ok(())
}
)
);
impl<W: Write> EncoderWriteExt for W {
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError> {
if value {
self.write_u8(1)?;
} else {
self.write_u8(0)?;
}
Ok(())
}
fn write_string(&mut self, value: &str, max_length: u16) -> Result<(), EncodeError> {
let length = value.len();
if length > max_length as usize {
return Err(EncodeError::StringTooLong { length, max_length });
}
self.write_var_i32(value.len() as i32)?;
self.write_all(value.as_bytes())?;
Ok(())
}
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError> {
self.write_var_i32(value.len() as i32)?;
self.write_all(value)?;
Ok(())
}
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError> {
nbt::encode::write_compound_tag(self, &value)?;
Ok(())
}
write_signed_var_int!(i32, write_var_i32);
write_signed_var_int!(i64, write_var_i64);
}
impl Encoder for u8 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_u8(*self)?)
}
}
impl Encoder for i16 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_i16::<BigEndian>(*self)?)
}
}
impl Encoder for i32 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_i32::<BigEndian>(*self)?)
}
}
impl Encoder for u16 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_u16::<BigEndian>(*self)?)
}
}
impl Encoder for u32 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_u32::<BigEndian>(*self)?)
}
}
impl Encoder for i64 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_i64::<BigEndian>(*self)?)
}
}
impl Encoder for u64 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_u64::<BigEndian>(*self)?)
}
}
impl Encoder for f32 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_f32::<BigEndian>(*self)?)
}
}
impl Encoder for f64 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_f64::<BigEndian>(*self)?)
}
}
impl Encoder for String {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_string(self, 32_768)?)
}
}
impl Encoder for bool {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_bool(*self)?)
}
}
impl Encoder for Vec<u8> {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_byte_array(self)?)
}
}
impl Encoder for Uuid {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_all(self.as_bytes())?)
}
}
impl Encoder for CompoundTag {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_compound_tag(self)?)
}
}
impl Encoder for Vec<CompoundTag> {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
writer.write_var_i32(self.len() as i32)?;
for compound_tag in self {
writer.write_compound_tag(&compound_tag)?;
}
Ok(())
}
}
pub mod var_int {
use crate::encoder::EncoderWriteExt;
use crate::error::EncodeError;
use std::io::Write;
pub fn encode<W: Write>(value: &i32, writer: &mut W) -> Result<(), EncodeError> {
writer.write_var_i32(*value)?;
Ok(())
}
}
pub mod var_long {
use crate::encoder::EncoderWriteExt;
use crate::error::EncodeError;
use std::io::Write;
pub fn encode<W: Write>(value: &i64, writer: &mut W) -> Result<(), EncodeError> {
writer.write_var_i64(*value)?;
Ok(())
}
}
pub mod rest {
use crate::error::EncodeError;
use std::io::Write;
pub fn encode<W: Write>(value: &[u8], writer: &mut W) -> Result<(), EncodeError> {
writer.write_all(value)?;
Ok(())
}
}
pub mod uuid_hyp_str {
use crate::encoder::EncoderWriteExt;
use crate::error::EncodeError;
use std::io::Write;
use uuid::Uuid;
pub fn encode<W: Write>(value: &Uuid, writer: &mut W) -> Result<(), EncodeError> {
let uuid_hyphenated_string = value.to_hyphenated().to_string();
writer.write_string(&uuid_hyphenated_string, 36)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::encoder::EncoderWriteExt;
use std::io::Cursor;
#[test]
fn test_write_variable_i32_2_bytes_value() {
let mut cursor = Cursor::new(Vec::with_capacity(5));
cursor.write_var_i32(300).unwrap();
assert_eq!(cursor.into_inner(), vec![0b10101100, 0b00000010]);
}
#[test]
fn test_write_variable_i32_5_bytes_value() {
let mut cursor = Cursor::new(Vec::with_capacity(5));
cursor.write_var_i32(2147483647).unwrap();
assert_eq!(cursor.into_inner(), vec![0xff, 0xff, 0xff, 0xff, 0x07]);
}
}

View File

@ -63,9 +63,9 @@ pub enum DecodeError {
UuidParseError {
uuid_parse_error: UuidParseError,
},
/// Type id was not parsed as valid enum value.
// Type id was not parsed as valid enum value.
UnknownEnumType {
type_id: usize,
type_id: u8,
},
TagDecodeError {
tag_decode_error: TagDecodeError,

View File

@ -1,35 +1,620 @@
//! This crate implements Minecraft protocol.
//!
//! Information about protocol can be found at https://wiki.vg/Protocol.
use std::io::{Cursor, Read, Write};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use nbt::CompoundTag;
use num_traits::{FromPrimitive, ToPrimitive};
use uuid::Uuid;
use data::chat::Message;
use crate::error::{DecodeError, EncodeError};
pub mod data;
pub mod decoder;
pub mod encoder;
pub mod error;
pub mod version;
/// 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;
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 i8 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_i8(*self)?)
}
}
impl Decoder for i8 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_i8()?)
}
}
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 i16 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_i16::<BigEndian>(*self)?)
}
}
impl Decoder for i16 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_i16::<BigEndian>()?)
}
}
impl Encoder for u16 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_u16::<BigEndian>(*self)?)
}
}
impl Decoder for u16 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_u16::<BigEndian>()?)
}
}
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 f32 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_f32::<BigEndian>(*self)?)
}
}
impl Decoder for f32 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_f32::<BigEndian>()?)
}
}
impl Encoder for f64 {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
Ok(writer.write_f64::<BigEndian>(*self)?)
}
}
impl Decoder for f64 {
type Output = Self;
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
Ok(reader.read_f64::<BigEndian>()?)
}
}
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::Encoder for $ty {
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::error::EncodeError> {
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::encoder::EncoderWriteExt::write_string(writer, &json, crate::STRING_MAX_LENGTH)?;
crate::EncoderWriteExt::write_string(writer, &json, crate::STRING_MAX_LENGTH)?;
Ok(())
}
}
impl crate::decoder::Decoder for $ty {
impl crate::Decoder for $ty {
type Output = Self;
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
let json = crate::decoder::DecoderReadExt::read_string(reader, crate::STRING_MAX_LENGTH)?;
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 std::io::{Read, Write};
use crate::{DecodeError, EncodeError};
use crate::{DecoderReadExt, EncoderWriteExt};
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 std::io::{Read, Write};
use crate::{DecodeError, EncodeError};
use crate::{DecoderReadExt, EncoderWriteExt};
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 std::io::{Read, Write};
use crate::{DecodeError, EncodeError};
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 std::io::Cursor;
use std::io::{Read, Write};
use uuid::Uuid;
use crate::{
DecodeError, DecoderReadExt, EncodeError, EncoderWriteExt, HYPHENATED_UUID_LENGTH,
};
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]);
}

View File

@ -1 +1,72 @@
pub mod v1_14_4;
// This file is automatically generated.
// It is not intended for manual editing.
#[cfg(feature = "15w40b")]
pub mod v_15w40b;
#[cfg(feature = "16w20a")]
pub mod v_16w20a;
#[cfg(feature = "16w35a")]
pub mod v_16w35a;
#[cfg(feature = "17w15a")]
pub mod v_17w15a;
#[cfg(feature = "17w18b")]
pub mod v_17w18b;
#[cfg(feature = "17w50a")]
pub mod v_17w50a;
#[cfg(feature = "1.10")]
pub mod v_1_10;
#[cfg(feature = "1.10-pre1")]
pub mod v_1_10_pre1;
#[cfg(feature = "1.11")]
pub mod v_1_11;
#[cfg(feature = "1.12")]
pub mod v_1_12;
#[cfg(feature = "1.12.1")]
pub mod v_1_12_1;
#[cfg(feature = "1.12.2")]
pub mod v_1_12_2;
#[cfg(feature = "1.12-pre4")]
pub mod v_1_12_pre4;
#[cfg(feature = "1.13")]
pub mod v_1_13;
#[cfg(feature = "1.13.1")]
pub mod v_1_13_1;
#[cfg(feature = "1.13.2")]
pub mod v_1_13_2;
#[cfg(feature = "1.13.2-pre1")]
pub mod v_1_13_2_pre1;
#[cfg(feature = "1.13.2-pre2")]
pub mod v_1_13_2_pre2;
#[cfg(feature = "1.14")]
pub mod v_1_14;
#[cfg(feature = "1.14.1")]
pub mod v_1_14_1;
#[cfg(feature = "1.14.3")]
pub mod v_1_14_3;
#[cfg(feature = "1.14.4")]
pub mod v_1_14_4;
#[cfg(feature = "1.15")]
pub mod v_1_15;
#[cfg(feature = "1.15.1")]
pub mod v_1_15_1;
#[cfg(feature = "1.15.2")]
pub mod v_1_15_2;
#[cfg(feature = "1.16")]
pub mod v_1_16;
#[cfg(feature = "1.16.1")]
pub mod v_1_16_1;
#[cfg(feature = "1.16.2")]
pub mod v_1_16_2;
#[cfg(feature = "1.16-rc1")]
pub mod v_1_16_rc1;
#[cfg(feature = "1.8")]
pub mod v_1_8;
#[cfg(feature = "1.9")]
pub mod v_1_9;
#[cfg(feature = "1.9.1-pre2")]
pub mod v_1_9_1_pre2;
#[cfg(feature = "1.9.2")]
pub mod v_1_9_2;
#[cfg(feature = "1.9.4")]
pub mod v_1_9_4;
#[cfg(feature = "20w13b")]
pub mod v_20w13b;

View File

@ -1,734 +0,0 @@
use crate::data::chat::Message;
use crate::decoder::Decoder;
use crate::decoder::DecoderReadExt;
use crate::encoder::EncoderWriteExt;
use crate::error::DecodeError;
use byteorder::{ReadBytesExt, WriteBytesExt};
use minecraft_protocol_derive::{Decoder, Encoder};
use nbt::CompoundTag;
use std::io::Read;
use uuid::Uuid;
pub enum GameServerBoundPacket {
ServerBoundChatMessage(ServerBoundChatMessage),
ServerBoundKeepAlive(ServerBoundKeepAlive),
ServerBoundAbilities(ServerBoundAbilities),
}
pub enum GameClientBoundPacket {
ClientBoundChatMessage(ClientBoundChatMessage),
JoinGame(JoinGame),
ClientBoundKeepAlive(ClientBoundKeepAlive),
ChunkData(ChunkData),
GameDisconnect(GameDisconnect),
BossBar(BossBar),
EntityAction(EntityAction),
}
impl GameServerBoundPacket {
pub fn get_type_id(&self) -> u8 {
match self {
GameServerBoundPacket::ServerBoundChatMessage(_) => 0x03,
GameServerBoundPacket::ServerBoundKeepAlive(_) => 0x0F,
GameServerBoundPacket::ServerBoundAbilities(_) => 0x19,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x03 => {
let chat_message = ServerBoundChatMessage::decode(reader)?;
Ok(GameServerBoundPacket::ServerBoundChatMessage(chat_message))
}
0x0F => {
let keep_alive = ServerBoundKeepAlive::decode(reader)?;
Ok(GameServerBoundPacket::ServerBoundKeepAlive(keep_alive))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
}
impl GameClientBoundPacket {
pub fn get_type_id(&self) -> u8 {
match self {
GameClientBoundPacket::ClientBoundChatMessage(_) => 0x0E,
GameClientBoundPacket::GameDisconnect(_) => 0x1A,
GameClientBoundPacket::ClientBoundKeepAlive(_) => 0x20,
GameClientBoundPacket::ChunkData(_) => 0x21,
GameClientBoundPacket::JoinGame(_) => 0x25,
GameClientBoundPacket::BossBar(_) => 0x0D,
GameClientBoundPacket::EntityAction(_) => 0x1B,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x0E => {
let chat_message = ClientBoundChatMessage::decode(reader)?;
Ok(GameClientBoundPacket::ClientBoundChatMessage(chat_message))
}
0x1A => {
let game_disconnect = GameDisconnect::decode(reader)?;
Ok(GameClientBoundPacket::GameDisconnect(game_disconnect))
}
0x20 => {
let keep_alive = ClientBoundKeepAlive::decode(reader)?;
Ok(GameClientBoundPacket::ClientBoundKeepAlive(keep_alive))
}
0x21 => {
let chunk_data = ChunkData::decode(reader)?;
Ok(GameClientBoundPacket::ChunkData(chunk_data))
}
0x25 => {
let join_game = JoinGame::decode(reader)?;
Ok(GameClientBoundPacket::JoinGame(join_game))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct ServerBoundChatMessage {
#[data_type(max_length = 256)]
pub message: String,
}
impl ServerBoundChatMessage {
pub fn new(message: String) -> GameServerBoundPacket {
let chat_message = ServerBoundChatMessage { message };
GameServerBoundPacket::ServerBoundChatMessage(chat_message)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct ClientBoundChatMessage {
pub message: Message,
pub position: MessagePosition,
}
#[derive(Encoder, Decoder, Debug, Eq, PartialEq)]
pub enum MessagePosition {
Chat,
System,
HotBar,
}
impl ClientBoundChatMessage {
pub fn new(message: Message, position: MessagePosition) -> GameClientBoundPacket {
let chat_message = ClientBoundChatMessage { message, position };
GameClientBoundPacket::ClientBoundChatMessage(chat_message)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct JoinGame {
pub entity_id: u32,
pub game_mode: GameMode,
pub dimension: i32,
pub max_players: u8,
#[data_type(max_length = 16)]
pub level_type: String,
#[data_type(with = "var_int")]
pub view_distance: i32,
pub reduced_debug_info: bool,
}
#[derive(Encoder, Decoder, Debug, Eq, PartialEq)]
pub enum GameMode {
Survival = 0,
Creative = 1,
Adventure = 2,
Spectator = 3,
Hardcore = 8,
}
impl JoinGame {
pub fn new(
entity_id: u32,
game_mode: GameMode,
dimension: i32,
max_players: u8,
level_type: String,
view_distance: i32,
reduced_debug_info: bool,
) -> GameClientBoundPacket {
let join_game = JoinGame {
entity_id,
game_mode,
dimension,
max_players,
level_type,
view_distance,
reduced_debug_info,
};
GameClientBoundPacket::JoinGame(join_game)
}
}
#[derive(Encoder, Decoder)]
pub struct ServerBoundKeepAlive {
pub id: u64,
}
impl ServerBoundKeepAlive {
pub fn new(id: u64) -> GameServerBoundPacket {
let keep_alive = ServerBoundKeepAlive { id };
GameServerBoundPacket::ServerBoundKeepAlive(keep_alive)
}
}
#[derive(Encoder, Decoder)]
pub struct ClientBoundKeepAlive {
pub id: u64,
}
impl ClientBoundKeepAlive {
pub fn new(id: u64) -> GameClientBoundPacket {
let keep_alive = ClientBoundKeepAlive { id };
GameClientBoundPacket::ClientBoundKeepAlive(keep_alive)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct ChunkData {
pub x: i32,
pub z: i32,
pub full: bool,
#[data_type(with = "var_int")]
pub primary_mask: i32,
pub heights: CompoundTag,
pub data: Vec<u8>,
pub tiles: Vec<CompoundTag>,
}
impl ChunkData {
pub fn new(
x: i32,
z: i32,
full: bool,
primary_mask: i32,
heights: CompoundTag,
data: Vec<u8>,
tiles: Vec<CompoundTag>,
) -> GameClientBoundPacket {
let chunk_data = ChunkData {
x,
z,
full,
primary_mask,
heights,
data,
tiles,
};
GameClientBoundPacket::ChunkData(chunk_data)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct GameDisconnect {
pub reason: Message,
}
impl GameDisconnect {
pub fn new(reason: Message) -> GameClientBoundPacket {
let game_disconnect = GameDisconnect { reason };
GameClientBoundPacket::GameDisconnect(game_disconnect)
}
}
#[derive(Encoder, Decoder, Debug, PartialEq)]
pub struct BossBar {
pub id: Uuid,
pub action: BossBarAction,
}
#[derive(Encoder, Decoder, Debug, PartialEq)]
pub enum BossBarAction {
Add {
title: Message,
health: f32,
color: BossBarColor,
division: BossBarDivision,
flags: u8,
},
Remove,
UpdateHealth {
health: f32,
},
UpdateTitle {
title: Message,
},
UpdateStyle {
color: BossBarColor,
division: BossBarDivision,
},
UpdateFlags {
flags: u8,
},
}
#[derive(Encoder, Decoder, Debug, PartialEq)]
pub enum BossBarColor {
Pink,
Blue,
Red,
Green,
Yellow,
Purple,
White,
}
#[derive(Encoder, Decoder, Debug, PartialEq)]
pub enum BossBarDivision {
None,
Notches6,
Notches10,
Notches12,
Notches20,
}
impl BossBar {
pub fn new(id: Uuid, action: BossBarAction) -> GameClientBoundPacket {
let boss_bar = BossBar { id, action };
GameClientBoundPacket::BossBar(boss_bar)
}
}
#[derive(Encoder, Decoder, Debug, PartialEq)]
pub struct EntityAction {
#[data_type(with = "var_int")]
pub entity_id: i32,
pub action_id: EntityActionId,
#[data_type(with = "var_int")]
pub jump_boost: i32,
}
#[derive(Encoder, Decoder, Debug, PartialEq)]
#[data_type(with = "var_int")]
pub enum EntityActionId {
StartSneaking,
StopSneaking,
LeaveBad,
StartSprinting,
StopSprinting,
StartJumpWithHorse,
StopJumpWithHorse,
OpenHorseInventory,
StartFlyingWithElytra,
}
#[derive(Encoder, Decoder, Debug, PartialEq)]
pub struct ServerBoundAbilities {
#[data_type(bitfield)]
pub invulnerable: bool,
#[data_type(bitfield)]
pub allow_flying: bool,
#[data_type(bitfield)]
pub flying: bool,
#[data_type(bitfield)]
pub creative_mode: bool,
pub fly_speed: f32,
pub walk_speed: f32,
}
#[cfg(test)]
mod tests {
use crate::data::chat::Payload;
use crate::decoder::Decoder;
use crate::encoder::Encoder;
use crate::encoder::EncoderWriteExt;
use crate::error::{DecodeError, EncodeError};
use crate::version::v1_14_4::game::*;
use crate::STRING_MAX_LENGTH;
use nbt::CompoundTag;
use std::io::Cursor;
use std::str::FromStr;
#[test]
fn test_server_bound_chat_message_encode() {
let chat_message = ServerBoundChatMessage {
message: String::from("hello server!"),
};
let mut vec = Vec::new();
chat_message.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/server_bound_chat_message.dat").to_vec()
);
}
#[test]
fn test_server_bound_chat_message_decode() {
let mut cursor = Cursor::new(
include_bytes!("../../../test/packet/game/server_bound_chat_message.dat").to_vec(),
);
let chat_message = ServerBoundChatMessage::decode(&mut cursor).unwrap();
assert_eq!(chat_message.message, "hello server!");
}
#[test]
fn test_server_bound_chat_message_encode_invalid_length() {
let chat_message = ServerBoundChatMessage {
message: "abc".repeat(100),
};
let mut vec = Vec::new();
let encode_error = chat_message
.encode(&mut vec)
.err()
.expect("Expected error `StringTooLong` because message has invalid length");
match encode_error {
EncodeError::StringTooLong { length, max_length } => {
assert_eq!(length, 300);
assert_eq!(max_length, 256);
}
_ => panic!("Expected `StringTooLong` but got `{:?}`", encode_error),
}
}
#[test]
fn test_server_bound_chat_message_decode_invalid_length() {
let message = "abc".repeat(100);
let mut vec = Vec::new();
vec.write_string(&message, STRING_MAX_LENGTH).unwrap();
let mut cursor = Cursor::new(vec);
let decode_error = ServerBoundChatMessage::decode(&mut cursor)
.err()
.expect("Expected error `StringTooLong` because message has invalid length");
match decode_error {
DecodeError::StringTooLong { length, max_length } => {
assert_eq!(length, 300);
assert_eq!(max_length, 256);
}
_ => panic!("Expected `StringTooLong` but got `{:?}`", decode_error),
}
}
#[test]
fn test_client_bound_chat_message_encode() {
let chat_message = ClientBoundChatMessage {
message: Message::new(Payload::text("hello client!")),
position: MessagePosition::System,
};
let mut vec = Vec::new();
chat_message.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/client_bound_chat_message.dat").to_vec()
);
}
#[test]
fn test_client_bound_chat_message_decode() {
let mut cursor = Cursor::new(
include_bytes!("../../../test/packet/game/client_bound_chat_message.dat").to_vec(),
);
let chat_message = ClientBoundChatMessage::decode(&mut cursor).unwrap();
assert_eq!(
chat_message.message,
Message::new(Payload::text("hello client!"))
);
assert_eq!(chat_message.position, MessagePosition::System);
}
#[test]
fn test_server_bound_keep_alive_encode() {
let keep_alive = ServerBoundKeepAlive { id: 31122019 };
let mut vec = Vec::new();
keep_alive.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/server_bound_keep_alive.dat").to_vec()
);
}
#[test]
fn test_server_bound_keep_alive_decode() {
let mut cursor = Cursor::new(
include_bytes!("../../../test/packet/game/server_bound_keep_alive.dat").to_vec(),
);
let keep_alive = ServerBoundKeepAlive::decode(&mut cursor).unwrap();
assert_eq!(keep_alive.id, 31122019);
}
#[test]
fn test_client_bound_keep_alive_encode() {
let keep_alive = ClientBoundKeepAlive { id: 240714 };
let mut vec = Vec::new();
keep_alive.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/client_bound_keep_alive.dat").to_vec()
);
}
#[test]
fn test_client_bound_keep_alive_decode() {
let mut cursor = Cursor::new(
include_bytes!("../../../test/packet/game/client_bound_keep_alive.dat").to_vec(),
);
let keep_alive = ClientBoundKeepAlive::decode(&mut cursor).unwrap();
assert_eq!(keep_alive.id, 240714);
}
#[test]
fn test_join_game_encode() {
let join_game = JoinGame {
entity_id: 27,
game_mode: GameMode::Hardcore,
dimension: 23,
max_players: 100,
level_type: String::from("default"),
view_distance: 10,
reduced_debug_info: true,
};
let mut vec = Vec::new();
join_game.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/join_game.dat").to_vec()
);
}
#[test]
fn test_join_game_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/game/join_game.dat").to_vec());
let join_game = JoinGame::decode(&mut cursor).unwrap();
assert_eq!(join_game.entity_id, 27);
assert_eq!(join_game.game_mode, GameMode::Hardcore);
assert_eq!(join_game.dimension, 23);
assert_eq!(join_game.max_players, 100);
assert_eq!(join_game.level_type, String::from("default"));
assert_eq!(join_game.view_distance, 10);
assert!(join_game.reduced_debug_info);
}
#[test]
fn test_chunk_data_encode() {
let chunk_data = ChunkData {
x: -2,
z: 5,
full: true,
primary_mask: 65535,
heights: CompoundTag::named("HeightMaps"),
data: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
tiles: vec![CompoundTag::named("TileEntity")],
};
let mut vec = Vec::new();
chunk_data.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/chunk_data.dat").to_vec()
);
}
#[test]
fn test_chunk_data_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/game/chunk_data.dat").to_vec());
let chunk_data = ChunkData::decode(&mut cursor).unwrap();
assert_eq!(chunk_data.x, -2);
assert_eq!(chunk_data.z, 5);
assert!(chunk_data.full);
assert_eq!(chunk_data.heights.name, Some(String::from("HeightMaps")));
assert_eq!(chunk_data.data, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
assert_eq!(chunk_data.primary_mask, 65535);
assert_eq!(chunk_data.tiles[0].name, Some(String::from("TileEntity")));
}
#[test]
fn test_game_disconnect_encode() {
let game_disconnect = GameDisconnect {
reason: Message::new(Payload::text("Message")),
};
let mut vec = Vec::new();
game_disconnect.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/game_disconnect.dat").to_vec()
);
}
#[test]
fn test_game_disconnect_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/game/game_disconnect.dat").to_vec());
let game_disconnect = GameDisconnect::decode(&mut cursor).unwrap();
assert_eq!(
game_disconnect.reason,
Message::new(Payload::text("Message"))
);
}
#[test]
fn test_boss_bar_add_encode() {
let boss_bar_add = create_boss_bar_add_packet();
let mut vec = Vec::new();
boss_bar_add.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/boss_bar_add.dat").to_vec()
);
}
#[test]
fn test_boss_bar_add_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/game/boss_bar_add.dat").to_vec());
let boss_bar_add = BossBar::decode(&mut cursor).unwrap();
assert_eq!(boss_bar_add, create_boss_bar_add_packet());
}
fn create_boss_bar_add_packet() -> BossBar {
BossBar {
id: Uuid::from_str("afa32ac8-d3bf-47f3-99eb-294d60b3dca2").unwrap(),
action: BossBarAction::Add {
title: Message::from_str("Boss title"),
health: 123.45,
color: BossBarColor::Yellow,
division: BossBarDivision::Notches10,
flags: 7,
},
}
}
#[test]
fn test_boss_bar_remove_encode() {
let boss_bar_remove = create_boss_bar_remove_packet();
let mut vec = Vec::new();
boss_bar_remove.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/boss_bar_remove.dat").to_vec()
);
}
#[test]
fn test_boss_bar_remove_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/game/boss_bar_remove.dat").to_vec());
let boss_bar_remove = BossBar::decode(&mut cursor).unwrap();
assert_eq!(boss_bar_remove, create_boss_bar_remove_packet());
}
fn create_boss_bar_remove_packet() -> BossBar {
BossBar {
id: Uuid::from_str("afa32ac8-d3bf-47f3-99eb-294d60b3dca2").unwrap(),
action: BossBarAction::Remove,
}
}
#[test]
fn test_entity_action_encode() {
let entity_action = EntityAction {
entity_id: 12345,
action_id: EntityActionId::StartFlyingWithElytra,
jump_boost: i32::MAX,
};
let mut vec = Vec::new();
entity_action.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/entity_action.dat").to_vec()
);
}
#[test]
fn test_entity_action_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/game/entity_action.dat").to_vec());
let entity_action = EntityAction::decode(&mut cursor).unwrap();
assert_eq!(
entity_action,
EntityAction {
entity_id: 12345,
action_id: EntityActionId::StartFlyingWithElytra,
jump_boost: i32::MAX,
}
);
}
#[test]
fn test_serverbound_abilities_encode() {
let abilities = ServerBoundAbilities {
invulnerable: true,
flying: true,
allow_flying: false,
creative_mode: true,
fly_speed: 0.0,
walk_speed: 0.0,
};
let mut vec = Vec::new();
abilities.encode(&mut vec).unwrap();
assert_eq!(vec, [13, 0, 0, 0, 0, 0, 0, 0, 0]);
}
#[test]
fn test_serverbound_abilities_decode() {
let vec = [13, 0, 0, 0, 0, 0, 0, 0, 0].to_vec();
let mut cursor = Cursor::new(vec);
let abilities = ServerBoundAbilities::decode(&mut cursor).unwrap();
assert!(abilities.invulnerable);
assert!(!abilities.allow_flying);
assert!(abilities.flying);
assert!(abilities.creative_mode);
}
}

View File

@ -1,55 +0,0 @@
use crate::decoder::Decoder;
use crate::error::DecodeError;
use minecraft_protocol_derive::{Decoder, Encoder};
use std::io::Read;
pub enum HandshakeServerBoundPacket {
Handshake(Handshake),
}
impl HandshakeServerBoundPacket {
pub fn get_type_id(&self) -> u8 {
match self {
HandshakeServerBoundPacket::Handshake(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let handshake = Handshake::decode(reader)?;
Ok(HandshakeServerBoundPacket::Handshake(handshake))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct Handshake {
#[data_type(with = "var_int")]
pub protocol_version: i32,
#[data_type(max_length = 255)]
pub server_addr: String,
pub server_port: u16,
#[data_type(with = "var_int")]
pub next_state: i32,
}
impl Handshake {
pub fn new(
protocol_version: i32,
server_addr: String,
server_port: u16,
next_state: i32,
) -> HandshakeServerBoundPacket {
let handshake = Handshake {
protocol_version,
server_addr,
server_port,
next_state,
};
HandshakeServerBoundPacket::Handshake(handshake)
}
}

View File

@ -1,482 +0,0 @@
use std::io::Read;
use uuid::Uuid;
use crate::data::chat::Message;
use crate::decoder::Decoder;
use crate::error::DecodeError;
use minecraft_protocol_derive::{Decoder, Encoder};
pub enum LoginServerBoundPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
LoginPluginResponse(LoginPluginResponse),
}
pub enum LoginClientBoundPacket {
LoginDisconnect(LoginDisconnect),
EncryptionRequest(EncryptionRequest),
LoginSuccess(LoginSuccess),
SetCompression(SetCompression),
LoginPluginRequest(LoginPluginRequest),
}
impl LoginServerBoundPacket {
pub fn get_type_id(&self) -> u8 {
match self {
LoginServerBoundPacket::LoginStart(_) => 0x00,
LoginServerBoundPacket::EncryptionResponse(_) => 0x01,
LoginServerBoundPacket::LoginPluginResponse(_) => 0x02,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let login_start = LoginStart::decode(reader)?;
Ok(LoginServerBoundPacket::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(LoginServerBoundPacket::EncryptionResponse(
encryption_response,
))
}
0x02 => {
let login_plugin_response = LoginPluginResponse::decode(reader)?;
Ok(LoginServerBoundPacket::LoginPluginResponse(
login_plugin_response,
))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
}
impl LoginClientBoundPacket {
pub fn get_type_id(&self) -> u8 {
match self {
LoginClientBoundPacket::LoginDisconnect(_) => 0x00,
LoginClientBoundPacket::EncryptionRequest(_) => 0x01,
LoginClientBoundPacket::LoginSuccess(_) => 0x02,
LoginClientBoundPacket::SetCompression(_) => 0x03,
LoginClientBoundPacket::LoginPluginRequest(_) => 0x04,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let login_disconnect = LoginDisconnect::decode(reader)?;
Ok(LoginClientBoundPacket::LoginDisconnect(login_disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(LoginClientBoundPacket::EncryptionRequest(
encryption_request,
))
}
0x02 => {
let login_success = LoginSuccess::decode(reader)?;
Ok(LoginClientBoundPacket::LoginSuccess(login_success))
}
0x03 => {
let set_compression = SetCompression::decode(reader)?;
Ok(LoginClientBoundPacket::SetCompression(set_compression))
}
0x04 => {
let login_plugin_request = LoginPluginRequest::decode(reader)?;
Ok(LoginClientBoundPacket::LoginPluginRequest(
login_plugin_request,
))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct LoginStart {
pub name: String,
}
impl LoginStart {
pub fn new(name: String) -> LoginServerBoundPacket {
let login_start = LoginStart { name };
LoginServerBoundPacket::LoginStart(login_start)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
impl EncryptionResponse {
pub fn new(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> LoginServerBoundPacket {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
LoginServerBoundPacket::EncryptionResponse(encryption_response)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct LoginPluginResponse {
#[data_type(with = "var_int")]
pub message_id: i32,
pub successful: bool,
#[data_type(with = "rest")]
pub data: Vec<u8>,
}
impl LoginPluginResponse {
pub fn new(message_id: i32, successful: bool, data: Vec<u8>) -> LoginServerBoundPacket {
let login_plugin_response = LoginPluginResponse {
message_id,
successful,
data,
};
LoginServerBoundPacket::LoginPluginResponse(login_plugin_response)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct LoginDisconnect {
pub reason: Message,
}
impl LoginDisconnect {
pub fn new(reason: Message) -> LoginClientBoundPacket {
let login_disconnect = LoginDisconnect { reason };
LoginClientBoundPacket::LoginDisconnect(login_disconnect)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct EncryptionRequest {
#[data_type(max_length = 20)]
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
impl EncryptionRequest {
pub fn new(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> LoginClientBoundPacket {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
LoginClientBoundPacket::EncryptionRequest(encryption_request)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct LoginSuccess {
#[data_type(with = "uuid_hyp_str")]
pub uuid: Uuid,
#[data_type(max_length = 16)]
pub username: String,
}
impl LoginSuccess {
pub fn new(uuid: Uuid, username: String) -> LoginClientBoundPacket {
let login_success = LoginSuccess { uuid, username };
LoginClientBoundPacket::LoginSuccess(login_success)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct SetCompression {
#[data_type(with = "var_int")]
pub threshold: i32,
}
impl SetCompression {
pub fn new(threshold: i32) -> LoginClientBoundPacket {
let set_compression = SetCompression { threshold };
LoginClientBoundPacket::SetCompression(set_compression)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct LoginPluginRequest {
#[data_type(with = "var_int")]
pub message_id: i32,
pub channel: String,
#[data_type(with = "rest")]
pub data: Vec<u8>,
}
impl LoginPluginRequest {
pub fn new(message_id: i32, channel: String, data: Vec<u8>) -> LoginClientBoundPacket {
let login_plugin_request = LoginPluginRequest {
message_id,
channel,
data,
};
LoginClientBoundPacket::LoginPluginRequest(login_plugin_request)
}
}
#[cfg(test)]
mod tests {
use crate::data::chat::Payload;
use crate::decoder::Decoder;
use crate::encoder::Encoder;
use crate::version::v1_14_4::login::*;
use std::io::Cursor;
use uuid::Uuid;
#[test]
fn test_login_start_packet_encode() {
let login_start = LoginStart {
name: String::from("Username"),
};
let mut vec = Vec::new();
login_start.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/login/login_start.dat").to_vec()
);
}
#[test]
fn test_login_start_packet_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/login/login_start.dat").to_vec());
let login_start = LoginStart::decode(&mut cursor).unwrap();
assert_eq!(login_start.name, String::from("Username"));
}
#[test]
fn test_encryption_response_encode() {
let encryption_response = EncryptionResponse {
shared_secret: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
verify_token: vec![1, 2, 3, 4],
};
let mut vec = Vec::new();
encryption_response.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/login/encryption_response.dat").to_vec()
);
}
#[test]
fn test_encryption_response_decode() {
let mut cursor = Cursor::new(
include_bytes!("../../../test/packet/login/encryption_response.dat").to_vec(),
);
let encryption_response = EncryptionResponse::decode(&mut cursor).unwrap();
assert_eq!(
encryption_response.shared_secret,
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
);
assert_eq!(encryption_response.verify_token, vec![1, 2, 3, 4]);
}
#[test]
fn test_login_plugin_response_encode() {
let login_plugin_response = LoginPluginResponse {
message_id: 55,
successful: true,
data: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
};
let mut vec = Vec::new();
login_plugin_response.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/login/login_plugin_response.dat").to_vec()
);
}
#[test]
fn test_login_plugin_response_decode() {
let mut cursor = Cursor::new(
include_bytes!("../../../test/packet/login/login_plugin_response.dat").to_vec(),
);
let login_plugin_response = LoginPluginResponse::decode(&mut cursor).unwrap();
assert_eq!(login_plugin_response.message_id, 55);
assert!(login_plugin_response.successful);
assert_eq!(
login_plugin_response.data,
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
);
}
#[test]
fn test_login_disconnect_encode() {
let login_disconnect = LoginDisconnect {
reason: Message::new(Payload::text("Message")),
};
let mut vec = Vec::new();
login_disconnect.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/login/login_disconnect.dat").to_vec()
);
}
#[test]
fn test_login_disconnect_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/login/login_disconnect.dat").to_vec());
let login_disconnect = LoginDisconnect::decode(&mut cursor).unwrap();
assert_eq!(
login_disconnect.reason,
Message::new(Payload::text("Message"))
);
}
#[test]
fn test_encryption_request_encode() {
let encryption_request = EncryptionRequest {
server_id: String::from("ServerID"),
public_key: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
verify_token: vec![1, 2, 3, 4],
};
let mut vec = Vec::new();
encryption_request.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/login/encryption_request.dat").to_vec()
);
}
#[test]
fn test_encryption_request_decode() {
let mut cursor = Cursor::new(
include_bytes!("../../../test/packet/login/encryption_request.dat").to_vec(),
);
let encryption_request = EncryptionRequest::decode(&mut cursor).unwrap();
assert_eq!(encryption_request.server_id, String::from("ServerID"));
assert_eq!(
encryption_request.public_key,
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
);
assert_eq!(encryption_request.verify_token, vec![1, 2, 3, 4]);
}
#[test]
fn test_login_success_encode() {
let login_success = LoginSuccess {
uuid: Uuid::parse_str("35ee313b-d89a-41b8-b25e-d32e8aff0389").unwrap(),
username: String::from("Username"),
};
let mut vec = Vec::new();
login_success.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/login/login_success.dat").to_vec()
);
}
#[test]
fn test_login_success_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/login/login_success.dat").to_vec());
let login_success = LoginSuccess::decode(&mut cursor).unwrap();
assert_eq!(login_success.username, String::from("Username"));
assert_eq!(
login_success.uuid,
Uuid::parse_str("35ee313b-d89a-41b8-b25e-d32e8aff0389").unwrap()
);
}
#[test]
fn test_set_compression_encode() {
let set_compression = SetCompression { threshold: 1 };
let mut vec = Vec::new();
set_compression.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/login/login_set_compression.dat").to_vec()
);
}
#[test]
fn test_set_compression_decode() {
let mut cursor = Cursor::new(
include_bytes!("../../../test/packet/login/login_set_compression.dat").to_vec(),
);
let set_compression = SetCompression::decode(&mut cursor).unwrap();
assert_eq!(set_compression.threshold, 1);
}
#[test]
fn test_login_plugin_request_encode() {
let login_plugin_request = LoginPluginRequest {
message_id: 55,
channel: String::from("Channel"),
data: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
};
let mut vec = Vec::new();
login_plugin_request.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/login/login_plugin_request.dat").to_vec()
);
}
#[test]
fn test_login_plugin_request_decode() {
let mut cursor = Cursor::new(
include_bytes!("../../../test/packet/login/login_plugin_request.dat").to_vec(),
);
let login_plugin_request = LoginPluginRequest::decode(&mut cursor).unwrap();
assert_eq!(login_plugin_request.message_id, 55);
assert_eq!(login_plugin_request.channel, String::from("Channel"));
assert_eq!(
login_plugin_request.data,
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
);
}
}

View File

@ -1,4 +0,0 @@
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -1,200 +0,0 @@
use crate::data::server_status::*;
use crate::decoder::Decoder;
use crate::error::DecodeError;
use minecraft_protocol_derive::{Decoder, Encoder};
use std::io::Read;
pub enum StatusServerBoundPacket {
StatusRequest,
PingRequest(PingRequest),
}
pub enum StatusClientBoundPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl StatusServerBoundPacket {
pub fn get_type_id(&self) -> u8 {
match self {
StatusServerBoundPacket::StatusRequest => 0x00,
StatusServerBoundPacket::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(StatusServerBoundPacket::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(StatusServerBoundPacket::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
}
impl StatusClientBoundPacket {
pub fn get_type_id(&self) -> u8 {
match self {
StatusClientBoundPacket::StatusResponse(_) => 0x00,
StatusClientBoundPacket::PingResponse(_) => 0x01,
}
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct PingRequest {
pub time: u64,
}
impl PingRequest {
pub fn new(time: u64) -> StatusServerBoundPacket {
let ping_request = PingRequest { time };
StatusServerBoundPacket::PingRequest(ping_request)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct PingResponse {
pub time: u64,
}
impl PingResponse {
pub fn new(time: u64) -> StatusClientBoundPacket {
let ping_response = PingResponse { time };
StatusClientBoundPacket::PingResponse(ping_response)
}
}
#[derive(Encoder, Decoder, Debug)]
pub struct StatusResponse {
pub server_status: ServerStatus,
}
impl StatusResponse {
pub fn new(server_status: ServerStatus) -> StatusClientBoundPacket {
let status_response = StatusResponse { server_status };
StatusClientBoundPacket::StatusResponse(status_response)
}
}
#[cfg(test)]
mod tests {
use crate::data::chat::{Message, Payload};
use crate::decoder::Decoder;
use crate::encoder::Encoder;
use crate::version::v1_14_4::status::*;
use std::io::Cursor;
use uuid::Uuid;
#[test]
fn test_ping_request_encode() {
let ping_request = PingRequest {
time: 1577735845610,
};
let mut vec = Vec::new();
ping_request.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/status/ping_request.dat").to_vec()
);
}
#[test]
fn test_status_ping_request_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/status/ping_request.dat").to_vec());
let ping_request = PingRequest::decode(&mut cursor).unwrap();
assert_eq!(ping_request.time, 1577735845610);
}
#[test]
fn test_ping_response_encode() {
let ping_response = PingResponse {
time: 1577735845610,
};
let mut vec = Vec::new();
ping_response.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/status/ping_response.dat").to_vec()
);
}
#[test]
fn test_status_ping_response_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/status/ping_response.dat").to_vec());
let ping_response = PingResponse::decode(&mut cursor).unwrap();
assert_eq!(ping_response.time, 1577735845610);
}
#[test]
fn test_status_response_encode() {
let version = ServerVersion {
name: String::from("1.15.1"),
protocol: 575,
};
let player = OnlinePlayer {
id: Uuid::parse_str("2a1e1912-7103-4add-80fc-91ebc346cbce").unwrap(),
name: String::from("Username"),
};
let players = OnlinePlayers {
online: 10,
max: 100,
sample: vec![player],
};
let server_status = ServerStatus {
version,
description: Message::new(Payload::text("Description")),
players,
};
let status_response = StatusResponse { server_status };
let mut vec = Vec::new();
status_response.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/status/status_response.dat").to_vec()
);
}
#[test]
fn test_status_response_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/status/status_response.dat").to_vec());
let status_response = StatusResponse::decode(&mut cursor).unwrap();
let server_status = status_response.server_status;
let player = OnlinePlayer {
id: Uuid::parse_str("2a1e1912-7103-4add-80fc-91ebc346cbce").unwrap(),
name: String::from("Username"),
};
assert_eq!(server_status.version.name, String::from("1.15.1"));
assert_eq!(server_status.version.protocol, 575);
assert_eq!(server_status.players.max, 100);
assert_eq!(server_status.players.online, 10);
assert_eq!(server_status.players.sample, vec![player]);
assert_eq!(
server_status.description,
Message::new(Payload::text("Description"))
);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

View File

@ -0,0 +1,162 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundLoginPacket {
LoginStart(LoginStart),
EncryptionResponse(EncryptionResponse),
}
impl ServerBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::LoginStart(_) => 0x00,
Self::EncryptionResponse(_) => 0x01,
}
}
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(Self::LoginStart(login_start))
}
0x01 => {
let encryption_response = EncryptionResponse::decode(reader)?;
Ok(Self::EncryptionResponse(encryption_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn login_start(username: String) -> Self {
let login_start = LoginStart { username };
Self::LoginStart(login_start)
}
pub fn encryption_response(shared_secret: Vec<u8>, verify_token: Vec<u8>) -> Self {
let encryption_response = EncryptionResponse {
shared_secret,
verify_token,
};
Self::EncryptionResponse(encryption_response)
}
}
pub enum ClientBoundLoginPacket {
Disconnect(Disconnect),
EncryptionRequest(EncryptionRequest),
Success(Success),
Compress(Compress),
}
impl ClientBoundLoginPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::Disconnect(_) => 0x00,
Self::EncryptionRequest(_) => 0x01,
Self::Success(_) => 0x02,
Self::Compress(_) => 0x03,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let disconnect = Disconnect::decode(reader)?;
Ok(Self::Disconnect(disconnect))
}
0x01 => {
let encryption_request = EncryptionRequest::decode(reader)?;
Ok(Self::EncryptionRequest(encryption_request))
}
0x02 => {
let success = Success::decode(reader)?;
Ok(Self::Success(success))
}
0x03 => {
let compress = Compress::decode(reader)?;
Ok(Self::Compress(compress))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn disconnect(reason: String) -> Self {
let disconnect = Disconnect { reason };
Self::Disconnect(disconnect)
}
pub fn encryption_request(
server_id: String,
public_key: Vec<u8>,
verify_token: Vec<u8>,
) -> Self {
let encryption_request = EncryptionRequest {
server_id,
public_key,
verify_token,
};
Self::EncryptionRequest(encryption_request)
}
pub fn success(uuid: String, username: String) -> Self {
let success = Success { uuid, username };
Self::Success(success)
}
pub fn compress(threshold: i32) -> Self {
let compress = Compress { threshold };
Self::Compress(compress)
}
}
#[derive(Packet, Debug)]
pub struct LoginStart {
pub username: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionResponse {
pub shared_secret: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Disconnect {
pub reason: String,
}
#[derive(Packet, Debug)]
pub struct EncryptionRequest {
pub server_id: String,
pub public_key: Vec<u8>,
pub verify_token: Vec<u8>,
}
#[derive(Packet, Debug)]
pub struct Success {
pub uuid: String,
pub username: String,
}
#[derive(Packet, Debug)]
pub struct Compress {
#[packet(with = "var_int")]
pub threshold: i32,
}

View File

@ -0,0 +1,6 @@
// This file is automatically generated.
// It is not intended for manual editing.
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;

View File

@ -0,0 +1,99 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundStatusPacket {
StatusRequest,
PingRequest(PingRequest),
}
impl ServerBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusRequest => 0x00,
Self::PingRequest(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => Ok(Self::StatusRequest),
0x01 => {
let ping_request = PingRequest::decode(reader)?;
Ok(Self::PingRequest(ping_request))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_request() -> Self {
Self::StatusRequest
}
pub fn ping_request(time: i64) -> Self {
let ping_request = PingRequest { time };
Self::PingRequest(ping_request)
}
}
pub enum ClientBoundStatusPacket {
StatusResponse(StatusResponse),
PingResponse(PingResponse),
}
impl ClientBoundStatusPacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::StatusResponse(_) => 0x00,
Self::PingResponse(_) => 0x01,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let status_response = StatusResponse::decode(reader)?;
Ok(Self::StatusResponse(status_response))
}
0x01 => {
let ping_response = PingResponse::decode(reader)?;
Ok(Self::PingResponse(ping_response))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn status_response(response: String) -> Self {
let status_response = StatusResponse { response };
Self::StatusResponse(status_response)
}
pub fn ping_response(time: i64) -> Self {
let ping_response = PingResponse { time };
Self::PingResponse(ping_response)
}
}
#[derive(Packet, Debug)]
pub struct PingRequest {
pub time: i64,
}
#[derive(Packet, Debug)]
pub struct StatusResponse {
pub response: String,
}
#[derive(Packet, Debug)]
pub struct PingResponse {
pub time: i64,
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
// This file is automatically generated.
// It is not intended for manual editing.
use crate::DecodeError;
use crate::Decoder;
use minecraft_protocol_derive::Packet;
use std::io::Read;
pub enum ServerBoundHandshakePacket {
SetProtocol(SetProtocol),
}
impl ServerBoundHandshakePacket {
pub fn get_type_id(&self) -> u8 {
match self {
Self::SetProtocol(_) => 0x00,
}
}
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id {
0x00 => {
let set_protocol = SetProtocol::decode(reader)?;
Ok(Self::SetProtocol(set_protocol))
}
_ => Err(DecodeError::UnknownPacketType { type_id }),
}
}
pub fn set_protocol(
protocol_version: i32,
server_host: String,
server_port: u16,
next_state: i32,
) -> Self {
let set_protocol = SetProtocol {
protocol_version,
server_host,
server_port,
next_state,
};
Self::SetProtocol(set_protocol)
}
}
#[derive(Packet, Debug)]
pub struct SetProtocol {
#[packet(with = "var_int")]
pub protocol_version: i32,
pub server_host: String,
pub server_port: u16,
#[packet(with = "var_int")]
pub next_state: i32,
}

Some files were not shown because too many files have changed in this diff Show More