From d6e56f8aa86613ebd486430a9e5062af1c8da01f Mon Sep 17 00:00:00 2001 From: adelikat Date: Wed, 4 Mar 2020 18:15:04 -0600 Subject: [PATCH] RamSearchEngine - simplify watchList creation with extension methods --- .../tools/RamSearchEngine/Extensions.cs | 36 +++++++++++ .../tools/RamSearchEngine/RamSearchEngine.cs | 60 ++----------------- 2 files changed, 42 insertions(+), 54 deletions(-) diff --git a/BizHawk.Client.Common/tools/RamSearchEngine/Extensions.cs b/BizHawk.Client.Common/tools/RamSearchEngine/Extensions.cs index 5225baa068..bf770c33a8 100644 --- a/BizHawk.Client.Common/tools/RamSearchEngine/Extensions.cs +++ b/BizHawk.Client.Common/tools/RamSearchEngine/Extensions.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.Linq; +using BizHawk.Emulation.Common; namespace BizHawk.Client.Common.RamSearchEngine { @@ -9,5 +12,38 @@ namespace BizHawk.Client.Common.RamSearchEngine var bytes = BitConverter.GetBytes((int)val); return BitConverter.ToSingle(bytes, 0); } + + public static IEnumerable ToBytes(this IEnumerable addresses, SearchEngineSettings settings) + => settings.IsDetailed() + ? addresses.ToBytes(settings.Domain) + : addresses.ToDetailedBytes(settings.Domain); + + public static IEnumerable ToWords(this IEnumerable addresses, SearchEngineSettings settings) + => settings.IsDetailed() + ? addresses.ToWords(settings.Domain, settings.BigEndian) + : addresses.ToDetailedWords(settings.Domain, settings.BigEndian); + + public static IEnumerable ToDWords(this IEnumerable addresses, SearchEngineSettings settings) + => settings.IsDetailed() + ? addresses.ToDWords(settings.Domain, settings.BigEndian) + : addresses.ToDetailedDWords(settings.Domain, settings.BigEndian); + + private static IEnumerable ToBytes(this IEnumerable addresses, MemoryDomain domain) + => addresses.Select(a => new MiniByteWatch(domain, a)); + + private static IEnumerable ToDetailedBytes(this IEnumerable addresses, MemoryDomain domain) + => addresses.Select(a => new MiniByteWatchDetailed(domain, a)); + + private static IEnumerable ToWords(this IEnumerable addresses, MemoryDomain domain, bool bigEndian) + => addresses.Select(a => new MiniWordWatch(domain, a, bigEndian)); + + private static IEnumerable ToDetailedWords(this IEnumerable addresses, MemoryDomain domain, bool bigEndian) + => addresses.Select(a => new MiniWordWatchDetailed(domain, a, bigEndian)); + + private static IEnumerable ToDWords(this IEnumerable addresses, MemoryDomain domain, bool bigEndian) + => addresses.Select(a => new MiniDWordWatch(domain, a, bigEndian)); + + private static IEnumerable ToDetailedDWords(this IEnumerable addresses, MemoryDomain domain, bool bigEndian) + => addresses.Select(a => new MiniDWordWatchDetailed(domain, a, bigEndian)); } } diff --git a/BizHawk.Client.Common/tools/RamSearchEngine/RamSearchEngine.cs b/BizHawk.Client.Common/tools/RamSearchEngine/RamSearchEngine.cs index 73290cc20b..4329491533 100644 --- a/BizHawk.Client.Common/tools/RamSearchEngine/RamSearchEngine.cs +++ b/BizHawk.Client.Common/tools/RamSearchEngine/RamSearchEngine.cs @@ -298,61 +298,13 @@ namespace BizHawk.Client.Common.RamSearchEngine _watchList.Clear(); } - switch (_settings.Size) + _watchList = _settings.Size switch { - default: - case WatchSize.Byte: - if (_settings.IsDetailed()) - { - foreach (var addr in addresses) - { - _watchList.Add(new MiniByteWatchDetailed(_settings.Domain, addr)); - } - } - else - { - foreach (var addr in addresses) - { - _watchList.Add(new MiniByteWatch(_settings.Domain, addr)); - } - } - - break; - case WatchSize.Word: - if (_settings.IsDetailed()) - { - foreach (var addr in addresses) - { - _watchList.Add(new MiniWordWatchDetailed(_settings.Domain, addr, _settings.BigEndian)); - } - } - else - { - foreach (var addr in addresses) - { - _watchList.Add(new MiniWordWatch(_settings.Domain, addr, _settings.BigEndian)); - } - } - - break; - case WatchSize.DWord: - if (_settings.IsDetailed()) - { - foreach (var addr in addresses) - { - _watchList.Add(new MiniDWordWatchDetailed(_settings.Domain, addr, _settings.BigEndian)); - } - } - else - { - foreach (var addr in addresses) - { - _watchList.Add(new MiniDWordWatch(_settings.Domain, addr, _settings.BigEndian)); - } - } - - break; - } + WatchSize.Byte => addresses.ToBytes(_settings).ToList(), + WatchSize.Word => addresses.ToWords(_settings).ToList(), + WatchSize.DWord => addresses.ToDWords(_settings).ToList(), + _ => addresses.ToBytes(_settings).ToList() + }; } public void Sort(string column, bool reverse)