2019-09-30 19:25:02 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* luaEngine Lua Engine for Qt
|
|
|
|
* Copyright (C) 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 "LuaEngineOS.h"
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QProcess>
|
2019-10-01 05:59:28 +02:00
|
|
|
#include <QString>
|
2019-10-07 02:17:33 +02:00
|
|
|
#include <QObject>
|
2019-09-30 19:25:02 +02:00
|
|
|
|
2019-10-01 00:05:37 +02:00
|
|
|
LuaEngineOS::LuaEngineOS(QObject *parent, bool loadBaseLibraries) : LuaEngine(parent, loadBaseLibraries)
|
|
|
|
{
|
|
|
|
L = luaState();
|
|
|
|
pushClass(L);
|
|
|
|
}
|
|
|
|
|
2019-09-30 19:25:02 +02:00
|
|
|
void LuaEngineOS::pushClass(lua_State *L_p)
|
|
|
|
{
|
|
|
|
// Process
|
|
|
|
pushFunction(L_p, "executeProcess", executeProcess);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaEngineOS::pushClass(LuaEngine *luaEngine)
|
|
|
|
{
|
|
|
|
pushClass(luaEngine->luaState());
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaEngineOS::executeProcess(lua_State *L_p)
|
|
|
|
{
|
|
|
|
if (getArgumentCount(L_p) >= 1) {
|
2019-09-30 20:15:26 +02:00
|
|
|
int processReturn = 0;
|
|
|
|
bool runInBackground = false;
|
2019-09-30 20:23:47 +02:00
|
|
|
bool processSuccessed = false;
|
2019-09-30 19:25:02 +02:00
|
|
|
if (getArgumentCount(L_p) >= 2) {
|
|
|
|
QVariantList arguments = getArguments(L_p);
|
2019-09-30 20:15:26 +02:00
|
|
|
QVariant lastArgument = arguments.last();
|
|
|
|
if (lastArgument.type() == QVariant::Bool) {
|
|
|
|
runInBackground = lastArgument.toBool();
|
|
|
|
arguments.removeLast();
|
|
|
|
}
|
|
|
|
if (arguments.length() >= 2) {
|
|
|
|
QStringList processArguments;
|
|
|
|
QString processPath = arguments.first().toString();
|
|
|
|
arguments.removeFirst();
|
|
|
|
for (const QVariant &argument : arguments) {
|
|
|
|
processArguments << argument.toString();
|
|
|
|
}
|
|
|
|
if (runInBackground) {
|
2019-09-30 20:23:47 +02:00
|
|
|
processSuccessed = QProcess::startDetached(processPath, processArguments);
|
2019-09-30 20:15:26 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
processReturn = QProcess::execute(processPath, processArguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (runInBackground) {
|
2019-09-30 20:23:47 +02:00
|
|
|
processSuccessed = QProcess::startDetached(arguments.first().toString());
|
2019-09-30 20:15:26 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
processReturn = QProcess::execute(arguments.first().toString());
|
|
|
|
}
|
2019-09-30 19:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-09-30 20:15:26 +02:00
|
|
|
processReturn = QProcess::execute(getVariant(L_p, 1).toString());
|
|
|
|
}
|
2019-09-30 20:23:47 +02:00
|
|
|
if (runInBackground && !processSuccessed) {
|
2019-09-30 20:15:26 +02:00
|
|
|
processReturn = -2;
|
2019-09-30 19:25:02 +02:00
|
|
|
}
|
2019-09-30 20:23:47 +02:00
|
|
|
else if (!runInBackground && processReturn == 0) {
|
|
|
|
processSuccessed = true;
|
2019-09-30 20:20:07 +02:00
|
|
|
}
|
2019-09-30 20:23:47 +02:00
|
|
|
pushVariant(L_p, processSuccessed);
|
2019-09-30 20:15:26 +02:00
|
|
|
pushVariant(L_p, processReturn);
|
2019-09-30 20:20:07 +02:00
|
|
|
return 2;
|
2019-09-30 19:25:02 +02:00
|
|
|
}
|
2019-09-30 20:20:07 +02:00
|
|
|
pushVariant(L_p, false);
|
2019-09-30 20:15:26 +02:00
|
|
|
pushVariant(L_p, -2);
|
2019-09-30 20:20:07 +02:00
|
|
|
return 2;
|
2019-09-30 19:25:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString LuaEngineOS::nameForPointer(void *pointer)
|
|
|
|
{
|
|
|
|
QString nameStorage;
|
|
|
|
QTextStream(&nameStorage) << "LuaEngineOS" << pointer;
|
|
|
|
return nameStorage;
|
|
|
|
}
|