From 0e8d1c136f9533e2fac97feab16807a9495eebf5 Mon Sep 17 00:00:00 2001 From: Andreas Pardeike Date: Tue, 23 May 2023 18:29:10 +0200 Subject: [PATCH] cleanup before 2.3 release --- .editorconfig | 70 +++++++++++++++++++ Harmony.sln | 1 - Harmony/Documentation/Documentation.csproj | 2 +- Harmony/Documentation/GlobalSuppressions.cs | 29 ++++---- .../Documentation/examples/execution_with.cs | 4 +- Harmony/Extras/FastAccess.cs | 4 +- Harmony/GlobalSuppressions.cs | 28 +++++--- Harmony/Harmony.csproj | 5 +- Harmony/Internal/CodeTranspiler.cs | 16 ++--- Harmony/Internal/Emitter.cs | 4 +- Harmony/Internal/MethodCopier.cs | 10 +-- Harmony/Internal/MethodPatcher.cs | 29 +++----- Harmony/Internal/PatchArgumentExtensions.cs | 12 ++-- Harmony/Internal/PatchFunctions.cs | 2 +- Harmony/Internal/PatchSorter.cs | 6 +- Harmony/Internal/PatchTools.cs | 2 +- Harmony/Public/Harmony.cs | 8 +-- Harmony/Public/HarmonyMethod.cs | 16 ++--- Harmony/Public/Patch.cs | 2 +- Harmony/Public/PatchClassProcessor.cs | 23 +++--- Harmony/Public/PatchProcessor.cs | 6 +- Harmony/Public/Patches.cs | 8 +-- Harmony/Tools/AccessTools.cs | 33 ++++----- Harmony/Tools/CodeMatcher.cs | 12 +--- Harmony/Tools/Extensions.cs | 8 +-- Harmony/Tools/Tools.cs | 2 +- Harmony/Tools/Traverse.cs | 21 +++--- HarmonyTests/GlobalSuppressions.cs | 29 ++++---- HarmonyTests/HarmonyTests.csproj | 2 +- HarmonyTests/IL/DynamicArgumentPatches.cs | 2 +- HarmonyTests/IL/TestMethodBodyReader.cs | 2 +- HarmonyTests/Patching/Assets/PatchClasses.cs | 8 +-- HarmonyTests/Patching/Assets/Specials.cs | 6 +- HarmonyTests/Patching/GenericsPatches.cs | 6 +- HarmonyTests/Patching/Transpiling.cs | 2 +- HarmonyTests/Tools/TestAccessTools.cs | 2 +- HarmonyTests/Tools/TestGettingInstructions.cs | 2 +- 37 files changed, 243 insertions(+), 181 deletions(-) diff --git a/.editorconfig b/.editorconfig index fc9200a9..6967d7e7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -71,3 +71,73 @@ csharp_space_between_method_call_parameter_list_parentheses = false csharp_space_between_parentheses = false csharp_preserve_single_line_statements = true csharp_preserve_single_line_blocks = true +csharp_using_directive_placement = outside_namespace:silent +csharp_prefer_simple_using_statement = true:warning +csharp_style_namespace_declarations = block_scoped:silent +csharp_style_prefer_method_group_conversion = true:silent +csharp_style_prefer_top_level_statements = true:silent +csharp_style_expression_bodied_lambdas = when_on_single_line:silent +csharp_style_expression_bodied_local_functions = when_on_single_line:silent + +[*.{cs,vb}] +#### Naming styles #### + +# Naming rules + +dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion +dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface +dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i + +dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.types_should_be_pascal_case.symbols = types +dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case + +dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members +dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case + +# Symbol specifications + +dotnet_naming_symbols.interface.applicable_kinds = interface +dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.interface.required_modifiers = + +dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum +dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.types.required_modifiers = + +dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method +dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.non_field_members.required_modifiers = + +# Naming styles + +dotnet_naming_style.begins_with_i.required_prefix = I +dotnet_naming_style.begins_with_i.required_suffix = +dotnet_naming_style.begins_with_i.word_separator = +dotnet_naming_style.begins_with_i.capitalization = pascal_case + +dotnet_naming_style.pascal_case.required_prefix = +dotnet_naming_style.pascal_case.required_suffix = +dotnet_naming_style.pascal_case.word_separator = +dotnet_naming_style.pascal_case.capitalization = pascal_case + +dotnet_naming_style.pascal_case.required_prefix = +dotnet_naming_style.pascal_case.required_suffix = +dotnet_naming_style.pascal_case.word_separator = +dotnet_naming_style.pascal_case.capitalization = pascal_case +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion +dotnet_style_prefer_auto_properties = true:silent +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_prefer_simplified_boolean_expressions = true:suggestion +dotnet_style_prefer_conditional_expression_over_assignment = true:silent +dotnet_style_prefer_conditional_expression_over_return = true:silent +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_operator_placement_when_wrapping = beginning_of_line +tab_width = 3 +indent_size = 3 diff --git a/Harmony.sln b/Harmony.sln index 32b4cdf8..ca26eb78 100644 --- a/Harmony.sln +++ b/Harmony.sln @@ -16,7 +16,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Directory.Build.props = Directory.Build.props global.json = global.json LICENSE = LICENSE - nuget.config = nuget.config README.md = README.md EndProjectSection EndProject diff --git a/Harmony/Documentation/Documentation.csproj b/Harmony/Documentation/Documentation.csproj index 56dd720b..d1b1ac8b 100644 --- a/Harmony/Documentation/Documentation.csproj +++ b/Harmony/Documentation/Documentation.csproj @@ -27,7 +27,7 @@ - + diff --git a/Harmony/Documentation/GlobalSuppressions.cs b/Harmony/Documentation/GlobalSuppressions.cs index 45ee5e7c..d023e715 100644 --- a/Harmony/Documentation/GlobalSuppressions.cs +++ b/Harmony/Documentation/GlobalSuppressions.cs @@ -1,21 +1,22 @@ using System.Diagnostics.CodeAnalysis; -[assembly: SuppressMessage("Design", "CA1034:Nested types should not be visible")] -[assembly: SuppressMessage("Design", "CA1052:Static holder types should be Static or NotInheritable")] -[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types")] +[assembly: SuppressMessage("Design", "CA1034")] +[assembly: SuppressMessage("Design", "CA1052")] +[assembly: SuppressMessage("Design", "CA1031")] -[assembly: SuppressMessage("Style", "IDE0058:Expression value is never used")] -[assembly: SuppressMessage("Style", "IDE0025:Use expression body for properties")] -[assembly: SuppressMessage("Style", "IDE0060:Remove unused parameter")] -[assembly: SuppressMessage("Style", "IDE0044:Add readonly modifier")] -[assembly: SuppressMessage("Style", "IDE0062:")] -[assembly: SuppressMessage("Style", "IDE0059:Unnecessary assignment of a value")] +[assembly: SuppressMessage("Style", "IDE0058")] +[assembly: SuppressMessage("Style", "IDE0025")] +[assembly: SuppressMessage("Style", "IDE0060")] +[assembly: SuppressMessage("Style", "IDE0044")] +[assembly: SuppressMessage("Style", "IDE0062")] +[assembly: SuppressMessage("Style", "IDE0059")] -[assembly: SuppressMessage("Usage", "CA2211:Non-constant fields should not be visible")] +[assembly: SuppressMessage("Usage", "CA2211")] -[assembly: SuppressMessage("CodeQuality", "IDE0051:Remove unused private members")] +[assembly: SuppressMessage("CodeQuality", "IDE0051")] -[assembly: SuppressMessage("Performance", "CA1822:Mark members as static")] -[assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results")] +[assembly: SuppressMessage("Performance", "CA1822")] +[assembly: SuppressMessage("Performance", "CA1806")] -[assembly: SuppressMessage("", "CS0649:")] +[assembly: SuppressMessage("", "CS0649")] +[assembly: SuppressMessage("", "IDE0251")] diff --git a/Harmony/Documentation/examples/execution_with.cs b/Harmony/Documentation/examples/execution_with.cs index 46083ea3..ba1e6f9c 100644 --- a/Harmony/Documentation/examples/execution_with.cs +++ b/Harmony/Documentation/examples/execution_with.cs @@ -36,7 +36,7 @@ static R ReplacementMethod(T optionalThisArgument /*, ... arguments ... */ ) ex = EditFinalizer(ex, ref result); finalized = true; - if (ex is object) throw ex; + if (ex is not null) throw ex; return result; } catch (Exception e) @@ -61,7 +61,7 @@ static R ReplacementMethod(T optionalThisArgument /*, ... arguments ... */ ) else { // alternative 2: at least one non-void finalizer - if (ex is object) throw ex; + if (ex is not null) throw ex; } return result; diff --git a/Harmony/Extras/FastAccess.cs b/Harmony/Extras/FastAccess.cs index 49a3d1d8..b746fc5d 100644 --- a/Harmony/Extras/FastAccess.cs +++ b/Harmony/Extras/FastAccess.cs @@ -110,11 +110,11 @@ public static GetterHandler CreateFieldGetter(params string[] names) foreach (var name in names) { var field = typeof(T).GetField(name, AccessTools.all); - if (field is object) + if (field is not null) return CreateGetterHandler(field); var property = typeof(T).GetProperty(name, AccessTools.all); - if (property is object) + if (property is not null) return CreateGetterHandler(property); } diff --git a/Harmony/GlobalSuppressions.cs b/Harmony/GlobalSuppressions.cs index 5c488251..847e4bec 100644 --- a/Harmony/GlobalSuppressions.cs +++ b/Harmony/GlobalSuppressions.cs @@ -1,11 +1,21 @@ using System.Diagnostics.CodeAnalysis; -[assembly: SuppressMessage("Performance", "CA1813:Avoid unsealed attributes")] -[assembly: SuppressMessage("Performance", "CA1810:Initialize reference type static fields inline")] -[assembly: SuppressMessage("Performance", "CA1822:Mark members as static")] -[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types")] -[assembly: SuppressMessage("Design", "CA1018:Mark attributes with AttributeUsageAttribute")] -[assembly: SuppressMessage("", "CA1825")] - -[assembly: SuppressMessage("Code Quality", "IDE0051")] -[assembly: SuppressMessage("Code Quality", "IDE0057")] +[assembly: SuppressMessage("Performance", "CA1813")] +[assembly: SuppressMessage("Performance", "CA1810")] +[assembly: SuppressMessage("Performance", "CA1822")] +[assembly: SuppressMessage("Performance", "CA1825")] + +[assembly: SuppressMessage("Design", "CA1031")] +[assembly: SuppressMessage("Design", "CA1018")] + +[assembly: SuppressMessage("CodeQuality", "IDE0051")] +[assembly: SuppressMessage("CodeQuality", "IDE0057")] +[assembly: SuppressMessage("CodeQuality", "IDE0079")] + +[assembly: SuppressMessage("", "IDE0251")] +[assembly: SuppressMessage("Style", "IDE0270")] +[assembly: SuppressMessage("Usage", "CA2211")] +[assembly: SuppressMessage("GeneratedRegex", "SYSLIB1045")] + +[assembly: SuppressMessage("Reliability", "CA2020:Prevent from behavioral change", Justification = "", Scope = "member", Target = "~M:HarmonyLib.FileLog.LogBytes(System.Int64,System.Int32)")] +[assembly: SuppressMessage("Performance", "CA1850:Prefer static 'HashData' method over 'ComputeHash'", Justification = "", Scope = "member", Target = "~M:HarmonyLib.FileLog.LogBytes(System.Int64,System.Int32)")] diff --git a/Harmony/Harmony.csproj b/Harmony/Harmony.csproj index 5cd1dcaa..54a8ebac 100644 --- a/Harmony/Harmony.csproj +++ b/Harmony/Harmony.csproj @@ -88,10 +88,7 @@ - - - - + diff --git a/Harmony/Internal/CodeTranspiler.cs b/Harmony/Internal/CodeTranspiler.cs index df289ec0..14adb97e 100644 --- a/Harmony/Internal/CodeTranspiler.cs +++ b/Harmony/Internal/CodeTranspiler.cs @@ -66,7 +66,7 @@ internal static bool ShouldAddExceptionInfo(object op, int opIndex, List var isStartBlock = blocks.FirstOrDefault(block => block.blockType != ExceptionBlockType.EndExceptionBlock); var isEndBlock = blocks.FirstOrDefault(block => block.blockType == ExceptionBlockType.EndExceptionBlock); - if (isStartBlock is object && isEndBlock is null) + if (isStartBlock is not null && isEndBlock is null) { var pairInstruction = originalInstructions.Skip(originalIndex + 1).FirstOrDefault(instr => { @@ -77,7 +77,7 @@ internal static bool ShouldAddExceptionInfo(object op, int opIndex, List blocks = blocksObject as List; return blocks.Any(); }); - if (pairInstruction is object) + if (pairInstruction is not null) { var pairStart = originalIndex + 1; var pairEnd = pairStart + originalInstructions.Skip(pairStart).ToList().IndexOf(pairInstruction) - 1; @@ -94,7 +94,7 @@ internal static bool ShouldAddExceptionInfo(object op, int opIndex, List blocks = blocksObject as List; return blocks.Any(); }); - if (pairInstruction is object) + if (pairInstruction is not null) { pairStart = opIndex + 1; pairEnd = pairStart + newInstructions.Skip(opIndex + 1).ToList().IndexOf(pairInstruction) - 1; @@ -104,7 +104,7 @@ internal static bool ShouldAddExceptionInfo(object op, int opIndex, List } } } - if (isStartBlock is null && isEndBlock is object) + if (isStartBlock is null && isEndBlock is not null) { var pairInstruction = originalInstructions.GetRange(0, originalIndex).LastOrDefault(instr => { @@ -115,7 +115,7 @@ internal static bool ShouldAddExceptionInfo(object op, int opIndex, List blocks = blocksObject as List; return blocks.Any(); }); - if (pairInstruction is object) + if (pairInstruction is not null) { var pairStart = originalInstructions.GetRange(0, originalIndex).LastIndexOf(pairInstruction); var pairEnd = originalIndex; @@ -132,7 +132,7 @@ internal static bool ShouldAddExceptionInfo(object op, int opIndex, List blocks = blocksObject as List; return blocks.Any(); }); - if (pairInstruction is object) + if (pairInstruction is not null) { pairStart = newInstructions.GetRange(0, opIndex).LastIndexOf(pairInstruction); pairEnd = opIndex; @@ -235,7 +235,7 @@ internal List GetResult(ILGenerator generator, MethodBase metho // remember the order of the original input (for detection of dupped code instructions) List originalInstructions = null; - if (unassignedValues is object) + if (unassignedValues is not null) originalInstructions = instructions.Cast().ToList(); // call the transpiler @@ -245,7 +245,7 @@ internal List GetResult(ILGenerator generator, MethodBase metho instructions = newInstructions; // convert result back to 'our' CodeInstruction and re-assign otherwise lost fields - if (unassignedValues is object) + if (unassignedValues is not null) instructions = ConvertToOurInstructions(instructions, typeof(CodeInstruction), originalInstructions, unassignedValues); }); diff --git a/Harmony/Internal/Emitter.cs b/Harmony/Internal/Emitter.cs index d2aa4cb9..b626e7ce 100644 --- a/Harmony/Internal/Emitter.cs +++ b/Harmony/Internal/Emitter.cs @@ -99,7 +99,7 @@ internal static string FormatArgument(object argument, string extra = null) var type = argument.GetType(); if (argument is MethodBase method) - return method.FullDescription() + (extra is object ? " " + extra : ""); + return method.FullDescription() + (extra is not null ? " " + extra : ""); if (argument is FieldInfo field) return $"{field.FieldType.FullDescription()} {field.DeclaringType.FullDescription()}::{field.Name}"; @@ -351,7 +351,7 @@ internal void Emit(OpCode opcode, long arg) internal void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[] optionalParameterTypes) { instructions.Add(CurrentPos(), new CodeInstruction(opcode, methodInfo)); - var extra = optionalParameterTypes is object && optionalParameterTypes.Length > 0 ? optionalParameterTypes.Description() : null; + var extra = optionalParameterTypes is not null && optionalParameterTypes.Length > 0 ? optionalParameterTypes.Description() : null; LogIL(opcode, methodInfo, extra); il.EmitCall(opcode, methodInfo, optionalParameterTypes); } diff --git a/Harmony/Internal/MethodCopier.cs b/Harmony/Internal/MethodCopier.cs index 1d30fdfb..ccbf48b4 100644 --- a/Harmony/Internal/MethodCopier.cs +++ b/Harmony/Internal/MethodCopier.cs @@ -50,7 +50,7 @@ internal static List GetInstructions(ILGenerator generator, Met var copier = new MethodCopier(method, generator, originalVariables); var info = Harmony.GetPatchInfo(method); - if (info is object) + if (info is not null) { var sortedTranspilers = PatchFunctions.GetSortedPatchMethods(method, info.Transpilers.ToArray(), false); for (var i = 0; i < maxTranspilers && i < sortedTranspilers.Count; i++) @@ -111,7 +111,7 @@ internal MethodBodyReader(MethodBase method, ILGenerator generator) var type = method.DeclaringType; - if (type is object && type.IsGenericType) + if (type is not null && type.IsGenericType) { try { typeArguments = type.GetGenericArguments(); } catch { typeArguments = null; } @@ -219,7 +219,7 @@ internal void HandleNativeMethod() internal void DeclareVariables(LocalBuilder[] existingVariables) { if (generator is null) return; - if (existingVariables is object) + if (existingVariables is not null) variables = existingVariables; else variables = localVariables.Select(lvi => generator.DeclareLocal(lvi.LocalType, lvi.IsPinned)).ToArray(); @@ -329,7 +329,7 @@ internal List FinalizeILCodes(Emitter emitter, List case OperandType.InlineSwitch: { var targets = ilInstruction.operand as ILInstruction[]; - if (targets is object) + if (targets is not null) { var labels = new List