2013-10-28 19:13:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
2013-10-31 16:58:56 +00:00
|
|
|
|
|
|
|
|
|
using BizHawk.Client.Common;
|
2014-01-21 00:43:57 +00:00
|
|
|
|
using LuaInterface;
|
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
|
|
|
|
{
|
2013-11-25 00:55:56 +00:00
|
|
|
|
public class EmuLuaLibrary
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-05-21 00:17:35 +00:00
|
|
|
|
private readonly FormsLuaLibrary _formsLibrary;
|
|
|
|
|
private readonly EventLuaLibrary _eventLibrary;
|
|
|
|
|
private readonly GuiLuaLibrary _guiLibrary;
|
2014-01-21 00:43:57 +00:00
|
|
|
|
private readonly LuaConsole _caller;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
2014-01-21 00:43:57 +00:00
|
|
|
|
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)
|
2014-01-25 21:10:51 +00:00
|
|
|
|
: this()
|
2014-01-21 16:31:12 +00:00
|
|
|
|
{
|
|
|
|
|
LuaWait = new AutoResetEvent(false);
|
|
|
|
|
Docs.Clear();
|
|
|
|
|
_caller = passed.Get();
|
2014-05-21 00:17:35 +00:00
|
|
|
|
|
|
|
|
|
// Register lua libraries
|
2014-05-21 01:15:52 +00:00
|
|
|
|
|
2014-05-21 00:17:35 +00:00
|
|
|
|
_lua.RegisterFunction("print", this, GetType().GetMethod("Print"));
|
|
|
|
|
|
2014-05-21 01:15:52 +00:00
|
|
|
|
// TODO: Search the assemblies for objects that inherit LuaBaseLibrary, and instantiate and register them and put them into an array,
|
|
|
|
|
// rather than call them all by name here
|
|
|
|
|
|
2014-05-21 00:17:35 +00:00
|
|
|
|
_formsLibrary = new FormsLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput);
|
|
|
|
|
_formsLibrary.LuaRegister(Docs);
|
|
|
|
|
|
|
|
|
|
_eventLibrary = new EventLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput);
|
|
|
|
|
_eventLibrary.LuaRegister(Docs);
|
|
|
|
|
|
|
|
|
|
_guiLibrary = new GuiLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput);
|
|
|
|
|
_guiLibrary.LuaRegister(Docs);
|
|
|
|
|
|
|
|
|
|
new BitLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new EmuHawkLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new ConsoleLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
|
2014-05-21 01:15:52 +00:00
|
|
|
|
var emuLib = new EmulatorLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput)
|
|
|
|
|
{
|
|
|
|
|
FrameAdvanceCallback = Frameadvance,
|
|
|
|
|
YieldCallback = EmuYield
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
emuLib.LuaRegister(Docs);
|
2014-05-21 00:17:35 +00:00
|
|
|
|
|
|
|
|
|
new InputLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new JoypadLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new MemoryLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new MainMemoryLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
|
|
|
|
|
new MovieLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new NesLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new SavestateLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new SnesLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new StringLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
new GameInfoLuaLibrary(_lua, ConsoleLuaLibrary.LogOutput).LuaRegister(Docs);
|
|
|
|
|
|
|
|
|
|
Docs.Sort();
|
2014-01-21 16:31:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-21 00:43:57 +00:00
|
|
|
|
public LuaDocumentation Docs { get; private set; }
|
|
|
|
|
public bool IsRunning { get; set; }
|
|
|
|
|
public EventWaitHandle LuaWait { get; private set; }
|
|
|
|
|
public bool FrameAdvanceRequested { get; private set; }
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2014-02-14 01:27:38 +00:00
|
|
|
|
public LuaFunctionList RegisteredFunctions
|
|
|
|
|
{
|
|
|
|
|
get { return _eventLibrary.RegisteredFunctions; }
|
|
|
|
|
}
|
|
|
|
|
|
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 void CallFrameBeforeEvent()
|
|
|
|
|
{
|
|
|
|
|
_eventLibrary.CallFrameBeforeEvent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CallFrameAfterEvent()
|
|
|
|
|
{
|
|
|
|
|
_eventLibrary.CallFrameAfterEvent();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-26 03:08:16 +00:00
|
|
|
|
public void CallExitEvent(Lua thread)
|
|
|
|
|
{
|
|
|
|
|
_eventLibrary.CallExitEvent(thread);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-28 19:13:01 +00:00
|
|
|
|
public void Close()
|
|
|
|
|
{
|
|
|
|
|
_lua = new Lua();
|
2013-10-31 18:43:01 +00:00
|
|
|
|
_guiLibrary.Dispose();
|
|
|
|
|
}
|
2013-10-28 19:13:01 +00:00
|
|
|
|
|
2013-11-25 00:55:56 +00:00
|
|
|
|
public Lua SpawnCoroutine(string file)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-11-26 01:21:24 +00:00
|
|
|
|
var lua = _lua.NewThread();
|
2013-11-25 00:55:56 +00:00
|
|
|
|
var main = lua.LoadFile(file);
|
2014-01-21 00:43:57 +00:00
|
|
|
|
lua.Push(main); // push main function on to stack for subsequent resuming
|
2013-11-25 00:44:18 +00:00
|
|
|
|
return lua;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResumeResult ResumeScript(Lua script)
|
|
|
|
|
{
|
2013-11-26 01:21:24 +00:00
|
|
|
|
_eventLibrary.CurrentThread = script;
|
2013-11-25 00:55:56 +00:00
|
|
|
|
_currThread = script;
|
2013-11-26 01:21:24 +00:00
|
|
|
|
var execResult = script.Resume(0);
|
2013-11-25 00:55:56 +00:00
|
|
|
|
_currThread = null;
|
2013-10-28 19:13:01 +00:00
|
|
|
|
var result = new ResumeResult();
|
|
|
|
|
if (execResult == 0)
|
|
|
|
|
{
|
2014-01-21 00:43:57 +00:00
|
|
|
|
// terminated
|
2013-10-28 19:13:01 +00:00
|
|
|
|
result.Terminated = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-21 00:43:57 +00:00
|
|
|
|
// yielded
|
2013-10-28 19:13:01 +00:00
|
|
|
|
result.WaitForFrame = FrameAdvanceRequested;
|
|
|
|
|
}
|
2014-01-21 00:43:57 +00:00
|
|
|
|
|
2013-10-28 19:13:01 +00:00
|
|
|
|
FrameAdvanceRequested = false;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 05:07:43 +00:00
|
|
|
|
public static void Print(params object[] outputs)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-04-29 05:07:43 +00:00
|
|
|
|
ConsoleLuaLibrary.Log(outputs);
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 18:09:40 +00:00
|
|
|
|
private void Frameadvance()
|
|
|
|
|
{
|
|
|
|
|
FrameAdvanceRequested = true;
|
2013-11-25 00:55:56 +00:00
|
|
|
|
_currThread.Yield(0);
|
2013-10-31 18:09:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EmuYield()
|
|
|
|
|
{
|
2013-11-03 16:07:58 +00:00
|
|
|
|
GlobalWin.DisplayManager.NeedsToPaint = true;
|
2013-11-25 00:55:56 +00:00
|
|
|
|
_currThread.Yield(0);
|
2013-10-31 18:09:40 +00:00
|
|
|
|
}
|
2014-02-14 01:27:38 +00:00
|
|
|
|
|
|
|
|
|
public class ResumeResult
|
|
|
|
|
{
|
|
|
|
|
public bool WaitForFrame { get; set; }
|
|
|
|
|
public bool Terminated { get; set; }
|
|
|
|
|
}
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|