convert macros into functions
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
jake 2023-11-14 21:37:22 -07:00
parent 22278e04e6
commit 9ff866c489
3 changed files with 24 additions and 33 deletions

View File

@ -1,7 +1,8 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use psopacket::{pso_packet, PSOPacketData}; use psopacket::{pso_packet, PSOPacketData};
use crate::{PSOPacket, PacketParseError, PSOPacketData, utf8_to_utf16_array}; use crate::{PSOPacket, PacketParseError, PSOPacketData};
use crate::util::utf8_to_utf16_array;
use crate::character::character::SelectScreenCharacter; use crate::character::character::SelectScreenCharacter;
@ -492,7 +493,7 @@ impl ShipList {
menu: ships.get(0).map(|s| s.menu).unwrap_or(0), menu: ships.get(0).map(|s| s.menu).unwrap_or(0),
item: 0, item: 0,
flags: 0, flags: 0,
name: utf8_to_utf16_array!("Ship", 0x11), name: utf8_to_utf16_array("Ship"),
}, },
ships: ships, ships: ships,
} }

View File

@ -1,6 +1,6 @@
use psopacket::{pso_packet, PSOPacketData}; use psopacket::{pso_packet, PSOPacketData};
use crate::{PSOPacket, PacketParseError, PSOPacketData}; use crate::{PSOPacket, PacketParseError, PSOPacketData};
use crate::utf8_to_utf16_array; use crate::util::utf8_to_utf16_array;
use crate::packet::messages::GameMessage; use crate::packet::messages::GameMessage;
//use character::character::FullCharacter; //use character::character::FullCharacter;
use crate::character::character as character; use crate::character::character as character;
@ -82,13 +82,13 @@ impl ShipBlockList {
menu: BLOCK_MENU_ID, menu: BLOCK_MENU_ID,
item: 0, item: 0,
flags: 0, flags: 0,
name: utf8_to_utf16_array!(shipname, 0x11) name: utf8_to_utf16_array(shipname)
}, },
blocks: (0..num_blocks).map(|i| BlockEntry { blocks: (0..num_blocks).map(|i| BlockEntry {
menu: BLOCK_MENU_ID, menu: BLOCK_MENU_ID,
item: i as u32 + 1, item: i as u32 + 1,
flags: 0, flags: 0,
name: utf8_to_utf16_array!(format!("Block {}", i+1), 0x11) name: utf8_to_utf16_array(format!("Block {}", i+1))
}).collect() }).collect()
} }
} }

View File

@ -16,31 +16,19 @@ pub fn array_to_utf16(array: &[u8]) -> String {
} }
} }
pub fn utf8_to_array<const N: usize>(s: impl Into<String>) -> [u8; N] {
// TODO: const fn version of this! (helpful with tests) let mut array = [0u8; N];
#[macro_export] let s = s.into();
macro_rules! utf8_to_array { let bytes = s.as_bytes();
($s: expr, $size: expr) => { array[..bytes.len()].clone_from_slice(&bytes);
{ array
let mut array = [0u8; $size];
let bytes = $s.as_bytes();
array[..bytes.len()].clone_from_slice(&bytes);
array
}
}
} }
#[macro_export] pub fn utf8_to_utf16_array<const N: usize>(s: impl Into<String>) -> [u16; N] {
macro_rules! utf8_to_utf16_array { let mut array = [0u16; N];
($s: expr, $size: expr) => { let bytes = s.into().encode_utf16().collect::<Vec<_>>();
{ array[..bytes.len()].clone_from_slice(&bytes);
let mut array = [0u16; $size]; array
//let bytes = $s.as_bytes();
let bytes = $s.encode_utf16().collect::<Vec<_>>();
array[..bytes.len()].clone_from_slice(&bytes);
array
}
}
} }
pub fn vec_to_array<T: Default + Copy, const N: usize>(vec: Vec<T>) -> [T; N] { pub fn vec_to_array<T: Default + Copy, const N: usize>(vec: Vec<T>) -> [T; N] {
@ -53,10 +41,12 @@ pub fn vec_to_array<T: Default + Copy, const N: usize>(vec: Vec<T>) -> [T; N] {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*;
#[test] #[test]
fn test_utf8_to_array() { fn test_utf8_to_array() {
let s = "asdf".to_owned(); let s = "asdf".to_owned();
let a = utf8_to_array!(s, 8); let a = utf8_to_array(s);
let mut e = [0u8; 8]; let mut e = [0u8; 8];
e[..4].clone_from_slice(b"asdf"); e[..4].clone_from_slice(b"asdf");
@ -64,14 +54,14 @@ mod test {
} }
#[test] #[test]
fn utf8_to_utf16_array() { fn test_utf8_to_utf16_array() {
let utf16 = utf8_to_utf16_array!("asdf", 16); let utf16 = utf8_to_utf16_array("asdf");
assert!(utf16 == [97, 115, 100, 102, 0,0,0,0,0,0,0,0,0,0,0,0]) assert!(utf16 == [97, 115, 100, 102, 0,0,0,0,0,0,0,0,0,0,0,0])
} }
#[test] #[test]
fn utf8_to_utf16_array_unicode() { fn test_utf8_to_utf16_array_unicode() {
let utf16 = utf8_to_utf16_array!("あいうえお", 16); let utf16 = utf8_to_utf16_array("あいうえお");
assert!(utf16 == [0x3042 , 0x3044, 0x3046, 0x3048, 0x304A, 0,0,0,0,0,0,0,0,0,0,0]) assert!(utf16 == [0x3042 , 0x3044, 0x3046, 0x3048, 0x304A, 0,0,0,0,0,0,0,0,0,0,0])
} }
} }