Skip to content

Commit

Permalink
examples [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
sjml committed Dec 4, 2024
1 parent 15b9a84 commit c7a3900
Show file tree
Hide file tree
Showing 8 changed files with 816 additions and 300 deletions.
6 changes: 2 additions & 4 deletions docs/dev/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ This file is a rough todo list for the tool itself.

## dustoff notes
- update documentation before publishing 0.3.*
- call out that process_raw_bytes doesn't append null in Rust
- that this was an ergonomics decision (otherwise having to deal with vec of optionals), but can be revisited
- if you call from_bytes on a generic message, it has to be tagged
- UnpackMessages should check for EOF on reads and throw/return errors as appropriate
- make sure we're consuming the terminator, too
- Swift the PackMessages and stuff hangs off the array because of Reasons™
- UnpackMessages should check for EOF on reads and throw/return errors as appropriate
- make sure we're consuming the terminator, too

## possible future protocol features:
- ?? inline string and array length types so they don't have to be protocol-wide like they are now
Expand Down
146 changes: 134 additions & 12 deletions docs/generated_examples/c_example.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
This file was automatically generated by Beschi v0.2.0
This file was automatically generated by Beschi v0.3.0
<https://github.com/sjml/beschi>
Do not edit directly.
*/
Expand All @@ -12,9 +12,10 @@
#ifndef INCLUDE_APPMESSAGES_H
#define INCLUDE_APPMESSAGES_H

#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

typedef uint8_t AppMessages_err_t;
#define APPMESSAGES_ERR_OK 0
Expand Down Expand Up @@ -77,9 +78,12 @@ typedef enum {
AppMessages_MessageType AppMessages_GetMessageType(const void* m);
AppMessages_err_t AppMessages_GetSizeInBytes(const void* m, size_t* len);
AppMessages_err_t AppMessages_WriteBytes(AppMessages_DataAccess* w, const void* m, bool tag);
AppMessages_err_t AppMessages_ProcessRawBytes(AppMessages_DataAccess* r, void*** msgListOut, size_t* len);
AppMessages_err_t AppMessages_ProcessRawBytes(AppMessages_DataAccess* r, int32_t max, void*** msgListOut, size_t* len);
AppMessages_err_t AppMessages_Destroy(void* m);
AppMessages_err_t AppMessages_DestroyMessageList(void** msgList, size_t len);
AppMessages_err_t AppMessages_GetPackedSize(void** msgList, size_t len, size_t* packedSize);
AppMessages_err_t AppMessages_PackMessages(void** msgList, size_t len, AppMessages_DataAccess* w);
AppMessages_err_t AppMessages_UnpackMessages(AppMessages_DataAccess* r, void*** msgListOut, size_t* len);

typedef enum AppMessages_CharacterClass {
AppMessages_CharacterClass_Fighter = 0,
Expand Down Expand Up @@ -130,6 +134,7 @@ AppMessages_err_t AppMessages_Vector3Message_WriteBytes(AppMessages_DataAccess*
AppMessages_err_t AppMessages_Vector3Message_FromBytes(AppMessages_DataAccess* r, AppMessages_Vector3Message* dst);
AppMessages_err_t AppMessages_Vector3Message_GetSizeInBytes(const AppMessages_Vector3Message* m, size_t* size);
AppMessages_Vector3Message* AppMessages_Vector3Message_Create(void);
void AppMessages_Vector3Message_Cleanup(AppMessages_Vector3Message *m);
void AppMessages_Vector3Message_Destroy(AppMessages_Vector3Message *m);


Expand All @@ -154,6 +159,7 @@ AppMessages_err_t AppMessages_NewCharacterMessage_WriteBytes(AppMessages_DataAcc
AppMessages_err_t AppMessages_NewCharacterMessage_FromBytes(AppMessages_DataAccess* r, AppMessages_NewCharacterMessage* dst);
AppMessages_err_t AppMessages_NewCharacterMessage_GetSizeInBytes(const AppMessages_NewCharacterMessage* m, size_t* size);
AppMessages_NewCharacterMessage* AppMessages_NewCharacterMessage_Create(void);
void AppMessages_NewCharacterMessage_Cleanup(AppMessages_NewCharacterMessage *m);
void AppMessages_NewCharacterMessage_Destroy(AppMessages_NewCharacterMessage *m);


Expand All @@ -172,6 +178,7 @@ AppMessages_err_t AppMessages_CharacterJoinedTeam_WriteBytes(AppMessages_DataAcc
AppMessages_err_t AppMessages_CharacterJoinedTeam_FromBytes(AppMessages_DataAccess* r, AppMessages_CharacterJoinedTeam* dst);
AppMessages_err_t AppMessages_CharacterJoinedTeam_GetSizeInBytes(const AppMessages_CharacterJoinedTeam* m, size_t* size);
AppMessages_CharacterJoinedTeam* AppMessages_CharacterJoinedTeam_Create(void);
void AppMessages_CharacterJoinedTeam_Cleanup(AppMessages_CharacterJoinedTeam *m);
void AppMessages_CharacterJoinedTeam_Destroy(AppMessages_CharacterJoinedTeam *m);


Expand Down Expand Up @@ -308,9 +315,10 @@ AppMessages_err_t AppMessages__ReadString(AppMessages_DataAccess *r, char **s, u
if (r->bufferSize < r->position + *len) {
return APPMESSAGES_ERR_EOF;
}
*s = (char*)calloc(1, (size_t)(*len + 1));
*s = (char*)APPMESSAGES_MALLOC((size_t)(*len + 1));
if (*s == NULL) { return APPMESSAGES_ERR_ALLOCATION_FAILURE; }
memcpy(*s, r->buffer + r->position, *len);
(*s)[*len] = '\0';
r->position += *len;
return APPMESSAGES_ERR_OK;
}
Expand Down Expand Up @@ -486,13 +494,16 @@ AppMessages_err_t AppMessages_Destroy(void* m) {
return APPMESSAGES_ERR_INVALID_DATA;
}

AppMessages_err_t AppMessages_ProcessRawBytes(AppMessages_DataAccess* r, void*** msgListDst, size_t* len) {
AppMessages_err_t AppMessages_ProcessRawBytes(AppMessages_DataAccess* r, int32_t max, void*** msgListDst, size_t* len) {
AppMessages_err_t err = APPMESSAGES_ERR_OK;
size_t currCapacity = 8;
*msgListDst = (void**)APPMESSAGES_MALLOC(sizeof(void*) * currCapacity);
if (*msgListDst == NULL) { return APPMESSAGES_ERR_ALLOCATION_FAILURE; }
*len = 0;
while (!AppMessages_IsFinished(r)) {
if (max == 0) {
return APPMESSAGES_ERR_OK;
}
while (!AppMessages_IsFinished(r) && (max < 0 || *len < (size_t)max)) {
while (*len >= currCapacity) {
currCapacity *= 2;
*msgListDst = (void**)APPMESSAGES_REALLOC(*msgListDst, (sizeof(void*) * currCapacity));
Expand Down Expand Up @@ -567,6 +578,105 @@ AppMessages_err_t AppMessages_DestroyMessageList(void** msgList, size_t len) {
return APPMESSAGES_ERR_OK;
}

AppMessages_err_t AppMessages_GetPackedSize(void** msgList, size_t len, size_t* packedSize) {
*packedSize = 0;
for (size_t i = 0; i < len; i++) {
size_t individualSize = 0;
AppMessages_MessageType msgType = AppMessages_GetMessageType(msgList[i]);
switch (msgType) {
case AppMessages_MessageType_Vector3Message:
AppMessages_Vector3Message_GetSizeInBytes((AppMessages_Vector3Message*)msgList[i], &individualSize);
*packedSize += individualSize;
break;
case AppMessages_MessageType_NewCharacterMessage:
AppMessages_NewCharacterMessage_GetSizeInBytes((AppMessages_NewCharacterMessage*)msgList[i], &individualSize);
*packedSize += individualSize;
break;
case AppMessages_MessageType_CharacterJoinedTeam:
AppMessages_CharacterJoinedTeam_GetSizeInBytes((AppMessages_CharacterJoinedTeam*)msgList[i], &individualSize);
*packedSize += individualSize;
break;
case AppMessages_MessageType___NullMessage:
return APPMESSAGES_ERR_INVALID_DATA;
}
}
*packedSize += len;
*packedSize += 9;
return APPMESSAGES_ERR_OK;
}

AppMessages_err_t AppMessages_PackMessages(void** msgList, size_t len, AppMessages_DataAccess* w) {
AppMessages_err_t err = APPMESSAGES_ERR_OK;
if (msgList == NULL) {
return APPMESSAGES_ERR_INVALID_DATA;
}
const char header[] = "BSCI";
memcpy(w->buffer, header, 4);
w->position += 4;
err = AppMessages__WriteUInt32(w, (uint32_t)len);
if (err != APPMESSAGES_ERR_OK) {
return err;
}
for (size_t i = 0; i < len; i++) {
AppMessages_MessageType msgType = AppMessages_GetMessageType(msgList[i]);
switch (msgType) {
case AppMessages_MessageType_Vector3Message:
err = AppMessages_Vector3Message_WriteBytes(w, (AppMessages_Vector3Message*)msgList[i], true);
if (err != APPMESSAGES_ERR_OK) {
return err;
}
break;
case AppMessages_MessageType_NewCharacterMessage:
err = AppMessages_NewCharacterMessage_WriteBytes(w, (AppMessages_NewCharacterMessage*)msgList[i], true);
if (err != APPMESSAGES_ERR_OK) {
return err;
}
break;
case AppMessages_MessageType_CharacterJoinedTeam:
err = AppMessages_CharacterJoinedTeam_WriteBytes(w, (AppMessages_CharacterJoinedTeam*)msgList[i], true);
if (err != APPMESSAGES_ERR_OK) {
return err;
}
break;
case AppMessages_MessageType___NullMessage:
return APPMESSAGES_ERR_INVALID_DATA;
}
}
AppMessages__WriteUInt8(w, 0);

return APPMESSAGES_ERR_OK;
}

AppMessages_err_t AppMessages_UnpackMessages(AppMessages_DataAccess* r, void*** msgListOut, size_t* len) {
AppMessages_err_t err = APPMESSAGES_ERR_OK;
if (memcmp(r->buffer, "BSCI", 4) != 0) {
return APPMESSAGES_ERR_INVALID_DATA;
}
r->position += 4;
uint32_t msgCount;
err = AppMessages__ReadUInt32(r, &msgCount);
if (err != APPMESSAGES_ERR_OK) {
return err;
}
err = AppMessages_ProcessRawBytes(r, (int32_t)msgCount, msgListOut, len);
if (err != APPMESSAGES_ERR_OK) {
return err;
}
if (len == 0) {
APPMESSAGES_FREE(*msgListOut);
*msgListOut = NULL;
return APPMESSAGES_ERR_INVALID_DATA;
}
if (*len != (size_t)msgCount) {
AppMessages_DestroyMessageList(*msgListOut, *len);
*msgListOut = NULL;
*len = 0;
return APPMESSAGES_ERR_INVALID_DATA;
}

return APPMESSAGES_ERR_OK;
}

bool AppMessages_IsValidCharacterClass(uint8_t value) {
switch (value) {
case AppMessages_CharacterClass_Fighter:
Expand Down Expand Up @@ -701,7 +811,11 @@ AppMessages_Vector3Message* AppMessages_Vector3Message_Create(void) {
return out;
}

void AppMessages_Vector3Message_Cleanup(AppMessages_Vector3Message *m) {
}

void AppMessages_Vector3Message_Destroy(AppMessages_Vector3Message *m) {
AppMessages_Vector3Message_Cleanup(m);
APPMESSAGES_FREE(m);
}

Expand All @@ -726,7 +840,7 @@ AppMessages_err_t AppMessages_Vector3Message_FromBytes(AppMessages_DataAccess* r
AppMessages_err_t AppMessages_Vector3Message_WriteBytes(AppMessages_DataAccess* w, const AppMessages_Vector3Message* src, bool tag) {
AppMessages_err_t err;
if (tag) {
err = AppMessages__WriteUInt8(w, (const uint8_t)(src->_mt));
err = AppMessages__WriteUInt8(w, (uint8_t)(src->_mt));
if (err != APPMESSAGES_ERR_OK) {
return err;
}
Expand Down Expand Up @@ -788,13 +902,17 @@ AppMessages_NewCharacterMessage* AppMessages_NewCharacterMessage_Create(void) {
return out;
}

void AppMessages_NewCharacterMessage_Destroy(AppMessages_NewCharacterMessage *m) {
void AppMessages_NewCharacterMessage_Cleanup(AppMessages_NewCharacterMessage *m) {
APPMESSAGES_FREE(m->characterName);
for (uint16_t i1 = 0; i1 < m->nicknames_len; i1++) {
APPMESSAGES_FREE(m->nicknames[i1]);
}
APPMESSAGES_FREE(m->nicknames_els_len);
APPMESSAGES_FREE(m->nicknames);
}

void AppMessages_NewCharacterMessage_Destroy(AppMessages_NewCharacterMessage *m) {
AppMessages_NewCharacterMessage_Cleanup(m);
APPMESSAGES_FREE(m);
}

Expand Down Expand Up @@ -861,7 +979,7 @@ AppMessages_err_t AppMessages_NewCharacterMessage_FromBytes(AppMessages_DataAcce
AppMessages_err_t AppMessages_NewCharacterMessage_WriteBytes(AppMessages_DataAccess* w, const AppMessages_NewCharacterMessage* src, bool tag) {
AppMessages_err_t err;
if (tag) {
err = AppMessages__WriteUInt8(w, (const uint8_t)(src->_mt));
err = AppMessages__WriteUInt8(w, (uint8_t)(src->_mt));
if (err != APPMESSAGES_ERR_OK) {
return err;
}
Expand Down Expand Up @@ -943,9 +1061,13 @@ AppMessages_CharacterJoinedTeam* AppMessages_CharacterJoinedTeam_Create(void) {
return out;
}

void AppMessages_CharacterJoinedTeam_Destroy(AppMessages_CharacterJoinedTeam *m) {
void AppMessages_CharacterJoinedTeam_Cleanup(AppMessages_CharacterJoinedTeam *m) {
APPMESSAGES_FREE(m->teamName);
APPMESSAGES_FREE(m->teamColors);
}

void AppMessages_CharacterJoinedTeam_Destroy(AppMessages_CharacterJoinedTeam *m) {
AppMessages_CharacterJoinedTeam_Cleanup(m);
APPMESSAGES_FREE(m);
}

Expand Down Expand Up @@ -990,7 +1112,7 @@ AppMessages_err_t AppMessages_CharacterJoinedTeam_FromBytes(AppMessages_DataAcce
AppMessages_err_t AppMessages_CharacterJoinedTeam_WriteBytes(AppMessages_DataAccess* w, const AppMessages_CharacterJoinedTeam* src, bool tag) {
AppMessages_err_t err;
if (tag) {
err = AppMessages__WriteUInt8(w, (const uint8_t)(src->_mt));
err = AppMessages__WriteUInt8(w, (uint8_t)(src->_mt));
if (err != APPMESSAGES_ERR_OK) {
return err;
}
Expand Down
Loading

0 comments on commit c7a3900

Please sign in to comment.