diff --git a/src/common/luarunner.cpp b/src/common/luarunner.cpp index 9853b548..69b145ff 100644 --- a/src/common/luarunner.cpp +++ b/src/common/luarunner.cpp @@ -23,9 +23,9 @@ LuaRunner::~LuaRunner() } } -QByteArray LuaRunner::execute(const QString &script, const QByteArray &data) +QByteArray LuaRunner::execute(const QString &script, const QByteArray &data, int type) { - emit invokeExecute(script, data); + emit invokeExecute(script, data, type); QEventLoop loop; connect(this, &LuaRunner::executed, &loop, &QEventLoop::quit); @@ -48,8 +48,8 @@ void LuaRunner::run() connect(this, &LuaRunner::invokeExecute, obj, - [this](const QString &functionName, const QByteArray &data) { - executeInThread(functionName, data); + [this](const QString &functionName, const QByteArray &data, int type) { + executeInThread(functionName, data, type); }); exec(); @@ -58,7 +58,7 @@ void LuaRunner::run() obj = Q_NULLPTR; } -void LuaRunner::executeInThread(const QString &script, const QByteArray &data) +void LuaRunner::executeInThread(const QString &script, const QByteArray &data, int type) { m_error.clear(); m_result.clear(); @@ -79,6 +79,15 @@ void LuaRunner::executeInThread(const QString &script, const QByteArray &data) return; } + if (type == ParameterTypeTable) { + executeInThreadTable(script, data); + } else if (type == ParameterTypeString) { + executeInThreadString(script, data); + } +} + +void LuaRunner::executeInThreadTable(const QString &script, const QByteArray &data) +{ // 创建 Lua 表(数组)来存储字节数据 lua_newtable(m_lua); for (int i = 0; i < data.size(); ++i) { @@ -139,4 +148,27 @@ void LuaRunner::executeInThread(const QString &script, const QByteArray &data) lua_pop(m_lua, 1); // 清除数据值 emit executed(); +} + +void LuaRunner::executeInThreadString(const QString &script, const QByteArray &data) +{ + lua_pushstring(m_lua, data); + if (lua_pcall(m_lua, 1, 1, 0) != LUA_OK) { + const char *err = lua_tostring(m_lua, -1); + m_error = QString("Failed to call the lua script: %1").arg(err); + lua_pop(m_lua, 1); + emit executed(); + return; + } + + if (!lua_isstring(m_lua, -1)) { + m_error = "Lua function 'data_handler' must return a string."; + lua_pop(m_lua, 1); + emit executed(); + return; + } + + m_result = lua_tostring(m_lua, -1); + lua_pop(m_lua, 1); + emit executed(); } \ No newline at end of file diff --git a/src/common/luarunner.h b/src/common/luarunner.h index 2fcff521..26b96246 100644 --- a/src/common/luarunner.h +++ b/src/common/luarunner.h @@ -19,11 +19,14 @@ extern "C" { class LuaRunner : public QThread { Q_OBJECT +public: + enum ParameterType { ParameterTypeTable, ParameterTypeString }; + public: explicit LuaRunner(QObject *parent = nullptr); ~LuaRunner() override; - QByteArray execute(const QString &script, const QByteArray &data); + QByteArray execute(const QString &script, const QByteArray &data, int type); QString error() const; protected: @@ -35,8 +38,10 @@ private: lua_State *m_lua{nullptr}; private: - Q_SIGNAL void invokeExecute(const QString &functionName, const QByteArray &data); + Q_SIGNAL void invokeExecute(const QString &functionName, const QByteArray &data, int type); Q_SIGNAL void executed(); - void executeInThread(const QString &script, const QByteArray &data); + void executeInThread(const QString &script, const QByteArray &data, int type); + void executeInThreadTable(const QString &script, const QByteArray &data); + void executeInThreadString(const QString &script, const QByteArray &data); }; \ No newline at end of file diff --git a/src/page/panels/common/luapanel.cpp b/src/page/panels/common/luapanel.cpp index 8ec73bfe..8b61ac92 100644 --- a/src/page/panels/common/luapanel.cpp +++ b/src/page/panels/common/luapanel.cpp @@ -16,10 +16,12 @@ struct LuaPanelDataKeys { - const QString Script = "script"; - const QString TestFormat = "testFormat"; - const QString ResultFormat = "resultFormat"; - const QString Bypass = "bypass"; + const QString type = "type"; + const QString scriptTable = "scriptTable"; + const QString scriptString = "scriptString"; + const QString testFormat = "testFormat"; + const QString resultFormat = "resultFormat"; + const QString bypass = "bypass"; const QString testData = "testData"; }; @@ -33,6 +35,8 @@ LuaPanel::LuaPanel(QWidget *parent) m_menu = new QMenu(this); m_menu->addAction(tr("Default Lua Script"), this, &LuaPanel::onDefaultLuaScriptTriggered); m_menu->addAction(tr("Checksum Lua Script"), this, &LuaPanel::onCheckSumLuaScriptTriggered); + m_menu->addSeparator(); + m_menu->addAction(tr("Lua Script(String)"), this, &LuaPanel::onDefaultLuaScriptStringTriggered); onDefaultLuaScriptTriggered(); m_luaRunner = new LuaRunner(this); @@ -56,10 +60,13 @@ QVariantMap LuaPanel::save() const { QVariantMap map = Panel::save(); LuaPanelDataKeys keys; - map[keys.Script] = ui->plainTextEditScript->toPlainText(); - map[keys.TestFormat] = ui->comboBoxTestFormat->currentData().toInt(); - map[keys.ResultFormat] = ui->comboBoxResultFormat->currentData().toInt(); - map[keys.Bypass] = ui->checkBoxBypass->isChecked(); + + map[keys.type] = ui->tabWidgetLua->currentIndex(); + map[keys.scriptTable] = ui->plainTextEditScriptTable->toPlainText(); + map[keys.scriptString] = ui->plainTextEditScriptString->toPlainText(); + map[keys.testFormat] = ui->comboBoxTestFormat->currentData().toInt(); + map[keys.resultFormat] = ui->comboBoxResultFormat->currentData().toInt(); + map[keys.bypass] = ui->checkBoxBypass->isChecked(); map[keys.testData] = ui->lineEditTestData->text(); return map; } @@ -71,22 +78,31 @@ void LuaPanel::load(const QVariantMap ¶meters) int defaultTestFormat = static_cast(TextFormat::Hex); QString defaultTestData = bytes2string(m_testData, defaultTestFormat); LuaPanelDataKeys keys; - ui->plainTextEditScript->setPlainText(parameters[keys.Script].toString()); - if (ui->plainTextEditScript->toPlainText().isEmpty()) { + + int type = parameters.value(keys.type, 0).toInt(); + ui->tabWidgetLua->setCurrentIndex(type); + + ui->plainTextEditScriptTable->setPlainText(parameters[keys.scriptTable].toString()); + if (ui->plainTextEditScriptTable->toPlainText().isEmpty()) { onDefaultLuaScriptTriggered(); } - int testFormat = parameters.value(keys.TestFormat, defaultTestFormat).toInt(); + ui->plainTextEditScriptString->setPlainText(parameters[keys.scriptString].toString()); + if (ui->plainTextEditScriptString->toPlainText().isEmpty()) { + onDefaultLuaScriptStringTriggered(); + } + + int testFormat = parameters.value(keys.testFormat, defaultTestFormat).toInt(); int index = ui->comboBoxTestFormat->findData(testFormat); ui->comboBoxTestFormat->setCurrentIndex(index); setupTextFormatValidator(ui->lineEditTestData, testFormat); - int resultFormat = parameters.value(keys.ResultFormat, testFormat).toInt(); + int resultFormat = parameters.value(keys.resultFormat, testFormat).toInt(); index = ui->comboBoxResultFormat->findData(resultFormat); ui->comboBoxResultFormat->setCurrentIndex(index); setupTextFormatValidator(ui->lineEditResultData, resultFormat); - bool bypass = parameters.value(keys.Bypass, false).toBool(); + bool bypass = parameters.value(keys.bypass, false).toBool(); ui->checkBoxBypass->setChecked(bypass); QString testData = parameters.value(keys.testData, defaultTestData).toString(); @@ -103,7 +119,12 @@ QByteArray LuaPanel::handleData(const QByteArray &data) const if (ui->checkBoxBypass->isChecked()) { return data; } else { - return m_luaRunner->execute(ui->plainTextEditScript->toPlainText(), data); + int type = ui->tabWidgetLua->currentIndex(); + if (type == LuaRunner::ParameterTypeTable) { + return m_luaRunner->execute(ui->plainTextEditScriptTable->toPlainText(), data, type); + } else { + return m_luaRunner->execute(ui->plainTextEditScriptString->toPlainText(), data, type); + } } } @@ -117,9 +138,11 @@ void LuaPanel::onDefaultLuaScriptTriggered() QFile file(":/res/scripts/lua/default.lua"); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QString script = file.readAll(); - ui->plainTextEditScript->setPlainText(script); + ui->plainTextEditScriptTable->setPlainText(script); file.close(); } + + ui->tabWidgetLua->setCurrentIndex(0); } void LuaPanel::onCheckSumLuaScriptTriggered() @@ -127,9 +150,23 @@ void LuaPanel::onCheckSumLuaScriptTriggered() QFile file(":/res/scripts/lua/check_sum.lua"); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QString script = file.readAll(); - ui->plainTextEditScript->setPlainText(script); + ui->plainTextEditScriptTable->setPlainText(script); file.close(); } + + ui->tabWidgetLua->setCurrentIndex(0); +} + +void LuaPanel::onDefaultLuaScriptStringTriggered() +{ + QFile file(":/res/scripts/lua/default_string.lua"); + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QString script = file.readAll(); + ui->plainTextEditScriptString->setPlainText(script); + file.close(); + } + + ui->tabWidgetLua->setCurrentIndex(1); } void LuaPanel::onTestFormatChanged() @@ -150,11 +187,19 @@ void LuaPanel::onTestButtonClicked() { ui->labelInfo->clear(); - QString script = ui->plainTextEditScript->toPlainText(); + int type = ui->tabWidgetLua->currentIndex(); + QString script; + if (type == LuaRunner::ParameterTypeTable) { + script = ui->plainTextEditScriptTable->toPlainText(); + } else { + script = ui->plainTextEditScriptString->toPlainText(); + } + int testFormat = ui->comboBoxTestFormat->currentData().toInt(); QByteArray data = string2bytes(ui->lineEditTestData->text(), testFormat); int resultFormat = ui->comboBoxResultFormat->currentData().toInt(); - m_resultData = m_luaRunner->execute(script, data); + + m_resultData = m_luaRunner->execute(script, data, type); QString str = bytes2string(m_resultData, resultFormat); ui->lineEditResultData->setText(str); diff --git a/src/page/panels/common/luapanel.h b/src/page/panels/common/luapanel.h index ff78b603..97ae8555 100644 --- a/src/page/panels/common/luapanel.h +++ b/src/page/panels/common/luapanel.h @@ -36,6 +36,7 @@ private: private: void onDefaultLuaScriptTriggered(); void onCheckSumLuaScriptTriggered(); + void onDefaultLuaScriptStringTriggered(); void onTestFormatChanged(); void onResultFormatChanged(); diff --git a/src/page/panels/common/luapanel.ui b/src/page/panels/common/luapanel.ui index 0f5764b0..3c0f0465 100644 --- a/src/page/panels/common/luapanel.ui +++ b/src/page/panels/common/luapanel.ui @@ -26,47 +26,28 @@ 0 - + Test - - - - - - - - - Result data - - - - - - - - - - true - - - - - - - Test data - + + + + true + + + + color:red @@ -76,13 +57,80 @@ - + + + + Result data + + + + + + + Bypass + + + + + + + + + + 0 + + + + Table Parameters + + + + 4 + + + 4 + + + 4 + + + 4 + + + + + + + + + String Parameters + + + + 4 + + + 4 + + + 4 + + + 4 + + + + + + + + diff --git a/xTools.qrc b/xTools.qrc index e5f00e0f..b346e0e9 100644 --- a/xTools.qrc +++ b/xTools.qrc @@ -21,5 +21,6 @@ res/icons/dock_to_right.svg res/scripts/lua/default.lua res/scripts/lua/check_sum.lua + res/scripts/lua/default_string.lua