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:
parent
395aacf53e
commit
b3b816c511
4 changed files with 29 additions and 7 deletions
|
@ -890,7 +890,6 @@ async fn client_loop(
|
||||||
}
|
}
|
||||||
CustomIpcData::CheckNameIsAvailable { name } => {
|
CustomIpcData::CheckNameIsAvailable { name } => {
|
||||||
let is_name_free = database.check_is_name_free(name);
|
let is_name_free = database.check_is_name_free(name);
|
||||||
let is_name_free = if is_name_free { 1 } else { 0 };
|
|
||||||
|
|
||||||
// send response
|
// send response
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use binrw::binrw;
|
use binrw::binrw;
|
||||||
|
|
||||||
use crate::{
|
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,
|
lobby::ipc::CharacterDetails,
|
||||||
packet::{IpcSegment, ReadWriteIpcSegment},
|
packet::{IpcSegment, ReadWriteIpcSegment},
|
||||||
};
|
};
|
||||||
|
@ -88,7 +88,11 @@ pub enum CustomIpcData {
|
||||||
name: String,
|
name: String,
|
||||||
},
|
},
|
||||||
#[br(pre_assert(*magic == CustomIpcType::NameIsAvailableResponse))]
|
#[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))]
|
#[br(pre_assert(*magic == CustomIpcType::RequestCharacterList))]
|
||||||
RequestCharacterList { service_account_id: u32 },
|
RequestCharacterList { service_account_id: u32 },
|
||||||
#[br(pre_assert(*magic == CustomIpcType::RequestCharacterListRepsonse))]
|
#[br(pre_assert(*magic == CustomIpcType::RequestCharacterListRepsonse))]
|
||||||
|
|
|
@ -51,6 +51,14 @@ pub const INVALID_OBJECT_ID: ObjectId = ObjectId(0xE0000000);
|
||||||
/// Maxmimum length of a character's name.
|
/// Maxmimum length of a character's name.
|
||||||
pub const CHAR_NAME_MAX_LENGTH: usize = 32;
|
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 {
|
pub(crate) fn read_string(byte_stream: Vec<u8>) -> String {
|
||||||
let str = String::from_utf8(byte_stream).unwrap();
|
let str = String::from_utf8(byte_stream).unwrap();
|
||||||
str.trim_matches(char::from(0)).to_string() // trim \0 from the end of strings
|
str.trim_matches(char::from(0)).to_string() // trim \0 from the end of strings
|
||||||
|
@ -146,6 +154,20 @@ pub struct Attributes {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
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"
|
// "FOO\0"
|
||||||
const STRING_DATA: [u8; 4] = [0x46u8, 0x4Fu8, 0x4Fu8, 0x0u8];
|
const STRING_DATA: [u8; 4] = [0x46u8, 0x4Fu8, 0x4Fu8, 0x0u8];
|
||||||
|
|
||||||
|
|
|
@ -363,10 +363,7 @@ impl LobbyConnection {
|
||||||
|
|
||||||
tracing::info!("Is name free? {free}");
|
tracing::info!("Is name free? {free}");
|
||||||
|
|
||||||
// TODO: use read_bool_as
|
if *free {
|
||||||
let free: bool = *free == 1u8;
|
|
||||||
|
|
||||||
if free {
|
|
||||||
self.stored_character_creation_name = character_action.name.clone();
|
self.stored_character_creation_name = character_action.name.clone();
|
||||||
|
|
||||||
let ipc = ServerLobbyIpcSegment {
|
let ipc = ServerLobbyIpcSegment {
|
||||||
|
|
Loading…
Add table
Reference in a new issue