From e460394d413449089f23a6ca6008567b6c7dbd35 Mon Sep 17 00:00:00 2001 From: adelikat Date: Wed, 4 Mar 2020 17:39:05 -0600 Subject: [PATCH] RamSearchEngine - cleanup code slightly by making ToFloat() an extension method, also fix CompareDifrerent by float comparisons that were all incorrect --- .../tools/RamSearchEngine/Extensions.cs | 13 +++++ .../tools/RamSearchEngine/RamSearchEngine.cs | 52 ++++++++----------- 2 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 BizHawk.Client.Common/tools/RamSearchEngine/Extensions.cs diff --git a/BizHawk.Client.Common/tools/RamSearchEngine/Extensions.cs b/BizHawk.Client.Common/tools/RamSearchEngine/Extensions.cs new file mode 100644 index 0000000000..5225baa068 --- /dev/null +++ b/BizHawk.Client.Common/tools/RamSearchEngine/Extensions.cs @@ -0,0 +1,13 @@ +using System; + +namespace BizHawk.Client.Common.RamSearchEngine +{ + internal static class Extensions + { + public static float ToFloat(this long val) + { + var bytes = BitConverter.GetBytes((int)val); + return BitConverter.ToSingle(bytes, 0); + } + } +} diff --git a/BizHawk.Client.Common/tools/RamSearchEngine/RamSearchEngine.cs b/BizHawk.Client.Common/tools/RamSearchEngine/RamSearchEngine.cs index 4a17dc672f..dd93f4261e 100644 --- a/BizHawk.Client.Common/tools/RamSearchEngine/RamSearchEngine.cs +++ b/BizHawk.Client.Common/tools/RamSearchEngine/RamSearchEngine.cs @@ -473,26 +473,26 @@ namespace BizHawk.Client.Common.RamSearchEngine default: case ComparisonOperator.Equal: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) == ToFloat(w.Previous)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() == w.Previous.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) == SignExtendAsNeeded(w.Previous)); case ComparisonOperator.NotEqual: return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) != SignExtendAsNeeded(w.Previous)); case ComparisonOperator.GreaterThan: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) > ToFloat(w.Previous)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() > w.Previous.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) > SignExtendAsNeeded(w.Previous)); case ComparisonOperator.GreaterThanEqual: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) >= ToFloat(w.Previous)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() >= w.Previous.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) >= SignExtendAsNeeded(w.Previous)); case ComparisonOperator.LessThan: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) < ToFloat(w.Previous)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() < w.Previous.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) < SignExtendAsNeeded(w.Previous)); case ComparisonOperator.LessThanEqual: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) <= ToFloat(w.Previous)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() <= w.Previous.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) <= SignExtendAsNeeded(w.Previous)); case ComparisonOperator.DifferentBy: if (DifferentBy.HasValue) @@ -500,8 +500,8 @@ namespace BizHawk.Client.Common.RamSearchEngine var differentBy = DifferentBy.Value; if (_settings.Type == DisplayType.Float) { - return watchList.Where(w => ToFloat(GetValue(w.Address)) + differentBy == ToFloat(w.Previous) - || ToFloat(GetValue(w.Address)) - differentBy == ToFloat(w.Previous)); + return watchList.Where(w => GetValue(w.Address).ToFloat() + differentBy == w.Previous.ToFloat() + || GetValue(w.Address).ToFloat() - differentBy == w.Previous.ToFloat()); } return watchList.Where(w => @@ -529,28 +529,28 @@ namespace BizHawk.Client.Common.RamSearchEngine default: case ComparisonOperator.Equal: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) == ToFloat(compareValue)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() == compareValue.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) == SignExtendAsNeeded(CompareValue.Value)); case ComparisonOperator.NotEqual: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) != ToFloat(compareValue)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() != compareValue.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) != SignExtendAsNeeded(compareValue)); case ComparisonOperator.GreaterThan: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) > ToFloat(compareValue)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() > compareValue.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) > SignExtendAsNeeded(compareValue)); case ComparisonOperator.GreaterThanEqual: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) >= ToFloat(compareValue)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() >= compareValue.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) >= SignExtendAsNeeded(compareValue)); case ComparisonOperator.LessThan: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) < ToFloat(compareValue)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() < compareValue.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) < SignExtendAsNeeded(compareValue)); case ComparisonOperator.LessThanEqual: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) <= ToFloat(compareValue)) + ? watchList.Where(w => GetValue(w.Address).ToFloat() <= compareValue.ToFloat()) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) <= SignExtendAsNeeded(compareValue)); case ComparisonOperator.DifferentBy: @@ -559,8 +559,8 @@ namespace BizHawk.Client.Common.RamSearchEngine var differentBy = DifferentBy.Value; if (_settings.Type == DisplayType.Float) { - return watchList.Where(w => ToFloat(GetValue(w.Address)) + differentBy == compareValue - || ToFloat(GetValue(w.Address)) - differentBy == compareValue); + return watchList.Where(w => GetValue(w.Address).ToFloat() + differentBy == compareValue + || GetValue(w.Address).ToFloat() - differentBy == compareValue); } return watchList.Where(w @@ -667,27 +667,27 @@ namespace BizHawk.Client.Common.RamSearchEngine default: case ComparisonOperator.Equal: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) - ToFloat(w.Previous) == compareValue) + ? watchList.Where(w => GetValue(w.Address).ToFloat() - w.Previous.ToFloat() == compareValue) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) == compareValue); case ComparisonOperator.NotEqual: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous != compareValue) + ? watchList.Where(w => GetValue(w.Address).ToFloat() - w.Previous.ToFloat() != compareValue) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) != compareValue); case ComparisonOperator.GreaterThan: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous > compareValue) + ? watchList.Where(w => GetValue(w.Address).ToFloat() - w.Previous.ToFloat() > compareValue) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) > compareValue); case ComparisonOperator.GreaterThanEqual: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous >= compareValue) + ? watchList.Where(w => GetValue(w.Address).ToFloat() - w.Previous.ToFloat() >= compareValue) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) >= compareValue); case ComparisonOperator.LessThan: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous < compareValue) + ? watchList.Where(w => GetValue(w.Address).ToFloat() - w.Previous.ToFloat() < compareValue) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) < compareValue); case ComparisonOperator.LessThanEqual: return _settings.Type == DisplayType.Float - ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous <= compareValue) + ? watchList.Where(w => GetValue(w.Address).ToFloat() - w.Previous.ToFloat() <= compareValue) : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) <= compareValue); case ComparisonOperator.DifferentBy: if (DifferentBy.HasValue) @@ -695,8 +695,8 @@ namespace BizHawk.Client.Common.RamSearchEngine var differentBy = DifferentBy.Value; if (_settings.Type == DisplayType.Float) { - return watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous + differentBy == compareValue - || ToFloat(GetValue(w.Address)) - w.Previous - differentBy == w.Previous); + return watchList.Where(w => GetValue(w.Address).ToFloat() - w.Previous.ToFloat() + differentBy == compareValue + || GetValue(w.Address).ToFloat() - w.Previous.ToFloat() - differentBy == w.Previous); } return watchList.Where(w @@ -715,12 +715,6 @@ namespace BizHawk.Client.Common.RamSearchEngine #region Private parts - private static float ToFloat(long val) - { - var bytes = BitConverter.GetBytes((int)val); - return BitConverter.ToSingle(bytes, 0); - } - private long SignExtendAsNeeded(long val) { if (_settings.Type != DisplayType.Signed)