Skip to content

Commit

Permalink
fix: fixed README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Mon4ik committed Aug 13, 2024
1 parent 622c706 commit 97ebed9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
67 changes: 52 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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<number> => {
// deserialized type ---vvvvvvv
const vector2 = (): Field<Vector2> => {
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
Expand All @@ -43,10 +73,13 @@ export const fooBar = (): Field<number> => {
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
}
}
}
Expand All @@ -56,6 +89,10 @@ export const fooBar = (): Field<number> => {
// And then use it!
const SomeStruct = grechabuf.createStruct({
someField: fooBar()
someField: vector2()
})
SomeStruct.serialize({
someField: { x: 123.4, y: 567.8 }
})
```
7 changes: 7 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 97ebed9

Please sign in to comment.