Skip to content

Commit

Permalink
Added: Assembly Trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Jul 5, 2022
1 parent 9078b84 commit 19c63e2
Show file tree
Hide file tree
Showing 26 changed files with 624 additions and 112 deletions.
14 changes: 14 additions & 0 deletions source/Reloaded.Hooks.Definitions/Helpers/Trimming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
using static System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes;
#pragma warning disable CS1591

namespace Reloaded.Hooks.Definitions.Helpers;

public class Trimming
{
public const DynamicallyAccessedMemberTypes FuncPtrTypes = PublicParameterlessConstructor | PublicFields | PublicNestedTypes;
public const DynamicallyAccessedMemberTypes ReloadedAttributeTypes = FuncPtrTypes | PublicMethods | NonPublicMethods;
public const DynamicallyAccessedMemberTypes Methods = PublicMethods | NonPublicMethods;
}
#endif
52 changes: 45 additions & 7 deletions source/Reloaded.Hooks.Definitions/IFunction.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Reloaded.Hooks.Definitions.Enums;
using Reloaded.Hooks.Definitions.Helpers;

namespace Reloaded.Hooks.Definitions
{
/// <summary>
/// An interface for performing operations on native functions in memory.
/// </summary>
public interface IFunction<TFunction>
public interface IFunction<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.ReloadedAttributeTypes)]
#endif
TFunction>
{
/// <summary>
/// Address of the function in memory.
Expand Down Expand Up @@ -53,28 +59,44 @@ public interface IFunction<TFunction>
/// <param name="type">The type containing the method. Use "typeof()"</param>
/// <param name="methodName">The name of the method. Use nameof()</param>
/// <param name="minHookLength">Optional explicit length of hook. Use only in rare cases where auto-length check overflows a jmp/call opcode.</param>
unsafe IHook<TFunction> Hook(Type type, string methodName, int minHookLength);
unsafe IHook<TFunction> Hook(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.Methods)]
#endif
Type type, string methodName, int minHookLength);

/// <summary>
/// Creates a hook for a function at a given address.
/// Use only in .NET 5 and above with methods declared [UnmanagedCallersOnly].
/// </summary>
/// <param name="type">The type containing the method. Use "typeof()"</param>
/// <param name="methodName">The name of the method. Use nameof()</param>
unsafe IHook<TFunction> Hook(Type type, string methodName);
unsafe IHook<TFunction> Hook(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.Methods)]
#endif
Type type, string methodName);

/// <summary>
/// Creates a hook for this function using an alternative delegate/function pointer specified by <typeparamref name="TFunctionType"/>.
/// </summary>
/// <param name="function">The function to detour the original function to.</param>
/// <param name="minHookLength">Optional explicit length of hook. Use only in rare cases where auto-length check overflows a jmp/call opcode.</param>
unsafe IHook<TFunctionType> HookAs<TFunctionType>(void* function, int minHookLength);
unsafe IHook<TFunctionType> HookAs<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.ReloadedAttributeTypes)]
#endif
TFunctionType>(void* function, int minHookLength);

/// <summary>
/// Creates a hook for this function using an alternative delegate/function pointer specified by <typeparamref name="TFunctionType"/>.
/// </summary>
/// <param name="function">The function to detour the original function to.</param>
unsafe IHook<TFunctionType> HookAs<TFunctionType>(void* function);
unsafe IHook<TFunctionType> HookAs<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.ReloadedAttributeTypes)]
#endif
TFunctionType>(void* function);

/// <summary>
/// Creates a hook for this function using an alternative delegate/function pointer specified by <typeparamref name="TFunctionType"/>.
Expand All @@ -83,15 +105,31 @@ public interface IFunction<TFunction>
/// <param name="type">The type containing the method. Use "typeof()"</param>
/// <param name="methodName">The name of the method. Use nameof()</param>
/// <param name="minHookLength">Optional explicit length of hook. Use only in rare cases where auto-length check overflows a jmp/call opcode.</param>
unsafe IHook<TFunctionType> HookAs<TFunctionType>(Type type, string methodName, int minHookLength);
unsafe IHook<TFunctionType> HookAs<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.ReloadedAttributeTypes)]
#endif
TFunctionType>(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.Methods)]
#endif
Type type, string methodName, int minHookLength);

/// <summary>
/// Creates a hook for this function using an alternative delegate/function pointer specified by <typeparamref name="TFunctionType"/>.
/// Use only in .NET 5 and above with methods declared [UnmanagedCallersOnly].
/// </summary>
/// <param name="type">The type containing the method. Use "typeof()"</param>
/// <param name="methodName">The name of the method. Use nameof()</param>
unsafe IHook<TFunctionType> HookAs<TFunctionType>(Type type, string methodName);
unsafe IHook<TFunctionType> HookAs<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.ReloadedAttributeTypes)]
#endif
TFunctionType>(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.Methods)]
#endif
Type type, string methodName);

/// <summary>
/// Creates a wrapper function which allows you to call a function with a custom calling
Expand Down
8 changes: 7 additions & 1 deletion source/Reloaded.Hooks.Definitions/IFunctionPtr.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Reloaded.Hooks.Definitions.Helpers;

namespace Reloaded.Hooks.Definitions
{
/// <summary>
/// Utility interface for working with pointers to functions with non-standard conventions.
/// </summary>
public interface IFunctionPtr<TDelegate> where TDelegate : Delegate
public interface IFunctionPtr<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.ReloadedAttributeTypes)]
#endif
TDelegate> where TDelegate : Delegate
{
/// <summary>
/// The address of the pointer in memory with which this class was instantiated with.
Expand Down
8 changes: 7 additions & 1 deletion source/Reloaded.Hooks.Definitions/IHook.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Reloaded.Hooks.Definitions.Helpers;
using Reloaded.Hooks.Definitions.Structs;

namespace Reloaded.Hooks.Definitions
Expand Down Expand Up @@ -92,7 +94,11 @@ public interface IHook

/// <summary/>
/// <typeparam name="TFunction">A valid delegate type or struct representing a function pointer.</typeparam>
public interface IHook<TFunction> : IHook
public interface IHook<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.ReloadedAttributeTypes)]
#endif
TFunction> : IHook
{
/// <summary>
/// Allows you to call the original function that was hooked.
Expand Down

0 comments on commit 19c63e2

Please sign in to comment.