2026-01-17 22:52:36 -06:00
|
|
|
#include "pxl8_protocol.h"
|
|
|
|
|
#include "pxl8_bytes.h"
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_serialize_header(const pxl8_msg_header* msg, u8* buf, usize len) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_msg_header)) return 0;
|
|
|
|
|
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
|
|
|
|
pxl8_write_u32_be(&s, msg->sequence);
|
|
|
|
|
pxl8_write_u16_be(&s, msg->size);
|
|
|
|
|
pxl8_write_u8(&s, msg->type);
|
|
|
|
|
pxl8_write_u8(&s, msg->version);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_deserialize_header(const u8* buf, usize len, pxl8_msg_header* msg) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_msg_header)) return 0;
|
|
|
|
|
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
|
|
|
|
msg->sequence = pxl8_read_u32_be(&s);
|
|
|
|
|
msg->size = pxl8_read_u16_be(&s);
|
|
|
|
|
msg->type = pxl8_read_u8(&s);
|
|
|
|
|
msg->version = pxl8_read_u8(&s);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_serialize_input(const pxl8_input_msg* msg, u8* buf, usize len) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_input_msg)) return 0;
|
|
|
|
|
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
|
|
|
|
pxl8_write_u32_be(&s, msg->buttons);
|
|
|
|
|
pxl8_write_f32_be(&s, msg->look_dx);
|
|
|
|
|
pxl8_write_f32_be(&s, msg->look_dy);
|
|
|
|
|
pxl8_write_f32_be(&s, msg->move_x);
|
|
|
|
|
pxl8_write_f32_be(&s, msg->move_y);
|
|
|
|
|
pxl8_write_f32_be(&s, msg->yaw);
|
|
|
|
|
pxl8_write_u64_be(&s, msg->tick);
|
|
|
|
|
pxl8_write_u64_be(&s, msg->timestamp);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_deserialize_input(const u8* buf, usize len, pxl8_input_msg* msg) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_input_msg)) return 0;
|
|
|
|
|
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
|
|
|
|
msg->buttons = pxl8_read_u32_be(&s);
|
|
|
|
|
msg->look_dx = pxl8_read_f32_be(&s);
|
|
|
|
|
msg->look_dy = pxl8_read_f32_be(&s);
|
|
|
|
|
msg->move_x = pxl8_read_f32_be(&s);
|
|
|
|
|
msg->move_y = pxl8_read_f32_be(&s);
|
|
|
|
|
msg->yaw = pxl8_read_f32_be(&s);
|
|
|
|
|
msg->tick = pxl8_read_u64_be(&s);
|
|
|
|
|
msg->timestamp = pxl8_read_u64_be(&s);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_serialize_command(const pxl8_command_msg* msg, u8* buf, usize len) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_command_msg)) return 0;
|
|
|
|
|
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
|
|
|
|
pxl8_write_u16_be(&s, msg->cmd_type);
|
|
|
|
|
pxl8_write_bytes(&s, msg->payload, PXL8_COMMAND_PAYLOAD_SIZE);
|
|
|
|
|
pxl8_write_u16_be(&s, msg->payload_size);
|
|
|
|
|
pxl8_write_u64_be(&s, msg->tick);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_deserialize_command(const u8* buf, usize len, pxl8_command_msg* msg) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_command_msg)) return 0;
|
|
|
|
|
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
|
|
|
|
msg->cmd_type = pxl8_read_u16_be(&s);
|
|
|
|
|
pxl8_read_bytes(&s, msg->payload, PXL8_COMMAND_PAYLOAD_SIZE);
|
|
|
|
|
msg->payload_size = pxl8_read_u16_be(&s);
|
|
|
|
|
msg->tick = pxl8_read_u64_be(&s);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_serialize_entity_state(const pxl8_entity_state* state, u8* buf, usize len) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_entity_state)) return 0;
|
|
|
|
|
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
|
|
|
|
pxl8_write_u64_be(&s, state->entity_id);
|
|
|
|
|
pxl8_write_bytes(&s, state->userdata, 56);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_deserialize_entity_state(const u8* buf, usize len, pxl8_entity_state* state) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_entity_state)) return 0;
|
|
|
|
|
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
|
|
|
|
state->entity_id = pxl8_read_u64_be(&s);
|
|
|
|
|
pxl8_read_bytes(&s, state->userdata, 56);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_serialize_event(const pxl8_event_msg* msg, u8* buf, usize len) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_event_msg)) return 0;
|
|
|
|
|
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
|
|
|
|
pxl8_write_u8(&s, msg->event_type);
|
|
|
|
|
pxl8_write_bytes(&s, msg->payload, PXL8_EVENT_PAYLOAD_SIZE);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_deserialize_event(const u8* buf, usize len, pxl8_event_msg* msg) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_event_msg)) return 0;
|
|
|
|
|
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
|
|
|
|
msg->event_type = pxl8_read_u8(&s);
|
|
|
|
|
pxl8_read_bytes(&s, msg->payload, PXL8_EVENT_PAYLOAD_SIZE);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_serialize_snapshot_header(const pxl8_snapshot_header* hdr, u8* buf, usize len) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_snapshot_header)) return 0;
|
|
|
|
|
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
|
|
|
|
pxl8_write_u16_be(&s, hdr->entity_count);
|
|
|
|
|
pxl8_write_u16_be(&s, hdr->event_count);
|
|
|
|
|
pxl8_write_u64_be(&s, hdr->player_id);
|
|
|
|
|
pxl8_write_u64_be(&s, hdr->tick);
|
|
|
|
|
pxl8_write_f32_be(&s, hdr->time);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:19:50 -06:00
|
|
|
usize pxl8_protocol_deserialize_snapshot_header(const u8* buf, usize len, pxl8_snapshot_header* hdr) {
|
2026-01-17 22:52:36 -06:00
|
|
|
if (len < sizeof(pxl8_snapshot_header)) return 0;
|
|
|
|
|
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
|
|
|
|
hdr->entity_count = pxl8_read_u16_be(&s);
|
|
|
|
|
hdr->event_count = pxl8_read_u16_be(&s);
|
|
|
|
|
hdr->player_id = pxl8_read_u64_be(&s);
|
|
|
|
|
hdr->tick = pxl8_read_u64_be(&s);
|
|
|
|
|
hdr->time = pxl8_read_f32_be(&s);
|
|
|
|
|
return s.offset;
|
|
|
|
|
}
|