Store the client's UUID and username when logging in

This commit is contained in:
Thinkofname 2016-04-06 22:50:31 +01:00
parent c21a740077
commit 0bbb10918e
2 changed files with 34 additions and 3 deletions

View File

@ -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)

View File

@ -38,6 +38,8 @@ use format;
mod sun;
pub struct Server {
username: String,
uuid: protocol::UUID,
conn: Option<protocol::Conn>,
read_queue: Option<mpsc::Receiver<Result<packet::Packet, protocol::Error>>>,
@ -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<Result<packet::Packet, protocol::Error>> {
@ -209,7 +215,7 @@ impl Server {
}
pub fn dummy_server(resources: Arc<RwLock<resources::Manager>>, console: Arc<Mutex<console::Console>>) -> 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<RwLock<resources::Manager>>, console: Arc<Mutex<console::Console>>,
conn: Option<protocol::Conn>, read_queue: Option<mpsc::Receiver<Result<packet::Packet, protocol::Error>>>
) -> Server {
@ -257,6 +264,8 @@ impl Server {
let version = resources.read().unwrap().version();
Server {
username: username,
uuid: uuid,
conn: conn,
read_queue: read_queue,