WIP: procmacro for automatic packet generation
This commit is contained in:
630
protocol/src/chat.rs
Normal file
630
protocol/src/chat.rs
Normal file
@@ -0,0 +1,630 @@
|
||||
//! Minecraft chat are represented as json object. It's used in different packets.
|
||||
//! Information about format can be found at https://wiki.vg/Chat.
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ## Serialize
|
||||
//!
|
||||
//! ```
|
||||
//! use minecraft_protocol::chat::{Payload, Color, MessageBuilder};
|
||||
//!
|
||||
//! let message = MessageBuilder::builder(Payload::text("Hello"))
|
||||
//! .color(Color::Yellow)
|
||||
//! .bold(true)
|
||||
//! .then(Payload::text("world"))
|
||||
//! .color(Color::Green)
|
||||
//! .bold(true)
|
||||
//! .italic(true)
|
||||
//! .then(Payload::text("!"))
|
||||
//! .color(Color::Blue)
|
||||
//! .build();
|
||||
//!
|
||||
//! println!("{}", message.to_json().unwrap());
|
||||
//! ```
|
||||
//!
|
||||
//! ## Deserialize
|
||||
//!
|
||||
//! ```
|
||||
//! use minecraft_protocol::chat::{MessageBuilder, Color, Payload, Message};
|
||||
//!
|
||||
//! let json = r#"
|
||||
//! {
|
||||
//! "bold":true,
|
||||
//! "color":"yellow",
|
||||
//! "text":"Hello",
|
||||
//! "extra":[
|
||||
//! {
|
||||
//! "bold":true,
|
||||
//! "italic":true,
|
||||
//! "color":"green",
|
||||
//! "text":"world"
|
||||
//! },
|
||||
//! {
|
||||
//! "color":"blue",
|
||||
//! "text":"!"
|
||||
//! }
|
||||
//! ]
|
||||
//! }
|
||||
//! "#;
|
||||
//!
|
||||
//! let expected_message = MessageBuilder::builder(Payload::text("Hello"))
|
||||
//! .color(Color::Yellow)
|
||||
//! .bold(true)
|
||||
//! .then(Payload::text("world"))
|
||||
//! .color(Color::Green)
|
||||
//! .bold(true)
|
||||
//! .italic(true)
|
||||
//! .then(Payload::text("!"))
|
||||
//! .color(Color::Blue)
|
||||
//! .build();
|
||||
//!
|
||||
//! assert_eq!(expected_message, Message::from_json(json).unwrap());
|
||||
//! ```
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Error;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Color {
|
||||
Black,
|
||||
DarkBlue,
|
||||
DarkGreen,
|
||||
DarkAqua,
|
||||
DarkRed,
|
||||
DarkPurple,
|
||||
Gold,
|
||||
Gray,
|
||||
DarkGray,
|
||||
Blue,
|
||||
Green,
|
||||
Aqua,
|
||||
Red,
|
||||
LightPurple,
|
||||
Yellow,
|
||||
White,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ClickAction {
|
||||
OpenUrl,
|
||||
RunCommand,
|
||||
SuggestCommand,
|
||||
ChangePage,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ClickEvent {
|
||||
pub action: ClickAction,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
impl ClickEvent {
|
||||
pub fn new(action: ClickAction, value: &str) -> Self {
|
||||
ClickEvent {
|
||||
action,
|
||||
value: value.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum HoverAction {
|
||||
ShowText,
|
||||
ShowItem,
|
||||
ShowEntity,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct HoverEvent {
|
||||
pub action: HoverAction,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
impl HoverEvent {
|
||||
pub fn new(action: HoverAction, value: &str) -> Self {
|
||||
HoverEvent {
|
||||
action,
|
||||
value: value.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Payload {
|
||||
Text {
|
||||
text: String,
|
||||
},
|
||||
Translation {
|
||||
translate: String,
|
||||
with: Vec<Message>,
|
||||
},
|
||||
Keybind {
|
||||
keybind: String,
|
||||
},
|
||||
Score {
|
||||
name: String,
|
||||
objective: String,
|
||||
value: String,
|
||||
},
|
||||
Selector {
|
||||
selector: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl Payload {
|
||||
pub fn text(text: &str) -> Self {
|
||||
Payload::Text {
|
||||
text: text.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn translation(translate: &str, with: Vec<Message>) -> Self {
|
||||
Payload::Translation {
|
||||
translate: translate.to_owned(),
|
||||
with,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn keybind(keybind: &str) -> Self {
|
||||
Payload::Keybind {
|
||||
keybind: keybind.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn score(name: &str, objective: &str, value: &str) -> Self {
|
||||
Payload::Score {
|
||||
name: name.to_owned(),
|
||||
objective: objective.to_owned(),
|
||||
value: value.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn selector(selector: &str) -> Self {
|
||||
Payload::Selector {
|
||||
selector: selector.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Message {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub bold: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub italic: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub underlined: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub strikethrough: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub obfuscated: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub color: Option<Color>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub insertion: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub click_event: Option<ClickEvent>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub hover_event: Option<HoverEvent>,
|
||||
#[serde(flatten)]
|
||||
pub payload: Payload,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty", default)]
|
||||
pub extra: Vec<Message>,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn new(payload: Payload) -> Self {
|
||||
Message {
|
||||
bold: None,
|
||||
italic: None,
|
||||
underlined: None,
|
||||
strikethrough: None,
|
||||
obfuscated: None,
|
||||
color: None,
|
||||
insertion: None,
|
||||
click_event: None,
|
||||
hover_event: None,
|
||||
payload,
|
||||
extra: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_json(json: &str) -> Result<Self, Error> {
|
||||
serde_json::from_str(json)
|
||||
}
|
||||
|
||||
pub fn to_json(&self) -> Result<String, Error> {
|
||||
serde_json::to_string(&self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MessageBuilder {
|
||||
current: Message,
|
||||
root: Option<Message>,
|
||||
}
|
||||
|
||||
macro_rules! create_builder_style_method (
|
||||
($style: ident) => (
|
||||
pub fn $style(mut self, value: bool) -> Self {
|
||||
self.current.$style = Some(value);
|
||||
self
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
macro_rules! create_builder_click_event_method (
|
||||
($method_name: ident, $event: ident) => (
|
||||
pub fn $method_name(mut self, value: &str) -> Self {
|
||||
let click_event = ClickEvent::new(ClickAction::$event, value);
|
||||
self.current.click_event = Some(click_event);
|
||||
self
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
macro_rules! create_builder_hover_event_method (
|
||||
($method_name: ident, $event: ident) => (
|
||||
pub fn $method_name(mut self, value: &str) -> Self {
|
||||
let hover_event = HoverEvent::new(HoverAction::$event, value);
|
||||
self.current.hover_event = Some(hover_event);
|
||||
self
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
impl MessageBuilder {
|
||||
pub fn builder(payload: Payload) -> Self {
|
||||
let current = Message::new(payload);
|
||||
|
||||
MessageBuilder {
|
||||
current,
|
||||
root: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn color(mut self, color: Color) -> Self {
|
||||
self.current.color = Some(color);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn insertion(mut self, insertion: &str) -> Self {
|
||||
self.current.insertion = Some(insertion.to_owned());
|
||||
self
|
||||
}
|
||||
|
||||
create_builder_style_method!(bold);
|
||||
create_builder_style_method!(italic);
|
||||
create_builder_style_method!(underlined);
|
||||
create_builder_style_method!(strikethrough);
|
||||
create_builder_style_method!(obfuscated);
|
||||
|
||||
create_builder_click_event_method!(click_open_url, OpenUrl);
|
||||
create_builder_click_event_method!(click_run_command, RunCommand);
|
||||
create_builder_click_event_method!(click_suggest_command, SuggestCommand);
|
||||
create_builder_click_event_method!(click_change_page, ChangePage);
|
||||
|
||||
create_builder_hover_event_method!(hover_show_text, ShowText);
|
||||
create_builder_hover_event_method!(hover_show_item, ShowItem);
|
||||
create_builder_hover_event_method!(hover_show_entity, ShowEntity);
|
||||
|
||||
pub fn then(mut self, payload: Payload) -> Self {
|
||||
match self.root.as_mut() {
|
||||
Some(root) => {
|
||||
root.extra.push(self.current);
|
||||
}
|
||||
None => {
|
||||
self.root = Some(self.current);
|
||||
}
|
||||
}
|
||||
|
||||
self.current = Message::new(payload);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Message {
|
||||
match self.root {
|
||||
Some(mut root) => {
|
||||
root.extra.push(self.current);
|
||||
root
|
||||
}
|
||||
None => self.current,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_text_hello_world() {
|
||||
let message = MessageBuilder::builder(Payload::text("Hello"))
|
||||
.color(Color::Yellow)
|
||||
.bold(true)
|
||||
.then(Payload::text("world"))
|
||||
.color(Color::Green)
|
||||
.bold(true)
|
||||
.italic(true)
|
||||
.then(Payload::text("!"))
|
||||
.color(Color::Blue)
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/text_hello_world.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_text_hello_world() {
|
||||
let expected_message = MessageBuilder::builder(Payload::text("Hello"))
|
||||
.color(Color::Yellow)
|
||||
.bold(true)
|
||||
.then(Payload::text("world"))
|
||||
.color(Color::Green)
|
||||
.bold(true)
|
||||
.italic(true)
|
||||
.then(Payload::text("!"))
|
||||
.color(Color::Blue)
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/text_hello_world.json")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_translate_opped_steve() {
|
||||
let with = vec![Message::new(Payload::text("Steve"))];
|
||||
let message = Message::new(Payload::translation("Opped %s", with));
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/translate_opped_steve.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_translate_opped_steve() {
|
||||
let with = vec![Message::new(Payload::text("Steve"))];
|
||||
let expected_message = Message::new(Payload::translation("Opped %s", with));
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/translate_opped_steve.json")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_keybind_jump() {
|
||||
let message = MessageBuilder::builder(Payload::text("Press \""))
|
||||
.color(Color::Yellow)
|
||||
.bold(true)
|
||||
.then(Payload::keybind("key.jump"))
|
||||
.color(Color::Blue)
|
||||
.bold(false)
|
||||
.underlined(true)
|
||||
.then(Payload::text("\" to jump!"))
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/keybind_jump.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_keybind_jump() {
|
||||
let expected_message = MessageBuilder::builder(Payload::text("Press \""))
|
||||
.color(Color::Yellow)
|
||||
.bold(true)
|
||||
.then(Payload::keybind("key.jump"))
|
||||
.color(Color::Blue)
|
||||
.bold(false)
|
||||
.underlined(true)
|
||||
.then(Payload::text("\" to jump!"))
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/keybind_jump.json")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_click_open_url() {
|
||||
let message = MessageBuilder::builder(Payload::text("click me"))
|
||||
.color(Color::Yellow)
|
||||
.bold(true)
|
||||
.click_open_url("http://minecraft.net")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/click_open_url.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_click_open_url() {
|
||||
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
||||
.color(Color::Yellow)
|
||||
.bold(true)
|
||||
.click_open_url("http://minecraft.net")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/click_open_url.json")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_click_run_command() {
|
||||
let message = MessageBuilder::builder(Payload::text("click me"))
|
||||
.color(Color::LightPurple)
|
||||
.italic(true)
|
||||
.click_run_command("/help")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/click_run_command.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_click_run_command() {
|
||||
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
||||
.color(Color::LightPurple)
|
||||
.italic(true)
|
||||
.click_run_command("/help")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/click_run_command.json")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_click_suggest_command() {
|
||||
let message = MessageBuilder::builder(Payload::text("click me"))
|
||||
.color(Color::Blue)
|
||||
.obfuscated(true)
|
||||
.click_suggest_command("/help")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/click_suggest_command.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_click_suggest_command() {
|
||||
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
||||
.color(Color::Blue)
|
||||
.obfuscated(true)
|
||||
.click_suggest_command("/help")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/click_suggest_command.json")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_click_change_page() {
|
||||
let message = MessageBuilder::builder(Payload::text("click me"))
|
||||
.color(Color::DarkGray)
|
||||
.underlined(true)
|
||||
.click_change_page("2")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/click_change_page.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_click_change_page() {
|
||||
let expected_message = MessageBuilder::builder(Payload::text("click me"))
|
||||
.color(Color::DarkGray)
|
||||
.underlined(true)
|
||||
.click_change_page("2")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/click_change_page.json")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_hover_show_text() {
|
||||
let message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||
.color(Color::DarkPurple)
|
||||
.bold(true)
|
||||
.hover_show_text("Herobrine behind you!")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/hover_show_text.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_hover_show_text() {
|
||||
let expected_message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||
.color(Color::DarkPurple)
|
||||
.bold(true)
|
||||
.hover_show_text("Herobrine behind you!")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/hover_show_text.json")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_hover_show_item() {
|
||||
let message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||
.color(Color::DarkRed)
|
||||
.italic(true)
|
||||
.hover_show_item("{\"id\":\"stone\",\"Count\":1}")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/hover_show_item.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_hover_show_item() {
|
||||
let expected_message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||
.color(Color::DarkRed)
|
||||
.italic(true)
|
||||
.hover_show_item("{\"id\":\"stone\",\"Count\":1}")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/hover_show_item.json")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_hover_show_entity() {
|
||||
let message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||
.color(Color::DarkAqua)
|
||||
.obfuscated(true)
|
||||
.hover_show_entity("{\"id\":\"7e4a61cc-83fa-4441-a299-bf69786e610a\",\"type\":\"minecraft:zombie\",\"name\":\"Zombie}\"")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
message.to_json().unwrap(),
|
||||
include_str!("../test/chat/hover_show_entity.json")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_hover_show_entity() {
|
||||
let expected_message = MessageBuilder::builder(Payload::text("hover at me"))
|
||||
.color(Color::DarkAqua)
|
||||
.obfuscated(true)
|
||||
.hover_show_entity("{\"id\":\"7e4a61cc-83fa-4441-a299-bf69786e610a\",\"type\":\"minecraft:zombie\",\"name\":\"Zombie}\"")
|
||||
.build();
|
||||
|
||||
assert_eq!(
|
||||
expected_message,
|
||||
Message::from_json(include_str!("../test/chat/hover_show_entity.json")).unwrap()
|
||||
);
|
||||
}
|
369
protocol/src/game.rs
Normal file
369
protocol/src/game.rs
Normal file
@@ -0,0 +1,369 @@
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use num_derive::{FromPrimitive, ToPrimitive};
|
||||
|
||||
use crate::chat::Message;
|
||||
use crate::{DecodeError, EncodeError, Packet, PacketRead, PacketWrite};
|
||||
use mc_varint::{VarIntRead, VarIntWrite};
|
||||
use nbt::CompoundTag;
|
||||
|
||||
const SERVER_BOUND_CHAT_MESSAGE_MAX_LENGTH: u32 = 256;
|
||||
const LEVEL_TYPE_MAX_LENGTH: u32 = 16;
|
||||
|
||||
pub enum GameServerBoundPacket {
|
||||
ServerBoundChatMessage(ServerBoundChatMessage),
|
||||
ServerBoundKeepAlive(ServerBoundKeepAlive),
|
||||
}
|
||||
|
||||
pub enum GameClientBoundPacket {
|
||||
ClientBoundChatMessage(ClientBoundChatMessage),
|
||||
JoinGame(JoinGame),
|
||||
ClientBoundKeepAlive(ClientBoundKeepAlive),
|
||||
ChunkData(ChunkData),
|
||||
}
|
||||
|
||||
impl GameServerBoundPacket {
|
||||
pub fn get_type_id(&self) -> u8 {
|
||||
match self {
|
||||
GameServerBoundPacket::ServerBoundChatMessage(_) => 0x03,
|
||||
GameServerBoundPacket::ServerBoundKeepAlive(_) => 0x0F,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
|
||||
match type_id {
|
||||
0x03 => {
|
||||
let chat_message = ServerBoundChatMessage::decode(reader)?;
|
||||
|
||||
Ok(GameServerBoundPacket::ServerBoundChatMessage(chat_message))
|
||||
}
|
||||
0x0F => {
|
||||
let keep_alive = ServerBoundKeepAlive::decode(reader)?;
|
||||
|
||||
Ok(GameServerBoundPacket::ServerBoundKeepAlive(keep_alive))
|
||||
}
|
||||
_ => Err(DecodeError::UnknownPacketType { type_id }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GameClientBoundPacket {
|
||||
pub fn get_type_id(&self) -> u8 {
|
||||
match self {
|
||||
GameClientBoundPacket::ClientBoundChatMessage(_) => 0x0E,
|
||||
GameClientBoundPacket::ClientBoundKeepAlive(_) => 0x20,
|
||||
GameClientBoundPacket::ChunkData(_) => 0x21,
|
||||
GameClientBoundPacket::JoinGame(_) => 0x25,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
|
||||
match type_id {
|
||||
0x0E => {
|
||||
let chat_message = ClientBoundChatMessage::decode(reader)?;
|
||||
|
||||
Ok(GameClientBoundPacket::ClientBoundChatMessage(chat_message))
|
||||
}
|
||||
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 }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ServerBoundChatMessage {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl ServerBoundChatMessage {
|
||||
pub fn new(message: String) -> GameServerBoundPacket {
|
||||
let chat_message = ServerBoundChatMessage { message };
|
||||
|
||||
GameServerBoundPacket::ServerBoundChatMessage(chat_message)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for ServerBoundChatMessage {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_string(&self.message, SERVER_BOUND_CHAT_MESSAGE_MAX_LENGTH)
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let message = reader.read_string(SERVER_BOUND_CHAT_MESSAGE_MAX_LENGTH)?;
|
||||
|
||||
Ok(ServerBoundChatMessage { message })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ClientBoundChatMessage {
|
||||
pub message: Message,
|
||||
pub position: MessagePosition,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for ClientBoundChatMessage {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_chat_message(&self.message)?;
|
||||
writer.write_enum(&self.position)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let message = reader.read_chat_message()?;
|
||||
let position = reader.read_enum()?;
|
||||
|
||||
let chat_message = ClientBoundChatMessage { message, position };
|
||||
|
||||
Ok(chat_message)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct JoinGame {
|
||||
pub entity_id: u32,
|
||||
pub game_mode: GameMode,
|
||||
pub dimension: i32,
|
||||
pub max_players: u8,
|
||||
pub level_type: String,
|
||||
pub view_distance: u8,
|
||||
pub reduced_debug_info: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
pub enum GameMode {
|
||||
Survival = 0,
|
||||
Creative = 1,
|
||||
Adventure = 2,
|
||||
Spectator = 3,
|
||||
Hardcore = 8,
|
||||
}
|
||||
|
||||
impl JoinGame {
|
||||
pub fn new(
|
||||
entity_id: u32,
|
||||
game_mode: GameMode,
|
||||
dimension: i32,
|
||||
max_players: u8,
|
||||
level_type: String,
|
||||
view_distance: u8,
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for JoinGame {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_u32::<BigEndian>(self.entity_id)?;
|
||||
writer.write_enum(&self.game_mode)?;
|
||||
writer.write_i32::<BigEndian>(self.dimension)?;
|
||||
writer.write_u8(self.max_players)?;
|
||||
writer.write_string(&self.level_type, LEVEL_TYPE_MAX_LENGTH)?;
|
||||
writer.write_var_u32(self.view_distance as u32)?;
|
||||
writer.write_bool(self.reduced_debug_info)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let entity_id = reader.read_u32::<BigEndian>()?;
|
||||
let game_mode = reader.read_enum()?;
|
||||
let dimension = reader.read_i32::<BigEndian>()?;
|
||||
let max_players = reader.read_u8()?;
|
||||
let level_type = reader.read_string(LEVEL_TYPE_MAX_LENGTH)?;
|
||||
let view_distance = reader.read_var_u32()? as u8;
|
||||
let reduced_debug_info = reader.read_bool()?;
|
||||
|
||||
Ok(JoinGame {
|
||||
entity_id,
|
||||
game_mode,
|
||||
dimension,
|
||||
max_players,
|
||||
level_type,
|
||||
view_distance,
|
||||
reduced_debug_info,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ServerBoundKeepAlive {
|
||||
pub id: u64,
|
||||
}
|
||||
|
||||
impl ServerBoundKeepAlive {
|
||||
pub fn new(id: u64) -> GameServerBoundPacket {
|
||||
let keep_alive = ServerBoundKeepAlive { id };
|
||||
|
||||
GameServerBoundPacket::ServerBoundKeepAlive(keep_alive)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for ServerBoundKeepAlive {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_u64::<BigEndian>(self.id)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let id = reader.read_u64::<BigEndian>()?;
|
||||
|
||||
Ok(ServerBoundKeepAlive { id })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ClientBoundKeepAlive {
|
||||
pub id: u64,
|
||||
}
|
||||
|
||||
impl ClientBoundKeepAlive {
|
||||
pub fn new(id: u64) -> GameClientBoundPacket {
|
||||
let keep_alive = ClientBoundKeepAlive { id };
|
||||
|
||||
GameClientBoundPacket::ClientBoundKeepAlive(keep_alive)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for ClientBoundKeepAlive {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_u64::<BigEndian>(self.id)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let id = reader.read_u64::<BigEndian>()?;
|
||||
|
||||
Ok(ClientBoundKeepAlive { id })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ChunkData {
|
||||
pub x: i32,
|
||||
pub z: i32,
|
||||
pub full: bool,
|
||||
pub primary_mask: u32,
|
||||
pub heights: CompoundTag,
|
||||
pub data: Vec<u8>,
|
||||
pub tiles: Vec<CompoundTag>,
|
||||
}
|
||||
|
||||
impl ChunkData {
|
||||
pub fn new(
|
||||
x: i32,
|
||||
z: i32,
|
||||
full: bool,
|
||||
primary_mask: u32,
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for ChunkData {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_i32::<BigEndian>(self.x)?;
|
||||
writer.write_i32::<BigEndian>(self.z)?;
|
||||
writer.write_bool(self.full)?;
|
||||
writer.write_var_u32(self.primary_mask)?;
|
||||
writer.write_compound_tag(&self.heights)?;
|
||||
writer.write_byte_array(&self.data)?;
|
||||
writer.write_var_u32(self.tiles.len() as u32)?;
|
||||
|
||||
for tile_compound_tag in self.tiles.iter() {
|
||||
writer.write_compound_tag(&tile_compound_tag)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let x = reader.read_i32::<BigEndian>()?;
|
||||
let z = reader.read_i32::<BigEndian>()?;
|
||||
let full = reader.read_bool()?;
|
||||
let primary_mask = reader.read_var_u32()?;
|
||||
let heights = reader.read_compound_tag()?;
|
||||
let data = reader.read_byte_array()?;
|
||||
|
||||
let tiles_length = reader.read_var_u32()?;
|
||||
let mut tiles = Vec::new();
|
||||
|
||||
for _ in 0..tiles_length {
|
||||
let tile_compound_tag = reader.read_compound_tag()?;
|
||||
tiles.push(tile_compound_tag);
|
||||
}
|
||||
|
||||
Ok(ChunkData {
|
||||
x,
|
||||
z,
|
||||
full,
|
||||
primary_mask,
|
||||
heights,
|
||||
data,
|
||||
tiles,
|
||||
})
|
||||
}
|
||||
}
|
261
protocol/src/lib.rs
Normal file
261
protocol/src/lib.rs
Normal file
@@ -0,0 +1,261 @@
|
||||
//! This crate implements Minecraft protocol.
|
||||
//!
|
||||
//! Information about protocol can be found at https://wiki.vg/Protocol.
|
||||
use io::Error as IoError;
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use byteorder::ReadBytesExt;
|
||||
use byteorder::WriteBytesExt;
|
||||
use mc_varint::{VarIntRead, VarIntWrite};
|
||||
use serde_json::error::Error as JsonError;
|
||||
use uuid::parser::ParseError as UuidParseError;
|
||||
|
||||
use crate::chat::Message;
|
||||
use nbt::decode::TagDecodeError;
|
||||
use nbt::CompoundTag;
|
||||
use num_traits::{FromPrimitive, ToPrimitive};
|
||||
|
||||
pub mod chat;
|
||||
pub mod game;
|
||||
pub mod login;
|
||||
pub mod status;
|
||||
|
||||
/// Current supported protocol version.
|
||||
pub const PROTOCOL_VERSION: usize = 498;
|
||||
/// String maximum length.
|
||||
const STRING_MAX_LENGTH: u32 = 32_768;
|
||||
|
||||
/// Possible errors while encoding packet.
|
||||
pub enum EncodeError {
|
||||
/// String length can't be more than provided value.
|
||||
StringTooLong {
|
||||
/// String length.
|
||||
length: usize,
|
||||
/// Max string length.
|
||||
max_length: u32,
|
||||
},
|
||||
IOError {
|
||||
io_error: IoError,
|
||||
},
|
||||
JsonError {
|
||||
json_error: JsonError,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<IoError> for EncodeError {
|
||||
fn from(io_error: IoError) -> Self {
|
||||
EncodeError::IOError { io_error }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<JsonError> for EncodeError {
|
||||
fn from(json_error: JsonError) -> Self {
|
||||
EncodeError::JsonError { json_error }
|
||||
}
|
||||
}
|
||||
|
||||
/// Possible errors while decoding packet.
|
||||
pub enum DecodeError {
|
||||
/// Packet was not recognized. Invalid data or wrong protocol version.
|
||||
UnknownPacketType {
|
||||
type_id: u8,
|
||||
},
|
||||
/// String length can't be more than provided value.
|
||||
StringTooLong {
|
||||
/// String length.
|
||||
length: u32,
|
||||
/// Max string length.
|
||||
max_length: u32,
|
||||
},
|
||||
IOError {
|
||||
io_error: IoError,
|
||||
},
|
||||
JsonError {
|
||||
json_error: JsonError,
|
||||
},
|
||||
/// Byte array was not recognized as valid UTF-8 string.
|
||||
Utf8Error {
|
||||
utf8_error: FromUtf8Error,
|
||||
},
|
||||
/// Boolean are parsed from byte. Valid byte value are 0 or 1.
|
||||
NonBoolValue,
|
||||
UuidParseError {
|
||||
uuid_parse_error: UuidParseError,
|
||||
},
|
||||
// Type id was not parsed as valid enum value.
|
||||
UnknownEnumType {
|
||||
type_id: u8,
|
||||
},
|
||||
TagDecodeError {
|
||||
tag_decode_error: TagDecodeError,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<IoError> for DecodeError {
|
||||
fn from(io_error: IoError) -> Self {
|
||||
DecodeError::IOError { io_error }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<JsonError> for DecodeError {
|
||||
fn from(json_error: JsonError) -> Self {
|
||||
DecodeError::JsonError { json_error }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FromUtf8Error> for DecodeError {
|
||||
fn from(utf8_error: FromUtf8Error) -> Self {
|
||||
DecodeError::Utf8Error { utf8_error }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UuidParseError> for DecodeError {
|
||||
fn from(uuid_parse_error: UuidParseError) -> Self {
|
||||
DecodeError::UuidParseError { uuid_parse_error }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TagDecodeError> for DecodeError {
|
||||
fn from(tag_decode_error: TagDecodeError) -> Self {
|
||||
DecodeError::TagDecodeError { tag_decode_error }
|
||||
}
|
||||
}
|
||||
|
||||
trait Packet {
|
||||
type Output;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError>;
|
||||
|
||||
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 {
|
||||
fn write_bool(&mut self, value: bool) -> Result<(), EncodeError>;
|
||||
|
||||
fn write_string(&mut self, value: &str, max_length: u32) -> 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>;
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_string(&mut self, max_length: u32) -> Result<String, DecodeError> {
|
||||
let length = self.read_var_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_u32()?;
|
||||
|
||||
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<W: Write> PacketWrite 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: u32) -> Result<(), EncodeError> {
|
||||
let length = value.len();
|
||||
|
||||
if length > max_length as usize {
|
||||
return Err(EncodeError::StringTooLong { length, max_length });
|
||||
}
|
||||
|
||||
self.write_var_u32(value.len() as u32)?;
|
||||
self.write_all(value.as_bytes())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError> {
|
||||
self.write_var_u32(value.len() as u32)?;
|
||||
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(())
|
||||
}
|
||||
}
|
381
protocol/src/login.rs
Normal file
381
protocol/src/login.rs
Normal file
@@ -0,0 +1,381 @@
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use mc_varint::{VarIntRead, VarIntWrite};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::chat::Message;
|
||||
use crate::{DecodeError, EncodeError, Packet, PacketRead, PacketWrite, STRING_MAX_LENGTH};
|
||||
|
||||
const LOGIN_MAX_LENGTH: u32 = 16;
|
||||
const SERVER_ID_MAX_LENGTH: u32 = 20;
|
||||
const HYPHENATED_UUID_LENGTH: u32 = 36;
|
||||
|
||||
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(minecraft_protocol_derive::MinecraftPacket)]
|
||||
pub struct LoginStart {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl LoginStart {
|
||||
pub fn new(name: String) -> LoginServerBoundPacket {
|
||||
let login_start = LoginStart { name };
|
||||
|
||||
LoginServerBoundPacket::LoginStart(login_start)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for EncryptionResponse {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_byte_array(&self.shared_secret)?;
|
||||
writer.write_byte_array(&self.verify_token)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let shared_secret = reader.read_byte_array()?;
|
||||
let verify_token = reader.read_byte_array()?;
|
||||
|
||||
Ok(EncryptionResponse {
|
||||
shared_secret,
|
||||
verify_token,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LoginPluginResponse {
|
||||
pub message_id: i32,
|
||||
pub successful: bool,
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for LoginPluginResponse {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_var_i32(self.message_id)?;
|
||||
writer.write_bool(self.successful)?;
|
||||
writer.write_all(&self.data)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let message_id = reader.read_var_i32()?;
|
||||
let successful = reader.read_bool()?;
|
||||
|
||||
let mut data = Vec::new();
|
||||
reader.read_to_end(data.as_mut())?;
|
||||
|
||||
Ok(LoginPluginResponse {
|
||||
message_id,
|
||||
successful,
|
||||
data,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LoginDisconnect {
|
||||
pub reason: Message,
|
||||
}
|
||||
|
||||
impl LoginDisconnect {
|
||||
pub fn new(reason: Message) -> LoginClientBoundPacket {
|
||||
let login_disconnect = LoginDisconnect { reason };
|
||||
|
||||
LoginClientBoundPacket::LoginDisconnect(login_disconnect)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for LoginDisconnect {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_chat_message(&self.reason)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let reason = reader.read_chat_message()?;
|
||||
|
||||
Ok(LoginDisconnect { reason })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EncryptionRequest {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for EncryptionRequest {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_string(&self.server_id, SERVER_ID_MAX_LENGTH)?;
|
||||
writer.write_byte_array(&self.public_key)?;
|
||||
writer.write_byte_array(&self.verify_token)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let server_id = reader.read_string(SERVER_ID_MAX_LENGTH)?;
|
||||
let public_key = reader.read_byte_array()?;
|
||||
let verify_token = reader.read_byte_array()?;
|
||||
|
||||
Ok(EncryptionRequest {
|
||||
server_id,
|
||||
public_key,
|
||||
verify_token,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LoginSuccess {
|
||||
pub uuid: Uuid,
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
impl LoginSuccess {
|
||||
pub fn new(uuid: Uuid, username: String) -> LoginClientBoundPacket {
|
||||
let login_success = LoginSuccess { uuid, username };
|
||||
|
||||
LoginClientBoundPacket::LoginSuccess(login_success)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for LoginSuccess {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
let uuid_hyphenated_string = self.uuid.to_hyphenated().to_string();
|
||||
|
||||
writer.write_string(&uuid_hyphenated_string, HYPHENATED_UUID_LENGTH)?;
|
||||
writer.write_string(&self.username, LOGIN_MAX_LENGTH)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let uuid_hyphenated_string = reader.read_string(HYPHENATED_UUID_LENGTH)?;
|
||||
|
||||
let uuid = Uuid::parse_str(&uuid_hyphenated_string)?;
|
||||
let username = reader.read_string(LOGIN_MAX_LENGTH)?;
|
||||
|
||||
Ok(LoginSuccess { uuid, username })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SetCompression {
|
||||
pub threshold: i32,
|
||||
}
|
||||
|
||||
impl SetCompression {
|
||||
pub fn new(threshold: i32) -> LoginClientBoundPacket {
|
||||
let set_compression = SetCompression { threshold };
|
||||
|
||||
LoginClientBoundPacket::SetCompression(set_compression)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for SetCompression {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_var_i32(self.threshold)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let threshold = reader.read_var_i32()?;
|
||||
|
||||
Ok(SetCompression { threshold })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LoginPluginRequest {
|
||||
pub message_id: i32,
|
||||
pub channel: String,
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for LoginPluginRequest {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_var_i32(self.message_id)?;
|
||||
writer.write_string(&self.channel, STRING_MAX_LENGTH)?;
|
||||
writer.write_all(&self.data)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let message_id = reader.read_var_i32()?;
|
||||
let channel = reader.read_string(STRING_MAX_LENGTH)?;
|
||||
let mut data = Vec::new();
|
||||
reader.read_to_end(data.as_mut())?;
|
||||
|
||||
Ok(LoginPluginRequest {
|
||||
message_id,
|
||||
channel,
|
||||
data,
|
||||
})
|
||||
}
|
||||
}
|
159
protocol/src/status.rs
Normal file
159
protocol/src/status.rs
Normal file
@@ -0,0 +1,159 @@
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{DecodeError, EncodeError, Packet, PacketWrite, STRING_MAX_LENGTH};
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PingRequest {
|
||||
pub time: u64,
|
||||
}
|
||||
|
||||
impl PingRequest {
|
||||
pub fn new(time: u64) -> StatusServerBoundPacket {
|
||||
let ping_request = PingRequest { time };
|
||||
|
||||
StatusServerBoundPacket::PingRequest(ping_request)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for PingRequest {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_u64::<BigEndian>(self.time)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let time = reader.read_u64::<BigEndian>()?;
|
||||
|
||||
Ok(PingRequest { time })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PingResponse {
|
||||
pub time: u64,
|
||||
}
|
||||
|
||||
impl PingResponse {
|
||||
pub fn new(time: u64) -> StatusClientBoundPacket {
|
||||
let ping_response = PingResponse { time };
|
||||
|
||||
StatusClientBoundPacket::PingResponse(ping_response)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for PingResponse {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
writer.write_u64::<BigEndian>(self.time)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let time = reader.read_u64::<BigEndian>()?;
|
||||
|
||||
Ok(PingResponse { time })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ServerStatus {
|
||||
pub version: ServerVersion,
|
||||
pub description: String,
|
||||
pub players: OnlinePlayers,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ServerVersion {
|
||||
pub name: String,
|
||||
pub protocol: u32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct OnlinePlayers {
|
||||
pub online: u32,
|
||||
pub max: u32,
|
||||
pub sample: Vec<OnlinePlayer>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct OnlinePlayer {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Packet for StatusResponse {
|
||||
type Output = Self;
|
||||
|
||||
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
|
||||
let json = serde_json::to_string(&self.server_status)?;
|
||||
writer.write_string(&json, STRING_MAX_LENGTH)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError> {
|
||||
let server_status = serde_json::from_reader(reader)?;
|
||||
let status_response = StatusResponse { server_status };
|
||||
|
||||
Ok(status_response)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user