From 144e65da73bb1cce6cfbd34f5f08aabe3d2366e9 Mon Sep 17 00:00:00 2001 From: Syping Date: Wed, 2 Oct 2019 19:16:17 +0200 Subject: [PATCH] fix Lua script parsing issues --- src/luaengine/luaengine/LuaEngine.cpp | 16 +++++++++------- src/luaengineapp/app.rc | 6 +++--- src/luaengineapp/luaengineapp.pro | 3 ++- src/luaengineapp/main.cpp | 11 +++++------ src/luaengineapp/resource.h | 3 +-- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/luaengine/luaengine/LuaEngine.cpp b/src/luaengine/luaengine/LuaEngine.cpp index a943c56..e7638f3 100644 --- a/src/luaengine/luaengine/LuaEngine.cpp +++ b/src/luaengine/luaengine/LuaEngine.cpp @@ -20,6 +20,7 @@ #include #include #include +#include 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) { - int returnCode = luaL_dostring(L, data.data()); - return (returnCode == 0) ? true : false; + int result = luaL_loadbuffer(L, data.data(), data.size(), "script"); + if (result == 0) + return (lua_pcall(L, 0, LUA_MULTRET, 0) == 0) ? true : false; + return false; } 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) { - int returnCount = (requireReturn) ? 1 : 0; + int returnCount = (requireReturn) ? LUA_MULTRET : 0; lua_getglobal(L_p, name); 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) { - int returnCount = (requireReturn) ? 1 : 0; + int returnCount = (requireReturn) ? LUA_MULTRET : 0; lua_getglobal(L_p, name); pushVariant(L_p, argument); 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) { - int returnCount = (requireReturn) ? 1 : 0; + int returnCount = (requireReturn) ? LUA_MULTRET : 0; lua_getglobal(L_p, name); for (const QVariant &argument : args) { pushVariant(L_p, argument); @@ -213,12 +216,11 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant) else if (variant.type() == QVariant::StringList) { QStringList stringList = variant.toStringList(); lua_createtable(L_p, 0, stringList.count()); - int top = lua_gettop(L_p); int currentId = 1; for (const QString &string : stringList) { lua_pushnumber(L_p, currentId); lua_pushstring(L_p, string.toUtf8().data()); - lua_settable(L_p, top); + lua_settable(L_p, -3); currentId++; } } diff --git a/src/luaengineapp/app.rc b/src/luaengineapp/app.rc index 0d67dfe..37e8794 100644 --- a/src/luaengineapp/app.rc +++ b/src/luaengineapp/app.rc @@ -1,3 +1,3 @@ -#include -#include -IDR_RC_TEXT1 RC_TEXT "app.lua" +#include "windows.h" +#include "resource.h" +IDR_SCRIPT1 SCRIPT "app.lua.bin" diff --git a/src/luaengineapp/luaengineapp.pro b/src/luaengineapp/luaengineapp.pro index f938acf..d47dec2 100644 --- a/src/luaengineapp/luaengineapp.pro +++ b/src/luaengineapp/luaengineapp.pro @@ -39,6 +39,7 @@ win32: HEADERS += \ resource.h OTHER_FILES += \ - app.lua + app.lua \ + app.rc win32: RC_FILE = app.rc diff --git a/src/luaengineapp/main.cpp b/src/luaengineapp/main.cpp index 75a1424..c7a501b 100644 --- a/src/luaengineapp/main.cpp +++ b/src/luaengineapp/main.cpp @@ -18,6 +18,7 @@ #include "LuaEngineGui.h" #include "LuaEngineOS.h" #include +#include #include #include #ifdef Q_OS_WIN @@ -42,15 +43,13 @@ int main(int argc, char *argv[]) QByteArray luaScript; #ifdef Q_OS_WIN { - HMODULE handle = GetModuleHandle(NULL); - HRSRC resource = FindResource(handle, MAKEINTRESOURCE(IDR_RC_TEXT1), MAKEINTRESOURCE(RC_TEXT)); + HMODULE handle = GetModuleHandleW(NULL); + HRSRC resource = FindResourceW(handle, MAKEINTRESOURCE(IDR_SCRIPT1), L"SCRIPT"); HGLOBAL resourceData = LoadResource(handle, resource); DWORD size = SizeofResource(handle, resource); + qDebug() << size; const char *data = static_cast(LockResource(resourceData)); - char* buffer = new char[size + 1]; - memcpy(buffer, data, size); - buffer[size] = 0; - luaScript = QByteArray(buffer); + luaScript = QByteArray(data, size); } #endif diff --git a/src/luaengineapp/resource.h b/src/luaengineapp/resource.h index bd16694..119df06 100644 --- a/src/luaengineapp/resource.h +++ b/src/luaengineapp/resource.h @@ -1,2 +1 @@ -#define IDR_RC_TEXT1 201 -#define RC_TEXT 256 +#define IDR_SCRIPT1 201