aseprite / api

Scripting API for Aseprite
https://www.aseprite.org/api/
MIT License
241 stars 52 forks source link

Add debugging support for Lua #13

Open sadovsf opened 5 years ago

sadovsf commented 5 years ago

It would be nice to be able to debug plugins in Lua editor like for example Zero Brane Studio: https://studio.zerobrane.com/doc-remote-debugging

This may require to either have lua as dll instead of static library inside exe or at least added luasocket multiplatform library into Aseprite exe so it is possible to start a debug session. Also require needs to be allowed to load moddebug extension

slembcke commented 4 years ago

Going to throw the idea out there again, but debugger.lua works well with Aseprite, and is very simple to embed. Users can drop debugger.lua in their scripts folder and use it from the command line. I've been doing this quite successfully.

OR...

From the native side, you can drop a single .c/.h file pair in the code that embeds the debugger and replace lua_pcall() with dbg_call(). That will still operate from the command line, but all of the debugger's I/O is done through a pair of read/write functions so you can divert it to a console in the UI.

It basically "just works" since it's pure Lua based without requiring any special native hooks. debugging

To run it as a user, copy debugger.lua into the scripts folder. (Optionally remove the .lua so Ase won't treat it as as script)

-- Load the debugger:
dbg = loadfile'debugger'() -- require() not available, workaround
-- Some optional debugger config changes:
dbg.exit = function() dbg.writeln("aborting script") end -- So ctrl-d just aborts the script and let's you know.
dbg.auto_where = 2 -- Optional, print the surrounding code at breakpoints.
-- Now run your code!
app.transaction(function() dbg.call(my_plugin_func) end) -- Wrap the inside of your transactions with dbg.call()

There are some gotchas though:

1) Asesprite doesn't export the module package, so you can't use require() to load it. Not a big deal, but it would be nice to do it the "normal" way. ;) 2) app.transaction() silently swallows errors it seems? This was slightly confusing at first. You have to wrap the inside of the transaction with dbg.call() to catch errors. 3) Aseprite re-throws errors using lua source embedded in the C code. This makes the debugger thing the error happens inside that embedded code, and it kinda gross-ifies the output. Not a big problem though, you can simple move down a stack frame to see the actual error location.

gross-ified

sadovsf commented 4 years ago

Sure, but you cannot compare having full featured Lua IDE with breakpoints watches etc with command line which is more or less just fancy data print. You can as well just output logs in text file. This will help to some extend but has no benefit over full IDE possibility.