Registered Lua Functions dialog - update when a lua script unregisters a file, make dupating more robust to catch other possible scenarios where it failed to update

This commit is contained in:
adelikat 2020-07-12 13:19:48 -05:00
parent c4bcb2451a
commit 1e3324cfab
2 changed files with 26 additions and 11 deletions

View File

@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@ -9,11 +10,17 @@ namespace BizHawk.Client.Common
public class LuaFunctionList : IEnumerable<NamedLuaFunction>
{
private readonly List<NamedLuaFunction> _functions = new List<NamedLuaFunction>();
public Action ChangedCallback { get; set; }
public NamedLuaFunction this[string guid] =>
_functions.FirstOrDefault(nlf => nlf.Guid.ToString() == guid);
public void Add(NamedLuaFunction nlf) => _functions.Add(nlf);
public void Add(NamedLuaFunction nlf)
{
_functions.Add(nlf);
Changed();
}
public bool Remove(NamedLuaFunction function, IEmulator emulator)
{
@ -27,7 +34,13 @@ namespace BizHawk.Client.Common
emulator.AsDebuggable().MemoryCallbacks.Remove(function.MemCallback);
}
return _functions.Remove(function);
var result = _functions.Remove(function);
if (result)
{
Changed();
}
return result;
}
public void RemoveForFile(LuaFile file, IEmulator emulator)
@ -40,6 +53,11 @@ namespace BizHawk.Client.Common
{
Remove(function, emulator);
}
if (functionsToRemove.Count != 0)
{
Changed();
}
}
public void Clear(IEmulator emulator)
@ -56,10 +74,13 @@ namespace BizHawk.Client.Common
}
_functions.Clear();
Changed();
}
public IEnumerator<NamedLuaFunction> GetEnumerator() => _functions.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _functions.GetEnumerator();
private void Changed() => ChangedCallback?.Invoke();
}
public static class LuaFunctionListExtensions

View File

@ -195,6 +195,7 @@ namespace BizHawk.Client.EmuHawk
SetColumns();
splitContainer1.SetDistanceOrDefault(Settings.SplitDistance, _defaultSplitDistance);
LuaImp.RegisteredFunctions.ChangedCallback = UpdateRegisteredFunctionsDialog;
}
private void BranchesMarkersSplit_SplitterMoved(object sender, SplitterEventArgs e)
@ -225,10 +226,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var file in runningScripts)
{
LuaImp.CallExitEvent(file);
LuaImp.RegisteredFunctions.RemoveForFile(file, Emulator);
UpdateRegisteredFunctionsDialog();
file.Stop();
}
}
@ -1009,8 +1007,7 @@ namespace BizHawk.Client.EmuHawk
LuaImp.RegisteredFunctions.RemoveForFile(item, Emulator);
LuaImp.ScriptList.Remove(item);
}
UpdateRegisteredFunctionsDialog();
UpdateDialog();
}
}
@ -1558,14 +1555,11 @@ namespace BizHawk.Client.EmuHawk
{
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, Emulator);
UpdateRegisteredFunctionsDialog();
LuaImp.CallExitEvent(file);
file.Stop();
ReDraw();