This is a buffer parser inspired by the Node.JS "binary-parser" package.
By using it, you can create templates to parse your buffers and can be extremely useful when handling network messages.
::HOW TO USE IT::
First, you need to create the required parser templates:
var header_parser = new parser()._uint8("command");
var join_game_parser = new
parser()._skip(1)._array("conditions",5,"bool")._string("room")._uint16("posx")._uint16("posy")._string("username");
var login_parser = new parser()._skip(1)._strictstring("username")._strictstring("password");
(you can add as many parsing elements as you need)
(the endian element will set the endianess for all the following 2-more bytes numbers read and will require 1 argument - true for little endian and false for big endian)
(as default, GameMaker will write buffers using the little endian format)
Then you can call the "parse(buffer)" function to get the result object:
var result = join_game_parser.parse(buf);
var result = login_parser.parse(buf);
Results:
{ room : "rm_first_map", conditions : [ 1,1,1,0,0 ], posx : 12458, posy : 3596, username : "player 1", _valid : 1 }
{ password : "mysecretpassword", username : "player 1", _valid : 1 }
The difference between "strictstring" and "string" is that "strictstring" will only accept "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,./\<>?;':\"|[]{}[email protected]#$%&()-=_+áÁéÉíÍóÓúÚàÀèÈìÌòÒùÙäÄëËïÏöÖüÜâÂêÊîÎôÔûÛãÃõÕ " as valid characters and will return an invalid object ("_valid = false") if any invalid character is found.
The valid elements are:
_endian(true/false)
_skip(amount)
_string(name,charlimit)
_strictstring(name,charlimit)
_uint8(name)
_uint16(name)
_uint32(name)
_sint8(name)
_sint16(name)
_sint32(name)
_sint64(name)
_float16(name)
_float32(name)
_float64(name)
_bool(name)
_array(name,length,datatype,stringbufferlimit or -1,_surfw if surface,_surfh if surface){
_buffer(name,size or -1) **new**
_surface(name,surfw,surfh) **new**
Updates:
**0.0.2: Huge update!**
You can now parse buffers and surfaces.
You can now use previously read elements as a valid value for array size, string limit, buffer size, surface width and
surface height. (*p = new parser()._uint16("surfw")._uint16("surfh")._surface("element","surfw","surfh"); //SURFACE WILL USE THE READ VALUES FROM "SURFW" AND "SURFH" AS PARAMETERES*)
**0.0.1:**
Parsed object now returns the "_position", showing the next byte to be read on the buffer
End User Licence Agreement (EULA).
Huge update - added support for named variables and buffer and surface as elements