diff --git a/BizHawk.Common/Extensions/IOExtensions.cs b/BizHawk.Common/Extensions/IOExtensions.cs index 309b8ddcb4..ea05a546a0 100644 --- a/BizHawk.Common/Extensions/IOExtensions.cs +++ b/BizHawk.Common/Extensions/IOExtensions.cs @@ -34,7 +34,7 @@ namespace BizHawk.Common.IOExtensions public static string ReadStringUtf8NullTerminated(this BinaryReader br) { - var ms = new MemoryStream(); + using var ms = new MemoryStream(); for (;;) { var b = br.ReadByte(); diff --git a/BizHawk.Common/HawkFile.cs b/BizHawk.Common/HawkFile.cs index 2e50fc27b5..adc5f559eb 100644 --- a/BizHawk.Common/HawkFile.cs +++ b/BizHawk.Common/HawkFile.cs @@ -147,10 +147,8 @@ namespace BizHawk.Common /// public static bool ExistsAt(string path) { - using (var file = new HawkFile(path)) - { - return file.Exists; - } + using var file = new HawkFile(path); + return file.Exists; } /// @@ -158,20 +156,16 @@ namespace BizHawk.Common /// public static byte[] ReadAllBytes(string path) { - using (var file = new HawkFile(path)) + using var file = new HawkFile(path); + if (!file.Exists) { - if (!file.Exists) - { - throw new FileNotFoundException(path); - } - - using (Stream stream = file.GetStream()) - { - var ms = new MemoryStream((int)stream.Length); - stream.CopyTo(ms); - return ms.GetBuffer(); - } + throw new FileNotFoundException(path); } + + using Stream stream = file.GetStream(); + using var ms = new MemoryStream((int)stream.Length); + stream.CopyTo(ms); + return ms.GetBuffer(); } /// diff --git a/BizHawk.Emulation.Cores/Calculator/TI83.IStatable.cs b/BizHawk.Emulation.Cores/Calculator/TI83.IStatable.cs index 9c18bcbf70..e0a7286fa3 100644 --- a/BizHawk.Emulation.Cores/Calculator/TI83.IStatable.cs +++ b/BizHawk.Emulation.Cores/Calculator/TI83.IStatable.cs @@ -7,10 +7,7 @@ namespace BizHawk.Emulation.Cores.Calculators { public partial class TI83 : IStatable { - public bool BinarySaveStatesPreferred - { - get { return true; } - } + public bool BinarySaveStatesPreferred => true; public void SaveStateText(TextWriter writer) { @@ -34,8 +31,8 @@ namespace BizHawk.Emulation.Cores.Calculators public byte[] SaveStateBinary() { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); diff --git a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IStatable.cs b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IStatable.cs index 7f006c8f23..92bc33c506 100644 --- a/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IStatable.cs +++ b/BizHawk.Emulation.Cores/Computers/AmstradCPC/AmstradCPC.IStatable.cs @@ -10,13 +10,9 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC /// public partial class AmstradCPC : IStatable { + public bool BinarySaveStatesPreferred => true; - public bool BinarySaveStatesPreferred - { - get { return true; } - } - - public void SaveStateText(TextWriter writer) + public void SaveStateText(TextWriter writer) { SyncState(new Serializer(writer)); } @@ -38,8 +34,8 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC public byte[] SaveStateBinary() { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); diff --git a/BizHawk.Emulation.Cores/Computers/Commodore64/C64.IStatable.cs b/BizHawk.Emulation.Cores/Computers/Commodore64/C64.IStatable.cs index d8c3e9840f..33d7787322 100644 --- a/BizHawk.Emulation.Cores/Computers/Commodore64/C64.IStatable.cs +++ b/BizHawk.Emulation.Cores/Computers/Commodore64/C64.IStatable.cs @@ -31,13 +31,11 @@ namespace BizHawk.Emulation.Cores.Computers.Commodore64 public byte[] SaveStateBinary() { - using (var ms = new MemoryStream()) - { - var bw = new BinaryWriter(ms); - SaveStateBinary(bw); - bw.Flush(); - return ms.ToArray(); - } + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); + SaveStateBinary(bw); + bw.Flush(); + return ms.ToArray(); } private void SyncState(Serializer ser) diff --git a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.IStatable.cs b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.IStatable.cs index c0b6b4b06d..c4f054fcdf 100644 --- a/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.IStatable.cs +++ b/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/ZXSpectrum.IStatable.cs @@ -10,12 +10,9 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// public partial class ZXSpectrum : IStatable { - public bool BinarySaveStatesPreferred - { - get { return true; } - } + public bool BinarySaveStatesPreferred => true; - public void SaveStateText(TextWriter writer) + public void SaveStateText(TextWriter writer) { SyncState(new Serializer(writer)); } @@ -37,8 +34,8 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum public byte[] SaveStateBinary() { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); diff --git a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.IStatable.cs index 7751a535f5..8ab93398d9 100644 --- a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.IStatable.cs @@ -7,10 +7,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 { public partial class Atari2600 : IStatable { - public bool BinarySaveStatesPreferred - { - get { return true; } - } + public bool BinarySaveStatesPreferred => true; public void SaveStateText(TextWriter writer) { @@ -34,8 +31,8 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 public byte[] SaveStateBinary() { - var ms = new MemoryStream(); - var bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); diff --git a/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.IStatable.cs index 3203b0d194..d6bf7f09a0 100644 --- a/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.IStatable.cs @@ -31,8 +31,8 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk public byte[] SaveStateBinary() { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); diff --git a/BizHawk.Emulation.Cores/Consoles/Atari/lynx/Lynx.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Atari/lynx/Lynx.IStatable.cs index adad5e2c4b..e30efd4873 100644 --- a/BizHawk.Emulation.Cores/Consoles/Atari/lynx/Lynx.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Atari/lynx/Lynx.IStatable.cs @@ -74,8 +74,8 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx public byte[] SaveStateBinary() { - var ms = new MemoryStream(_savebuff2, true); - var bw = new BinaryWriter(ms); + using var ms = new MemoryStream(_savebuff2, true); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); if (ms.Position != _savebuff2.Length) diff --git a/BizHawk.Emulation.Cores/Consoles/Atari/lynx/Lynx.cs b/BizHawk.Emulation.Cores/Consoles/Atari/lynx/Lynx.cs index 24fb8a0095..72f42f4f66 100644 --- a/BizHawk.Emulation.Cores/Consoles/Atari/lynx/Lynx.cs +++ b/BizHawk.Emulation.Cores/Consoles/Atari/lynx/Lynx.cs @@ -27,8 +27,8 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx byte[] realfile = null; { - var ms = new MemoryStream(file, false); - var br = new BinaryReader(ms); + using var ms = new MemoryStream(file, false); + using var br = new BinaryReader(ms); string header = Encoding.ASCII.GetString(br.ReadBytes(4)); int p0 = br.ReadUInt16(); int p1 = br.ReadUInt16(); diff --git a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IStatable.cs index c5b767d73a..8770d66919 100644 --- a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IStatable.cs @@ -7,10 +7,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision { public partial class ColecoVision : IStatable { - public bool BinarySaveStatesPreferred - { - get { return true; } - } + public bool BinarySaveStatesPreferred => true; public void SaveStateText(TextWriter writer) { @@ -34,8 +31,8 @@ namespace BizHawk.Emulation.Cores.ColecoVision public byte[] SaveStateBinary() { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); @@ -46,7 +43,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision byte[] core = null; if (ser.IsWriter) { - var ms = new MemoryStream(); + using var ms = new MemoryStream(); ms.Close(); core = ms.ToArray(); } diff --git a/BizHawk.Emulation.Cores/Consoles/Magnavox/Odyssey2/O2Hawk.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Magnavox/Odyssey2/O2Hawk.IStatable.cs index ff886bc27e..cf87f77347 100644 --- a/BizHawk.Emulation.Cores/Consoles/Magnavox/Odyssey2/O2Hawk.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Magnavox/Odyssey2/O2Hawk.IStatable.cs @@ -31,8 +31,8 @@ namespace BizHawk.Emulation.Cores.Consoles.O2Hawk public byte[] SaveStateBinary() { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); @@ -43,7 +43,7 @@ namespace BizHawk.Emulation.Cores.Consoles.O2Hawk byte[] core = null; if (ser.IsWriter) { - var ms = new MemoryStream(); + using var ms = new MemoryStream(); ms.Close(); core = ms.ToArray(); } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.IStatable.cs index 0ea979f480..e28e042b68 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.IStatable.cs @@ -84,8 +84,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA public byte[] SaveStateBinary() { StartSaveStateBinaryInternal(); - var ms = new MemoryStream(_savebuff2, true); - var bw = new BinaryWriter(ms); + using var ms = new MemoryStream(_savebuff2, true); + using var bw = new BinaryWriter(ms); FinishSaveStateBinaryInternal(bw); bw.Flush(); ms.Close(); diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.IStatable.cs index 2e9128ecba..93afa3e795 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.IStatable.cs @@ -31,8 +31,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk public byte[] SaveStateBinary() { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); @@ -43,7 +43,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk byte[] core = null; if (ser.IsWriter) { - var ms = new MemoryStream(); + using var ms = new MemoryStream(); ms.Close(); core = ms.ToArray(); } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/N64/N64.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/N64/N64.IStatable.cs index 31434f2b0c..247e146f19 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/N64/N64.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/N64/N64.IStatable.cs @@ -9,7 +9,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64 { public partial class N64 : IStatable { - public bool BinarySaveStatesPreferred { get { return true; } } + public bool BinarySaveStatesPreferred => true; // these functions are all exact copy paste from gambatte. // if something's wrong here, it's probably wrong there too @@ -82,8 +82,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64 SaveStateBinaryPrivateBuff = new byte[lenwant]; } - var ms = new MemoryStream(SaveStateBinaryPrivateBuff); - var bw = new BinaryWriter(ms); + using var ms = new MemoryStream(SaveStateBinaryPrivateBuff); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs index aa3eb566ba..1307db41dc 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs @@ -658,7 +658,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES //create the board's rom and vrom if (iNesHeaderInfo != null) { - var ms = new MemoryStream(file, false); + using var ms = new MemoryStream(file, false); ms.Seek(16, SeekOrigin.Begin); // ines header //pluck the necessary bytes out of the file if (iNesHeaderInfo.trainer_size != 0) diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IStatable.cs index 6f6ff4298d..ffb3fedfd7 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IStatable.cs @@ -48,8 +48,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES public byte[] SaveStateBinary() { - var ms = new MemoryStream(); - var bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IStatable.cs index 1ba1712d05..d83575310c 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IStatable.cs @@ -3,15 +3,11 @@ using BizHawk.Common; using BizHawk.Emulation.Common; - namespace BizHawk.Emulation.Cores.Sega.MasterSystem { public partial class SMS : IStatable { - public bool BinarySaveStatesPreferred - { - get { return true; } - } + public bool BinarySaveStatesPreferred => true; public void SaveStateText(TextWriter writer) { @@ -35,8 +31,8 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem public byte[] SaveStateBinary() { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); return ms.ToArray(); @@ -47,13 +43,13 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem byte[] core = null; if (ser.IsWriter) { - var ms = new MemoryStream(); + using var ms = new MemoryStream(); ms.Close(); core = ms.ToArray(); } Cpu.SyncState(ser); - ser.BeginSection(nameof(SMS)); + ser.BeginSection(nameof(SMS)); Vdp.SyncState(ser); PSG.SyncState(ser); ser.Sync("RAM", ref SystemRam, false); diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGX.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGX.IStatable.cs index 7ea082d563..3cdd5cebca 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGX.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGX.IStatable.cs @@ -8,10 +8,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx { public partial class GPGX : IStatable { - public bool BinarySaveStatesPreferred - { - get { return true; } - } + public bool BinarySaveStatesPreferred => true; public void SaveStateText(TextWriter writer) { @@ -59,8 +56,8 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx public byte[] SaveStateBinary() { - var ms = new MemoryStream(); - var bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); ms.Close(); diff --git a/BizHawk.Emulation.Cores/Consoles/Sony/PSX/Octoshock.cs b/BizHawk.Emulation.Cores/Consoles/Sony/PSX/Octoshock.cs index 30f3bd3cac..c92103927c 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sony/PSX/Octoshock.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sony/PSX/Octoshock.cs @@ -1181,8 +1181,8 @@ namespace BizHawk.Emulation.Cores.Sony.PSX public byte[] SaveStateBinary() { //this are objectionable shenanigans, but theyre required to get the extra info in the stream. we need a better approach. - var ms = new MemoryStream(savebuff2, true); - var bw = new BinaryWriter(ms); + using var ms = new MemoryStream(savebuff2, true); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); if (ms.Position != savebuff2.Length) @@ -1191,10 +1191,7 @@ namespace BizHawk.Emulation.Cores.Sony.PSX return savebuff2; } - public bool BinarySaveStatesPreferred - { - get { return true; } - } + public bool BinarySaveStatesPreferred => true; #endregion diff --git a/BizHawk.Emulation.Cores/Consoles/WonderSwan/WonderSwan.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/WonderSwan/WonderSwan.IStatable.cs index eb25855adf..0abf83c15c 100644 --- a/BizHawk.Emulation.Cores/Consoles/WonderSwan/WonderSwan.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/WonderSwan/WonderSwan.IStatable.cs @@ -16,7 +16,7 @@ namespace BizHawk.Emulation.Cores.WonderSwan savebuff2 = new byte[savebuff.Length + 13]; } - JsonSerializer ser = new JsonSerializer() { Formatting = Formatting.Indented }; + JsonSerializer ser = new JsonSerializer { Formatting = Formatting.Indented }; [StructLayout(LayoutKind.Sequential)] class TextStateData @@ -88,8 +88,8 @@ namespace BizHawk.Emulation.Cores.WonderSwan public byte[] SaveStateBinary() { - var ms = new MemoryStream(savebuff2, true); - var bw = new BinaryWriter(ms); + using var ms = new MemoryStream(savebuff2, true); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); if (ms.Position != savebuff2.Length) @@ -98,9 +98,6 @@ namespace BizHawk.Emulation.Cores.WonderSwan return savebuff2; } - public bool BinarySaveStatesPreferred - { - get { return true; } - } + public bool BinarySaveStatesPreferred => true; } } diff --git a/BizHawk.Emulation.Cores/Waterbox/WaterboxCore.cs b/BizHawk.Emulation.Cores/Waterbox/WaterboxCore.cs index 11031bf693..cb572e335a 100644 --- a/BizHawk.Emulation.Cores/Waterbox/WaterboxCore.cs +++ b/BizHawk.Emulation.Cores/Waterbox/WaterboxCore.cs @@ -303,8 +303,8 @@ namespace BizHawk.Emulation.Cores.Waterbox public byte[] SaveStateBinary() { - var ms = new MemoryStream(); - var bw = new BinaryWriter(ms); + using var ms = new MemoryStream(); + using var bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); ms.Close();