diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index f05fe35..347c8c4 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -346,6 +346,28 @@ impl Serializable for f64 { #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub struct UUID(u64, u64); +impl UUID { + pub fn from_str(s: &str) -> UUID { + use rustc_serialize::hex::FromHex; + // TODO: Panics aren't the best idea here + if s.len() != 36 { + panic!("Invalid UUID format"); + } + let mut parts = s[..8].from_hex().unwrap(); + parts.extend_from_slice(&s[9..13].from_hex().unwrap()); + parts.extend_from_slice(&s[14..18].from_hex().unwrap()); + parts.extend_from_slice(&s[19..23].from_hex().unwrap()); + parts.extend_from_slice(&s[24..36].from_hex().unwrap()); + let mut high = 0u64; + let mut low = 0u64; + for i in 0 .. 8 { + high |= (parts[i] as u64) << (56 - i*8); + low |= (parts[i + 8] as u64) << (56 - i*8); + } + UUID(high, low) + } +} + impl Default for UUID { fn default() -> Self { UUID(0, 0) diff --git a/src/server/mod.rs b/src/server/mod.rs index 6fd4363..c913413 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -38,6 +38,8 @@ use format; mod sun; pub struct Server { + username: String, + uuid: protocol::UUID, conn: Option, read_queue: Option>>, @@ -142,7 +144,7 @@ impl Server { read.state = protocol::State::Play; write.state = protocol::State::Play; let rx = Self::spawn_reader(read); - return Ok(Server::new(resources, console, Some(write), Some(rx))); + return Ok(Server::new(val.username, protocol::UUID::from_str(&val.uuid), resources, console, Some(write), Some(rx))); } protocol::packet::Packet::LoginDisconnect(val) => return Err(protocol::Error::Disconnect(val.reason)), val => return Err(protocol::Error::Err(format!("Wrong packet: {:?}", val))), @@ -169,6 +171,8 @@ impl Server { read.enable_encyption(&shared, true); write.enable_encyption(&shared, false); + let username; + let uuid; loop { match try!(read.read_packet()) { protocol::packet::Packet::SetInitialCompression(val) => { @@ -177,6 +181,8 @@ impl Server { } protocol::packet::Packet::LoginSuccess(val) => { debug!("Login: {} {}", val.username, val.uuid); + username = val.username; + uuid = val.uuid; read.state = protocol::State::Play; write.state = protocol::State::Play; break; @@ -188,7 +194,7 @@ impl Server { let rx = Self::spawn_reader(read); - Ok(Server::new(resources, console, Some(write), Some(rx))) + Ok(Server::new(username, protocol::UUID::from_str(&uuid), resources, console, Some(write), Some(rx))) } fn spawn_reader(mut read: protocol::Conn) -> mpsc::Receiver> { @@ -209,7 +215,7 @@ impl Server { } pub fn dummy_server(resources: Arc>, console: Arc>) -> Server { - let mut server = Server::new(resources, console, None, None); + let mut server = Server::new("dummy".to_owned(), protocol::UUID::default(), resources, console, None, None); let mut rng = rand::thread_rng(); for x in -7*16 .. 7*16 { for z in -7*16 .. 7*16 { @@ -245,6 +251,7 @@ impl Server { } fn new( + username: String, uuid: protocol::UUID, resources: Arc>, console: Arc>, conn: Option, read_queue: Option>> ) -> Server { @@ -257,6 +264,8 @@ impl Server { let version = resources.read().unwrap().version(); Server { + username: username, + uuid: uuid, conn: conn, read_queue: read_queue,