Lua - implement event.onexit() - fires when the calling script stops execution, supports multiple callbacks per script

This commit is contained in:
adelikat 2014-05-26 03:08:16 +00:00
parent 299c27ae6e
commit 0782c9820d
3 changed files with 29 additions and 0 deletions

View File

@ -21,6 +21,15 @@ namespace BizHawk.Client.Common
#region Events Library Helpers #region Events Library Helpers
public void CallExitEvent(Lua thread)
{
var exitCallbacks = _luaFunctions.Where(x => x.Lua == thread && x.Event == "OnExit");
foreach (var exitCallback in exitCallbacks)
{
exitCallback.Call();
}
}
public LuaFunctionList RegisteredFunctions { get { return _luaFunctions; } } public LuaFunctionList RegisteredFunctions { get { return _luaFunctions; } }
public void CallSaveStateEvent(string name) public void CallSaveStateEvent(string name)
@ -204,6 +213,17 @@ namespace BizHawk.Client.Common
return nlf.Guid.ToString(); return nlf.Guid.ToString();
} }
[LuaMethodAttributes(
"onexit",
"Fires after the calling script has stopped"
)]
public string OnExit(LuaFunction luaf, string name = null)
{
var nlf = new NamedLuaFunction(luaf, "OnExit", LogOutputCallback, CurrentThread, name);
_luaFunctions.Add(nlf);
return nlf.Guid.ToString();
}
[LuaMethodAttributes( [LuaMethodAttributes(
"unregisterbyid", "unregisterbyid",
"Removes the registered function that matches the guid. If a function is found and remove the function will return true. If unable to find a match, the function will return false." "Removes the registered function that matches the guid. If a function is found and remove the function will return true. If unable to find a match, the function will return false."

View File

@ -111,6 +111,11 @@ namespace BizHawk.Client.EmuHawk
_eventLibrary.CallFrameAfterEvent(); _eventLibrary.CallFrameAfterEvent();
} }
public void CallExitEvent(Lua thread)
{
_eventLibrary.CallExitEvent(thread);
}
public void Close() public void Close()
{ {
_lua = new Lua(); _lua = new Lua();

View File

@ -389,6 +389,7 @@ namespace BizHawk.Client.EmuHawk
var result = LuaImp.ResumeScript(lf.Thread); var result = LuaImp.ResumeScript(lf.Thread);
if (result.Terminated) if (result.Terminated)
{ {
LuaImp.CallExitEvent(lf.Thread);
lf.Stop(); lf.Stop();
} }
@ -717,6 +718,8 @@ namespace BizHawk.Client.EmuHawk
} }
else if (!item.Enabled && item.Thread != null) else if (!item.Enabled && item.Thread != null)
{ {
LuaImp.CallExitEvent(item.Thread);
var items = SelectedItems.ToList(); var items = SelectedItems.ToList();
foreach (var sitem in items) foreach (var sitem in items)
{ {
@ -730,6 +733,7 @@ namespace BizHawk.Client.EmuHawk
UpdateRegisteredFunctionsDialog(); UpdateRegisteredFunctionsDialog();
} }
LuaImp.CallExitEvent(item.Thread);
item.Stop(); item.Stop();
} }
} }