Browse Source

convert macros into functions

somecleaning
jake 6 months ago
parent
commit
9ff866c489
  1. 5
      src/packet/login.rs
  2. 6
      src/packet/ship.rs
  3. 46
      src/util.rs

5
src/packet/login.rs

@ -1,7 +1,8 @@
use chrono::{DateTime, Utc};
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;
@ -492,7 +493,7 @@ impl ShipList {
menu: ships.get(0).map(|s| s.menu).unwrap_or(0),
item: 0,
flags: 0,
name: utf8_to_utf16_array!("Ship", 0x11),
name: utf8_to_utf16_array("Ship"),
},
ships: ships,
}

6
src/packet/ship.rs

@ -1,6 +1,6 @@
use psopacket::{pso_packet, 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 character::character::FullCharacter;
use crate::character::character as character;
@ -82,13 +82,13 @@ impl ShipBlockList {
menu: BLOCK_MENU_ID,
item: 0,
flags: 0,
name: utf8_to_utf16_array!(shipname, 0x11)
name: utf8_to_utf16_array(shipname)
},
blocks: (0..num_blocks).map(|i| BlockEntry {
menu: BLOCK_MENU_ID,
item: i as u32 + 1,
flags: 0,
name: utf8_to_utf16_array!(format!("Block {}", i+1), 0x11)
name: utf8_to_utf16_array(format!("Block {}", i+1))
}).collect()
}
}

46
src/util.rs

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