Fix memoisation in `FilesystemFilterSet`

fixes 1232157cc, 0743a1f8e
This commit is contained in:
YoshiRulz 2025-07-31 00:26:29 +10:00
parent 0743a1f8ea
commit 795055076c
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
8 changed files with 51 additions and 59 deletions

View File

@ -5,33 +5,37 @@ using System.Linq;
namespace BizHawk.Client.Common
{
public sealed class FilesystemFilterSet
public sealed class FilesystemFilterSet(
string? combinedEntryDesc,
bool appendAllFilesEntry,
params FilesystemFilter[] filters)
{
private string? _ser = null;
private const bool DEFAULT_USE_ALL_FILES_ENTRY = true;
public bool AppendAllFilesEntry { get; set; } = true;
private readonly Lazy<string> _ser = new(() =>
{
var entries = filters.Select(static filter => filter.ToString()).ToList();
if (combinedEntryDesc is not null) entries.Insert(0, FilesystemFilter.SerializeEntry(
combinedEntryDesc,
filters.SelectMany(static filter => filter.Extensions).Distinct().Order().ToList()));
if (appendAllFilesEntry) entries.Add(FilesystemFilter.AllFilesEntry);
return string.Join("|", entries);
});
public string? CombinedEntryDesc { get; set; } = null;
public IReadOnlyList<FilesystemFilter> Filters
=> filters;
private IReadOnlyCollection<string> CombinedExts
=> Filters.SelectMany(static filter => filter.Extensions).Distinct().Order().ToList();
public FilesystemFilterSet(string combinedEntryDesc, params FilesystemFilter[] filters)
: this(combinedEntryDesc, appendAllFilesEntry: DEFAULT_USE_ALL_FILES_ENTRY, filters) {}
public readonly IReadOnlyList<FilesystemFilter> Filters;
public FilesystemFilterSet(bool appendAllFilesEntry, params FilesystemFilter[] filters)
: this(combinedEntryDesc: null, appendAllFilesEntry: appendAllFilesEntry, filters) {}
public FilesystemFilterSet(params FilesystemFilter[] filters)
=> Filters = filters;
: this(appendAllFilesEntry: DEFAULT_USE_ALL_FILES_ENTRY, filters) {}
public override string ToString()
{
if (_ser is null)
{
var entries = Filters.Select(static filter => filter.ToString()).ToList();
if (CombinedEntryDesc is not null) entries.Insert(0, FilesystemFilter.SerializeEntry(CombinedEntryDesc, CombinedExts));
if (AppendAllFilesEntry) entries.Add(FilesystemFilter.AllFilesEntry);
_ser = string.Join("|", entries);
}
return _ser;
}
=> _ser.Value;
public static readonly FilesystemFilterSet Palettes = new(new FilesystemFilter("Palette Files", new[] { "pal" }));

View File

@ -1124,6 +1124,7 @@ namespace BizHawk.Client.Common
/// <remarks>TODO add and handle <see cref="FilesystemFilter.LuaScripts"/> (you can drag-and-drop scripts and there are already non-rom things in this list, so why not?)</remarks>
public static readonly FilesystemFilterSet RomFilter = new(
combinedEntryDesc: "Everything",
FilesystemFilter.Archives,
new FilesystemFilter("Disc Images", FilesystemFilter.DiscExtensions),
new FilesystemFilter("Rom Bundles", new[] { "xml" }),
@ -1165,10 +1166,7 @@ namespace BizHawk.Client.Common
new FilesystemFilter(/*VSystemID.Raw.WSWAN*/"WonderSwan", RomFileExtensions.WSWAN, addArchiveExts: true),
new FilesystemFilter(/*VSystemID.Raw.ZXSpectrum*/"Sinclair ZX Spectrum", RomFileExtensions.ZXSpectrum.Concat(new[] { "csw", "wav" }).ToList(), addArchiveExts: true),
new FilesystemFilter("Music Files", Array.Empty<string>(), devBuildExtraExts: new[] { "psf", "minipsf", "sid", "nsf", "gbs" }),
FilesystemFilter.EmuHawkSaveStates)
{
CombinedEntryDesc = "Everything",
};
FilesystemFilter.EmuHawkSaveStates);
public static readonly IReadOnlyCollection<string> KnownRomExtensions = RomFilter.Filters
.SelectMany(f => f.Extensions)

View File

@ -22,12 +22,10 @@ namespace BizHawk.Client.Common
=> Importers.Any(kvp => kvp.Value.Extension.EqualsIgnoreCase(extension));
public static readonly FilesystemFilterSet AvailableImporters = new FilesystemFilterSet(
Importers.Values.OrderBy(attr => attr.Emulator)
combinedEntryDesc: "Movie Files",
filters: Importers.Values.OrderBy(static attr => attr.Emulator)
.Select(attr => new FilesystemFilter(attr.Emulator, new[] { attr.Extension.Substring(1) })) // substring removes initial '.'
.ToArray())
{
CombinedEntryDesc = "Movie Files",
};
.ToArray());
// Attempt to import another type of movie file into a movie object.
public static ImportResult ImportFile(

View File

@ -18,15 +18,13 @@ namespace BizHawk.Client.EmuHawk
{
public partial class MainForm
{
private static readonly FilesystemFilterSet MAMERomsFSFilterSet = new(new FilesystemFilter("MAME Arcade ROMs", new[] { "zip" }))
{
AppendAllFilesEntry = false,
};
private static readonly FilesystemFilterSet MAMERomsFSFilterSet = new(
appendAllFilesEntry: false,
new FilesystemFilter("MAME Arcade ROMs", extensions: [ "zip" ]));
private static readonly FilesystemFilterSet ScreenshotsFSFilterSet = new(FilesystemFilter.PNGs)
{
AppendAllFilesEntry = false,
};
private static readonly FilesystemFilterSet ScreenshotsFSFilterSet = new(
appendAllFilesEntry: false,
FilesystemFilter.PNGs);
private void FileSubMenu_DropDownOpened(object sender, EventArgs e)
{

View File

@ -981,15 +981,13 @@ namespace BizHawk.Client.EmuHawk
if (result.IsOk()) AddOnScreenMessage("Palette settings saved");
}
private static readonly FilesystemFilterSet DOSBoxHDDImageFilterSet = new(new FilesystemFilter("DOSBox HDD Images", new[] { "hdd" }))
{
AppendAllFilesEntry = false,
};
private static readonly FilesystemFilterSet DOSBoxHDDImageFilterSet = new(
appendAllFilesEntry: false,
new FilesystemFilter("DOSBox HDD Images", extensions: [ "hdd" ]));
private static readonly FilesystemFilterSet ZXStateFilesFSFilterSet = new(new FilesystemFilter("ZX-State files", new[] { "szx" }))
{
AppendAllFilesEntry = false,
};
private static readonly FilesystemFilterSet ZXStateFilesFSFilterSet = new(
appendAllFilesEntry: false,
new FilesystemFilter("ZX-State files", extensions: [ "szx" ]));
private DialogResult OpenZXHawkSyncSettingsDialog(ISettingsAdapter settable)
{

View File

@ -47,10 +47,9 @@ namespace BizHawk.Client.EmuHawk
{
private static readonly FilesystemFilterSet EmuHawkSaveStatesFSFilterSet = new(FilesystemFilter.EmuHawkSaveStates);
private static readonly FilesystemFilterSet LibretroCoresFSFilterSet = new(new FilesystemFilter("Libretro Cores", new[] { OSTailoredCode.IsUnixHost ? "so" : "dll" }))
{
AppendAllFilesEntry = false,
};
private static readonly FilesystemFilterSet LibretroCoresFSFilterSet = new(
appendAllFilesEntry: false,
new FilesystemFilter("Libretro Cores", extensions: [ (OSTailoredCode.IsUnixHost ? "so" : "dll") ]));
private const int WINDOW_SCALE_MAX = 10;
@ -2497,10 +2496,9 @@ namespace BizHawk.Client.EmuHawk
new object/*?*/[] { c },
CultureInfo.InvariantCulture);
public static readonly FilesystemFilterSet ConfigFileFSFilterSet = new(new FilesystemFilter("Config File", new[] { "ini" }))
{
AppendAllFilesEntry = false,
};
public static readonly FilesystemFilterSet ConfigFileFSFilterSet = new(
appendAllFilesEntry: false,
new FilesystemFilter("Config File", extensions: [ "ini" ]));
private void OpenRom()
{

View File

@ -11,10 +11,9 @@ namespace BizHawk.Client.EmuHawk
{
public partial class DisplayConfig : Form, IDialogParent
{
private static readonly FilesystemFilterSet CgShaderPresetsFSFilterSet = new(new FilesystemFilter(".CGP Files", [ "cgp", "glslp" ]))
{
AppendAllFilesEntry = false,
};
private static readonly FilesystemFilterSet CgShaderPresetsFSFilterSet = new(
appendAllFilesEntry: false,
new FilesystemFilter(".CGP Files", extensions: [ "cgp", "glslp" ]));
private readonly Config _config;

View File

@ -16,10 +16,9 @@ namespace BizHawk.Client.EmuHawk
{
public partial class NESMusicRipper : ToolFormBase, IToolFormAutoConfig
{
private static readonly FilesystemFilterSet RenoiseFilesFSFilterSet = new(new FilesystemFilter("Renoise Song Files", new[] { "xrns" }))
{
AppendAllFilesEntry = false,
};
private static readonly FilesystemFilterSet RenoiseFilesFSFilterSet = new(
appendAllFilesEntry: false,
new FilesystemFilter("Renoise Song Files", extensions: [ "xrns" ]));
public static Icon ToolIcon
=> Properties.Resources.NesControllerIcon;