A problem was found during tolua binding using cocos2dx. If the returned or input is STD: String, binary data cannot be included, while STD: string is allowed, this leads to a series of problems, especially when we need to use STD: string to transmit some protocol information.
The cause is that the length parameter is not added to the Code generated by tolua for STD: string, which causes the code to be truncated once binary data is generated and \ 0 is encountered. The modification method is actually very simple. You just need to modify the basic. Lua script and add the following code.
Index fac0c7a... 4c184c7 100644
--- A/projects/law.ile/classes/Lua/basic. Lua
++ B/projects/law.ile/classes/Lua/basic. Lua
@-307,7 + 307,16 @ extern "C "{
Using namespace cocos2d;
Using namespace cocos2d: extension;
-Using namespace cocosdenshion;])
+ Using namespace cocosdenshion;
+
+ Static STD: String bt_tocppstring (lua_state * tolua_s, int NARG, const char * DEF ){
+ If (lua_gettop (tolua_s) <ABS (NARG )){
+ Return "";
+}
+ Size_t length = 0;
+ Const char * Data = lua_tolstring (tolua_s, NARG, & length );
+ Return STD: string (data, length );
+}])
Replace ([[/* exported function */
Tolua_api int tolua_cocos2d_open (lua_state * tolua_s);], [[])
@-323,6 + 332,10 @ tolua_api int tolua_cocos2d_open (lua_state * tolua_s);], [[])
Replace ([[tolua_usertype (tolua_s, "lua_function");], [[])
+ Replace ([[tolua_tocppstring], [[bt_tocppstring])
+
+ Replace ([partition (tolua_s, (const char *) tolua_ret);], [[lua_pushlstring (tolua_s, tolua_ret.c_str (), tolua_ret.size ();])
+
Replace ([[toluafix_pushusertype_ccobject (tolua_s, (void *) tolua_ret],
[[Int nid = (tolua_ret )? (INT) tolua_ret-> m_uid:-1;
Int * pluaid = (tolua_ret )? & Tolua_ret-> m_nluaid: NULL;
In fact, we need to modify the push and tostring related to cppstring.