Running Lua Script on the main GUI thread

Switched running the LUA script from the emuThread to the main window / main GUI thread... Should have just done this from the start, this makes things much simpler to implement, and will make future planned GUI functions much easier to add.
This commit is contained in:
NPO 2024-10-28 13:15:56 -04:00
parent a312d4d2e7
commit c21d794482
5 changed files with 42 additions and 57 deletions

View File

@ -441,15 +441,9 @@ void EmuThread::run()
}
handleMessages();
LuaConsoleDialog* dialog = emuInstance->getMainWindow()->getLuaDialog();
//Lua Script Stuff (-for now happens at the end of each frame regardless of emuStatus)
if (dialog!=nullptr)
{
LuaBundle* lua = dialog->getLuaBundle();
lua->createLuaState();//Create LuaState if needed
lua->luaUpdate(); //"_Update()" gets called in current lua script
}
emit signalLuaUpdate();
}
file = Platform::OpenLocalFile("rtc.bin", Platform::FileMode::Write);

View File

@ -107,12 +107,6 @@ public:
void deinitContext();
void updateVideoSettings() { videoSettingsDirty = true; }
void onLuaPrint(const QString&);
void onLuaClearConsole();
void onLuaLoadState(const QString&);
void onLuaSaveState(const QString&);
void onLuaLayoutChange();
int FrontBuffer = 0;
QMutex FrontBufferLock;
@ -136,10 +130,7 @@ signals:
void syncVolumeLevel();
void signalLuaPrint(const QString&);
void signalLuaClearConsole();
void signalLuaSaveState(const QString&);
void signalLuaLoadState(const QString&);
void signalLuaUpdate();
private:
void handleMessages();

View File

@ -12,34 +12,6 @@
#include <NDS_Header.h>
#include "main.h"
//EmuThread Signals
void EmuThread::onLuaPrint(const QString& string)
{
emit signalLuaPrint(string);
}
void EmuThread::onLuaClearConsole()
{
emit signalLuaClearConsole();
}
void EmuThread::onLuaLoadState(const QString& string)
{
emit signalLuaLoadState(string);
}
/*
void EmuThread::onLuaLayoutChange()
{
//TODO:
//emit screenLayoutChange();
}
*/
void EmuThread::onLuaSaveState(const QString& string)
{
emit signalLuaSaveState(string);
}
LuaBundle::LuaBundle(LuaConsoleDialog* dialog, EmuInstance* inst)
{
@ -74,8 +46,6 @@ LuaConsoleDialog::LuaConsoleDialog(QWidget* parent) : QDialog(parent)
connect(buttonOpenScript,&QPushButton::clicked,this,&LuaConsoleDialog::onOpenScript);
connect(buttonStartStop,&QPushButton::clicked,this,&LuaConsoleDialog::onStop);
connect(buttonPausePlay,&QPushButton::clicked,this,&LuaConsoleDialog::onPausePlay);
connect(bundle->getEmuThread(),&EmuThread::signalLuaPrint,console,&LuaConsole::onGetText);
connect(bundle->getEmuThread(),&EmuThread::signalLuaClearConsole,console,&LuaConsole::onClear);
this->setWindowTitle("Lua Script");
}
@ -107,6 +77,21 @@ void LuaConsole::onGetText(const QString& string)
bar->setValue(bar->maximum());
}
void LuaBundle::printText(QString string)
{
this->luaDialog->console->onGetText(string);
}
void LuaConsoleDialog::onLuaSaveState(QString string)
{
emit signalLuaSaveState(string);
}
void LuaConsoleDialog::onLuaLoadState(QString string)
{
emit signalLuaLoadState(string);
}
void LuaConsole::onClear()
{
this->clear();
@ -160,7 +145,7 @@ void LuaBundle::createLuaState()
}
else //Error loading script
{
emuThread->onLuaPrint(lua_tostring(L,-1));
printText(lua_tostring(L,-1));
}
}
@ -175,20 +160,26 @@ void LuaConsoleDialog::onPausePlay()
bundle->flagPause = !bundle->flagPause;
}
void LuaConsoleDialog::onLuaUpdate()
{
bundle->createLuaState();
bundle->luaUpdate();
}
//Gets Called once a frame
void LuaBundle::luaUpdate()
{
if (!luaState || flagPause) return;
if (lua_getglobal(luaState,"_Update")!=LUA_TFUNCTION)
{
emuThread->onLuaPrint("No \"_Update\" Function found, pausing script...");
printText("No \"_Update\" Function found, pausing script...");
flagPause = true;
return;
}
if (lua_pcall(luaState,0,0,0)!=0)
{
//Handel Errors
emuThread->onLuaPrint(lua_tostring(luaState,-1));
printText(lua_tostring(luaState,-1));
luaState = nullptr;
}
}
@ -246,7 +237,7 @@ int lua_MelonPrint(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
const char* string = luaL_checkstring(L,1);
bundle->getEmuThread()->onLuaPrint((QString)string);
bundle->printText((QString)string);
return 0;
}
AddLuaFunction(lua_MelonPrint,print);
@ -254,7 +245,7 @@ AddLuaFunction(lua_MelonPrint,print);
int lua_MelonClear(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
bundle->getEmuThread()->onLuaClearConsole();
bundle->getluaDialog()->console->clear();
return 0;
}
AddLuaFunction(lua_MelonClear,MelonClear);
@ -378,7 +369,7 @@ int Lua_StateSave(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
const char* filename = luaL_checkstring(L,1);
bundle->getEmuThread()->onLuaSaveState((QString)filename);
bundle->getluaDialog()->onLuaSaveState((QString)filename);
return 0;
}
AddLuaFunction(Lua_StateSave,StateSave);
@ -387,7 +378,8 @@ int Lua_StateLoad(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
const char* filename = luaL_checkstring(L,1);
bundle->getEmuThread()->onLuaLoadState((QString)filename);
bundle->getluaDialog()->onLuaLoadState((QString)filename);
return 0;
}
AddLuaFunction(Lua_StateLoad,StateLoad);

View File

@ -31,17 +31,22 @@ public:
QPushButton* buttonPausePlay;
QScrollBar* bar;
bool flagClosed;
void onLuaSaveState(QString);
void onLuaLoadState(QString);
protected:
void closeEvent(QCloseEvent *event) override;
LuaBundle* bundle;
signals:
void signalNewLua();
void signalClosing();
void signalLuaSaveState(const QString&);
void signalLuaLoadState(const QString&);
public slots:
//void onStartStop();
void onOpenScript();
void onStop();
void onPausePlay();
void onLuaUpdate();
};
//Based on ScreenLayout::GetScreenTransforms
@ -95,6 +100,8 @@ public:
lua_State* getLuaState(){return luaState;};
EmuThread* getEmuThread(){return emuThread;};
EmuInstance* getEmuInstance(){return emuInstance;};
LuaConsoleDialog* getluaDialog(){return luaDialog;};
void printText(QString string);
void createLuaState();
void luaUpdate();
bool flagPause = false;

View File

@ -1689,8 +1689,9 @@ void MainWindow::onOpenLuaScript()
return;
luaDialog = new LuaConsoleDialog(this);
luaDialog->show();
connect(emuThread,&EmuThread::signalLuaSaveState,this,&MainWindow::onLuaSaveState);
connect(emuThread,&EmuThread::signalLuaLoadState,this,&MainWindow::onLuaLoadState);
connect(emuThread,&EmuThread::signalLuaUpdate,luaDialog,&LuaConsoleDialog::onLuaUpdate);
connect(luaDialog,&LuaConsoleDialog::signalLuaSaveState,this,&MainWindow::onLuaSaveState);
connect(luaDialog,&LuaConsoleDialog::signalLuaLoadState,this,&MainWindow::onLuaLoadState);
}
void MainWindow::onEnableCheats(bool checked)