LuaEngine: Support for Binary Content

This commit is contained in:
Syping 2021-08-09 15:21:58 +02:00
parent 7603ed4160
commit ad194edbba

View file

@ -272,7 +272,7 @@ void LuaEngine::pushVariant(const QVariant &variant)
void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
{
if (variant.type() == QVariant::Bool) {
lua_pushboolean(L_p, (int)variant.toBool());
lua_pushboolean(L_p, static_cast<int>(variant.toBool()));
}
else if (variant.type() == QVariant::Int) {
lua_pushinteger(L_p, variant.toInt());
@ -280,22 +280,28 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
else if (variant.type() == QVariant::Double) {
lua_pushnumber(L_p, variant.toDouble());
}
else if (variant.type() == QVariant::ByteArray) {
const QByteArray luaBArray = variant.toByteArray();
lua_pushlstring(L_p, luaBArray.data(), luaBArray.size());
}
else if (variant.type() == QVariant::String) {
lua_pushstring(L_p, variant.toString().toUtf8().data());
const QByteArray luaBArray = variant.toString().toUtf8();
lua_pushlstring(L_p, luaBArray.data(), luaBArray.size());
}
else if (variant.type() == QVariant::StringList) {
QStringList stringList = variant.toStringList();
const QStringList stringList = variant.toStringList();
lua_createtable(L_p, 0, stringList.count());
int currentId = 1;
for (const QString &string : qAsConst(stringList)) {
for (const QString &string : stringList) {
lua_pushinteger(L_p, currentId);
lua_pushstring(L_p, string.toUtf8().data());
const QByteArray luaBArray = string.toUtf8();
lua_pushlstring(L_p, luaBArray.data(), luaBArray.size());
lua_settable(L_p, -3);
currentId++;
}
}
else if ((QMetaType::Type)variant.type() == QMetaType::QVariantList) {
QVariantList variantList = variant.toList();
else if (static_cast<QMetaType::Type>(variant.type()) == QMetaType::QVariantList) {
const QVariantList variantList = variant.toList();
lua_createtable(L_p, 0, variantList.count());
int currentId = 1;
for (const QVariant &variant : qAsConst(variantList)) {
@ -305,16 +311,17 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
currentId++;
}
}
else if ((QMetaType::Type)variant.type() == QMetaType::QVariantMap) {
QVariantMap variantMap = variant.toMap();
else if (static_cast<QMetaType::Type>(variant.type()) == QMetaType::QVariantMap) {
const QVariantMap variantMap = variant.toMap();
lua_createtable(L_p, 0, variantMap.count());
for (auto it = variantMap.constBegin(); it != variantMap.constEnd(); it++) {
lua_pushstring(L_p, it.key().toUtf8().data());
const QByteArray luaBArray = it.key().toUtf8();
lua_pushlstring(L_p, luaBArray.data(), luaBArray.size());
pushVariant(L_p, it.value());
lua_settable(L_p, -3);
}
}
else if ((QMetaType::Type)variant.type() == QMetaType::Void || (QMetaType::Type)variant.type() == QMetaType::VoidStar) {
else if (static_cast<QMetaType::Type>(variant.type()) == QMetaType::Void || static_cast<QMetaType::Type>(variant.type()) == QMetaType::VoidStar) {
lua_pushlightuserdata(L_p, variant.value<void*>());
}
else {
@ -342,7 +349,7 @@ QVariant LuaEngine::getVariant(int index)
QVariant LuaEngine::getVariant(lua_State *L_p, int index)
{
if (lua_isboolean(L_p, index)) {
return QVariant::fromValue((bool)lua_toboolean(L_p, index));
return QVariant::fromValue(static_cast<bool>(lua_toboolean(L_p, index)));
}
else if (lua_isinteger(L_p, index)) {
return QVariant::fromValue(lua_tointeger(L_p, index));
@ -351,7 +358,9 @@ QVariant LuaEngine::getVariant(lua_State *L_p, int index)
return QVariant::fromValue(lua_tonumber(L_p, index));
}
else if (lua_isstring(L_p, index)) {
return QVariant::fromValue(QString(lua_tostring(L_p, index)));
size_t size;
const char* str = lua_tolstring(L_p, index, &size);
return QVariant::fromValue(QByteArray(str, size));
}
else if (lua_istable(L_p, index)) {
QVariantMap variantMap;
@ -359,8 +368,10 @@ QVariant LuaEngine::getVariant(lua_State *L_p, int index)
lua_pushnil(L_p);
while (lua_next(L_p, -2) != 0) {
lua_pushvalue(L_p, -2);
QString key = QString(lua_tostring(L_p, -1));
QVariant value = getVariant(L_p, -2);
size_t size;
const char* str = lua_tolstring(L_p, -1, &size);
const QString key = QString::fromUtf8(str, size);
const QVariant value = getVariant(L_p, -2);
variantMap.insert(key, value);
lua_pop(L_p, 2);
}