mirror of
https://github.com/x-tools-author/x-tools.git
synced 2025-09-15 15:28:40 +08:00
feat: add string parameter lua script
This commit is contained in:
parent
a554372a9c
commit
931b63ef8c
@ -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();
|
||||
}
|
||||
@ -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);
|
||||
};
|
||||
@ -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<int>(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);
|
||||
|
||||
|
||||
@ -36,6 +36,7 @@ private:
|
||||
private:
|
||||
void onDefaultLuaScriptTriggered();
|
||||
void onCheckSumLuaScriptTriggered();
|
||||
void onDefaultLuaScriptStringTriggered();
|
||||
void onTestFormatChanged();
|
||||
void onResultFormatChanged();
|
||||
|
||||
|
||||
@ -26,47 +26,28 @@
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item row="2" column="3">
|
||||
<item row="2" column="4">
|
||||
<widget class="QPushButton" name="pushButtonTest">
|
||||
<property name="text">
|
||||
<string>Test</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0" colspan="4">
|
||||
<widget class="QPlainTextEdit" name="plainTextEditScript"/>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QComboBox" name="comboBoxTestFormat"/>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Result data</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QComboBox" name="comboBoxResultFormat"/>
|
||||
</item>
|
||||
<item row="3" column="2">
|
||||
<widget class="QLineEdit" name="lineEditResultData">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="2">
|
||||
<widget class="QLineEdit" name="lineEditTestData"/>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Test data</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="4">
|
||||
<item row="3" column="3">
|
||||
<widget class="QLineEdit" name="lineEditResultData">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1" colspan="4">
|
||||
<widget class="QLabel" name="labelInfo">
|
||||
<property name="styleSheet">
|
||||
<string notr="true">color:red</string>
|
||||
@ -76,13 +57,80 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="3">
|
||||
<item row="3" column="1">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Result data</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="3">
|
||||
<widget class="QLineEdit" name="lineEditTestData"/>
|
||||
</item>
|
||||
<item row="3" column="4">
|
||||
<widget class="QCheckBox" name="checkBoxBypass">
|
||||
<property name="text">
|
||||
<string>Bypass</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="2">
|
||||
<widget class="QComboBox" name="comboBoxTestFormat"/>
|
||||
</item>
|
||||
<item row="3" column="2">
|
||||
<widget class="QComboBox" name="comboBoxResultFormat"/>
|
||||
</item>
|
||||
<item row="0" column="1" colspan="4">
|
||||
<widget class="QTabWidget" name="tabWidgetLua">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="tabTable">
|
||||
<attribute name="title">
|
||||
<string>Table Parameters</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<property name="leftMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QPlainTextEdit" name="plainTextEditScriptTable"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tabString">
|
||||
<attribute name="title">
|
||||
<string>String Parameters</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<property name="leftMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QPlainTextEdit" name="plainTextEditScriptString"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
|
||||
@ -21,5 +21,6 @@
|
||||
<file>res/icons/dock_to_right.svg</file>
|
||||
<file>res/scripts/lua/default.lua</file>
|
||||
<file>res/scripts/lua/check_sum.lua</file>
|
||||
<file>res/scripts/lua/default_string.lua</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user