diff --git a/cocos/scripting/lua/bindings/LuaBasicConversions.cpp b/cocos/scripting/lua/bindings/LuaBasicConversions.cpp index 6e96f48d6b..ad7bdeb666 100644 --- a/cocos/scripting/lua/bindings/LuaBasicConversions.cpp +++ b/cocos/scripting/lua/bindings/LuaBasicConversions.cpp @@ -1425,14 +1425,81 @@ bool luaval_to_ccvaluevector(lua_State* L, int lo, cocos2d::ValueVector* ret) bool luaval_to_std_vector_string(lua_State* L, int lo, std::vector* ret) { - // TO BE DONE IN CPP FILE - return false; + if (nullptr == L || nullptr == ret || lua_gettop(L) < lo) + return false; + + tolua_Error tolua_err; + bool ok = true; + if (!tolua_istable(L, lo, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + size_t len = lua_objlen(L, lo); + std::string value = ""; + for (int i = 0; i < len; i++) + { + lua_pushnumber(L, i + 1); + lua_gettable(L,lo); + if(lua_isstring(L, -1)) + { + ok = luaval_to_std_string(L, -1, &value); + if(ok) + ret->push_back(value); + } + else + { + CCASSERT(false, "string type is needed"); + } + + lua_pop(L, 1); + } + } + + return ok; } bool luaval_to_std_vector_int(lua_State* L, int lo, std::vector* ret) { - // TO BE DONE IN CPP FILE - return false; + if (nullptr == L || nullptr == ret || lua_gettop(L) < lo) + return false; + + tolua_Error tolua_err; + bool ok = true; + if (!tolua_istable(L, lo, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + size_t len = lua_objlen(L, lo); + for (int i = 0; i < len; i++) + { + lua_pushnumber(L, i + 1); + lua_gettable(L,lo); + if(lua_isnumber(L, -1)) + { + ret->push_back((int)tolua_tonumber(L, -1, 0)); + } + else + { + CCASSERT(false, "int type is needed"); + } + + lua_pop(L, 1); + } + } + + return ok; } void point_to_luaval(lua_State* L,const Point& pt) diff --git a/cocos/scripting/lua/bindings/LuaBasicConversions.h b/cocos/scripting/lua/bindings/LuaBasicConversions.h index fe7f05b091..feaf396f70 100644 --- a/cocos/scripting/lua/bindings/LuaBasicConversions.h +++ b/cocos/scripting/lua/bindings/LuaBasicConversions.h @@ -120,8 +120,47 @@ bool luaval_to_std_vector_int(lua_State* L, int lo, std::vector* ret); template bool luaval_to_ccmap_string_key(lua_State* L, int lo, cocos2d::Map* ret) { - // TO BE DONE: - return false; + if(nullptr == L || nullptr == ret || lua_gettop(L) < lo) + return false; + + tolua_Error tolua_err; + bool ok = true; + if (!tolua_istable(L, lo, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + std::string stringKey = ""; + lua_pushnil(L); /* first key L: lotable ..... nil */ + while ( 0 != lua_next(L, lo ) ) /* L: lotable ..... key value */ + { + if (!lua_isstring(L, -2)) + { + lua_pop(L, 1); /* removes 'value'; keep 'key' for next iteration*/ + continue; + } + + if (lua_isnil(L, -1) || !lua_isuserdata(L, -1)) + { + lua_pop(L, 1); + continue; + } + + luaval_to_std_string(L, -2, &stringKey); + T obj = static_cast(tolua_tousertype(L, -1, NULL) ); + if (nullptr != obj) + ret->insert(stringKey, obj); + + lua_pop(L, 1); /* L: lotable ..... key */ + } + } + + return ok; } @@ -179,7 +218,29 @@ void ccvector_to_luaval(lua_State* L,const cocos2d::Vector& inValue) template void ccmap_string_key_to_luaval(lua_State* L, const cocos2d::Map& v) { - // TO BE DONE: + lua_newtable(L); + + if(nullptr == L) + return; + + for (auto iter = v.begin(); iter != v.end(); ++iter) + { + std::string key = iter->first; + T obj = iter->second; + if (nullptr != dynamic_cast(obj)) + { + std::string name = typeid(*obj).name(); + auto typeIter = g_luaType.find(name); + if (g_luaType.end() != typeIter) + { + lua_pushstring(L, name.c_str()); + int ID = (obj) ? (int)obj->_ID : -1; + int* luaID = (obj) ? &obj->_luaID : NULL; + toluafix_pushusertype_ccobject(L, ID, luaID, (void*)obj,typeIter->second.c_str()); + lua_rawset(L, -3); + } + } + } } void ccvalue_to_luaval(lua_State* L,const cocos2d::Value& inValue);