Skip to content

Commit

Permalink
Added: AsmHook APIs with string parameter for code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Feb 26, 2023
1 parent 2906230 commit 9662a67
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
46 changes: 45 additions & 1 deletion source/Reloaded.Hooks.Definitions/IReloadedHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ public interface IReloadedHooks
/// </param>
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
IAsmHook CreateAsmHook(string[] asmCode, long functionAddress);

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
/// </summary>
/// <param name="asmCode">
/// The assembly code to execute, in FASM syntax.
/// (Should start with use32/use64)
/// </param>
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
IAsmHook CreateAsmHook(string asmCode, long functionAddress);

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
Expand All @@ -314,7 +324,18 @@ public interface IReloadedHooks
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookBehaviour behaviour);


/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
/// </summary>
/// <param name="asmCode">
/// The assembly code to execute, in FASM syntax.
/// (Should start with use32/use64)
/// </param>
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookBehaviour behaviour);

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
/// </summary>
Expand All @@ -334,6 +355,18 @@ public interface IReloadedHooks
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
/// <param name="hookLength">Optional explicit length of hook. Use only in rare cases where auto-length check overflows a jmp/call opcode. Default: -1</param>
IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookBehaviour behaviour, int hookLength);

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
/// </summary>
/// <param name="asmCode">
/// The assembly code to execute, in FASM syntax.
/// (Should start with use32/use64)
/// </param>
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
/// <param name="hookLength">Optional explicit length of hook. Use only in rare cases where auto-length check overflows a jmp/call opcode. Default: -1</param>
IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookBehaviour behaviour, int hookLength);

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
Expand All @@ -354,6 +387,17 @@ public interface IReloadedHooks
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
/// <param name="options">Controls the generation behaviour of assembly hooks.</param>
IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookOptions options);

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
/// </summary>
/// <param name="asmCode">
/// The assembly code to execute, in FASM syntax.
/// (Should start with use32/use64)
/// </param>
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
/// <param name="options">Controls the generation behaviour of assembly hooks.</param>
IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookOptions options);

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
Expand Down
5 changes: 4 additions & 1 deletion source/Reloaded.Hooks.Definitions/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@

Reloaded.Hooks.Definitions.IReloadedHooks.CreateAsmHook(string asmCode, long functionAddress) -> Reloaded.Hooks.Definitions.IAsmHook
Reloaded.Hooks.Definitions.IReloadedHooks.CreateAsmHook(string asmCode, long functionAddress, Reloaded.Hooks.Definitions.AsmHookOptions options) -> Reloaded.Hooks.Definitions.IAsmHook
Reloaded.Hooks.Definitions.IReloadedHooks.CreateAsmHook(string asmCode, long functionAddress, Reloaded.Hooks.Definitions.Enums.AsmHookBehaviour behaviour) -> Reloaded.Hooks.Definitions.IAsmHook
Reloaded.Hooks.Definitions.IReloadedHooks.CreateAsmHook(string asmCode, long functionAddress, Reloaded.Hooks.Definitions.Enums.AsmHookBehaviour behaviour, int hookLength) -> Reloaded.Hooks.Definitions.IAsmHook
17 changes: 7 additions & 10 deletions source/Reloaded.Hooks.Tests.X64/CalculatorAsmHookTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,13 @@ public void Dispose()
private void TestHookAddNoOriginal_Internal(AsmHookOptions options)
{
int wordSize = IntPtr.Size;
string[] addFunction =
{
$"{Macros._use32}",
$"push {Macros._ebp}",
$"mov {Macros._ebp}, {Macros._esp}",

$"mov {Macros._eax}, [{Macros._ebp} + {wordSize * 2}]", // Left Parameter
$"mov {Macros._ecx}, [{Macros._ebp} + {wordSize * 3}]", // Right Parameter
$"add {Macros._eax}, 1", // Left Parameter
};
string addFunction =
@$"{Macros._use32}
push {Macros._ebp}
mov {Macros._ebp}, {Macros._esp}
mov {Macros._eax}, [{Macros._ebp} + {wordSize * 2}]
mov {Macros._ecx}, [{Macros._ebp} + {wordSize * 3}]
add {Macros._eax}, 1";

_addNoOriginalHook = ReloadedHooks.Instance.CreateAsmHook(addFunction, (long)_nativeCalculator.Add, options).Activate();

Expand Down
25 changes: 25 additions & 0 deletions source/Reloaded.Hooks/AsmHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ private AsmHook()
public AsmHook(string[] asmCode, nuint functionAddress, AsmHookBehaviour behaviour = AsmHookBehaviour.ExecuteFirst, int hookLength = -1)
: this(Utilities.Assembler.Assemble(asmCode), functionAddress, new AsmHookOptions() { Behaviour = behaviour, hookLength = hookLength })
{ }

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
/// </summary>
/// <param name="asmCode">
/// The assembly code to execute, in FASM syntax.
/// (Should start with use32/use64)
/// </param>
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
/// <param name="hookLength">Optional explicit length of hook. Use only in rare cases where auto-length check overflows a jmp/call opcode.</param>
public AsmHook(string asmCode, nuint functionAddress, AsmHookBehaviour behaviour = AsmHookBehaviour.ExecuteFirst, int hookLength = -1)
: this(Utilities.Assembler.Assemble(asmCode), functionAddress, new AsmHookOptions() { Behaviour = behaviour, hookLength = hookLength })
{ }

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
Expand All @@ -73,6 +87,17 @@ public AsmHook(string[] asmCode, nuint functionAddress, AsmHookBehaviour behavio
/// <param name="options">The options used for creating the assembly hook.</param>
public AsmHook(string[] asmCode, nuint functionAddress, AsmHookOptions options = default) : this(Utilities.Assembler.Assemble(asmCode), functionAddress, options)
{ }

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
/// </summary>
/// <param name="asmCode">
/// The assembly code to execute, in FASM syntax.
/// </param>
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
/// <param name="options">The options used for creating the assembly hook.</param>
public AsmHook(string asmCode, nuint functionAddress, AsmHookOptions options = default) : this(Utilities.Assembler.Assemble(asmCode), functionAddress, options)
{ }

/// <summary>
/// Creates a cheat engine style hook, replacing instruction(s) with a JMP to a user provided set of ASM instructions (and optionally the original ones).
Expand Down
6 changes: 5 additions & 1 deletion source/Reloaded.Hooks/ReloadedHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,21 @@ public class ReloadedHooks : IReloadedHooks
#endif
TDelegate>(ulong functionPointer) where TDelegate : Delegate => new FunctionPtr<TDelegate>(functionPointer);
public IAsmHook CreateAsmHook(string[] asmCode, long functionAddress) => CreateAsmHook(asmCode, functionAddress, AsmHookBehaviour.ExecuteFirst, -1);
public IAsmHook CreateAsmHook(string asmCode, long functionAddress) => CreateAsmHook(asmCode, functionAddress, AsmHookBehaviour.ExecuteFirst, -1);

public IAsmHook CreateAsmHook(byte[] asmCode, long functionAddress) => CreateAsmHook(asmCode, functionAddress, AsmHookBehaviour.ExecuteFirst, -1);

public IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookBehaviour behaviour) => CreateAsmHook(asmCode, functionAddress, behaviour, -1);

public IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookBehaviour behaviour) => CreateAsmHook(asmCode, functionAddress, behaviour, -1);

public IAsmHook CreateAsmHook(byte[] asmCode, long functionAddress, AsmHookBehaviour behaviour) => CreateAsmHook(asmCode, functionAddress, behaviour, -1);
public IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookBehaviour behaviour, int hookLength) => new AsmHook(asmCode, (nuint)functionAddress.ToUnsigned(), behaviour, hookLength);

public IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookBehaviour behaviour, int hookLength) => new AsmHook(asmCode, (nuint)functionAddress.ToUnsigned(), behaviour, hookLength);

public IAsmHook CreateAsmHook(byte[] asmCode, long functionAddress, AsmHookBehaviour behaviour, int hookLength) => new AsmHook(asmCode, (nuint)functionAddress.ToUnsigned(), behaviour, hookLength);
public IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookOptions options) => new AsmHook(asmCode, (nuint)functionAddress.ToUnsigned(), options);
public IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookOptions options) => new AsmHook(asmCode, (nuint)functionAddress.ToUnsigned(), options);

public IAsmHook CreateAsmHook(byte[] asmCode, long functionAddress, AsmHookOptions options) => new AsmHook(asmCode, (nuint)functionAddress.ToUnsigned(), options);

Expand Down

0 comments on commit 9662a67

Please sign in to comment.