Skip to content

Commit

Permalink
code cleanup, net8.0, optional ret code for methods ending in throw
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Dec 29, 2023
1 parent d916a30 commit 606ba09
Show file tree
Hide file tree
Showing 36 changed files with 163 additions and 132 deletions.
2 changes: 1 addition & 1 deletion Harmony/Documentation/Documentation.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net35;net452;net472;net48;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net35;net452;net472;net48;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>preview</LangVersion>
<IsPackable>false</IsPackable>
<IntermediateOutputPath>obj</IntermediateOutputPath>
Expand Down
5 changes: 3 additions & 2 deletions Harmony/Harmony.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net35;net452;net472;net48;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net35;net452;net472;net48;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Product>Harmony</Product>
<Company>Andreas Pardeike</Company>
Expand Down Expand Up @@ -35,7 +35,7 @@

<!-- In .NET 5.0 Binary Formatters are off by default. Support is added for .NET 5.0+ for fallback JSON Serialization -->
<Choose>
<When Condition="'$(TargetFramework)'=='net5.0' Or '$(TargetFramework)'=='net6.0' Or '$(TargetFramework)'=='net7.0'">
<When Condition="'$(TargetFramework)'=='net5.0' Or '$(TargetFramework)'=='net6.0' Or '$(TargetFramework)'=='net7.0' Or '$(TargetFramework)'=='net8.0'">
<PropertyGroup>
<IsNET5OrGreater>true</IsNET5OrGreater>
</PropertyGroup>
Expand Down Expand Up @@ -122,6 +122,7 @@
<Compile Remove="**\*.net5.cs" />
<Compile Remove="**\*.net6.cs" />
<Compile Remove="**\*.net7.cs" />
<Compile Remove="**\*.net8.cs" />
</ItemGroup>

<Target Name="RemoveOldNuGetPackages" BeforeTargets="PreBuildEvent">
Expand Down
18 changes: 9 additions & 9 deletions Harmony/Internal/AccessCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ internal enum MemberType
{ MemberType.Static, BasicFlags | BindingFlags.Static }
};

readonly Dictionary<Type, Dictionary<string, FieldInfo>> declaredFields = new();
readonly Dictionary<Type, Dictionary<string, PropertyInfo>> declaredProperties = new();
readonly Dictionary<Type, Dictionary<string, Dictionary<int, MethodBase>>> declaredMethods = new();
readonly Dictionary<Type, Dictionary<string, FieldInfo>> declaredFields = [];
readonly Dictionary<Type, Dictionary<string, PropertyInfo>> declaredProperties = [];
readonly Dictionary<Type, Dictionary<string, Dictionary<int, MethodBase>>> declaredMethods = [];

readonly Dictionary<Type, Dictionary<string, FieldInfo>> inheritedFields = new();
readonly Dictionary<Type, Dictionary<string, PropertyInfo>> inheritedProperties = new();
readonly Dictionary<Type, Dictionary<string, Dictionary<int, MethodBase>>> inheritedMethods = new();
readonly Dictionary<Type, Dictionary<string, FieldInfo>> inheritedFields = [];
readonly Dictionary<Type, Dictionary<string, PropertyInfo>> inheritedProperties = [];
readonly Dictionary<Type, Dictionary<string, Dictionary<int, MethodBase>>> inheritedMethods = [];

static T Get<T>(Dictionary<Type, Dictionary<string, T>> dict, Type type, string name, Func<T> fetcher)
{
lock (dict)
{
if (dict.TryGetValue(type, out var valuesByName) is false)
{
valuesByName = new Dictionary<string, T>();
valuesByName = [];
dict[type] = valuesByName;
}
if (valuesByName.TryGetValue(name, out var value) is false)
Expand All @@ -53,12 +53,12 @@ static T Get<T>(Dictionary<Type, Dictionary<string, Dictionary<int, T>>> dict, T
{
if (dict.TryGetValue(type, out var valuesByName) is false)
{
valuesByName = new Dictionary<string, Dictionary<int, T>>();
valuesByName = [];
dict[type] = valuesByName;
}
if (valuesByName.TryGetValue(name, out var valuesByArgument) is false)
{
valuesByArgument = new Dictionary<int, T>();
valuesByArgument = [];
valuesByName[name] = valuesByArgument;
}
var argumentsHash = AccessTools.CombinedHashCode(arguments);
Expand Down
4 changes: 2 additions & 2 deletions Harmony/Internal/CodeTranspiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace HarmonyLib
internal class CodeTranspiler
{
readonly IEnumerable<CodeInstruction> codeInstructions;
readonly List<MethodInfo> transpilers = new();
readonly List<MethodInfo> transpilers = [];

internal CodeTranspiler(List<ILInstruction> ilInstructions)
{
Expand Down Expand Up @@ -156,7 +156,7 @@ internal static IEnumerable ConvertInstructionsAndUnassignedValues(Type type, IE
var listType = enumerableAssembly.GetType(genericListTypeWithElement.FullName);
var list = Activator.CreateInstance(listType);
var listAdd = list.GetType().GetMethod("Add");
unassignedValues = new Dictionary<object, Dictionary<string, object>>();
unassignedValues = [];
foreach (var op in enumerable)
{
var elementTo = ConvertInstruction(elementType, op, out var unassigned);
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Internal/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override string ToString()
internal class Emitter
{
readonly CecilILGenerator il;
readonly Dictionary<int, CodeInstruction> instructions = new();
readonly Dictionary<int, CodeInstruction> instructions = [];
readonly bool debug;

internal Emitter(ILGenerator il, bool debug)
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Internal/HarmonySharedState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static HarmonySharedState()
state = (Dictionary<MethodBase, byte[]>)stateField.GetValue(null);

// copy 'originals' over to our fields
originals = new Dictionary<MethodInfo, MethodBase>();
originals = [];
if (originalsField != null) // may not exist in older versions
originals = (Dictionary<MethodInfo, MethodBase>)originalsField.GetValue(null);
}
Expand Down
4 changes: 2 additions & 2 deletions Harmony/Internal/ILInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ internal class ILInstruction
internal object operand;
internal object argument;

internal List<Label> labels = new();
internal List<ExceptionBlock> blocks = new();
internal List<Label> labels = [];
internal List<ExceptionBlock> blocks = [];

internal ILInstruction(OpCode opcode, object operand = null)
{
Expand Down
6 changes: 3 additions & 3 deletions Harmony/Internal/MethodCopier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace HarmonyLib
internal class MethodCopier
{
readonly MethodBodyReader reader;
readonly List<MethodInfo> transpilers = new();
readonly List<MethodInfo> transpilers = [];

internal MethodCopier(MethodBase fromMethod, ILGenerator toILGenerator, LocalBuilder[] existingVariables = null)
{
Expand Down Expand Up @@ -98,7 +98,7 @@ internal MethodBodyReader(MethodBase method, ILGenerator generator)
if ((body?.GetILAsByteArray()?.Length ?? 0) == 0)
{
ilBytes = new ByteBuffer(new byte[0]);
ilInstructions = new List<ILInstruction>();
ilInstructions = [];
}
else
{
Expand Down Expand Up @@ -127,7 +127,7 @@ internal MethodBodyReader(MethodBase method, ILGenerator generator)
this_parameter = new ThisParameter(method);
parameters = method.GetParameters();

localVariables = body?.LocalVariables?.ToList() ?? new List<LocalVariableInfo>();
localVariables = body?.LocalVariables?.ToList() ?? [];
exceptions = body?.ExceptionHandlingClauses ?? new List<ExceptionHandlingClause>();
}

Expand Down
3 changes: 2 additions & 1 deletion Harmony/Internal/MethodPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ internal MethodInfo CreateReplacement(out Dictionary<int, CodeInstruction> final
emitter.Emit(OpCodes.Ldloc, resultVariable);
}

emitter.Emit(OpCodes.Ret);
if (endsInThrow == false || hasFinalizers) // methods ending in throw cannot have more code after the throw
emitter.Emit(OpCodes.Ret);

This comment has been minimized.

Copy link
@sunnamed434

sunnamed434 Feb 1, 2024

Perhaps that's the problem for #564


finalInstructions = emitter.GetInstructions();

Expand Down
10 changes: 5 additions & 5 deletions Harmony/Internal/PatchModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ internal class Job
{
internal MethodBase original;
internal T replacement;
internal List<HarmonyMethod> prefixes = new();
internal List<HarmonyMethod> postfixes = new();
internal List<HarmonyMethod> transpilers = new();
internal List<HarmonyMethod> finalizers = new();
internal List<HarmonyMethod> prefixes = [];
internal List<HarmonyMethod> postfixes = [];
internal List<HarmonyMethod> transpilers = [];
internal List<HarmonyMethod> finalizers = [];

internal void AddPatch(AttributePatch patch)
{
Expand All @@ -39,7 +39,7 @@ internal void AddPatch(AttributePatch patch)
}
}

internal Dictionary<MethodBase, Job> state = new();
internal Dictionary<MethodBase, Job> state = [];

internal Job GetJob(MethodBase method)
{
Expand Down
8 changes: 4 additions & 4 deletions Harmony/Internal/PatchSorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ internal List<MethodInfo> Sort(MethodBase original)
if (sortedPatchArray is not null) return sortedPatchArray.Select(x => x.GetMethod(original)).ToList();

// Initialize internal structures used for sorting.
handledPatches = new HashSet<PatchSortingWrapper>();
waitingList = new List<PatchSortingWrapper>();
handledPatches = [];
waitingList = [];
result = new List<PatchSortingWrapper>(patches.Count);
var queue = new Queue<PatchSortingWrapper>(patches);

Expand Down Expand Up @@ -165,8 +165,8 @@ class PatchSortingWrapper : IComparable
internal PatchSortingWrapper(Patch patch)
{
innerPatch = patch;
before = new HashSet<PatchSortingWrapper>();
after = new HashSet<PatchSortingWrapper>();
before = [];
after = [];
}

/// <summary>Determines how patches sort</summary>
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Internal/PatchTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace HarmonyLib
{
internal static class PatchTools
{
private static readonly Dictionary<MethodBase, ICoreDetour> detours = new();
private static readonly Dictionary<MethodBase, ICoreDetour> detours = [];

internal static void DetourMethod(MethodBase method, MethodBase replacement)
{
Expand Down
10 changes: 5 additions & 5 deletions Harmony/Public/CodeInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CodeInstruction
{
internal static class State
{
internal static readonly Dictionary<int, Delegate> closureCache = new();
internal static readonly Dictionary<int, Delegate> closureCache = [];
}

/// <summary>The opcode</summary>
Expand All @@ -26,11 +26,11 @@ internal static class State

/// <summary>All labels defined on this instruction</summary>
///
public List<Label> labels = new();
public List<Label> labels = [];

/// <summary>All exception block boundaries defined on this instruction</summary>
///
public List<ExceptionBlock> blocks = new();
public List<ExceptionBlock> blocks = [];

// Internal parameterless constructor that AccessTools.CreateInstance can use, ensuring that labels/blocks are initialized.
internal CodeInstruction()
Expand Down Expand Up @@ -67,8 +67,8 @@ public CodeInstruction Clone()
{
return new CodeInstruction(this)
{
labels = new List<Label>(),
blocks = new List<ExceptionBlock>()
labels = [],
blocks = []
};
}

Expand Down
2 changes: 1 addition & 1 deletion Harmony/Public/InlineSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class InlineSignature : ICallSiteGenerator

/// <summary>The list of all parameter types or function pointer signatures received by the call site</summary>
///
public List<object> Parameters { get; set; } = new List<object>();
public List<object> Parameters { get; set; } = [];

/// <summary>The return type or function pointer signature returned by the call site</summary>
///
Expand Down
22 changes: 11 additions & 11 deletions Harmony/Public/PatchClassProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public class PatchClassProcessor

readonly Type containerType;
readonly HarmonyMethod containerAttributes;
readonly Dictionary<Type, MethodInfo> auxiliaryMethods;
readonly Dictionary<Type, MethodInfo> auxilaryMethods;

readonly List<AttributePatch> patchMethods;

static readonly List<Type> auxiliaryTypes = new()
{
static readonly List<Type> auxilaryTypes =
[
typeof(HarmonyPrepare),
typeof(HarmonyCleanup),
typeof(HarmonyTargetMethod),
typeof(HarmonyTargetMethods)
};
];

/// <summary name="Category">Name of the patch class's category</summary>
public string Category { get; set; }
Expand Down Expand Up @@ -51,11 +51,11 @@ public PatchClassProcessor(Harmony instance, Type type)

Category = containerAttributes.category;

auxiliaryMethods = new Dictionary<Type, MethodInfo>();
foreach (var auxType in auxiliaryTypes)
auxilaryMethods = [];
foreach (var auxType in auxilaryTypes)
{
var method = PatchTools.GetPatchMethod(containerType, auxType.FullName);
if (method is not null) auxiliaryMethods[auxType] = method;
if (method is not null) auxilaryMethods[auxType] = method;
}

patchMethods = PatchTools.GetPatchMethods(containerType);
Expand All @@ -82,7 +82,7 @@ public List<MethodInfo> Patch()
{
RunMethod<HarmonyCleanup>(ref exception);
ReportException(exception, null);
return new List<MethodInfo>();
return [];
}

var replacements = new List<MethodInfo>();
Expand Down Expand Up @@ -235,7 +235,7 @@ List<MethodBase> GetBulkMethods()
else if (result.Any(m => m is null)) error = "some element was null";
if (error != null)
{
if (auxiliaryMethods.TryGetValue(typeof(HarmonyTargetMethods), out var method))
if (auxilaryMethods.TryGetValue(typeof(HarmonyTargetMethods), out var method))
throw new Exception($"Method {method.FullDescription()} returned an unexpected result: {error}");
else
throw new Exception($"Some method returned an unexpected result: {error}");
Expand Down Expand Up @@ -276,7 +276,7 @@ void ReportException(Exception exception, MethodBase original)

T RunMethod<S, T>(T defaultIfNotExisting, T defaultIfFailing, Func<T, string> failOnResult = null, params object[] parameters)
{
if (auxiliaryMethods.TryGetValue(typeof(S), out var method))
if (auxilaryMethods.TryGetValue(typeof(S), out var method))
{
var input = (parameters ?? new object[0]).Union(new object[] { instance }).ToArray();
var actualParameters = AccessTools.ActualParameters(method, input);
Expand Down Expand Up @@ -314,7 +314,7 @@ T RunMethod<S, T>(T defaultIfNotExisting, T defaultIfFailing, Func<T, string> fa

void RunMethod<S>(ref Exception exception, params object[] parameters)
{
if (auxiliaryMethods.TryGetValue(typeof(S), out var method))
if (auxilaryMethods.TryGetValue(typeof(S), out var method))
{
var input = (parameters ?? new object[0]).Union(new object[] { instance }).ToArray();
var actualParameters = AccessTools.ActualParameters(method, input);
Expand Down
Loading

0 comments on commit 606ba09

Please sign in to comment.