mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
Minecraft implementation (#6)
* Initial minecraft support * Made previews_chat an option * Better error handling and removed version structure * Minecraft Server types * Fixed compilation and renamed stuff * 'extract till you drop!' extracted sockets * extracted java version and fixed socket udp receive * Legacy 1.4 and 1.6 implementation (incomplete) * Furter implementation * Implementations work * Protocol beta v1.8+ implemented * Removed bedrock support * Added auto query * Renamed minecraft to mc and added to md's * Docs, renames and small optimization changes * Changed java version to be able to return None on players sample
This commit is contained in:
parent
974e093e23
commit
ee0223a7a3
23 changed files with 810 additions and 80 deletions
9
src/protocols/minecraft/mod.rs
Normal file
9
src/protocols/minecraft/mod.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
/// The implementation.
|
||||
pub mod protocol;
|
||||
/// All types used by the implementation.
|
||||
pub mod types;
|
||||
|
||||
pub use protocol::*;
|
||||
pub use types::*;
|
||||
pub use protocol::*;
|
||||
127
src/protocols/minecraft/protocol/java.rs
Normal file
127
src/protocols/minecraft/protocol/java.rs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
use serde_json::Value;
|
||||
use crate::{GDError, GDResult};
|
||||
use crate::protocols::minecraft::{as_varint, get_string, get_varint, Player, Response, Server};
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
use crate::socket::{Socket, TcpSocket};
|
||||
|
||||
pub struct Java {
|
||||
socket: TcpSocket
|
||||
}
|
||||
|
||||
impl Java {
|
||||
fn new(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Self> {
|
||||
let socket = TcpSocket::new(address, port)?;
|
||||
socket.apply_timeout(timeout_settings)?;
|
||||
|
||||
Ok(Self {
|
||||
socket
|
||||
})
|
||||
}
|
||||
|
||||
fn send(&mut self, data: Vec<u8>) -> GDResult<()> {
|
||||
self.socket.send(&[as_varint(data.len() as i32), data].concat())
|
||||
}
|
||||
|
||||
fn receive(&mut self) -> GDResult<Vec<u8>> {
|
||||
let buf = self.socket.receive(None)?;
|
||||
let mut pos = 0;
|
||||
|
||||
let _packet_length = get_varint(&buf, &mut pos)? as usize;
|
||||
//this declared 'packet length' from within the packet might be wrong (?), not checking with it...
|
||||
|
||||
Ok(buf[pos..].to_vec())
|
||||
}
|
||||
|
||||
fn send_handshake(&mut self) -> GDResult<()> {
|
||||
self.send([
|
||||
//Packet ID (0)
|
||||
0x00,
|
||||
//Protocol Version (-1 to determine version)
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
|
||||
//Server address (can be anything)
|
||||
0x07, 0x47, 0x61, 0x6D, 0x65, 0x44, 0x69, 0x67,
|
||||
//Server port (can be anything)
|
||||
0x00, 0x00,
|
||||
//Next state (1 for status)
|
||||
0x01].to_vec())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send_status_request(&mut self) -> GDResult<()> {
|
||||
self.send([
|
||||
//Packet ID (0)
|
||||
0x00].to_vec())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send_ping_request(&mut self) -> GDResult<()> {
|
||||
self.send([
|
||||
//Packet ID (1)
|
||||
0x01].to_vec())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_info(&mut self) -> GDResult<Response> {
|
||||
self.send_handshake()?;
|
||||
self.send_status_request()?;
|
||||
self.send_ping_request()?;
|
||||
|
||||
let buf = self.receive()?;
|
||||
let mut pos = 0;
|
||||
|
||||
if get_varint(&buf, &mut pos)? != 0 { //first var int is the packet id
|
||||
return Err(GDError::PacketBad("Bad receive packet id.".to_string()));
|
||||
}
|
||||
|
||||
let json_response = get_string(&buf, &mut pos)?;
|
||||
let value_response: Value = serde_json::from_str(&json_response)
|
||||
.map_err(|e| GDError::JsonParse(e.to_string()))?;
|
||||
|
||||
let version_name = value_response["version"]["name"].as_str()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected string.".to_string()))?.to_string();
|
||||
let version_protocol = value_response["version"]["protocol"].as_i64()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected number.".to_string()))? as i32;
|
||||
|
||||
let max_players = value_response["players"]["max"].as_u64()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected number.".to_string()))? as u32;
|
||||
let online_players = value_response["players"]["online"].as_u64()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected number.".to_string()))? as u32;
|
||||
let sample_players: Option<Vec<Player>> = match value_response["players"]["sample"].is_null() {
|
||||
true => None,
|
||||
false => Some({
|
||||
let players_values = value_response["players"]["sample"].as_array()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected array.".to_string()))?;
|
||||
|
||||
let mut players = Vec::with_capacity(players_values.len());
|
||||
for player in players_values {
|
||||
players.push(Player {
|
||||
name: player["name"].as_str().ok_or(GDError::PacketBad("Couldn't get expected string.".to_string()))?.to_string(),
|
||||
id: player["id"].as_str().ok_or(GDError::PacketBad("Couldn't get expected string.".to_string()))?.to_string()
|
||||
})
|
||||
}
|
||||
|
||||
players
|
||||
})
|
||||
};
|
||||
|
||||
Ok(Response {
|
||||
version_name,
|
||||
version_protocol,
|
||||
max_players,
|
||||
online_players,
|
||||
sample_players,
|
||||
description: value_response["description"].to_string(),
|
||||
favicon: value_response["favicon"].as_str().map(str::to_string),
|
||||
previews_chat: value_response["previewsChat"].as_bool(),
|
||||
enforces_secure_chat: value_response["enforcesSecureChat"].as_bool(),
|
||||
server_type: Server::Java
|
||||
})
|
||||
}
|
||||
|
||||
pub fn query(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Response> {
|
||||
Java::new(address, port, timeout_settings)?.get_info()
|
||||
}
|
||||
}
|
||||
71
src/protocols/minecraft/protocol/legacy_bv1_8.rs
Normal file
71
src/protocols/minecraft/protocol/legacy_bv1_8.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
|
||||
use crate::{GDError, GDResult};
|
||||
use crate::protocols::minecraft::{LegacyGroup, Response, Server};
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
use crate::socket::{Socket, TcpSocket};
|
||||
use crate::utils::buffer::{get_string_utf16_be, get_u16_be, get_u8};
|
||||
|
||||
pub struct LegacyBV1_8 {
|
||||
socket: TcpSocket
|
||||
}
|
||||
|
||||
impl LegacyBV1_8 {
|
||||
fn new(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Self> {
|
||||
let socket = TcpSocket::new(address, port)?;
|
||||
socket.apply_timeout(timeout_settings)?;
|
||||
|
||||
Ok(Self {
|
||||
socket
|
||||
})
|
||||
}
|
||||
|
||||
fn send_initial_request(&mut self) -> GDResult<()> {
|
||||
self.socket.send(&[0xFE])
|
||||
}
|
||||
|
||||
fn get_info(&mut self) -> GDResult<Response> {
|
||||
self.send_initial_request()?;
|
||||
|
||||
let buf = self.socket.receive(None)?;
|
||||
let mut pos = 0;
|
||||
|
||||
if get_u8(&buf, &mut pos)? != 0xFF {
|
||||
return Err(GDError::PacketBad("Expected 0xFF".to_string()));
|
||||
}
|
||||
|
||||
let length = get_u16_be(&buf, &mut pos)? * 2;
|
||||
if buf.len() != (length + 3) as usize { //+ 3 because of the first byte and the u16
|
||||
return Err(GDError::PacketBad("Not right size".to_string()));
|
||||
}
|
||||
|
||||
let packet_string = get_string_utf16_be(&buf, &mut pos)?;
|
||||
|
||||
let split: Vec<&str> = packet_string.split("§").collect();
|
||||
if split.len() != 3 {
|
||||
return Err(GDError::PacketBad("Not right size".to_string()));
|
||||
}
|
||||
|
||||
let description = split[0].to_string();
|
||||
let online_players = split[1].parse()
|
||||
.map_err(|_| GDError::PacketBad("Expected int".to_string()))?;
|
||||
let max_players = split[2].parse()
|
||||
.map_err(|_| GDError::PacketBad("Expected int".to_string()))?;
|
||||
|
||||
Ok(Response {
|
||||
version_name: "Beta 1.8+".to_string(),
|
||||
version_protocol: -1,
|
||||
max_players,
|
||||
online_players,
|
||||
sample_players: None,
|
||||
description,
|
||||
favicon: None,
|
||||
previews_chat: None,
|
||||
enforces_secure_chat: None,
|
||||
server_type: Server::Legacy(LegacyGroup::VB1_8)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn query(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Response> {
|
||||
LegacyBV1_8::new(address, port, timeout_settings)?.get_info()
|
||||
}
|
||||
}
|
||||
76
src/protocols/minecraft/protocol/legacy_v1_4.rs
Normal file
76
src/protocols/minecraft/protocol/legacy_v1_4.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
|
||||
use crate::{GDError, GDResult};
|
||||
use crate::protocols::minecraft::{LegacyGroup, Response, Server};
|
||||
use crate::protocols::minecraft::protocol::legacy_v1_6::LegacyV1_6;
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
use crate::socket::{Socket, TcpSocket};
|
||||
use crate::utils::buffer::{get_string_utf16_be, get_u16_be, get_u8};
|
||||
|
||||
pub struct LegacyV1_4 {
|
||||
socket: TcpSocket
|
||||
}
|
||||
|
||||
impl LegacyV1_4 {
|
||||
fn new(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Self> {
|
||||
let socket = TcpSocket::new(address, port)?;
|
||||
socket.apply_timeout(timeout_settings)?;
|
||||
|
||||
Ok(Self {
|
||||
socket
|
||||
})
|
||||
}
|
||||
|
||||
fn send_initial_request(&mut self) -> GDResult<()> {
|
||||
self.socket.send(&[0xFE, 0x01])
|
||||
}
|
||||
|
||||
fn get_info(&mut self) -> GDResult<Response> {
|
||||
self.send_initial_request()?;
|
||||
|
||||
let buf = self.socket.receive(None)?;
|
||||
let mut pos = 0;
|
||||
|
||||
if get_u8(&buf, &mut pos)? != 0xFF {
|
||||
return Err(GDError::PacketBad("Expected 0xFF".to_string()));
|
||||
}
|
||||
|
||||
let length = get_u16_be(&buf, &mut pos)? * 2;
|
||||
if buf.len() != (length + 3) as usize { //+ 3 because of the first byte and the u16
|
||||
return Err(GDError::PacketBad("Not right size".to_string()));
|
||||
}
|
||||
|
||||
if LegacyV1_6::is_protocol(&buf, &mut pos)? {
|
||||
return LegacyV1_6::get_response(&buf, &mut pos);
|
||||
}
|
||||
|
||||
let packet_string = get_string_utf16_be(&buf, &mut pos)?;
|
||||
|
||||
let split: Vec<&str> = packet_string.split("§").collect();
|
||||
if split.len() != 3 {
|
||||
return Err(GDError::PacketBad("Not right size".to_string()));
|
||||
}
|
||||
|
||||
let description = split[0].to_string();
|
||||
let online_players = split[1].parse()
|
||||
.map_err(|_| GDError::PacketBad("Expected int".to_string()))?;
|
||||
let max_players = split[2].parse()
|
||||
.map_err(|_| GDError::PacketBad("Expected int".to_string()))?;
|
||||
|
||||
Ok(Response {
|
||||
version_name: "1.4+".to_string(),
|
||||
version_protocol: -1,
|
||||
max_players,
|
||||
online_players,
|
||||
sample_players: None,
|
||||
description,
|
||||
favicon: None,
|
||||
previews_chat: None,
|
||||
enforces_secure_chat: None,
|
||||
server_type: Server::Legacy(LegacyGroup::V1_4)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn query(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Response> {
|
||||
LegacyV1_4::new(address, port, timeout_settings)?.get_info()
|
||||
}
|
||||
}
|
||||
103
src/protocols/minecraft/protocol/legacy_v1_6.rs
Normal file
103
src/protocols/minecraft/protocol/legacy_v1_6.rs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
use crate::{GDError, GDResult};
|
||||
use crate::protocols::minecraft::{LegacyGroup, Response, Server};
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
use crate::socket::{Socket, TcpSocket};
|
||||
use crate::utils::buffer::{get_string_utf16_be, get_u16_be, get_u8};
|
||||
|
||||
pub struct LegacyV1_6 {
|
||||
socket: TcpSocket
|
||||
}
|
||||
|
||||
impl LegacyV1_6 {
|
||||
fn new(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Self> {
|
||||
let socket = TcpSocket::new(address, port)?;
|
||||
socket.apply_timeout(timeout_settings)?;
|
||||
|
||||
Ok(Self {
|
||||
socket
|
||||
})
|
||||
}
|
||||
|
||||
fn send_initial_request(&mut self) -> GDResult<()> {
|
||||
self.socket.send(&[
|
||||
// Packet ID (FE)
|
||||
0xfe,
|
||||
// Ping payload (01)
|
||||
0x01,
|
||||
// Packet identifier for plugin message
|
||||
0xfa,
|
||||
// Length of 'GameDig' string (7) as unsigned short
|
||||
0x00, 0x07,
|
||||
// 'GameDig' string as UTF-16BE
|
||||
0x00, 0x47, 0x00, 0x61, 0x00, 0x6D, 0x00, 0x65, 0x00, 0x44, 0x00, 0x69, 0x00, 0x67])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_protocol(buf: &[u8], pos: &mut usize) -> GDResult<bool> {
|
||||
let state = buf[*pos..].starts_with(&[0x00, 0xA7, 0x00, 0x31, 0x00, 0x00]);
|
||||
|
||||
if state {
|
||||
*pos += 6;
|
||||
}
|
||||
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
pub fn get_response(buf: &[u8], pos: &mut usize) -> GDResult<Response> {
|
||||
let packet_string = get_string_utf16_be(&buf, pos)?;
|
||||
|
||||
let split: Vec<&str> = packet_string.split("\x00").collect();
|
||||
if split.len() != 5 {
|
||||
return Err(GDError::PacketBad("Not right split size".to_string()));
|
||||
}
|
||||
|
||||
let version_protocol = split[0].parse()
|
||||
.map_err(|_| GDError::PacketBad("Expected int".to_string()))?;
|
||||
let version_name = split[1].to_string();
|
||||
let description = split[2].to_string();
|
||||
let max_players = split[3].parse()
|
||||
.map_err(|_| GDError::PacketBad("Expected int".to_string()))?;
|
||||
let online_players = split[4].parse()
|
||||
.map_err(|_| GDError::PacketBad("Expected int".to_string()))?;
|
||||
|
||||
Ok(Response {
|
||||
version_name,
|
||||
version_protocol,
|
||||
max_players,
|
||||
online_players,
|
||||
sample_players: None,
|
||||
description,
|
||||
favicon: None,
|
||||
previews_chat: None,
|
||||
enforces_secure_chat: None,
|
||||
server_type: Server::Legacy(LegacyGroup::V1_6)
|
||||
})
|
||||
}
|
||||
|
||||
fn get_info(&mut self) -> GDResult<Response> {
|
||||
self.send_initial_request()?;
|
||||
|
||||
let buf = self.socket.receive(None)?;
|
||||
let mut pos = 0;
|
||||
|
||||
if get_u8(&buf, &mut pos)? != 0xFF {
|
||||
return Err(GDError::PacketBad("Expected 0xFF".to_string()));
|
||||
}
|
||||
|
||||
let length = get_u16_be(&buf, &mut pos)? * 2;
|
||||
if buf.len() != (length + 3) as usize { //+ 3 because of the first byte and the u16
|
||||
return Err(GDError::PacketBad("Not right size".to_string()));
|
||||
}
|
||||
|
||||
if !LegacyV1_6::is_protocol(&buf, &mut pos)? {
|
||||
return Err(GDError::PacketBad("Not good".to_string()));
|
||||
}
|
||||
|
||||
LegacyV1_6::get_response(&buf, &mut pos)
|
||||
}
|
||||
|
||||
pub fn query(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Response> {
|
||||
LegacyV1_6::new(address, port, timeout_settings)?.get_info()
|
||||
}
|
||||
}
|
||||
45
src/protocols/minecraft/protocol/mod.rs
Normal file
45
src/protocols/minecraft/protocol/mod.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use crate::{GDError, GDResult};
|
||||
use crate::protocols::minecraft::{LegacyGroup, Response, Server};
|
||||
use crate::protocols::minecraft::protocol::java::Java;
|
||||
use crate::protocols::minecraft::protocol::legacy_v1_4::LegacyV1_4;
|
||||
use crate::protocols::minecraft::protocol::legacy_v1_6::LegacyV1_6;
|
||||
use crate::protocols::minecraft::protocol::legacy_bv1_8::LegacyBV1_8;
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
|
||||
mod java;
|
||||
mod legacy_v1_4;
|
||||
mod legacy_v1_6;
|
||||
mod legacy_bv1_8;
|
||||
|
||||
/// Queries a Minecraft server.
|
||||
pub fn query(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Response> {
|
||||
if let Ok(response) = query_specific(Server::Java, address, port, timeout_settings.clone()) {
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
if let Ok(response) = query_specific(Server::Legacy(LegacyGroup::V1_6), address, port, timeout_settings.clone()) {
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
if let Ok(response) = query_specific(Server::Legacy(LegacyGroup::V1_4), address, port, timeout_settings.clone()) {
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
if let Ok(response) = query_specific(Server::Legacy(LegacyGroup::VB1_8), address, port, timeout_settings.clone()) {
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
Err(GDError::AutoQuery("No protocol returned a response.".to_string()))
|
||||
}
|
||||
|
||||
/// Queries a specific Minecraft Server type.
|
||||
pub fn query_specific(mc_type: Server, address: &str, port: u16, timeout_settings: Option<TimeoutSettings>) -> GDResult<Response> {
|
||||
match mc_type {
|
||||
Server::Java => Java::query(address, port, timeout_settings),
|
||||
Server::Legacy(category) => match category {
|
||||
LegacyGroup::V1_6 => LegacyV1_6::query(address, port, timeout_settings),
|
||||
LegacyGroup::V1_4 => LegacyV1_4::query(address, port, timeout_settings),
|
||||
LegacyGroup::VB1_8 => LegacyBV1_8::query(address, port, timeout_settings),
|
||||
}
|
||||
}
|
||||
}
|
||||
151
src/protocols/minecraft/types.rs
Normal file
151
src/protocols/minecraft/types.rs
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
|
||||
/*
|
||||
|
||||
This file contains lightly modified versions of the original code. (using only the varint parts)
|
||||
Code reference: https://github.com/thisjaiden/golden_apple/blob/master/src/lib.rs
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-2022 Jaiden Bernard
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
use crate::{GDError, GDResult};
|
||||
use crate::utils::buffer::get_u8;
|
||||
|
||||
/// The type of Minecraft Server you want to query
|
||||
#[derive(Debug)]
|
||||
pub enum Server {
|
||||
/// Java Edition
|
||||
Java,
|
||||
/// Legacy Java
|
||||
Legacy(LegacyGroup)
|
||||
}
|
||||
|
||||
/// Legacy Java (Versions) Groups
|
||||
#[derive(Debug)]
|
||||
pub enum LegacyGroup {
|
||||
/// 1.6
|
||||
V1_6,
|
||||
/// 1.4 - 1.5
|
||||
V1_4,
|
||||
/// Beta 1.8 - 1.3
|
||||
VB1_8
|
||||
}
|
||||
|
||||
/// Information about a player
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
pub name: String,
|
||||
pub id: String
|
||||
}
|
||||
|
||||
/// A query response
|
||||
#[derive(Debug)]
|
||||
pub struct Response {
|
||||
/// Version name, example: "1.19.2"
|
||||
pub version_name: String,
|
||||
/// Version protocol, example: 760 (for 1.19.2)
|
||||
pub version_protocol: i32,
|
||||
/// Number of server capacity
|
||||
pub max_players: u32,
|
||||
/// Number of online players
|
||||
pub online_players: u32,
|
||||
/// Some online players (can be missing)
|
||||
pub sample_players: Option<Vec<Player>>,
|
||||
/// Server's description or MOTD
|
||||
pub description: String,
|
||||
/// The favicon (can be missing)
|
||||
pub favicon: Option<String>,
|
||||
/// Tells if the chat preview is enabled (can be missing)
|
||||
pub previews_chat: Option<bool>,
|
||||
/// Tells if secure chat is enforced (can be missing)
|
||||
pub enforces_secure_chat: Option<bool>,
|
||||
/// Tell's the server type
|
||||
pub server_type: Server
|
||||
}
|
||||
|
||||
pub fn get_varint(buf: &[u8], pos: &mut usize) -> GDResult<i32> {
|
||||
let mut result = 0;
|
||||
|
||||
let msb: u8 = 0b10000000;
|
||||
let mask: u8 = !msb;
|
||||
|
||||
for i in 0..5 {
|
||||
let current_byte = get_u8(buf, pos)?;
|
||||
|
||||
result |= ((current_byte & mask) as i32) << (7 * i);
|
||||
|
||||
// The 5th byte is only allowed to have the 4 smallest bits set
|
||||
if i == 4 && (current_byte & 0xf0 != 0) {
|
||||
return Err(GDError::PacketBad("VarInt Overflow".to_string()))
|
||||
}
|
||||
|
||||
if (current_byte & msb) == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn as_varint(value: i32) -> Vec<u8> {
|
||||
let mut bytes = vec![];
|
||||
let mut reading_value = value;
|
||||
|
||||
let msb: u8 = 0b10000000;
|
||||
let mask: i32 = 0b01111111;
|
||||
|
||||
for _ in 0..5 {
|
||||
let tmp = (reading_value & mask) as u8;
|
||||
|
||||
reading_value &= !mask;
|
||||
reading_value = reading_value.rotate_right(7);
|
||||
|
||||
if reading_value != 0 {
|
||||
bytes.push(tmp | msb);
|
||||
} else {
|
||||
bytes.push(tmp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bytes
|
||||
}
|
||||
|
||||
pub fn get_string(buf: &[u8], pos: &mut usize) -> GDResult<String> {
|
||||
let length = get_varint(buf, pos)? as usize;
|
||||
let mut text = vec![0; length];
|
||||
|
||||
for i in 0..length {
|
||||
text[i] = get_u8(buf, pos)?;
|
||||
}
|
||||
|
||||
Ok(String::from_utf8(text)
|
||||
.map_err(|_| GDError::PacketBad("Minecraft bad String".to_string()))?)
|
||||
}
|
||||
|
||||
pub fn as_string(value: String) -> Vec<u8> {
|
||||
let mut buf = as_varint(value.len() as i32);
|
||||
buf.extend(value.as_bytes().to_vec());
|
||||
|
||||
buf
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue