Browse Source

turns out not everything has a flag, putting it in the struct will be easier at this point

pull/3/head
Jake Probst 6 years ago
parent
commit
7cac700dd5
  1. 30
      psopacket/src/lib.rs
  2. 16
      src/patch/packet.rs

30
psopacket/src/lib.rs

@ -10,10 +10,9 @@ use quote::quote;
pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
let arg = parse_macro_input!(attr as syn::LitInt);
let pkt_cmd = arg.value() as u16;
let parsed = parse_macro_input!(item as ItemStruct);
let mut has_flag: bool = false;
let mut from_bytes = Vec::new();
let mut as_bytes = Vec::new();
let mut dbg_write_vars = Vec::new();
@ -139,13 +138,6 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
});
},
"u32" => {
if ident_str == "flag" {
if i != 0 {
return syn::Error::new(ident.span(), "flag must be first member of struct").to_compile_error().into();
}
has_flag = true;
continue;
}
from_bytes.push(quote! {
#ident: {
let mut b: [u8; 4] = [0; 4];
@ -181,17 +173,6 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
let this_struct = parsed.ident.clone();
let this_struct_str = this_struct.to_string();
let flag_write = if has_flag {
quote! {
buf.extend_from_slice(&u32::to_le_bytes(self.flag));
}
}
else {
quote! {
buf.extend_from_slice(&u32::to_le_bytes(0));
}
};
let psopacket = quote! {
impl PSOPacket for #this_struct {
fn from_bytes(data: &Vec<u8>) -> Result<#this_struct, PacketParseError> {
@ -205,22 +186,15 @@ pub fn pso_packet(attr: TokenStream, item: TokenStream) -> TokenStream {
return Err(PacketParseError::WrongPacketCommand);
}
if #has_flag {
cur.seek(SeekFrom::Start(4)).unwrap();
}
else {
cur.seek(SeekFrom::Start(8)).unwrap();
}
Ok(#this_struct {
#(#from_bytes)*
})
}
fn as_bytes(&self) -> Vec<u8> {
let mut buf: Vec<u8> = Vec::new();
#flag_write
#(#as_bytes)*
let pkt_len = buf.len() as u16;
let pkt_len = (buf.len() + 4) as u16;
let mut prebuf: Vec<u8> = Vec::new();
prebuf.extend_from_slice(&u16::to_le_bytes(pkt_len));

16
src/patch/packet.rs

@ -6,6 +6,7 @@ use std::io::{Read, Seek, SeekFrom};
#[allow(non_camel_case_types)]
type u8_str = u8;
// outgoing packets
#[pso_packet(0x02)]
pub struct PatchWelcome {
copyright: [u8_str; 44],
@ -15,7 +16,7 @@ pub struct PatchWelcome {
}
impl PatchWelcome {
fn new(server_key: u32, client_key: u32) -> PatchWelcome {
pub fn new(server_key: u32, client_key: u32) -> PatchWelcome {
PatchWelcome {
copyright: b"Patch Server. Copyright SonicTeam, LTD. 2001".clone(),
padding: [0; 20],
@ -25,9 +26,14 @@ impl PatchWelcome {
}
}
pub enum PatchPacket {
PatchWelcome(PatchWelcome),
// incoming packets
#[pso_packet(0x02)]
pub struct PatchWelcomeReply {
}
#[cfg(test)]
@ -38,7 +44,7 @@ mod tests {
let pkt = super::PatchWelcome::new(123, 456);
assert!(pkt.as_bytes() == vec![0x4C, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x61, 0x74, 0x63, 0x68, 0x20, 0x53, 0x65,
assert!(pkt.as_bytes() == vec![0x4C, 0x00, 0x02, 0x00, 0x50, 0x61, 0x74, 0x63, 0x68, 0x20, 0x53, 0x65,
0x72, 0x76, 0x65, 0x72, 0x2E, 0x20, 0x43, 0x6F, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20,
0x53, 0x6F, 0x6E, 0x69, 0x63, 0x54, 0x65, 0x61, 0x6D, 0x2C, 0x20, 0x4C, 0x54, 0x44, 0x2E, 0x20,
0x32, 0x30, 0x30, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -47,7 +53,7 @@ mod tests {
let mut bytes = pkt.as_bytes();
bytes.splice(32..41, b"Elsewhere".iter().cloned());
bytes.splice(28..37, b"Elsewhere".iter().cloned());
let new_pkt = super::PatchWelcome::from_bytes(&bytes);

Loading…
Cancel
Save