From 20b3112284f03c37b653f48795d14773deedfdf6 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Wed, 20 Mar 2019 15:13:04 +1000 Subject: [PATCH] Use string interpolation Excludes Sprintf.cs --- BizHawk.Common/BinaryQuickSerializer.cs | 8 ++++---- BizHawk.Common/BizInvoke/BizExvoker.cs | 3 +-- BizHawk.Common/BizInvoke/BizInvokeUtilities.cs | 4 ++-- BizHawk.Common/BizInvoke/BizInvoker.cs | 10 +++++----- BizHawk.Common/Extensions/BufferExtensions.cs | 6 +++--- BizHawk.Common/Extensions/NumberExtensions.cs | 12 ++++++------ BizHawk.Common/HawkFile.cs | 4 ++-- BizHawk.Common/InstanceDll.cs | 4 ++-- BizHawk.Common/NDBDatabase.cs | 4 ++-- BizHawk.Common/Serializer.cs | 4 ++-- BizHawk.Common/SettingsUtil.cs | 4 ++-- BizHawk.Common/TempFileManager.cs | 2 +- BizHawk.Common/Util.cs | 6 +++--- BizHawk.Common/Win32Hacks.cs | 2 +- 14 files changed, 36 insertions(+), 37 deletions(-) diff --git a/BizHawk.Common/BinaryQuickSerializer.cs b/BizHawk.Common/BinaryQuickSerializer.cs index 7db5441720..421461c328 100644 --- a/BizHawk.Common/BinaryQuickSerializer.cs +++ b/BizHawk.Common/BinaryQuickSerializer.cs @@ -117,8 +117,8 @@ namespace BizHawk.Common .OrderBy(fi => (int)Marshal.OffsetOf(t, fi.Name)) .ToList(); - var rmeth = new DynamicMethod(t.Name + "_r", null, new[] { typeof(object), typeof(BinaryReader) }, true); - var wmeth = new DynamicMethod(t.Name + "_w", null, new[] { typeof(object), typeof(BinaryWriter) }, true); + var rmeth = new DynamicMethod($"{t.Name}_r", null, new[] { typeof(object), typeof(BinaryReader) }, true); + var wmeth = new DynamicMethod($"{t.Name}_w", null, new[] { typeof(object), typeof(BinaryWriter) }, true); { var il = rmeth.GetILGenerator(); @@ -134,7 +134,7 @@ namespace BizHawk.Common MethodInfo m; if (!Readhandlers.TryGetValue(field.FieldType, out m)) { - throw new InvalidOperationException("(R) Can't handle nested type " + field.FieldType); + throw new InvalidOperationException($"(R) Can't handle nested type {field.FieldType}"); } il.Emit(OpCodes.Callvirt, m); @@ -159,7 +159,7 @@ namespace BizHawk.Common MethodInfo m; if (!Writehandlers.TryGetValue(field.FieldType, out m)) { - throw new InvalidOperationException("(W) Can't handle nested type " + field.FieldType); + throw new InvalidOperationException($"(W) Can't handle nested type {field.FieldType}"); } il.Emit(OpCodes.Callvirt, m); diff --git a/BizHawk.Common/BizInvoke/BizExvoker.cs b/BizHawk.Common/BizInvoke/BizExvoker.cs index 120025f9cd..f5944e7586 100644 --- a/BizHawk.Common/BizInvoke/BizExvoker.cs +++ b/BizHawk.Common/BizInvoke/BizExvoker.cs @@ -67,8 +67,7 @@ namespace BizHawk.Common.BizInvoke .Where(a => a.Attr != null) .ToList(); - var typeBuilder = ImplModuleBuilder.DefineType( - "Bizhawk.BizExvokeHolder" + type.Name, TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed); + var typeBuilder = ImplModuleBuilder.DefineType($"Bizhawk.BizExvokeHolder{type.Name}", TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed); foreach (var a in methods) { diff --git a/BizHawk.Common/BizInvoke/BizInvokeUtilities.cs b/BizHawk.Common/BizInvoke/BizInvokeUtilities.cs index 6bb1436d1b..55a3c9feab 100644 --- a/BizHawk.Common/BizInvoke/BizInvokeUtilities.cs +++ b/BizHawk.Common/BizInvoke/BizInvokeUtilities.cs @@ -27,7 +27,7 @@ namespace BizHawk.Common.BizInvoke // create the delegate type var delegateType = enclosingType.DefineNestedType( - "DelegateType" + method.Name, + $"DelegateType{method.Name}", TypeAttributes.Class | TypeAttributes.NestedPrivate | TypeAttributes.Sealed, typeof(MulticastDelegate)); @@ -85,7 +85,7 @@ namespace BizHawk.Common.BizInvoke return new CustomAttributeBuilder(t.GetConstructor(Type.EmptyTypes), new object[0]); } - throw new InvalidOperationException("Unknown parameter attribute " + t.Name); + throw new InvalidOperationException($"Unknown parameter attribute {t.Name}"); } } } diff --git a/BizHawk.Common/BizInvoke/BizInvoker.cs b/BizHawk.Common/BizInvoke/BizInvoker.cs index 8ebdf321a3..5f47b7ffa8 100644 --- a/BizHawk.Common/BizInvoke/BizInvoker.cs +++ b/BizHawk.Common/BizInvoke/BizInvoker.cs @@ -141,7 +141,7 @@ namespace BizHawk.Common.BizInvoke var uo = baseMethods.FirstOrDefault(a => !a.Info.IsVirtual || a.Info.IsFinal); if (uo != null) { - throw new InvalidOperationException("Method " + uo.Info.Name + " cannot be overriden!"); + throw new InvalidOperationException($"Method {uo.Info.Name} cannot be overriden!"); } // there's no technical reason to disallow this, but we wouldn't be doing anything @@ -149,14 +149,14 @@ namespace BizHawk.Common.BizInvoke var na = baseMethods.FirstOrDefault(a => !a.Info.IsAbstract); if (na != null) { - throw new InvalidOperationException("Method " + na.Info.Name + " is not abstract!"); + throw new InvalidOperationException($"Method {na.Info.Name} is not abstract!"); } } // hooks that will be run on the created proxy object var postCreateHooks = new List>(); - var type = ImplModuleBuilder.DefineType("Bizhawk.BizInvokeProxy" + baseType.Name, TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed, baseType); + var type = ImplModuleBuilder.DefineType($"Bizhawk.BizInvokeProxy{baseType.Name}", TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed, baseType); var monitorField = monitor ? type.DefineField("MonitorField", typeof(IMonitor), FieldAttributes.Public) : null; @@ -210,7 +210,7 @@ namespace BizHawk.Common.BizInvoke // define a field on the class to hold the delegate var field = type.DefineField( - "DelegateField" + baseMethod.Name, + $"DelegateField{baseMethod.Name}", delegateType, FieldAttributes.Public); @@ -293,7 +293,7 @@ namespace BizHawk.Common.BizInvoke // define a field on the type to hold the entry pointer var field = type.DefineField( - "EntryPtrField" + baseMethod.Name, + $"EntryPtrField{baseMethod.Name}", typeof(IntPtr), FieldAttributes.Public); diff --git a/BizHawk.Common/Extensions/BufferExtensions.cs b/BizHawk.Common/Extensions/BufferExtensions.cs index e04788f445..d02ded6d2e 100644 --- a/BizHawk.Common/Extensions/BufferExtensions.cs +++ b/BizHawk.Common/Extensions/BufferExtensions.cs @@ -100,7 +100,7 @@ namespace BizHawk.Common.BufferExtensions for (int i = 0; i < buffer.Length && i * 2 < hex.Length; i++) { - var bytehex = "" + hex[i * 2] + hex[(i * 2) + 1]; + var bytehex = $"{hex[i * 2]}{hex[(i * 2) + 1]}"; buffer[i] = byte.Parse(bytehex, NumberStyles.HexNumber); } } @@ -136,7 +136,7 @@ namespace BizHawk.Common.BufferExtensions for (int i = 0; i < buffer.Length && i * 4 < hex.Length; i++) { - var shorthex = "" + hex[i * 4] + hex[(i * 4) + 1] + hex[(i * 4) + 2] + hex[(i * 4) + 3]; + var shorthex = $"{hex[i * 4]}{hex[(i * 4) + 1]}{hex[(i * 4) + 2]}{hex[(i * 4) + 3]}"; buffer[i] = short.Parse(shorthex, NumberStyles.HexNumber); } } @@ -150,7 +150,7 @@ namespace BizHawk.Common.BufferExtensions for (int i = 0; i < buffer.Length && i * 4 < hex.Length; i++) { - var ushorthex = "" + hex[i * 4] + hex[(i * 4) + 1] + hex[(i * 4) + 2] + hex[(i * 4) + 3]; + var ushorthex = $"{hex[i * 4]}{hex[(i * 4) + 1]}{hex[(i * 4) + 2]}{hex[(i * 4) + 3]}"; buffer[i] = ushort.Parse(ushorthex, NumberStyles.HexNumber); } } diff --git a/BizHawk.Common/Extensions/NumberExtensions.cs b/BizHawk.Common/Extensions/NumberExtensions.cs index 38438659a5..f0a7309902 100644 --- a/BizHawk.Common/Extensions/NumberExtensions.cs +++ b/BizHawk.Common/Extensions/NumberExtensions.cs @@ -7,32 +7,32 @@ namespace BizHawk.Common.NumberExtensions { public static string ToHexString(this int n, int numdigits) { - return string.Format("{0:X" + numdigits + "}", n); + return string.Format($"{{0:X{numdigits}}}", n); } public static string ToHexString(this uint n, int numdigits) { - return string.Format("{0:X" + numdigits + "}", n); + return string.Format($"{{0:X{numdigits}}}", n); } public static string ToHexString(this byte n, int numdigits) { - return string.Format("{0:X" + numdigits + "}", n); + return string.Format($"{{0:X{numdigits}}}", n); } public static string ToHexString(this ushort n, int numdigits) { - return string.Format("{0:X" + numdigits + "}", n); + return string.Format($"{{0:X{numdigits}}}", n); } public static string ToHexString(this long n, int numdigits) { - return string.Format("{0:X" + numdigits + "}", n); + return string.Format($"{{0:X{numdigits}}}", n); } public static string ToHexString(this ulong n, int numdigits) { - return string.Format("{0:X" + numdigits + "}", n); + return string.Format($"{{0:X{numdigits}}}", n); } public static bool Bit(this byte b, int index) diff --git a/BizHawk.Common/HawkFile.cs b/BizHawk.Common/HawkFile.cs index b4b89e3ceb..6dc5a44bd1 100644 --- a/BizHawk.Common/HawkFile.cs +++ b/BizHawk.Common/HawkFile.cs @@ -334,7 +334,7 @@ namespace BizHawk.Common _extractor.ExtractFile(archiveIndex, _boundStream); _boundStream.Position = 0; _memberPath = _archiveItems[index].Name; // TODO - maybe go through our own list of names? maybe not, its indexes dont match.. - Console.WriteLine("HawkFile bound " + CanonicalFullPath); + Console.WriteLine($"HawkFile bound {CanonicalFullPath}"); _boundIndex = archiveIndex; return this; } @@ -360,7 +360,7 @@ namespace BizHawk.Common private void BindRoot() { _boundStream = _rootStream; - Console.WriteLine("HawkFile bound " + CanonicalFullPath); + Console.WriteLine($"HawkFile bound {CanonicalFullPath}"); } /// diff --git a/BizHawk.Common/InstanceDll.cs b/BizHawk.Common/InstanceDll.cs index 3bd1212e56..8a7b558e73 100644 --- a/BizHawk.Common/InstanceDll.cs +++ b/BizHawk.Common/InstanceDll.cs @@ -12,7 +12,7 @@ namespace BizHawk.Common public InstanceDll(string dllPath) { // copy the dll to a temp directory - var path = TempFileManager.GetTempFilename(string.Format("{0}", Path.GetFileNameWithoutExtension(dllPath)),".dll",false); + var path = TempFileManager.GetTempFilename(Path.GetFileNameWithoutExtension(dllPath), ".dll", false); using (var stream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.ReadWrite | FileShare.Delete, 4 * 1024, FileOptions.None)) using (var sdll = File.OpenRead(dllPath)) sdll.CopyTo(stream); @@ -24,7 +24,7 @@ namespace BizHawk.Common var envpath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process); try { - string envpath_new = Path.GetDirectoryName(path) + ";" + envpath; + string envpath_new = $"{Path.GetDirectoryName(path)};{envpath}"; Environment.SetEnvironmentVariable("PATH", envpath_new, EnvironmentVariableTarget.Process); _hModule = LoadLibrary(path); //consider using LoadLibraryEx instead of shenanigans? if (_hModule == IntPtr.Zero) diff --git a/BizHawk.Common/NDBDatabase.cs b/BizHawk.Common/NDBDatabase.cs index 3dd2b60966..75334a9300 100644 --- a/BizHawk.Common/NDBDatabase.cs +++ b/BizHawk.Common/NDBDatabase.cs @@ -107,10 +107,10 @@ namespace BizHawk.Common public void Store(string name, byte[] buf, int offset, int length) { if (Items.ContainsKey(name)) - throw new InvalidOperationException(string.Format("Can't add already existing key of name {0}", name)); + throw new InvalidOperationException($"Can't add already existing key of name {name}"); if (length > Remain) - throw new OutOfMemoryException(string.Format("Insufficient storage reserved for {0} bytes", length)); + throw new OutOfMemoryException($"Insufficient storage reserved for {length} bytes"); long todo = length; int src = offset; diff --git a/BizHawk.Common/Serializer.cs b/BizHawk.Common/Serializer.cs index 0a882f6f6c..f50a4127a3 100644 --- a/BizHawk.Common/Serializer.cs +++ b/BizHawk.Common/Serializer.cs @@ -740,7 +740,7 @@ namespace BizHawk.Common } else { - throw new Exception(string.Format("Duplicate key \"{0}\" in serializer savestate!", name)); + throw new Exception($"Duplicate key \"{name}\" in serializer savestate!"); } curs = news; @@ -763,7 +763,7 @@ namespace BizHawk.Common } else { - throw new Exception(string.Format("Duplicate key \"{0}\" in serializer savestate!", key)); + throw new Exception($"Duplicate key \"{key}\" in serializer savestate!"); } } } diff --git a/BizHawk.Common/SettingsUtil.cs b/BizHawk.Common/SettingsUtil.cs index 6741bf7d88..6a8e940692 100644 --- a/BizHawk.Common/SettingsUtil.cs +++ b/BizHawk.Common/SettingsUtil.cs @@ -50,7 +50,7 @@ namespace BizHawk.Common private static DefaultValueSetter CreateSetter(Type t) { - var dyn = new DynamicMethod("SetDefaultValues_" + t.Name, null, new[] { typeof(object), typeof(object[]) }, false); + var dyn = new DynamicMethod($"SetDefaultValues_{t.Name}", null, new[] { typeof(object), typeof(object[]) }, false); var il = dyn.GetILGenerator(); List DefaultValues = new List(); @@ -90,7 +90,7 @@ namespace BizHawk.Common } else { - throw new InvalidOperationException(string.Format("Default value assignment will fail for {0}.{1}", t.Name, prop.Name)); + throw new InvalidOperationException($"Default value assignment will fail for {t.Name}.{prop.Name}"); } il.Emit(OpCodes.Callvirt, method); } diff --git a/BizHawk.Common/TempFileManager.cs b/BizHawk.Common/TempFileManager.cs index 255ab47a66..d21da556bd 100644 --- a/BizHawk.Common/TempFileManager.cs +++ b/BizHawk.Common/TempFileManager.cs @@ -37,7 +37,7 @@ namespace BizHawk.Common throw new InvalidOperationException(); } - filename = "bizdelete-" + filename.Remove(0, 4); + filename = $"bizdelete-{filename.Remove(0, 4)}"; return Path.Combine(dir, filename); } diff --git a/BizHawk.Common/Util.cs b/BizHawk.Common/Util.cs index 9869e60e93..c0d6f94185 100644 --- a/BizHawk.Common/Util.cs +++ b/BizHawk.Common/Util.cs @@ -389,7 +389,7 @@ namespace BizHawk.Common } const string precision = "2"; - return string.Format("{0:N" + precision + "}{1}", size, suffix); + return string.Format($"{{0:N{precision}}}{{1}}", size, suffix); } // http://stackoverflow.com/questions/3928822/comparing-2-dictionarystring-string-instances @@ -510,7 +510,7 @@ namespace BizHawk.Common static SuperGloballyUniqueID() { - StaticPart = "bizhawk-" + System.Diagnostics.Process.GetCurrentProcess().Id + "-" + Guid.NewGuid(); + StaticPart = $"bizhawk-{System.Diagnostics.Process.GetCurrentProcess().Id}-{Guid.NewGuid()}"; } public static string Next() @@ -521,7 +521,7 @@ namespace BizHawk.Common myctr = ctr++; } - return StaticPart + "-" + myctr; + return $"{StaticPart}-{myctr}"; } } diff --git a/BizHawk.Common/Win32Hacks.cs b/BizHawk.Common/Win32Hacks.cs index fd49816c80..77960636ff 100644 --- a/BizHawk.Common/Win32Hacks.cs +++ b/BizHawk.Common/Win32Hacks.cs @@ -456,7 +456,7 @@ namespace BizHawk.Common //I only put this for use here by external cores public static void RemoveMOTW(string path) { - DeleteFileW(path + ":Zone.Identifier"); + DeleteFileW($"{path}:Zone.Identifier"); } [DllImport("kernel32.dll")]