2014-05-21 00:17:35 +00:00
using System ;
2014-06-03 02:19:13 +00:00
using System.ComponentModel ;
2014-05-21 00:17:35 +00:00
using System.Linq ;
using LuaInterface ;
2014-09-01 18:43:41 +00:00
using BizHawk.Emulation.Common ;
2014-12-05 00:32:29 +00:00
using BizHawk.Emulation.Common.IEmulatorExtensions ;
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-06-03 02:19:13 +00:00
[Description("Functions related specifically to Nes Cores")]
2014-06-01 22:02:59 +00:00
public sealed 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 ) { }
2014-12-17 23:03:58 +00:00
[OptionalService]
private NES _neshawk { get ; set ; }
[OptionalService]
private QuickNES _quicknes { get ; set ; }
private bool NESAvailable { get { return _neshawk ! = null | | _quicknes ! = null ; } }
2014-05-21 00:17:35 +00:00
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-12-17 23:03:58 +00:00
if ( NESAvailable )
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-12-05 00:32:29 +00:00
Global . Emulator . AsMemoryDomains ( ) . 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
) ]
2014-12-17 23:03:58 +00:00
public bool GetAllowMoreThanEightSprites ( )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _quicknes ! = null )
2014-04-22 01:38:19 +00:00
{
2014-12-17 23:03:58 +00:00
return _quicknes . GetSettings ( ) . NumSprites ! = 8 ;
}
if ( _neshawk ! = null )
{
return _neshawk . GetSettings ( ) . AllowMoreThanEightSprites ;
2014-04-22 01:38:19 +00:00
}
2014-12-17 23:03:58 +00:00
throw new InvalidOperationException ( ) ;
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
) ]
2014-12-17 23:03:58 +00:00
public int GetBottomScanline ( bool pal = false )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _quicknes ! = null )
{
return _quicknes . GetSettings ( ) . ClipTopAndBottom ? 231 : 239 ;
}
if ( _neshawk ! = null )
2014-04-22 01:38:19 +00:00
{
2014-12-17 23:03:58 +00:00
return pal
? _neshawk . GetSettings ( ) . PAL_BottomLine
: _neshawk . GetSettings ( ) . NTSC_BottomLine ;
2014-04-22 01:38:19 +00:00
}
2014-12-17 23:03:58 +00:00
throw new InvalidOperationException ( ) ;
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
) ]
2014-12-17 23:03:58 +00:00
public bool GetClipLeftAndRight ( )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _quicknes ! = null )
{
return _quicknes . GetSettings ( ) . ClipLeftAndRight ;
}
if ( _neshawk ! = null )
2014-04-22 01:38:19 +00:00
{
2014-12-17 23:03:58 +00:00
return _neshawk . GetSettings ( ) . ClipLeftAndRight ;
2014-04-22 01:38:19 +00:00
}
2014-12-17 23:03:58 +00:00
throw new InvalidOperationException ( ) ;
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
) ]
2014-12-17 23:03:58 +00:00
public bool GetDisplayBackground ( )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _quicknes ! = null )
2014-04-22 01:38:19 +00:00
{
return true ;
}
2014-12-17 23:03:58 +00:00
if ( _neshawk ! = null )
{
return _neshawk . GetSettings ( ) . DispBackground ;
}
throw new InvalidOperationException ( ) ;
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
) ]
2014-12-17 23:03:58 +00:00
public bool GetDisplaySprites ( )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _quicknes ! = null )
2014-04-22 01:38:19 +00:00
{
2014-12-17 23:03:58 +00:00
return _quicknes . GetSettings ( ) . NumSprites > 0 ;
}
if ( _neshawk ! = null )
{
return _neshawk . GetSettings ( ) . DispSprites ;
2014-04-22 01:38:19 +00:00
}
2014-12-17 23:03:58 +00:00
throw new InvalidOperationException ( ) ;
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
) ]
2014-12-17 23:03:58 +00:00
public int GetTopScanline ( bool pal = false )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _quicknes ! = null )
2014-04-22 01:38:19 +00:00
{
2014-12-17 23:03:58 +00:00
return _quicknes . GetSettings ( ) . ClipTopAndBottom ? 8 : 0 ;
2014-04-22 01:38:19 +00:00
}
2014-12-17 23:03:58 +00:00
if ( _neshawk ! = null )
{
return pal
? _neshawk . GetSettings ( ) . PAL_TopLine
: _neshawk . GetSettings ( ) . NTSC_TopLine ;
}
throw new InvalidOperationException ( ) ;
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-12-17 23:03:58 +00:00
if ( NESAvailable )
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
) ]
2014-12-17 23:03:58 +00:00
public void SetAllowMoreThanEightSprites ( bool allow )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _neshawk ! = null )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
var s = _neshawk . GetSettings ( ) ;
2013-12-22 00:44:39 +00:00
s . AllowMoreThanEightSprites = allow ;
2014-12-17 23:03:58 +00:00
_neshawk . PutSettings ( s ) ;
2013-10-28 19:13:01 +00:00
}
2014-12-17 23:03:58 +00:00
else if ( _quicknes ! = null )
2014-04-22 01:38:19 +00:00
{
2014-12-17 23:03:58 +00:00
var s = _quicknes . GetSettings ( ) ;
2014-04-22 01:38:19 +00:00
s . NumSprites = allow ? 64 : 8 ;
2014-12-17 23:03:58 +00:00
_quicknes . PutSettings ( s ) ;
2014-04-22 01:38:19 +00:00
}
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
) ]
2014-12-17 23:03:58 +00:00
public void SetClipLeftAndRight ( bool leftandright )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _neshawk ! = null )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
var s = _neshawk . GetSettings ( ) ;
2013-12-22 00:44:39 +00:00
s . ClipLeftAndRight = leftandright ;
2014-12-17 23:03:58 +00:00
_neshawk . PutSettings ( s ) ;
2013-10-28 19:13:01 +00:00
}
2014-12-17 23:03:58 +00:00
else if ( _quicknes ! = null )
2014-04-22 01:38:19 +00:00
{
2014-12-17 23:03:58 +00:00
var s = _quicknes . GetSettings ( ) ;
2014-04-22 01:38:19 +00:00
s . ClipLeftAndRight = leftandright ;
2014-12-17 23:03:58 +00:00
_quicknes . PutSettings ( s ) ;
2014-04-22 01:38:19 +00:00
}
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
) ]
2014-12-17 23:03:58 +00:00
public void SetDisplayBackground ( bool show )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _neshawk ! = null )
2013-12-22 00:44:39 +00:00
{
2014-12-17 23:03:58 +00:00
var s = _neshawk . GetSettings ( ) ;
2013-12-22 00:44:39 +00:00
s . DispBackground = show ;
2014-12-17 23:03:58 +00:00
_neshawk . PutSettings ( s ) ;
2013-12-22 00:44:39 +00:00
}
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
) ]
2014-12-17 23:03:58 +00:00
public void SetDisplaySprites ( bool show )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _neshawk ! = null )
2013-12-22 00:44:39 +00:00
{
2014-12-17 23:03:58 +00:00
var s = _neshawk . GetSettings ( ) ;
2013-12-22 00:44:39 +00:00
s . DispSprites = show ;
2014-12-17 23:03:58 +00:00
_neshawk . PutSettings ( s ) ;
}
else if ( _quicknes ! = null )
{
var s = _quicknes . GetSettings ( ) ;
s . NumSprites = show ? 8 : 0 ;
_quicknes . PutSettings ( s ) ;
2013-12-22 00:44:39 +00:00
}
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-12-17 23:03:58 +00:00
public void SetScanlines ( int top , int bottom , bool pal = false )
2013-10-28 19:13:01 +00:00
{
2014-12-17 23:03:58 +00:00
if ( _neshawk ! = null )
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
2014-12-17 23:03:58 +00:00
var s = _neshawk . 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
2014-12-17 23:03:58 +00:00
_neshawk . PutSettings ( s ) ;
2013-10-28 19:13:01 +00:00
}
}
}
}