mirror of
https://gitlab.com/Syping/luaengineapp.git
synced 2024-11-05 05:26:55 +01:00
EngineType handling improved and JSON array support
This commit is contained in:
parent
406905b359
commit
1fd16e168c
11 changed files with 102 additions and 56 deletions
|
@ -26,8 +26,10 @@
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)
|
||||||
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
|
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
|
||||||
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
|
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
|
||||||
|
#endif
|
||||||
QApplication a(argc, argv);
|
QApplication a(argc, argv);
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
|
@ -50,7 +52,7 @@ int main(int argc, char *argv[])
|
||||||
QVariant scriptPath = arguments.first();
|
QVariant scriptPath = arguments.first();
|
||||||
QFile luaScript(scriptPath.toString());
|
QFile luaScript(scriptPath.toString());
|
||||||
if (!luaScript.open(QIODevice::ReadOnly)) {
|
if (!luaScript.open(QIODevice::ReadOnly)) {
|
||||||
QTextStream(stderr) << "Error: Failed to open \"" << arguments.first().toString() << "\"." << endl;
|
QTextStream(stderr) << "Error: Failed to open \"" << arguments.first().toString() << "\"." << Qt::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,22 +63,29 @@ int main(int argc, char *argv[])
|
||||||
luaScript.reset();
|
luaScript.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaEngineGui luaEngineGui;
|
LuaEngineGui luaEngineGui(LuaEngine::RuntimeEngineType);
|
||||||
LuaEngineIO::pushClass(&luaEngineGui);
|
LuaEngineIO::pushClass(&luaEngineGui);
|
||||||
LuaEngineNetwork::pushClass(&luaEngineGui);
|
LuaEngineNetwork::pushClass(&luaEngineGui);
|
||||||
LuaEngineOS::pushClass(&luaEngineGui);
|
LuaEngineOS::pushClass(&luaEngineGui);
|
||||||
luaEngineGui.pushVariant("_LuaEngineRT", rtPath);
|
luaEngineGui.pushVariant("_LuaEngineRT", rtPath);
|
||||||
luaEngineGui.pushVariant("_LuaEngineMode", "GUI");
|
luaEngineGui.pushVariant("_LuaEngineMode", "GUI");
|
||||||
luaEngineGui.setProperty("EngineType", "Runtime");
|
|
||||||
luaEngineGui.setProperty("ScriptPath", scriptPath);
|
luaEngineGui.setProperty("ScriptPath", scriptPath);
|
||||||
luaEngineGui.executeLuaScript(luaScript.readAll());
|
luaEngineGui.executeLuaScript(luaScript.readAll());
|
||||||
|
|
||||||
if (luaEngineGui.executeLuaFunction("main", arguments, true)) {
|
if (luaEngineGui.executeLuaFunction("main", arguments, true)) {
|
||||||
QVariant variant = luaEngineGui.returnVariant();
|
QVariant variant = luaEngineGui.returnVariant();
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
if (variant.typeId() == QMetaType::Int || variant.typeId() == QMetaType::LongLong) {
|
||||||
|
#else
|
||||||
if (variant.type() == QVariant::Int || variant.type() == QVariant::LongLong) {
|
if (variant.type() == QVariant::Int || variant.type() == QVariant::LongLong) {
|
||||||
|
#endif
|
||||||
return variant.toInt();
|
return variant.toInt();
|
||||||
}
|
}
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
else if (variant.typeId() == QMetaType::QString) {
|
||||||
|
#else
|
||||||
else if (variant.type() == QVariant::String) {
|
else if (variant.type() == QVariant::String) {
|
||||||
|
#endif
|
||||||
if (variant.toString() == "GuiExecuted" || variant.toString() == "CliExecuted") {
|
if (variant.toString() == "GuiExecuted" || variant.toString() == "CliExecuted") {
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,12 +76,11 @@ int main(int argc, char *argv[])
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
LuaEngineGui luaEngineGui;
|
LuaEngineGui luaEngineGui(LuaEngine::PortableEngineType);
|
||||||
LuaEngineIO::pushClass(&luaEngineGui);
|
LuaEngineIO::pushClass(&luaEngineGui);
|
||||||
LuaEngineNetwork::pushClass(&luaEngineGui);
|
LuaEngineNetwork::pushClass(&luaEngineGui);
|
||||||
LuaEngineOS::pushClass(&luaEngineGui);
|
LuaEngineOS::pushClass(&luaEngineGui);
|
||||||
luaEngineGui.pushVariant("_LuaEngineMode", "GUI");
|
luaEngineGui.pushVariant("_LuaEngineMode", "GUI");
|
||||||
luaEngineGui.setProperty("EngineType", "Portable");
|
|
||||||
luaEngineGui.executeLuaScript(luaScript);
|
luaEngineGui.executeLuaScript(luaScript);
|
||||||
|
|
||||||
QVariantList arguments;
|
QVariantList arguments;
|
||||||
|
@ -91,10 +90,18 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
if (luaEngineGui.executeLuaFunction("main", arguments, true)) {
|
if (luaEngineGui.executeLuaFunction("main", arguments, true)) {
|
||||||
const QVariant variant = luaEngineGui.returnVariant();
|
const QVariant variant = luaEngineGui.returnVariant();
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
if (variant.typeId() == QMetaType::Int || variant.typeId() == QMetaType::LongLong) {
|
||||||
|
#else
|
||||||
if (variant.type() == QVariant::Int || variant.type() == QVariant::LongLong) {
|
if (variant.type() == QVariant::Int || variant.type() == QVariant::LongLong) {
|
||||||
|
#endif
|
||||||
return variant.toInt();
|
return variant.toInt();
|
||||||
}
|
}
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
else if (variant.typeId() == QMetaType::QString) {
|
||||||
|
#else
|
||||||
else if (variant.type() == QVariant::String) {
|
else if (variant.type() == QVariant::String) {
|
||||||
|
#endif
|
||||||
if (variant.toString() == "GuiExecuted" || variant.toString() == "CliExecuted") {
|
if (variant.toString() == "GuiExecuted" || variant.toString() == "CliExecuted") {
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ int main(int argc, char *argv[])
|
||||||
QString outputFile;
|
QString outputFile;
|
||||||
const QStringList args = parser.positionalArguments();
|
const QStringList args = parser.positionalArguments();
|
||||||
if (args.length() < 1) {
|
if (args.length() < 1) {
|
||||||
QTextStream(stderr) << "Error: No input files." << endl;
|
QTextStream(stderr) << "Error: No input files." << Qt::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -62,7 +62,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
QFile luaScript(inputFile);
|
QFile luaScript(inputFile);
|
||||||
if (!luaScript.open(QIODevice::ReadOnly)) {
|
if (!luaScript.open(QIODevice::ReadOnly)) {
|
||||||
QTextStream(stderr) << "Error: Failed to open \"" << inputFile << "\"." << endl;
|
QTextStream(stderr) << "Error: Failed to open \"" << inputFile << "\"." << Qt::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,13 +75,13 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
LuaEngine luaEngine;
|
LuaEngine luaEngine;
|
||||||
if (!luaEngine.loadLuaScript(luaScript.readAll())) {
|
if (!luaEngine.loadLuaScript(luaScript.readAll())) {
|
||||||
QTextStream(stderr) << "Error: Failed to load \"" << inputFile << "\"." << endl;
|
QTextStream(stderr) << "Error: Failed to load \"" << inputFile << "\"." << Qt::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QSaveFile outputSaveFile(outputFile);
|
QSaveFile outputSaveFile(outputFile);
|
||||||
if (!outputSaveFile.open(QIODevice::WriteOnly)) {
|
if (!outputSaveFile.open(QIODevice::WriteOnly)) {
|
||||||
QTextStream(stderr) << "Error: Failed to open \"" << outputFile << "\"." << endl;
|
QTextStream(stderr) << "Error: Failed to open \"" << outputFile << "\"." << Qt::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ int main(int argc, char *argv[])
|
||||||
if (!portableLuaEngine.open(QIODevice::ReadOnly)) {
|
if (!portableLuaEngine.open(QIODevice::ReadOnly)) {
|
||||||
outputSaveFile.cancelWriting();
|
outputSaveFile.cancelWriting();
|
||||||
outputSaveFile.commit();
|
outputSaveFile.commit();
|
||||||
QTextStream(stderr) << "Error: Failed to open \"" << pLuaEngine << "\"." << endl;
|
QTextStream(stderr) << "Error: Failed to open \"" << pLuaEngine << "\"." << Qt::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
outputSaveFile.write(portableLuaEngine.readAll());
|
outputSaveFile.write(portableLuaEngine.readAll());
|
||||||
|
@ -106,7 +106,7 @@ int main(int argc, char *argv[])
|
||||||
if (lengthArray.size() > 8) {
|
if (lengthArray.size() > 8) {
|
||||||
outputSaveFile.cancelWriting();
|
outputSaveFile.cancelWriting();
|
||||||
outputSaveFile.commit();
|
outputSaveFile.commit();
|
||||||
QTextStream(stderr) << "Error: Lua Engine script is too large." << endl;
|
QTextStream(stderr) << "Error: Lua Engine script is too large." << Qt::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -119,7 +119,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!outputSaveFile.commit()) {
|
if (!outputSaveFile.commit()) {
|
||||||
QTextStream(stderr) << "Error: Failed to write \"" << args.at(1) << "\"." << endl;
|
QTextStream(stderr) << "Error: Failed to write \"" << args.at(1) << "\"." << Qt::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ int main(int argc, char *argv[])
|
||||||
QVariant scriptPath = arguments.first();
|
QVariant scriptPath = arguments.first();
|
||||||
QFile luaScript(scriptPath.toString());
|
QFile luaScript(scriptPath.toString());
|
||||||
if (!luaScript.open(QIODevice::ReadOnly)) {
|
if (!luaScript.open(QIODevice::ReadOnly)) {
|
||||||
QTextStream(stderr) << "Error: Failed to open \"" << arguments.first().toString() << "\"." << endl;
|
QTextStream(stderr) << "Error: Failed to open \"" << arguments.first().toString() << "\"." << Qt::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,22 +48,29 @@ int main(int argc, char *argv[])
|
||||||
luaScript.reset();
|
luaScript.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaEngine luaEngine;
|
LuaEngine luaEngine(LuaEngine::RuntimeEngineType);
|
||||||
LuaEngineIO::pushClass(&luaEngine);
|
LuaEngineIO::pushClass(&luaEngine);
|
||||||
LuaEngineNetwork::pushClass(&luaEngine);
|
LuaEngineNetwork::pushClass(&luaEngine);
|
||||||
LuaEngineOS::pushClass(&luaEngine);
|
LuaEngineOS::pushClass(&luaEngine);
|
||||||
luaEngine.pushVariant("_LuaEngineRT", rtPath);
|
luaEngine.pushVariant("_LuaEngineRT", rtPath);
|
||||||
luaEngine.pushVariant("_LuaEngineMode", "CLI");
|
luaEngine.pushVariant("_LuaEngineMode", "CLI");
|
||||||
luaEngine.setProperty("EngineType", "Runtime");
|
|
||||||
luaEngine.setProperty("ScriptPath", scriptPath);
|
luaEngine.setProperty("ScriptPath", scriptPath);
|
||||||
luaEngine.executeLuaScript(luaScript.readAll());
|
luaEngine.executeLuaScript(luaScript.readAll());
|
||||||
|
|
||||||
if (luaEngine.executeLuaFunction("main", arguments, true)) {
|
if (luaEngine.executeLuaFunction("main", arguments, true)) {
|
||||||
QVariant variant = luaEngine.returnVariant();
|
QVariant variant = luaEngine.returnVariant();
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
if (variant.typeId() == QMetaType::Int || variant.typeId() == QMetaType::LongLong) {
|
||||||
|
#else
|
||||||
if (variant.type() == QVariant::Int || variant.type() == QVariant::LongLong) {
|
if (variant.type() == QVariant::Int || variant.type() == QVariant::LongLong) {
|
||||||
|
#endif
|
||||||
return variant.toInt();
|
return variant.toInt();
|
||||||
}
|
}
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
else if (variant.typeId() == QMetaType::QString) {
|
||||||
|
#else
|
||||||
else if (variant.type() == QVariant::String) {
|
else if (variant.type() == QVariant::String) {
|
||||||
|
#endif
|
||||||
if (variant.toString() == "CliExecuted") {
|
if (variant.toString() == "CliExecuted") {
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* luaEngine Lua Engine for Qt
|
* luaEngine Lua Engine for Qt
|
||||||
* Copyright (C) 2018-2019 Syping
|
* Copyright (C) 2018-2021 Syping
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,8 +21,12 @@
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QMetaMethod>
|
#include <QMetaMethod>
|
||||||
|
|
||||||
LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent)
|
LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent)
|
||||||
|
{
|
||||||
|
LuaEngine(LuaEngineType::UnknownEngineType, parent, loadBaseLibraries);
|
||||||
|
}
|
||||||
|
|
||||||
|
LuaEngine::LuaEngine(LuaEngineType engineType, QObject *parent, bool loadBaseLibraries) : QObject(parent), p_engineType(engineType)
|
||||||
{
|
{
|
||||||
L = luaL_newstate();
|
L = luaL_newstate();
|
||||||
if (loadBaseLibraries)
|
if (loadBaseLibraries)
|
||||||
|
@ -53,6 +57,11 @@ lua_State* LuaEngine::luaState()
|
||||||
return L;
|
return L;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LuaEngine::LuaEngineType LuaEngine::engineType()
|
||||||
|
{
|
||||||
|
return p_engineType;
|
||||||
|
}
|
||||||
|
|
||||||
void LuaEngine::loadBaseLibraries()
|
void LuaEngine::loadBaseLibraries()
|
||||||
{
|
{
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
|
@ -148,7 +157,7 @@ bool LuaEngine::loadLuaScript(QIODevice *device, bool closeDevice)
|
||||||
bool LuaEngine::executeLuaScript(const QByteArray &data)
|
bool LuaEngine::executeLuaScript(const QByteArray &data)
|
||||||
{
|
{
|
||||||
if (loadLuaScript(data))
|
if (loadLuaScript(data))
|
||||||
return (lua_pcall(L, 0, LUA_MULTRET, 0) == 0) ? true : false;
|
return (lua_pcall(L, 0, LUA_MULTRET, 0) == 0);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,7 +190,7 @@ bool LuaEngine::executeLuaFunction(lua_State *L_p, const char *name, bool requir
|
||||||
{
|
{
|
||||||
int returnCount = (requireReturn) ? LUA_MULTRET : 0;
|
int returnCount = (requireReturn) ? LUA_MULTRET : 0;
|
||||||
lua_getglobal(L_p, name);
|
lua_getglobal(L_p, name);
|
||||||
return (lua_pcall(L_p, 0, returnCount, 0) == 0) ? true : false;
|
return (lua_pcall(L_p, 0, returnCount, 0) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LuaEngine::executeLuaFunction(const char *name, const QVariant &argument, bool requireReturn)
|
bool LuaEngine::executeLuaFunction(const char *name, const QVariant &argument, bool requireReturn)
|
||||||
|
@ -194,7 +203,7 @@ bool LuaEngine::executeLuaFunction(lua_State *L_p, const char *name, const QVari
|
||||||
int returnCount = (requireReturn) ? LUA_MULTRET : 0;
|
int returnCount = (requireReturn) ? LUA_MULTRET : 0;
|
||||||
lua_getglobal(L_p, name);
|
lua_getglobal(L_p, name);
|
||||||
pushVariant(L_p, argument);
|
pushVariant(L_p, argument);
|
||||||
return (lua_pcall(L_p, 1, returnCount, 0) == 0) ? true : false;
|
return (lua_pcall(L_p, 1, returnCount, 0) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LuaEngine::executeLuaFunction(const char *name, const QVariantList &args, bool requireReturn)
|
bool LuaEngine::executeLuaFunction(const char *name, const QVariantList &args, bool requireReturn)
|
||||||
|
@ -209,7 +218,7 @@ bool LuaEngine::executeLuaFunction(lua_State *L_p, const char *name, const QVari
|
||||||
for (const QVariant &argument : args) {
|
for (const QVariant &argument : args) {
|
||||||
pushVariant(L_p, argument);
|
pushVariant(L_p, argument);
|
||||||
}
|
}
|
||||||
return (lua_pcall(L_p, args.count(), returnCount, 0) == 0) ? true : false;
|
return (lua_pcall(L_p, args.count(), returnCount, 0) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaEngine::pushFunction(const char *name, lua_CFunction function)
|
void LuaEngine::pushFunction(const char *name, lua_CFunction function)
|
||||||
|
@ -278,7 +287,7 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
|
||||||
QStringList stringList = variant.toStringList();
|
QStringList stringList = variant.toStringList();
|
||||||
lua_createtable(L_p, 0, stringList.count());
|
lua_createtable(L_p, 0, stringList.count());
|
||||||
int currentId = 1;
|
int currentId = 1;
|
||||||
for (const QString &string : stringList) {
|
for (const QString &string : qAsConst(stringList)) {
|
||||||
lua_pushinteger(L_p, currentId);
|
lua_pushinteger(L_p, currentId);
|
||||||
lua_pushstring(L_p, string.toUtf8().data());
|
lua_pushstring(L_p, string.toUtf8().data());
|
||||||
lua_settable(L_p, -3);
|
lua_settable(L_p, -3);
|
||||||
|
@ -289,7 +298,7 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
|
||||||
QVariantList variantList = variant.toList();
|
QVariantList variantList = variant.toList();
|
||||||
lua_createtable(L_p, 0, variantList.count());
|
lua_createtable(L_p, 0, variantList.count());
|
||||||
int currentId = 1;
|
int currentId = 1;
|
||||||
for (const QVariant &variant : variantList) {
|
for (const QVariant &variant : qAsConst(variantList)) {
|
||||||
lua_pushinteger(L_p, currentId);
|
lua_pushinteger(L_p, currentId);
|
||||||
pushVariant(L_p, variant);
|
pushVariant(L_p, variant);
|
||||||
lua_settable(L_p, -3);
|
lua_settable(L_p, -3);
|
||||||
|
@ -299,13 +308,10 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
|
||||||
else if ((QMetaType::Type)variant.type() == QMetaType::QVariantMap) {
|
else if ((QMetaType::Type)variant.type() == QMetaType::QVariantMap) {
|
||||||
QVariantMap variantMap = variant.toMap();
|
QVariantMap variantMap = variant.toMap();
|
||||||
lua_createtable(L_p, 0, variantMap.count());
|
lua_createtable(L_p, 0, variantMap.count());
|
||||||
QVariantMap::const_iterator it = variantMap.constBegin();
|
for (auto it = variantMap.constBegin(); it != variantMap.constEnd(); it++) {
|
||||||
QVariantMap::const_iterator end = variantMap.constEnd();
|
|
||||||
while (it != end) {
|
|
||||||
lua_pushstring(L_p, it.key().toUtf8().data());
|
lua_pushstring(L_p, it.key().toUtf8().data());
|
||||||
pushVariant(L_p, it.value());
|
pushVariant(L_p, it.value());
|
||||||
lua_settable(L_p, -3);
|
lua_settable(L_p, -3);
|
||||||
it++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((QMetaType::Type)variant.type() == QMetaType::Void || (QMetaType::Type)variant.type() == QMetaType::VoidStar) {
|
else if ((QMetaType::Type)variant.type() == QMetaType::Void || (QMetaType::Type)variant.type() == QMetaType::VoidStar) {
|
||||||
|
@ -367,7 +373,7 @@ QVariant LuaEngine::getVariant(lua_State *L_p, int index)
|
||||||
else if (lua_isnoneornil(L_p, index)) {
|
else if (lua_isnoneornil(L_p, index)) {
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
QTextStream(stderr) << "Warning: Didn't catch lua_isnoneornil before empty QVariant got returned" << endl;
|
QTextStream(stderr) << "Warning: Didn't catch lua_isnoneornil before empty QVariant got returned" << Qt::endl;
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -433,8 +439,7 @@ int LuaEngine::luaObjectDelete_p(lua_State *L_p)
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
void *pointer = getPointer(L_p, 1);
|
void *pointer = getPointer(L_p, 1);
|
||||||
if (pointer != NULL) {
|
if (pointer != NULL) {
|
||||||
switch (getVariant(L_p, 2).toInt())
|
switch (getVariant(L_p, 2).toInt()) {
|
||||||
{
|
|
||||||
case 1:
|
case 1:
|
||||||
((QObject*)pointer)->deleteLater();
|
((QObject*)pointer)->deleteLater();
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* luaEngine Lua Engine for Qt
|
* luaEngine Lua Engine for Qt
|
||||||
* Copyright (C) 2018-2019 Syping
|
* Copyright (C) 2018-2021 Syping
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -33,9 +33,12 @@ class LUAENGINESHARED_EXPORT LuaEngine : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
enum LuaEngineType : qint8 { RuntimeEngineType = 0, PortableEngineType = 1, UnknownEngineType = -1 };
|
||||||
LuaEngine(QObject *parent = nullptr, bool loadBaseLibraries = true);
|
LuaEngine(QObject *parent = nullptr, bool loadBaseLibraries = true);
|
||||||
|
LuaEngine(LuaEngineType engineType, QObject *parent = nullptr, bool loadBaseLibraries = true);
|
||||||
~LuaEngine();
|
~LuaEngine();
|
||||||
lua_State* luaState();
|
lua_State* luaState();
|
||||||
|
LuaEngineType engineType();
|
||||||
void loadBaseLibraries();
|
void loadBaseLibraries();
|
||||||
QByteArray dumpLuaScript();
|
QByteArray dumpLuaScript();
|
||||||
bool loadLuaScript(const QByteArray &data);
|
bool loadLuaScript(const QByteArray &data);
|
||||||
|
@ -75,6 +78,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
|
LuaEngineType p_engineType;
|
||||||
static int luaEngineWriter_p(lua_State *L_p, const void *buffer, size_t size, void *array);
|
static int luaEngineWriter_p(lua_State *L_p, const void *buffer, size_t size, void *array);
|
||||||
static int luaEngineQuit_p(lua_State *L_p);
|
static int luaEngineQuit_p(lua_State *L_p);
|
||||||
static int luaEngineVersion_p(lua_State *L_p);
|
static int luaEngineVersion_p(lua_State *L_p);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#/*****************************************************************************
|
#/*****************************************************************************
|
||||||
#* luaEngine Lua Engine for Qt
|
#* luaEngine Lua Engine for Qt
|
||||||
#* Copyright (C) 2018-2020 Syping
|
#* Copyright (C) 2018-2021 Syping
|
||||||
#*
|
#*
|
||||||
#* Licensed under the Apache License, Version 2.0 (the "License");
|
#* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
#* you may not use this file except in compliance with the License.
|
#* you may not use this file except in compliance with the License.
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* luaEngine Lua Engine for Qt
|
* luaEngine Lua Engine for Qt
|
||||||
* Copyright (C) 2018-2020 Syping
|
* Copyright (C) 2018-2021 Syping
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -54,8 +54,12 @@
|
||||||
|
|
||||||
LuaEngineGui::LuaEngineGui(QObject *parent, bool loadBaseLibraries) : LuaEngine(parent, loadBaseLibraries)
|
LuaEngineGui::LuaEngineGui(QObject *parent, bool loadBaseLibraries) : LuaEngine(parent, loadBaseLibraries)
|
||||||
{
|
{
|
||||||
L = luaState();
|
pushClass(luaState());
|
||||||
pushClass(L);
|
}
|
||||||
|
|
||||||
|
LuaEngineGui::LuaEngineGui(LuaEngineType engineType, QObject *parent, bool loadBaseLibraries) : LuaEngine(engineType, parent, loadBaseLibraries)
|
||||||
|
{
|
||||||
|
pushClass(luaState());
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaEngineGui::pushClass(lua_State *L_p)
|
void LuaEngineGui::pushClass(lua_State *L_p)
|
||||||
|
@ -223,7 +227,7 @@ int LuaEngineGui::updateUi(lua_State *L_p)
|
||||||
int LuaEngineGui::showFileDialog(lua_State *L_p)
|
int LuaEngineGui::showFileDialog(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString dialogTitle = "LuaEngine";
|
QString dialogTitle = QStringLiteral("LuaEngine");
|
||||||
QString fileFormats = "All files (*)";
|
QString fileFormats = "All files (*)";
|
||||||
QFileDialog::FileMode fileMode = QFileDialog::ExistingFile;
|
QFileDialog::FileMode fileMode = QFileDialog::ExistingFile;
|
||||||
QFileDialog::Options fileOptions;
|
QFileDialog::Options fileOptions;
|
||||||
|
@ -288,7 +292,7 @@ int LuaEngineGui::showMessageBox(lua_State *L_p)
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString boxMessage = getVariant(L_p, 2).toString();
|
QString boxMessage = getVariant(L_p, 2).toString();
|
||||||
QString boxTitle = "LuaEngine";
|
QString boxTitle = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 3) {
|
if (getArgumentCount(L_p) >= 3) {
|
||||||
boxTitle = getVariant(L_p, 3).toString();
|
boxTitle = getVariant(L_p, 3).toString();
|
||||||
if (getArgumentCount(L_p) >= 4) {
|
if (getArgumentCount(L_p) >= 4) {
|
||||||
|
@ -912,7 +916,7 @@ int LuaEngineGui::createCheckBox(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QLayout *layout = nullptr;
|
QLayout *layout = nullptr;
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString labelText = "LuaEngine";
|
QString labelText = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
labelText = getVariant(L_p, 1).toString();
|
labelText = getVariant(L_p, 1).toString();
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
|
@ -932,7 +936,7 @@ int LuaEngineGui::createCheckBox(lua_State *L_p)
|
||||||
int LuaEngineGui::createDialog(lua_State *L_p)
|
int LuaEngineGui::createDialog(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString windowTitle = "LuaEngine";
|
QString windowTitle = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
windowTitle = getVariant(L_p, 1).toString();
|
windowTitle = getVariant(L_p, 1).toString();
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
|
@ -958,7 +962,7 @@ int LuaEngineGui::createGroupBox(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QLayout *layout = nullptr;
|
QLayout *layout = nullptr;
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString groupBoxTitle = "LuaEngine";
|
QString groupBoxTitle = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
groupBoxTitle = getVariant(L_p, 1).toString();
|
groupBoxTitle = getVariant(L_p, 1).toString();
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
|
@ -979,7 +983,7 @@ int LuaEngineGui::createLabel(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QLayout *layout = nullptr;
|
QLayout *layout = nullptr;
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString labelInput = "LuaEngine";
|
QString labelInput = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
labelInput = getVariant(L_p, 1).toString();
|
labelInput = getVariant(L_p, 1).toString();
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
|
@ -1062,7 +1066,7 @@ int LuaEngineGui::createLineEdit(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QLayout *layout = nullptr;
|
QLayout *layout = nullptr;
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString editText = "LuaEngine";
|
QString editText = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
editText = getVariant(L_p, 1).toString();
|
editText = getVariant(L_p, 1).toString();
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
|
@ -1119,7 +1123,7 @@ int LuaEngineGui::createListView(lua_State *L_p)
|
||||||
int LuaEngineGui::createMainWindow(lua_State *L_p)
|
int LuaEngineGui::createMainWindow(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString windowTitle = "LuaEngine";
|
QString windowTitle = QStringLiteral("LuaEngine");
|
||||||
bool centralWidget = true;
|
bool centralWidget = true;
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
windowTitle = getVariant(L_p, 1).toString();
|
windowTitle = getVariant(L_p, 1).toString();
|
||||||
|
@ -1134,6 +1138,9 @@ int LuaEngineGui::createMainWindow(lua_State *L_p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QMainWindow *mainWindow = new QMainWindow(parent);
|
QMainWindow *mainWindow = new QMainWindow(parent);
|
||||||
|
#if QT_VERSION >= 0x050900
|
||||||
|
mainWindow->setWindowFlag(Qt::WindowContextHelpButtonHint, false);
|
||||||
|
#endif
|
||||||
mainWindow->setObjectName(nameForPointer(mainWindow));
|
mainWindow->setObjectName(nameForPointer(mainWindow));
|
||||||
mainWindow->setWindowTitle(windowTitle);
|
mainWindow->setWindowTitle(windowTitle);
|
||||||
if (centralWidget) {
|
if (centralWidget) {
|
||||||
|
@ -1238,7 +1245,7 @@ int LuaEngineGui::createPlainTextEdit(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QLayout *layout = nullptr;
|
QLayout *layout = nullptr;
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString editText = "LuaEngine";
|
QString editText = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
editText = getVariant(L_p, 1).toString();
|
editText = getVariant(L_p, 1).toString();
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
|
@ -1288,7 +1295,7 @@ int LuaEngineGui::createPushButton(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QLayout *layout = nullptr;
|
QLayout *layout = nullptr;
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString buttonText = "LuaEngine";
|
QString buttonText = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
buttonText = getVariant(L_p, 1).toString();
|
buttonText = getVariant(L_p, 1).toString();
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
|
@ -1309,7 +1316,7 @@ int LuaEngineGui::createRadioButton(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QLayout *layout = nullptr;
|
QLayout *layout = nullptr;
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString labelText = "LuaEngine";
|
QString labelText = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
labelText = getVariant(L_p, 1).toString();
|
labelText = getVariant(L_p, 1).toString();
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
|
@ -1386,7 +1393,7 @@ int LuaEngineGui::createTextEdit(lua_State *L_p)
|
||||||
{
|
{
|
||||||
QLayout *layout = nullptr;
|
QLayout *layout = nullptr;
|
||||||
QWidget *parent = nullptr;
|
QWidget *parent = nullptr;
|
||||||
QString editText = "LuaEngine";
|
QString editText = QStringLiteral("LuaEngine");
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
editText = getVariant(L_p, 1).toString();
|
editText = getVariant(L_p, 1).toString();
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
|
@ -1640,8 +1647,8 @@ QWidget* LuaEngineGui::windowForObject(QObject *object)
|
||||||
QString LuaEngineGui::pathForString(const QString &string, LuaEngine *engine)
|
QString LuaEngineGui::pathForString(const QString &string, LuaEngine *engine)
|
||||||
{
|
{
|
||||||
if (QFileInfo(string).isRelative()) {
|
if (QFileInfo(string).isRelative()) {
|
||||||
QString engineType = engine->property("EngineType").toString();
|
const LuaEngineType engineType = engine->engineType();
|
||||||
if (engineType == "Runtime") {
|
if (engineType == LuaEngineType::RuntimeEngineType) {
|
||||||
QString scriptPath = engine->property("ScriptPath").toString();
|
QString scriptPath = engine->property("ScriptPath").toString();
|
||||||
QString scriptDirPath = QFileInfo(scriptPath).canonicalPath();
|
QString scriptDirPath = QFileInfo(scriptPath).canonicalPath();
|
||||||
QString newImagePath = QString("%1/%2").arg(scriptDirPath, string);
|
QString newImagePath = QString("%1/%2").arg(scriptDirPath, string);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* luaEngine Lua Engine for Qt
|
* luaEngine Lua Engine for Qt
|
||||||
* Copyright (C) 2018-2020 Syping
|
* Copyright (C) 2018-2021 Syping
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -30,6 +30,7 @@ class LUAENGINEGUISHARED_EXPORT LuaEngineGui : public LuaEngine
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
LuaEngineGui(QObject *parent = nullptr, bool loadBaseLibraries = true);
|
LuaEngineGui(QObject *parent = nullptr, bool loadBaseLibraries = true);
|
||||||
|
LuaEngineGui(LuaEngineType engineType, QObject *parent = nullptr, bool loadBaseLibraries = true);
|
||||||
static void pushClass(lua_State *L_p);
|
static void pushClass(lua_State *L_p);
|
||||||
static void pushClass(LuaEngine *luaEngine);
|
static void pushClass(LuaEngine *luaEngine);
|
||||||
static int updateUi(lua_State *L_p);
|
static int updateUi(lua_State *L_p);
|
||||||
|
@ -95,7 +96,6 @@ public:
|
||||||
static int getWidgetPixelRatio(lua_State *L_p);
|
static int getWidgetPixelRatio(lua_State *L_p);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
lua_State *L;
|
|
||||||
static QWidget* windowForObject(QObject *object);
|
static QWidget* windowForObject(QObject *object);
|
||||||
static QString pathForString(const QString &string, LuaEngine *engine);
|
static QString pathForString(const QString &string, LuaEngine *engine);
|
||||||
static void lpForPointer(void *pointer, QLayout **layout, QWidget **parent);
|
static void lpForPointer(void *pointer, QLayout **layout, QWidget **parent);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* luaEngine Lua Engine for Qt
|
* luaEngine Lua Engine for Qt
|
||||||
* Copyright (C) 2019 Syping
|
* Copyright (C) 2019-2021 Syping
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -291,9 +291,16 @@ int LuaEngineIO::linkFile(lua_State *L_p)
|
||||||
int LuaEngineIO::jsonToTable(lua_State *L_p)
|
int LuaEngineIO::jsonToTable(lua_State *L_p)
|
||||||
{
|
{
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
pushVariant(L_p, QJsonDocument::fromJson(getVariant(L_p, 1).toString().toUtf8()).object().toVariantMap());
|
const QJsonDocument jsonDocument = QJsonDocument::fromJson(getVariant(L_p, 1).toString().toUtf8());
|
||||||
|
if (jsonDocument.isObject()) {
|
||||||
|
pushVariant(L_p, jsonDocument.object().toVariantMap());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
else if (jsonDocument.isArray()) {
|
||||||
|
pushVariant(L_p, jsonDocument.array().toVariantList());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,7 +309,7 @@ int LuaEngineIO::tableToJson(lua_State *L_p)
|
||||||
if (getArgumentCount(L_p) >= 1) {
|
if (getArgumentCount(L_p) >= 1) {
|
||||||
QJsonDocument::JsonFormat jsonFormat = QJsonDocument::Compact;
|
QJsonDocument::JsonFormat jsonFormat = QJsonDocument::Compact;
|
||||||
if (getArgumentCount(L_p) >= 2) {
|
if (getArgumentCount(L_p) >= 2) {
|
||||||
jsonFormat = (QJsonDocument::JsonFormat)getVariant(L_p, 2).toInt();
|
jsonFormat = static_cast<QJsonDocument::JsonFormat>(getVariant(L_p, 2).toInt());
|
||||||
}
|
}
|
||||||
pushVariant(L_p, QString::fromUtf8(QJsonDocument(QJsonObject::fromVariantMap(getVariant(L_p, 1).toMap())).toJson(jsonFormat)));
|
pushVariant(L_p, QString::fromUtf8(QJsonDocument(QJsonObject::fromVariantMap(getVariant(L_p, 1).toMap())).toJson(jsonFormat)));
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#/*****************************************************************************
|
#/*****************************************************************************
|
||||||
#* luaEngine Lua Engine for Qt
|
#* luaEngine Lua Engine for Qt
|
||||||
#* Copyright (C) 2019-2020 Syping
|
#* Copyright (C) 2019-2021 Syping
|
||||||
#*
|
#*
|
||||||
#* Licensed under the Apache License, Version 2.0 (the "License");
|
#* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
#* you may not use this file except in compliance with the License.
|
#* you may not use this file except in compliance with the License.
|
||||||
|
|
Loading…
Reference in a new issue