diff --git a/src/BizHawk.Client.Common/CoreFileProvider.cs b/src/BizHawk.Client.Common/CoreFileProvider.cs index 876009950a..96baf54009 100644 --- a/src/BizHawk.Client.Common/CoreFileProvider.cs +++ b/src/BizHawk.Client.Common/CoreFileProvider.cs @@ -34,59 +34,41 @@ namespace BizHawk.Client.Common public string GetRetroSystemPath(IGameInfo game) => _pathEntries.RetroSystemAbsolutePath(game); - private void FirmwareWarn(FirmwareID id, bool required, string msg = null) + private (byte[] FW, string Path)? GetFirmwareWithPath(FirmwareID id) { - if (required) - { - var fullMsg = $"Couldn't find required firmware {id}. This is fatal{(msg != null ? $": {msg}" : ".")}"; - throw new MissingFirmwareException(fullMsg); - } - - if (msg != null) - { - var fullMsg = $"Couldn't find firmware {id}. Will attempt to continue: {msg}"; - _dialogParent.ModalMessageBox(fullMsg, "Warning", EMsgBoxIcon.Warning); - } - } - - private byte[] GetFirmwareWithPath(FirmwareID id, bool required, string msg, out string path) - { - var firmwarePath = _firmwareManager.Request(_pathEntries, _firmwareUserSpecifications, id); - - if (firmwarePath == null || !File.Exists(firmwarePath)) - { - path = null; - FirmwareWarn(id, required, msg); - return null; - } - + var path = _firmwareManager.Request(_pathEntries, _firmwareUserSpecifications, id); try { - var ret = File.ReadAllBytes(firmwarePath); - path = firmwarePath; - return ret; + if (path is not null && File.Exists(path)) return (File.ReadAllBytes(path), path); + // else fall through } catch (IOException) { - path = null; - FirmwareWarn(id, required, msg); - return null; + // fall through } + return null; } - /// not found and is true - public byte[] GetFirmware(FirmwareID id, bool required, string msg = null) - => GetFirmwareWithPath(id, required, msg, out _); + private (byte[] FW, string Path) GetFirmwareWithPathOrThrow(FirmwareID id, string msg) + => GetFirmwareWithPath(id) ?? throw new MissingFirmwareException($"Couldn't find required firmware {id}. This is fatal{(msg is null ? "." : $": {msg}")}"); - /// not found and is true - public byte[] GetFirmwareWithGameInfo(FirmwareID id, bool required, out GameInfo gi, string msg = null) + public byte[] GetFirmware(FirmwareID id, string msg = null) { - var ret = GetFirmwareWithPath(id, required, msg, out var path); - gi = ret != null && path != null - ? Database.GetGameInfo(ret, path) - : null; + var tuple = GetFirmwareWithPath(id); + if (tuple is null && msg is not null) + { + _dialogParent.ModalMessageBox($"Couldn't find firmware {id}. Will attempt to continue: {msg}", "Warning", EMsgBoxIcon.Warning); + } + return tuple?.FW; + } - return ret; + public byte[] GetFirmwareOrThrow(FirmwareID id, string msg = null) + => GetFirmwareWithPathOrThrow(id, msg).FW; + + public (byte[] FW, GameInfo Game) GetFirmwareWithGameInfoOrThrow(FirmwareID id, string msg = null) + { + var (fw, path) = GetFirmwareWithPathOrThrow(id, msg); + return (fw, Database.GetGameInfo(fw, path)); } } -} \ No newline at end of file +} diff --git a/src/BizHawk.Emulation.Common/Extensions.cs b/src/BizHawk.Emulation.Common/Extensions.cs index 059021a0f4..cdf90098dd 100644 --- a/src/BizHawk.Emulation.Common/Extensions.cs +++ b/src/BizHawk.Emulation.Common/Extensions.cs @@ -480,18 +480,13 @@ namespace BizHawk.Emulation.Common public static AxisSpec With(this in AxisSpec spec, Range range, int neutral) => new AxisSpec(range, neutral, spec.IsReversed, spec.Constraint); - /// Get a firmware as a byte array - /// the core systemID - /// the firmware id - /// if true, result is guaranteed to be valid; else null is possible if not found - /// message to show if fail to get - /// TODO inline (only change is wrapping strings in ctor, these IDs should probably be consts in each core's class) - public static byte[] GetFirmware(this ICoreFileProvider cfp, string sysId, string firmwareId, bool required, string msg = null) - => cfp.GetFirmware(new(system: sysId, firmware: firmwareId), required: required, msg: msg); - - /// TODO inline (only change is wrapping strings in ctor, these IDs should probably be consts in each core's class) - public static byte[] GetFirmwareWithGameInfo(this ICoreFileProvider cfp, string sysId, string firmwareId, bool required, out GameInfo gi, string msg = null) - => cfp.GetFirmwareWithGameInfo(new(system: sysId, firmware: firmwareId), required: required, out gi, msg: msg); + /// message to show on failure, either in an exception iff , or in a warning dialog otherwise + /// if not found and is + /// TODO inline + public static byte[] GetFirmware(this ICoreFileProvider cfp, string sysID, string firmwareID, bool required, string msg = null) + => required + ? cfp.GetFirmwareOrThrow(new(sysID, firmwareID), msg) + : cfp.GetFirmware(new(sysID, firmwareID), msg); public static string SystemIDToDisplayName(string sysID) => SystemIDDisplayNames.TryGetValue(sysID, out var dispName) ? dispName : string.Empty; diff --git a/src/BizHawk.Emulation.Common/Interfaces/ICoreFileProvider.cs b/src/BizHawk.Emulation.Common/Interfaces/ICoreFileProvider.cs index 6fbfbe4e8d..2c6263af88 100644 --- a/src/BizHawk.Emulation.Common/Interfaces/ICoreFileProvider.cs +++ b/src/BizHawk.Emulation.Common/Interfaces/ICoreFileProvider.cs @@ -20,14 +20,17 @@ /// string GetRetroSystemPath(IGameInfo game); - /// - /// Get a firmware as a byte array - /// - /// the firmware id - /// if true, result is guaranteed to be valid; else null is possible if not found - /// message to show if fail to get - byte[] GetFirmware(FirmwareID id, bool required, string msg = null); + /// warning message to show on failure + /// iff failed + byte[] GetFirmware(FirmwareID id, string msg = null); - byte[] GetFirmwareWithGameInfo(FirmwareID id, bool required, out GameInfo gi, string msg = null); + /// exception message to show on failure + /// if not found + byte[] GetFirmwareOrThrow(FirmwareID id, string msg = null); + + /// exception message to show on failure + /// if not found + /// only used in PCEHawk + (byte[] FW, GameInfo Game) GetFirmwareWithGameInfoOrThrow(FirmwareID id, string msg = null); } } diff --git a/src/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs b/src/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs index 8c0e1c24b1..cb689b6d40 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs @@ -33,7 +33,8 @@ namespace BizHawk.Emulation.Cores.PCEngine Settings = (PCESettings)lp.Settings ?? new PCESettings(); _syncSettings = (PCESyncSettings)lp.SyncSettings ?? new PCESyncSettings(); - byte[] rom = lp.Comm.CoreFileProvider.GetFirmwareWithGameInfo("PCECD", "Bios", true, out var biosInfo, + var (rom, biosInfo) = lp.Comm.CoreFileProvider.GetFirmwareWithGameInfoOrThrow( + new("PCECD", "Bios"), "PCE-CD System Card not found. Please check the BIOS settings in Config->Firmwares."); if (biosInfo.Status == RomStatus.BadDump) diff --git a/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.cs b/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.cs index 86ae60f23e..e14a221880 100644 --- a/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.cs +++ b/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.cs @@ -58,13 +58,9 @@ namespace BizHawk.Emulation.Cores.Waterbox { if (firmwares != null && firmwares.TryGetValue(name, out var id)) { - var data = CoreComm.CoreFileProvider.GetFirmware(id, true, - "Firmware files are usually required and may stop your game from loading"); - if (data != null) - { - _exe.AddReadonlyFile(data, name); - filesToRemove.Add(name); - } + var data = CoreComm.CoreFileProvider.GetFirmwareOrThrow(id, "Firmware files are usually required and may stop your game from loading"); + _exe.AddReadonlyFile(data, name); + filesToRemove.Add(name); } else {