(de)serialise entities too but it does even the uninitialised ones too due to the array thing
This commit is contained in:
parent
9ec7f392c7
commit
578f4446b4
@ -15,8 +15,8 @@ int main(void) {
|
||||
err = sock_init(&sock);
|
||||
if (err) goto handle_error;
|
||||
|
||||
char ibuf[1024] = { 0 };
|
||||
read(sock, ibuf, 1024);
|
||||
char ibuf[8192] = { 0 };
|
||||
read(sock, ibuf, 8192);
|
||||
|
||||
struct world_t world = { 0 };
|
||||
err = world_deserialise(&world, ibuf);
|
||||
@ -25,11 +25,11 @@ int main(void) {
|
||||
printf("CLIENT: (%zu %zu) ->", world.height++, world.width ++);
|
||||
printf(" (%zu %zu)\n", world.height, world.width);
|
||||
|
||||
char obuf[1024] = { 0 };
|
||||
err = world_serialise_buf(&world, obuf, 1024);
|
||||
char obuf[8192] = { 0 };
|
||||
err = world_serialise_buf(&world, obuf, 8192);
|
||||
if (err != ERR_OK) goto handle_error_sock;
|
||||
|
||||
write(sock, obuf, 1024);
|
||||
write(sock, obuf, 8192);
|
||||
|
||||
close(sock);
|
||||
return ERR_OK;
|
||||
|
@ -3,6 +3,10 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#include "error.h"
|
||||
|
||||
struct entity_t {
|
||||
size_t id;
|
||||
size_t x;
|
||||
@ -11,4 +15,7 @@ struct entity_t {
|
||||
|
||||
void entity_init(struct entity_t *, size_t, size_t, size_t);
|
||||
|
||||
enum error_t entity_serialise(struct entity_t const *, struct json_t **);
|
||||
enum error_t entity_deserialise(struct entity_t *, struct json_t *);
|
||||
|
||||
#endif
|
||||
|
@ -4,6 +4,10 @@
|
||||
|
||||
#include "entity/entity.h"
|
||||
|
||||
|
||||
static char const *const ENTITY_JSON_FMT = "{sI, sI, sI}";
|
||||
|
||||
|
||||
void entity_init(struct entity_t *self, size_t id, size_t x, size_t y) {
|
||||
assert(self != NULL);
|
||||
|
||||
@ -11,3 +15,37 @@ void entity_init(struct entity_t *self, size_t id, size_t x, size_t y) {
|
||||
self->x = x;
|
||||
self->y = y;
|
||||
}
|
||||
|
||||
|
||||
enum error_t entity_serialise(
|
||||
struct entity_t const *self,
|
||||
struct json_t **json
|
||||
) {
|
||||
assert(self != NULL);
|
||||
assert(json != NULL);
|
||||
|
||||
struct json_t *maybe_json = json_pack(ENTITY_JSON_FMT,
|
||||
"id", self->id,
|
||||
"x", self->x,
|
||||
"y", self->y
|
||||
);
|
||||
if (maybe_json == NULL) return ERR_JSON_SERIALISE;
|
||||
|
||||
*json = maybe_json;
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
enum error_t entity_deserialise(struct entity_t *self, struct json_t *json) {
|
||||
assert(self != NULL);
|
||||
assert(json != NULL);
|
||||
|
||||
int json_err = json_unpack(json, ENTITY_JSON_FMT,
|
||||
"id", &self->id,
|
||||
"x", &self->x,
|
||||
"y", &self->y
|
||||
);
|
||||
if (json_err < 0) return ERR_JSON_DESERIALISE;
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
@ -116,7 +116,10 @@ enum error_t entity_registry_serialise(
|
||||
if (err != ERR_OK) goto error_cleanup_array;
|
||||
|
||||
int json_err = json_array_append_new(entities_json, maybe_entity_json);
|
||||
if (json_err < 0) goto error_cleanup_array;
|
||||
if (json_err < 0) {
|
||||
err = ERR_JSON_SERIALISE;
|
||||
goto error_cleanup_array;
|
||||
}
|
||||
}
|
||||
assert(json_array_size(entities_json) == self->size);
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
#include "world.h"
|
||||
|
||||
static char const *const JSON_FMT = "{so, sI, sI}";
|
||||
static char const *const JSON_FMT = "{so, so, sI, sI}";
|
||||
|
||||
enum error_t world_init(struct world_t *self, size_t height, size_t width) {
|
||||
assert(self != NULL);
|
||||
@ -15,8 +15,8 @@ enum error_t world_init(struct world_t *self, size_t height, size_t width) {
|
||||
|
||||
for (size_t i = 0; i < MAX_ENTITIES; i++) {
|
||||
self->entities[i].id = 0;
|
||||
self->entities[i].x = self->width;
|
||||
self->entities[i].y = self->height;
|
||||
self->entities[i].x = 0;
|
||||
self->entities[i].y = 0;
|
||||
}
|
||||
|
||||
enum error_t err = entity_registry_init(&self->registered_entities);
|
||||
@ -47,16 +47,32 @@ static enum error_t serialise(struct world_t *self, struct json_t **json) {
|
||||
assert(json != NULL);
|
||||
// TODO: (de)serialise the following:
|
||||
// struct entity_t entities[MAX_ENTITIES];
|
||||
// struct entity_registry_t registered_entities;
|
||||
|
||||
enum error_t err = ERR_OK;
|
||||
|
||||
struct json_t *entities_json = json_array();
|
||||
for (size_t i = 0; i < MAX_ENTITIES; i++) {
|
||||
struct json_t *maybe_entity_json;
|
||||
err = entity_serialise(&self->entities[i], &maybe_entity_json);
|
||||
if (err != ERR_OK) goto error_cleanup_array;
|
||||
|
||||
int json_err = json_array_append_new(entities_json, maybe_entity_json);
|
||||
if (json_err < 0) {
|
||||
err = ERR_JSON_SERIALISE;
|
||||
goto error_cleanup_array;
|
||||
}
|
||||
}
|
||||
assert(json_array_size(entities_json) == MAX_ENTITIES);
|
||||
|
||||
struct json_t *registered_entities_json;
|
||||
enum error_t err = entity_registry_serialise(
|
||||
err = entity_registry_serialise(
|
||||
&self->registered_entities,
|
||||
®istered_entities_json
|
||||
);
|
||||
if (err != ERR_OK) return err;
|
||||
|
||||
struct json_t *maybe_json = json_pack(JSON_FMT,
|
||||
"entities", entities_json,
|
||||
"registered_entities", registered_entities_json,
|
||||
"height", self->height,
|
||||
"width", self->width
|
||||
@ -65,6 +81,10 @@ static enum error_t serialise(struct world_t *self, struct json_t **json) {
|
||||
|
||||
*json = maybe_json;
|
||||
return ERR_OK;
|
||||
|
||||
error_cleanup_array:
|
||||
json_array_clear(entities_json);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
@ -72,14 +92,24 @@ static enum error_t deserialise(struct world_t *self, struct json_t *json) {
|
||||
assert(self != NULL);
|
||||
assert(json != NULL);
|
||||
|
||||
struct json_t *entities_json;
|
||||
struct json_t *registered_entities_json;
|
||||
int json_err = json_unpack(json, JSON_FMT,
|
||||
"entities", &entities_json,
|
||||
"registered_entities", ®istered_entities_json,
|
||||
"height", &self->height,
|
||||
"width", &self->width
|
||||
);
|
||||
if (json_err < 0) return ERR_JSON_DESERIALISE;
|
||||
|
||||
for (size_t i = 0; i < MAX_ENTITIES; i++) {
|
||||
struct json_t *entity_json = json_array_get(entities_json, i);
|
||||
if (entity_json == NULL) return ERR_JSON_DESERIALISE;
|
||||
|
||||
enum error_t err = entity_deserialise(&self->entities[i], entity_json);
|
||||
if (err != ERR_OK) return err;
|
||||
}
|
||||
|
||||
enum error_t err = entity_registry_deserialise(
|
||||
&self->registered_entities,
|
||||
registered_entities_json
|
||||
|
@ -25,14 +25,14 @@ static void handle_signal(int signal_no) {
|
||||
enum error_t game_loop(struct data_t *data) {
|
||||
enum error_t err = ERR_OK;
|
||||
|
||||
char obuf[1024] = { 0 };
|
||||
err = world_serialise_buf(&data->world, obuf, 1024);
|
||||
char obuf[8192] = { 0 };
|
||||
err = world_serialise_buf(&data->world, obuf, 8192);
|
||||
if (err != ERR_OK) return err;
|
||||
|
||||
write(data->socket_accept, obuf, 1024);
|
||||
write(data->socket_accept, obuf, 8192);
|
||||
|
||||
char ibuf[1024] = { 0 };
|
||||
read(data->socket_accept, ibuf, 1024);
|
||||
char ibuf[8192] = { 0 };
|
||||
read(data->socket_accept, ibuf, 8192);
|
||||
|
||||
err = world_deserialise(&data->world, ibuf);
|
||||
if (err != ERR_OK) return err;
|
||||
|
Loading…
Reference in New Issue
Block a user