cocos2dx使用tolua关于字符串处理的一个问题

正在使用cocos2dx的tolua binding在此过程中发现的一个问题。假设一回或输入是std::string当我们不同意包括二进制数据,和std::string我同意,这样一来就导致了不正确的使用等。这可能会导致一系列的问题,特别是,我们需要使用std::string议信息的时候。

造成问题的解决办法是tolua生成的代码对于std::string都没有添加length參数,这样就造成了一旦是二进制数据而且遇到\0就会被截断。

改动的办法事实上也非常easy,仅仅须要改动一下basic.lua脚本,添加例如以下代码就可以

index fac0c7a..4c184c7 100644

--- a/projects/LastMile/Classes/lua/basic.lua

+++ b/projects/LastMile/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([[tolua_pushcppstring(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;

实际上它会随着内部cppstring有关push和tostring哪些变化

版权声明:本文博客原创文章,博客,未经同意,不得转载。

上一篇:dbgview 在windows 10 中关闭后再次打开时无法“capture kernel”


下一篇:Beginning Scala study note(9) Scala and Java Interoperability