Refactor a lot of lua code to use a LuaFile object instead of a Lua object, and unify some luafile manipulation logic into methods
This commit is contained in:
parent
679f691b46
commit
d757b8e64f
|
@ -33,9 +33,12 @@ namespace BizHawk.Client.Common
|
|||
|
||||
#region Events Library Helpers
|
||||
|
||||
public void CallExitEvent(Lua thread)
|
||||
public void CallExitEvent(LuaFile luaFile)
|
||||
{
|
||||
var exitCallbacks = RegisteredFunctions.Where(l => l.Lua == thread && l.Event == "OnExit");
|
||||
var exitCallbacks = RegisteredFunctions
|
||||
.ForFile(luaFile)
|
||||
.ForEvent("OnExit");
|
||||
|
||||
foreach (var exitCallback in exitCallbacks)
|
||||
{
|
||||
exitCallback.Call();
|
||||
|
@ -125,7 +128,7 @@ namespace BizHawk.Client.Common
|
|||
[LuaMethod("onframeend", "Calls the given lua function at the end of each frame, after all emulation and drawing has completed. Note: this is the default behavior of lua scripts")]
|
||||
public string OnFrameEnd(LuaFunction luaf, string name = null)
|
||||
{
|
||||
var nlf = new NamedLuaFunction(luaf, "OnFrameEnd", LogOutputCallback, CurrentThread, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnFrameEnd", LogOutputCallback, CurrentFile, name);
|
||||
RegisteredFunctions.Add(nlf);
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
@ -134,7 +137,7 @@ namespace BizHawk.Client.Common
|
|||
[LuaMethod("onframestart", "Calls the given lua function at the beginning of each frame before any emulation and drawing occurs")]
|
||||
public string OnFrameStart(LuaFunction luaf, string name = null)
|
||||
{
|
||||
var nlf = new NamedLuaFunction(luaf, "OnFrameStart", LogOutputCallback, CurrentThread, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnFrameStart", LogOutputCallback, CurrentFile, name);
|
||||
RegisteredFunctions.Add(nlf);
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
@ -143,7 +146,7 @@ namespace BizHawk.Client.Common
|
|||
[LuaMethod("oninputpoll", "Calls the given lua function after each time the emulator core polls for input")]
|
||||
public string OnInputPoll(LuaFunction luaf, string name = null)
|
||||
{
|
||||
var nlf = new NamedLuaFunction(luaf, "OnInputPoll", LogOutputCallback, CurrentThread, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnInputPoll", LogOutputCallback, CurrentFile, name);
|
||||
RegisteredFunctions.Add(nlf);
|
||||
|
||||
if (InputPollableCore != null)
|
||||
|
@ -173,7 +176,7 @@ namespace BizHawk.Client.Common
|
|||
[LuaMethod("onloadstate", "Fires after a state is loaded. Receives a lua function name, and registers it to the event immediately following a successful savestate event")]
|
||||
public string OnLoadState(LuaFunction luaf, string name = null)
|
||||
{
|
||||
var nlf = new NamedLuaFunction(luaf, "OnSavestateLoad", LogOutputCallback, CurrentThread, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnSavestateLoad", LogOutputCallback, CurrentFile, name);
|
||||
RegisteredFunctions.Add(nlf);
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
@ -187,7 +190,7 @@ namespace BizHawk.Client.Common
|
|||
if (DebuggableCore != null && DebuggableCore.MemoryCallbacksAvailable() &&
|
||||
DebuggableCore.MemoryCallbacks.ExecuteCallbacksAvailable)
|
||||
{
|
||||
var nlf = new NamedLuaFunction(luaf, "OnMemoryExecute", LogOutputCallback, CurrentThread, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnMemoryExecute", LogOutputCallback, CurrentFile, name);
|
||||
RegisteredFunctions.Add(nlf);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(domain))
|
||||
|
@ -221,7 +224,7 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
if (DebuggableCore != null && DebuggableCore.MemoryCallbacksAvailable())
|
||||
{
|
||||
var nlf = new NamedLuaFunction(luaf, "OnMemoryRead", LogOutputCallback, CurrentThread, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnMemoryRead", LogOutputCallback, CurrentFile, name);
|
||||
RegisteredFunctions.Add(nlf);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(domain))
|
||||
|
@ -255,7 +258,7 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
if (DebuggableCore != null && DebuggableCore.MemoryCallbacksAvailable())
|
||||
{
|
||||
var nlf = new NamedLuaFunction(luaf, "OnMemoryWrite", LogOutputCallback, CurrentThread, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnMemoryWrite", LogOutputCallback, CurrentFile, name);
|
||||
RegisteredFunctions.Add(nlf);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(domain))
|
||||
|
@ -285,7 +288,7 @@ namespace BizHawk.Client.Common
|
|||
[LuaMethod("onsavestate", "Fires after a state is saved")]
|
||||
public string OnSaveState(LuaFunction luaf, string name = null)
|
||||
{
|
||||
var nlf = new NamedLuaFunction(luaf, "OnSavestateSave", LogOutputCallback, CurrentThread, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnSavestateSave", LogOutputCallback, CurrentFile, name);
|
||||
RegisteredFunctions.Add(nlf);
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
@ -294,7 +297,7 @@ namespace BizHawk.Client.Common
|
|||
[LuaMethod("onexit", "Fires after the calling script has stopped")]
|
||||
public string OnExit(LuaFunction luaf, string name = null)
|
||||
{
|
||||
var nlf = new NamedLuaFunction(luaf, "OnExit", LogOutputCallback, CurrentThread, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnExit", LogOutputCallback, CurrentFile, name);
|
||||
RegisteredFunctions.Add(nlf);
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
|
|
@ -7,13 +7,8 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
public class LuaFunctionList : List<NamedLuaFunction>
|
||||
{
|
||||
public NamedLuaFunction this[string guid]
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.FirstOrDefault(nlf => nlf.Guid.ToString() == guid);
|
||||
}
|
||||
}
|
||||
public NamedLuaFunction this[string guid] =>
|
||||
this.FirstOrDefault(nlf => nlf.Guid.ToString() == guid);
|
||||
|
||||
public new bool Remove(NamedLuaFunction function)
|
||||
{
|
||||
|
@ -30,6 +25,18 @@ namespace BizHawk.Client.Common
|
|||
return base.Remove(function);
|
||||
}
|
||||
|
||||
public void RemoveForFile(LuaFile file)
|
||||
{
|
||||
var functionsToRemove = this
|
||||
.ForFile(file)
|
||||
.ToList();
|
||||
|
||||
foreach (var function in functionsToRemove)
|
||||
{
|
||||
Remove(function);
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearAll()
|
||||
{
|
||||
if (Global.Emulator.InputCallbacksAvailable())
|
||||
|
@ -46,4 +53,18 @@ namespace BizHawk.Client.Common
|
|||
Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static class LuaFunctionListExtensions
|
||||
{
|
||||
public static IEnumerable<NamedLuaFunction> ForFile(this IEnumerable<NamedLuaFunction> list, LuaFile luaFile)
|
||||
{
|
||||
return list
|
||||
.Where(l => l.LuaFile.Thread == luaFile.Thread);
|
||||
}
|
||||
|
||||
public static IEnumerable<NamedLuaFunction> ForEvent(this IEnumerable<NamedLuaFunction> list, string eventName)
|
||||
{
|
||||
return list.Where(l => l.Event == eventName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ namespace BizHawk.Client.Common
|
|||
LogOutputCallback = logOutputCallback;
|
||||
}
|
||||
|
||||
protected static Lua CurrentThread { get; private set; }
|
||||
protected static LuaFile CurrentFile { get; private set; }
|
||||
|
||||
private static Thread CurrentHostThread;
|
||||
private static readonly object ThreadMutex = new object();
|
||||
|
@ -35,11 +35,11 @@ namespace BizHawk.Client.Common
|
|||
lock (ThreadMutex)
|
||||
{
|
||||
CurrentHostThread = null;
|
||||
CurrentThread = null;
|
||||
CurrentFile = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetCurrentThread(Lua luaThread)
|
||||
public static void SetCurrentThread(LuaFile luaFile)
|
||||
{
|
||||
lock (ThreadMutex)
|
||||
{
|
||||
|
@ -49,7 +49,7 @@ namespace BizHawk.Client.Common
|
|||
}
|
||||
|
||||
CurrentHostThread = Thread.CurrentThread;
|
||||
CurrentThread = luaThread;
|
||||
CurrentFile = luaFile;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,12 +9,12 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
private readonly LuaFunction _function;
|
||||
|
||||
public NamedLuaFunction(LuaFunction function, string theEvent, Action<string> logCallback, Lua lua, string name = null)
|
||||
public NamedLuaFunction(LuaFunction function, string theEvent, Action<string> logCallback, LuaFile luaFile, string name = null)
|
||||
{
|
||||
_function = function;
|
||||
Name = name ?? "Anonymous";
|
||||
Event = theEvent;
|
||||
Lua = lua;
|
||||
LuaFile = luaFile;
|
||||
Guid = Guid.NewGuid();
|
||||
|
||||
Callback = delegate
|
||||
|
@ -39,7 +39,7 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public string Name { get; }
|
||||
|
||||
public Lua Lua { get; }
|
||||
public LuaFile LuaFile { get; }
|
||||
|
||||
public string Event { get; }
|
||||
|
||||
|
@ -49,7 +49,7 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public void Call(string name = null)
|
||||
{
|
||||
LuaSandbox.Sandbox(Lua, () =>
|
||||
LuaSandbox.Sandbox(LuaFile.Thread, () =>
|
||||
{
|
||||
_function.Call(name);
|
||||
});
|
||||
|
|
|
@ -302,9 +302,9 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
if (control.Handle == ptr)
|
||||
{
|
||||
if (control is LuaCheckbox)
|
||||
if (control is LuaCheckbox checkbox)
|
||||
{
|
||||
return (control as LuaCheckbox).Checked;
|
||||
return checkbox.Checked;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -360,7 +360,7 @@ namespace BizHawk.Client.EmuHawk
|
|||
"newform", "creates a new default dialog, if both width and height are specified it will create a dialog of the specified size. If title is specified it will be the caption of the dialog, else the dialog caption will be 'Lua Dialog'. The function will return an int representing the handle of the dialog created.")]
|
||||
public int NewForm(int? width = null, int? height = null, string title = null, LuaFunction onClose = null)
|
||||
{
|
||||
var form = new LuaWinform(CurrentThread);
|
||||
var form = new LuaWinform(CurrentFile);
|
||||
_luaForms.Add(form);
|
||||
if (width.HasValue && height.HasValue)
|
||||
{
|
||||
|
|
|
@ -162,14 +162,9 @@ namespace BizHawk.Client.EmuHawk
|
|||
EventsLibrary.CallFrameAfterEvent();
|
||||
}
|
||||
|
||||
public void CallExitEvent(Lua thread)
|
||||
{
|
||||
EventsLibrary.CallExitEvent(thread);
|
||||
}
|
||||
|
||||
public override void CallExitEvent(LuaFile lf)
|
||||
{
|
||||
CallExitEvent(lf.Thread);
|
||||
EventsLibrary.CallExitEvent(lf);
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
|
@ -214,7 +209,7 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
try
|
||||
{
|
||||
LuaLibraryBase.SetCurrentThread(_currThread);
|
||||
LuaLibraryBase.SetCurrentThread(lf);
|
||||
|
||||
var execResult = _currThread.Resume(0);
|
||||
|
||||
|
|
|
@ -183,8 +183,7 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
LuaImp.CallExitEvent(file);
|
||||
|
||||
LuaImp.RegisteredFunctions.RemoveAll(lf => lf.Lua == file.Thread);
|
||||
|
||||
LuaImp.RegisteredFunctions.RemoveForFile(file);
|
||||
UpdateRegisteredFunctionsDialog();
|
||||
|
||||
file.Stop();
|
||||
|
@ -904,12 +903,10 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
foreach (var item in items)
|
||||
{
|
||||
var temp = item;
|
||||
LuaImp.RegisteredFunctions.RemoveAll(x => x.Lua == temp.Thread);
|
||||
|
||||
LuaImp.RegisteredFunctions.RemoveForFile(item);
|
||||
LuaImp.ScriptList.Remove(item);
|
||||
}
|
||||
|
||||
|
||||
UpdateRegisteredFunctionsDialog();
|
||||
UpdateDialog();
|
||||
}
|
||||
|
@ -1475,8 +1472,7 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
foreach (var selectedItem in SelectedItems)
|
||||
{
|
||||
var temp = selectedItem;
|
||||
LuaImp.RegisteredFunctions.RemoveAll(lf => lf.Lua == temp.Thread);
|
||||
LuaImp.RegisteredFunctions.RemoveForFile(selectedItem);
|
||||
UpdateRegisteredFunctionsDialog();
|
||||
}
|
||||
|
||||
|
|
|
@ -13,12 +13,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
public List<LuaEvent> ControlEvents { get; } = new List<LuaEvent>();
|
||||
|
||||
private readonly string _currentDirectory = Environment.CurrentDirectory;
|
||||
private readonly Lua _ownerThread;
|
||||
private readonly LuaFile _ownerFile;
|
||||
|
||||
public LuaWinform(Lua ownerThread)
|
||||
public LuaWinform(LuaFile ownerFile)
|
||||
{
|
||||
InitializeComponent();
|
||||
_ownerThread = ownerThread;
|
||||
_ownerFile = ownerFile;
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
Closing += (o, e) => CloseThis();
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
public void DoLuaEvent(IntPtr handle)
|
||||
{
|
||||
LuaSandbox.Sandbox(_ownerThread, () =>
|
||||
LuaSandbox.Sandbox(_ownerFile.Thread, () =>
|
||||
{
|
||||
Environment.CurrentDirectory = _currentDirectory;
|
||||
foreach (LuaEvent luaEvent in ControlEvents)
|
||||
|
@ -49,9 +49,9 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
public class LuaEvent
|
||||
{
|
||||
public LuaEvent(IntPtr handle, LuaFunction lfunction)
|
||||
public LuaEvent(IntPtr handle, LuaFunction luaFunction)
|
||||
{
|
||||
Event = lfunction;
|
||||
Event = luaFunction;
|
||||
Control = handle;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue