2013-10-28 19:13:01 +00:00
using System ;
2014-01-02 01:11:00 +00:00
using System.Collections.Generic ;
2014-06-03 02:19:13 +00:00
using System.ComponentModel ;
2015-04-10 21:56:03 +00:00
using System.Linq ;
2014-05-21 00:17:35 +00:00
using LuaInterface ;
2015-04-12 17:37:06 +00:00
using BizHawk.Common ;
2015-01-01 20:58:57 +00:00
using BizHawk.Emulation.Common ;
2013-10-28 19:13:01 +00:00
using BizHawk.Client.Common ;
2015-04-12 17:37:06 +00:00
2013-11-03 03:54:37 +00:00
namespace BizHawk.Client.EmuHawk
2013-10-28 19:13:01 +00:00
{
2014-06-03 02:19:13 +00:00
[Description("A library for manipulating the EmuHawk client UI")]
2014-06-01 22:02:59 +00:00
public sealed class EmuHawkLuaLibrary : LuaLibraryBase
2013-10-28 19:13:01 +00:00
{
2015-01-01 20:58:57 +00:00
[RequiredService]
public IEmulator Emulator { get ; set ; }
2015-01-14 22:37:37 +00:00
[RequiredService]
public IVideoProvider VideoProvider { get ; set ; }
2015-01-01 20:58:57 +00:00
2014-01-26 16:15:45 +00:00
private readonly Dictionary < int , string > _filterMappings = new Dictionary < int , string >
2014-01-02 01:11:00 +00:00
{
{ 0 , "None" } ,
{ 1 , "x2SAI" } ,
{ 2 , "SuperX2SAI" } ,
{ 3 , "SuperEagle" } ,
{ 4 , "Scanlines" } ,
} ;
2014-05-21 00:17:35 +00:00
public EmuHawkLuaLibrary ( Lua lua )
: base ( lua ) { }
2013-10-31 00:31:25 +00:00
2014-05-21 00:17:35 +00:00
public EmuHawkLuaLibrary ( Lua lua , Action < string > logOutputCallback )
: base ( lua , logOutputCallback ) { }
2013-10-31 00:31:25 +00:00
public override string Name { get { return "client" ; } }
2014-01-26 16:15:45 +00:00
2015-07-31 22:44:53 +00:00
[ LuaMethodAttributes (
"exit" ,
"Closes the emulator"
) ]
public void CloseEmulator ( )
{
GlobalWin . MainForm . CloseEmulator ( ) ;
}
2014-10-10 11:02:18 +00:00
[ LuaMethodAttributes (
"borderheight" ,
"Gets the current height in pixels of the border around the emulator's drawing area"
) ]
public static int BorderHeight ( )
{
var point = new System . Drawing . Point ( 0 , 0 ) ;
return GlobalWin . DisplayManager . TransformPoint ( point ) . Y ;
}
[ LuaMethodAttributes (
"borderwidth" ,
"Gets the current width in pixels of the border around the emulator's drawing area"
) ]
public static int BorderWidth ( )
{
var point = new System . Drawing . Point ( 0 , 0 ) ;
return GlobalWin . DisplayManager . TransformPoint ( point ) . X ;
}
[ LuaMethodAttributes (
"bufferheight" ,
"Gets the current height in pixels of the emulator's drawing area"
) ]
2015-01-01 21:44:49 +00:00
public int BufferHeight ( )
2014-10-10 11:02:18 +00:00
{
2015-01-14 22:37:37 +00:00
var height = VideoProvider . BufferHeight ;
2014-10-10 11:02:18 +00:00
var point = new System . Drawing . Point ( 0 , height ) ;
return GlobalWin . DisplayManager . TransformPoint ( point ) . Y - BorderHeight ( ) ;
}
[ LuaMethodAttributes (
"bufferwidth" ,
"Gets the current width in pixels of the emulator's drawing area"
) ]
2015-01-01 21:44:49 +00:00
public int BufferWidth ( )
2014-10-10 11:02:18 +00:00
{
2015-01-14 22:37:37 +00:00
var width = VideoProvider . BufferWidth ;
2014-10-10 11:02:18 +00:00
var point = new System . Drawing . Point ( width , 0 ) ;
return GlobalWin . DisplayManager . TransformPoint ( point ) . X - BorderWidth ( ) ;
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"clearautohold" ,
2014-01-26 18:36:27 +00:00
"Clears all autohold keys"
2014-01-26 16:15:45 +00:00
) ]
public void ClearAutohold ( )
2014-01-21 00:36:22 +00:00
{
GlobalWin . MainForm . ClearHolds ( ) ;
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"closerom" ,
2014-01-26 18:36:27 +00:00
"Closes the loaded Rom"
2014-01-26 16:15:45 +00:00
) ]
public static void CloseRom ( )
2013-10-28 19:13:01 +00:00
{
2013-11-27 23:35:32 +00:00
GlobalWin . MainForm . CloseRom ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"enablerewind" ,
2014-01-26 18:36:27 +00:00
"Sets whether or not the rewind feature is enabled"
2014-01-26 16:15:45 +00:00
) ]
2014-07-11 17:14:45 +00:00
public void EnableRewind ( bool enabled )
2013-10-31 18:09:40 +00:00
{
2014-07-11 17:14:45 +00:00
GlobalWin . MainForm . EnableRewind ( enabled ) ;
2013-10-31 18:09:40 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"frameskip" ,
2014-01-26 18:36:27 +00:00
"Sets the frame skip value of the client UI"
2014-01-26 16:15:45 +00:00
) ]
2014-01-26 20:36:00 +00:00
public void FrameSkip ( int numFrames )
2013-10-31 18:09:40 +00:00
{
2014-04-13 18:39:46 +00:00
if ( numFrames > 0 )
2013-10-31 18:09:40 +00:00
{
2014-04-13 18:39:46 +00:00
Global . Config . FrameSkip = numFrames ;
2014-01-26 16:15:45 +00:00
GlobalWin . MainForm . FrameSkipMessage ( ) ;
2013-10-31 18:09:40 +00:00
}
2014-01-26 16:15:45 +00:00
else
2013-10-31 18:09:40 +00:00
{
2014-01-26 16:15:45 +00:00
ConsoleLuaLibrary . Log ( "Invalid frame skip value" ) ;
2013-10-31 18:09:40 +00:00
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"gettargetscanlineintensity" ,
2014-01-27 20:19:08 +00:00
"Gets the current scanline intensity setting, used for the scanline display filter"
2014-01-26 16:15:45 +00:00
) ]
public static int GetTargetScanlineIntensity ( )
2014-01-02 01:11:00 +00:00
{
return Global . Config . TargetScanlineFilterIntensity ;
2013-10-31 18:09:40 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"getwindowsize" ,
2014-01-27 20:19:08 +00:00
"Gets the main window's size Possible values are 1, 2, 3, 4, 5, and 10"
2014-01-26 16:15:45 +00:00
) ]
public static int GetWindowSize ( )
2013-10-28 19:13:01 +00:00
{
return Global . Config . TargetZoomFactor ;
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"ispaused" ,
2014-01-27 20:19:08 +00:00
"Returns true if emulator is paused, otherwise, false"
2014-01-26 16:15:45 +00:00
) ]
public static bool IsPaused ( )
2014-01-02 01:11:00 +00:00
{
return GlobalWin . MainForm . EmulatorPaused ;
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"opencheats" ,
2014-01-27 20:19:08 +00:00
"opens the Cheats dialog"
2014-01-26 16:15:45 +00:00
) ]
public static void OpenCheats ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . Tools . Load < Cheats > ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"openhexeditor" ,
2014-01-27 20:19:08 +00:00
"opens the Hex Editor dialog"
2014-01-26 16:15:45 +00:00
) ]
public static void OpenHexEditor ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . Tools . Load < HexEditor > ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"openramwatch" ,
2014-01-27 20:19:08 +00:00
"opens the Ram Watch dialog"
2014-01-26 16:15:45 +00:00
) ]
public static void OpenRamWatch ( )
2013-10-28 19:13:01 +00:00
{
2014-01-26 16:15:45 +00:00
GlobalWin . Tools . LoadRamWatch ( loadDialog : true ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"openramsearch" ,
2014-01-27 20:19:08 +00:00
"opens the Ram Search dialog"
2014-01-26 16:15:45 +00:00
) ]
public static void OpenRamSearch ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . Tools . Load < RamSearch > ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"openrom" ,
2014-01-27 20:19:08 +00:00
"opens the Open ROM dialog"
2014-01-26 16:15:45 +00:00
) ]
public static void OpenRom ( string path )
2013-10-28 19:13:01 +00:00
{
2014-08-02 15:32:48 +00:00
GlobalWin . MainForm . LoadRom ( path ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"opentasstudio" ,
2014-01-27 20:19:08 +00:00
"opens the TAStudio dialog"
2014-01-26 16:15:45 +00:00
) ]
public static void OpenTasStudio ( )
2013-10-28 19:13:01 +00:00
{
2013-12-29 23:35:42 +00:00
GlobalWin . Tools . Load < TAStudio > ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"opentoolbox" ,
2014-01-27 20:19:08 +00:00
"opens the Toolbox Dialog"
2014-01-26 16:15:45 +00:00
) ]
public static void OpenToolBox ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . Tools . Load < ToolBox > ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"opentracelogger" ,
2014-01-27 20:19:08 +00:00
"opens the tracelogger if it is available for the given core"
2014-01-26 16:15:45 +00:00
) ]
public static void OpenTraceLogger ( )
2013-10-28 19:13:01 +00:00
{
2014-12-14 01:39:15 +00:00
GlobalWin . Tools . Load < TraceLogger > ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"paint" ,
2014-01-27 20:19:08 +00:00
"Causes the client UI to repaint the screen"
2014-01-26 16:15:45 +00:00
) ]
public static void Paint ( )
2013-11-01 15:47:37 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2013-11-01 15:47:37 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"pause" ,
2014-01-27 20:19:08 +00:00
"Pauses the emulator"
2014-01-26 16:15:45 +00:00
) ]
public static void Pause ( )
2013-10-31 18:09:40 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . MainForm . PauseEmulator ( ) ;
2013-10-31 18:09:40 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"pause_av" ,
2014-01-27 20:19:08 +00:00
"If currently capturing Audio/Video, this will suspend the record. Frames will not be captured into the AV until client.unpause_av() is called"
2014-01-26 16:15:45 +00:00
) ]
public static void PauseAv ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . MainForm . PauseAVI = true ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"reboot_core" ,
2014-01-27 20:19:08 +00:00
"Reboots the currently loaded core"
2014-01-26 16:15:45 +00:00
) ]
public static void RebootCore ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . MainForm . RebootCore ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"screenheight" ,
2014-01-27 20:19:08 +00:00
"Gets the current width in pixels of the emulator's drawing area"
2014-01-26 16:15:45 +00:00
) ]
public static int ScreenHeight ( )
2013-10-28 19:13:01 +00:00
{
2015-02-22 03:53:07 +00:00
return GlobalWin . MainForm . PresentationPanel . NativeSize . Height ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"screenshot" ,
2014-07-27 01:19:33 +00:00
"if a parameter is passed it will function as the Screenshot As menu item of EmuHawk, else it will function as the Screenshot menu item"
2014-01-26 16:15:45 +00:00
) ]
public static void Screenshot ( string path = null )
2013-10-28 19:13:01 +00:00
{
if ( path = = null )
{
2013-11-03 16:07:58 +00:00
GlobalWin . MainForm . TakeScreenshot ( ) ;
2013-10-28 19:13:01 +00:00
}
else
{
2014-01-26 16:15:45 +00:00
GlobalWin . MainForm . TakeScreenshot ( path ) ;
2013-10-28 19:13:01 +00:00
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"screenshottoclipboard" ,
2014-07-27 01:19:33 +00:00
"Performs the same function as EmuHawk's Screenshot To Clipboard menu item"
2014-01-26 16:15:45 +00:00
) ]
public static void ScreenshotToClipboard ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . MainForm . TakeScreenshotToClipboard ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"settargetscanlineintensity" ,
2014-01-27 20:19:08 +00:00
"Sets the current scanline intensity setting, used for the scanline display filter"
2014-01-26 16:15:45 +00:00
) ]
2014-01-26 20:36:00 +00:00
public static void SetTargetScanlineIntensity ( int val )
2014-01-02 01:11:00 +00:00
{
2014-01-26 20:36:00 +00:00
Global . Config . TargetScanlineFilterIntensity = val ;
2014-01-02 01:11:00 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"setscreenshotosd" ,
2014-01-27 20:19:08 +00:00
"Sets the screenshot Capture OSD property of the client"
2014-01-26 16:15:45 +00:00
) ]
public static void SetScreenshotOSD ( bool value )
2013-10-28 19:13:01 +00:00
{
Global . Config . Screenshot_CaptureOSD = value ;
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"screenwidth" ,
2014-01-27 20:19:08 +00:00
"Gets the current height in pixels of the emulator's drawing area"
2014-01-26 16:15:45 +00:00
) ]
public static int ScreenWidth ( )
2013-10-28 19:13:01 +00:00
{
2015-02-22 03:53:07 +00:00
return GlobalWin . MainForm . PresentationPanel . NativeSize . Width ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"setwindowsize" ,
2014-01-27 20:19:08 +00:00
"Sets the main window's size to the give value. Accepted values are 1, 2, 3, 4, 5, and 10"
2014-01-26 16:15:45 +00:00
) ]
2014-01-26 20:36:00 +00:00
public void SetWindowSize ( int size )
2013-10-28 19:13:01 +00:00
{
2014-04-13 18:35:27 +00:00
if ( size = = 1 | | size = = 2 | | size = = 3 | | size = = 4 | | size = = 5 | | size = = 10 )
2013-10-28 19:13:01 +00:00
{
2014-04-13 18:35:27 +00:00
Global . Config . TargetZoomFactor = size ;
2014-01-26 16:15:45 +00:00
GlobalWin . MainForm . FrameBufferResized ( ) ;
2014-04-13 18:35:27 +00:00
GlobalWin . OSD . AddMessage ( "Window size set to " + size + "x" ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
else
2013-10-28 19:13:01 +00:00
{
2014-01-26 16:15:45 +00:00
Log ( "Invalid window size" ) ;
2013-10-28 19:13:01 +00:00
}
2013-10-31 18:09:40 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"speedmode" ,
2014-01-27 20:19:08 +00:00
"Sets the speed of the emulator (in terms of percent)"
2014-01-26 16:15:45 +00:00
) ]
2014-01-26 20:36:00 +00:00
public void SpeedMode ( int percent )
2013-10-31 18:09:40 +00:00
{
2014-04-13 18:35:27 +00:00
if ( percent > 0 & & percent < 6400 )
2013-10-31 18:09:40 +00:00
{
2014-04-13 18:35:27 +00:00
GlobalWin . MainForm . ClickSpeedItem ( percent ) ;
2013-10-31 18:09:40 +00:00
}
2014-01-26 16:15:45 +00:00
else
2013-10-31 18:09:40 +00:00
{
2014-01-26 16:15:45 +00:00
Log ( "Invalid speed value" ) ;
2013-10-31 18:09:40 +00:00
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"togglepause" ,
2014-01-27 20:19:08 +00:00
"Toggles the current pause state"
2014-01-26 16:15:45 +00:00
) ]
public static void TogglePause ( )
2013-10-31 18:09:40 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . MainForm . TogglePause ( ) ;
2013-10-31 18:09:40 +00:00
}
2014-10-10 11:02:18 +00:00
[ LuaMethodAttributes (
"transformPointX" ,
"Transforms an x-coordinate in emulator space to an x-coordinate in client space"
) ]
public static int TransformPointX ( int x )
{
var point = new System . Drawing . Point ( x , 0 ) ;
return GlobalWin . DisplayManager . TransformPoint ( point ) . X ;
}
[ LuaMethodAttributes (
"transformPointY" ,
"Transforms an y-coordinate in emulator space to an y-coordinate in client space"
) ]
public static int TransformPointY ( int y )
{
var point = new System . Drawing . Point ( 0 , y ) ;
return GlobalWin . DisplayManager . TransformPoint ( point ) . Y ;
}
2013-10-28 19:13:01 +00:00
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"unpause" ,
2014-01-27 20:19:08 +00:00
"Unpauses the emulator"
2014-01-26 16:15:45 +00:00
) ]
public static void Unpause ( )
2013-10-31 18:09:40 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . MainForm . UnpauseEmulator ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"unpause_av" ,
2014-01-27 20:19:08 +00:00
"If currently capturing Audio/Video this resumes capturing"
2014-01-26 16:15:45 +00:00
) ]
public static void UnpauseAv ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . MainForm . PauseAVI = false ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"xpos" ,
2014-01-27 20:19:08 +00:00
"Returns the x value of the screen position where the client currently sits"
2014-01-26 16:15:45 +00:00
) ]
public static int Xpos ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
return GlobalWin . MainForm . DesktopLocation . X ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"ypos" ,
2014-01-27 20:19:08 +00:00
"Returns the y value of the screen position where the client currently sits"
2014-01-26 16:15:45 +00:00
) ]
public static int Ypos ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
return GlobalWin . MainForm . DesktopLocation . Y ;
2013-10-28 19:13:01 +00:00
}
2015-04-10 21:56:03 +00:00
[ LuaMethodAttributes (
2015-04-12 17:37:06 +00:00
"getavailabletools" ,
2015-04-10 21:56:03 +00:00
"Returns a list of the tools currently open"
) ]
2015-04-12 17:37:06 +00:00
public LuaTable GetAvailableTools ( )
2015-04-10 21:56:03 +00:00
{
var t = Lua . NewTable ( ) ;
var tools = GlobalWin . Tools . AvailableTools . ToList ( ) ;
for ( int i = 0 ; i < tools . Count ; i + + )
{
2015-04-12 17:37:06 +00:00
t [ i ] = tools [ i ] . Name . ToLower ( ) ;
2015-04-10 21:56:03 +00:00
}
return t ;
}
[ LuaMethodAttributes (
"gettool" ,
2015-04-12 17:37:06 +00:00
"Returns an object that represents a tool of the given name (not case sensitive). If the tool is not open, it will be loaded if available. Use gettools to get a list of names"
2015-04-10 21:56:03 +00:00
) ]
public LuaTable GetTool ( string name )
{
2015-04-12 17:37:06 +00:00
var toolType = ReflectionUtil . GetTypeByName ( name )
. FirstOrDefault ( x = > typeof ( IToolForm ) . IsAssignableFrom ( x ) & & ! x . IsInterface ) ;
if ( toolType ! = null )
{
GlobalWin . Tools . Load ( toolType ) ;
}
2015-04-10 21:56:03 +00:00
var selectedTool = GlobalWin . Tools . AvailableTools
. FirstOrDefault ( tool = > tool . GetType ( ) . Name . ToLower ( ) = = name . ToLower ( ) ) ;
if ( selectedTool ! = null )
{
return LuaHelper . ToLuaTable ( Lua , selectedTool ) ;
}
return null ;
}
2015-04-12 17:46:27 +00:00
[ LuaMethodAttributes (
"createinstance" ,
"returns a default instance of the given type of object if it exists (not case sensitive). Note: This will only work on objects which have a parameterless constructor. If no suitable type is found, or the type does not have a parameterless constructor, then nil is returned"
) ]
public LuaTable CreateInstance ( string name )
{
var possibleTypes = ReflectionUtil . GetTypeByName ( name ) ;
if ( possibleTypes . Any ( ) )
{
var instance = Activator . CreateInstance ( possibleTypes . First ( ) ) ;
return LuaHelper . ToLuaTable ( Lua , instance ) ;
}
return null ;
}
2013-10-28 19:13:01 +00:00
}
}