Skip to content

Commit

Permalink
Add ToBytes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed May 14, 2024
1 parent d64e6c8 commit ea21987
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 137 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions korangar/src/loaders/archive/native/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ impl Writable for NativeArchiveBuilder {
let version = 0x200;
let file_header = Header::new(file_table_offset, reserved_files, raw_file_count, version);

let mut bytes = Vec::new();

bytes.extend(file_header.to_bytes().unwrap());
let mut bytes = file_header.to_bytes().unwrap();
bytes.extend_from_slice(&self.data);

let mut file_table_data = Vec::new();
Expand Down
4 changes: 2 additions & 2 deletions korangar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ fn main() {
&map,
saved_login_data.account_id,
character_information,
WorldPosition::new(0, 0),
WorldPosition { x: 0, y: 0 },
client_tick,
);
let player = Entity::Player(player);
Expand Down Expand Up @@ -1094,7 +1094,7 @@ fn main() {
},
UserEvent::RequestPlayerMove(destination) => {
if !entities.is_empty() {
let _ = networking_system.player_move(WorldPosition::new(destination.x, destination.y));
let _ = networking_system.player_move(WorldPosition { x: destination.x, y: destination.y });
}
}
UserEvent::RequestPlayerInteract(entity_id) => {
Expand Down
21 changes: 6 additions & 15 deletions korangar_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,20 @@ where
events
}

async fn handle_server_thing<Ping>(
async fn handle_server_thing<PingPacket>(
address: SocketAddr,
mut action_receiver: UnboundedReceiver<Vec<u8>>,
event_sender: UnboundedSender<NetworkEvent>,
mut packet_handler: PacketHandler<NetworkEventList, (), Callback>,
ping_factory: impl Fn() -> Ping,
ping_factory: impl Fn() -> PingPacket,
ping_frequency: Duration,
// After logging in to the character server, it sends the account id without any packet.
// Since our packet handler has no way of working with this, we need to add some special
// logic.
mut read_account_id: bool,
) -> Result<(), NetworkTaskError>
where
Ping: ragnarok_packets::Packet + ClientPacket,
PingPacket: Packet + ClientPacket,
Callback: PacketCallback,
{
let mut stream = TcpStream::connect(address).await.map_err(|_| NetworkTaskError::FailedToConnect)?;
Expand Down Expand Up @@ -415,10 +415,7 @@ where
self.map_server_connection = ServerConnection::ClosingManually;
}

pub fn send_login_server_packet<Packet>(&mut self, packet: &Packet) -> Result<(), NotConnectedError>
where
Packet: ragnarok_packets::Packet + LoginServerPacket,
{
pub fn send_login_server_packet(&mut self, packet: &(impl Packet + LoginServerPacket)) -> Result<(), NotConnectedError> {
match &mut self.login_server_connection {
ServerConnection::Connected { action_sender, .. } => {
self.packet_callback.outgoing_packet(packet);
Expand All @@ -430,10 +427,7 @@ where
}
}

pub fn send_character_server_packet<Packet>(&mut self, packet: &Packet) -> Result<(), NotConnectedError>
where
Packet: ragnarok_packets::Packet + CharacterServerPacket,
{
pub fn send_character_server_packet(&mut self, packet: &(impl Packet + CharacterServerPacket)) -> Result<(), NotConnectedError> {
match &mut self.character_server_connection {
ServerConnection::Connected { action_sender, .. } => {
self.packet_callback.outgoing_packet(packet);
Expand All @@ -445,10 +439,7 @@ where
}
}

pub fn send_map_server_packet<Packet>(&mut self, packet: &Packet) -> Result<(), NotConnectedError>
where
Packet: ragnarok_packets::Packet + MapServerPacket,
{
pub fn send_map_server_packet(&mut self, packet: &(impl Packet + MapServerPacket)) -> Result<(), NotConnectedError> {
match &mut self.map_server_connection {
ServerConnection::Connected { action_sender, .. } => {
self.packet_callback.outgoing_packet(packet);
Expand Down
104 changes: 104 additions & 0 deletions ragnarok_bytes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,107 @@ pub use self::fixed::{FixedByteSize, FixedByteSizeCollection};
pub use self::from_bytes::{FromBytes, FromBytesExt};
pub use self::stream::ByteStream;
pub use self::to_bytes::{ToBytes, ToBytesExt};

#[cfg(test)]
mod conversion {
use crate::{ByteStream, FromBytes, ToBytes};

fn encode_decode<T: FromBytes + ToBytes>(input: &[u8]) {
let mut byte_stream = ByteStream::<()>::without_metadata(input);

let data = T::from_bytes(&mut byte_stream).unwrap();
let output = data.to_bytes().unwrap();

assert_eq!(input, output.as_slice());
}

#[test]
pub fn u8() {
encode_decode::<u8>(&[170]);
}

#[test]
pub fn u16() {
encode_decode::<u16>(&[170, 85]);
}

#[test]
pub fn u32() {
encode_decode::<u32>(&[170, 85, 170, 85]);
}

#[test]
pub fn u64() {
encode_decode::<u64>(&[170, 85, 170, 85, 170, 85, 170, 85]);
}

#[test]
pub fn i8() {
encode_decode::<i8>(&[170]);
}

#[test]
pub fn i16() {
encode_decode::<i16>(&[170, 85]);
}

#[test]
pub fn i32() {
encode_decode::<i32>(&[170, 85, 170, 85]);
}

#[test]
pub fn i64() {
encode_decode::<i64>(&[170, 85, 170, 85, 170, 85, 170, 85]);
}

#[test]
pub fn f32() {
encode_decode::<f32>(&[170, 85, 170, 85]);
}

#[test]
pub fn array() {
encode_decode::<[u8; 4]>(&[1, 2, 3, 4]);
}

#[test]
pub fn string() {
encode_decode::<String>(b"testing\0");
}

#[test]
pub fn vector() {
encode_decode::<Vec<u8>>(&[1, 2, 3, 4]);
}

#[cfg(feature = "cgmath")]
#[test]
pub fn vector2() {
encode_decode::<cgmath::Vector2<u8>>(&[1, 2]);
}

#[cfg(feature = "cgmath")]
#[test]
pub fn vector3() {
encode_decode::<cgmath::Vector3<u8>>(&[1, 2, 3]);
}

#[cfg(feature = "cgmath")]
#[test]
pub fn vector4() {
encode_decode::<cgmath::Vector4<u8>>(&[1, 2, 3, 4]);
}

#[cfg(feature = "cgmath")]
#[test]
pub fn quaternion() {
encode_decode::<cgmath::Quaternion<u8>>(&[1, 2, 3, 4]);
}

#[cfg(feature = "cgmath")]
#[test]
pub fn matrix3() {
encode_decode::<cgmath::Matrix3<u8>>(&[1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
}
21 changes: 19 additions & 2 deletions ragnarok_bytes/src/to_bytes/implement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,30 @@ impl<T: ToBytes> ToBytes for Vector4<T> {
#[cfg(feature = "cgmath")]
impl<T: ToBytes> ToBytes for Quaternion<T> {
fn to_bytes(&self) -> ConversionResult<Vec<u8>> {
todo!()
let mut bytes = self.v.x.to_bytes().trace::<Self>()?;
bytes.append(&mut self.v.y.to_bytes().trace::<Self>()?);
bytes.append(&mut self.v.z.to_bytes().trace::<Self>()?);
bytes.append(&mut self.s.to_bytes().trace::<Self>()?);

Ok(bytes)
}
}

#[cfg(feature = "cgmath")]
impl<T: ToBytes> ToBytes for Matrix3<T> {
fn to_bytes(&self) -> ConversionResult<Vec<u8>> {
todo!()
let mut bytes = self.x.x.to_bytes().trace::<Self>()?;
bytes.append(&mut self.x.y.to_bytes().trace::<Self>()?);
bytes.append(&mut self.x.z.to_bytes().trace::<Self>()?);

bytes.append(&mut self.y.x.to_bytes().trace::<Self>()?);
bytes.append(&mut self.y.y.to_bytes().trace::<Self>()?);
bytes.append(&mut self.y.z.to_bytes().trace::<Self>()?);

bytes.append(&mut self.z.x.to_bytes().trace::<Self>()?);
bytes.append(&mut self.z.y.to_bytes().trace::<Self>()?);
bytes.append(&mut self.z.z.to_bytes().trace::<Self>()?);

Ok(bytes)
}
}
2 changes: 2 additions & 0 deletions ragnarok_formats/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ pub struct Event {
#[derive(Debug, Clone, ByteConvertable)]
#[cfg_attr(feature = "interface", derive(korangar_interface::elements::PrototypeElement))]
pub struct ActionsData {
#[new_default]
pub signature: Signature<b"AC">,
#[version]
pub version: Version<MinorFirst>,
// #[new_derive]
pub action_count: u16,
#[new_default]
pub reserved: [u8; 10],
#[repeating(action_count)]
pub actions: Vec<Action>,
Expand Down
Loading

0 comments on commit ea21987

Please sign in to comment.