2014-05-21 00:17:35 +00:00
using System ;
using System.Linq ;
using LuaInterface ;
2014-04-22 01:38:19 +00:00
2013-11-14 13:15:41 +00:00
using BizHawk.Emulation.Cores.Nintendo.NES ;
2014-04-22 01:38:19 +00:00
using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES ;
2013-10-28 19:13:01 +00:00
2013-11-02 01:56:00 +00:00
namespace BizHawk.Client.Common
2013-10-28 19:13:01 +00:00
{
2014-02-03 20:48:01 +00:00
public class NesLuaLibrary : LuaLibraryBase
2013-10-28 19:13:01 +00:00
{
2014-01-26 03:26:52 +00:00
// TODO:
// perhaps with the new core config system, one could
// automatically bring out all of the settings to a lua table, with names. that
// would be completely arbitrary and would remove the whole requirement for this mess
2014-05-21 00:17:35 +00:00
public NesLuaLibrary ( Lua lua )
: base ( lua ) { }
public NesLuaLibrary ( Lua lua , Action < string > logOutputCallback )
: base ( lua , logOutputCallback ) { }
2013-10-31 00:31:25 +00:00
public override string Name { get { return "nes" ; } }
2013-10-29 16:03:06 +00:00
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"addgamegenie" ,
2014-01-26 18:36:27 +00:00
"Adds the specified game genie code. If an NES game is not currently loaded or the code is not a valid game genie code, this will have no effect"
2014-01-26 03:26:52 +00:00
) ]
public void AddGameGenie ( string code )
2013-10-28 19:13:01 +00:00
{
2014-01-07 01:43:31 +00:00
if ( Global . Emulator . SystemId = = "NES" )
2013-10-28 19:13:01 +00:00
{
2013-11-02 01:48:35 +00:00
var decoder = new NESGameGenieDecoder ( code ) ;
2013-12-30 01:58:44 +00:00
var watch = Watch . GenerateWatch (
2014-01-07 01:43:31 +00:00
Global . Emulator . MemoryDomains [ "System Bus" ] ,
2013-11-02 01:48:35 +00:00
decoder . Address ,
Watch . WatchSize . Byte ,
Watch . DisplayType . Hex ,
code ,
2014-02-03 20:48:01 +00:00
false ) ;
2013-11-02 01:48:35 +00:00
Global . CheatList . Add ( new Cheat (
watch ,
decoder . Value ,
2014-02-03 20:48:01 +00:00
decoder . Compare ) ) ;
2013-10-28 19:13:01 +00:00
}
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"getallowmorethaneightsprites" ,
2014-01-26 18:36:27 +00:00
"Gets the NES setting 'Allow more than 8 sprites per scanline' value"
2014-01-26 03:26:52 +00:00
) ]
public static bool GetAllowMoreThanEightSprites ( )
2013-10-28 19:13:01 +00:00
{
2014-04-22 01:38:19 +00:00
if ( Global . Config . NES_InQuickNES )
{
return ( ( QuickNES . QuickNESSettings ) Global . Emulator . GetSettings ( ) ) . NumSprites ! = 8 ;
}
2013-12-22 00:44:39 +00:00
return ( ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ) . AllowMoreThanEightSprites ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"getbottomscanline" ,
2014-01-26 18:36:27 +00:00
"Gets the current value for the bottom scanline value"
2014-01-26 03:26:52 +00:00
) ]
public static int GetBottomScanline ( bool pal = false )
2013-10-28 19:13:01 +00:00
{
2014-04-22 01:38:19 +00:00
if ( Global . Config . NES_InQuickNES )
{
return ( ( QuickNES . QuickNESSettings ) Global . Emulator . GetSettings ( ) ) . ClipTopAndBottom ? 231 : 239 ;
}
2013-10-28 19:13:01 +00:00
if ( pal )
{
2013-12-22 00:44:39 +00:00
return ( ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ) . PAL_BottomLine ;
2013-10-28 19:13:01 +00:00
}
2014-02-03 20:48:01 +00:00
return ( ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ) . NTSC_BottomLine ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"getclipleftandright" ,
2014-01-26 18:36:27 +00:00
"Gets the current value for the Clip Left and Right sides option"
2014-01-26 03:26:52 +00:00
) ]
public static bool GetClipLeftAndRight ( )
2013-10-28 19:13:01 +00:00
{
2014-04-22 01:38:19 +00:00
if ( Global . Config . NES_InQuickNES )
{
return ( ( QuickNES . QuickNESSettings ) Global . Emulator . GetSettings ( ) ) . ClipLeftAndRight ;
}
2013-12-22 00:44:39 +00:00
return ( ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ) . ClipLeftAndRight ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"getdispbackground" ,
2014-01-26 18:36:27 +00:00
"Indicates whether or not the bg layer is being displayed"
2014-01-26 03:26:52 +00:00
) ]
public static bool GetDisplayBackground ( )
2013-10-28 19:13:01 +00:00
{
2014-04-22 01:38:19 +00:00
if ( Global . Config . NES_InQuickNES )
{
return true ;
}
2013-12-22 00:44:39 +00:00
return ( ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ) . DispBackground ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"getdispsprites" ,
2014-01-26 18:36:27 +00:00
"Indicates whether or not sprites are being displayed"
2014-01-26 03:26:52 +00:00
) ]
public static bool GetDisplaySprites ( )
2013-10-28 19:13:01 +00:00
{
2014-04-22 01:38:19 +00:00
if ( Global . Config . NES_InQuickNES )
{
return true ;
}
2013-12-22 00:44:39 +00:00
return ( ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ) . DispSprites ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"gettopscanline" ,
2014-01-26 18:36:27 +00:00
"Gets the current value for the top scanline value"
2014-01-26 03:26:52 +00:00
) ]
public static int GetTopScanline ( bool pal = false )
2013-10-28 19:13:01 +00:00
{
2014-04-22 01:38:19 +00:00
if ( Global . Config . NES_InQuickNES )
{
return ( ( QuickNES . QuickNESSettings ) Global . Emulator . GetSettings ( ) ) . ClipTopAndBottom ? 8 : 0 ;
}
2013-10-28 19:13:01 +00:00
if ( pal )
{
2013-12-22 00:44:39 +00:00
return ( ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ) . PAL_TopLine ;
2013-10-28 19:13:01 +00:00
}
2014-02-03 20:48:01 +00:00
return ( ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ) . NTSC_TopLine ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"removegamegenie" ,
2014-01-26 18:36:27 +00:00
"Removes the specified game genie code. If an NES game is not currently loaded or the code is not a valid game genie code, this will have no effect"
2014-01-26 03:26:52 +00:00
) ]
public void RemoveGameGenie ( string code )
2013-10-28 19:13:01 +00:00
{
2014-01-07 01:43:31 +00:00
if ( Global . Emulator . SystemId = = "NES" )
2013-10-28 19:13:01 +00:00
{
2013-11-02 01:48:35 +00:00
var decoder = new NESGameGenieDecoder ( code ) ;
Global . CheatList . RemoveRange (
2014-02-03 20:48:01 +00:00
Global . CheatList . Where ( x = > x . Address = = decoder . Address ) ) ;
2013-10-28 19:13:01 +00:00
}
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"setallowmorethaneightsprites" ,
2014-01-26 18:36:27 +00:00
"Sets the NES setting 'Allow more than 8 sprites per scanline'"
2014-01-26 03:26:52 +00:00
) ]
public static void SetAllowMoreThanEightSprites ( bool allow )
2013-10-28 19:13:01 +00:00
{
if ( Global . Emulator is NES )
{
2013-12-22 00:44:39 +00:00
var s = ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ;
s . AllowMoreThanEightSprites = allow ;
Global . Emulator . PutSettings ( s ) ;
2013-10-28 19:13:01 +00:00
}
2014-04-22 01:38:19 +00:00
else if ( Global . Emulator is QuickNES )
{
var s = ( QuickNES . QuickNESSettings ) Global . Emulator . GetSettings ( ) ;
s . NumSprites = allow ? 64 : 8 ;
Global . Emulator . PutSettings ( s ) ;
}
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"setclipleftandright" ,
2014-01-26 18:36:27 +00:00
"Sets the Clip Left and Right sides option"
2014-01-26 03:26:52 +00:00
) ]
public static void SetClipLeftAndRight ( bool leftandright )
2013-10-28 19:13:01 +00:00
{
if ( Global . Emulator is NES )
{
2013-12-22 00:44:39 +00:00
var s = ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ;
s . ClipLeftAndRight = leftandright ;
Global . Emulator . PutSettings ( s ) ;
2013-10-28 19:13:01 +00:00
}
2014-04-22 01:38:19 +00:00
else if ( Global . Emulator is QuickNES )
{
var s = ( QuickNES . QuickNESSettings ) Global . Emulator . GetSettings ( ) ;
s . ClipLeftAndRight = leftandright ;
Global . Emulator . PutSettings ( s ) ;
}
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"setdispbackground" ,
2014-01-26 18:36:27 +00:00
"Sets whether or not the background layer will be displayed"
2014-01-26 03:26:52 +00:00
) ]
public static void SetDisplayBackground ( bool show )
2013-10-28 19:13:01 +00:00
{
2013-12-22 00:44:39 +00:00
if ( Global . Emulator is NES )
{
var s = ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ;
s . DispBackground = show ;
Global . Emulator . PutSettings ( s ) ;
}
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"setdispsprites" ,
2014-01-26 18:36:27 +00:00
"Sets whether or not sprites will be displayed"
2014-01-26 03:26:52 +00:00
) ]
public static void SetDisplaySprites ( bool show )
2013-10-28 19:13:01 +00:00
{
2013-12-22 00:44:39 +00:00
if ( Global . Emulator is NES )
{
var s = ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ;
s . DispSprites = show ;
Global . Emulator . PutSettings ( s ) ;
}
2013-10-28 19:13:01 +00:00
}
2014-01-26 03:26:52 +00:00
[ LuaMethodAttributes (
"setscanlines" ,
2014-04-22 01:38:19 +00:00
"sets the top and bottom scanlines to be drawn (same values as in the graphics options dialog). Top must be in the range of 0 to 127, bottom must be between 128 and 239. Not supported in the Quick Nes core"
2014-01-26 03:26:52 +00:00
) ]
2014-01-27 01:15:56 +00:00
public static void SetScanlines ( int top , int bottom , bool pal = false )
2013-10-28 19:13:01 +00:00
{
2013-12-22 00:44:39 +00:00
if ( Global . Emulator is NES )
2013-10-28 19:13:01 +00:00
{
2014-01-27 01:15:56 +00:00
if ( top > 127 )
2013-12-22 00:44:39 +00:00
{
2014-01-27 01:15:56 +00:00
top = 127 ;
2013-12-22 00:44:39 +00:00
}
2014-01-27 01:15:56 +00:00
else if ( top < 0 )
2013-12-22 00:44:39 +00:00
{
2014-01-27 01:15:56 +00:00
top = 0 ;
2013-12-22 00:44:39 +00:00
}
2013-10-28 19:13:01 +00:00
2014-01-27 01:15:56 +00:00
if ( bottom > 239 )
2013-12-22 00:44:39 +00:00
{
2014-01-27 01:15:56 +00:00
bottom = 239 ;
2013-12-22 00:44:39 +00:00
}
2014-01-27 01:15:56 +00:00
else if ( bottom < 128 )
2013-12-22 00:44:39 +00:00
{
2014-01-27 01:15:56 +00:00
bottom = 128 ;
2013-12-22 00:44:39 +00:00
}
2013-10-28 19:13:01 +00:00
2013-12-22 00:44:39 +00:00
var s = ( NES . NESSettings ) Global . Emulator . GetSettings ( ) ;
2013-10-28 19:13:01 +00:00
if ( pal )
{
2014-01-27 01:15:56 +00:00
s . PAL_TopLine = top ;
s . PAL_BottomLine = bottom ;
2013-10-28 19:13:01 +00:00
}
else
{
2014-01-27 01:15:56 +00:00
s . NTSC_TopLine = top ;
s . NTSC_BottomLine = bottom ;
2013-10-28 19:13:01 +00:00
}
2013-12-22 00:44:39 +00:00
Global . Emulator . PutSettings ( s ) ;
2013-10-28 19:13:01 +00:00
}
}
}
}