|
|
|
@@ -6,8 +6,7 @@ use std::io;
|
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
use std::string::FromUtf8Error;
|
|
|
|
|
|
|
|
|
|
use byteorder::WriteBytesExt;
|
|
|
|
|
use byteorder::{BigEndian, ReadBytesExt};
|
|
|
|
|
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
|
|
|
|
use mc_varint::{VarIntRead, VarIntWrite};
|
|
|
|
|
use serde_json::error::Error as JsonError;
|
|
|
|
|
use uuid::parser::ParseError as UuidParseError;
|
|
|
|
@@ -16,7 +15,6 @@ use crate::chat::Message;
|
|
|
|
|
use nbt::decode::TagDecodeError;
|
|
|
|
|
use nbt::CompoundTag;
|
|
|
|
|
use num_traits::{FromPrimitive, ToPrimitive};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
pub mod chat;
|
|
|
|
@@ -137,23 +135,8 @@ trait Decoder {
|
|
|
|
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Trait adds additional helper methods for `Read` to read protocol data.
|
|
|
|
|
trait PacketRead {
|
|
|
|
|
fn read_bool(&mut self) -> Result<bool, DecodeError>;
|
|
|
|
|
|
|
|
|
|
fn read_string(&mut self, max_length: u32) -> 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>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Trait adds additional helper methods for `Write` to write protocol data.
|
|
|
|
|
trait PacketWrite {
|
|
|
|
|
trait EncoderWriteExt {
|
|
|
|
|
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError>;
|
|
|
|
|
|
|
|
|
|
fn write_string(&mut self, value: &str, max_length: u32) -> Result<(), EncodeError>;
|
|
|
|
@@ -167,57 +150,22 @@ trait PacketWrite {
|
|
|
|
|
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<R: Read> PacketRead for R {
|
|
|
|
|
fn read_bool(&mut self) -> Result<bool, DecodeError> {
|
|
|
|
|
match self.read_u8()? {
|
|
|
|
|
0 => Ok(false),
|
|
|
|
|
1 => Ok(true),
|
|
|
|
|
_ => Err(DecodeError::NonBoolValue),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// 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: u32) -> Result<String, DecodeError> {
|
|
|
|
|
let length = self.read_var_i32()? as u32;
|
|
|
|
|
fn read_string(&mut self, max_length: u32) -> Result<String, DecodeError>;
|
|
|
|
|
|
|
|
|
|
if length > max_length as u32 {
|
|
|
|
|
return Err(DecodeError::StringTooLong { length, max_length });
|
|
|
|
|
}
|
|
|
|
|
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError>;
|
|
|
|
|
|
|
|
|
|
let mut buf = vec![0; length as usize];
|
|
|
|
|
self.read_exact(&mut buf)?;
|
|
|
|
|
fn read_chat_message(&mut self) -> Result<Message, DecodeError>;
|
|
|
|
|
|
|
|
|
|
Ok(String::from_utf8(buf)?)
|
|
|
|
|
}
|
|
|
|
|
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError>;
|
|
|
|
|
|
|
|
|
|
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)?)
|
|
|
|
|
}
|
|
|
|
|
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<W: Write> PacketWrite for W {
|
|
|
|
|
impl<W: Write> EncoderWriteExt for W {
|
|
|
|
|
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError> {
|
|
|
|
|
if value {
|
|
|
|
|
self.write_u8(1)?;
|
|
|
|
@@ -266,7 +214,55 @@ impl<W: Write> PacketWrite for W {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Replace primitive impls with macros.
|
|
|
|
|
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: u32) -> Result<String, DecodeError> {
|
|
|
|
|
let length = self.read_var_i32()? as u32;
|
|
|
|
|
|
|
|
|
|
if length > max_length as u32 {
|
|
|
|
|
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)?)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Encoder for u8 {
|
|
|
|
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
|
|
@@ -366,20 +362,6 @@ impl Decoder for bool {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: ToPrimitive> Encoder for T {
|
|
|
|
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
|
|
|
Ok(writer.write_enum(self)?)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: ToPrimitive> Decoder for T {
|
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
|
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
|
|
|
Ok(reader.read_enum())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Encoder for Vec<u8> {
|
|
|
|
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
|
|
|
Ok(writer.write_byte_array(self)?)
|
|
|
|
@@ -404,8 +386,8 @@ impl Decoder for Uuid {
|
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
|
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
|
|
|
let buf = [0; 16];
|
|
|
|
|
reader.read_exact(&buf)?;
|
|
|
|
|
let mut buf = [0; 16];
|
|
|
|
|
reader.read_exact(&mut buf)?;
|
|
|
|
|
|
|
|
|
|
Ok(Uuid::from_bytes(buf))
|
|
|
|
|
}
|
|
|
|
@@ -453,20 +435,45 @@ impl Decoder for Vec<CompoundTag> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Serialize> Encoder for T {
|
|
|
|
|
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
|
|
|
|
let json = serde_json::to_string(self)?;
|
|
|
|
|
writer.write_string(&json, STRING_MAX_LENGTH)?;
|
|
|
|
|
#[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)?)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl crate::Decoder for $ty {
|
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
|
|
impl<'de, T: Deserialize<'de>> Decoder for T {
|
|
|
|
|
type Output = Self;
|
|
|
|
|
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::DecodeError> {
|
|
|
|
|
Ok(crate::DecoderReadExt::read_enum(reader)?)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
|
|
|
|
let json = reader.read_string(STRING_MAX_LENGTH)?;
|
|
|
|
|
serde_json::from_str(&json)?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! impl_json_encoder_decoder (
|
|
|
|
|
($ty: ident) => (
|
|
|
|
|
impl crate::Encoder for $ty {
|
|
|
|
|
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::EncodeError> {
|
|
|
|
|
let json = serde_json::to_string(self)?;
|
|
|
|
|
crate::EncoderWriteExt::write_string(writer, &json, crate::STRING_MAX_LENGTH)?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl crate::Decoder for $ty {
|
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
|
|
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::DecodeError> {
|
|
|
|
|
let json = crate::DecoderReadExt::read_string(reader, crate::STRING_MAX_LENGTH)?;
|
|
|
|
|
|
|
|
|
|
Ok(serde_json::from_str(&json)?)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
);
|
|
|
|
|