From edb6319189ac59600904b10ab92df4894c5ec7d8 Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 14 Nov 2023 22:05:13 -0700 Subject: [PATCH] how has libpso avoided the baleful eye of clippy --- psopacket/src/lib.rs | 10 +++---- src/character/mod.rs | 1 + src/crypto/bb.rs | 15 ++++------ src/crypto/mod.rs | 12 ++++---- src/crypto/pc.rs | 14 ++++----- src/lib.rs | 4 +-- src/packet/login.rs | 67 +++++++++++++++++++++--------------------- src/packet/messages.rs | 16 +++++----- src/packet/patch.rs | 45 ++++++++++++---------------- src/packet/ship.rs | 40 ++++++++++++------------- src/util.rs | 6 ++-- 11 files changed, 109 insertions(+), 121 deletions(-) diff --git a/psopacket/src/lib.rs b/psopacket/src/lib.rs index bbe96ca..fa93e38 100644 --- a/psopacket/src/lib.rs +++ b/psopacket/src/lib.rs @@ -149,14 +149,14 @@ fn generate_psopacket_impl(pkt_cmd: u16, name: syn::Ident, attrs: &Vec fn from_bytes(data: &[u8]) -> Result<#name, PacketParseError> { let mut cur = std::io::Cursor::new(data); let mut b: [u8; 2] = [0; 2]; - cur.read(&mut b).unwrap(); + cur.read_exact(&mut b).unwrap(); let len = u16::from_le_bytes(b); - cur.read(&mut b).unwrap(); + cur.read_exact(&mut b).unwrap(); let cmd = u16::from_le_bytes(b); let mut f: [u8; 4] = [0; 4]; let flag = if #include_flag { - cur.read(&mut f).unwrap(); + cur.read_exact(&mut f).unwrap(); u32::from_le_bytes(f) } else { 0 }; @@ -542,9 +542,9 @@ fn generate_psomessage_impl(msg_cmd: u8, name: syn::Ident, attrs: &Vec const CMD: u8 = #msg_cmd; fn from_bytes(mut cur: &mut R) -> Result<#name, PacketParseError> { let mut buf1 = [0u8; 1]; - cur.read(&mut buf1).unwrap(); + cur.read_exact(&mut buf1).unwrap(); let cmd = buf1[0]; - cur.read(&mut buf1).unwrap(); + cur.read_exact(&mut buf1).unwrap(); let size = buf1[0]; let mut subbuf = vec![0u8; size as usize * 4 - 2]; diff --git a/src/character/mod.rs b/src/character/mod.rs index 43fca43..d8668f3 100644 --- a/src/character/mod.rs +++ b/src/character/mod.rs @@ -1,3 +1,4 @@ +#![allow(clippy::module_inception)] pub mod settings; pub mod character; pub mod guildcard; diff --git a/src/crypto/bb.rs b/src/crypto/bb.rs index de7c4b8..3b2c4df 100644 --- a/src/crypto/bb.rs +++ b/src/crypto/bb.rs @@ -48,7 +48,7 @@ impl PSOBBCipher { for k in cipher.p_array.iter_mut() { let mut pt = *k as u16; pt = ((pt & 0x00FF) << 8) + ((pt & 0xFF00) >> 8); - *k = ((((*k >> 16) ^ pt as u32) << 16)) + pt as u32; + *k = (((*k >> 16) ^ pt as u32) << 16) + pt as u32; } for i in 0..18 { @@ -85,7 +85,7 @@ impl PSOBBCipher { } impl PSOCipher for PSOBBCipher { - fn encrypt(&mut self, data: &Vec) -> Result, CipherError> { + fn encrypt(&mut self, data: &[u8]) -> Result, CipherError> { let mut real_data = data.chunks(4).map(|k| { u32::from_le_bytes([k[0], k[1], k[2], k[3]]) }).collect::>(); @@ -106,9 +106,7 @@ impl PSOCipher for PSOBBCipher { l ^= self.p_array[4]; r ^= self.p_array[5]; - let tmp = l; - l = r; - r = tmp; + std::mem::swap(&mut l, &mut r); result.extend_from_slice(&l.to_le_bytes()); result.extend_from_slice(&r.to_le_bytes()); @@ -117,7 +115,7 @@ impl PSOCipher for PSOBBCipher { Ok(result) } - fn decrypt(&mut self, data: &Vec) -> Result, CipherError> { + fn decrypt(&mut self, data: &[u8]) -> Result, CipherError> { if data.len() % 8 != 0 { return Err(CipherError::InvalidSize); } @@ -139,9 +137,8 @@ impl PSOCipher for PSOBBCipher { l ^= self.p_array[1]; r ^= self.p_array[0]; - let tmp = l; - l = r; - r = tmp; + + std::mem::swap(&mut l, &mut r); result.extend_from_slice(&l.to_le_bytes()); result.extend_from_slice(&r.to_le_bytes()); diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index ea3271e..dd4429a 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -10,8 +10,8 @@ pub enum CipherError { pub trait PSOCipher { - fn encrypt(&mut self, data: &Vec) -> Result, CipherError>; - fn decrypt(&mut self, data: &Vec) -> Result, CipherError>; + fn encrypt(&mut self, data: &[u8]) -> Result, CipherError>; + fn decrypt(&mut self, data: &[u8]) -> Result, CipherError>; fn header_size(&self) -> usize; fn block_size(&self) -> usize { self.header_size() @@ -24,12 +24,12 @@ pub struct NullCipher { } impl PSOCipher for NullCipher { - fn encrypt(&mut self, data: &Vec) -> Result, CipherError> { - Ok(data.clone()) + fn encrypt(&mut self, data: &[u8]) -> Result, CipherError> { + Ok(data.to_vec()) } - fn decrypt(&mut self, data: &Vec) -> Result, CipherError> { - Ok(data.clone()) + fn decrypt(&mut self, data: &[u8]) -> Result, CipherError> { + Ok(data.to_vec()) } fn header_size(&self) -> usize { diff --git a/src/crypto/pc.rs b/src/crypto/pc.rs index 6df6b09..e481fc9 100644 --- a/src/crypto/pc.rs +++ b/src/crypto/pc.rs @@ -31,15 +31,15 @@ impl PSOPCCipher { eax = edi; var1 = eax / W(55); edx = eax - (var1 * W(55)); - ebx = ebx - esi; - edi = edi + W(0x15); + ebx -= esi; + edi += W(0x15); stream[edx.0 as usize] = esi.0; esi = ebx; ebx = W(stream[edx.0 as usize]); } let mut cipher = PSOPCCipher { - stream: stream, + stream, offset: 1, }; @@ -63,7 +63,7 @@ impl PSOPCCipher { while edx > W(0) { esi = W(self.stream[eax.0 as usize + 0x1F]); ebp = W(self.stream[eax.0 as usize]); - ebp = ebp - esi; + ebp -= esi; self.stream[eax.0 as usize] = ebp.0; eax += W(1); edx -= W(1); @@ -74,7 +74,7 @@ impl PSOPCCipher { while edx > W(0) { esi = W(self.stream[eax.0 as usize - 0x18]); ebp = W(self.stream[eax.0 as usize]); - ebp = ebp - esi; + ebp -= esi; self.stream[eax.0 as usize] = ebp.0; eax += W(1); edx -= W(1); @@ -94,7 +94,7 @@ impl PSOPCCipher { } impl PSOCipher for PSOPCCipher { - fn encrypt(&mut self, data: &Vec) -> Result, CipherError> { + fn encrypt(&mut self, data: &[u8]) -> Result, CipherError> { let mut result = Vec::new(); if data.len() % 4 != 0 { return Err(CipherError::InvalidSize) @@ -108,7 +108,7 @@ impl PSOCipher for PSOPCCipher { Ok(result) } - fn decrypt(&mut self, data: &Vec) -> Result, CipherError> { + fn decrypt(&mut self, data: &[u8]) -> Result, CipherError> { self.encrypt(data) } diff --git a/src/lib.rs b/src/lib.rs index b59322f..cf60c15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ pub mod crypto; pub mod packet; pub mod character; pub mod util; -pub mod item; +//pub mod item; use std::io::{Read, Seek}; #[derive(Debug, PartialEq)] @@ -119,7 +119,7 @@ impl PSOPacketData for ConsumingBlob { let mut blob: Vec = Vec::new(); cursor.read_to_end(&mut blob).map_err(|_| PacketParseError::ReadError)?; Ok(ConsumingBlob { - blob: blob, + blob, }) } fn as_bytes(&self) -> Vec { diff --git a/src/packet/login.rs b/src/packet/login.rs index c32d3aa..ff124f7 100644 --- a/src/packet/login.rs +++ b/src/packet/login.rs @@ -24,9 +24,9 @@ impl LoginWelcome { let mut copyright = [0u8; 0x60]; copyright[..0x4B].clone_from_slice(b"Phantasy Star Online Blue Burst Game Server. Copyright 1999-2004 SONICTEAM."); LoginWelcome { - copyright: copyright, - server_key: server_key, - client_key: client_key, + copyright, + server_key, + client_key, } } } @@ -76,8 +76,8 @@ pub struct Session { pub character_slot: u8, // 1..=4 } -impl Session { - pub fn new() -> Session { +impl Default for Session { + fn default() -> Session { Session { version: [0; 30], session_id: 0, @@ -180,12 +180,12 @@ pub struct LoginResponse { impl LoginResponse { pub fn by_status(status: AccountStatus, session: Session) -> LoginResponse { LoginResponse { - status: status, + status, tag: 0x00010000, //tag: 0x00000100, guildcard: 0, team_id: 0, - session: session, + session, caps: 0x00000102, } } @@ -194,9 +194,9 @@ impl LoginResponse { status: AccountStatus::Ok, tag: 0x00010000, //tag: 0x00000100, - guildcard: guildcard, - team_id: team_id, - session: session, + guildcard, + team_id, + session, caps: 0x00000102, } } @@ -229,10 +229,10 @@ impl SendKeyAndTeamSettings { pub fn new(keyboard_config: [u8; 0x16C], gamepad_config: [u8; 0x38], guildcard: u32, team_id: u32) -> SendKeyAndTeamSettings { SendKeyAndTeamSettings { unknown: [0; 0x114], - keyboard_config: keyboard_config, - gamepad_config: gamepad_config, - guildcard: guildcard, - team_id: team_id, + keyboard_config, + gamepad_config, + guildcard, + team_id, //team_info: [0; 2], team_info: [0; 8], team_priv: 0, @@ -255,8 +255,8 @@ pub struct RedirectClient { impl RedirectClient { pub fn new(ip: u32, port: u16) -> RedirectClient { RedirectClient { - ip: ip, - port: port, + ip, + port, padding: 0, } } @@ -276,7 +276,7 @@ pub struct ChecksumAck { impl ChecksumAck { pub fn new(ack: u32) -> ChecksumAck { ChecksumAck { - ack: ack, + ack, } } } @@ -315,7 +315,7 @@ impl GuildcardDataHeader { GuildcardDataHeader { one: 1, len: len as u32, - checksum: checksum + checksum, } } } @@ -331,7 +331,6 @@ pub struct GuildcardDataChunk { _unknown: u32, chunk: u32, pub buffer: [u8; GUILD_CARD_CHUNK_SIZE], - len: usize, } @@ -339,9 +338,9 @@ impl GuildcardDataChunk { pub fn new(chunk: u32, buffer: [u8; GUILD_CARD_CHUNK_SIZE], len: usize) -> GuildcardDataChunk { GuildcardDataChunk { _unknown: 0, - chunk: chunk as u32, - buffer: buffer, - len: len, + chunk, + buffer, + len, } } } @@ -372,12 +371,12 @@ impl PSOPacket for GuildcardDataChunk { impl std::fmt::Debug for GuildcardDataChunk { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "packet GuildcardDataChunk {{\n").unwrap(); - write!(f, " flag: {:?}\n", 0).unwrap(); - write!(f, " _unknown: {:#X?}\n", self._unknown).unwrap(); - write!(f, " chunk: {:#X?}\n", self.chunk).unwrap(); - write!(f, " buffer: [0..{:#X}]\n", self.len).unwrap(); - write!(f, "}}") + writeln!(f, "packet GuildcardDataChunk {{").unwrap(); + writeln!(f, " flag: {:?}", 0).unwrap(); + writeln!(f, " _unknown: {:#X?}", self._unknown).unwrap(); + writeln!(f, " chunk: {:#X?}", self.chunk).unwrap(); + writeln!(f, " buffer: [0..{:#X}]", self.len).unwrap(); + writeln!(f, "}}") } } @@ -429,9 +428,9 @@ impl PSOPacket for ParamDataHeader { impl std::fmt::Debug for ParamDataHeader { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "packet ParamDataHeader{{\n").unwrap(); - write!(f, " files: [..]\n").unwrap(); - write!(f, "}}") + writeln!(f, "packet ParamDataHeader{{").unwrap(); + writeln!(f, " files: [..]").unwrap(); + writeln!(f, "}}") } } @@ -495,7 +494,7 @@ impl ShipList { flags: 0, name: utf8_to_utf16_array("Ship"), }, - ships: ships, + ships, } } } @@ -518,7 +517,7 @@ mod tests { tag: 0, guildcard: 0, team_id: 0, - session: Session::new(), + session: Session::default(), caps: 0, }; @@ -561,7 +560,7 @@ mod tests { #[test] fn test_session_size() { use super::PSOPacketData; - let session = super::Session::new(); + let session = super::Session::default(); assert!(session.as_bytes().len() == 40); } } diff --git a/src/packet/messages.rs b/src/packet/messages.rs index 1b94b19..560e630 100644 --- a/src/packet/messages.rs +++ b/src/packet/messages.rs @@ -764,21 +764,21 @@ impl PSOPacketData for TradeRequestCommand { 0, 0, 0, 0, 0, 0] .into_iter() - .chain(meseta.to_le_bytes().to_vec().into_iter()) + .chain(meseta.to_le_bytes().iter().copied()) .collect() }, TradeRequestCommand::AddItem(item_id, amount) => { vec![1u8, 0, 0, 0] .into_iter() - .chain(item_id.to_le_bytes().to_vec().into_iter()) - .chain(amount.to_le_bytes().to_vec().into_iter()) + .chain(item_id.to_le_bytes().iter().copied()) + .chain(amount.to_le_bytes().iter().copied()) .collect() }, TradeRequestCommand::RemoveItem(item_id, amount) => { vec![2u8, 0, 0, 0] .into_iter() - .chain(item_id.to_le_bytes().to_vec().into_iter()) - .chain(amount.to_le_bytes().to_vec().into_iter()) + .chain(item_id.to_le_bytes().iter().copied()) + .chain(amount.to_le_bytes().iter().copied()) .collect() }, TradeRequestCommand::Confirm => { @@ -1178,7 +1178,7 @@ pub enum GameMessage { PlayerTelepipe(PlayerTelepipe), NpcSpawn(NpcSpawn), ActivateBossWarp(ActivateBossWarp), - PlayerJoiningGame(PlayerJoiningGame), + PlayerJoiningGame(Box), PlayerJoiningGame2(PlayerJoiningGame2), BurstDone(BurstDone), WordSelect(WordSelect), @@ -1346,7 +1346,7 @@ impl PSOPacketData for GameMessage { PlayerTelepipe::CMD => Ok(GameMessage::PlayerTelepipe(PlayerTelepipe::from_bytes(&mut cur)?)), NpcSpawn::CMD => Ok(GameMessage::NpcSpawn(NpcSpawn::from_bytes(&mut cur)?)), ActivateBossWarp::CMD => Ok(GameMessage::ActivateBossWarp(ActivateBossWarp::from_bytes(&mut cur)?)), - PlayerJoiningGame::CMD => Ok(GameMessage::PlayerJoiningGame(PlayerJoiningGame::from_bytes(&mut cur)?)), + PlayerJoiningGame::CMD => Ok(GameMessage::PlayerJoiningGame(Box::new(PlayerJoiningGame::from_bytes(&mut cur)?))), PlayerJoiningGame2::CMD => Ok(GameMessage::PlayerJoiningGame2(PlayerJoiningGame2::from_bytes(&mut cur)?)), BurstDone::CMD => Ok(GameMessage::BurstDone(BurstDone::from_bytes(&mut cur)?)), WordSelect::CMD => Ok(GameMessage::WordSelect(WordSelect::from_bytes(&mut cur)?)), @@ -1427,7 +1427,7 @@ impl PSOPacketData for GameMessage { _ => Err(PacketParseError::UnknownMessage(byte[0], { let mut b = vec![0; len[0] as usize * 4]; - cur.read(&mut b).unwrap(); + cur.read_exact(&mut b).unwrap(); b.to_vec() } )), diff --git a/src/packet/patch.rs b/src/packet/patch.rs index 4339d34..596f533 100644 --- a/src/packet/patch.rs +++ b/src/packet/patch.rs @@ -19,10 +19,10 @@ pub struct PatchWelcome { impl PatchWelcome { pub fn new(server_key: u32, client_key: u32) -> PatchWelcome { PatchWelcome { - copyright: b"Patch Server. Copyright SonicTeam, LTD. 2001".clone(), + copyright: *b"Patch Server. Copyright SonicTeam, LTD. 2001", padding: [0; 20], - server_key: server_key, - client_key: client_key, + server_key, + client_key, } } } @@ -58,8 +58,8 @@ impl StartFileSend { *dst = *src } StartFileSend { - id: id, - size: size, + id, + size, filename: f, } } @@ -106,31 +106,22 @@ impl PSOPacket for FileSend { impl std::fmt::Debug for FileSend { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "packet FileSend {{\n").unwrap(); - write!(f, " chunk_num: {:?}\n", self.chunk_num).unwrap(); - write!(f, " checksum: {:X?}\n", self.checksum).unwrap(); - write!(f, " chunk_size: {:X?}\n", self.chunk_size).unwrap(); - write!(f, " buffer: [...a large array ...]\n").unwrap(); - write!(f, "}}") + writeln!(f, "packet FileSend {{").unwrap(); + writeln!(f, " chunk_num: {:?}", self.chunk_num).unwrap(); + writeln!(f, " checksum: {:X?}", self.checksum).unwrap(); + writeln!(f, " chunk_size: {:X?}", self.chunk_size).unwrap(); + writeln!(f, " buffer: [...a large array ...]").unwrap(); + writeln!(f, "}}") } } +#[derive(Default)] #[pso_packet(0x08, no_flag)] pub struct EndFileSend { padding: u32, } -impl EndFileSend { - pub fn new() -> EndFileSend { - EndFileSend { - padding: 0, - } - } -} - - - #[pso_packet(0x0B, no_flag)] pub struct PatchStartList { } @@ -169,7 +160,7 @@ impl FileInfo { *dst = *src }; FileInfo { - id: id, + id, filename: f, } } @@ -200,8 +191,8 @@ pub struct FilesToPatchMetadata { impl FilesToPatchMetadata { pub fn new(data_size: u32, file_count: u32) -> FilesToPatchMetadata { FilesToPatchMetadata { - data_size: data_size, - file_count: file_count, + data_size, + file_count, } } } @@ -221,7 +212,7 @@ impl Message { pub fn new(mut msg: String) -> Message { msg.push('\0'); Message { - msg: msg, + msg, } } } @@ -237,8 +228,8 @@ pub struct RedirectClient { impl RedirectClient { pub fn new(ip: u32, port: u16) -> RedirectClient { RedirectClient { - ip: ip, - port: port, + ip, + port, padding: 0, } } diff --git a/src/packet/ship.rs b/src/packet/ship.rs index 1b2d579..aa93e77 100644 --- a/src/packet/ship.rs +++ b/src/packet/ship.rs @@ -26,9 +26,9 @@ impl ShipWelcome { let mut copyright = [0u8; 0x60]; copyright[..0x4B].clone_from_slice(b"Phantasy Star Online Blue Burst Game Server. Copyright 1999-2004 SONICTEAM."); ShipWelcome { - copyright: copyright, - server_key: server_key, - client_key: client_key, + copyright, + server_key, + client_key, } } } @@ -149,8 +149,8 @@ pub struct BurstDone72 { target: u8, } -impl BurstDone72 { - pub fn new() -> BurstDone72 { +impl Default for BurstDone72 { + fn default() -> BurstDone72 { BurstDone72 { msg: 0x72, len: 3, @@ -169,7 +169,7 @@ pub struct Message { impl Message { pub fn new(msg: GameMessage) -> Message { Message { - msg: msg, + msg, } } } @@ -184,7 +184,7 @@ impl DirectMessage { pub fn new(target: u32, msg: GameMessage) -> DirectMessage { DirectMessage { flag: target, - msg: msg, + msg, } } } @@ -280,7 +280,7 @@ impl SmallDialog { } SmallDialog { padding: [0; 0x02], - msg: msg, + msg, } } } @@ -299,7 +299,7 @@ impl RightText { } RightText { padding: [0; 0x02], - msg: msg, + msg, } } } @@ -317,7 +317,7 @@ impl SmallLeftDialog { } SmallLeftDialog { padding: [0x00004500, 0x45004500], - msg: msg, + msg, } } } @@ -335,7 +335,7 @@ impl LargeDialog { } LargeDialog { padding: [0, 0x45000000], - msg: msg, + msg, } } } @@ -383,8 +383,8 @@ pub struct LeaveLobby { impl LeaveLobby { pub fn new(client: u8, leader: u8) -> LeaveLobby { LeaveLobby { - client: client, - leader: leader, + client, + leader, _padding: 0, } } @@ -400,8 +400,8 @@ pub struct LeaveRoom { impl LeaveRoom { pub fn new(client: u8, leader: u8) -> LeaveRoom { LeaveRoom { - client: client, - leader: leader, + client, + leader, _padding: 0, } } @@ -418,8 +418,8 @@ impl PlayerChat { pub fn new(guildcard: u32, message: String) -> PlayerChat { PlayerChat { unknown: 0x00010000, - guildcard: guildcard, - message: message, + guildcard, + message, } } } @@ -501,7 +501,7 @@ pub struct LobbyEntry { impl LobbyEntry { pub fn new(menu_id: u32, lobby_id: u32) -> LobbyEntry { LobbyEntry { - menu_id: menu_id, + menu_id, item_id: lobby_id, padding: 0, } @@ -514,8 +514,8 @@ pub struct LobbyList { entries: [LobbyEntry; 16], } -impl LobbyList { - pub fn new() -> LobbyList { +impl Default for LobbyList { + fn default() -> LobbyList { let lobbies = (0..16).fold([LobbyEntry::default(); 16], |mut acc, index| { acc[index].menu_id = LOBBY_MENU_ID; diff --git a/src/util.rs b/src/util.rs index 0b6291c..99bb62d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,7 +2,7 @@ pub fn array_to_utf8(array: [u8; X]) -> Result { String::from_utf8(array.to_vec()) .map(|mut s| { - if let Some(index) = s.find("\u{0}") { + if let Some(index) = s.find('\u{0}') { s.truncate(index); } s @@ -12,7 +12,7 @@ pub fn array_to_utf8(array: [u8; X]) -> Result String { unsafe { let (_, data, _) = array.align_to(); - String::from_utf16_lossy(&data).trim_matches(char::from(0)).into() + String::from_utf16_lossy(data).trim_matches(char::from(0)).into() } } @@ -20,7 +20,7 @@ pub fn utf8_to_array(s: impl Into) -> [u8; N] { let mut array = [0u8; N]; let s = s.into(); let bytes = s.as_bytes(); - array[..bytes.len()].clone_from_slice(&bytes); + array[..bytes.len()].clone_from_slice(bytes); array }