1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-04-20 06:37:45 +00:00

Use bool in "check if name is free" IPC instead of u8

This doesn't change anything functionally, just works a bit nicer.
This commit is contained in:
Joshua Goins 2025-04-11 08:41:50 -04:00
parent 395aacf53e
commit b3b816c511
4 changed files with 29 additions and 7 deletions

View file

@ -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
{

View file

@ -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::<u8>)]
#[bw(map = write_bool_as::<u8>)]
free: bool,
},
#[br(pre_assert(*magic == CustomIpcType::RequestCharacterList))]
RequestCharacterList { service_account_id: u32 },
#[br(pre_assert(*magic == CustomIpcType::RequestCharacterListRepsonse))]

View file

@ -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<T: std::convert::From<u8> + std::cmp::PartialEq>(x: T) -> bool {
x == T::from(1u8)
}
pub(crate) fn write_bool_as<T: std::convert::From<u8>>(x: &bool) -> T {
if *x { T::from(1u8) } else { T::from(0u8) }
}
pub(crate) fn read_string(byte_stream: Vec<u8>) -> 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::<u8>(DATA[0]));
assert!(read_bool_from::<u8>(DATA[1]));
}
#[test]
fn write_bool_u8() {
assert_eq!(write_bool_as::<u8>(&false), DATA[0]);
assert_eq!(write_bool_as::<u8>(&true), DATA[1]);
}
// "FOO\0"
const STRING_DATA: [u8; 4] = [0x46u8, 0x4Fu8, 0x4Fu8, 0x0u8];

View file

@ -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 {