Browse Source

how has libpso avoided the baleful eye of clippy

somecleaning
jake 6 months ago
parent
commit
edb6319189
  1. 10
      psopacket/src/lib.rs
  2. 1
      src/character/mod.rs
  3. 15
      src/crypto/bb.rs
  4. 12
      src/crypto/mod.rs
  5. 14
      src/crypto/pc.rs
  6. 4
      src/lib.rs
  7. 67
      src/packet/login.rs
  8. 16
      src/packet/messages.rs
  9. 45
      src/packet/patch.rs
  10. 40
      src/packet/ship.rs
  11. 6
      src/util.rs

10
psopacket/src/lib.rs

@ -149,14 +149,14 @@ fn generate_psopacket_impl(pkt_cmd: u16, name: syn::Ident, attrs: &Vec<AttrType>
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<AttrType>
const CMD: u8 = #msg_cmd;
fn from_bytes<R: std::io::Read + std::io::Seek >(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];

1
src/character/mod.rs

@ -1,3 +1,4 @@
#![allow(clippy::module_inception)]
pub mod settings;
pub mod character;
pub mod guildcard;

15
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<u8>) -> Result<Vec<u8>, CipherError> {
fn encrypt(&mut self, data: &[u8]) -> Result<Vec<u8>, CipherError> {
let mut real_data = data.chunks(4).map(|k| {
u32::from_le_bytes([k[0], k[1], k[2], k[3]])
}).collect::<Vec<_>>();
@ -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<u8>) -> Result<Vec<u8>, CipherError> {
fn decrypt(&mut self, data: &[u8]) -> Result<Vec<u8>, 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());

12
src/crypto/mod.rs

@ -10,8 +10,8 @@ pub enum CipherError {
pub trait PSOCipher {
fn encrypt(&mut self, data: &Vec<u8>) -> Result<Vec<u8>, CipherError>;
fn decrypt(&mut self, data: &Vec<u8>) -> Result<Vec<u8>, CipherError>;
fn encrypt(&mut self, data: &[u8]) -> Result<Vec<u8>, CipherError>;
fn decrypt(&mut self, data: &[u8]) -> Result<Vec<u8>, 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<u8>) -> Result<Vec<u8>, CipherError> {
Ok(data.clone())
fn encrypt(&mut self, data: &[u8]) -> Result<Vec<u8>, CipherError> {
Ok(data.to_vec())
}
fn decrypt(&mut self, data: &Vec<u8>) -> Result<Vec<u8>, CipherError> {
Ok(data.clone())
fn decrypt(&mut self, data: &[u8]) -> Result<Vec<u8>, CipherError> {
Ok(data.to_vec())
}
fn header_size(&self) -> usize {

14
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<u8>) -> Result<Vec<u8>, CipherError> {
fn encrypt(&mut self, data: &[u8]) -> Result<Vec<u8>, 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<u8>) -> Result<Vec<u8>, CipherError> {
fn decrypt(&mut self, data: &[u8]) -> Result<Vec<u8>, CipherError> {
self.encrypt(data)
}

4
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<u8> = Vec::new();
cursor.read_to_end(&mut blob).map_err(|_| PacketParseError::ReadError)?;
Ok(ConsumingBlob {
blob: blob,
blob,
})
}
fn as_bytes(&self) -> Vec<u8> {

67
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);
}
}

16
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<PlayerJoiningGame>),
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()
}
)),

45
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,
}
}

40
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;

6
src/util.rs

@ -2,7 +2,7 @@
pub fn array_to_utf8<const X: usize>(array: [u8; X]) -> Result<String, std::string::FromUtf8Error> {
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<const X: usize>(array: [u8; X]) -> Result<String, std::stri
pub fn array_to_utf16(array: &[u8]) -> 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<const N: usize>(s: impl Into<String>) -> [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
}

Loading…
Cancel
Save