diff --git a/Core/Console.cpp b/Core/Console.cpp index 6dca0987..9a9f3c1d 100644 --- a/Core/Console.cpp +++ b/Core/Console.cpp @@ -546,6 +546,8 @@ void Console::Run() _hdPackBuilder.reset(); _hdData.reset(); + _debugger.reset(); + _stopLock.Release(); _runLock.Release(); diff --git a/Core/Debugger.cpp b/Core/Debugger.cpp index 19fc3217..72c8dad1 100644 --- a/Core/Debugger.cpp +++ b/Core/Debugger.cpp @@ -114,6 +114,17 @@ Debugger::~Debugger() _stopFlag = true; Console::Pause(); + + { + auto lock = _scriptLock.AcquireSafe(); + for(shared_ptr script : _scripts) { + //Send a ScriptEnded event to all active scripts + script->ProcessEvent(EventType::ScriptEnded); + } + _scripts.clear(); + _hasScript = false; + } + if(Debugger::Instance == this) { Debugger::Instance = nullptr; } @@ -1245,6 +1256,9 @@ int Debugger::LoadScript(string name, string content, int32_t scriptId) return script->GetScriptId() == scriptId; }); if(result != _scripts.end()) { + //Send a ScriptEnded event before reloading the code + (*result)->ProcessEvent(EventType::ScriptEnded); + (*result)->LoadScript(name, content, this); return scriptId; } @@ -1257,7 +1271,14 @@ void Debugger::RemoveScript(int32_t scriptId) { DebugBreakHelper helper(this); auto lock = _scriptLock.AcquireSafe(); - _scripts.erase(std::remove_if(_scripts.begin(), _scripts.end(), [=](const shared_ptr& script) { return script->GetScriptId() == scriptId; }), _scripts.end()); + _scripts.erase(std::remove_if(_scripts.begin(), _scripts.end(), [=](const shared_ptr& script) { + if(script->GetScriptId() == scriptId) { + //Send a ScriptEnded event before unloading the script + script->ProcessEvent(EventType::ScriptEnded); + return true; + } + return false; + }), _scripts.end()); _hasScript = _scripts.size() > 0; } diff --git a/Core/DebuggerTypes.h b/Core/DebuggerTypes.h index ec66523d..82f29f3a 100644 --- a/Core/DebuggerTypes.h +++ b/Core/DebuggerTypes.h @@ -147,6 +147,7 @@ enum class EventType StateSaved = 7, InputPolled = 8, SpriteZeroHit = 9, + ScriptEnded = 10, EventTypeSize }; diff --git a/Core/LuaApi.cpp b/Core/LuaApi.cpp index 994f4c12..76593370 100644 --- a/Core/LuaApi.cpp +++ b/Core/LuaApi.cpp @@ -161,6 +161,7 @@ int LuaApi::GetLibrary(lua_State *lua) lua_pushintvalue(stateLoaded, EventType::StateLoaded); lua_pushintvalue(stateSaved, EventType::StateSaved); lua_pushintvalue(inputPolled, EventType::InputPolled); + lua_pushintvalue(scriptEnded, EventType::ScriptEnded); lua_settable(lua, -3); lua_pushliteral(lua, "executeCountType"); diff --git a/Docs/content/apireference/Enums.md b/Docs/content/apireference/Enums.md index 76a08bf8..b346a1d8 100644 --- a/Docs/content/apireference/Enums.md +++ b/Docs/content/apireference/Enums.md @@ -22,7 +22,9 @@ endFrame = 4, Triggered at the end of a frame (cycle 0, scanline 241) codeBreak = 5, Triggered when code execution breaks (e.g due to a breakpoint, etc.) stateLoaded = 6, Triggered when a user manually loads a savestate stateSaved = 7, Triggered when a user manually saves a savestate -inputPolled = 8 Triggered when the emulation core polls the state of the input devices for the next frame +inputPolled = 8, Triggered when the emulation core polls the state of the input devices for the next frame +spriteZeroHit = 9, Triggered when the PPU sets the sprite zero hit flag +scriptEnded = 10 Triggered when the current Lua script ends (script window closed, execution stopped, etc.) ``` **Description** diff --git a/GUI.NET/Debugger/frmScript.cs b/GUI.NET/Debugger/frmScript.cs index f89b0596..4085f7ac 100644 --- a/GUI.NET/Debugger/frmScript.cs +++ b/GUI.NET/Debugger/frmScript.cs @@ -504,7 +504,7 @@ namespace Mesen.GUI.Debugger new List {"func","emu.getRomInfo","emu.getRomInfo()","","*Table* Information about the current ROM","Returns information about the ROM file that is currently running."}, new List {"func","emu.getScriptDataFolder","emu.getScriptDataFolder()","","*String* The script’s data folder.","This function returns the path to a unique folder (based on the script’s filename) where the script should store its data (if any data needs to be saved).\nThe data will be saved in subfolders inside the LuaScriptData folder in Mesen’s home folder."}, new List {"func","emu.takeScreenshot","emu.takeScreenshot()","","*String* A binary string containing a PNG image.","Takes a screenshot and returns a PNG file as a string.\nThe screenshot is not saved to the disk."}, - new List {"enum","emu.eventType","emu.eventType.[value]","","","Values:\nreset = 0,\nnmi = 1,\nirq = 2,\nstartFrame = 3,\nendFrame = 4,\ncodeBreak = 5\nstateLoaded = 6,\nstateSaved = 7,\ninputPolled = 8\n\nUsed by addEventCallback / removeEventCallback calls."}, + new List {"enum","emu.eventType","emu.eventType.[value]","","","Values:\nreset = 0,\nnmi = 1,\nirq = 2,\nstartFrame = 3,\nendFrame = 4,\ncodeBreak = 5\nstateLoaded = 6,\nstateSaved = 7,\ninputPolled = 8,\nspriteZeroHit = 9,\nscriptEnded = 10\n\nUsed by addEventCallback / removeEventCallback calls."}, new List {"enum","emu.eventType.reset","Triggered when a soft reset occurs","","",""}, new List {"enum","emu.eventType.nmi","Triggered when an nmi occurs","","",""}, new List {"enum","emu.eventType.irq","Triggered when an irq occurs","","",""}, @@ -514,6 +514,8 @@ namespace Mesen.GUI.Debugger new List {"enum","emu.eventType.stateLoaded","Triggered when a user manually loads a savestate","","",""}, new List {"enum","emu.eventType.stateSaved","Triggered when a user manually saves a savestate","","",""}, new List {"enum","emu.eventType.inputPolled","Triggered when the emulation core polls the state of the input devices for the next frame","","",""}, + new List {"enum","emu.eventType.spriteZeroHit","Triggered when the PPU sets the sprite zero hit flag","","",""}, + new List {"enum","emu.eventType.scriptEnded","Triggered when the current Lua script ends (script window closed, execution stopped, etc.)","","",""}, new List {"enum","emu.executeCountType","emu.executeCountType.[value]","","","Values:\ncpuCycles = 0,\nppuCycles = 1,\ncpuInstructions = 2\n\nUsed by execute calls." }, new List {"enum","emu.executeCountType.cpuCycles","Count the number of CPU cycles","","",""}, new List {"enum","emu.executeCountType.ppuCycles","Count the number of PPU cycles","","",""},