Skip to content

Commit e2232fd

Browse files
changed status packet building method
1 parent 257dc55 commit e2232fd

File tree

5 files changed

+37
-44
lines changed

5 files changed

+37
-44
lines changed

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"server_ver": "1.21.4",
88
"motd": "§6mc-oauth.andcool.ru",
99
"code_life_time": 300,
10-
"icon": "server_icon.png"
10+
"icon_path": "server_icon.png"
1111
}

src/config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub async fn load(path: &str) -> Result<()> {
1616

1717
let mut config: types::Config =
1818
serde_json::from_str(&file).expect("Couldn't parse config json");
19-
match server_icon::load(&config.icon).await {
19+
match server_icon::load(&config.icon_path).await {
2020
Ok(base64) => config.image = Some(format!("data:image/png;base64,{}", base64)),
2121
Err(e) => warn!("Error loading server icon: {}", e)
2222
}

src/config/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Config {
2727
pub code_life_time: u64,
2828

2929
/// Path to the server icon
30-
pub icon: String,
30+
pub icon_path: String,
3131

3232
#[serde(skip)]
3333
/// Base 64 encoded server icon

src/packets/status.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::Result;
22
use bytes::BytesMut;
3-
use serde_json::{json, Value};
3+
use serde::Serialize;
4+
use serde_json::Value;
45

56
use crate::byte_buf_utils::{add_size, write_utf8, write_varint};
67

@@ -11,37 +12,31 @@ impl StatusPacket {
1112
let mut buffer = BytesMut::new();
1213

1314
write_varint(&mut buffer, 0x00); // Packet id
14-
write_utf8(&mut buffer, &data.build().to_string())?;
15+
write_utf8(&mut buffer, &serde_json::to_string(&data)?)?;
1516

1617
Ok(add_size(&buffer))
1718
}
1819
}
1920

21+
#[derive(Serialize)]
2022
pub struct StatusData {
21-
pub version_name: String,
22-
pub version_protocol: usize,
23-
pub players_max: usize,
24-
pub players_online: usize,
23+
pub version: Version,
24+
pub players: Players,
2525
pub description: Value,
26-
pub favicon: String,
26+
#[serde(skip_serializing_if = "Option::is_none")]
27+
pub favicon: Option<String>,
2728
pub enforces_secure_chat: bool,
2829
}
2930

30-
impl StatusData {
31-
pub fn build(&self) -> Value {
32-
json!({
33-
"version": {
34-
"name": self.version_name,
35-
"protocol": self.version_protocol
36-
},
37-
"players": {
38-
"max": self.players_max,
39-
"online": self.players_online,
40-
"sample": []
41-
},
42-
"description": self.description,
43-
"favicon": self.favicon,
44-
"enforcesSecureChat": self.enforces_secure_chat
45-
})
46-
}
31+
#[derive(Serialize)]
32+
pub struct Players {
33+
pub max: usize,
34+
pub online: usize,
35+
pub sample: Vec<Value>,
36+
}
37+
38+
#[derive(Serialize)]
39+
pub struct Version {
40+
pub name: String,
41+
pub protocol: usize,
4742
}

src/responses/status.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ use anyhow::Result;
33
use serde_json::json;
44
use tokio::{io::AsyncWriteExt, net::TcpStream};
55

6-
use crate::{
7-
client_sessions::Session,
8-
config::get_config,
9-
packets::status::{StatusData, StatusPacket},
10-
};
6+
use crate::{client_sessions::Session, config::get_config, packets::status};
117

128
pub async fn send(stream: &mut TcpStream, session: &mut Session) -> Result<()> {
139
let config = get_config().await;
@@ -17,21 +13,23 @@ pub async fn send(stream: &mut TcpStream, session: &mut Session) -> Result<()> {
1713
config.proto_ver
1814
};
1915

20-
let icon = match &config.image {
21-
Some(img) => img,
22-
None => &"".to_string()
23-
};
24-
25-
let data = StatusData {
26-
version_name: config.server_ver.clone(),
27-
version_protocol: proto_ver,
28-
players_max: config.players_max,
29-
players_online: config.players_online,
16+
let data = status::StatusData {
17+
version: status::Version {
18+
name: config.server_ver.clone(),
19+
protocol: proto_ver,
20+
},
21+
players: status::Players {
22+
max: config.players_max,
23+
online: config.players_online,
24+
sample: vec![],
25+
},
3026
description: json!({"text": config.motd.clone()}),
31-
favicon: icon.to_string(),
27+
favicon: config.image.clone(),
3228
enforces_secure_chat: false,
3329
};
3430

35-
stream.write_all(&StatusPacket::build(data)?).await?;
31+
stream
32+
.write_all(&status::StatusPacket::build(data)?)
33+
.await?;
3634
Ok(())
3735
}

0 commit comments

Comments
 (0)