diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 0909d37..f979fd5 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -890,7 +890,6 @@ async fn client_loop( } CustomIpcData::CheckNameIsAvailable { name } => { let is_name_free = database.check_is_name_free(name); - let is_name_free = if is_name_free { 1 } else { 0 }; // send response { diff --git a/src/common/custom_ipc.rs b/src/common/custom_ipc.rs index 0b70a13..a407c21 100644 --- a/src/common/custom_ipc.rs +++ b/src/common/custom_ipc.rs @@ -1,7 +1,7 @@ use binrw::binrw; use crate::{ - common::{CHAR_NAME_MAX_LENGTH, read_string}, + common::{CHAR_NAME_MAX_LENGTH, read_bool_from, read_string, write_bool_as}, lobby::ipc::CharacterDetails, packet::{IpcSegment, ReadWriteIpcSegment}, }; @@ -88,7 +88,11 @@ pub enum CustomIpcData { name: String, }, #[br(pre_assert(*magic == CustomIpcType::NameIsAvailableResponse))] - NameIsAvailableResponse { free: u8 }, + NameIsAvailableResponse { + #[br(map = read_bool_from::)] + #[bw(map = write_bool_as::)] + free: bool, + }, #[br(pre_assert(*magic == CustomIpcType::RequestCharacterList))] RequestCharacterList { service_account_id: u32 }, #[br(pre_assert(*magic == CustomIpcType::RequestCharacterListRepsonse))] diff --git a/src/common/mod.rs b/src/common/mod.rs index ea8222b..4e17c78 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -51,6 +51,14 @@ pub const INVALID_OBJECT_ID: ObjectId = ObjectId(0xE0000000); /// Maxmimum length of a character's name. pub const CHAR_NAME_MAX_LENGTH: usize = 32; +pub(crate) fn read_bool_from + std::cmp::PartialEq>(x: T) -> bool { + x == T::from(1u8) +} + +pub(crate) fn write_bool_as>(x: &bool) -> T { + if *x { T::from(1u8) } else { T::from(0u8) } +} + pub(crate) fn read_string(byte_stream: Vec) -> String { let str = String::from_utf8(byte_stream).unwrap(); str.trim_matches(char::from(0)).to_string() // trim \0 from the end of strings @@ -146,6 +154,20 @@ pub struct Attributes { mod tests { use super::*; + const DATA: [u8; 2] = [0u8, 1u8]; + + #[test] + fn read_bool_u8() { + assert!(!read_bool_from::(DATA[0])); + assert!(read_bool_from::(DATA[1])); + } + + #[test] + fn write_bool_u8() { + assert_eq!(write_bool_as::(&false), DATA[0]); + assert_eq!(write_bool_as::(&true), DATA[1]); + } + // "FOO\0" const STRING_DATA: [u8; 4] = [0x46u8, 0x4Fu8, 0x4Fu8, 0x0u8]; diff --git a/src/lobby/connection.rs b/src/lobby/connection.rs index b7b6904..bf18673 100644 --- a/src/lobby/connection.rs +++ b/src/lobby/connection.rs @@ -363,10 +363,7 @@ impl LobbyConnection { tracing::info!("Is name free? {free}"); - // TODO: use read_bool_as - let free: bool = *free == 1u8; - - if free { + if *free { self.stored_character_creation_name = character_action.name.clone(); let ipc = ServerLobbyIpcSegment {