From ef14f661c03eb6fc2e4bc80ac726c99bca7d0974 Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 28 Jul 2014 00:49:48 +0000 Subject: [PATCH] Hex Editor - properly handle (and not throw exceptions) the display of cheat values in a byte size not equal to the cheat byte size --- BizHawk.Client.Common/tools/CheatList.cs | 51 +++++++++++++++++++ .../tools/HexEditor/HexEditor.cs | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/BizHawk.Client.Common/tools/CheatList.cs b/BizHawk.Client.Common/tools/CheatList.cs index 6fb53ca27d..ff071c4800 100644 --- a/BizHawk.Client.Common/tools/CheatList.cs +++ b/BizHawk.Client.Common/tools/CheatList.cs @@ -193,6 +193,11 @@ namespace BizHawk.Client.Common return _cheatList.Any(c => c == cheat); } + public bool Contains(MemoryDomain domain, int address) + { + return _cheatList.Any(c => c.Domain == domain && c.Address == address); + } + public void CopyTo(Cheat[] array, int arrayIndex) { _cheatList.CopyTo(array, arrayIndex); @@ -268,6 +273,52 @@ namespace BizHawk.Client.Common return activeCheat.GetByteVal(addr); } + /// + /// Returns the value of a given cheat, or a partial value of a multi-byte cheat + /// Note that address + size MUST NOT exceed the range of the cheat or undefined behavior will occur + /// + /// The starting address for which you will get the number of bytes + /// The number of bytes of the cheat to return + /// The value, or null if it can't resolve the address with a given cheat + public int? GetCheatValue(MemoryDomain domain, int addr, Watch.WatchSize size) + { + var activeCheat = _cheatList.FirstOrDefault(cheat => cheat.Contains(addr)); + if (activeCheat == (Cheat)null) + { + return null; + } + + switch (activeCheat.Size) + { + default: + case Watch.WatchSize.Byte: + return activeCheat.Value; + case Watch.WatchSize.Word: + if (size == Watch.WatchSize.Byte) + { + return GetByteValue(domain, addr); + } + + return activeCheat.Value; + case Watch.WatchSize.DWord: + if (size == Watch.WatchSize.Byte) + { + return GetByteValue(domain, addr); + } + else if (size == Watch.WatchSize.Word) + { + if (activeCheat.Address == addr) + { + return (activeCheat.Value.Value >> 16) & 0xFFFF; + } + + return activeCheat.Value.Value & 0xFFFF; + } + + return activeCheat.Value; + } + } + public void SaveOnClose() { if (Global.Config.CheatsAutoSaveOnClose) diff --git a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs index 0332479084..a7579a9bed 100644 --- a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs +++ b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs @@ -574,7 +574,7 @@ namespace BizHawk.Client.EmuHawk { if (Global.CheatList.IsActive(_domain, address)) { - return Global.CheatList[_domain, address].Value.Value; + return Global.CheatList.GetCheatValue(_domain, address, (Watch.WatchSize)_dataSize ).Value; } switch (_dataSize)