Skip to content

Commit

Permalink
housekeeping, fixes docs error, net9 for docs, better api docs landin…
Browse files Browse the repository at this point in the history
…g page, net9 locking in filelog
  • Loading branch information
pardeike committed Dec 19, 2024
1 parent b1f5a0e commit 57ed506
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 56 deletions.
6 changes: 3 additions & 3 deletions 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;net8.0</TargetFrameworks>
<TargetFrameworks>net35;net452;net472;net48;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<LangVersion>preview</LangVersion>
<IsPackable>false</IsPackable>
<IntermediateOutputPath>obj</IntermediateOutputPath>
Expand All @@ -19,8 +19,8 @@
<!-- Reference assemblies are needed for non-Windows .NET Framework targeting builds. -->
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net35" Version="1.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions Harmony/Documentation/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
[assembly: SuppressMessage("", "CA1016")]
[assembly: SuppressMessage("", "CS0649")]
[assembly: SuppressMessage("", "IDE0251")]
[assembly: SuppressMessage("", "IDE0130")]
61 changes: 60 additions & 1 deletion Harmony/Documentation/api/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
# Harmony API
# Harmony API

Use the navigation to the left to drill down into the Harmony API documentation.

Here is a selected list of classes:

### Basics

- [Harmony](HarmonyLib.html#Harmony)
- [PatchClassProcessor](HarmonyLib.html#PatchClassProcessor)
- [PatchProcessor](HarmonyLib.html#PatchProcessor)
- [HarmonyPatch](HarmonyLib.HarmonyPatch.html)
- [PatchInfo](HarmonyLib.PatchInfo.html)
- [Patches](HarmonyLib.Patches.html)
- [Patch](HarmonyLib.Patch.html)

### Targeting

- [HarmonyMethod](HarmonyLib.HarmonyMethod.html)
- [HarmonyMethodExtensions](HarmonyLib.HarmonyMethodExtensions.html)
- [MethodType](HarmonyLib.MethodType.html)

### Attributes

- [HarmonyAttribute](HarmonyLib.HarmonyAttribute.html)
- [HarmonyPrefix](HarmonyLib.HarmonyPrefix.html)
- [HarmonyPostfix](HarmonyLib.HarmonyPostfix.html)
- [HarmonyTranspiler](HarmonyLib.HarmonyTranspiler.html)
- [HarmonyFinalizer](HarmonyLib.HarmonyFinalizer.html)
- [HarmonyDebug](HarmonyLib.HarmonyDebug.html)

### Additional patch class annotations

- [HarmonyTargetMethod](HarmonyLib.HarmonyTargetMethod.html)
- [HarmonyTargetMethods](HarmonyLib.HarmonyTargetMethods.html)
- [HarmonyPrepare](HarmonyLib.HarmonyPrepare.html)
- [HarmonyCleanup](HarmonyLib.HarmonyCleanup.html)
- [HarmonyArgument](HarmonyLib.HarmonyArgument.html)

### Transpiling

- [Transpilers](HarmonyLib.Transpilers.html)
- [CodeInstruction](HarmonyLib.CodeInstruction.html)
- [CodeInstructionExtensions](HarmonyLib.CodeInstructionExtensions.html)
- [CodeInstructionsExtensions](HarmonyLib.CodeInstructionsExtensions.html)
- [CodeMatcher](HarmonyLib.CodeMatcher.html)
- [CodeMatch](HarmonyLib.CodeMatch.html)
- [Code](HarmonyLib.Code.html)


### Helper classes

- [AccessTools](HarmonyLib.AccessTools.html)
- [AccessToolsExtensions](HarmonyLib.AccessToolsExtensions.html)
- [SymbolExtensions](HarmonyLib.SymbolExtensions.html)
- [FastAccess](HarmonyLib.FastAccess.html)
- [FileLog](HarmonyLib.FileLog.html)
- [Traverse](HarmonyLib.Traverse.html)
- [GeneralExtensions](HarmonyLib.GeneralExtensions.html)
- [HarmonyDelegate](HarmonyLib.HarmonyDelegate.html)
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
var codeMatcher = new CodeMatcher(instructions /*, ILGenerator generator*/);

codeMatcher.MatchStartForward(
CodeMatch.Calls(() => default(DamageHandler).Kill(default(Player)))
CodeMatch.Calls(() => default(DamageHandler).Kill(default))
)
.ThrowIfInvalid("Could not find call to DamageHandler.Kill")
.RemoveInstruction()
Expand All @@ -45,7 +45,7 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
[HarmonyPatch]
public static class DamageHandler_Apply_Patch_Alternative
{
static IEnumerable<MethodBase> TargetMethods() => new List<MethodBase>();
static IEnumerable<MethodBase> TargetMethods() => [];

static void MyDeathHandler(DamageHandler handler, Player player) { }

Expand All @@ -55,7 +55,7 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst

// <replacement_alt>
codeMatcher.ThrowIfNotMatchForward("Could not find call to DamageHandler.Kill",
CodeMatch.Calls(() => default(DamageHandler).Kill(default(Player)))
CodeMatch.Calls(() => default(DamageHandler).Kill(default))
)
.RemoveInstruction()
.InsertAndAdvance(
Expand Down
10 changes: 5 additions & 5 deletions Harmony/Extras/FastAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static InstantiationHandler<T> CreateInstantiationHandler<T>()
var generator = dynamicMethod.GetILGenerator();
generator.Emit(OpCodes.Newobj, constructorInfo);
generator.Emit(OpCodes.Ret);
return (InstantiationHandler<T>)dynamicMethod.Generate().CreateDelegate(typeof(InstantiationHandler<T>));
return dynamicMethod.Generate().CreateDelegate<InstantiationHandler<T>>();
}

/// <summary>Creates an getter delegate for a property</summary>
Expand All @@ -75,7 +75,7 @@ public static GetterHandler<T, S> CreateGetterHandler<T, S>(PropertyInfo propert
getGenerator.Emit(OpCodes.Call, getMethodInfo);
getGenerator.Emit(OpCodes.Ret);

return (GetterHandler<T, S>)dynamicGet.Generate().CreateDelegate(typeof(GetterHandler<T, S>));
return dynamicGet.Generate().CreateDelegate<GetterHandler<T, S>>();
}

/// <summary>Creates an getter delegate for a field</summary>
Expand All @@ -94,7 +94,7 @@ public static GetterHandler<T, S> CreateGetterHandler<T, S>(FieldInfo fieldInfo)
getGenerator.Emit(OpCodes.Ldfld, fieldInfo);
getGenerator.Emit(OpCodes.Ret);

return (GetterHandler<T, S>)dynamicGet.Generate().CreateDelegate(typeof(GetterHandler<T, S>));
return dynamicGet.Generate().CreateDelegate<GetterHandler<T, S>>();
}

/// <summary>Creates an getter delegate for a field (with a list of possible field names)</summary>
Expand Down Expand Up @@ -139,7 +139,7 @@ public static SetterHandler<T, S> CreateSetterHandler<T, S>(PropertyInfo propert
setGenerator.Emit(OpCodes.Call, setMethodInfo);
setGenerator.Emit(OpCodes.Ret);

return (SetterHandler<T, S>)dynamicSet.Generate().CreateDelegate(typeof(SetterHandler<T, S>));
return dynamicSet.Generate().CreateDelegate<SetterHandler<T, S>>();
}

/// <summary>Creates an setter delegate for a field</summary>
Expand All @@ -159,7 +159,7 @@ public static SetterHandler<T, S> CreateSetterHandler<T, S>(FieldInfo fieldInfo)
setGenerator.Emit(OpCodes.Stfld, fieldInfo);
setGenerator.Emit(OpCodes.Ret);

return (SetterHandler<T, S>)dynamicSet.Generate().CreateDelegate(typeof(SetterHandler<T, S>));
return dynamicSet.Generate().CreateDelegate<SetterHandler<T, S>>();
}

static DynamicMethodDefinition CreateGetDynamicMethod<T, S>(Type type) => new($"DynamicGet_{type.Name}", typeof(S), [typeof(T)]);
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Extras/MethodInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static FastInvokeHandler GetHandler(MethodInfo methodInfo, bool directBox

Emit(il, OpCodes.Ret);

var invoder = (FastInvokeHandler)dynamicMethod.Generate().CreateDelegate(typeof(FastInvokeHandler));
var invoder = dynamicMethod.Generate().CreateDelegate<FastInvokeHandler>();
return invoder;
}

Expand Down
1 change: 1 addition & 0 deletions Harmony/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[assembly: SuppressMessage("CodeQuality", "IDE0079")]

[assembly: SuppressMessage("", "IDE0251")]
[assembly: SuppressMessage("", "IDE0130")]
[assembly: SuppressMessage("Style", "IDE0270")]
[assembly: SuppressMessage("Usage", "CA2211")]
[assembly: SuppressMessage("GeneratedRegex", "SYSLIB1045")]
Expand Down
11 changes: 6 additions & 5 deletions Harmony/Tools/AccessTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,15 +1532,16 @@ public static FieldRef<F> StaticFieldRefAccess<F>(FieldInfo fieldInfo)
/// Use this if the generic arguments of <typeparamref name="DelegateType"/> doesn't represent the delegate's
/// arguments, and calling this function fails
/// <returns>A delegate of given <typeparamref name="DelegateType"/> to given <paramref name="method"/></returns>
/// </param>
/// <remarks>
/// <para>
/// <param>
/// Delegate invocation is more performant and more convenient to use than <see cref="MethodBase.Invoke(object, object[])"/>
/// at a one-time setup cost.
/// </para>
/// <para>
/// </param>
/// <param>
/// Works for both type of static and instance methods, both open and closed (a.k.a. unbound and bound) instance methods,
/// and both class and struct methods.
/// </para>
/// </param>
/// </remarks>
///
public static DelegateType MethodDelegate<DelegateType>(MethodInfo method, object instance = null, bool virtualCall = true, Type[] delegateArgs = null) where DelegateType : Delegate
Expand Down Expand Up @@ -1734,7 +1735,7 @@ public static DelegateType MethodDelegate<DelegateType>(string typeColonName, ob
/// <returns>A delegate of given <typeparamref name="DelegateType"/> to the method specified via [<see cref="HarmonyLib.HarmonyDelegate"/>]
/// attributes on <typeparamref name="DelegateType"/></returns>
/// <remarks>
/// This calls <see cref="MethodDelegate{DelegateType}(MethodInfo, object, bool)"/> with the <c>method</c> and <c>virtualCall</c> arguments
/// This calls <see cref="MethodDelegate{DelegateType}(MethodInfo, object, bool, Type[])"/> with the <c>method</c> and <c>virtualCall</c> arguments
/// determined from the [<see cref="HarmonyLib.HarmonyDelegate"/>] attributes on <typeparamref name="DelegateType"/>,
/// and the given <paramref name="instance"/> (for closed instance delegates).
/// </remarks>
Expand Down
7 changes: 7 additions & 0 deletions Harmony/Tools/FileLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
#if NET9_0_OR_GREATER
using System.Threading;
#endif

namespace HarmonyLib
{
/// <summary>A file log for debugging</summary>
///
public static class FileLog
{
#if NET9_0_OR_GREATER
private static readonly Lock fileLock = new();
#else
private static readonly object fileLock = new();
#endif
private static bool _logPathInited;
private static string _logPath;

Expand Down
6 changes: 3 additions & 3 deletions Harmony/Tools/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal static FieldRef<T, F> FieldRefAccess<T, F>(FieldInfo fieldInfo, bool ne
}
il.Emit(OpCodes.Ret);

return (FieldRef<T, F>)dm.Generate().CreateDelegate(typeof(FieldRef<T, F>));
return dm.Generate().CreateDelegate<FieldRef<T, F>>();
}

internal static StructFieldRef<T, F> StructFieldRefAccess<T, F>(FieldInfo fieldInfo) where T : struct
Expand All @@ -95,7 +95,7 @@ internal static StructFieldRef<T, F> StructFieldRefAccess<T, F>(FieldInfo fieldI
il.Emit(OpCodes.Ldflda, fieldInfo);
il.Emit(OpCodes.Ret);

return (StructFieldRef<T, F>)dm.Generate().CreateDelegate(typeof(StructFieldRef<T, F>));
return dm.Generate().CreateDelegate<StructFieldRef<T, F>>();
}

internal static FieldRef<F> StaticFieldRefAccess<F>(FieldInfo fieldInfo)
Expand All @@ -111,7 +111,7 @@ internal static FieldRef<F> StaticFieldRefAccess<F>(FieldInfo fieldInfo)
il.Emit(OpCodes.Ldsflda, fieldInfo);
il.Emit(OpCodes.Ret);

return (FieldRef<F>)dm.Generate().CreateDelegate(typeof(FieldRef<F>));
return dm.Generate().CreateDelegate<FieldRef<F>>();
}

internal static FieldInfo GetInstanceField(Type type, string fieldName)
Expand Down
2 changes: 2 additions & 0 deletions HarmonyTests/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
[assembly: SuppressMessage("Style", "IDE0066")]
[assembly: SuppressMessage("Style", "IDE0251")]

[assembly: SuppressMessage("", "IDE0130")]

[assembly: SuppressMessage("Usage", "CA2208")]
[assembly: SuppressMessage("Usage", "CA2211")]

Expand Down
7 changes: 6 additions & 1 deletion HarmonyTests/Patching/Assets/ArgumentCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace HarmonyLibTests.Assets
Expand Down Expand Up @@ -149,7 +150,11 @@ public class NullableResults
private string s = "foo";

[MethodImpl(MethodImplOptions.NoInlining)]
public bool? Method() => false;
public bool? Method()
{
_ = s;
return false;
}
}

[HarmonyPatch(typeof(NullableResults), nameof(NullableResults.Method))]
Expand Down
4 changes: 2 additions & 2 deletions HarmonyTests/Patching/FinalizerPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private void AssertThrownException<E>()
{
Assert.NotNull(info, "info should not be null");
Assert.NotNull(info["outerexception"], "Should throw an exception");
Assert.IsInstanceOf(typeof(E), info["outerexception"]);
Assert.IsInstanceOf<E>(info["outerexception"]);
}

private void AssertNullExceptionInput()
Expand All @@ -350,7 +350,7 @@ private void AssertExceptionInput<E>()
Assert.NotNull(info, "info should not be null");
Assert.True(info.ContainsKey("exception"), "Finalizer should have an exception field");
Assert.NotNull(info["exception"], "Finalizer should get an exception input");
Assert.IsInstanceOf(typeof(E), info["exception"]);
Assert.IsInstanceOf<E>(info["exception"]);
}
}
}
2 changes: 1 addition & 1 deletion HarmonyTests/Patching/Specials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public void Test_PatchExceptionWithCleanup2()
catch (HarmonyException ex)
{
Assert.NotNull(ex.InnerException);
Assert.IsInstanceOf(typeof(ArgumentException), ex.InnerException);
Assert.IsInstanceOf<ArgumentException>(ex.InnerException);
Assert.AreEqual("Test", ex.InnerException.Message);
return;
}
Expand Down
Loading

0 comments on commit 57ed506

Please sign in to comment.