|
|
@ -75,9 +75,232 @@ impl PSOPacketData for String { |
|
|
|
|
|
|
|
|
|
|
|
pub trait PSOPacket: std::fmt::Debug {
|
|
|
|
// const CMD: u16;
|
|
|
|
fn from_bytes(data: &[u8]) -> Result<Self, PacketParseError> where Self: Sized;
|
|
|
|
fn as_bytes(&self) -> Vec<u8>;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use psopacket::pso_packet2;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basic_pso_packet() {
|
|
|
|
#[pso_packet2(0x23)]
|
|
|
|
struct Test {
|
|
|
|
a: u32,
|
|
|
|
b: u16,
|
|
|
|
c: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
let test = Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
c: 9999,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = test.as_bytes();
|
|
|
|
assert!(bytes == vec![16, 0, 35, 0, 0, 0, 0, 0, 21, 205, 91, 7, 49, 212, 15, 39]);
|
|
|
|
|
|
|
|
bytes[11] = 17;
|
|
|
|
let test2 = Test::from_bytes(&bytes).unwrap();
|
|
|
|
assert!(test2 == Test {
|
|
|
|
a: 291228949,
|
|
|
|
b: 54321,
|
|
|
|
c: 9999,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_array_in_packet() {
|
|
|
|
#[pso_packet2(0x23)]
|
|
|
|
struct Test {
|
|
|
|
a: u32,
|
|
|
|
b: u16,
|
|
|
|
c: [u16; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
let test = Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
c: [1111, 2222, 3333],
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = test.as_bytes();
|
|
|
|
assert!(bytes == [20, 0, 35, 0, 0, 0, 0, 0, 21, 205, 91, 7, 49, 212, 87, 4, 174, 8, 5, 13]);
|
|
|
|
|
|
|
|
bytes[17] = 17;
|
|
|
|
let test2 = Test::from_bytes(&bytes).unwrap();
|
|
|
|
assert!(test2 == Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
c: [1111, 4526, 3333],
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_type_in_packet() {
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
struct MyType {
|
|
|
|
k: u8,
|
|
|
|
j: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PSOPacketData for MyType {
|
|
|
|
fn from_bytes<R: std::io::Read>(cursor: &mut R) -> Result<MyType, PacketParseError> {
|
|
|
|
let mut kb = [0u8; 1];
|
|
|
|
cursor.read(&mut kb).map_err(|_| PacketParseError::ReadError)?;
|
|
|
|
let mut jb = [0u8; 4];
|
|
|
|
cursor.read(&mut jb).map_err(|_| PacketParseError::ReadError)?;
|
|
|
|
Ok(MyType {
|
|
|
|
k: kb[0],
|
|
|
|
j: u32::from_le_bytes(jb)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fn as_bytes(&self) -> Vec<u8> {
|
|
|
|
let jbytes = u32::to_le_bytes(self.j);
|
|
|
|
vec![self.k, jbytes[0], jbytes[1], jbytes[2], jbytes[3]]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pso_packet2(0x23)]
|
|
|
|
struct Test {
|
|
|
|
a: u32,
|
|
|
|
b: u16,
|
|
|
|
c: MyType,
|
|
|
|
d: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let test = Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
c: MyType {
|
|
|
|
k: 23,
|
|
|
|
j: 999999,
|
|
|
|
},
|
|
|
|
d: 5,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = test.as_bytes();
|
|
|
|
assert!(bytes == [20, 0, 35, 0, 0, 0, 0, 0, 21, 205, 91, 7, 49, 212, 23, 63, 66, 15, 0, 5]);
|
|
|
|
bytes[17] = 17;
|
|
|
|
let test2 = Test::from_bytes(&bytes).unwrap();
|
|
|
|
assert!(test2 == Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
c: MyType {
|
|
|
|
k: 23,
|
|
|
|
j: 1131071,
|
|
|
|
},
|
|
|
|
d: 5,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string_in_packet() {
|
|
|
|
#[pso_packet2(0x23)]
|
|
|
|
struct Test {
|
|
|
|
a: u32,
|
|
|
|
b: u16,
|
|
|
|
s: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
let test = Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
s: "asdf あえいおう".to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = test.as_bytes();
|
|
|
|
assert!(bytes == vec![36, 0, 35, 0, 0, 0, 0, 0, 21, 205, 91, 7, 49, 212, 97, 0, 115, 0, 100, 0,
|
|
|
|
102, 0, 32, 0, 66, 48, 72, 48, 68, 48, 74, 48, 70, 48, 0, 0]);
|
|
|
|
bytes[18] = 99;
|
|
|
|
let test2 = Test::from_bytes(&bytes).unwrap();
|
|
|
|
assert!(test2 == Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
s: "ascf あえいおう\u{0}".to_string(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vec_in_packet() {
|
|
|
|
#[pso_packet2(0x23)]
|
|
|
|
struct Test {
|
|
|
|
a: u32,
|
|
|
|
b: u16,
|
|
|
|
v: Vec<u16>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let test = Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
v: vec![123,456,789],
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = test.as_bytes();
|
|
|
|
assert!(bytes == vec![20, 0, 35, 0, 3, 0, 0, 0, 21, 205, 91, 7, 49, 212, 123, 0, 200, 1, 21, 3]);
|
|
|
|
bytes[18] = 99;
|
|
|
|
let test2 = Test::from_bytes(&bytes).unwrap();
|
|
|
|
assert!(test2 == Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
v: vec![123,456,867],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_flag_packet() {
|
|
|
|
#[pso_packet2(0x23, no_flag)]
|
|
|
|
struct Test {
|
|
|
|
a: u32,
|
|
|
|
b: u16,
|
|
|
|
c: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
let test = Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
c: 9999,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = test.as_bytes();
|
|
|
|
assert!(bytes == vec![12, 0, 35, 0, 21, 205, 91, 7, 49, 212, 15, 39]);
|
|
|
|
|
|
|
|
bytes[11] = 17;
|
|
|
|
let test2 = Test::from_bytes(&bytes).unwrap();
|
|
|
|
assert!(test2 == Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
c: 4367,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_command_error() {
|
|
|
|
#[pso_packet2(0x23)]
|
|
|
|
struct Test {
|
|
|
|
a: u32,
|
|
|
|
b: u16,
|
|
|
|
c: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
let test = Test {
|
|
|
|
a: 123456789,
|
|
|
|
b: 54321,
|
|
|
|
c: 9999,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = test.as_bytes();
|
|
|
|
assert!(bytes == vec![16, 0, 35, 0, 0, 0, 0, 0, 21, 205, 91, 7, 49, 212, 15, 39]);
|
|
|
|
|
|
|
|
bytes[2] = 17;
|
|
|
|
let test2 = Test::from_bytes(&bytes);
|
|
|
|
assert!(test2 == Err(PacketParseError::WrongPacketCommand));
|
|
|
|
}
|
|
|
|
}
|