diff --git a/src/BizHawk.Common/BinaryQuickSerializer.cs b/src/BizHawk.Common/BinaryQuickSerializer.cs index a4917cd737..77f410d61b 100644 --- a/src/BizHawk.Common/BinaryQuickSerializer.cs +++ b/src/BizHawk.Common/BinaryQuickSerializer.cs @@ -1,6 +1,4 @@ -#nullable disable - -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; @@ -17,15 +15,9 @@ namespace BizHawk.Common public static class BinaryQuickSerializer { private static MethodInfo FromExpression(Expression e) - { - var caller = e as MethodCallExpression; - if (caller == null) - { - throw new ArgumentException("Expression must be a method call"); - } - - return caller.Method; - } + => e is MethodCallExpression caller + ? caller.Method + : throw new ArgumentException("Expression must be a method call"); private static MethodInfo Method(Expression> f) { @@ -95,6 +87,13 @@ namespace BizHawk.Common public Type Type; public Reader Read; public Writer Write; + + public SerializationFactory(Type type, Reader read, Writer write) + { + Type = type; + Read = read; + Write = write; + } } private static SerializationFactory CreateFactory(Type t) @@ -153,12 +152,10 @@ namespace BizHawk.Common il.Emit(OpCodes.Ret); } - return new SerializationFactory - { - Type = t, - Read = (Reader)rmeth.CreateDelegate(typeof(Reader)), - Write = (Writer)wmeth.CreateDelegate(typeof(Writer)) - }; + return new SerializationFactory( + t, + (Reader) rmeth.CreateDelegate(typeof(Reader)), + (Writer) wmeth.CreateDelegate(typeof(Writer))); } private static readonly IDictionary Serializers = diff --git a/src/BizHawk.Common/Bit.cs b/src/BizHawk.Common/Bit.cs index 16b941d3d5..bd72ffe6b0 100644 --- a/src/BizHawk.Common/Bit.cs +++ b/src/BizHawk.Common/Bit.cs @@ -1,5 +1,4 @@ -#nullable disable - +using System; using System.Diagnostics; namespace BizHawk.Common @@ -82,9 +81,9 @@ namespace BizHawk.Common return _val.GetHashCode(); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { - return this == (Bit)obj; // this is probably wrong + return this == (Bit) (obj ?? throw new NullReferenceException()); // this is probably wrong } } } \ No newline at end of file diff --git a/src/BizHawk.Common/BitReverse.cs b/src/BizHawk.Common/BitReverse.cs index 9e803f00c1..870af5af79 100644 --- a/src/BizHawk.Common/BitReverse.cs +++ b/src/BizHawk.Common/BitReverse.cs @@ -1,25 +1,8 @@ -#nullable disable - -namespace BizHawk.Common +namespace BizHawk.Common { public static class BitReverse { static BitReverse() - { - MakeByte8(); - } - - public static byte[] Byte8; - - public static uint Reverse32(uint v) - { - return (uint)((Byte8[v & 0xff] << 24) | - (Byte8[(v >> 8) & 0xff] << 16) | - (Byte8[(v >> 16) & 0xff] << 8) | - Byte8[(v >> 24) & 0xff]); - } - - private static void MakeByte8() { int bits = 8; const int n = 1 << 8; @@ -42,5 +25,14 @@ namespace BizHawk.Common } } } + + public static readonly byte[] Byte8; + + public static uint Reverse32(uint v) => (uint) ( + (Byte8[v & 0xFF] << 24) + | (Byte8[(v >> 8) & 0xFF] << 16) + | (Byte8[(v >> 16) & 0xFF] << 8) + | Byte8[(v >> 24) & 0xFF] + ); } } diff --git a/src/BizHawk.Common/CRC32.cs b/src/BizHawk.Common/CRC32.cs index 84807cee87..564f63fe7b 100644 --- a/src/BizHawk.Common/CRC32.cs +++ b/src/BizHawk.Common/CRC32.cs @@ -1,6 +1,4 @@ -#nullable disable - -namespace BizHawk.Common +namespace BizHawk.Common { // we could get a little list of crcs from here and make it clear which crc this class was for, and expose others // http://www.ross.net/crc/download/crc_v3.txt diff --git a/src/BizHawk.Common/DeepEquality.cs b/src/BizHawk.Common/DeepEquality.cs index aafd1808e8..a3c17ad9ca 100644 --- a/src/BizHawk.Common/DeepEquality.cs +++ b/src/BizHawk.Common/DeepEquality.cs @@ -1,6 +1,4 @@ -#nullable disable - -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -32,7 +30,7 @@ namespace BizHawk.Common /// /// return all instance fields of a type /// - public static IEnumerable GetAllFields(Type t) + public static IEnumerable GetAllFields(Type? t) { while (t != null) { @@ -49,17 +47,20 @@ namespace BizHawk.Common } } - private static readonly MethodInfo ArrayEqualsGeneric = typeof(DeepEquality).GetMethod("ArrayEquals", BindingFlags.NonPublic | BindingFlags.Static); + private static readonly MethodInfo ArrayEqualsGeneric + = typeof(DeepEquality).GetMethod("ArrayEquals", BindingFlags.NonPublic | BindingFlags.Static) + ?? throw new NullReferenceException(); /// test if two objects and are equal, field-by-field (with deep inspection of each field) /// is an array with rank > 1 or is a non-zero-indexed array - public static bool DeepEquals(object o1, object o2) + public static bool DeepEquals(object? o1, object? o2) { if (o1 == o2) { // reference equal, so nothing else to be done return true; } + if (o1 == null || o2 == null) return false; // not equal (above) and one is null Type t1 = o1.GetType(); Type t2 = o2.GetType(); diff --git a/src/BizHawk.Common/Extensions/BufferExtensions.cs b/src/BizHawk.Common/Extensions/BufferExtensions.cs index b9f5f68f69..dbf7c3751f 100644 --- a/src/BizHawk.Common/Extensions/BufferExtensions.cs +++ b/src/BizHawk.Common/Extensions/BufferExtensions.cs @@ -1,6 +1,4 @@ -#nullable disable - -using System; +using System; using System.IO; using System.Text; using System.Security.Cryptography; diff --git a/src/BizHawk.Common/Extensions/NumberExtensions.cs b/src/BizHawk.Common/Extensions/NumberExtensions.cs index 6d86c46aac..5a59b74679 100644 --- a/src/BizHawk.Common/Extensions/NumberExtensions.cs +++ b/src/BizHawk.Common/Extensions/NumberExtensions.cs @@ -1,6 +1,4 @@ -#nullable disable - -using System; +using System; using System.Linq; namespace BizHawk.Common.NumberExtensions diff --git a/src/BizHawk.Common/Extensions/ReflectionExtensions.cs b/src/BizHawk.Common/Extensions/ReflectionExtensions.cs index 897f96b926..add6c18a84 100644 --- a/src/BizHawk.Common/Extensions/ReflectionExtensions.cs +++ b/src/BizHawk.Common/Extensions/ReflectionExtensions.cs @@ -1,6 +1,4 @@ -#nullable disable - -using System; +using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel; @@ -52,16 +50,8 @@ namespace BizHawk.Common.ReflectionExtensions /// public static string DisplayName(this Type type) { - var displayName = (DisplayNameAttribute)type - .GetCustomAttributes(typeof(DisplayNameAttribute), false) - .FirstOrDefault(); - - if (displayName != null) - { - return displayName.DisplayName; - } - - return type.Name; + var attr = type.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault(); + return attr is DisplayNameAttribute displayName ? displayName.DisplayName : type.Name; } /// @@ -70,15 +60,11 @@ namespace BizHawk.Common.ReflectionExtensions /// The description attribute value /// The type of the enum /// An enum value with the given description attribute, if no suitable description is found then a default value of the enum is returned - /// does not inherit /// implementation from https://stackoverflow.com/a/4367868/7467292 public static T GetEnumFromDescription(this string description) + where T : Enum { var type = typeof(T); - if (!type.IsEnum) - { - throw new InvalidOperationException(); - } foreach (var field in type.GetFields()) { @@ -99,7 +85,8 @@ namespace BizHawk.Common.ReflectionExtensions } } - return default(T); + var def = default(T); // does anyone know why Roslyn thinks this can evaluate to null? --yoshi + return def ?? throw new NullReferenceException(); } /// diff --git a/src/BizHawk.Common/Log.cs b/src/BizHawk.Common/Log.cs index 4608225881..7834c7633f 100644 --- a/src/BizHawk.Common/Log.cs +++ b/src/BizHawk.Common/Log.cs @@ -1,6 +1,4 @@ -#nullable disable - -using System; +using System; using System.Collections.Generic; using System.IO; @@ -60,13 +58,13 @@ namespace BizHawk.Common } // ============== Default Logger Action ============== - public static Stream HACK_LOG_STREAM; + public static Stream? HACK_LOG_STREAM; private static readonly bool LogToConsole = false; private static readonly bool LogToFile = false; private const string LogFilename = "bizhawk.txt"; - private static StreamWriter _writer; + private static StreamWriter? _writer; private static void DefaultLogger(string message) { @@ -75,13 +73,9 @@ namespace BizHawk.Common Console.WriteLine(message); } - if (LogToFile && _writer == null) - { - _writer = new StreamWriter(LogFilename); - } - if (LogToFile) { + _writer ??= new StreamWriter(LogFilename); _writer.WriteLine(message); _writer.Flush(); } diff --git a/src/BizHawk.Common/TempFileManager.cs b/src/BizHawk.Common/TempFileManager.cs index 03245563e4..44ff48318f 100644 --- a/src/BizHawk.Common/TempFileManager.cs +++ b/src/BizHawk.Common/TempFileManager.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Linq; using System.Collections.Generic; @@ -17,7 +15,7 @@ namespace BizHawk.Common { // TODO - manage paths other than %temp%, make not static, or allow adding multiple paths to static instance - public static string GetTempFilename(string friendlyName, string dotAndExtension = null, bool delete = true) + public static string GetTempFilename(string friendlyName, string? dotAndExtension = null, bool delete = true) { string guidPart = Guid.NewGuid().ToString(); var fname = $"biz-{System.Diagnostics.Process.GetCurrentProcess().Id}-{friendlyName}-{guidPart}{dotAndExtension ?? ""}"; @@ -33,7 +31,7 @@ namespace BizHawk.Common public static string RenameTempFilenameForDelete(string path) { string filename = Path.GetFileName(path); - string dir = Path.GetDirectoryName(path); + var dir = Path.GetDirectoryName(path) ?? throw new NullReferenceException(); if (!filename.StartsWith("biz-")) { throw new InvalidOperationException(); @@ -77,37 +75,35 @@ namespace BizHawk.Common foreach(var di in dis) { - FileInfo[] fis = null; + FileInfo[] fis; try { fis = di.GetFiles("bizdelete-*"); } catch { + continue; } - if (fis != null) + foreach (var fi in fis) { - foreach (var fi in fis) + try { - try + if (OSTailoredCode.IsUnixHost) { - if (OSTailoredCode.IsUnixHost) - { - fi.Delete(); // naive deletion, Mono doesn't care - } - else - { - Win32Imports.DeleteFileW(fi.FullName); // SHUT. UP. THE. EXCEPTIONS. - } + fi.Delete(); // naive deletion, Mono doesn't care } - catch + else { + Win32Imports.DeleteFileW(fi.FullName); // SHUT. UP. THE. EXCEPTIONS. } - - // try not to do more than one thing per frame - Thread.Sleep(100); } + catch + { + } + + // try not to do more than one thing per frame + Thread.Sleep(100); } } @@ -120,7 +116,7 @@ namespace BizHawk.Common { } - private static Thread thread; + private static Thread? thread; public static void HelperSetTempPath(string path) {