Enable MEN014 and fix noncompliance

"Prefer TryGetValue"
This commit is contained in:
YoshiRulz 2023-04-25 16:12:31 +10:00
parent c8589de5c6
commit fa48278354
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
13 changed files with 40 additions and 32 deletions

View File

@ -122,7 +122,7 @@
<Rule Id="MEN013" Action="Hidden" />
<!-- Prefer TryGetValue -->
<Rule Id="MEN014" Action="Hidden" />
<Rule Id="MEN014" Action="Warning" />
<!-- Use Preferred Terms -->
<Rule Id="MEN015" Action="Hidden" />

View File

@ -356,9 +356,11 @@ namespace BizHawk.Client.Common
return;
}
using var g = GetGraphics(surfaceID);
var isCached = _imageCache.ContainsKey(path);
var img = isCached ? _imageCache[path] : Image.FromFile(path);
if (!isCached && cache) _imageCache[path] = img;
if (!_imageCache.TryGetValue(path, out var img))
{
img = Image.FromFile(path);
if (cache) _imageCache[path] = img;
}
g.CompositingMode = _compositingMode;
g.DrawImage(
img,

View File

@ -211,8 +211,8 @@ namespace BizHawk.Client.Common
{
if (string.IsNullOrWhiteSpace(movie.Core))
{
PopupMessage(preferredCores.TryGetValue(systemId, out _)
? $"No core specified in the movie file, using the preferred core {preferredCores[systemId]} instead."
PopupMessage(preferredCores.TryGetValue(systemId, out var coreName)
? $"No core specified in the movie file, using the preferred core {coreName} instead."
: "No core specified in the movie file, using the default core instead.");
}
else

View File

@ -25,16 +25,23 @@ namespace BizHawk.Client.Common
public void SetState(int frame, Stream stream)
{
if (!_streams.ContainsKey(frame))
if (_streams.TryGetValue(frame, out var foundStream))
{
string filename = TempFileManager.GetTempFilename("State");
_streams[frame] = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose);
foundStream.Seek(0, SeekOrigin.Begin);
}
else
_streams[frame].Seek(0, SeekOrigin.Begin);
{
_streams[frame] = foundStream = new FileStream(
TempFileManager.GetTempFilename("State"),
FileMode.Create,
FileAccess.ReadWrite,
FileShare.None,
4096,
FileOptions.DeleteOnClose);
}
_streams[frame].SetLength(stream.Length);
stream.CopyTo(_streams[frame]);
foundStream.SetLength(stream.Length);
stream.CopyTo(foundStream);
}
public ICollection<int> Keys => _streams.Keys;

View File

@ -150,15 +150,7 @@ namespace BizHawk.Client.EmuHawk
protected override int IdentifyHash(string hash)
{
_gameHash ??= hash;
if (_cachedGameIds.ContainsKey(hash))
{
return _cachedGameIds[hash];
}
var ret = SendHash(hash);
_cachedGameIds.Add(hash, ret);
return ret;
return _cachedGameIds.TryGetValue(hash, out var id) ? id : (_cachedGameIds[hash] = SendHash(hash));
}
protected override int IdentifyRom(byte[] rom)

View File

@ -249,13 +249,13 @@ namespace BizHawk.Client.EmuHawk
color = Palette.AnalogEdit_Col;
}
if (!_alternateRowColor.ContainsKey(columnName))
if (!_alternateRowColor.TryGetValue(columnName, out var useAltColor))
{
int playerNumber = ControllerDefinition.PlayerNumber(columnName);
_alternateRowColor[columnName] = playerNumber != 0 && playerNumber % 2 == 0;
_alternateRowColor[columnName] = useAltColor = playerNumber % 2 is 0 && playerNumber is not 0;
}
if (_alternateRowColor[columnName])
if (useAltColor)
{
color = Color.FromArgb(0x0D, 0x00, 0x00, 0x00);
}

View File

@ -89,10 +89,10 @@ namespace BizHawk.Common
{
il.Emit(OpCodes.Unbox_Any, desiredType);
}
else if (IntTypes.ContainsKey(sourceType) && IntTypes.ContainsKey(desiredType))
else if (IntTypes.ContainsKey(sourceType) && IntTypes.TryGetValue(desiredType, out var desiredOpcode))
{
il.Emit(OpCodes.Unbox_Any, sourceType);
il.Emit(IntTypes[desiredType]);
il.Emit(desiredOpcode);
}
else
{

View File

@ -57,6 +57,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
private void SyncByteArrayDomain(string name, byte[] data)
{
#pragma warning disable MEN014 // see ZXHawk copy
if (_memoryDomainsInit || _byteArrayDomains.ContainsKey(name))
{
var m = _byteArrayDomains[name];
@ -67,6 +68,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
var m = new MemoryDomainByteArray(name, MemoryDomain.Endian.Little, data, true, 1);
_byteArrayDomains.Add(name, m);
}
#pragma warning restore MEN014
}
}
}

View File

@ -89,6 +89,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
private void SyncByteArrayDomain(string name, byte[] data)
{
#pragma warning disable MEN014 // unclear how this check works from looking only at this code --yoshi
if (_memoryDomainsInit || _byteArrayDomains.ContainsKey(name))
{
var m = _byteArrayDomains[name];
@ -99,6 +100,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
var m = new MemoryDomainByteArray(name, MemoryDomain.Endian.Little, data, true, 1);
_byteArrayDomains.Add(name, m);
}
#pragma warning restore MEN014
}
}
}

View File

@ -38,6 +38,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
Rom = rom;
_game = game;
#pragma warning disable MEN014 // could rewrite this to be 1 read + 0-1 writes, but nah --yoshi
if (!game.GetOptions().ContainsKey("m"))
{
game.AddOption("m", DetectMapper(rom));
@ -51,6 +52,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
}
Console.WriteLine("Game uses mapper " + game.GetOptions()["m"]);
#pragma warning restore MEN014
Console.WriteLine(romHashSHA1);
RebootCore();
SetupMemoryDomains();

View File

@ -47,6 +47,7 @@ namespace BizHawk.Emulation.Cores.Consoles.ChannelF
public void SyncByteArrayDomain(string name, byte[] data)
{
#pragma warning disable MEN014 // see ZXHawk copy
if (_memoryDomainsInit || _byteArrayDomains.ContainsKey(name))
{
var m = _byteArrayDomains[name];
@ -57,6 +58,7 @@ namespace BizHawk.Emulation.Cores.Consoles.ChannelF
var m = new MemoryDomainByteArray(name, MemoryDomain.Endian.Big, data, false, 1);
_byteArrayDomains.Add(name, m);
}
#pragma warning restore MEN014
}
}
}

View File

@ -333,10 +333,7 @@ namespace BizHawk.Emulation.Cores.PCEngine
Dictionary<string, int> sizes = new Dictionary<string, int>();
foreach (var m in mm)
{
if (!sizes.ContainsKey(m.Name) || m.MaxOffs >= sizes[m.Name])
{
sizes[m.Name] = m.MaxOffs;
}
if (!sizes.TryGetValue(m.Name, out var size) || size <= m.MaxOffs) sizes[m.Name] = m.MaxOffs;
}
var keys = new List<string>(sizes.Keys);

View File

@ -287,8 +287,10 @@ namespace BizHawk.Emulation.Cores.Waterbox
foreach (var portInfo in allPorts)
{
if (s.AllOverrides.ContainsKey(portInfo.FullName) && s.AllOverrides[portInfo.FullName].Default != null)
portInfo.DefaultDeviceShortName = s.AllOverrides[portInfo.FullName].Default;
if (s.AllOverrides.TryGetValue(portInfo.FullName, out var portOverride) && portOverride.Default is not null)
{
portInfo.DefaultDeviceShortName = portOverride.Default;
}
s.Ports.Add(new NymaSettingsInfo.Port
{