diff --git a/README.md b/README.md index bd21740..7255d9c 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,36 @@ # Grechabuf -Binary data (de)serializer, highly inspired by [Tsoding's multiplayer prototype](https://github.com/tsoding/multiplayer-game-prototype) +binary data (de)serializer, highly inspired by [Tsoding's multiplayer prototype](https://github.com/tsoding/multiplayer-game-prototype) -## why didnt you use the Protobuf!??? - - Protobuf is bloated dependency - - Grechabuf is singlefile .ts/.js to use - - Grechabuf is extandable +```ts +// just declare struct +const PersonStruct = grechabuf.createStruct({ + name: grechabuf.string(), + roles: grechabuf.array( + grechabuf.string() + ), + health: grechabuf.i8(), + food: grechabuf.i8(), + + lostBrainCells: grechabuf.u32(), +}) + +// and just serialize! +const buffer = PersonStruct.serialize({ + name: "John Doe", + roles: ["Admin", "TsodingSessionsEnjoyer"], + health: 1, + food: 1, + moving: 1, + lostBrainCells: 69420 +}) +``` + +## why don't use my favourite Protobuf!?!?? + - Protobuf is bloated dependency _(its like opening json in Visual Studio)_ + - Grechabuf is singlefile .ts/.cjs/.mjs to use + - Grechabuf is easy to extand [(see custom fields)](#custom-fields) + - Grechabuf has TypeScript support ## installation - With NPM (with bundler on web) @@ -18,23 +43,28 @@ Binary data (de)serializer, highly inspired by [Tsoding's multiplayer prototype] - If you using Javascript: go to [`/dist`](./dist/) folder and grab here .cjs/.mjs files - If you using Typescript: grab [`grachabuf.ts`](./grechabuf.ts) and copy it to your project +## examples +[(go to examples/)](examples/) + ## custom fields ```ts +type Vector2 = { x: number, y: number } -// javascript type ---vvvvvv -export const fooBar = (): Field => { +// deserialized type ---vvvvvvv +const vector2 = (): Field => { return { - // aproximated size - size(_view, _position) { - return 1 + // calculate size for certain value (useful for strings/arrays/other dynamic values) + size(value) { + return 4 + 4 }, // serialization // takes DataView, position (byteOffset) and value (javascript value) // returns count of written bytes serialize(view, position, value) { - view.setInt8(position, value) - return 1 + view.setFloat32(position, value.x) + view.setFloat32(position + 4, value.y) + return 4 + 4 }, // deserialization @@ -43,10 +73,13 @@ export const fooBar = (): Field => { deserialize(view, position) { return { // deserialized data - data: view.getInt8(position), + data: { + x: view.getFloat32(position), + y: view.getFloat32(position + 4) + }, // count of read bytes - length: 1 + length: 4 + 4 } } } @@ -56,6 +89,10 @@ export const fooBar = (): Field => { // And then use it! const SomeStruct = grechabuf.createStruct({ - someField: fooBar() + someField: vector2() +}) + +SomeStruct.serialize({ + someField: { x: 123.4, y: 567.8 } }) ``` diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..c4f6e88 --- /dev/null +++ b/TODO.md @@ -0,0 +1,7 @@ +## todos + +- [ ] add tests (with Jest) +- [ ] add boolean +- [ ] add bigint support +- [ ] add enums (or bitmasks) +- [ ] overload `deserialize` with supporting both DataView and ArrayBuffer