eco2d/code/game/source/editors/texed_prj.c

104 lines
3.3 KiB
C
Raw Normal View History

2021-05-15 15:19:50 +00:00
//~ NOTE(zaklaus): DATA SERIALISATION
2021-05-16 10:56:36 +00:00
#define ECOTEX_VERSION 3
2021-05-15 15:19:50 +00:00
#define UNPACK(kind) cw_unpack_next(&uc); assert(uc.item.type == kind);
void texed_load(void) {
assert(ctx.filepath);
zpl_printf("Loading %s ...\n", ctx.filepath);
2021-05-16 09:10:33 +00:00
is_repaint_locked = true;
2021-05-15 15:19:50 +00:00
zpl_array_clear(ctx.ops);
uint32_t size = 0;
uint8_t *databuf = LoadFileData(zpl_bprintf("art/%s", ctx.filepath), &size);
cw_unpack_context uc;
cw_unpack_context_init(&uc, databuf, (size_t)size, NULL);
UNPACK(CWP_ITEM_POSITIVE_INTEGER);
assert(uc.item.as.u64 == ECOTEX_VERSION);
UNPACK(CWP_ITEM_POSITIVE_INTEGER);
int selected_op = (int)uc.item.as.u64;
2021-05-15 17:40:27 +00:00
UNPACK(CWP_ITEM_FLOAT);
zoom = uc.item.as.real;
2021-05-15 15:19:50 +00:00
UNPACK(CWP_ITEM_ARRAY);
int arrsize = (int)uc.item.as.array.size;
for (int i = 0; i < arrsize; i += 1) {
UNPACK(CWP_ITEM_POSITIVE_INTEGER);
int kind = (int)uc.item.as.u64;
texed_add_op(kind);
td_op *op = zpl_array_end(ctx.ops);
UNPACK(CWP_ITEM_BOOLEAN);
2021-05-16 10:56:36 +00:00
op->is_locked = uc.item.as.boolean;
UNPACK(CWP_ITEM_BOOLEAN);
2021-05-15 15:19:50 +00:00
op->is_hidden = uc.item.as.boolean;
UNPACK(CWP_ITEM_ARRAY);
2021-05-15 17:17:47 +00:00
op->num_params = default_ops[kind].num_params;
2021-05-15 15:19:50 +00:00
op->params = zpl_malloc(sizeof(td_param)*op->num_params);
int parmarrsize = (int)uc.item.as.array.size;
for (int j = 0; j < parmarrsize; j += 1) {
td_param *p = &op->params[j];
UNPACK(CWP_ITEM_STR);
zpl_memcopy(p->str, uc.item.as.str.start, uc.item.as.str.length);
// NOTE(zaklaus): fix up other metadata
p->name = default_ops[kind].params[j].name;
2021-05-15 17:17:47 +00:00
p->kind = default_ops[kind].params[j].kind;
}
// NOTE(zaklaus): resolve missing params
for (int j = parmarrsize; j < default_ops[kind].num_params; j += 1) {
td_param *p = &op->params[j];
p->name = default_ops[kind].params[j].name;
p->kind = default_ops[kind].params[j].kind;
zpl_strcpy(p->str, default_ops[kind].params[j].str);
2021-05-15 15:19:50 +00:00
}
}
assert(uc.return_code == CWP_RC_OK);
cw_unpack_next(&uc);
assert(uc.return_code == CWP_RC_END_OF_INPUT);
ctx.selected_op = selected_op;
2021-05-16 09:10:33 +00:00
is_repaint_locked = false;
2021-05-15 15:19:50 +00:00
texed_repaint_preview();
UnloadFileData(databuf);
2021-05-16 11:36:01 +00:00
ctx.is_saved = true;
2021-05-15 15:19:50 +00:00
}
void texed_save(void) {
assert(ctx.filepath);
zpl_printf("Saving %s ...\n", ctx.filepath);
static uint8_t databuf[400000] = {0};
cw_pack_context pc;
cw_pack_context_init(&pc, databuf, sizeof(databuf), NULL);
cw_pack_unsigned(&pc, ECOTEX_VERSION);
cw_pack_unsigned(&pc, ctx.selected_op);
2021-05-15 17:40:27 +00:00
cw_pack_float(&pc, zoom);
2021-05-15 15:19:50 +00:00
cw_pack_array_size(&pc, zpl_array_count(ctx.ops));
for (int i = 0; i < zpl_array_count(ctx.ops); i += 1) {
td_op *op = &ctx.ops[i];
cw_pack_unsigned(&pc, op->kind);
2021-05-16 10:56:36 +00:00
cw_pack_boolean(&pc, (bool)op->is_locked);
2021-05-15 15:19:50 +00:00
cw_pack_boolean(&pc, (bool)op->is_hidden);
cw_pack_array_size(&pc, op->num_params);
for (int j = 0; j < op->num_params; j += 1) {
td_param *p = &op->params[j];
cw_pack_str(&pc, p->str, zpl_strlen(p->str));
}
}
SaveFileData(zpl_bprintf("art/%s", ctx.filepath), databuf, pc.current - pc.start);
2021-05-16 11:36:01 +00:00
ctx.is_saved = true;
2021-05-15 15:19:50 +00:00
}