BizHawk/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs

163 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.Threading;
using BizHawk.Client.Common;
using LuaInterface;
namespace BizHawk.Client.EmuHawk
{
2013-11-25 00:55:56 +00:00
public class EmuLuaLibrary
{
2013-11-25 00:55:56 +00:00
private readonly FormsLuaLibrary _formsLibrary = new FormsLuaLibrary();
private readonly EventLuaLibrary _eventLibrary = new EventLuaLibrary(ConsoleLuaLibrary.console_log);
private readonly GuiLuaLibrary _guiLibrary = new GuiLuaLibrary();
private readonly LuaConsole _caller;
private Lua _lua = new Lua();
private Lua _currThread;
public EmuLuaLibrary()
{
Docs = new LuaDocumentation();
}
2014-01-21 16:31:12 +00:00
public EmuLuaLibrary(LuaConsole passed)
: this()
2014-01-21 16:31:12 +00:00
{
LuaWait = new AutoResetEvent(false);
Docs.Clear();
_caller = passed.Get();
LuaRegister(_lua);
}
public LuaDocumentation Docs { get; private set; }
public bool IsRunning { get; set; }
public EventWaitHandle LuaWait { get; private set; }
public bool FrameAdvanceRequested { get; private set; }
public GuiLuaLibrary GuiLibrary
{
get { return _guiLibrary; }
}
2013-10-31 16:45:08 +00:00
public void WindowClosed(IntPtr handle)
{
_formsLibrary.WindowClosed(handle);
}
public void CallSaveStateEvent(string name)
{
_eventLibrary.CallSaveStateEvent(name);
}
public void CallLoadStateEvent(string name)
{
_eventLibrary.CallLoadStateEvent(name);
}
public LuaFunctionList RegisteredFunctions
{
get { return _eventLibrary.RegisteredFunctions; }
}
public void CallFrameBeforeEvent()
{
_eventLibrary.CallFrameBeforeEvent();
}
public void CallFrameAfterEvent()
{
_eventLibrary.CallFrameAfterEvent();
}
public void Close()
{
_lua = new Lua();
_guiLibrary.Dispose();
}
public void LuaRegister(Lua lua)
{
lua.RegisterFunction("print", this, GetType().GetMethod("Print"));
new BitLuaLibrary().LuaRegisterNew(lua, Docs);
new MultiClientLuaLibrary(ConsoleLuaLibrary.console_log).LuaRegister(lua, Docs);
new ConsoleLuaLibrary().LuaRegister(lua, Docs);
new EmulatorLuaLibrary(
_lua,
2013-11-25 00:55:56 +00:00
Frameadvance,
EmuYield
).LuaRegisterNew(lua, Docs);
2014-01-25 21:37:25 +00:00
_eventLibrary.LuaRegisterNew(lua, Docs);
2013-10-31 16:45:08 +00:00
_formsLibrary.LuaRegister(lua, Docs);
_guiLibrary.LuaRegister(lua, Docs);
new InputLuaLibrary(_lua).LuaRegister(lua, Docs);
new JoypadLuaLibrary(_lua).LuaRegisterNew(lua, Docs);
2014-01-26 02:43:28 +00:00
new MemoryLuaLibrary().LuaRegisterNew(lua, Docs);
new MainMemoryLuaLibrary(_lua).LuaRegisterNew(lua, Docs);
2014-01-26 02:50:26 +00:00
new MovieLuaLibrary(_lua).LuaRegisterNew(lua, Docs);
new NESLuaLibrary().LuaRegisterNew(lua, Docs);
new SavestateLuaLibrary().LuaRegister(lua, Docs);
new SNESLuaLibrary().LuaRegisterNew(lua, Docs);
2014-01-20 17:06:09 +00:00
new StringLuaLibrary().LuaRegister(lua, Docs);
2013-10-28 20:57:25 +00:00
Docs.Sort();
}
2013-11-25 00:55:56 +00:00
public Lua SpawnCoroutine(string file)
{
var lua = _lua.NewThread();
2013-11-25 00:55:56 +00:00
var main = lua.LoadFile(file);
lua.Push(main); // push main function on to stack for subsequent resuming
return lua;
}
public class ResumeResult
{
public bool WaitForFrame { get; set; }
public bool Terminated { get; set; }
}
public ResumeResult ResumeScript(Lua script)
{
_eventLibrary.CurrentThread = script;
2013-11-25 00:55:56 +00:00
_currThread = script;
var execResult = script.Resume(0);
2013-11-25 00:55:56 +00:00
_currThread = null;
var result = new ResumeResult();
if (execResult == 0)
{
// terminated
result.Terminated = true;
}
else
{
// yielded
result.WaitForFrame = FrameAdvanceRequested;
}
FrameAdvanceRequested = false;
return result;
}
2013-11-25 00:55:56 +00:00
public void Print(string s)
{
_caller.ConsoleLog(s);
}
private void Frameadvance()
{
FrameAdvanceRequested = true;
2013-11-25 00:55:56 +00:00
_currThread.Yield(0);
}
private void EmuYield()
{
GlobalWin.DisplayManager.NeedsToPaint = true;
2013-11-25 00:55:56 +00:00
_currThread.Yield(0);
}
}
}