Coder Social home page Coder Social logo

godot-msgpack's Introduction

godot-msgpack

A MessagePack serializer implemented in pure GDScript

  • No dependency
  • No native binding required

Installation

Copy the msgpack.gd at the root of the repository into your godot project

Usage

var Msgpack = preload("res://msgpack.gd")

func _myfunc():
    var data = {"type": "wizard", "attack": 10, "weapon": ["dagger", "staff"]}

    var res = Msgpack.encode(data)
    print(res.result) # A PoolByteArray of the data encoded MessagePack

    var res2 = Msgpack.decode(res.result)
    print(res2.result) # Get back the original data

class Msgpack

static Dictionary encode(Variant value)

Convert a value (number, string, array and dictionary) into their counterparts in messagepack. Returns dictionary with three fields: result which is the packed data (a PoolByteArray); error which is the error code; and error_string which is a human readable error message

static Dictionary decode(PoolByteArray bytes)

Convert a packed data (a PoolByteArray) back into a value, the reverse of the encode function. The return value is similar to the one in the encode method

Limitation

  • Only support null, boolean, integer, float, PoolByteArray, string, array and dictionary. No support for other data type like Vector2 and Vector3
  • No support for the ext datatype in MessagePack
  • Slow compare to the built-in binary serialization in godot

Testcases

godot -s run_test.gd

godot-msgpack's People

Contributors

xtpor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

godot-msgpack's Issues

License

Would you mind adding a license to your project? Without a license, no one is legally allowed to add your library to their project.

128 converts to -128; Problem with signed integers

There seems to be some trouble with signed and unsigned on integers.
I see you only use signed integers for encoding. But if the number is 128 or 32768 it seems to wrap around and i get -128 and -32768.

This is in Godot 4 actually, but the bug also exists in godot 3.
I changed the int-section on func _encode to fix it:

	TYPE_INT:
		if value >= 0:
			if value < 128:
				buf.put_u8(value)
			elif value <= 0xff :
				buf.put_u8(0xcc)
				buf.put_u8(value)
			elif value <= 0xffff:
				buf.put_u8(0xcd)
				buf.put_u16(value)
			elif value <= 0xffffffff:
				buf.put_u8(0xce)
				buf.put_u32(value)
			else:
				buf.put_u8(0xcf)
				buf.put_u64(value)
			
		else:
			if value >= -32 :
				buf.put_u8(0xe0 + (value + 32))
			elif value >= -128 :
				buf.put_u8(0xd0)
				buf.put_8(value)
			elif value >= -32768 :
				buf.put_u8(0xd1)
				buf.put_16(value)
			elif value >= -2147483648 :
				buf.put_u8(0xd2)
				buf.put_32(value)
			else:
				buf.put_u8(0xd3)
				buf.put_64(value)

I quickly tested it and it seems to work good so far.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.