fix Lua script parsing issues

This commit is contained in:
Syping 2019-10-02 19:16:17 +02:00
parent 2c53b502d8
commit 144e65da73
5 changed files with 20 additions and 19 deletions

View file

@ -20,6 +20,7 @@
#include <QTextStream> #include <QTextStream>
#include <QMetaMethod> #include <QMetaMethod>
#include <QtGlobal> #include <QtGlobal>
#include <QDebug>
LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent) LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent)
{ {
@ -84,8 +85,10 @@ int LuaEngine::luaEnginePlatform_p(lua_State *L_p)
bool LuaEngine::executeLuaScript(const QByteArray &data) bool LuaEngine::executeLuaScript(const QByteArray &data)
{ {
int returnCode = luaL_dostring(L, data.data()); int result = luaL_loadbuffer(L, data.data(), data.size(), "script");
return (returnCode == 0) ? true : false; if (result == 0)
return (lua_pcall(L, 0, LUA_MULTRET, 0) == 0) ? true : false;
return false;
} }
bool LuaEngine::executeLuaScript(QIODevice *device, bool closeDevice) bool LuaEngine::executeLuaScript(QIODevice *device, bool closeDevice)
@ -115,7 +118,7 @@ bool LuaEngine::executeLuaFunction(const char *name, bool requireReturn)
bool LuaEngine::executeLuaFunction(lua_State *L_p, const char *name, bool requireReturn) bool LuaEngine::executeLuaFunction(lua_State *L_p, const char *name, bool requireReturn)
{ {
int returnCount = (requireReturn) ? 1 : 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) ? true : false;
} }
@ -127,7 +130,7 @@ bool LuaEngine::executeLuaFunction(const char *name, const QVariant &argument, b
bool LuaEngine::executeLuaFunction(lua_State *L_p, const char *name, const QVariant &argument, bool requireReturn) bool LuaEngine::executeLuaFunction(lua_State *L_p, const char *name, const QVariant &argument, bool requireReturn)
{ {
int returnCount = (requireReturn) ? 1 : 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) ? true : false;
@ -140,7 +143,7 @@ bool LuaEngine::executeLuaFunction(const char *name, const QVariantList &args, b
bool LuaEngine::executeLuaFunction(lua_State *L_p, const char *name, const QVariantList &args, bool requireReturn) bool LuaEngine::executeLuaFunction(lua_State *L_p, const char *name, const QVariantList &args, bool requireReturn)
{ {
int returnCount = (requireReturn) ? 1 : 0; int returnCount = (requireReturn) ? LUA_MULTRET : 0;
lua_getglobal(L_p, name); lua_getglobal(L_p, name);
for (const QVariant &argument : args) { for (const QVariant &argument : args) {
pushVariant(L_p, argument); pushVariant(L_p, argument);
@ -213,12 +216,11 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
else if (variant.type() == QVariant::StringList) { else if (variant.type() == QVariant::StringList) {
QStringList stringList = variant.toStringList(); QStringList stringList = variant.toStringList();
lua_createtable(L_p, 0, stringList.count()); lua_createtable(L_p, 0, stringList.count());
int top = lua_gettop(L_p);
int currentId = 1; int currentId = 1;
for (const QString &string : stringList) { for (const QString &string : stringList) {
lua_pushnumber(L_p, currentId); lua_pushnumber(L_p, currentId);
lua_pushstring(L_p, string.toUtf8().data()); lua_pushstring(L_p, string.toUtf8().data());
lua_settable(L_p, top); lua_settable(L_p, -3);
currentId++; currentId++;
} }
} }

View file

@ -1,3 +1,3 @@
#include <resource.h> #include "windows.h"
#include <windows.h> #include "resource.h"
IDR_RC_TEXT1 RC_TEXT "app.lua" IDR_SCRIPT1 SCRIPT "app.lua.bin"

View file

@ -39,6 +39,7 @@ win32: HEADERS += \
resource.h resource.h
OTHER_FILES += \ OTHER_FILES += \
app.lua app.lua \
app.rc
win32: RC_FILE = app.rc win32: RC_FILE = app.rc

View file

@ -18,6 +18,7 @@
#include "LuaEngineGui.h" #include "LuaEngineGui.h"
#include "LuaEngineOS.h" #include "LuaEngineOS.h"
#include <QApplication> #include <QApplication>
#include <QDebug>
#include <QFont> #include <QFont>
#include <QFile> #include <QFile>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
@ -42,15 +43,13 @@ int main(int argc, char *argv[])
QByteArray luaScript; QByteArray luaScript;
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
{ {
HMODULE handle = GetModuleHandle(NULL); HMODULE handle = GetModuleHandleW(NULL);
HRSRC resource = FindResource(handle, MAKEINTRESOURCE(IDR_RC_TEXT1), MAKEINTRESOURCE(RC_TEXT)); HRSRC resource = FindResourceW(handle, MAKEINTRESOURCE(IDR_SCRIPT1), L"SCRIPT");
HGLOBAL resourceData = LoadResource(handle, resource); HGLOBAL resourceData = LoadResource(handle, resource);
DWORD size = SizeofResource(handle, resource); DWORD size = SizeofResource(handle, resource);
qDebug() << size;
const char *data = static_cast<const char*>(LockResource(resourceData)); const char *data = static_cast<const char*>(LockResource(resourceData));
char* buffer = new char[size + 1]; luaScript = QByteArray(data, size);
memcpy(buffer, data, size);
buffer[size] = 0;
luaScript = QByteArray(buffer);
} }
#endif #endif

View file

@ -1,2 +1 @@
#define IDR_RC_TEXT1 201 #define IDR_SCRIPT1 201
#define RC_TEXT 256