lua: internal infrsatructure changes to nes library

This commit is contained in:
goyuken 2014-12-17 23:03:58 +00:00
parent eaaaef8f1d
commit cfaf59f887
2 changed files with 104 additions and 60 deletions

View File

@ -20,6 +20,13 @@ namespace BizHawk.Client.Common
public NesLuaLibrary(Lua lua) public NesLuaLibrary(Lua lua)
: base(lua) { } : base(lua) { }
[OptionalService]
private NES _neshawk { get; set; }
[OptionalService]
private QuickNES _quicknes { get; set; }
private bool NESAvailable { get { return _neshawk != null || _quicknes != null; } }
public NesLuaLibrary(Lua lua, Action<string> logOutputCallback) public NesLuaLibrary(Lua lua, Action<string> logOutputCallback)
: base(lua, logOutputCallback) { } : base(lua, logOutputCallback) { }
@ -31,7 +38,7 @@ namespace BizHawk.Client.Common
)] )]
public void AddGameGenie(string code) public void AddGameGenie(string code)
{ {
if (Global.Emulator.SystemId == "NES") if (NESAvailable)
{ {
var decoder = new NESGameGenieDecoder(code); var decoder = new NESGameGenieDecoder(code);
var watch = Watch.GenerateWatch( var watch = Watch.GenerateWatch(
@ -49,95 +56,121 @@ namespace BizHawk.Client.Common
} }
} }
private static QuickNES AsQuickNES { get { return Global.Emulator as QuickNES; } }
private static NES AsNES { get { return Global.Emulator as NES; } }
[LuaMethodAttributes( [LuaMethodAttributes(
"getallowmorethaneightsprites", "getallowmorethaneightsprites",
"Gets the NES setting 'Allow more than 8 sprites per scanline' value" "Gets the NES setting 'Allow more than 8 sprites per scanline' value"
)] )]
public static bool GetAllowMoreThanEightSprites() public bool GetAllowMoreThanEightSprites()
{ {
if (AsQuickNES != null) if (_quicknes != null)
{ {
return AsQuickNES.GetSettings().NumSprites != 8; return _quicknes.GetSettings().NumSprites != 8;
}
if (_neshawk != null)
{
return _neshawk.GetSettings().AllowMoreThanEightSprites;
} }
return AsNES.GetSettings().AllowMoreThanEightSprites; throw new InvalidOperationException();
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"getbottomscanline", "getbottomscanline",
"Gets the current value for the bottom scanline value" "Gets the current value for the bottom scanline value"
)] )]
public static int GetBottomScanline(bool pal = false) public int GetBottomScanline(bool pal = false)
{ {
if (AsQuickNES != null) if (_quicknes != null)
{ {
return AsQuickNES.GetSettings().ClipTopAndBottom ? 231 : 239; return _quicknes.GetSettings().ClipTopAndBottom ? 231 : 239;
} }
if (_neshawk != null)
{
return pal return pal
? AsNES.GetSettings().PAL_BottomLine ? _neshawk.GetSettings().PAL_BottomLine
: AsNES.GetSettings().NTSC_BottomLine; : _neshawk.GetSettings().NTSC_BottomLine;
}
throw new InvalidOperationException();
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"getclipleftandright", "getclipleftandright",
"Gets the current value for the Clip Left and Right sides option" "Gets the current value for the Clip Left and Right sides option"
)] )]
public static bool GetClipLeftAndRight() public bool GetClipLeftAndRight()
{ {
if (AsQuickNES != null) if (_quicknes != null)
{ {
return AsQuickNES.GetSettings().ClipLeftAndRight; return _quicknes.GetSettings().ClipLeftAndRight;
} }
return AsNES.GetSettings().ClipLeftAndRight; if (_neshawk != null)
{
return _neshawk.GetSettings().ClipLeftAndRight;
}
throw new InvalidOperationException();
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"getdispbackground", "getdispbackground",
"Indicates whether or not the bg layer is being displayed" "Indicates whether or not the bg layer is being displayed"
)] )]
public static bool GetDisplayBackground() public bool GetDisplayBackground()
{ {
if (AsQuickNES != null) if (_quicknes != null)
{ {
return true; return true;
} }
return AsNES.GetSettings().DispBackground; if (_neshawk != null)
{
return _neshawk.GetSettings().DispBackground;
}
throw new InvalidOperationException();
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"getdispsprites", "getdispsprites",
"Indicates whether or not sprites are being displayed" "Indicates whether or not sprites are being displayed"
)] )]
public static bool GetDisplaySprites() public bool GetDisplaySprites()
{ {
if (AsQuickNES != null) if (_quicknes != null)
{ {
return true; return _quicknes.GetSettings().NumSprites > 0;
} }
return AsNES.GetSettings().DispSprites; if (_neshawk != null)
{
return _neshawk.GetSettings().DispSprites;
}
throw new InvalidOperationException();
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"gettopscanline", "gettopscanline",
"Gets the current value for the top scanline value" "Gets the current value for the top scanline value"
)] )]
public static int GetTopScanline(bool pal = false) public int GetTopScanline(bool pal = false)
{ {
if (AsQuickNES != null) if (_quicknes != null)
{ {
return AsQuickNES.GetSettings().ClipTopAndBottom ? 8 : 0; return _quicknes.GetSettings().ClipTopAndBottom ? 8 : 0;
} }
if (_neshawk != null)
{
return pal return pal
? AsNES.GetSettings().PAL_TopLine ? _neshawk.GetSettings().PAL_TopLine
: AsNES.GetSettings().NTSC_TopLine; : _neshawk.GetSettings().NTSC_TopLine;
}
throw new InvalidOperationException();
} }
[LuaMethodAttributes( [LuaMethodAttributes(
@ -146,7 +179,7 @@ namespace BizHawk.Client.Common
)] )]
public void RemoveGameGenie(string code) public void RemoveGameGenie(string code)
{ {
if (Global.Emulator.SystemId == "NES") if (NESAvailable)
{ {
var decoder = new NESGameGenieDecoder(code); var decoder = new NESGameGenieDecoder(code);
Global.CheatList.RemoveRange( Global.CheatList.RemoveRange(
@ -158,19 +191,19 @@ namespace BizHawk.Client.Common
"setallowmorethaneightsprites", "setallowmorethaneightsprites",
"Sets the NES setting 'Allow more than 8 sprites per scanline'" "Sets the NES setting 'Allow more than 8 sprites per scanline'"
)] )]
public static void SetAllowMoreThanEightSprites(bool allow) public void SetAllowMoreThanEightSprites(bool allow)
{ {
if (Global.Emulator is NES) if (_neshawk != null)
{ {
var s = AsNES.GetSettings(); var s = _neshawk.GetSettings();
s.AllowMoreThanEightSprites = allow; s.AllowMoreThanEightSprites = allow;
AsNES.PutSettings(s); _neshawk.PutSettings(s);
} }
else if (Global.Emulator is QuickNES) else if (_quicknes != null)
{ {
var s = AsQuickNES.GetSettings(); var s = _quicknes.GetSettings();
s.NumSprites = allow ? 64 : 8; s.NumSprites = allow ? 64 : 8;
AsQuickNES.PutSettings(s); _quicknes.PutSettings(s);
} }
} }
@ -179,19 +212,19 @@ namespace BizHawk.Client.Common
"setclipleftandright", "setclipleftandright",
"Sets the Clip Left and Right sides option" "Sets the Clip Left and Right sides option"
)] )]
public static void SetClipLeftAndRight(bool leftandright) public void SetClipLeftAndRight(bool leftandright)
{ {
if (Global.Emulator is NES) if (_neshawk != null)
{ {
var s = AsNES.GetSettings(); var s = _neshawk.GetSettings();
s.ClipLeftAndRight = leftandright; s.ClipLeftAndRight = leftandright;
AsNES.PutSettings(s); _neshawk.PutSettings(s);
} }
else if (Global.Emulator is QuickNES) else if (_quicknes != null)
{ {
var s = AsQuickNES.GetSettings(); var s = _quicknes.GetSettings();
s.ClipLeftAndRight = leftandright; s.ClipLeftAndRight = leftandright;
AsQuickNES.PutSettings(s); _quicknes.PutSettings(s);
} }
} }
@ -199,13 +232,13 @@ namespace BizHawk.Client.Common
"setdispbackground", "setdispbackground",
"Sets whether or not the background layer will be displayed" "Sets whether or not the background layer will be displayed"
)] )]
public static void SetDisplayBackground(bool show) public void SetDisplayBackground(bool show)
{ {
if (Global.Emulator is NES) if (_neshawk != null)
{ {
var s = AsNES.GetSettings(); var s = _neshawk.GetSettings();
s.DispBackground = show; s.DispBackground = show;
AsNES.PutSettings(s); _neshawk.PutSettings(s);
} }
} }
@ -213,13 +246,19 @@ namespace BizHawk.Client.Common
"setdispsprites", "setdispsprites",
"Sets whether or not sprites will be displayed" "Sets whether or not sprites will be displayed"
)] )]
public static void SetDisplaySprites(bool show) public void SetDisplaySprites(bool show)
{ {
if (Global.Emulator is NES) if (_neshawk != null)
{ {
var s = AsNES.GetSettings(); var s = _neshawk.GetSettings();
s.DispSprites = show; s.DispSprites = show;
AsNES.PutSettings(s); _neshawk.PutSettings(s);
}
else if (_quicknes != null)
{
var s = _quicknes.GetSettings();
s.NumSprites = show ? 8 : 0;
_quicknes.PutSettings(s);
} }
} }
@ -227,9 +266,9 @@ namespace BizHawk.Client.Common
"setscanlines", "setscanlines",
"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" "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"
)] )]
public static void SetScanlines(int top, int bottom, bool pal = false) public void SetScanlines(int top, int bottom, bool pal = false)
{ {
if (Global.Emulator is NES) if (_neshawk != null)
{ {
if (top > 127) if (top > 127)
{ {
@ -249,7 +288,7 @@ namespace BizHawk.Client.Common
bottom = 128; bottom = 128;
} }
var s = AsNES.GetSettings(); var s = _neshawk.GetSettings();
if (pal) if (pal)
{ {
@ -262,7 +301,7 @@ namespace BizHawk.Client.Common
s.NTSC_BottomLine = bottom; s.NTSC_BottomLine = bottom;
} }
AsNES.PutSettings(s); _neshawk.PutSettings(s);
} }
} }
} }

View File

@ -6,6 +6,7 @@ using BizHawk.Client.Common;
using LuaInterface; using LuaInterface;
using System.Reflection; using System.Reflection;
using System.Collections.Generic; using System.Collections.Generic;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
@ -59,11 +60,13 @@ namespace BizHawk.Client.EmuHawk
*/ */
// Register lua libraries // Register lua libraries
// why sealed ones only?
var libs = Assembly var libs = Assembly
.Load("BizHawk.Client.Common") .Load("BizHawk.Client.Common")
.GetTypes() .GetTypes()
.Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t)) .Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t))
.Where(t => t.IsSealed) .Where(t => t.IsSealed)
.Where(t => ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, t))
.ToList(); .ToList();
libs.AddRange( libs.AddRange(
@ -72,6 +75,7 @@ namespace BizHawk.Client.EmuHawk
.GetTypes() .GetTypes()
.Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t)) .Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t))
.Where(t => t.IsSealed) .Where(t => t.IsSealed)
.Where(t => ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, t))
); );
foreach (var lib in libs) foreach (var lib in libs)
@ -88,6 +92,7 @@ namespace BizHawk.Client.EmuHawk
var instance = (LuaLibraryBase)Activator.CreateInstance(lib, _lua); var instance = (LuaLibraryBase)Activator.CreateInstance(lib, _lua);
instance.LuaRegister(lib, Docs); instance.LuaRegister(lib, Docs);
instance.LogOutputCallback = ConsoleLuaLibrary.LogOutput; instance.LogOutputCallback = ConsoleLuaLibrary.LogOutput;
ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, instance);
Libraries.Add(lib, instance); Libraries.Add(lib, instance);
} }
} }
@ -147,7 +152,7 @@ namespace BizHawk.Client.EmuHawk
public Lua SpawnCoroutine(string file) public Lua SpawnCoroutine(string file)
{ {
var lua = _lua.NewThread(); var lua = _lua.NewThread();
var main = lua.LoadFile(PathManager.MakeAbsolutePath(file,null)); var main = lua.LoadFile(PathManager.MakeAbsolutePath(file, null));
lua.Push(main); // push main function on to stack for subsequent resuming lua.Push(main); // push main function on to stack for subsequent resuming
return lua; return lua;
} }