Refactor CFP.GetFirmware*

This commit is contained in:
YoshiRulz 2021-07-19 08:43:31 +10:00
parent 09afbdd6eb
commit 70037ee0fc
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
5 changed files with 47 additions and 70 deletions

View File

@ -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;
}
/// <exception cref="MissingFirmwareException">not found and <paramref name="required"/> is true</exception>
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}")}");
/// <exception cref="MissingFirmwareException">not found and <paramref name="required"/> is true</exception>
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));
}
}
}
}

View File

@ -480,18 +480,13 @@ namespace BizHawk.Emulation.Common
public static AxisSpec With(this in AxisSpec spec, Range<int> range, int neutral) => new AxisSpec(range, neutral, spec.IsReversed, spec.Constraint);
/// <summary>Get a firmware as a byte array</summary>
/// <param name="sysId">the core systemID</param>
/// <param name="firmwareId">the firmware id</param>
/// <param name="required">if true, result is guaranteed to be valid; else null is possible if not found</param>
/// <param name="msg">message to show if fail to get</param>
/// <remarks>TODO inline (only change is wrapping strings in <see cref="FirmwareID"/> ctor, these IDs should probably be consts in each core's class)</remarks>
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);
/// <remarks>TODO inline (only change is wrapping strings in <see cref="FirmwareID"/> ctor, these IDs should probably be consts in each core's class)</remarks>
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);
/// <param name="msg">message to show on failure, either in an exception iff <paramref name="required"/>, or in a warning dialog otherwise</param>
/// <exception cref="MissingFirmwareException">if not found and <paramref name="required"/> is <see langword="true"/></exception>
/// <remarks>TODO inline</remarks>
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;

View File

@ -20,14 +20,17 @@
/// </summary>
string GetRetroSystemPath(IGameInfo game);
/// <summary>
/// Get a firmware as a byte array
/// </summary>
/// <param name="id">the firmware id</param>
/// <param name="required">if true, result is guaranteed to be valid; else null is possible if not found</param>
/// <param name="msg">message to show if fail to get</param>
byte[] GetFirmware(FirmwareID id, bool required, string msg = null);
/// <param name="msg">warning message to show on failure</param>
/// <returns><see langword="null"/> iff failed</returns>
byte[] GetFirmware(FirmwareID id, string msg = null);
byte[] GetFirmwareWithGameInfo(FirmwareID id, bool required, out GameInfo gi, string msg = null);
/// <param name="msg">exception message to show on failure</param>
/// <exception cref="MissingFirmwareException">if not found</exception>
byte[] GetFirmwareOrThrow(FirmwareID id, string msg = null);
/// <param name="msg">exception message to show on failure</param>
/// <exception cref="MissingFirmwareException">if not found</exception>
/// <remarks>only used in PCEHawk</remarks>
(byte[] FW, GameInfo Game) GetFirmwareWithGameInfoOrThrow(FirmwareID id, string msg = null);
}
}

View File

@ -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)

View File

@ -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
{