From 1820e9f071af16ed62b6c2c813c9ba94bb3b2b0f Mon Sep 17 00:00:00 2001 From: TheHellBox Date: Wed, 31 Mar 2021 02:11:22 +0500 Subject: [PATCH] cargo fmt --- kissmp-bridge/src/decoder.rs | 10 +++--- kissmp-bridge/src/discord.rs | 4 +-- kissmp-bridge/src/encoder.rs | 8 ++--- kissmp-bridge/src/http_proxy.rs | 2 +- kissmp-bridge/src/main.rs | 7 ++-- kissmp-server/src/events.rs | 36 +++++++++++-------- kissmp-server/src/file_transfer.rs | 19 ++++++---- kissmp-server/src/incoming.rs | 6 ++-- kissmp-server/src/lua.rs | 21 +++++++---- kissmp-server/src/main.rs | 55 +++++++++++++++++------------ kissmp-server/src/server_vehicle.rs | 5 +-- 11 files changed, 99 insertions(+), 74 deletions(-) diff --git a/kissmp-bridge/src/decoder.rs b/kissmp-bridge/src/decoder.rs index 02c2031..e64dcae 100644 --- a/kissmp-bridge/src/decoder.rs +++ b/kissmp-bridge/src/decoder.rs @@ -12,7 +12,7 @@ pub async fn decode(data: &[u8], writer: tokio::sync::mpsc::Sender>) { result.append(&mut data_left.to_le_bytes().to_vec()); result.append(&mut data.clone()); writer.send(result).await.unwrap(); - }, + } _ => { let json = serde_json::to_string(&decoded); if let Ok(json) = json { @@ -22,14 +22,12 @@ pub async fn decode(data: &[u8], writer: tokio::sync::mpsc::Sender>) { result.append(&mut (data.len() as u32).to_le_bytes().to_vec()); result.append(&mut data); writer.send(result).await.unwrap(); - } - else{ + } else { println!("Error: {:?}", json); } - }, + } } - } - else{ + } else { println!("Error bin: {:?}", decoded); } } diff --git a/kissmp-bridge/src/discord.rs b/kissmp-bridge/src/discord.rs index 3e9c2b7..bfa7a7c 100644 --- a/kissmp-bridge/src/discord.rs +++ b/kissmp-bridge/src/discord.rs @@ -1,7 +1,7 @@ pub async fn spawn_discord_rpc(mut discord_rx: std::sync::mpsc::Receiver) { //let discord_rx = tokio_stream::wrappers::ReceiverStream::new(discord_rx); std::thread::spawn(move || { - async move{ + async move { let mut drpc_client = discord_rpc_client::Client::new(771278096627662928); drpc_client.start(); drpc_client @@ -21,7 +21,7 @@ pub async fn spawn_discord_rpc(mut discord_rx: std::sync::mpsc::Receiver Vec { if let Ok(decoded) = decoded { let binary = bincode::serialize::(&decoded); if let Ok(binary) = binary { - return binary - } - else{ + return binary; + } else { println!("e {:?}", binary); } - } - else{ + } else { println!("e j {:?}", decoded); } vec![] diff --git a/kissmp-bridge/src/http_proxy.rs b/kissmp-bridge/src/http_proxy.rs index 59b27a3..d37d356 100644 --- a/kissmp-bridge/src/http_proxy.rs +++ b/kissmp-bridge/src/http_proxy.rs @@ -5,7 +5,7 @@ pub async fn spawn_http_proxy(mut discord_tx: std::sync::mpsc::Sender { let player_name = connection.client_info_public.name.clone(); @@ -109,7 +109,9 @@ impl Server { } } VehicleUpdate(data) => { - if let Some(server_id) = self.get_server_id_from_game_id(client_id, data.vehicle_id) { + if let Some(server_id) = + self.get_server_id_from_game_id(client_id, data.vehicle_id) + { if let Some(vehicle) = self.vehicles.get_mut(&server_id) { vehicle.data.position = data.transform.position; vehicle.data.rotation = data.transform.rotation; @@ -121,12 +123,15 @@ impl Server { } VehicleData(data) => { // Remove old vehicle with the same ID - if let Some(server_id) = self.get_server_id_from_game_id(client_id, data.in_game_id) + if let Some(server_id) = + self.get_server_id_from_game_id(client_id, data.in_game_id) { self.remove_vehicle(server_id, Some(client_id)).await; } if let Some(client_vehicles) = self.vehicle_ids.get(&client_id) { - if (data.name == "unicicle") && (client_vehicles.len() as u8 >= self.max_vehicles_per_client) { + if (data.name == "unicicle") + && (client_vehicles.len() as u8 >= self.max_vehicles_per_client) + { return; } } @@ -164,7 +169,8 @@ impl Server { } } VehicleMetaUpdate(meta) => { - if let Some(server_id) = self.get_server_id_from_game_id(client_id, meta.vehicle_id) + if let Some(server_id) = + self.get_server_id_from_game_id(client_id, meta.vehicle_id) { if let Some(vehicle) = self.vehicles.get_mut(&server_id) { vehicle.data.color = meta.colors_table[0]; @@ -186,7 +192,7 @@ impl Server { if let Some(server_id) = self.get_server_id_from_game_id(client_id, vehicle_id) { - /* if let Some(vehicle) = self.vehicles.get_mut(&server_id) { + /* if let Some(vehicle) = self.vehicles.get_mut(&server_id) { for (key, value) in &undefined_update.diff { if let Some(electrics) = &mut vehicle.electrics { electrics.undefined.insert(key.clone(), *value); @@ -196,21 +202,23 @@ impl Server { for (_, client) in &mut self.connections { let _ = client .ordered - .send(ServerCommand::ElectricsUndefinedUpdate(server_id, undefined_update.clone())) + .send(ServerCommand::ElectricsUndefinedUpdate( + server_id, + undefined_update.clone(), + )) .await; } } } Ping(ping) => { - let connection = self.connections - .get_mut(&client_id) - .unwrap(); - connection - .client_info_public - .ping = ping as u32; + let connection = self.connections.get_mut(&client_id).unwrap(); + connection.client_info_public.ping = ping as u32; let start = std::time::SystemTime::now(); let since_the_epoch = start.duration_since(std::time::UNIX_EPOCH).unwrap(); - let data = bincode::serialize(&shared::ServerCommand::Pong(since_the_epoch.as_secs_f64())).unwrap(); + let data = bincode::serialize(&shared::ServerCommand::Pong( + since_the_epoch.as_secs_f64(), + )) + .unwrap(); let _ = connection.conn.send_datagram(data.into()); } VehicleChanged(id) => { diff --git a/kissmp-server/src/file_transfer.rs b/kissmp-server/src/file_transfer.rs index 52039a4..dd3d2d3 100644 --- a/kissmp-server/src/file_transfer.rs +++ b/kissmp-server/src/file_transfer.rs @@ -19,13 +19,18 @@ pub async fn transfer_file( break; } let mut stream = connection.open_uni().await?; - send(&mut stream, &bincode::serialize(&shared::ServerCommand::FilePart( - file_name.to_string(), - buf[0..n].to_vec(), - chunk_n, - file_length, - n as u32 - )).unwrap()).await?; + send( + &mut stream, + &bincode::serialize(&shared::ServerCommand::FilePart( + file_name.to_string(), + buf[0..n].to_vec(), + chunk_n, + file_length, + n as u32, + )) + .unwrap(), + ) + .await?; stream.finish(); chunk_n += 1; } diff --git a/kissmp-server/src/incoming.rs b/kissmp-server/src/incoming.rs index ff42326..d348da5 100644 --- a/kissmp-server/src/incoming.rs +++ b/kissmp-server/src/incoming.rs @@ -4,7 +4,7 @@ use crate::*; pub enum IncomingEvent { ClientConnected(Connection), ConnectionLost, - ClientCommand(shared::ClientCommand) + ClientCommand(shared::ClientCommand), } impl Server { @@ -14,7 +14,9 @@ impl Server { client_events_tx: &mut mpsc::Sender<(u32, IncomingEvent)>, ) -> anyhow::Result<()> { let client_command = bincode::deserialize::(&data)?; - client_events_tx.send((id, IncomingEvent::ClientCommand(client_command))).await?; + client_events_tx + .send((id, IncomingEvent::ClientCommand(client_command))) + .await?; Ok(()) } } diff --git a/kissmp-server/src/lua.rs b/kissmp-server/src/lua.rs index 71fa88e..2501370 100644 --- a/kissmp-server/src/lua.rs +++ b/kissmp-server/src/lua.rs @@ -25,7 +25,11 @@ struct LuaVehicleData(VehicleData); impl rlua::UserData for LuaTransform { fn add_methods<'lua, M: rlua::UserDataMethods<'lua, Self>>(methods: &mut M) { methods.add_method("getPosition", |_, this, _: ()| { - Ok(vec![this.0.position[0], this.0.position[1], this.0.position[2]]) + Ok(vec![ + this.0.position[0], + this.0.position[1], + this.0.position[2], + ]) }); methods.add_method("getRotation", |_, this, _: ()| { Ok(vec![ @@ -36,7 +40,11 @@ impl rlua::UserData for LuaTransform { ]) }); methods.add_method("getVelocity", |_, this, _: ()| { - Ok(vec![this.0.velocity[0], this.0.velocity[1], this.0.velocity[2]]) + Ok(vec![ + this.0.velocity[0], + this.0.velocity[1], + this.0.velocity[2], + ]) }); methods.add_method("getAngularVelocity", |_, this, _: ()| { Ok(vec![ @@ -68,12 +76,13 @@ impl rlua::UserData for Vehicle { methods.add_method("getTransform", |_, this, _: ()| { if let Some(transform) = &this.transform { Ok(Some(LuaTransform(transform.clone()))) - } - else{ + } else { Ok(None) } }); - methods.add_method("getData", |_, this, _: ()| Ok(LuaVehicleData(this.data.clone()))); + methods.add_method("getData", |_, this, _: ()| { + Ok(LuaVehicleData(this.data.clone())) + }); methods.add_method("remove", |lua_ctx, this, _: ()| { let globals = lua_ctx.globals(); let sender: MpscChannelSender = globals.get("MPSC_CHANNEL_SENDER")?; @@ -397,7 +406,7 @@ pub fn setup_lua() -> (rlua::Lua, mpsc::Receiver) { Vec, Vec, )| { - Ok(LuaVehicleData(VehicleData{ + Ok(LuaVehicleData(VehicleData { parts_config, in_game_id: 0, color: [color[0], color[1], color[2], color[3]], diff --git a/kissmp-server/src/main.rs b/kissmp-server/src/main.rs index 6e4cb3f..7da42a0 100644 --- a/kissmp-server/src/main.rs +++ b/kissmp-server/src/main.rs @@ -11,8 +11,8 @@ pub mod outgoing; pub mod server_vehicle; use incoming::IncomingEvent; -use shared::{ClientInfoPublic, ClientInfoPrivate, ServerCommand}; use server_vehicle::*; +use shared::{ClientInfoPrivate, ClientInfoPublic, ServerCommand}; use vehicle::*; use anyhow::Error; @@ -41,7 +41,10 @@ impl std::fmt::Debug for Connection { } impl Connection { pub async fn send_chat_message(&mut self, message: String) { - let _ = self.ordered.send(ServerCommand::Chat(message.clone())).await; + let _ = self + .ordered + .send(ServerCommand::Chat(message.clone())) + .await; } pub async fn send_lua(&mut self, lua: String) { let _ = self.ordered.send(ServerCommand::SendLua(lua.clone())).await; @@ -72,9 +75,12 @@ struct Server { impl Server { async fn run(mut self) { - let mut ticks = - IntervalStream::new(tokio::time::interval(std::time::Duration::from_secs(1) / self.tickrate as u32)).fuse(); - let mut send_info_ticks = IntervalStream::new(tokio::time::interval(std::time::Duration::from_secs(1))).fuse(); + let mut ticks = IntervalStream::new(tokio::time::interval( + std::time::Duration::from_secs(1) / self.tickrate as u32, + )) + .fuse(); + let mut send_info_ticks = + IntervalStream::new(tokio::time::interval(std::time::Duration::from_secs(1))).fuse(); let (certificate_chain, key) = generate_certificate(); let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), self.port); @@ -201,11 +207,11 @@ impl Server { let len = u32::from_le_bytes(buf) as usize; let mut buf: Vec = vec![0; len]; stream.read_exact(&mut buf).await?; - let info: shared::ClientCommand = bincode::deserialize::(&buf)?; + let info: shared::ClientCommand = + bincode::deserialize::(&buf)?; if let shared::ClientCommand::ClientInfo(info) = info { Ok(info) - } - else{ + } else { Err(anyhow::Error::msg("Failed to fetch client info")) } } else { @@ -232,24 +238,25 @@ impl Server { 0u32.into(), format!( "Client version mismatch.\nClient version: {:?}\nServer version: {:?}", - client_info.client_version, shared::VERSION + client_info.client_version, + shared::VERSION ) .as_bytes(), ); return; } - let client_info_public = ClientInfoPublic{ + let client_info_public = ClientInfoPublic { name: client_info.name.clone(), id: id, current_vehicle: 0, - ping: 0 + ping: 0, }; let client_connection = Connection { conn: connection_clone.clone(), ordered: ordered_tx, unreliable: unreliable_tx, client_info_private: client_info, - client_info_public: client_info_public + client_info_public: client_info_public, }; client_events_tx .send((id, IncomingEvent::ClientConnected(client_connection))) @@ -270,16 +277,18 @@ impl Server { } }); - let server_info = bincode::serialize(&shared::ServerCommand::ServerInfo(shared::ServerInfo{ - name: self.name.clone(), - player_count: self.connections.len() as u8, - client_id: id, - map: self.map.clone(), - tickrate: self.tickrate, - max_vehicles_per_client: self.max_vehicles_per_client, - mods: list_mods().unwrap_or(vec![]), - server_identifier: self.server_identifier.clone() - })).unwrap(); + let server_info = + bincode::serialize(&shared::ServerCommand::ServerInfo(shared::ServerInfo { + name: self.name.clone(), + player_count: self.connections.len() as u8, + client_id: id, + map: self.map.clone(), + tickrate: self.tickrate, + max_vehicles_per_client: self.max_vehicles_per_client, + mods: list_mods().unwrap_or(vec![]), + server_identifier: self.server_identifier.clone(), + })) + .unwrap(); // Sender tokio::spawn(async move { let mut stream = connection.open_uni().await; @@ -458,7 +467,7 @@ async fn main() { lua_watcher_rx: watcher_rx, lua_commands: receiver, server_identifier: config.server_identifier, - tick: 0 + tick: 0, }; server.run().await; } diff --git a/kissmp-server/src/server_vehicle.rs b/kissmp-server/src/server_vehicle.rs index eafd2d3..840118c 100644 --- a/kissmp-server/src/server_vehicle.rs +++ b/kissmp-server/src/server_vehicle.rs @@ -24,10 +24,7 @@ impl crate::Server { if Some(*cid) == client_id { continue; } - let _ = client - .ordered - .send(ServerCommand::RemoveVehicle(id)) - .await; + let _ = client.ordered.send(ServerCommand::RemoveVehicle(id)).await; } self.lua.context(|lua_ctx| { let _ = crate::lua::run_hook::<(u32, Option), ()>(