From a7db24490c6a2e22ae9c63377b779cbbe74ccef4 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Thu, 14 Jul 2022 03:20:42 +1000 Subject: [PATCH] Enable BHI1002 and fix noncompliance "Do not use anonymous types (classes)" --- Common.ruleset | 2 +- src/BizHawk.BizInvoke/BizExvoker.cs | 8 ++--- src/BizHawk.BizInvoke/BizInvoker.cs | 21 ++++---------- .../CallingConventionAdapter.cs | 2 +- src/BizHawk.Client.Common/RomLoader.cs | 12 ++------ .../movie/tasproj/TasBranch.cs | 29 ++++++++++++------- .../CustomControls/InputRoll/InputRoll.cs | 5 ++-- .../tools/Lua/Libraries/TAStudioLuaLibrary.cs | 10 ++++--- 8 files changed, 40 insertions(+), 49 deletions(-) diff --git a/Common.ruleset b/Common.ruleset index 67ebfd691b..eda23b84dc 100644 --- a/Common.ruleset +++ b/Common.ruleset @@ -5,7 +5,7 @@ - + diff --git a/src/BizHawk.BizInvoke/BizExvoker.cs b/src/BizHawk.BizInvoke/BizExvoker.cs index 4f4526b60d..2802d2bc95 100644 --- a/src/BizHawk.BizInvoke/BizExvoker.cs +++ b/src/BizHawk.BizInvoke/BizExvoker.cs @@ -59,12 +59,8 @@ namespace BizHawk.BizInvoke public DelegateStorage(Type type) { var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public) - .Select(m => new - { - Info = m, - Attr = m.GetCustomAttributes(true).OfType().FirstOrDefault() - }) - .Where(a => a.Attr != null); + .Select(static m => (Info: m, Attr: m.GetCustomAttributes(true).OfType().FirstOrDefault())) + .Where(static a => a.Attr is not null); var typeBuilder = ImplModuleBuilder.DefineType($"Bizhawk.BizExvokeHolder{type.Name}", TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed); diff --git a/src/BizHawk.BizInvoke/BizInvoker.cs b/src/BizHawk.BizInvoke/BizInvoker.cs index 892612411e..e05e58f86b 100644 --- a/src/BizHawk.BizInvoke/BizInvoker.cs +++ b/src/BizHawk.BizInvoke/BizInvoker.cs @@ -6,6 +6,7 @@ using System.Reflection.Emit; using System.Runtime.InteropServices; using System.Text; using BizHawk.Common; +using BizHawk.Common.CollectionExtensions; namespace BizHawk.BizInvoke { @@ -167,11 +168,7 @@ namespace BizHawk.BizInvoke } var baseMethods = baseType.GetMethods(BindingFlags.Instance | BindingFlags.Public) - .Select(m => new - { - Info = m, - Attr = m.GetCustomAttributes(true).OfType().FirstOrDefault() - }) + .Select(static m => (Info: m, Attr: m.GetCustomAttributes(true).OfType().FirstOrDefault())) .Where(a => a.Attr != null) .ToList(); @@ -181,19 +178,13 @@ namespace BizHawk.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!"); - } + var uo = baseMethods.FirstOrNull(static a => !a.Info.IsVirtual || a.Info.IsFinal); + if (uo is not null) throw new InvalidOperationException($"Method {uo.Value.Info.Name} cannot be overriden!"); // there's no technical reason to disallow this, but we wouldn't be doing anything // with the base implementation, so it's probably a user error - var na = baseMethods.FirstOrDefault(a => !a.Info.IsAbstract); - if (na != null) - { - throw new InvalidOperationException($"Method {na.Info.Name} is not abstract!"); - } + var na = baseMethods.FirstOrNull(static a => !a.Info.IsAbstract); + if (na is not null) throw new InvalidOperationException($"Method {na.Value.Info.Name} is not abstract!"); } // hooks that will be run on the created proxy object diff --git a/src/BizHawk.BizInvoke/CallingConventionAdapter.cs b/src/BizHawk.BizInvoke/CallingConventionAdapter.cs index ccc3fc2eb5..10d5660897 100644 --- a/src/BizHawk.BizInvoke/CallingConventionAdapter.cs +++ b/src/BizHawk.BizInvoke/CallingConventionAdapter.cs @@ -177,7 +177,7 @@ namespace BizHawk.BizInvoke { if (slots != null) { - _slots = slots.Select((cb, i) => new { cb, i }) + _slots = slots.Select(static (cb, i) => (cb, i)) .ToDictionary(a => a.cb, a => a.i, new ReferenceEqualityComparer()); } _waterboxHost = waterboxHost; diff --git a/src/BizHawk.Client.Common/RomLoader.cs b/src/BizHawk.Client.Common/RomLoader.cs index 75ff7c1b54..3541ee402b 100644 --- a/src/BizHawk.Client.Common/RomLoader.cs +++ b/src/BizHawk.Client.Common/RomLoader.cs @@ -335,11 +335,7 @@ namespace BizHawk.Client.Common var discs = m3u.Entries .Select(e => e.Path) .Where(p => Disc.IsValidExtension(Path.GetExtension(p))) - .Select(path => new - { - d = DiscExtensions.CreateAnyType(path, str => DoLoadErrorCallback(str, "???", LoadErrorType.DiscError)), - p = path, - }) + .Select(path => (p: path, d: DiscExtensions.CreateAnyType(path, str => DoLoadErrorCallback(str, "???", LoadErrorType.DiscError)))) .Where(a => a.d != null) .Select(a => (IDiscAsset)new DiscAsset { @@ -540,11 +536,7 @@ namespace BizHawk.Client.Common .ToList(), Discs = xmlGame.AssetFullPaths .Where(p => Disc.IsValidExtension(Path.GetExtension(p))) - .Select(path => new - { - d = DiscExtensions.CreateAnyType(path, str => DoLoadErrorCallback(str, system, LoadErrorType.DiscError)), - p = path, - }) + .Select(path => (p: path, d: DiscExtensions.CreateAnyType(path, str => DoLoadErrorCallback(str, system, LoadErrorType.DiscError)))) .Where(a => a.d != null) .Select(a => (IDiscAsset)new DiscAsset { diff --git a/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs b/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs index d12bde45af..fdead917ab 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs @@ -9,6 +9,22 @@ namespace BizHawk.Client.Common { public class TasBranch { + internal struct ForSerialization + { + public readonly int Frame; + + public readonly DateTime TimeStamp; + + public readonly Guid UniqueIdentifier; + + public ForSerialization(int frame, DateTime timeStamp, Guid uniqueIdentifier) + { + Frame = frame; + TimeStamp = timeStamp; + UniqueIdentifier = uniqueIdentifier; + } + } + public int Frame { get; set; } public byte[] CoreData { get; set; } public IStringLog InputLog { get; set; } @@ -20,6 +36,8 @@ namespace BizHawk.Client.Common public Guid Uuid { get; set; } public string UserText { get; set; } + internal ForSerialization ForSerial => new(Frame, TimeStamp, Uuid); + public TasBranch Clone() => (TasBranch)MemberwiseClone(); } @@ -123,16 +141,7 @@ namespace BizHawk.Client.Common var nusertext = new IndexedStateLump(BinaryStateLump.BranchUserText); foreach (var b in this) { - bs.PutLump(nheader, tw => - { - // if this header needs more stuff in it, handle it sensibly - tw.WriteLine(JsonConvert.SerializeObject(new - { - b.Frame, - b.TimeStamp, - UniqueIdentifier = b.Uuid - })); - }); + bs.PutLump(nheader, tw => tw.WriteLine(JsonConvert.SerializeObject(b.ForSerial))); bs.PutLump(ncore, (Stream s) => s.Write(b.CoreData, 0, b.CoreData.Length)); diff --git a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs index 3d79b2383d..3d64bf144c 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs @@ -8,6 +8,7 @@ using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Client.EmuHawk.CustomControls; using BizHawk.Common; +using BizHawk.Common.CollectionExtensions; // TODO: There are some bad interactions between ScrollToIndex and MakeIndexVisible that are preventing things from working as intended. // But, the current behaviour is ok for now for what it is used for. @@ -1891,8 +1892,8 @@ namespace BizHawk.Client.EmuHawk { if (_horizontalOrientation) { - return _columns.VisibleColumns.Select((n, i) => new { Column = n, Index = i }) - .FirstOrDefault(item => (GetHColTop(item.Index) - _vBar.Value).RangeTo(GetHColBottom(item.Index) - _vBar.Value).Contains(pixel)) + return _columns.VisibleColumns.Select(static (n, i) => (Column: n, Index: i)) + .FirstOrNull(item => (GetHColTop(item.Index) - _vBar.Value).RangeTo(GetHColBottom(item.Index) - _vBar.Value).Contains(pixel)) ?.Column; } return _columns.VisibleColumns.FirstOrDefault(column => (column.Left - _hBar.Value).RangeTo(column.Right - _hBar.Value).Contains(pixel)); diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/TAStudioLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/TAStudioLuaLibrary.cs index 89abd80fbf..3aff6f7873 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/TAStudioLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/TAStudioLuaLibrary.cs @@ -382,11 +382,13 @@ namespace BizHawk.Client.EmuHawk { if (!Engaged()) return _th.CreateTable(); return _th.EnumerateToLuaTable( - Tastudio.CurrentTasMovie.Branches.Select(b => new + Tastudio.CurrentTasMovie.Branches.Select(b => { - Id = b.Uuid.ToString(), - b.Frame, - Text = UnFixString(b.UserText) + var table = _th.CreateTable(); + table["Id"] = b.Uuid.ToString(); + table["Frame"] = b.Frame; + table["Text"] = UnFixString(b.UserText); + return table; }), indexFrom: 0); }