using System.Collections; using System.Collections.Generic; using System.Linq; using BizHawk.Emulation.Common; namespace BizHawk.Client.Common { public class LuaFunctionList : IEnumerable { private readonly List _functions = new List(); public NamedLuaFunction this[string guid] => _functions.FirstOrDefault(nlf => nlf.Guid.ToString() == guid); public void Add(NamedLuaFunction nlf) => _functions.Add(nlf); public bool Remove(NamedLuaFunction function, IEmulator emulator) { if (emulator.InputCallbacksAvailable()) { emulator.AsInputPollable().InputCallbacks.Remove(function.Callback); } if (emulator.MemoryCallbacksAvailable()) { emulator.AsDebuggable().MemoryCallbacks.Remove(function.MemCallback); } return _functions.Remove(function); } public void RemoveForFile(LuaFile file, IEmulator emulator) { var functionsToRemove = _functions .ForFile(file) .ToList(); foreach (var function in functionsToRemove) { Remove(function, emulator); } } public void Clear(IEmulator emulator) { if (emulator.InputCallbacksAvailable()) { emulator.AsInputPollable().InputCallbacks.RemoveAll(_functions.Select(w => w.Callback)); } if (emulator.MemoryCallbacksAvailable()) { var memoryCallbacks = emulator.AsDebuggable().MemoryCallbacks; memoryCallbacks.RemoveAll(_functions.Select(w => w.MemCallback)); } _functions.Clear(); } public IEnumerator GetEnumerator() => _functions.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => _functions.GetEnumerator(); } public static class LuaFunctionListExtensions { public static IEnumerable ForFile(this IEnumerable list, LuaFile luaFile) { return list .Where(l => l.LuaFile.Path == luaFile.Path || l.LuaFile.Thread == luaFile.Thread); } public static IEnumerable ForEvent(this IEnumerable list, string eventName) { return list.Where(l => l.Event == eventName); } } }