From 1232157cc11edc36cc66be7a5ac29d0ceb225002 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sat, 8 Oct 2022 11:23:40 +1000 Subject: [PATCH] Improve caching of `FilesystemFilterSet.ToString` --- .../FilesystemFilterSet.cs | 38 ++++++++++--------- src/BizHawk.Client.Common/RomLoader.cs | 12 +++--- .../movie/import/MovieImport.cs | 6 ++- src/BizHawk.Client.EmuHawk/MainForm.Events.cs | 4 +- src/BizHawk.Client.EmuHawk/MainForm.cs | 2 +- .../MultiDiskBundler/MultiDiskFileSelector.cs | 2 +- 6 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/BizHawk.Client.Common/FilesystemFilterSet.cs b/src/BizHawk.Client.Common/FilesystemFilterSet.cs index dfa1cb730c..d5d3710013 100644 --- a/src/BizHawk.Client.Common/FilesystemFilterSet.cs +++ b/src/BizHawk.Client.Common/FilesystemFilterSet.cs @@ -9,28 +9,30 @@ namespace BizHawk.Client.Common { public sealed class FilesystemFilterSet { - private string? _allSer = null; + private string? _ser = null; - public readonly IReadOnlyCollection Filters; + public bool AppendAllFilesEntry { get; init; } = true; + + public string? CombinedEntryDesc { get; init; } = null; + + private IReadOnlyCollection CombinedExts + => Filters.SelectMany(static filter => filter.Extensions).Distinct().Order().ToList(); + + public readonly IReadOnlyList Filters; public FilesystemFilterSet(params FilesystemFilter[] filters) + => Filters = filters; + + public override string ToString() { - Filters = filters; - } - - /// appends All Files entry (calls with ), return value is a valid Filter for Save-/OpenFileDialog - public override string ToString() => ToString(true); - - /// call other overload to prepend combined entry, return value is a valid Filter for Save-/OpenFileDialog - public string ToString(bool addAllFilesEntry) => addAllFilesEntry - ? $"{ToString(false)}|{FilesystemFilter.AllFilesEntry}" - : string.Join("|", Filters.Select(filter => filter.ToString())); - - /// call other overload (omit ) to not prepend combined entry, return value is a valid Filter for Save-/OpenFileDialog - public string ToString(string combinedEntryDesc, bool addAllFilesEntry = true) - { - _allSer ??= FilesystemFilter.SerializeEntry(combinedEntryDesc, Filters.SelectMany(static filter => filter.Extensions).Distinct().Order().ToList()); - return $"{_allSer}|{ToString(addAllFilesEntry)}"; + 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; } public static readonly FilesystemFilterSet Screenshots = new FilesystemFilterSet(FilesystemFilter.PNGs, new FilesystemFilter(".bmp Files", new[] { "bmp" })); diff --git a/src/BizHawk.Client.Common/RomLoader.cs b/src/BizHawk.Client.Common/RomLoader.cs index e42b619435..db7f6d2cc8 100644 --- a/src/BizHawk.Client.Common/RomLoader.cs +++ b/src/BizHawk.Client.Common/RomLoader.cs @@ -869,7 +869,7 @@ namespace BizHawk.Client.Common } /// TODO add and handle (you can drag-and-drop scripts and there are already non-rom things in this list, so why not?) - private static readonly FilesystemFilterSet RomFSFilterSet = new FilesystemFilterSet( + public static readonly FilesystemFilterSet RomFilter = new( new FilesystemFilter("Music Files", Array.Empty(), devBuildExtraExts: new[] { "psf", "minipsf", "sid", "nsf", "gbs" }), new FilesystemFilter("Disc Images", new[] { "cue", "ccd", "mds", "m3u" }), new FilesystemFilter("NES", RomFileExtensions.NES.Concat(new[] { "nsf" }).ToList(), addArchiveExts: true), @@ -905,16 +905,16 @@ namespace BizHawk.Client.Common new FilesystemFilter("Uzebox", RomFileExtensions.UZE), new FilesystemFilter("Vectrex", RomFileExtensions.VEC), new FilesystemFilter("MSX", RomFileExtensions.MSX), - FilesystemFilter.EmuHawkSaveStates - ); + FilesystemFilter.EmuHawkSaveStates) + { + CombinedEntryDesc = "Everything", + }; - public static readonly IReadOnlyCollection KnownRomExtensions = RomFSFilterSet.Filters + public static readonly IReadOnlyCollection KnownRomExtensions = RomFilter.Filters .SelectMany(f => f.Extensions) .Distinct() .Except(FilesystemFilter.ArchiveExtensions.Concat(new[] { "State" })) .Select(s => $".{s.ToUpperInvariant()}") // this is what's expected at call-site .ToList(); - - public static readonly string RomFilter = RomFSFilterSet.ToString("Everything"); } } diff --git a/src/BizHawk.Client.Common/movie/import/MovieImport.cs b/src/BizHawk.Client.Common/movie/import/MovieImport.cs index ec1960fc6f..38e09b98d4 100644 --- a/src/BizHawk.Client.Common/movie/import/MovieImport.cs +++ b/src/BizHawk.Client.Common/movie/import/MovieImport.cs @@ -27,8 +27,10 @@ namespace BizHawk.Client.Common public static readonly FilesystemFilterSet AvailableImporters = new FilesystemFilterSet( Importers.Values.OrderBy(attr => attr.Emulator) .Select(attr => new FilesystemFilter(attr.Emulator, new[] { attr.Extension.Substring(1) })) // substring removes initial '.' - .ToArray() - ); + .ToArray()) + { + CombinedEntryDesc = "Movie Files", + }; // Attempt to import another type of movie file into a movie object. public static ImportResult ImportFile( diff --git a/src/BizHawk.Client.EmuHawk/MainForm.Events.cs b/src/BizHawk.Client.EmuHawk/MainForm.Events.cs index 290bcee294..f9ae942976 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -302,7 +302,7 @@ namespace BizHawk.Client.EmuHawk var args = new LoadRomArgs(); - var filter = RomLoader.RomFilter; + var filter = RomLoader.RomFilter.ToString(); if (oac.Result == AdvancedRomLoaderType.LibretroLaunchGame) { @@ -494,7 +494,7 @@ namespace BizHawk.Client.EmuHawk { InitialDirectory = Config.PathEntries.RomAbsolutePath(Emulator.SystemId), Multiselect = true, - Filter = MovieImport.AvailableImporters.ToString("Movie Files"), + Filter = MovieImport.AvailableImporters.ToString(), RestoreDirectory = false }; diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index b95cd78d03..0351c1a67d 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -2399,7 +2399,7 @@ namespace BizHawk.Client.EmuHawk using var ofd = new OpenFileDialog { InitialDirectory = Config.PathEntries.RomAbsolutePath(Emulator.SystemId), - Filter = RomLoader.RomFilter, + Filter = RomLoader.RomFilter.ToString(), RestoreDirectory = false, FilterIndex = _lastOpenRomFilter }; diff --git a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs index 208498864b..cf202e2402 100644 --- a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs +++ b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs @@ -75,7 +75,7 @@ namespace BizHawk.Client.EmuHawk using var ofd = new OpenFileDialog { InitialDirectory = _pathEntries.RomAbsolutePath(), - Filter = RomLoader.RomFilter, + Filter = RomLoader.RomFilter.ToString(), RestoreDirectory = true };