Skip to content

Commit ac50ae6

Browse files
committed
Add sourcepawn files.
1 parent cc801a3 commit ac50ae6

File tree

2 files changed

+265
-0
lines changed

2 files changed

+265
-0
lines changed

sourcepawn/msgpack-tests.sp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
This is a plugin to test the C++ extension. You don't need this use the extension.
3+
*/
4+
5+
public Plugin:myinfo =
6+
{
7+
name = "Message Pack Tests",
8+
author = "Bottiger",
9+
description = "",
10+
version = "1.0",
11+
url = "http://skial.com"
12+
};
13+
14+
#include <msgpack>
15+
16+
public OnPluginStart() {
17+
TestPack1();
18+
TestPack2();
19+
}
20+
21+
stock bool:BufferEqual(length, const String:b1[], const String:b2[]) {
22+
for(new i=0;i<length;i++) {
23+
if(b1[i] != b2[i])
24+
return false;
25+
}
26+
return true;
27+
}
28+
29+
TestPack1() {
30+
PrintToServer("Test 1 start");
31+
32+
new Handle:pack = MsgPack_NewPack();
33+
34+
MsgPack_PackArray(pack, 4);
35+
MsgPack_PackInt(pack, 1);
36+
MsgPack_PackInt(pack, -1);
37+
MsgPack_PackInt(pack, 65536);
38+
MsgPack_PackString(pack, "foobar");
39+
40+
new size = MsgPack_GetPackSize(pack);
41+
PrintToServer("Size Good: %i", size == 15);
42+
43+
decl String:dumped[size];
44+
MsgPack_GetPackBuffer(pack, dumped, size);
45+
CloseHandle(pack);
46+
PrintToServer("Content Good: %i", BufferEqual(size, dumped, "\x94\x01\xff\xce\x00\x01\x00\x00\xa6foobar"));
47+
48+
new offset;
49+
new Handle:root = MsgPack_NewUnpack(dumped, size, offset);
50+
// zero out buffer test
51+
for(new i=0;i<size;i++) {
52+
dumped[i] = 255;
53+
}
54+
PrintToServer("Offset Good: %i", offset == size);
55+
56+
// debug print test
57+
MsgPack_UnpackPrint(root);
58+
59+
// Test Unpacking
60+
PrintToServer("root Type Good: %i Size Good %i",
61+
MsgPack_UnpackType(root) == MSGPACK_OBJECT_ARRAY,
62+
MsgPack_UnpackCount(root) == 4);
63+
64+
new Handle:e = MsgPack_UnpackArray(root, 0);
65+
PrintToServer("0 Type Good: %i Size Good %i",
66+
MsgPack_UnpackType(e) == MSGPACK_OBJECT_POSITIVE_INTEGER,
67+
MsgPack_UnpackInt(e) == 1);
68+
CloseHandle(e);
69+
70+
e = MsgPack_UnpackArray(root, 1);
71+
PrintToServer("1 Type Good: %i Size Good %i",
72+
MsgPack_UnpackType(e) == MSGPACK_OBJECT_NEGATIVE_INTEGER,
73+
MsgPack_UnpackInt(e) == -1);
74+
CloseHandle(e);
75+
76+
e = MsgPack_UnpackArray(root, 2);
77+
PrintToServer("2 Type Good: %i Size Good %i",
78+
MsgPack_UnpackType(e) == MSGPACK_OBJECT_POSITIVE_INTEGER,
79+
MsgPack_UnpackInt(e) == 65536);
80+
CloseHandle(e);
81+
82+
83+
e = MsgPack_UnpackArray(root, 3);
84+
decl String:s[64];
85+
MsgPack_UnpackString(e, s, sizeof(s));
86+
PrintToServer("3 Type Good: %i Size Good %i",
87+
MsgPack_UnpackType(e) == MSGPACK_OBJECT_RAW,
88+
StrEqual(s, "foobar"));
89+
CloseHandle(e);
90+
91+
CloseHandle(root);
92+
PrintToServer("Test 1 done");
93+
}
94+
95+
TestPack2() {
96+
PrintToServer("Test 2 start");
97+
new Handle:pack = MsgPack_NewPack();
98+
MsgPack_PackArray(pack, 4);
99+
MsgPack_PackNil(pack);
100+
MsgPack_PackFalse(pack);
101+
MsgPack_PackTrue(pack);
102+
MsgPack_PackMap(pack, 2);
103+
MsgPack_PackTrue(pack);
104+
MsgPack_PackFalse(pack);
105+
MsgPack_PackString(pack, "foo");
106+
MsgPack_PackFloat(pack, 1.2342);
107+
108+
new size = MsgPack_GetPackSize(pack);
109+
decl String:dumped[size];
110+
MsgPack_GetPackBuffer(pack, dumped, size);
111+
CloseHandle(pack);
112+
113+
new Handle:root = MsgPack_NewUnpack(dumped, size);
114+
MsgPack_UnpackPrint(root);
115+
116+
new Handle:map = MsgPack_UnpackArray(root, 3);
117+
new Handle:k = MsgPack_UnpackKey(map, 1);
118+
new Handle:v = MsgPack_UnpackValue(map, 1);
119+
120+
decl String:key[64];
121+
MsgPack_UnpackString(k, key, sizeof(key));
122+
new Float:value = MsgPack_UnpackFloat(v);
123+
PrintToServer("Key foo=%s Value 1.2342=%f", key, value);
124+
125+
CloseHandle(root);
126+
CloseHandle(map);
127+
CloseHandle(k);
128+
CloseHandle(v);
129+
130+
PrintToServer("Test 2 done");
131+
}

sourcepawn/msgpack.inc

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#if defined _msgpack_included
2+
#endinput
3+
#endif
4+
#define _msgpack_included
5+
6+
enum MsgPack_Type {
7+
MSGPACK_OBJECT_NIL = 0x00,
8+
MSGPACK_OBJECT_BOOLEAN = 0x01,
9+
MSGPACK_OBJECT_POSITIVE_INTEGER = 0x02,
10+
MSGPACK_OBJECT_NEGATIVE_INTEGER = 0x03,
11+
MSGPACK_OBJECT_DOUBLE = 0x04,
12+
MSGPACK_OBJECT_RAW = 0x05,
13+
MSGPACK_OBJECT_ARRAY = 0x06,
14+
MSGPACK_OBJECT_MAP = 0x07,
15+
};
16+
17+
/*
18+
* Packing Functions
19+
*/
20+
21+
// create a pack handle to pack with
22+
native Handle:MsgPack_NewPack();
23+
24+
native MsgPack_PackInt(Handle:pack, value);
25+
native MsgPack_PackFloat(Handle:pack, Float:value);
26+
native MsgPack_PackNil(Handle:pack);
27+
native MsgPack_PackTrue(Handle:pack);
28+
native MsgPack_PackFalse(Handle:pack);
29+
native MsgPack_PackArray(Handle:pack, size);
30+
native MsgPack_PackMap(Handle:pack, size);
31+
32+
// pack some bytes. does not null terminate
33+
native MsgPack_PackRaw(Handle:pack, const String:buffer[], buffer_length);
34+
35+
stock MsgPack_PackString(Handle:pack, const String:null_terminated_string[]) {
36+
return MsgPack_PackRaw(pack, null_terminated_string, strlen(null_terminated_string));
37+
}
38+
39+
// get the pack length in bytes
40+
native MsgPack_GetPackSize(Handle:pack);
41+
42+
// get the buffer so you can send it somewhere
43+
native MsgPack_GetPackBuffer(Handle:pack, const String:buffer[], buffer_length);
44+
45+
/*
46+
* Unpacking Functions
47+
*/
48+
49+
// create a messagepack object handle.
50+
// offset is the number of bytes consumed
51+
// enabling save_buffer copies the buffer so objects still work properly when the buffer goes out of scope
52+
// the buffer is shared between all objects that come from here and will be deleted when they are all closed
53+
native Handle:MsgPack_NewUnpack(const String:buffer[], buffer_length, &offset=0, bool:save_buffer=true);
54+
55+
56+
// pretty print object to console for debugging
57+
native MsgPack_UnpackPrint(Handle:obj);
58+
59+
// get the object type
60+
native MsgPack_Type:MsgPack_UnpackType(Handle:obj);
61+
62+
// get the number of elements for maps, arrays, and raws. returns 0 if not one of those types
63+
native MsgPack_UnpackCount(Handle:obj);
64+
65+
native bool:MsgPack_UnpackBool(Handle:obj);
66+
native MsgPack_UnpackInt(Handle:obj);
67+
native Float:MsgPack_UnpackFloat(Handle:obj);
68+
native MsgPack_UnpackRaw(Handle:obj, String:buffer[], buffer_length);
69+
stock MsgPack_UnpackString(Handle:obj, String:buffer[], buffer_length) {
70+
new written = MsgPack_UnpackRaw(obj, buffer, buffer_length);
71+
if(written-1 >= buffer_length)
72+
buffer[buffer_length] = 0;
73+
else
74+
buffer[written] = 0;
75+
76+
return written;
77+
}
78+
native Handle:MsgPack_UnpackArray(Handle:obj, index);
79+
native Handle:MsgPack_UnpackKey(Handle:obj, index);
80+
native Handle:MsgPack_UnpackValue(Handle:obj, index);
81+
native Handle:MsgPack_UnpackNext(Handle:obj);
82+
native any:MsgPack_UnpackArrayCell(Handle:obj, index);
83+
84+
////////////////////////////////////////////////// old functions
85+
#pragma deprecated Renamed Object to Unpack
86+
native MsgPack_ObjectPrint(Handle:obj);
87+
88+
#pragma deprecated Renamed Object to Unpack
89+
native MsgPack_Type:MsgPack_ObjectType(Handle:obj);
90+
91+
#pragma deprecated Renamed Object to Unpack
92+
native MsgPack_ObjectCount(Handle:obj);
93+
94+
#pragma deprecated Renamed Object to Unpack
95+
native bool:MsgPack_ObjectBool(Handle:obj);
96+
97+
#pragma deprecated Renamed Object to Unpack
98+
native MsgPack_ObjectInt(Handle:obj);
99+
100+
#pragma deprecated Renamed Object to Unpack
101+
native Float:MsgPack_ObjectFloat(Handle:obj);
102+
103+
#pragma deprecated Renamed Object to Unpack
104+
native MsgPack_ObjectRaw(Handle:obj, String:buffer[], buffer_length);
105+
106+
#pragma deprecated Renamed Object to Unpack
107+
stock MsgPack_ObjectString(Handle:obj, String:buffer[], buffer_length) {
108+
return MsgPack_UnpackString(obj, buffer, buffer_length);
109+
}
110+
111+
#pragma deprecated Renamed Object to Unpack
112+
native Handle:MsgPack_ObjectArray(Handle:obj, index);
113+
114+
#pragma deprecated Renamed Object to Unpack
115+
native Handle:MsgPack_ObjectKey(Handle:obj, index);
116+
117+
#pragma deprecated Renamed Object to Unpack
118+
native Handle:MsgPack_ObjectValue(Handle:obj, index);
119+
120+
public Extension:__ext_MsgPack =
121+
{
122+
name = "msgpack",
123+
file = "msgpack.ext",
124+
#if defined AUTOLOAD_EXTENSIONS
125+
autoload = 1,
126+
#else
127+
autoload = 0,
128+
#endif
129+
#if defined REQUIRE_EXTENSIONS
130+
required = 1,
131+
#else
132+
required = 0,
133+
#endif
134+
}

0 commit comments

Comments
 (0)