2013-10-28 19:13:01 +00:00
using System ;
using System.Linq ;
2013-12-30 01:58:44 +00:00
2013-10-28 19:13:01 +00:00
using LuaInterface ;
2013-11-01 14:51:51 +00:00
namespace BizHawk.Client.Common
2013-10-28 19:13:01 +00:00
{
2013-10-31 16:58:56 +00:00
public class EventLuaLibrary : LuaLibraryBase
2013-10-28 19:13:01 +00:00
{
2014-02-03 20:48:01 +00:00
private readonly LuaFunctionList _luaFunctions = new LuaFunctionList ( ) ;
2013-11-01 14:51:51 +00:00
public EventLuaLibrary ( Action < string > logOutputCallback )
{
LogOutputCallback = logOutputCallback ;
}
2013-10-31 16:58:56 +00:00
public override string Name { get { return "event" ; } }
2013-12-30 01:58:44 +00:00
public Action < string > LogOutputCallback { get ; set ; }
public Lua CurrentThread { get ; set ; }
2013-11-01 14:51:51 +00:00
2013-10-28 19:13:01 +00:00
#region Events Library Helpers
2013-11-10 19:19:58 +00:00
public LuaFunctionList RegisteredFunctions { get { return _luaFunctions ; } }
2013-10-28 19:13:01 +00:00
2013-10-31 16:58:56 +00:00
public void CallSaveStateEvent ( string name )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var lfs = _luaFunctions . Where ( x = > x . Event = = "OnSavestateSave" ) . ToList ( ) ;
2013-10-28 19:13:01 +00:00
if ( lfs . Any ( ) )
{
try
{
2013-11-26 01:21:24 +00:00
foreach ( var lf in lfs )
2013-10-28 19:13:01 +00:00
{
lf . Call ( name ) ;
}
}
catch ( SystemException e )
{
2013-11-01 14:51:51 +00:00
LogOutputCallback (
"error running function attached by lua function event.onsavestate" +
"\nError message: " +
e . Message ) ;
2013-10-28 19:13:01 +00:00
}
}
}
2013-10-31 16:58:56 +00:00
public void CallLoadStateEvent ( string name )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var lfs = _luaFunctions . Where ( x = > x . Event = = "OnSavestateLoad" ) . ToList ( ) ;
2013-10-28 19:13:01 +00:00
if ( lfs . Any ( ) )
{
try
{
2013-11-26 01:21:24 +00:00
foreach ( var lf in lfs )
2013-10-28 19:13:01 +00:00
{
lf . Call ( name ) ;
}
}
catch ( SystemException e )
{
2013-11-01 14:51:51 +00:00
LogOutputCallback (
"error running function attached by lua function event.onloadstate" +
"\nError message: " +
e . Message ) ;
2013-10-28 19:13:01 +00:00
}
}
}
2013-10-31 16:58:56 +00:00
public void CallFrameBeforeEvent ( )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var lfs = _luaFunctions . Where ( x = > x . Event = = "OnFrameStart" ) . ToList ( ) ;
2013-10-28 19:13:01 +00:00
if ( lfs . Any ( ) )
{
try
{
2013-11-26 01:21:24 +00:00
foreach ( var lf in lfs )
2013-10-28 19:13:01 +00:00
{
lf . Call ( ) ;
}
}
catch ( SystemException e )
{
2013-11-01 14:51:51 +00:00
LogOutputCallback (
"error running function attached by lua function event.onframestart" +
"\nError message: " +
e . Message ) ;
2013-10-28 19:13:01 +00:00
}
}
}
2013-10-31 16:58:56 +00:00
public void CallFrameAfterEvent ( )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var lfs = _luaFunctions . Where ( x = > x . Event = = "OnFrameEnd" ) . ToList ( ) ;
2013-10-28 19:13:01 +00:00
if ( lfs . Any ( ) )
{
try
{
2013-11-26 01:21:24 +00:00
foreach ( var lf in lfs )
2013-10-28 19:13:01 +00:00
{
lf . Call ( ) ;
}
}
catch ( SystemException e )
{
2013-11-01 14:51:51 +00:00
LogOutputCallback (
"error running function attached by lua function event.onframeend" +
"\nError message: " +
e . Message ) ;
2013-10-28 19:13:01 +00:00
}
}
}
#endregion
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"onframeend" ,
2014-01-26 18:36:27 +00:00
"Calls the given lua function at the end of each frame, after all emulation and drawing has completed. Note: this is the default behavior of lua scripts"
2014-01-25 21:37:25 +00:00
) ]
public string OnFrameEnd ( LuaFunction luaf , string name = null )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var nlf = new NamedLuaFunction ( luaf , "OnFrameEnd" , LogOutputCallback , CurrentThread , name ) ;
2013-11-10 19:19:58 +00:00
_luaFunctions . Add ( nlf ) ;
2013-11-26 01:21:24 +00:00
return nlf . Guid . ToString ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"onframestart" ,
2014-01-26 18:36:27 +00:00
"Calls the given lua function at the beginning of each frame before any emulation and drawing occurs"
2014-01-25 21:37:25 +00:00
) ]
public string OnFrameStart ( LuaFunction luaf , string name = null )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var nlf = new NamedLuaFunction ( luaf , "OnFrameStart" , LogOutputCallback , CurrentThread , name ) ;
2013-11-10 19:19:58 +00:00
_luaFunctions . Add ( nlf ) ;
2013-11-26 01:21:24 +00:00
return nlf . Guid . ToString ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"oninputpoll" ,
2014-01-26 18:36:27 +00:00
"Calls the given lua function after each time the emulator core polls for input"
2014-01-25 21:37:25 +00:00
) ]
public void OnInputPoll ( LuaFunction luaf , string name = null )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var nlf = new NamedLuaFunction ( luaf , "OnInputPoll" , LogOutputCallback , CurrentThread , name ) ;
2013-11-10 19:19:58 +00:00
_luaFunctions . Add ( nlf ) ;
Global . Emulator . CoreComm . InputCallback . Add ( nlf . Callback ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"onloadstate" ,
2014-01-26 18:36:27 +00:00
"Fires after a state is loaded. Receives a lua function name, and registers it to the event immediately following a successful savestate event"
2014-01-25 21:37:25 +00:00
) ]
public string OnLoadState ( LuaFunction luaf , string name = null )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var nlf = new NamedLuaFunction ( luaf , "OnSavestateLoad" , LogOutputCallback , CurrentThread , name ) ;
2013-11-10 19:19:58 +00:00
_luaFunctions . Add ( nlf ) ;
2013-11-26 01:21:24 +00:00
return nlf . Guid . ToString ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"onmemoryexecute" ,
2014-01-26 18:36:27 +00:00
"Fires after the given address is executed by the core"
2014-01-25 21:37:25 +00:00
) ]
2014-01-27 01:15:56 +00:00
public string OnMemoryExecute ( LuaFunction luaf , uint address , string name = null )
2013-11-12 01:51:07 +00:00
{
2013-11-26 01:21:24 +00:00
var nlf = new NamedLuaFunction ( luaf , "OnMemoryExecute" , LogOutputCallback , CurrentThread , name ) ;
2013-11-12 01:51:07 +00:00
_luaFunctions . Add ( nlf ) ;
2014-01-27 01:15:56 +00:00
Global . CoreComm . MemoryCallbackSystem . AddExecute ( nlf . Callback , address ) ;
2013-11-26 01:21:24 +00:00
return nlf . Guid . ToString ( ) ;
2013-11-12 01:51:07 +00:00
}
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"onmemoryread" ,
2014-01-26 18:36:27 +00:00
"Fires after the given address is read by the core. If no address is given, it will attach to every memory read"
2014-01-25 21:37:25 +00:00
) ]
2014-01-27 01:15:56 +00:00
public string OnMemoryRead ( LuaFunction luaf , uint? address = null , string name = null )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var nlf = new NamedLuaFunction ( luaf , "OnMemoryRead" , LogOutputCallback , CurrentThread , name ) ;
2013-11-10 21:20:55 +00:00
_luaFunctions . Add ( nlf ) ;
2014-01-27 01:15:56 +00:00
Global . CoreComm . MemoryCallbackSystem . AddRead ( nlf . Callback , address ) ;
2013-11-26 01:21:24 +00:00
return nlf . Guid . ToString ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"onmemorywrite" ,
2014-01-26 18:36:27 +00:00
"Fires after the given address is written by the core. If no address is given, it will attach to every memory write"
2014-01-25 21:37:25 +00:00
) ]
2014-01-27 01:15:56 +00:00
public string OnMemoryWrite ( LuaFunction luaf , uint? address = null , string name = null )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var nlf = new NamedLuaFunction ( luaf , "OnMemoryWrite" , LogOutputCallback , CurrentThread , name ) ;
2013-11-10 21:20:55 +00:00
_luaFunctions . Add ( nlf ) ;
2014-01-27 01:15:56 +00:00
Global . CoreComm . MemoryCallbackSystem . AddWrite ( nlf . Callback , address ) ;
2013-11-26 01:21:24 +00:00
return nlf . Guid . ToString ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"onsavestate" ,
2014-01-26 18:36:27 +00:00
"Fires after a state is saved"
2014-01-25 21:37:25 +00:00
) ]
public string OnSaveState ( LuaFunction luaf , string name = null )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
var nlf = new NamedLuaFunction ( luaf , "OnSavestateSave" , LogOutputCallback , CurrentThread , name ) ;
2013-11-10 19:19:58 +00:00
_luaFunctions . Add ( nlf ) ;
2013-11-26 01:21:24 +00:00
return nlf . Guid . ToString ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"unregisterbyid" ,
2014-01-26 18:36:27 +00:00
"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."
2014-01-25 21:37:25 +00:00
) ]
public bool UnregisterById ( object guid )
2013-10-28 19:13:01 +00:00
{
2013-11-26 01:21:24 +00:00
foreach ( var nlf in _luaFunctions . Where ( nlf = > nlf . Guid . ToString ( ) = = guid . ToString ( ) ) )
2013-10-28 19:13:01 +00:00
{
2013-12-19 01:02:50 +00:00
_luaFunctions . Remove ( nlf ) ;
2013-11-26 01:21:24 +00:00
return true ;
2013-10-28 19:13:01 +00:00
}
return false ;
}
2014-01-25 21:37:25 +00:00
[ LuaMethodAttributes (
"unregisterbyname" ,
2014-01-26 18:36:27 +00:00
"Removes the first registered function that matches Name. If a function is found and remove the function will return true. If unable to find a match, the function will return false."
2014-01-25 21:37:25 +00:00
) ]
2014-01-26 18:36:27 +00:00
public bool UnregisterByName ( string name )
2013-10-28 19:13:01 +00:00
{
2014-01-26 18:36:27 +00:00
foreach ( var nlf in _luaFunctions . Where ( nlf = > nlf . Name = = name ) )
2013-10-28 19:13:01 +00:00
{
2013-12-19 01:02:50 +00:00
_luaFunctions . Remove ( nlf ) ;
2013-11-26 01:21:24 +00:00
return true ;
2013-10-28 19:13:01 +00:00
}
return false ;
}
}
}