From 4e85a71d53e47f30b8891f273c3758b8d89a396e Mon Sep 17 00:00:00 2001 From: kalimag <kalimag@users.noreply.github.com> Date: Wed, 21 Aug 2024 07:59:39 +0200 Subject: [PATCH] HexEditor "File on Disk" fixes (squashed PR #3998) * Fix keeping rom domain selected * Turn unnecessary field into local * Handle `null` return from `GetRomBytes()` * Simplify `GetRomBytes()` * Add try-catch around opening rom file --- .../tools/HexEditor/HexEditor.cs | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs index 03a06fdaa3..8b557fae2f 100644 --- a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs +++ b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs @@ -10,7 +10,6 @@ using System.Windows.Forms; using BizHawk.Common; using BizHawk.Common.NumberExtensions; using BizHawk.Common.StringExtensions; -using BizHawk.Common.IOExtensions; using BizHawk.Emulation.Common; using BizHawk.Client.Common; using BizHawk.Client.EmuHawk.Properties; @@ -110,7 +109,6 @@ namespace BizHawk.Client.EmuHawk private string _findStr = ""; private bool _mouseIsDown; - private byte[] _rom; private MemoryDomain _romDomain; private HexFind _hexFind; private string _lastRom = ""; @@ -231,24 +229,26 @@ namespace BizHawk.Client.EmuHawk public override void Restart() { + _romDomain = null; if (Emulator.SystemId is not VSystemID.Raw.Arcade) { - _rom = GetRomBytes(); - _romDomain = new MemoryDomainByteArray(ROM_DOMAIN_NAME, MemoryDomain.Endian.Little, _rom, writable: true, wordSize: 1); - - if (_domain.Name == _romDomain.Name) + var rom = GetRomBytes(); + if (rom is not null) { - _domain = _romDomain; + _romDomain = new MemoryDomainByteArray(ROM_DOMAIN_NAME, MemoryDomain.Endian.Little, rom, writable: true, wordSize: 1); } } - else + + if (_domain.Name == ROM_DOMAIN_NAME && _romDomain is not null) { - _romDomain = null; + _domain = _romDomain; + } + else + { + _domain = MemoryDomains.Any(x => x.Name == _domain.Name) + ? MemoryDomains[_domain.Name] + : MemoryDomains.MainMemory; } - - _domain = MemoryDomains.Any(x => x.Name == _domain.Name) - ? MemoryDomains[_domain.Name] - : MemoryDomains.MainMemory; BigEndian = _domain.EndianType == MemoryDomain.Endian.Big; @@ -456,24 +456,25 @@ namespace BizHawk.Client.EmuHawk { var path = MainForm.CurrentlyOpenRomArgs.OpenAdvanced.SimplePath; if (string.IsNullOrEmpty(path)) - { - return new byte[] { 0xFF }; - } - - using var file = new HawkFile(path); - - if (!file.Exists) { return null; } - if (file.IsArchive) + try { - var stream = file.GetStream(); - return stream.ReadAllBytes(); + using var file = new HawkFile(path); + if (file.Exists) + { + return file.ReadAllBytes(); + } + } + catch (Exception ex) + { + using var exceptionBox = new ExceptionBox(ex); + this.ShowDialogWithTempMute(exceptionBox); } - return File.ReadAllBytes(path); + return null; } private static int GetNumDigits(long i)