From 41d27344c3b489466632bd2f1314fd69ff0a363c Mon Sep 17 00:00:00 2001 From: Trumeet Date: Fri, 29 Jul 2022 11:55:40 -0700 Subject: fix(libacron): spec incompliance: player is optional in disconnect events API:CHANGE --- client/helloworld/main.c | 20 ++++++++++++-------- client/libacron/ids.c | 9 ++++++--- client/libacron/include/events.h | 2 +- client/libacron/private/serializer.c | 16 +++++++++++----- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/client/helloworld/main.c b/client/helloworld/main.c index c112fcb..cba7e30 100644 --- a/client/helloworld/main.c +++ b/client/helloworld/main.c @@ -120,13 +120,17 @@ static void handle_event(const ac_event_t *event) { } case AC_EVENT_PLAYER_DISCONNECT: { ac_event_player_disconnect_t *o = (ac_event_player_disconnect_t *) event; - printf("Player '%s' disconnected at %s(%.2f, %.2f, %.2f): %s.\n", - o->player.name, - world_name(o->player.world), - o->player.pos.x, - o->player.pos.y, - o->player.pos.z, - o->reason); + if (!o->player) { + printf("Unknown player disconnected: %s.\n", o->reason); + } else { + printf("Player '%s' disconnected at %s(%.2f, %.2f, %.2f): %s.\n", + o->player->name, + world_name(o->player->world), + o->player->pos.x, + o->player->pos.y, + o->player->pos.z, + o->reason); + } break; } case AC_EVENT_PLAYER_JOIN: { @@ -367,4 +371,4 @@ int main(int argc, char **argv) { mtx_destroy(&mtx_conn); #endif return r; -} \ No newline at end of file +} diff --git a/client/libacron/ids.c b/client/libacron/ids.c index 89fff9a..7af471d 100644 --- a/client/libacron/ids.c +++ b/client/libacron/ids.c @@ -20,8 +20,11 @@ int ac_object_free(ac_obj_t *obj) { } case AC_EVENT_PLAYER_DISCONNECT: { ac_event_player_disconnect_t *v = (ac_event_player_disconnect_t *) obj; - if (v->player.name) free(v->player.name); - if (v->player.uuid) free(v->player.uuid); + if (v->player) { + if (v->player->name) free(v->player->name); + if (v->player->uuid) free(v->player->uuid); + free(v->player); + } if (v->reason) free(v->reason); goto ok; } @@ -77,4 +80,4 @@ int ac_object_free(ac_obj_t *obj) { free(obj); return AC_E_OK; }; -} \ No newline at end of file +} diff --git a/client/libacron/include/events.h b/client/libacron/include/events.h index 93a5184..17ecd74 100644 --- a/client/libacron/include/events.h +++ b/client/libacron/include/events.h @@ -23,7 +23,7 @@ typedef struct ac_event_player_join { #define AC_EVENT_PLAYER_DISCONNECT AC_ID(AC_TYPE_EVENT, 2 /* 0b0000010 */) /* 0b00000010 */ typedef struct ac_event_player_disconnect { uint8_t type; - ac_entity_t player; + ac_entity_t *player; char *reason; } ac_event_player_disconnect_t; diff --git a/client/libacron/private/serializer.c b/client/libacron/private/serializer.c index 9196305..d5171ba 100644 --- a/client/libacron/private/serializer.c +++ b/client/libacron/private/serializer.c @@ -269,14 +269,20 @@ static int deserialize_event(json_object *obj, const char *type_str, ac_event_t } disconnect->type = AC_EVENT_PLAYER_DISCONNECT; - json_object *arg; - if ((r = get_child(obj, "player", json_type_object, true, &arg))) { + json_object *arg = NULL; + if ((r = get_child(obj, "player", json_type_object, false, &arg))) { ac_object_free((ac_obj_t *) disconnect); goto fail; } - if ((r = deserialize_entity(arg, &disconnect->player))) { - ac_object_free((ac_obj_t *) disconnect); - goto fail; + if (arg) { + if ((r = SALLOC(ac_entity_t, &disconnect->player))) { + ac_object_free((ac_obj_t *) disconnect); + goto fail; + } + if ((r = deserialize_entity(arg, disconnect->player))) { + ac_object_free((ac_obj_t *) disconnect); + goto fail; + } } if ((r = get_child(obj, "reason", json_type_string, true, &arg))) { -- cgit v1.2.3