diff --git a/src/main.c b/src/main.c index 2c0ce03..c705ebb 100644 --- a/src/main.c +++ b/src/main.c @@ -6,13 +6,13 @@ int main(int argc, char **argv) { packet_context_init(2); - packet_context_register(PCK_CTX_C2S, PCK_ID_REGISTER, 2, 8, 2); - packet_context_register(PCK_CTX_C2S, PCK_ID_LOGIN, 2, 8, 2); - packet_context_register(PCK_CTX_C2S, PCK_ID_CHANGE_CTX, 2, 8, 2); + packet_context_register(PCK_CTX_C2S, PCK_ID_REGISTER, 0, 2, 8, 2); + packet_context_register(PCK_CTX_C2S, PCK_ID_LOGIN, 0, 2, 8, 2); + packet_context_register(PCK_CTX_C2S, PCK_ID_CHANGE_CTX, 0, 2, 1, 2); - packet_context_register(PCK_CTX_S2C, PCK_ID_REGISTER, 2, 1, 16); - packet_context_register(PCK_CTX_S2C, PCK_ID_LOGIN, 3, 1, 2, 16); - packet_context_register(PCK_CTX_S2C, PCK_ID_CHANGE_CTX, 1, 2); + packet_context_register(PCK_CTX_S2C, PCK_ID_REGISTER, 0, 2, 1, 16); + packet_context_register(PCK_CTX_S2C, PCK_ID_LOGIN, 0, 3, 1, 2, 16); + packet_context_register(PCK_CTX_S2C, PCK_ID_CHANGE_CTX, 0, 1, 2); if(argc == 2 && strcmp(argv[1], "server") == 0) { register_spawn_type(SPAWN_SERVER); diff --git a/src/packet.c b/src/packet.c index ce8be5b..4c75134 100644 --- a/src/packet.c +++ b/src/packet.c @@ -8,6 +8,45 @@ struct packet_ctx_t { uint32_t length; }; -static packet_ctx_t *c2s_ctx; -static packet_ctx_t *s2c_ctx; +struct { + uint8_t count; + packet_ctx_t *c2s; + packet_ctx_t *s2c; +} static ctx; +void packet_context_init(uint8_t count) { + ctx.count = count; + ctx.c2s = (packet_ctx_t*)malloc(count * sizeof(packet_ctx_t)); + ctx.s2c = (packet_ctx_t*)malloc(count * sizeof(packet_ctx_t)); +} + +void packet_context_register(uint8_t direction, uint8_t id, uint8_t iter_pt, uint8_t count, ...) { + if(id > ctx.count) + return; + + int i; + va_list args; + packet_ctx_t *ptr = + direction == PCK_CTX_C2S ? ctx.c2s : ctx.s2c; + + ptr[id].length = 0; + ptr[id].iter_start = iter_pt; + ptr[id].region_count = count; + ptr[id].region_lengths = (uint16_t*)malloc(count * sizeof(uint16_t)); + + va_start(args, count); + for(i = 0; i < count; ++i) { + uint16_t length = va_arg(args, uint16_t); + ptr[id].region_lengths[i] = length; + ptr[id].length += length; + } + va_end(args); +} + +void packet_context_free() { + free(ctx.c2s.region_lengths); + free(ctx.c2s); + + free(ctx.s2c.region_lengths); + free(ctx.s2c); +} \ No newline at end of file diff --git a/src/packet.h b/src/packet.h index 1ed3c19..0f5b1e2 100644 --- a/src/packet.h +++ b/src/packet.h @@ -3,6 +3,7 @@ #include #include +#include #include "list.h" #include "common.h" @@ -28,6 +29,6 @@ void packet_context_init(uint8_t); void packet_context_register(uint8_t, uint8_t, uint8_t, ...); void packet_context_register_iter(uint8_t, uint8_t, uint8_t, uint8_t, ...); - +void packet_context_free(); #endif \ No newline at end of file