From 6acd8d202a6bb15df0e2ce54f8e52eae2293f10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Sun, 31 Jul 2022 11:21:27 +0200 Subject: [PATCH] packets: rework encode func --- code/game/src/packet_utils.h | 8 ++++++++ code/game/src/packets/pkt_00_init.c | 9 +-------- code/game/src/packets/pkt_00_init.h | 1 - code/game/src/packets/pkt_01_welcome.c | 12 +++--------- code/game/src/packets/pkt_01_welcome.h | 1 - code/game/src/packets/pkt_send_keystate.c | 9 +-------- code/game/src/packets/pkt_send_keystate.h | 1 - 7 files changed, 13 insertions(+), 28 deletions(-) diff --git a/code/game/src/packet_utils.h b/code/game/src/packet_utils.h index 3a206a2..7df60ff 100644 --- a/code/game/src/packet_utils.h +++ b/code/game/src/packet_utils.h @@ -137,4 +137,12 @@ static inline uint32_t pkt_pack_desc_args(pkt_desc *desc) { return cnt; } +static inline size_t pkt_table_encode(pkt_desc *desc, void *raw_blob, uint32_t blob_size) { + assert(desc && raw_blob && blob_size > 0); + cw_pack_context pc = {0}; + pkt_pack_msg(&pc, pkt_pack_desc_args(desc)); + pkt_pack_struct(&pc, desc, raw_blob, blob_size); + return pkt_pack_msg_size(&pc); +} + void pkt_dump_struct(pkt_desc *desc, void* raw_blob, uint32_t blob_size); diff --git a/code/game/src/packets/pkt_00_init.c b/code/game/src/packets/pkt_00_init.c index 1c9376c..2409b6c 100644 --- a/code/game/src/packets/pkt_00_init.c +++ b/code/game/src/packets/pkt_00_init.c @@ -16,16 +16,9 @@ pkt_desc pkt_00_init_desc[] = { { PKT_END }, }; -size_t pkt_00_init_encode(pkt_00_init *table) { - cw_pack_context pc = {0}; - pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_00_init_desc)); - pkt_pack_struct(&pc, pkt_00_init_desc, PKT_STRUCT_PTR(table)); - return pkt_pack_msg_size(&pc); -} - size_t pkt_00_init_send(uint16_t view_id) { pkt_00_init table = {.view_id = view_id }; - return pkt_world_write(MSG_ID_00_INIT, pkt_00_init_encode(&table), 1, view_id, NULL, 1); + return pkt_world_write(MSG_ID_00_INIT, pkt_table_encode(pkt_00_init_desc, PKT_STRUCT_PTR(&table)), 1, view_id, NULL, 1); } int32_t pkt_00_init_handler(pkt_header *header) { diff --git a/code/game/src/packets/pkt_00_init.h b/code/game/src/packets/pkt_00_init.h index d52b8c5..1ce1c70 100644 --- a/code/game/src/packets/pkt_00_init.h +++ b/code/game/src/packets/pkt_00_init.h @@ -6,7 +6,6 @@ typedef struct { uint16_t view_id; } pkt_00_init; -size_t pkt_00_init_encode(pkt_00_init *table); size_t pkt_00_init_send(uint16_t view_id); extern pkt_desc pkt_00_init_desc[]; diff --git a/code/game/src/packets/pkt_01_welcome.c b/code/game/src/packets/pkt_01_welcome.c index 55fe659..1b0d6ac 100644 --- a/code/game/src/packets/pkt_01_welcome.c +++ b/code/game/src/packets/pkt_01_welcome.c @@ -13,12 +13,6 @@ pkt_desc pkt_01_welcome_desc[] = { { PKT_END }, }; -size_t pkt_01_welcome_encode(pkt_01_welcome *table) { - cw_pack_context pc = {0}; - pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_01_welcome_desc)); - pkt_pack_struct(&pc, pkt_01_welcome_desc, PKT_STRUCT_PTR(table)); - return pkt_pack_msg_size(&pc); -} size_t pkt_01_welcome_send(uint32_t seed, uint64_t peer_id, uint16_t view_id, @@ -26,15 +20,15 @@ size_t pkt_01_welcome_send(uint32_t seed, uint16_t chunk_size, uint16_t world_size) { pkt_01_welcome table = {.seed = seed, .ent_id = ent_id, .chunk_size = chunk_size, .world_size = world_size}; - return pkt_world_write(MSG_ID_01_WELCOME, pkt_01_welcome_encode(&table), 1, view_id, (void*)peer_id, 0); + return pkt_world_write(MSG_ID_01_WELCOME, pkt_table_encode(pkt_01_welcome_desc, PKT_STRUCT_PTR(&table)), 1, view_id, (void*)peer_id, 0); } int32_t pkt_01_welcome_handler(pkt_header *header) { pkt_01_welcome table; PKT_IF(pkt_msg_decode(header, pkt_01_welcome_desc, pkt_pack_desc_args(pkt_01_welcome_desc), PKT_STRUCT_PTR(&table))); - + world_view *view = game_world_view_get(header->view_id); - + zpl_printf("[INFO] initializing read-only world view ...\n"); world_view_init(view, table.seed, table.ent_id, table.chunk_size, table.world_size); game_world_view_set_active(view); diff --git a/code/game/src/packets/pkt_01_welcome.h b/code/game/src/packets/pkt_01_welcome.h index d88af6c..11a0c85 100644 --- a/code/game/src/packets/pkt_01_welcome.h +++ b/code/game/src/packets/pkt_01_welcome.h @@ -15,7 +15,6 @@ size_t pkt_01_welcome_send(uint32_t seed, uint64_t ent_id, uint16_t chunk_size, uint16_t world_size); -size_t pkt_01_welcome_encode(pkt_01_welcome *table); extern pkt_desc pkt_01_welcome_desc[]; PKT_HANDLER_PROC(pkt_01_welcome_handler); diff --git a/code/game/src/packets/pkt_send_keystate.c b/code/game/src/packets/pkt_send_keystate.c index 7901a37..d4a11de 100644 --- a/code/game/src/packets/pkt_send_keystate.c +++ b/code/game/src/packets/pkt_send_keystate.c @@ -27,14 +27,7 @@ pkt_desc pkt_send_keystate_desc[] = { size_t pkt_send_keystate_send(uint16_t view_id, game_keystate_data *data) { - return pkt_world_write(MSG_ID_SEND_KEYSTATE, pkt_send_keystate_encode(data), 1, view_id, NULL, 1); -} - -size_t pkt_send_keystate_encode(pkt_send_keystate *table) { - cw_pack_context pc = {0}; - pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_send_keystate_desc)); - pkt_pack_struct(&pc, pkt_send_keystate_desc, PKT_STRUCT_PTR(table)); - return pkt_pack_msg_size(&pc); + return pkt_world_write(MSG_ID_SEND_KEYSTATE, pkt_table_encode(pkt_send_keystate_desc, PKT_STRUCT_PTR(data)), 1, view_id, NULL, 1); } int32_t pkt_send_keystate_handler(pkt_header *header) { diff --git a/code/game/src/packets/pkt_send_keystate.h b/code/game/src/packets/pkt_send_keystate.h index e331c03..3c2446b 100644 --- a/code/game/src/packets/pkt_send_keystate.h +++ b/code/game/src/packets/pkt_send_keystate.h @@ -26,7 +26,6 @@ typedef pkt_send_keystate game_keystate_data; size_t pkt_send_keystate_send(uint16_t view_id, game_keystate_data *data); -size_t pkt_send_keystate_encode(pkt_send_keystate *table); extern pkt_desc pkt_send_keystate_desc[]; PKT_HANDLER_PROC(pkt_send_keystate_handler);