Tolua used in cocos2dx has a problem about string processing.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.