From 2afcfc45893f818a4e1dd9d5e57b96deba0024df Mon Sep 17 00:00:00 2001 From: gocha Date: Sun, 5 Aug 2012 15:45:56 +0900 Subject: [PATCH] Lua: add basic callback functions; emu.registerbefore, emu.registerafter, emu.registerstart, emu.registerexit --- cpu.cpp | 5 +++++ cpuexec.cpp | 9 +++++++++ lua-engine.cpp | 15 ++++++++------- win32/wsnes9x.cpp | 4 ++++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/cpu.cpp b/cpu.cpp index 4bdb92b2..7d976deb 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -186,6 +186,7 @@ #include "snapshot.h" #include "cheats.h" #include "logger.h" +#include "lua-engine.h" #ifdef DEBUGGER #include "debug.h" #endif @@ -302,6 +303,10 @@ void S9xReset (void) S9xResetSRTC(); S9xInitCheatData(); + +#ifdef HAVE_LUA + CallRegisteredLuaFunctions(LUACALL_ONSTART); +#endif } void S9xSoftReset (void) diff --git a/cpuexec.cpp b/cpuexec.cpp index 8607523d..831cac90 100644 --- a/cpuexec.cpp +++ b/cpuexec.cpp @@ -183,6 +183,7 @@ #include "apu/apu.h" #include "fxemu.h" #include "snapshot.h" +#include "lua-engine.h" #ifdef DEBUGGER #include "debug.h" #include "missing.h" @@ -322,6 +323,10 @@ static inline void StartS9xMainLoop (void) pad_read_last = pad_read; pad_read = FALSE; +#ifdef HAVE_LUA + CallRegisteredLuaFunctions(LUACALL_BEFOREEMULATION); +#endif + IPPU.InMainLoop = TRUE; } @@ -331,6 +336,10 @@ static inline void EndS9xMainLoop (void) if(!pad_read) IPPU.PadIgnoredFrames++; +#ifdef HAVE_LUA + CallRegisteredLuaFunctions(LUACALL_AFTEREMULATION); +#endif + IPPU.InMainLoop = FALSE; } diff --git a/lua-engine.cpp b/lua-engine.cpp index 2ec7510c..870c59c9 100644 --- a/lua-engine.cpp +++ b/lua-engine.cpp @@ -2,6 +2,7 @@ #ifdef HAVE_LUA #include "port.h" +#include "snes9x.h" #include "lua-engine.h" #include #include @@ -319,7 +320,7 @@ DEFINE_LUA_FUNCTION(emu_registerexit, "func") lua_setfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_BEFOREEXIT]); StopScriptIfFinished(luaStateToUIDMap[L]); return 1; -}/* +} DEFINE_LUA_FUNCTION(emu_registerstart, "func") { if (!lua_isnil(L,1)) @@ -329,11 +330,11 @@ DEFINE_LUA_FUNCTION(emu_registerstart, "func") lua_insert(L,1); lua_pushvalue(L,-1); // copy the function so we can also call it lua_setfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_ONSTART]); - if (!lua_isnil(L,-1) && ((Genesis_Started)||(SegaCD_Started)||(_32X_Started))) + if (!lua_isnil(L,-1) && !Settings.StopEmulation) lua_call(L,0,0); // call the function now since the game has already started and this start function hasn't been called yet StopScriptIfFinished(luaStateToUIDMap[L]); return 1; -}*/ +} DEFINE_LUA_FUNCTION(gui_register, "func") { if (!lua_isnil(L,1)) @@ -3688,10 +3689,10 @@ static const struct luaL_reg emulib [] = // {"lagged", emu_lagged}, // {"emulating", emu_emulating}, // {"atframeboundary", emu_atframeboundary}, -// {"registerbefore", emu_registerbefore}, -// {"registerafter", emu_registerafter}, -// {"registerstart", emu_registerstart}, -// {"registerexit", emu_registerexit}, + {"registerbefore", emu_registerbefore}, + {"registerafter", emu_registerafter}, + {"registerstart", emu_registerstart}, + {"registerexit", emu_registerexit}, {"persistglobalvariables", emu_persistglobalvariables}, // {"message", emu_message}, {"print", print}, // sure, why not diff --git a/win32/wsnes9x.cpp b/win32/wsnes9x.cpp index 171e2a1a..6321dbaf 100644 --- a/win32/wsnes9x.cpp +++ b/win32/wsnes9x.cpp @@ -213,6 +213,7 @@ #include "../controls.h" #include "../conffile.h" #include "../statemanager.h" +#include "../lua-engine.h" #include "AVIOutput.h" #include "InputCustom.h" #include "CWindow.h" @@ -3686,6 +3687,9 @@ int WINAPI WinMain( loop_exit: +#ifdef HAVE_LUA + StopAllLuaScripts(); +#endif CloseAllToolWindows(); Settings.StopEmulation = TRUE;