2013-10-28 19:13:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
2013-10-31 16:58:56 +00:00
|
|
|
|
|
2013-10-28 19:13:01 +00:00
|
|
|
|
using LuaInterface;
|
2013-10-31 16:58:56 +00:00
|
|
|
|
using BizHawk.Client.Common;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
2013-11-03 03:54:37 +00:00
|
|
|
|
namespace BizHawk.Client.EmuHawk
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
|
|
|
|
public partial class EmuLuaLibrary
|
|
|
|
|
{
|
|
|
|
|
private Lua _lua = new Lua();
|
2013-10-28 20:57:25 +00:00
|
|
|
|
private readonly LuaConsole _caller;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
private Lua currThread;
|
2013-10-31 16:45:08 +00:00
|
|
|
|
private FormsLuaLibrary _formsLibrary = new FormsLuaLibrary();
|
2013-11-01 14:51:51 +00:00
|
|
|
|
private EventLuaLibrary _eventLibrary = new EventLuaLibrary(ConsoleLuaLibrary.console_log);
|
2013-10-31 18:43:01 +00:00
|
|
|
|
private GuiLuaLibrary _guiLibrary = new GuiLuaLibrary();
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
2013-10-28 20:57:25 +00:00
|
|
|
|
public LuaDocumentation Docs = new LuaDocumentation();
|
|
|
|
|
public bool IsRunning;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
public EventWaitHandle LuaWait;
|
|
|
|
|
public bool FrameAdvanceRequested;
|
|
|
|
|
|
2013-10-31 18:43:01 +00:00
|
|
|
|
public GuiLuaLibrary GuiLibrary
|
|
|
|
|
{
|
2013-11-01 14:51:51 +00:00
|
|
|
|
get { return _guiLibrary; }
|
2013-10-31 18:43:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 16:45:08 +00:00
|
|
|
|
public void WindowClosed(IntPtr handle)
|
|
|
|
|
{
|
|
|
|
|
_formsLibrary.WindowClosed(handle);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 16:58:56 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-28 19:13:01 +00:00
|
|
|
|
public EmuLuaLibrary(LuaConsole passed)
|
|
|
|
|
{
|
|
|
|
|
LuaWait = new AutoResetEvent(false);
|
2013-10-28 20:57:25 +00:00
|
|
|
|
Docs.Clear();
|
2013-11-17 15:55:13 +00:00
|
|
|
|
_caller = passed.Get();
|
2013-10-28 19:13:01 +00:00
|
|
|
|
LuaRegister(_lua);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
|
{
|
|
|
|
|
_lua = new Lua();
|
2013-10-31 18:43:01 +00:00
|
|
|
|
_guiLibrary.Dispose();
|
|
|
|
|
}
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
|
|
|
|
public void LuaRegister(Lua lua)
|
|
|
|
|
{
|
|
|
|
|
lua.RegisterFunction("print", this, GetType().GetMethod("print"));
|
|
|
|
|
|
2013-10-31 00:31:25 +00:00
|
|
|
|
new BitLuaLibrary().LuaRegister(lua, Docs);
|
|
|
|
|
new MultiClientLuaLibrary(ConsoleLuaLibrary.console_log).LuaRegister(lua, Docs);
|
|
|
|
|
new ConsoleLuaLibrary().LuaRegister(lua, Docs);
|
2013-10-31 18:09:40 +00:00
|
|
|
|
|
|
|
|
|
new EmulatorLuaLibrary(
|
2013-11-11 03:20:33 +00:00
|
|
|
|
_lua,
|
2013-10-31 18:09:40 +00:00
|
|
|
|
new Action(Frameadvance),
|
|
|
|
|
new Action(EmuYield)
|
|
|
|
|
).LuaRegister(lua, Docs);
|
|
|
|
|
|
2013-10-31 16:58:56 +00:00
|
|
|
|
_eventLibrary.LuaRegister(lua, Docs);
|
2013-10-31 16:45:08 +00:00
|
|
|
|
_formsLibrary.LuaRegister(lua, Docs);
|
2013-10-31 18:43:01 +00:00
|
|
|
|
_guiLibrary.LuaRegister(lua, Docs);
|
2013-10-31 13:07:42 +00:00
|
|
|
|
new InputLuaLibrary(_lua).LuaRegister(lua, Docs);
|
|
|
|
|
new JoypadLuaLibrary(_lua).LuaRegister(lua, Docs);
|
2013-10-31 16:10:20 +00:00
|
|
|
|
new MemoryLuaLibrary().LuaRegister(lua, Docs);
|
|
|
|
|
new MainMemoryLuaLibrary(_lua).LuaRegister(lua, Docs);
|
2013-10-31 13:07:42 +00:00
|
|
|
|
new MovieLuaLibrary(_lua).LuaRegister(lua, Docs);
|
2013-11-04 15:52:59 +00:00
|
|
|
|
new NESLuaLibrary().LuaRegister(lua, Docs);
|
2013-10-31 16:20:45 +00:00
|
|
|
|
new SavestateLuaLibrary().LuaRegister(lua, Docs);
|
2013-10-31 00:31:25 +00:00
|
|
|
|
new SNESLuaLibrary().LuaRegister(lua, Docs);
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
2013-10-28 20:57:25 +00:00
|
|
|
|
Docs.Sort();
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Lua SpawnCoroutine(string File)
|
|
|
|
|
{
|
2013-11-25 00:44:18 +00:00
|
|
|
|
Lua lua = _lua.NewThread();
|
|
|
|
|
var main = lua.LoadFile(File);
|
|
|
|
|
lua.Push(main); //push main function on to stack for subsequent resuming
|
|
|
|
|
return lua;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ResumeResult
|
|
|
|
|
{
|
|
|
|
|
public bool WaitForFrame;
|
|
|
|
|
public bool Terminated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResumeResult ResumeScript(Lua script)
|
|
|
|
|
{
|
|
|
|
|
currThread = script;
|
|
|
|
|
int execResult = script.Resume(0);
|
|
|
|
|
currThread = null;
|
|
|
|
|
var result = new ResumeResult();
|
|
|
|
|
if (execResult == 0)
|
|
|
|
|
{
|
|
|
|
|
//terminated
|
|
|
|
|
result.Terminated = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//yielded
|
|
|
|
|
result.WaitForFrame = FrameAdvanceRequested;
|
|
|
|
|
}
|
|
|
|
|
FrameAdvanceRequested = false;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void print(string s)
|
|
|
|
|
{
|
2013-10-28 20:57:25 +00:00
|
|
|
|
_caller.AddText(s);
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 18:09:40 +00:00
|
|
|
|
private void Frameadvance()
|
|
|
|
|
{
|
|
|
|
|
FrameAdvanceRequested = true;
|
|
|
|
|
currThread.Yield(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EmuYield()
|
|
|
|
|
{
|
2013-11-03 16:07:58 +00:00
|
|
|
|
GlobalWin.DisplayManager.NeedsToPaint = true;
|
2013-10-31 18:09:40 +00:00
|
|
|
|
currThread.Yield(0);
|
|
|
|
|
}
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|