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

258 lines
6.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using NLua;
using BizHawk.Common.ReflectionExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk
{
2013-11-25 00:55:56 +00:00
public class EmuLuaLibrary
{
public EmuLuaLibrary()
{
Docs = new LuaDocumentation();
//if(NLua.Lua.WhichLua == "NLua")
_lua["keepalives"] = _lua.NewTable();
}
2017-02-22 16:11:08 +00:00
public EmuLuaLibrary(IEmulatorServiceProvider serviceProvider)
: this()
2014-01-21 16:31:12 +00:00
{
LuaWait = new AutoResetEvent(false);
Docs.Clear();
// Register lua libraries
var libs = Assembly
.Load("BizHawk.Client.Common")
.GetTypes()
.Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t))
.Where(t => t.IsSealed)
.Where(t => ServiceInjector.IsAvailable(serviceProvider, t))
.ToList();
libs.AddRange(
Assembly
.GetAssembly(typeof(EmuLuaLibrary))
.GetTypes()
.Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t))
.Where(t => t.IsSealed)
2017-05-19 14:47:18 +00:00
.Where(t => ServiceInjector.IsAvailable(serviceProvider, t)));
foreach (var lib in libs)
{
bool addLibrary = true;
var attributes = lib.GetCustomAttributes(typeof(LuaLibraryAttribute), false);
if (attributes.Any())
{
addLibrary = VersionInfo.DeveloperBuild || (attributes.First() as LuaLibraryAttribute).Released;
}
if (addLibrary)
{
var instance = (LuaLibraryBase)Activator.CreateInstance(lib, _lua);
instance.LuaRegister(lib, Docs);
instance.LogOutputCallback = ConsoleLuaLibrary.LogOutput;
ServiceInjector.UpdateServices(serviceProvider, instance);
Libraries.Add(lib, instance);
}
}
_lua.RegisterFunction("print", this, GetType().GetMethod("Print"));
EmulatorLuaLibrary.FrameAdvanceCallback = Frameadvance;
EmulatorLuaLibrary.YieldCallback = EmuYield;
// Add LuaCanvas to Docs
Type luaCanvas = typeof(LuaCanvas);
var methods = luaCanvas
.GetMethods()
.Where(m => m.GetCustomAttributes(typeof(LuaMethodAttribute), false).Any());
foreach (var method in methods)
{
Docs.Add(new LibraryFunction(nameof(LuaCanvas), luaCanvas.Description(), method));
}
2014-01-21 16:31:12 +00:00
}
public bool IsRebootingCore { get; set; } // pretty hacky.. we dont want a lua script to be able to restart itself by rebooting the core
private readonly Dictionary<Type, LuaLibraryBase> Libraries = new Dictionary<Type, LuaLibraryBase>();
public LuaFileList ScriptList { get; } = new LuaFileList();
public IEnumerable<LuaFile> RunningScripts
{
get { return ScriptList.Where(lf => lf.Enabled); }
}
private Lua _lua = new Lua();
private Lua _currThread;
private FormsLuaLibrary FormsLibrary => (FormsLuaLibrary)Libraries[typeof(FormsLuaLibrary)];
private EventLuaLibrary EventsLibrary => (EventLuaLibrary)Libraries[typeof(EventLuaLibrary)];
private EmulatorLuaLibrary EmulatorLuaLibrary => (EmulatorLuaLibrary)Libraries[typeof(EmulatorLuaLibrary)];
public GuiLuaLibrary GuiLibrary => (GuiLuaLibrary)Libraries[typeof(GuiLuaLibrary)];
public void Restart(IEmulatorServiceProvider newServiceProvider)
{
foreach (var lib in Libraries)
{
ServiceInjector.UpdateServices(newServiceProvider, lib.Value);
}
}
public void StartLuaDrawing()
{
if (ScriptList.Any() && GuiLibrary.SurfaceIsNull)
{
GuiLibrary.DrawNew("emu");
}
}
public void EndLuaDrawing()
{
if (ScriptList.Any())
{
GuiLibrary.DrawFinish();
}
}
2017-05-19 14:47:18 +00:00
public LuaDocumentation Docs { get; }
public bool IsRunning { get; set; }
public EventWaitHandle LuaWait { get; private set; }
public bool FrameAdvanceRequested { get; private set; }
2017-05-19 14:47:18 +00:00
public LuaFunctionList RegisteredFunctions => EventsLibrary.RegisteredFunctions;
2013-10-31 16:45:08 +00:00
public void WindowClosed(IntPtr handle)
{
FormsLibrary.WindowClosed(handle);
2013-10-31 16:45:08 +00:00
}
public void CallSaveStateEvent(string name)
{
EventsLibrary.CallSaveStateEvent(name);
}
public void CallLoadStateEvent(string name)
{
EventsLibrary.CallLoadStateEvent(name);
}
public void CallFrameBeforeEvent()
{
EventsLibrary.CallFrameBeforeEvent();
}
public void CallFrameAfterEvent()
{
EventsLibrary.CallFrameAfterEvent();
}
public void CallExitEvent(Lua thread)
{
EventsLibrary.CallExitEvent(thread);
}
public void Close()
{
FormsLibrary.DestroyAll();
_lua.Close();
_lua = new Lua();
GuiLibrary.Dispose();
}
2013-11-25 00:55:56 +00:00
public Lua SpawnCoroutine(string file)
{
var lua = _lua.NewThread();
var content = File.ReadAllText(file);
var main = lua.LoadString(content, "main");
lua.Push(main); // push main function on to stack for subsequent resuming
//if (NLua.Lua.WhichLua == "NLua")
{
_lua.GetTable("keepalives")[lua] = 1;
//this not being run is the origin of a memory leak if you restart scripts too many times
_lua.Pop();
}
return lua;
}
2015-10-15 23:26:07 +00:00
public void ExecuteString(string command)
{
_currThread = _lua.NewThread();
_currThread.DoString(command);
//if (NLua.Lua.WhichLua == "NLua")
_lua.Pop();
2015-10-15 23:26:07 +00:00
}
public ResumeResult ResumeScript(Lua script)
{
2013-11-25 00:55:56 +00:00
_currThread = script;
try
{
LuaLibraryBase.SetCurrentThread(_currThread);
var execResult = script.Resume(0);
_lua.RunScheduledDisposes();
2017-05-19 14:47:18 +00:00
// not sure how this is going to work out, so do this too
script.RunScheduledDisposes();
_currThread = null;
var result = new ResumeResult();
if (execResult == 0)
{
// terminated
result.Terminated = true;
}
else
{
// yielded
result.WaitForFrame = FrameAdvanceRequested;
}
FrameAdvanceRequested = false;
return result;
}
finally
{
LuaLibraryBase.ClearCurrentThread();
}
}
public static void Print(params object[] outputs)
{
ConsoleLuaLibrary.Log(outputs);
}
private void Frameadvance()
{
FrameAdvanceRequested = true;
2013-11-25 00:55:56 +00:00
_currThread.Yield(0);
}
private void EmuYield()
{
2013-11-25 00:55:56 +00:00
_currThread.Yield(0);
}
public class ResumeResult
{
public bool WaitForFrame { get; set; }
public bool Terminated { get; set; }
}
}
}