mirror of
https://github.com/timvisee/lazymc.git
synced 2025-05-19 04:40:22 -07:00
Only send status response favicon to client versions that support it
This commit is contained in:
parent
20fb6ee715
commit
aebb5563e0
@ -1,3 +1,8 @@
|
||||
use crate::proto::client::ClientInfo;
|
||||
|
||||
/// Protocol version since when favicons are supported.
|
||||
const FAVICON_PROTOCOL_VERSION: i32 = 4;
|
||||
|
||||
/// Get default server status favicon.
|
||||
pub fn default_favicon() -> String {
|
||||
encode_favicon(include_bytes!("../../res/unknown_server_optimized.png"))
|
||||
@ -9,3 +14,13 @@ pub fn default_favicon() -> String {
|
||||
pub fn encode_favicon(data: &[u8]) -> String {
|
||||
format!("{}{}", "data:image/png;base64,", base64::encode(data))
|
||||
}
|
||||
|
||||
/// Check whether the status response favicon is supported based on the given client info.
|
||||
///
|
||||
/// Defaults to `true` if unsure.
|
||||
pub fn supports_favicon(client_info: &ClientInfo) -> bool {
|
||||
client_info
|
||||
.protocol_version
|
||||
.map(|p| p >= FAVICON_PROTOCOL_VERSION)
|
||||
.unwrap_or(true)
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ pub async fn serve(
|
||||
|
||||
// Hijack server status packet
|
||||
if client_state == ClientState::Status && packet.id == packets::status::SERVER_STATUS {
|
||||
let server_status = server_status(&config, &server).await;
|
||||
let server_status = server_status(&client_info, &config, &server).await;
|
||||
let packet = StatusResponse { server_status };
|
||||
|
||||
let mut data = Vec::new();
|
||||
@ -197,7 +197,7 @@ pub async fn serve(
|
||||
}
|
||||
|
||||
/// Build server status object to respond to client with.
|
||||
async fn server_status(config: &Config, server: &Server) -> ServerStatus {
|
||||
async fn server_status(client_info: &ClientInfo, config: &Config, server: &Server) -> ServerStatus {
|
||||
let status = server.status().await;
|
||||
let server_state = server.state();
|
||||
|
||||
@ -233,11 +233,13 @@ async fn server_status(config: &Config, server: &Server) -> ServerStatus {
|
||||
|
||||
// Extract favicon from real server status, load from disk, or use default
|
||||
let mut favicon = None;
|
||||
if config.motd.from_server && status.is_some() {
|
||||
favicon = status.as_ref().unwrap().favicon.clone()
|
||||
}
|
||||
if favicon.is_none() {
|
||||
favicon = Some(server_favicon(&config).await);
|
||||
if favicon::supports_favicon(client_info) {
|
||||
if config.motd.from_server && status.is_some() {
|
||||
favicon = status.as_ref().unwrap().favicon.clone()
|
||||
}
|
||||
if favicon.is_none() {
|
||||
favicon = Some(server_favicon(&config).await);
|
||||
}
|
||||
}
|
||||
|
||||
// Build status resposne
|
||||
|
Loading…
x
Reference in New Issue
Block a user