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 <QMetaMethod>
#include <QtGlobal>
#include <QDebug>
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++;
}
}

View file

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

View file

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

View file

@ -18,6 +18,7 @@
#include "LuaEngineGui.h"
#include "LuaEngineOS.h"
#include <QApplication>
#include <QDebug>
#include <QFont>
#include <QFile>
#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<const char*>(LockResource(resourceData));
char* buffer = new char[size + 1];
memcpy(buffer, data, size);
buffer[size] = 0;
luaScript = QByteArray(buffer);
luaScript = QByteArray(data, size);
}
#endif

View file

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