diff --git a/src/common/gamedata.rs b/src/common/gamedata.rs index fbd7a25..4cfa430 100644 --- a/src/common/gamedata.rs +++ b/src/common/gamedata.rs @@ -58,7 +58,7 @@ impl GameData { let sheet = WorldSheet::read_from(&mut self.game_data, Language::None)?; let row = sheet.get_row(world_id as u32)?; - row.Name().into_string().map(|x| x.clone()) + row.Name().into_string().cloned() } /// Gets the starting city-state from a given class/job id. @@ -66,7 +66,7 @@ impl GameData { let sheet = ClassJobSheet::read_from(&mut self.game_data, Language::English)?; let row = sheet.get_row(classjob_id as u32)?; - row.StartingTown().into_u8().map(|x| *x) + row.StartingTown().into_u8().copied() } pub fn get_racial_base_attributes(&mut self, tribe_id: u8) -> Option { @@ -127,11 +127,7 @@ impl GameData { } // Retrieves a zone's internal name, place name or parent region name. - pub fn get_territory_name( - &mut self, - zone_id: u32, - which: TerritoryNameKind, - ) -> Option { + pub fn get_territory_name(&mut self, zone_id: u32, which: TerritoryNameKind) -> Option { let sheet = TerritoryTypeSheet::read_from(&mut self.game_data, Language::None)?; let row = sheet.get_row(zone_id)?; @@ -269,7 +265,7 @@ impl GameData { .Weather() .iter() .cloned() - .zip(row.Rate().clone()) + .zip(row.Rate()) .map(|(x, y)| (*x.into_i32().unwrap(), *y.into_u8().unwrap() as i32)) .collect(); diff --git a/src/inventory/generic.rs b/src/inventory/generic.rs index c226c7b..531f8e8 100644 --- a/src/inventory/generic.rs +++ b/src/inventory/generic.rs @@ -2,19 +2,11 @@ use serde::{Deserialize, Serialize}; use super::{Item, Storage}; -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Default, Deserialize, Serialize)] pub struct GenericStorage { pub slots: Vec, } -impl GenericStorage { - pub fn default() -> Self { - Self { - slots: vec![Item::default(); N], - } - } -} - impl Storage for GenericStorage { fn max_slots(&self) -> u32 { N as u32 diff --git a/src/packet/parsing.rs b/src/packet/parsing.rs index 5ff4aa1..ce9275f 100644 --- a/src/packet/parsing.rs +++ b/src/packet/parsing.rs @@ -46,12 +46,12 @@ pub enum SegmentType { } #[binrw] -#[brw(import(kind: &SegmentType, size: u32, encryption_key: Option<&[u8]>))] +#[brw(import(kind: SegmentType, size: u32, encryption_key: Option<&[u8]>))] #[derive(Debug, Clone)] pub enum SegmentData { - #[br(pre_assert(*kind == SegmentType::None))] + #[br(pre_assert(kind == SegmentType::None))] None(), - #[br(pre_assert(*kind == SegmentType::Setup))] + #[br(pre_assert(kind == SegmentType::Setup))] Setup { #[brw(pad_before = 4)] // empty #[brw(pad_size_to = 36)] @@ -60,13 +60,13 @@ pub enum SegmentData { #[bw(map = write_string)] ticket: String, // square enix in their infinite wisdom has this as a STRING REPRESENTATION of an integer. what }, - #[br(pre_assert(*kind == SegmentType::Initialize))] + #[br(pre_assert(kind == SegmentType::Initialize))] Initialize { player_id: u32, #[brw(pad_after = 32)] timestamp: u32, }, - #[br(pre_assert(*kind == SegmentType::SecuritySetup))] + #[br(pre_assert(kind == SegmentType::SecuritySetup))] SecuritySetup { #[brw(pad_before = 36)] // empty #[brw(pad_size_to = 32)] @@ -79,24 +79,24 @@ pub enum SegmentData { #[brw(pad_after = 512)] // empty key: [u8; 4], }, - #[br(pre_assert(*kind == SegmentType::Ipc))] + #[br(pre_assert(kind == SegmentType::Ipc))] Ipc { #[br(parse_with = decrypt, args(size, encryption_key))] #[bw(write_with = encrypt, args(size, encryption_key))] data: T, }, - #[br(pre_assert(*kind == SegmentType::KeepAliveRequest))] + #[br(pre_assert(kind == SegmentType::KeepAliveRequest))] KeepAliveRequest { id: u32, timestamp: u32 }, - #[br(pre_assert(*kind == SegmentType::SecurityInitialize))] + #[br(pre_assert(kind == SegmentType::SecurityInitialize))] SecurityInitialize { #[br(count = 0x280)] #[brw(pad_size_to = 640)] data: Vec, }, - #[br(pre_assert(*kind == SegmentType::KeepAliveResponse))] + #[br(pre_assert(kind == SegmentType::KeepAliveResponse))] KeepAliveResponse { id: u32, timestamp: u32 }, - #[br(pre_assert(*kind == SegmentType::KawariIpc))] + #[br(pre_assert(kind == SegmentType::KawariIpc))] KawariIpc { data: CustomIpcSegment }, } @@ -131,7 +131,8 @@ pub struct PacketSegment { pub target_actor: u32, #[brw(pad_after = 2)] // padding pub segment_type: SegmentType, - #[brw(args(&segment_type, size, encryption_key))] + #[bw(args(*segment_type, size, encryption_key))] + #[br(args(segment_type, size, encryption_key))] #[br(err_context("segment size = {}", size))] pub data: SegmentData, } diff --git a/src/world/database.rs b/src/world/database.rs index 6f1fe83..44bf7a5 100644 --- a/src/world/database.rs +++ b/src/world/database.rs @@ -51,11 +51,9 @@ impl WorldDatabase { connection.execute(query, ()).unwrap(); } - let this = Self { + Self { connection: Mutex::new(connection), - }; - - this + } } pub fn import_character(&self, service_account_id: u32, path: &str) { diff --git a/src/world/server.rs b/src/world/server.rs index 146f03a..54b7540 100644 --- a/src/world/server.rs +++ b/src/world/server.rs @@ -93,17 +93,14 @@ impl WorldServer { /// Finds the instance associated with a zone, or creates it if it doesn't exist yet fn find_instance_mut(&mut self, zone_id: u16) -> &mut Instance { - self.instances.entry(zone_id).or_insert(Instance::default()) + self.instances.entry(zone_id).or_default() } /// Finds the instance associated with an actor, or returns None if they are not found. fn find_actor_instance_mut(&mut self, actor_id: u32) -> Option<&mut Instance> { - for instance in self.instances.values_mut() { - if instance.actors.contains_key(&ObjectId(actor_id)) { - return Some(instance); - } - } - None + self.instances + .values_mut() + .find(|instance| instance.actors.contains_key(&ObjectId(actor_id))) } /// Tell all the clients that a new NPC spawned. @@ -139,9 +136,9 @@ pub async fn server_main_loop(mut recv: Receiver) -> Result<(), std::i let mut data = data.lock().unwrap(); // create a new instance if necessary - if !data.instances.contains_key(&zone_id) { - data.instances.insert(zone_id, Instance::default()); - } + data.instances + .entry(zone_id) + .or_insert_with(Instance::default); // Send existing player data, if any if let Some(instance) = data.find_instance(zone_id).cloned() { @@ -571,7 +568,7 @@ pub async fn server_main_loop(mut recv: Receiver) -> Result<(), std::i if cast_time == 0 { // If instantaneous, send right back - send_execution(from_id.clone(), data.clone()); + send_execution(from_id, data.clone()); } else { // Otherwise, delay // NOTE: I know this won't scale, but it's a fine hack for now @@ -582,8 +579,6 @@ pub async fn server_main_loop(mut recv: Receiver) -> Result<(), std::i ); // we have to shadow these variables to tell rust not to move them into the async closure - let cast_time = cast_time.clone(); - let from_id = from_id.clone(); let data = data.clone(); tokio::task::spawn(async move { let mut interval =