LuaFunctionList - remove global usage, make an IEnumerable instead of List since add/remove needs to be carefully controlled

This commit is contained in:
adelikat 2020-04-12 13:29:55 -05:00
parent 622733d31a
commit 42c6168c7e
4 changed files with 31 additions and 23 deletions

View File

@ -342,7 +342,7 @@ namespace BizHawk.Client.Common
{
foreach (var nlf in RegisteredFunctions.Where(nlf => nlf.Guid.ToString() == guid))
{
RegisteredFunctions.Remove(nlf);
RegisteredFunctions.Remove(nlf, Emulator);
return true;
}
@ -355,7 +355,7 @@ namespace BizHawk.Client.Common
{
foreach (var nlf in RegisteredFunctions.Where(nlf => nlf.Name == name))
{
RegisteredFunctions.Remove(nlf);
RegisteredFunctions.Remove(nlf, Emulator);
return true;
}

View File

@ -1,57 +1,65 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
public class LuaFunctionList : List<NamedLuaFunction>
public class LuaFunctionList : IEnumerable<NamedLuaFunction>
{
private readonly List<NamedLuaFunction> _functions = new List<NamedLuaFunction>();
public NamedLuaFunction this[string guid] =>
this.FirstOrDefault(nlf => nlf.Guid.ToString() == guid);
_functions.FirstOrDefault(nlf => nlf.Guid.ToString() == guid);
public new bool Remove(NamedLuaFunction function)
public void Add(NamedLuaFunction nlf) => _functions.Add(nlf);
public bool Remove(NamedLuaFunction function, IEmulator emulator)
{
if (Global.Emulator.InputCallbacksAvailable())
if (emulator.InputCallbacksAvailable())
{
Global.Emulator.AsInputPollable().InputCallbacks.Remove(function.Callback);
emulator.AsInputPollable().InputCallbacks.Remove(function.Callback);
}
if (Global.Emulator.MemoryCallbacksAvailable())
if (emulator.MemoryCallbacksAvailable())
{
Global.Emulator.AsDebuggable().MemoryCallbacks.Remove(function.MemCallback);
emulator.AsDebuggable().MemoryCallbacks.Remove(function.MemCallback);
}
return base.Remove(function);
return _functions.Remove(function);
}
public void RemoveForFile(LuaFile file)
public void RemoveForFile(LuaFile file, IEmulator emulator)
{
var functionsToRemove = this
var functionsToRemove = _functions
.ForFile(file)
.ToList();
foreach (var function in functionsToRemove)
{
Remove(function);
Remove(function, emulator);
}
}
public new void Clear()
public void Clear()
{
if (Global.Emulator.InputCallbacksAvailable())
{
Global.Emulator.AsInputPollable().InputCallbacks.RemoveAll(this.Select(w => w.Callback));
Global.Emulator.AsInputPollable().InputCallbacks.RemoveAll(_functions.Select(w => w.Callback));
}
if (Global.Emulator.MemoryCallbacksAvailable())
{
var memoryCallbacks = Global.Emulator.AsDebuggable().MemoryCallbacks;
memoryCallbacks.RemoveAll(this.Select(w => w.MemCallback));
memoryCallbacks.RemoveAll(_functions.Select(w => w.MemCallback));
}
base.Clear();
_functions.Clear();
}
public IEnumerator<NamedLuaFunction> GetEnumerator() => _functions.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _functions.GetEnumerator();
}
public static class LuaFunctionListExtensions

View File

@ -211,7 +211,7 @@ namespace BizHawk.Client.EmuHawk
{
LuaImp.CallExitEvent(file);
LuaImp.RegisteredFunctions.RemoveForFile(file);
LuaImp.RegisteredFunctions.RemoveForFile(file, Emulator);
UpdateRegisteredFunctionsDialog();
file.Stop();
@ -941,7 +941,7 @@ namespace BizHawk.Client.EmuHawk
{
foreach (var item in items)
{
LuaImp.RegisteredFunctions.RemoveForFile(item);
LuaImp.RegisteredFunctions.RemoveForFile(item, Emulator);
LuaImp.ScriptList.Remove(item);
}
@ -1505,14 +1505,14 @@ namespace BizHawk.Client.EmuHawk
if (file.Enabled && file.Thread == null)
{
LuaImp.RegisteredFunctions.RemoveForFile(file); // First remove any existing registered functions for this file
LuaImp.RegisteredFunctions.RemoveForFile(file, Emulator); // First remove any existing registered functions for this file
EnableLuaFile(file);
UpdateRegisteredFunctionsDialog();
}
else if (!file.Enabled && file.Thread != null)
{
LuaImp.CallExitEvent(file);
LuaImp.RegisteredFunctions.RemoveForFile(file);
LuaImp.RegisteredFunctions.RemoveForFile(file, Emulator);
UpdateRegisteredFunctionsDialog();
LuaImp.CallExitEvent(file);

View File

@ -88,7 +88,7 @@ namespace BizHawk.Client.EmuHawk
{
var guid = FunctionView.Items[index].SubItems[2].Text;
var nlf = _registeredFunctions[guid];
_registeredFunctions.Remove(nlf);
_registeredFunctions.Remove(nlf, Global.Emulator); // TODO: don't use Global
}
PopulateListView();