From 2a3aa0af59ac0f7568bc848efffb5f8d3eff8ccb Mon Sep 17 00:00:00 2001 From: Syping Date: Mon, 27 Jul 2020 16:33:48 +0200 Subject: [PATCH] added quit + CLI --- src/luaengine/main.cpp | 3 +- src/luaenginecli/luaenginecli.pro | 63 +++++++++++++++++ src/luaenginecli/main.cpp | 84 +++++++++++++++++++++++ src/luaenginecore/luaengine/LuaEngine.cpp | 18 +++++ src/luaenginecore/luaengine/LuaEngine.h | 1 + src/src.pro | 1 + 6 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 src/luaenginecli/luaenginecli.pro create mode 100644 src/luaenginecli/main.cpp diff --git a/src/luaengine/main.cpp b/src/luaengine/main.cpp index 30fc821..2b01004 100644 --- a/src/luaengine/main.cpp +++ b/src/luaengine/main.cpp @@ -66,6 +66,7 @@ int main(int argc, char *argv[]) LuaEngineNetwork::pushClass(&luaEngineGui); LuaEngineOS::pushClass(&luaEngineGui); luaEngineGui.pushVariant("_LuaEngineRT", rtPath); + luaEngineGui.setProperty("EngineMode", "GUI"); luaEngineGui.setProperty("EngineType", "Runtime"); luaEngineGui.setProperty("ScriptPath", scriptPath); luaEngineGui.executeLuaScript(luaScript.readAll()); @@ -76,7 +77,7 @@ int main(int argc, char *argv[]) return variant.toInt(); } else if (variant.type() == QVariant::String) { - if (variant.toString() == "GuiExecuted") { + if (variant.toString() == "GuiExecuted" || variant.toString() == "CLIExecuted") { return a.exec(); } else { diff --git a/src/luaenginecli/luaenginecli.pro b/src/luaenginecli/luaenginecli.pro new file mode 100644 index 0000000..ce3db15 --- /dev/null +++ b/src/luaenginecli/luaenginecli.pro @@ -0,0 +1,63 @@ +#/***************************************************************************** +#* luaEngine Lua Engine for Qt +#* Copyright (C) 2020 Syping +#* +#* Licensed under the Apache License, Version 2.0 (the "License"); +#* you may not use this file except in compliance with the License. +#* You may obtain a copy of the License at +#* +#* http://www.apache.org/licenses/LICENSE-2.0 +#* +#* Unless required by applicable law or agreed to in writing, software +#* distributed under the License is distributed on an "AS IS" BASIS, +#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +#* See the License for the specific language governing permissions and +#* limitations under the License. +#*****************************************************************************/ + +QT += network +QT -= gui +TARGET = luaenginecli +CONFIG += c++11 \ + console +CONFIG -= app_bundle +VERSION = 0.1 + +static: DEFINES += LUAENGINE_STATIC + +# PROJECT PREFIX +unix { + isEmpty(LUAENGINE_PREFIX): LUAENGINE_PREFIX = /usr/local + isEmpty(LUAENGINE_BINDIR): LUAENGINE_BINDIR = $$LUAENGINE_PREFIX/bin + isEmpty(LUAENGINE_LIBDIR): LUAENGINE_LIBDIR = $$LUAENGINE_PREFIX/lib +} + +unix: LIBS += "-Wl,-rpath,\'$$LUAENGINE_LIBDIR\'" -L$$OUT_PWD/../luaengineos -lLuaEngineOS -L$$OUT_PWD/../luaenginenetwork -lLuaEngineNetwork -L$$OUT_PWD/../luaengineio -lLuaEngineIO -L$$OUT_PWD/../luaenginegui -lLuaEngineGui -L$$OUT_PWD/../luaenginecore -lLuaEngine +CONFIG(debug, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/debug -lLuaEngineOS -L$$OUT_PWD/../luaenginenetwork/debug -lLuaEngineNetwork -L$$OUT_PWD/../luaengineio/debug -lLuaEngineIO -L$$OUT_PWD/../luaenginegui/debug -lLuaEngineGui -L$$OUT_PWD/../luaenginecore/debug -lLuaEngine +CONFIG(release, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/release -lLuaEngineOS -L$$OUT_PWD/../luaenginenetwork/release -lLuaEngineNetwork -L$$OUT_PWD/../luaengineio/release -lLuaEngineIO -L$$OUT_PWD/../luaenginegui/release -lLuaEngineGui -L$$OUT_PWD/../luaenginecore/release -lLuaEngine + +INCLUDEPATH += \ + ../luaenginecore/luaengine \ + ../luaenginegui/luaengine \ + ../luaengineio/luaengine \ + ../luaenginenetwork/luaengine \ + ../luaengineos/luaengine + +SOURCES += \ + main.cpp + +include(../luaenginecore/lua_module.pri) + +unix { + target.path = $$LUAENGINE_BINDIR + INSTALLS += target +} + +win32 { + RC_LANG = 0x0 + RC_ICONS = ../../res/lua.ico + QMAKE_TARGET_COMPANY = "Syping" + QMAKE_TARGET_DESCRIPTION = "LuaEngine CLI Runtime" + QMAKE_TARGET_COPYRIGHT = "Copyright (c) 2020 Syping" + QMAKE_TARGET_PRODUCT = "luaenginecli" +} diff --git a/src/luaenginecli/main.cpp b/src/luaenginecli/main.cpp new file mode 100644 index 0000000..dc5a069 --- /dev/null +++ b/src/luaenginecli/main.cpp @@ -0,0 +1,84 @@ +/***************************************************************************** +* luaEngine Lua Engine for Qt +* Copyright (C) 2018-2019 Syping +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*****************************************************************************/ + +#include "LuaEngineNetwork.h" +#include "LuaEngineIO.h" +#include "LuaEngineOS.h" +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QVariantList arguments; + for (const QString &argument : a.arguments()) { + arguments << QVariant::fromValue(argument); + } + + if (arguments.length() >= 2) { + QVariant rtPath = arguments.first(); + arguments.removeFirst(); + QVariant scriptPath = arguments.first(); + QFile luaScript(scriptPath.toString()); + if (!luaScript.open(QIODevice::ReadOnly)) { + QTextStream(stderr) << "Error: Failed to open \"" << arguments.first().toString() << "\"." << endl; + return 1; + } + + if (QString::fromUtf8(luaScript.read(2)) == "#!") { + luaScript.readLine(); + } + else { + luaScript.reset(); + } + + LuaEngine luaEngine; + LuaEngineIO::pushClass(&luaEngine); + LuaEngineNetwork::pushClass(&luaEngine); + LuaEngineOS::pushClass(&luaEngine); + luaEngine.pushVariant("_LuaEngineRT", rtPath); + luaEngine.setProperty("EngineMode", "CLI"); + luaEngine.setProperty("EngineType", "Runtime"); + luaEngine.setProperty("ScriptPath", scriptPath); + luaEngine.executeLuaScript(luaScript.readAll()); + + if (luaEngine.executeLuaFunction("main", arguments, true)) { + QVariant variant = luaEngine.returnVariant(); + if (variant.type() == QVariant::Int || variant.type() == QVariant::LongLong) { + return variant.toInt(); + } + else if (variant.type() == QVariant::String) { + if (variant.toString() == "CLIExecuted") { + return a.exec(); + } + else { + return 1; + } + } + else { + return 1; + } + } + + return 1; + } + else { + return 1; + } +} diff --git a/src/luaenginecore/luaengine/LuaEngine.cpp b/src/luaenginecore/luaengine/LuaEngine.cpp index 1c9d989..38c71e9 100644 --- a/src/luaenginecore/luaengine/LuaEngine.cpp +++ b/src/luaenginecore/luaengine/LuaEngine.cpp @@ -18,6 +18,7 @@ #define LUA_LIB #include "LuaEngine.h" #include "LuaEngineRegistry.h" +#include #include #include @@ -30,11 +31,13 @@ LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent) pushVariant("DeleteInstant", 0); pushVariant("DeleteLater", 1); + pushVariant("CLIExecuted", "CLIExecuted"); pushFunction("delete", luaObjectDelete_p); pushFunction("connect", luaTriggerConnect_p); pushFunction("disconnect", luaTriggerDisconnect_p); pushFunction("getObjectParent", luaObjectGetParent_p); pushFunction("setObjectParent", luaObjectSetParent_p); + pushFunction("luaEngineQuit", luaEngineQuit_p); pushFunction("luaEngineVersion", luaEngineVersion_p); pushFunction("luaEnginePlatform", luaEnginePlatform_p); } @@ -62,6 +65,21 @@ int LuaEngine::luaEngineWriter_p(lua_State *L_p, const void *buffer, size_t size return 0; } +int LuaEngine::luaEngineQuit_p(lua_State *L_p) +{ + int argumentCount = getArgumentCount(L_p); + if (argumentCount == 1) { + bool ok; + int retcode = getVariant(L_p, 1).toInt(&ok); + if (ok) { + QCoreApplication::exit(retcode); + return 0; + } + } + QCoreApplication::quit(); + return 0; +} + int LuaEngine::luaEngineVersion_p(lua_State *L_p) { pushVariant(L_p, "0.1"); diff --git a/src/luaenginecore/luaengine/LuaEngine.h b/src/luaenginecore/luaengine/LuaEngine.h index 4fa3576..5551cc1 100644 --- a/src/luaenginecore/luaengine/LuaEngine.h +++ b/src/luaenginecore/luaengine/LuaEngine.h @@ -76,6 +76,7 @@ public: private: lua_State *L; 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 luaEngineVersion_p(lua_State *L_p); static int luaEnginePlatform_p(lua_State *L_p); static int luaObjectDelete_p(lua_State *L_p); diff --git a/src/src.pro b/src/src.pro index a19d75b..45eae4f 100644 --- a/src/src.pro +++ b/src/src.pro @@ -43,5 +43,6 @@ luaengineapp.depends += luaenginecore \ luaenginec.depends = luaenginecore CONFIG(WITH_LUAENGINE_RUNTIME): SUBDIRS += luaengine +CONFIG(WITH_LUAENGINE_RUNTIME): SUBDIRS += luaenginecli CONFIG(WITH_LUAENGINE_APPSTUB): SUBDIRS += luaengineapp CONFIG(WITH_LUAENGINE_COMPILER): SUBDIRS += luaenginec