@@ -6,13 +6,14 @@ use crate::state::attached_world_data::AttachedWorldData;
66use crate :: state:: {
77 Profile , ProfileInstallStage , attached_world_data, server_join_log,
88} ;
9- use crate :: util:: io;
109use crate :: util:: protocol_version:: OLD_PROTOCOL_VERSIONS ;
1110pub use crate :: util:: protocol_version:: ProtocolVersion ;
1211pub use crate :: util:: server_ping:: {
1312 ServerGameProfile , ServerPlayers , ServerStatus , ServerVersion ,
1413} ;
15- use crate :: { Context , ErrorKind , Result , State , launcher} ;
14+ use crate :: util:: { io, server_ping} ;
15+ use crate :: { Error , ErrorKind , Result , State , launcher} ;
16+ use async_minecraft_ping:: ServerDescription ;
1617use async_walkdir:: WalkDir ;
1718use async_zip:: { Compression , ZipEntryBuilder } ;
1819use chrono:: { DateTime , Local , TimeZone , Utc } ;
@@ -909,54 +910,67 @@ pub async fn get_server_status(
909910 "Pinging {address} with protocol version {protocol_version:?}"
910911 ) ;
911912
912- // get_server_status_old(address, protocol_version).await
913- get_server_status_new ( address, protocol_version) . await
914- }
915-
916- // async fn _get_server_status_old(
917- // address: &str,
918- // protocol_version: Option<ProtocolVersion>,
919- // ) -> Result<ServerStatus> {
920- // let (original_host, original_port) = parse_server_address(address)?;
921- // let (host, port) =
922- // resolve_server_address(original_host, original_port).await?;
923- // tracing::debug!(
924- // "Pinging {address} with protocol version {protocol_version:?}"
925- // );
926- // server_ping::get_server_status(
927- // &(&host as &str, port),
928- // (original_host, original_port),
929- // protocol_version,
930- // )
931- // .await
932-
933- async fn get_server_status_new (
913+ get_server_status_old ( address, protocol_version) . await
914+ // get_server_status_new(address, protocol_version).await
915+ }
916+
917+ async fn get_server_status_old (
918+ address : & str ,
919+ protocol_version : Option < ProtocolVersion > ,
920+ ) -> Result < ServerStatus > {
921+ let ( original_host, original_port) = parse_server_address ( address) ?;
922+ let ( host, port) =
923+ resolve_server_address ( original_host, original_port) . await ?;
924+ tracing:: debug!(
925+ "Pinging {address} with protocol version {protocol_version:?}"
926+ ) ;
927+ server_ping:: get_server_status (
928+ & ( & host as & str , port) ,
929+ ( original_host, original_port) ,
930+ protocol_version,
931+ )
932+ . await
933+ }
934+
935+ async fn _get_server_status_new (
934936 address : & str ,
935937 protocol_version : Option < ProtocolVersion > ,
936938) -> Result < ServerStatus > {
939+ let ( address, port) = match address. rsplit_once ( ':' ) {
940+ Some ( ( addr, port) ) => {
941+ let port = port. parse :: < u16 > ( ) . map_err ( |_err| {
942+ Error :: from ( ErrorKind :: InputError ( "invalid port number" . into ( ) ) )
943+ } ) ?;
944+ ( addr, port)
945+ }
946+ None => ( address, 25565 ) ,
947+ } ;
948+
937949 let mut builder = async_minecraft_ping:: ConnectionConfig :: build ( address)
950+ . with_port ( port)
938951 . with_srv_lookup ( ) ;
939952
940953 if let Some ( version) = protocol_version {
941954 builder = builder. with_protocol_version ( version. version as usize )
942955 }
943956
944- let conn = builder
945- . connect ( )
946- . await
947- . wrap_err ( "failed to connect to server" ) ?;
957+ let conn = builder. connect ( ) . await . map_err ( |_err| {
958+ Error :: from ( ErrorKind :: InputError ( "failed to connect to server" . into ( ) ) )
959+ } ) ?;
948960
949- let ping_conn = conn
950- . status ( )
951- . await
952- . wrap_err ( "failed to get server status" ) ?;
961+ let ping_conn = conn. status ( ) . await . map_err ( |_err| {
962+ Error :: from ( ErrorKind :: InputError ( "failed to get server status" . into ( ) ) )
963+ } ) ?;
953964 let status = & ping_conn. status ;
954- let description = status. description . as_ref ( ) . map ( |d| {
955- let json =
956- serde_json:: to_string ( d) . expect ( "serializing should not fail" ) ;
957- RawValue :: from_string ( json)
958- . expect ( "converting to `RawValue` should not fail" )
959- } ) ;
965+ let description = match & status. description {
966+ ServerDescription :: Plain ( text) => {
967+ serde_json:: value:: to_raw_value ( & text) . ok ( )
968+ }
969+ ServerDescription :: Object { text } => {
970+ // TODO: `text` always seems to be empty?
971+ RawValue :: from_string ( text. clone ( ) ) . ok ( )
972+ }
973+ } ;
960974
961975 let players = ServerPlayers {
962976 max : status. players . max ,
@@ -986,10 +1000,9 @@ async fn get_server_status_new(
9861000 let latency = {
9871001 let start = Instant :: now ( ) ;
9881002 let ping_magic = Utc :: now ( ) . timestamp_millis ( ) . cast_unsigned ( ) ;
989- ping_conn
990- . ping ( ping_magic)
991- . await
992- . wrap_err ( "failed to do ping" ) ?;
1003+ ping_conn. ping ( ping_magic) . await . map_err ( |_err| {
1004+ Error :: from ( ErrorKind :: InputError ( "failed to do ping" . into ( ) ) )
1005+ } ) ?;
9931006 start. elapsed ( ) . as_millis ( ) as i64
9941007 } ;
9951008
0 commit comments