Beispiel #1
0
int LuaV6Chat3App::GetUserList(lua_State *luaState)
{
	V6Chat3App *App = GetV6Chat3App(luaState);
	if (App)
	{
		UserList *list = UserList::Instance();

		lua_newtable(luaState);       //创建一个表格,放在栈顶
		int i=1;
		map<unsigned int, Handle<UserInfo>>& users = list->GetUsers();
		for(UserList::UserIter iter=users.begin(); iter!=users.end(); ++iter, ++i)
		{
			lua_pushnumber(luaState, i);
			lua_newtable(luaState);      //压入value,也是一个table(subtable)
			lua_pushstring(luaState, "groupname");
			lua_pushstring(luaState, V6Util::UnicodeToUtf8(iter->second->group_name.c_str()).c_str());
			lua_settable(luaState,-3);   //弹出key,value,并设置到subtable里面去
			lua_pushstring(luaState, "id");
			lua_pushnumber(luaState, iter->second->uid);
			lua_settable(luaState,-3);   //弹出key,value,并设置到subtable里面去
			lua_pushstring(luaState, "nickname");
			lua_pushstring(luaState, V6Util::UnicodeToUtf8(iter->second->nick_name.c_str()).c_str());
			lua_settable(luaState,-3);   //弹出key,value,并设置到subtable里面去
			lua_pushstring(luaState, "location");
			wstring location;
			iter->second->GetLocation(location);
			lua_pushstring(luaState, V6Util::UnicodeToUtf8(location.c_str()).c_str());
			lua_settable(luaState,-3);   //弹出key,value,并设置到subtable里面去
			wstring headImagePath(USERHEAD_RELATIVE_PATH);
			wstringstream sid;
			sid<<iter->second->uid;
			headImagePath = headImagePath + L"\\" + sid.str();
			XL_BITMAP_HANDLE bmp = V6Util::GetUserDataImageHandle(headImagePath.c_str());
			if(bmp)
			{
				lua_pushstring(luaState, "head");
				if(!XLUE_PushBitmap(luaState, bmp))
			        lua_pushnil(luaState);
			    lua_settable(luaState,-3);   //弹出key,value,并设置到subtable里面去
			}
			lua_settable(luaState,-3);//这时候父table的位置还是-3,弹出key,value(subtable),并设置到table里去
		}

		return 1;
	}

	lua_pushnil(luaState);
	return 1;
}