Skip to content

Basic Lua Scripting Support - #1671

Open
NPO-197 wants to merge 90 commits into
melonDS-emu:masterfrom
NPO-197:master
Open

Basic Lua Scripting Support#1671
NPO-197 wants to merge 90 commits into
melonDS-emu:masterfrom
NPO-197:master

Conversation

@NPO-197

@NPO-197 NPO-197 commented Apr 20, 2023

Copy link
Copy Markdown

Here is a proposed starting point to adding Lua script support for MelonDS natively. Currently has bare minimal functionality, as I would like to have others review my work before making to many additions. This is my first project in c++ so please let me know if there are any errors, or parts that I need to re-do. The aim for this is to support accessibility features and trackers, not as a comprehensive debugging tool.

@NPO-197
NPO-197 marked this pull request as ready for review April 21, 2023 11:57
@nadiaholmquist
nadiaholmquist force-pushed the master branch 3 times, most recently from e6c2fb6 to df0ab90 Compare April 23, 2023 16:51

@RSDuck RSDuck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LuaThread is a bit dubious and probably needs some thoughts how this could be implemented better.

Comment thread src/frontend/qt_sdl/OSD.cpp Outdated
Comment thread src/frontend/qt_sdl/LuaMain.cpp Outdated
luaThread = new LuaThread();
connect(luaThread,&LuaThread::signalPrint,console,&LuaConsole::onGetText);
connect(luaThread,&LuaThread::signalClearConsole,console,&LuaConsole::onClear);
connect(buttonPausePlay,&QPushButton::clicked,luaThread,&LuaThread::luaTogglePause);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

placement of spacing here and in a bunch of other places.

Comment thread src/frontend/qt_sdl/LuaMain.cpp Outdated
Comment thread LuaScript.md Outdated
Comment thread src/frontend/qt_sdl/LuaMain.cpp Outdated
Comment thread src/frontend/qt_sdl/LuaMain.cpp Outdated
@NPO-197

NPO-197 commented Apr 28, 2023

Copy link
Copy Markdown
Author

Still some more changes needed but majority of the refactoring is done, no more LuaThread, the Lua state is created and run by the emuThread. This should hopefully address any concerns around potential race conditions. I plan on cleaning up other issues on Sunday.

@NPO-197

NPO-197 commented May 6, 2023

Copy link
Copy Markdown
Author

I think this should be good for another review, I believe I have addressed all the major issues that have been brought up. If there is anything else that I need to change or fix please let me know. There are still more features / functions I'd like to add, but this is the kind of project that could easily lead to feature creep, so I'm gonna cut myself off here.

Comment thread src/frontend/qt_sdl/LuaMain.cpp Outdated
Comment thread src/frontend/qt_sdl/LuaMain.h Outdated
Comment thread src/frontend/qt_sdl/OSD.cpp Outdated
Comment thread src/frontend/qt_sdl/LuaMain.h Outdated
Comment thread src/frontend/qt_sdl/LuaMain.cpp Outdated
Comment thread src/frontend/qt_sdl/main.h Outdated
Comment thread src/frontend/qt_sdl/main.h Outdated
Comment thread src/frontend/qt_sdl/LuaMain.cpp Outdated
@RSDuck

RSDuck commented May 6, 2023

Copy link
Copy Markdown
Member

there's probably still some more things, but this is what I found from a glance.

@NPO-197

NPO-197 commented May 7, 2023

Copy link
Copy Markdown
Author

OK addressed those issues, still some bugs I found that I need to fix, I plan on working on that tomorrow.

@NPO-197

NPO-197 commented May 23, 2023

Copy link
Copy Markdown
Author

OK ready for another review @RSDuck when you have time.

@RSDuck RSDuck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to go over all the changes and fix the spacing. Then once the merge conflicts have been solved I guess we can merge it.

Comment thread LuaScript.md Outdated
@NPO-197

NPO-197 commented Jun 8, 2023

Copy link
Copy Markdown
Author

OK went over everything, double checked to make sure the spacing was good. Fixed spelling errors in the markdown. Will probably need someone else to take a look at the merge conflicts at least for the .yml and CMakeLists files.

@RSDuck

RSDuck commented Jun 12, 2023

Copy link
Copy Markdown
Member

blah something went wrong while resolving the merge conflict.

@RSDuck

RSDuck commented Jun 12, 2023

Copy link
Copy Markdown
Member

hm I don't really understand how dependencies are managed on macOS, I can't find anything on how it's installed in the code.

@RSDuck

RSDuck commented Jun 19, 2024

Copy link
Copy Markdown
Member

To the person who talked about this PR on our IRC:

If you are reading this, we are very sorry, the ban was due to the anti spam bot bot for some reason flagging your messages. Let us know how you can communicate again with us.

@Arisotura

Copy link
Copy Markdown
Member

adding on Generic's message: I removed the ban, so you can come back to the IRC. sorry about it.

@NPO-197

NPO-197 commented Jun 27, 2024

Copy link
Copy Markdown
Author

Hey sorry I have been kind of a ghost for a while! It's been over a year since I started this fork, I'm not super up to date on all the changes since then. I'd like to get this project working again with the current version, since I am able to continue working on it now.
I don't have much experience with merging repo's so I will start by asking:
Is there a way to update this fork so it's not like 100+ commits behind main, or should I try and make a new fork from the current version?
Also I wanted to thank everyone for all the help and feedback so far! I know this isn't exactly a high priority feature, but it's been a wonderful learning experience for me personally, and hopefully others will find it helpful in the future.

@RSDuck

RSDuck commented Jun 27, 2024

Copy link
Copy Markdown
Member

there have been a ton of changes on the frontend especially with regards to multi instance within one process support. At this point it would probably smarter to not rebase/merge and instead start fresh while moving over the more independent parts like the Lua interface.

@NPO-197

NPO-197 commented Jun 28, 2024

Copy link
Copy Markdown
Author

Understood! I'd prefer that option honestly. Most of the work was on the Lua interpreter side of things, I tried to keep the front end stuff to a minimum while working on this since I figured I'd probably need to remake the front end entirely at some point. So I won't be loosing much.

Also gives me a chance to refresh my memory on how everything works.

I'll make a new fork later today, once that's up I'll add a link to the new one and close this pull request.

@jahndan

jahndan commented Jul 9, 2024

Copy link
Copy Markdown

adding on Generic's message: I removed the ban, so you can come back to the IRC. sorry about it.

Hello, sorry I just saw this. I don't really know how to use IRC, and I just saw you guys added a link to the discord so I'll probably stick to that since it's more familiar.

I actually started a very rudimentary merge of this branch to main. (It definitely doesn't work, I was just using the merge conflicts to track which parts of the code base I hadn't yet looked at.) Because I've been busy IRL, I haven't touched anything in about 2 weeks, but before that I was mainly trying to get an overview of all the changes made externally to the Lua scripting module itself (i.e. what kind of integration was even necessary to begin with) as well as trying to understand the general structure of the codebase (and also what pieces got refactored, which were many).

Also gives me a chance to refresh my memory on how everything works.
I'll make a new fork later today, once that's up I'll add a link to the new one and close this pull request.

I don't see the new fork yet on your profile, but I'll push the half-merge (and some of my notes) I've got so far to mine if you want to take a look (I was in the middle of figuring out how/where to associate a Lua scripting engine for each instance, before starting to refactor the Lua module as a class that could have multiple instances). my fork with the partial merge

@NPO-197

NPO-197 commented Jul 10, 2024

Copy link
Copy Markdown
Author

I'll be sure to take a look at your notes, although I think for right now my goal is simply build that can get a helloworld.lua file to run... Once I have that then I'll start working on re-adding all the important features...

Although yes, one of the first things that will need to be figured out is how to handle multiple instances, since that is the biggest change from back when I first started.

I should be able to work on it some today, I originally wanted to start on this like 2 weeks ago but my availability has been... to keep it simple, rather unpredictable as of recently. Hopefully I will have the time to get a good start this week, however, just so everyone is aware, my progress will likely remain somewhat scattered for now.

I'll be sure to post a link to new fork here once I can get, at the bare minimum, "hello world" working on a single instance so we can have a starting point. In the meantime feel free to ask me any questions!

Also thanks again to everyone for the help and patience.

@ag-advania

Copy link
Copy Markdown

Hello, any update on this project ? Thanks !

@NPO-197

NPO-197 commented Aug 30, 2024

Copy link
Copy Markdown
Author

Small update for those interested, (thanks to everyone who reached out about this)
Currently I'm waiting on getting a new PC before I continue working on this project, should be able to get started again by the end of September, by then I should have much more free time to continue working on this, and eventually get this project to a state where it can be merged with the main project. Thanks again to everyone's support and assistance, as well as everyone's patience.

For those who have reached out asking if they can help in any way, I only ask for a little more time to get a minimal viable version working that is compatible with the recent updates to melonDS. Once that basic frame work is setup, I will be sure to let everyone know, and will then gladly appreciate any help!

@gaithern

Copy link
Copy Markdown

Hype!

-Still lots todo! but I have a (mostly) working version up and running!
@Izder456

Copy link
Copy Markdown
Contributor
image Tested the test lua script with ACWW on OpenBSD/amd64 7.8-stable

Not exactly sure what it is supposed to do, but this is what i see. is this correct? the top right "block" of the top screen that gets overlaid via OSD is flickering and giving me a headache.

@NPO-197

NPO-197 commented Feb 20, 2026

Copy link
Copy Markdown
Author

No it's not supposed to do that... 😓
This is what it's supposed to look like, (Here I'm using Metroid Prime, but that shouldn't make a difference...)
image

What are the video settings you are using?

@Izder456

Izder456 commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

No it's not supposed to do that... 😓 This is what it's supposed to look like, (Here I'm using Metroid Prime, but that shouldn't make a difference...)
...
What are the video settings you are using?

image

with the logo added to that dir, its a little better.

image

with the classic renderer it does this too.

also- i noticed the program segfaults if you try to load a script before you load a game. we should really have a guard against that.

@NPO-197

NPO-197 commented Feb 20, 2026

Copy link
Copy Markdown
Author

OK it appears to be an issue with the Open GL rendering that I thought I had fixed, but seems to be broken now. But at least I am able to re-create the issue on my end so I'll work on fixing that. Also yes I am aware of the seg fault issue when there is no game loaded, I will add in some safeguards for that as well.

@Izder456

Copy link
Copy Markdown
Contributor

I feel I should ask here. Is this PR adding a new depend for the program on lua5.4.x?

If so, is it an optional runtime-depend, is it dlopen'd at runtime and thus is non-optional, or is it just a build depend? This matters to me as I maintain the OpenBSD port for melonDS, and this will change how I package it. If this is not documented as a depend already, it should be.

@Amy54Desu

Copy link
Copy Markdown

Seeing how rocky the pull request is I'm really hoping it doesn't get abandoned, it's a very cool thing to have.

@NPO-197

NPO-197 commented Mar 3, 2026

Copy link
Copy Markdown
Author

I do plan to continue working on this PR I have just been quite busy this past month. I should be able to pick things up again next week. Sorry for the lack of communication on my end.

Prevents segfault when trying to read from NDS memory before any ROM has been loaded. Now we properly throw an error to the Lua console.
@NPO-197

NPO-197 commented Mar 7, 2026

Copy link
Copy Markdown
Author

OK fixed the issue with the segfault, ended up being caused when the lua script try's to read from the NDS memory before any game has been loaded. Now properly throws an error to the lua console instead. After I fix the current graphical issues I plan to entirely re-write the test script to test out all of the currently implemented functions. As well as make a check list of any other potential ways a lua script might cause a crash by mistake.

I'll reach out once I need people to run more test again.

NPO-197 added 2 commits March 8, 2026 11:37
Fixes bug with lua overlays not rendering properly when using openGL rendering.
 cleaned up lua scripts, as well as documentation. Fixed `DrawImage` and added `DrawImageRegion` to be same as bizhawk. Fixed missing `usememorydomain` function.
@NPO-197

NPO-197 commented Mar 10, 2026

Copy link
Copy Markdown
Author

OK the graphical issues turned out to be a simple fix in the end, the openGL shader for the lua overlays just wasn't linking properly due to a small change in how the screens are being rendered that I did not catch until now. I rewrote the test script and cleaned up some of the documentation.

@Izder456 if you could run another test on OpenBSD to confirm that everything is working properly now that would be a big help. 💛

@Izder456

Izder456 commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

OK the graphical issues turned out to be a simple fix in the end, the openGL shader for the lua overlays just wasn't linking properly due to a small change in how the screens are being rendered that I did not catch until now. I rewrote the test script and cleaned up some of the documentation.

@Izder456 if you could run another test on OpenBSD to confirm that everything is working properly now that would be a big help. 💛

Seems to work on my end. Thanks! (Also- you might wanna document the new build depend on lua 5.4 in BUILD.md)

image

NPO-197 added 3 commits March 10, 2026 12:10
clean up of code to fit contribution guidelines more closely. Fixed some memory functions as well.
@NPO-197

NPO-197 commented Mar 10, 2026

Copy link
Copy Markdown
Author

Should be good to go for anyone to review / test at this point. Let me know if there is anything else I can fix / clean up. 😄

@Veddy1674

Copy link
Copy Markdown

I have forked this repository and tried my own way of adding support for Lua scripting, if anyone is still interested, you could look up my alternative, which uses sol2 and lua 5.4, adds a "scripting" button which lets you execute scripts in-game, modules "memory" to read/write memory, "emu" to pause/unpause, save and load states, functions like get_state to get the raw pixels on screen, and as you may realize, it's not focused on TAS-making, as most scripting tools in emulators are, but more on dataset-making for AI training and such, but feel free to look it up and make it fit your needs.

Still experimental though

@DarthMDev

Copy link
Copy Markdown

im interested in this feature whats left for this to be merged into upstream 👀

@AntonioND

AntonioND commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

I've tested this a bit, and I see some issues:

  • I need some way to start the script at boot. Something like melonDS --lua myscript.lua rom.nds. The ROM should start paused if a Lua script is provided.
  • I need some way to control the number of frames that the emulator runs, and keep it paused at any other time.
  • I need to at least have some way to create screenshots and to shut down melonDS.

For context, most of my tests involve running the emulator for a few frames, maybe pressing a button and releasing it later, taking a screenshot and closing the emulator.

I'm not sure how feasible all of this is with the current melonDS, though.

I did something like this for my own emulator a few years ago, and I had to modify a few things to get it working: https://github.com/AntonioND/giibiiadvance/blob/ccb40c3cf7d9efec538858a36d4ab69bb4d0ce5c/source/lua_handler.c#L263-L270

NPO-197 and others added 3 commits August 25, 2026 09:26
Memory.write would only write zeros, I also fixed the M64 JoyStick example script.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.