Lua Console - when removing a script, remove its registered function. Currently however, this behavior will only happen if the script is currently running. If you disable it and then remove it, the functions remain.
This commit is contained in:
parent
d33d515ae9
commit
788aea970e
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LuaInterface;
|
||||
|
||||
|
@ -8,7 +7,6 @@ namespace BizHawk.Client.Common
|
|||
public class EventLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public EventLuaLibrary(Action<string> logOutputCallback)
|
||||
: base()
|
||||
{
|
||||
LogOutputCallback = logOutputCallback;
|
||||
}
|
||||
|
@ -29,12 +27,13 @@ namespace BizHawk.Client.Common
|
|||
"onmemorywrite",
|
||||
"onsavestate",
|
||||
"unregisterbyid",
|
||||
"unregisterbyname",
|
||||
"unregisterbyname"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public Action<string> LogOutputCallback = null;
|
||||
public Lua CurrentThread;
|
||||
|
||||
#region Events Library Helpers
|
||||
|
||||
|
@ -44,12 +43,12 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public void CallSaveStateEvent(string name)
|
||||
{
|
||||
List<NamedLuaFunction> lfs = _luaFunctions.Where(x => x.Event == "OnSavestateSave").ToList();
|
||||
var lfs = _luaFunctions.Where(x => x.Event == "OnSavestateSave").ToList();
|
||||
if (lfs.Any())
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (NamedLuaFunction lf in lfs)
|
||||
foreach (var lf in lfs)
|
||||
{
|
||||
lf.Call(name);
|
||||
}
|
||||
|
@ -66,12 +65,12 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public void CallLoadStateEvent(string name)
|
||||
{
|
||||
List<NamedLuaFunction> lfs = _luaFunctions.Where(x => x.Event == "OnSavestateLoad").ToList();
|
||||
var lfs = _luaFunctions.Where(x => x.Event == "OnSavestateLoad").ToList();
|
||||
if (lfs.Any())
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (NamedLuaFunction lf in lfs)
|
||||
foreach (var lf in lfs)
|
||||
{
|
||||
lf.Call(name);
|
||||
}
|
||||
|
@ -88,12 +87,12 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public void CallFrameBeforeEvent()
|
||||
{
|
||||
List<NamedLuaFunction> lfs = _luaFunctions.Where(x => x.Event == "OnFrameStart").ToList();
|
||||
var lfs = _luaFunctions.Where(x => x.Event == "OnFrameStart").ToList();
|
||||
if (lfs.Any())
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (NamedLuaFunction lf in lfs)
|
||||
foreach (var lf in lfs)
|
||||
{
|
||||
lf.Call();
|
||||
}
|
||||
|
@ -110,12 +109,12 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public void CallFrameAfterEvent()
|
||||
{
|
||||
List<NamedLuaFunction> lfs = _luaFunctions.Where(x => x.Event == "OnFrameEnd").ToList();
|
||||
var lfs = _luaFunctions.Where(x => x.Event == "OnFrameEnd").ToList();
|
||||
if (lfs.Any())
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (NamedLuaFunction lf in lfs)
|
||||
foreach (var lf in lfs)
|
||||
{
|
||||
lf.Call();
|
||||
}
|
||||
|
@ -134,72 +133,69 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public string event_onframeend(LuaFunction luaf, string name = null)
|
||||
{
|
||||
NamedLuaFunction nlf = new NamedLuaFunction(luaf, "OnFrameEnd", LogOutputCallback, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnFrameEnd", LogOutputCallback, CurrentThread, name);
|
||||
_luaFunctions.Add(nlf);
|
||||
return nlf.GUID.ToString();
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
||||
public string event_onframestart(LuaFunction luaf, string name = null)
|
||||
{
|
||||
NamedLuaFunction nlf = new NamedLuaFunction(luaf, "OnFrameStart", LogOutputCallback, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnFrameStart", LogOutputCallback, CurrentThread, name);
|
||||
_luaFunctions.Add(nlf);
|
||||
return nlf.GUID.ToString();
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
||||
public void event_oninputpoll(LuaFunction luaf, string name = null)
|
||||
{
|
||||
NamedLuaFunction nlf = new NamedLuaFunction(luaf, "OnInputPoll", LogOutputCallback, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnInputPoll", LogOutputCallback, CurrentThread, name);
|
||||
_luaFunctions.Add(nlf);
|
||||
Global.Emulator.CoreComm.InputCallback.Add(nlf.Callback);
|
||||
}
|
||||
|
||||
public string event_onloadstate(LuaFunction luaf, string name = null)
|
||||
{
|
||||
NamedLuaFunction nlf = new NamedLuaFunction(luaf, "OnSavestateLoad", LogOutputCallback, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnSavestateLoad", LogOutputCallback, CurrentThread, name);
|
||||
_luaFunctions.Add(nlf);
|
||||
return nlf.GUID.ToString();
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
||||
public string event_onmemoryexecute(LuaFunction luaf, object address, string name = null)
|
||||
{
|
||||
NamedLuaFunction nlf = new NamedLuaFunction(luaf, "OnMemoryExecute", LogOutputCallback, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnMemoryExecute", LogOutputCallback, CurrentThread, name);
|
||||
_luaFunctions.Add(nlf);
|
||||
Global.CoreComm.MemoryCallbackSystem.AddExecute(nlf.Callback, LuaUInt(address));
|
||||
return nlf.GUID.ToString();
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
||||
public string event_onmemoryread(LuaFunction luaf, object address = null, string name = null)
|
||||
{
|
||||
NamedLuaFunction nlf = new NamedLuaFunction(luaf, "OnMemoryRead", LogOutputCallback, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnMemoryRead", LogOutputCallback, CurrentThread, name);
|
||||
_luaFunctions.Add(nlf);
|
||||
Global.CoreComm.MemoryCallbackSystem.AddRead(nlf.Callback, (address != null ? LuaUInt(address) : (uint?)null));
|
||||
return nlf.GUID.ToString();
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
||||
public string event_onmemorywrite(LuaFunction luaf, object address = null, string name = null)
|
||||
{
|
||||
NamedLuaFunction nlf = new NamedLuaFunction(luaf, "OnMemoryWrite", LogOutputCallback, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnMemoryWrite", LogOutputCallback, CurrentThread, name);
|
||||
_luaFunctions.Add(nlf);
|
||||
Global.CoreComm.MemoryCallbackSystem.AddWrite(nlf.Callback, (address != null ? LuaUInt(address) : (uint?)null));
|
||||
return nlf.GUID.ToString();
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
||||
public string event_onsavestate(LuaFunction luaf, string name = null)
|
||||
{
|
||||
NamedLuaFunction nlf = new NamedLuaFunction(luaf, "OnSavestateSave", LogOutputCallback, name);
|
||||
var nlf = new NamedLuaFunction(luaf, "OnSavestateSave", LogOutputCallback, CurrentThread, name);
|
||||
_luaFunctions.Add(nlf);
|
||||
return nlf.GUID.ToString();
|
||||
return nlf.Guid.ToString();
|
||||
}
|
||||
|
||||
public bool event_unregisterbyid(object guid)
|
||||
{
|
||||
foreach (NamedLuaFunction nlf in _luaFunctions)
|
||||
foreach (var nlf in _luaFunctions.Where(nlf => nlf.Guid.ToString() == guid.ToString()))
|
||||
{
|
||||
if (nlf.GUID.ToString() == guid.ToString())
|
||||
{
|
||||
_luaFunctions.RemoveFunction(nlf);
|
||||
return true;
|
||||
}
|
||||
_luaFunctions.RemoveFunction(nlf);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -207,13 +203,10 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public bool event_unregisterbyname(object name)
|
||||
{
|
||||
foreach (NamedLuaFunction nlf in _luaFunctions)
|
||||
foreach (var nlf in _luaFunctions.Where(nlf => nlf.Name == name.ToString()))
|
||||
{
|
||||
if (nlf.Name == name.ToString())
|
||||
{
|
||||
_luaFunctions.RemoveFunction(nlf);
|
||||
return true;
|
||||
}
|
||||
_luaFunctions.RemoveFunction(nlf);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
|
@ -9,7 +9,7 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
get
|
||||
{
|
||||
return this.FirstOrDefault(x => x.GUID.ToString() == guid) ?? null;
|
||||
return this.FirstOrDefault(x => x.Guid.ToString() == guid) ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using LuaInterface;
|
||||
|
||||
namespace BizHawk.Client.Common
|
||||
|
@ -10,14 +9,15 @@ namespace BizHawk.Client.Common
|
|||
private readonly string _name;
|
||||
private readonly string _event;
|
||||
private readonly Action _action;
|
||||
public NamedLuaFunction(LuaFunction function, string theevent, Action<string> logCallback, string name = null)
|
||||
public NamedLuaFunction(LuaFunction function, string theevent, Action<string> logCallback, Lua lua, string name = null)
|
||||
{
|
||||
_function = function;
|
||||
_name = name ?? "Anonymous";
|
||||
_event = theevent;
|
||||
GUID = Guid.NewGuid();
|
||||
Lua = lua;
|
||||
Guid = Guid.NewGuid();
|
||||
|
||||
_action = new Action(delegate
|
||||
_action = delegate
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -33,10 +33,10 @@ namespace BizHawk.Client.Common
|
|||
);
|
||||
}
|
||||
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
public Guid GUID { get; private set; }
|
||||
public Guid Guid { get; private set; }
|
||||
|
||||
public void Call(string name = null)
|
||||
{
|
||||
|
@ -48,6 +48,8 @@ namespace BizHawk.Client.Common
|
|||
get { return _name; }
|
||||
}
|
||||
|
||||
public Lua Lua { get; private set; }
|
||||
|
||||
public string Event
|
||||
{
|
||||
get { return _event; }
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using LuaInterface;
|
||||
|
@ -9,7 +9,6 @@ namespace BizHawk.Client.EmuHawk
|
|||
public class InputLuaLibrary : LuaLibraryBase
|
||||
{
|
||||
public InputLuaLibrary(Lua lua)
|
||||
: base()
|
||||
{
|
||||
_lua = lua;
|
||||
}
|
||||
|
@ -27,21 +26,22 @@ namespace BizHawk.Client.EmuHawk
|
|||
}
|
||||
}
|
||||
|
||||
private Lua _lua;
|
||||
private readonly Lua _lua;
|
||||
|
||||
public LuaTable input_get()
|
||||
{
|
||||
LuaTable buttons = _lua.NewTable();
|
||||
foreach (var kvp in GlobalWin.ControllerInputCoalescer.BoolButtons())
|
||||
if (kvp.Value)
|
||||
buttons[kvp.Key] = true;
|
||||
var buttons = _lua.NewTable();
|
||||
foreach (var kvp in GlobalWin.ControllerInputCoalescer.BoolButtons().Where(kvp => kvp.Value))
|
||||
{
|
||||
buttons[kvp.Key] = true;
|
||||
}
|
||||
return buttons;
|
||||
}
|
||||
|
||||
public LuaTable input_getmouse()
|
||||
{
|
||||
LuaTable buttons = _lua.NewTable();
|
||||
Point p = GlobalWin.RenderPanel.ScreenToScreen(Control.MousePosition);
|
||||
var buttons = _lua.NewTable();
|
||||
var p = GlobalWin.RenderPanel.ScreenToScreen(Control.MousePosition);
|
||||
buttons["X"] = p.X;
|
||||
buttons["Y"] = p.Y;
|
||||
buttons[MouseButtons.Left.ToString()] = (Control.MouseButtons & MouseButtons.Left) != 0;
|
||||
|
|
|
@ -100,7 +100,7 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
public Lua SpawnCoroutine(string file)
|
||||
{
|
||||
Lua lua = _lua.NewThread();
|
||||
var lua = _lua.NewThread();
|
||||
var main = lua.LoadFile(file);
|
||||
lua.Push(main); //push main function on to stack for subsequent resuming
|
||||
return lua;
|
||||
|
@ -114,8 +114,9 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
public ResumeResult ResumeScript(Lua script)
|
||||
{
|
||||
_eventLibrary.CurrentThread = script;
|
||||
_currThread = script;
|
||||
int execResult = script.Resume(0);
|
||||
var execResult = script.Resume(0);
|
||||
_currThread = null;
|
||||
var result = new ResumeResult();
|
||||
if (execResult == 0)
|
||||
|
|
|
@ -115,13 +115,10 @@ namespace BizHawk.Client.EmuHawk
|
|||
}
|
||||
else
|
||||
{
|
||||
foreach (var file in _luaList)
|
||||
foreach (var file in _luaList.Where(file => path == file.Path && file.Enabled == false && !Global.Config.DisableLuaScriptsOnLoad))
|
||||
{
|
||||
if (path == file.Path && file.Enabled == false && !Global.Config.DisableLuaScriptsOnLoad)
|
||||
{
|
||||
file.Toggle();
|
||||
break;
|
||||
}
|
||||
file.Toggle();
|
||||
break;
|
||||
}
|
||||
|
||||
RunLuaScripts();
|
||||
|
@ -132,24 +129,25 @@ namespace BizHawk.Client.EmuHawk
|
|||
public void UpdateDialog()
|
||||
{
|
||||
LuaListView.ItemCount = _luaList.Count;
|
||||
LuaListView.Refresh();
|
||||
UpdateNumberOfScripts();
|
||||
}
|
||||
|
||||
public void RunLuaScripts()
|
||||
{
|
||||
foreach (var t in _luaList)
|
||||
foreach (var file in _luaList)
|
||||
{
|
||||
if (t.Enabled && t.Thread == null)
|
||||
if (file.Enabled && file.Thread == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
t.Thread = LuaImp.SpawnCoroutine(t.Path);
|
||||
file.Thread = LuaImp.SpawnCoroutine(file.Path);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (e.ToString().Substring(0, 32) == "LuaInterface.LuaScriptException:")
|
||||
{
|
||||
t.Enabled = false;
|
||||
file.Enabled = false;
|
||||
ConsoleLog(e.Message);
|
||||
}
|
||||
else MessageBox.Show(e.ToString());
|
||||
|
@ -157,7 +155,7 @@ namespace BizHawk.Client.EmuHawk
|
|||
}
|
||||
else
|
||||
{
|
||||
t.Stop();
|
||||
file.Stop();
|
||||
_luaList.Changes = true;
|
||||
}
|
||||
}
|
||||
|
@ -361,7 +359,10 @@ namespace BizHawk.Client.EmuHawk
|
|||
Environment.CurrentDirectory = lf.CurrentDirectory;
|
||||
}
|
||||
var result = LuaImp.ResumeScript(lf.Thread);
|
||||
if (result.Terminated) lf.Stop();
|
||||
if (result.Terminated)
|
||||
{
|
||||
lf.Stop();
|
||||
}
|
||||
lf.FrameWaiting = result.WaitForFrame;
|
||||
|
||||
//if the lua thread changed its current directory, capture that here
|
||||
|
@ -526,6 +527,14 @@ namespace BizHawk.Client.EmuHawk
|
|||
);
|
||||
}
|
||||
|
||||
private static void UpdateRegisteredFunctionsDialog()
|
||||
{
|
||||
foreach (var form in Application.OpenForms.OfType<LuaRegisteredFunctionsList>())
|
||||
{
|
||||
form.UpdateValues();
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<int> SelectedIndices
|
||||
{
|
||||
get { return LuaListView.SelectedIndices.Cast<int>(); }
|
||||
|
@ -708,9 +717,17 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
foreach (var item in items)
|
||||
{
|
||||
var temp = item;
|
||||
var functions = LuaImp.RegisteredFunctions.Where(x => x.Lua == temp.Thread).ToList();
|
||||
foreach (var function in functions)
|
||||
{
|
||||
LuaImp.RegisteredFunctions.Remove(function);
|
||||
}
|
||||
|
||||
_luaList.Remove(item);
|
||||
}
|
||||
|
||||
|
||||
UpdateRegisteredFunctionsDialog();
|
||||
UpdateDialog();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using BizHawk.Client.Common;
|
||||
|
||||
namespace BizHawk.Client.EmuHawk
|
||||
{
|
||||
public partial class LuaRegisteredFunctionsList : Form
|
||||
|
@ -16,6 +13,18 @@ namespace BizHawk.Client.EmuHawk
|
|||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void UpdateValues()
|
||||
{
|
||||
if (GlobalWin.Tools.LuaConsole.LuaImp.RegisteredFunctions.Any())
|
||||
{
|
||||
PopulateListView();
|
||||
}
|
||||
else
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void LuaRegisteredFunctionsList_Load(object sender, EventArgs e)
|
||||
{
|
||||
if (StartLocation.X > 0 && StartLocation.Y > 0)
|
||||
|
@ -34,12 +43,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
FunctionView.Items.Clear();
|
||||
|
||||
List<NamedLuaFunction> nlfs = GlobalWin.Tools.LuaConsole.LuaImp.RegisteredFunctions.OrderBy(x => x.Event).ThenBy(x => x.Name).ToList();
|
||||
foreach (NamedLuaFunction nlf in nlfs)
|
||||
var nlfs = GlobalWin.Tools.LuaConsole.LuaImp.RegisteredFunctions.OrderBy(x => x.Event).ThenBy(x => x.Name).ToList();
|
||||
foreach (var nlf in nlfs)
|
||||
{
|
||||
ListViewItem item = new ListViewItem { Text = nlf.Event };
|
||||
var item = new ListViewItem { Text = nlf.Event };
|
||||
item.SubItems.Add(nlf.Name);
|
||||
item.SubItems.Add(nlf.GUID.ToString());
|
||||
item.SubItems.Add(nlf.Guid.ToString());
|
||||
FunctionView.Items.Add(item);
|
||||
}
|
||||
|
||||
|
@ -58,12 +67,12 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
private void CallFunction()
|
||||
{
|
||||
ListView.SelectedIndexCollection indices = FunctionView.SelectedIndices;
|
||||
var indices = FunctionView.SelectedIndices;
|
||||
if (indices.Count > 0)
|
||||
{
|
||||
foreach (int index in indices)
|
||||
{
|
||||
string guid = FunctionView.Items[index].SubItems[2].Text;
|
||||
var guid = FunctionView.Items[index].SubItems[2].Text;
|
||||
GlobalWin.Tools.LuaConsole.LuaImp.RegisteredFunctions[guid].Call();
|
||||
}
|
||||
}
|
||||
|
@ -71,13 +80,13 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
private void RemoveFunctionButton()
|
||||
{
|
||||
ListView.SelectedIndexCollection indices = FunctionView.SelectedIndices;
|
||||
var indices = FunctionView.SelectedIndices;
|
||||
if (indices.Count > 0)
|
||||
{
|
||||
foreach (int index in indices)
|
||||
{
|
||||
string guid = FunctionView.Items[index].SubItems[2].Text;
|
||||
NamedLuaFunction nlf = GlobalWin.Tools.LuaConsole.LuaImp.RegisteredFunctions[guid];
|
||||
var guid = FunctionView.Items[index].SubItems[2].Text;
|
||||
var nlf = GlobalWin.Tools.LuaConsole.LuaImp.RegisteredFunctions[guid];
|
||||
GlobalWin.Tools.LuaConsole.LuaImp.RegisteredFunctions.RemoveFunction(nlf);
|
||||
}
|
||||
PopulateListView();
|
||||
|
@ -102,7 +111,7 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
private void DoButtonsStatus()
|
||||
{
|
||||
ListView.SelectedIndexCollection indexes = FunctionView.SelectedIndices;
|
||||
var indexes = FunctionView.SelectedIndices;
|
||||
CallButton.Enabled = indexes.Count > 0;
|
||||
RemoveButton.Enabled = indexes.Count > 0;
|
||||
RemoveAllBtn.Enabled = GlobalWin.Tools.LuaConsole.LuaImp.RegisteredFunctions.Any();
|
||||
|
|
Loading…
Reference in New Issue