Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Lua Scripting Support #1671

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2f19ab8
Next Attempt (2024)!
NPO-197 Sep 25, 2024
8d0de89
New Class "LuaBundle"
NPO-197 Sep 27, 2024
5eccaae
Update CMakeLists.txt find lua package...
NPO-197 Sep 28, 2024
11909c3
Update build-ubuntu.yml
NPO-197 Sep 28, 2024
fbfbf51
Update vcpkg.json
NPO-197 Sep 28, 2024
226053b
Update CMakeLists.txt
NPO-197 Sep 28, 2024
5c00b0b
Update CMakeLists.txt
NPO-197 Sep 28, 2024
0a1e420
Update flake.nix
NPO-197 Sep 28, 2024
e7e15a6
Update CMakeLists.txt
NPO-197 Sep 28, 2024
1cdf447
Update CMakeLists.txt
NPO-197 Sep 28, 2024
f144f75
Using `lua_getextraspace(` instead of redefining lua_State
NPO-197 Sep 29, 2024
487b80c
Update CMakeLists.txt & build-ubuntu.yml
NPO-197 Oct 15, 2024
7283efc
Fixed some broken functions, added Lua test script to tools
NPO-197 Oct 16, 2024
4f1b1d1
fixed LuaSaveState
NPO-197 Oct 17, 2024
4f4619d
Update LuaScriptTest.lua
NPO-197 Oct 17, 2024
ba82d93
Fixing qpainter font to work on MacOS
NPO-197 Oct 18, 2024
92fc1f9
Update LuaMain.cpp
NPO-197 Oct 18, 2024
5376f5a
Update LuaMain.cpp
NPO-197 Oct 18, 2024
3ee3abf
Update LuaMain.cpp
NPO-197 Oct 18, 2024
bc40d89
Update LuaMain.cpp
NPO-197 Oct 18, 2024
d92fb25
replaced QString with const char* while passing args
NPO-197 Oct 19, 2024
f16fe76
Create Lua_Docs.md
NPO-197 Oct 22, 2024
b828181
Update Lua_Docs.md
NPO-197 Oct 22, 2024
db78366
Code Clean Up
NPO-197 Oct 23, 2024
cc8b7f0
Merge branch 'master' of https://github.com/NPO-197/melonDS-lua
NPO-197 Oct 23, 2024
c1921f9
Clean LuaMain
NPO-197 Oct 23, 2024
a6dd236
Code Clean Up
NPO-197 Oct 23, 2024
b16912a
Changed MelonPrint to print,
NPO-197 Oct 27, 2024
a312d4d
Update Lua_Docs.md
NPO-197 Oct 28, 2024
c21d794
Running Lua Script on the main GUI thread
NPO-197 Oct 28, 2024
5196151
Merge branch 'master' into master
NPO-197 Oct 28, 2024
e3e31b8
Code Cleanup
NPO-197 Nov 1, 2024
e7e5ccb
Merge branch 'master' into master
NPO-197 Nov 1, 2024
359fbfb
Merge branch 'melonDS-emu:master' into master
NPO-197 Nov 5, 2024
839f733
Revised Keyboard input, currentDir
NPO-197 Nov 5, 2024
9180d4f
Merge branch 'master' of https://github.com/NPO-197/melonDS-lua
NPO-197 Nov 5, 2024
ecd943d
Merge branch 'melonDS-emu:master' into master
NPO-197 Nov 5, 2024
c43feca
Merge branch 'master' into master
NPO-197 Nov 17, 2024
36a54cb
Merge branch 'melonDS-emu:master' into master
NPO-197 Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
NPO-197 committed Oct 28, 2024
commit c21d794482b588db4020e1b4411a349bd26c8a72
10 changes: 2 additions & 8 deletions src/frontend/qt_sdl/EmuThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 1 addition & 10 deletions src/frontend/qt_sdl/EmuThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ class EmuThread : public QThread
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;

Expand All @@ -136,10 +130,7 @@ class EmuThread : public QThread

void syncVolumeLevel();

void signalLuaPrint(const QString&);
void signalLuaClearConsole();
void signalLuaSaveState(const QString&);
void signalLuaLoadState(const QString&);
void signalLuaUpdate();

private:
void handleMessages();
Expand Down
66 changes: 29 additions & 37 deletions src/frontend/qt_sdl/LuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -160,7 +145,7 @@ void LuaBundle::createLuaState()
}
else //Error loading script
{
emuThread->onLuaPrint(lua_tostring(L,-1));
printText(lua_tostring(L,-1));
}
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -246,15 +237,15 @@ 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);

int lua_MelonClear(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
bundle->getEmuThread()->onLuaClearConsole();
bundle->getluaDialog()->console->clear();
return 0;
}
AddLuaFunction(lua_MelonClear,MelonClear);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/frontend/qt_sdl/LuaMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@ class LuaConsoleDialog: public QDialog
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
Expand Down Expand Up @@ -95,6 +100,8 @@ class LuaBundle
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;
Expand Down
5 changes: 3 additions & 2 deletions src/frontend/qt_sdl/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down