mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
[Crate] Add formatting (#22)
* chore: add standard for formatting * chore: manually tidy up imports and format * chore: remove vscode and add to gitignore * chore: alphabetically order and fix * chore: format * chore: fix format issue with payload * chore: format as merge had unformatted code * [format] Fix comments, change max width and binop operator --------- Co-authored-by: CosminPerRam <cosmin.p@live.com>
This commit is contained in:
parent
e023e13236
commit
1b13d39856
71 changed files with 3165 additions and 2593 deletions
|
|
@ -1,68 +1,65 @@
|
|||
|
||||
use crate::GDResult;
|
||||
use crate::bufferer::{Bufferer, Endianess};
|
||||
use crate::GDError::{PacketBad, ProtocolFormat};
|
||||
use crate::protocols::minecraft::{LegacyGroup, JavaResponse, Server};
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
use crate::socket::{Socket, TcpSocket};
|
||||
use crate::utils::error_by_expected_size;
|
||||
|
||||
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<JavaResponse> {
|
||||
self.send_initial_request()?;
|
||||
|
||||
let mut buffer = Bufferer::new_with_data(Endianess::Big, &self.socket.receive(None)?);
|
||||
|
||||
if buffer.get_u8()? != 0xFF {
|
||||
return Err(ProtocolFormat);
|
||||
}
|
||||
|
||||
let length = buffer.get_u16()? * 2;
|
||||
error_by_expected_size((length + 3) as usize, buffer.data_length())?;
|
||||
|
||||
let packet_string = buffer.get_string_utf16()?;
|
||||
|
||||
let split: Vec<&str> = packet_string.split('§').collect();
|
||||
error_by_expected_size(3, split.len())?;
|
||||
|
||||
let description = split[0].to_string();
|
||||
let online_players = split[1].parse()
|
||||
.map_err(|_| PacketBad)?;
|
||||
let max_players = split[2].parse()
|
||||
.map_err(|_| PacketBad)?;
|
||||
|
||||
Ok(JavaResponse {
|
||||
version_name: "Beta 1.8+".to_string(),
|
||||
version_protocol: -1,
|
||||
players_maximum: max_players,
|
||||
players_online: online_players,
|
||||
players_sample: 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<JavaResponse> {
|
||||
LegacyBV1_8::new(address, port, timeout_settings)?.get_info()
|
||||
}
|
||||
}
|
||||
use crate::{
|
||||
bufferer::{Bufferer, Endianess},
|
||||
protocols::{
|
||||
minecraft::{JavaResponse, LegacyGroup, Server},
|
||||
types::TimeoutSettings,
|
||||
},
|
||||
socket::{Socket, TcpSocket},
|
||||
utils::error_by_expected_size,
|
||||
GDError::{PacketBad, ProtocolFormat},
|
||||
GDResult,
|
||||
};
|
||||
|
||||
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<JavaResponse> {
|
||||
self.send_initial_request()?;
|
||||
|
||||
let mut buffer = Bufferer::new_with_data(Endianess::Big, &self.socket.receive(None)?);
|
||||
|
||||
if buffer.get_u8()? != 0xFF {
|
||||
return Err(ProtocolFormat);
|
||||
}
|
||||
|
||||
let length = buffer.get_u16()? * 2;
|
||||
error_by_expected_size((length + 3) as usize, buffer.data_length())?;
|
||||
|
||||
let packet_string = buffer.get_string_utf16()?;
|
||||
|
||||
let split: Vec<&str> = packet_string.split('§').collect();
|
||||
error_by_expected_size(3, split.len())?;
|
||||
|
||||
let description = split[0].to_string();
|
||||
let online_players = split[1].parse().map_err(|_| PacketBad)?;
|
||||
let max_players = split[2].parse().map_err(|_| PacketBad)?;
|
||||
|
||||
Ok(JavaResponse {
|
||||
version_name: "Beta 1.8+".to_string(),
|
||||
version_protocol: -1,
|
||||
players_maximum: max_players,
|
||||
players_online: online_players,
|
||||
players_sample: 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<JavaResponse> {
|
||||
LegacyBV1_8::new(address, port, timeout_settings)?.get_info()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue