Skip to content

Commit 9662a67

Browse files
committed
Added: AsmHook APIs with string parameter for code.
1 parent 2906230 commit 9662a67

5 files changed

Lines changed: 86 additions & 13 deletions

File tree

source/Reloaded.Hooks.Definitions/IReloadedHooks.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ IFunctionPtr<TDelegate> CreateFunctionPtr<
296296
/// </param>
297297
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
298298
IAsmHook CreateAsmHook(string[] asmCode, long functionAddress);
299+
300+
/// <summary>
301+
/// 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).
302+
/// </summary>
303+
/// <param name="asmCode">
304+
/// The assembly code to execute, in FASM syntax.
305+
/// (Should start with use32/use64)
306+
/// </param>
307+
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
308+
IAsmHook CreateAsmHook(string asmCode, long functionAddress);
299309

300310
/// <summary>
301311
/// 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).
@@ -314,7 +324,18 @@ IFunctionPtr<TDelegate> CreateFunctionPtr<
314324
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
315325
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
316326
IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookBehaviour behaviour);
317-
327+
328+
/// <summary>
329+
/// 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).
330+
/// </summary>
331+
/// <param name="asmCode">
332+
/// The assembly code to execute, in FASM syntax.
333+
/// (Should start with use32/use64)
334+
/// </param>
335+
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
336+
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
337+
IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookBehaviour behaviour);
338+
318339
/// <summary>
319340
/// 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).
320341
/// </summary>
@@ -334,6 +355,18 @@ IFunctionPtr<TDelegate> CreateFunctionPtr<
334355
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
335356
/// <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>
336357
IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookBehaviour behaviour, int hookLength);
358+
359+
/// <summary>
360+
/// 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).
361+
/// </summary>
362+
/// <param name="asmCode">
363+
/// The assembly code to execute, in FASM syntax.
364+
/// (Should start with use32/use64)
365+
/// </param>
366+
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
367+
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
368+
/// <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>
369+
IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookBehaviour behaviour, int hookLength);
337370

338371
/// <summary>
339372
/// 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).
@@ -354,6 +387,17 @@ IFunctionPtr<TDelegate> CreateFunctionPtr<
354387
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
355388
/// <param name="options">Controls the generation behaviour of assembly hooks.</param>
356389
IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookOptions options);
390+
391+
/// <summary>
392+
/// 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).
393+
/// </summary>
394+
/// <param name="asmCode">
395+
/// The assembly code to execute, in FASM syntax.
396+
/// (Should start with use32/use64)
397+
/// </param>
398+
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
399+
/// <param name="options">Controls the generation behaviour of assembly hooks.</param>
400+
IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookOptions options);
357401

358402
/// <summary>
359403
/// 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).
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-

1+
Reloaded.Hooks.Definitions.IReloadedHooks.CreateAsmHook(string asmCode, long functionAddress) -> Reloaded.Hooks.Definitions.IAsmHook
2+
Reloaded.Hooks.Definitions.IReloadedHooks.CreateAsmHook(string asmCode, long functionAddress, Reloaded.Hooks.Definitions.AsmHookOptions options) -> Reloaded.Hooks.Definitions.IAsmHook
3+
Reloaded.Hooks.Definitions.IReloadedHooks.CreateAsmHook(string asmCode, long functionAddress, Reloaded.Hooks.Definitions.Enums.AsmHookBehaviour behaviour) -> Reloaded.Hooks.Definitions.IAsmHook
4+
Reloaded.Hooks.Definitions.IReloadedHooks.CreateAsmHook(string asmCode, long functionAddress, Reloaded.Hooks.Definitions.Enums.AsmHookBehaviour behaviour, int hookLength) -> Reloaded.Hooks.Definitions.IAsmHook

source/Reloaded.Hooks.Tests.X64/CalculatorAsmHookTest.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@ public void Dispose()
4242
private void TestHookAddNoOriginal_Internal(AsmHookOptions options)
4343
{
4444
int wordSize = IntPtr.Size;
45-
string[] addFunction =
46-
{
47-
$"{Macros._use32}",
48-
$"push {Macros._ebp}",
49-
$"mov {Macros._ebp}, {Macros._esp}",
50-
51-
$"mov {Macros._eax}, [{Macros._ebp} + {wordSize * 2}]", // Left Parameter
52-
$"mov {Macros._ecx}, [{Macros._ebp} + {wordSize * 3}]", // Right Parameter
53-
$"add {Macros._eax}, 1", // Left Parameter
54-
};
45+
string addFunction =
46+
@$"{Macros._use32}
47+
push {Macros._ebp}
48+
mov {Macros._ebp}, {Macros._esp}
49+
mov {Macros._eax}, [{Macros._ebp} + {wordSize * 2}]
50+
mov {Macros._ecx}, [{Macros._ebp} + {wordSize * 3}]
51+
add {Macros._eax}, 1";
5552

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

source/Reloaded.Hooks/AsmHook.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ private AsmHook()
5050
public AsmHook(string[] asmCode, nuint functionAddress, AsmHookBehaviour behaviour = AsmHookBehaviour.ExecuteFirst, int hookLength = -1)
5151
: this(Utilities.Assembler.Assemble(asmCode), functionAddress, new AsmHookOptions() { Behaviour = behaviour, hookLength = hookLength })
5252
{ }
53+
54+
/// <summary>
55+
/// 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).
56+
/// </summary>
57+
/// <param name="asmCode">
58+
/// The assembly code to execute, in FASM syntax.
59+
/// (Should start with use32/use64)
60+
/// </param>
61+
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
62+
/// <param name="behaviour">Defines what should be done with the original code that was replaced with the JMP instruction.</param>
63+
/// <param name="hookLength">Optional explicit length of hook. Use only in rare cases where auto-length check overflows a jmp/call opcode.</param>
64+
public AsmHook(string asmCode, nuint functionAddress, AsmHookBehaviour behaviour = AsmHookBehaviour.ExecuteFirst, int hookLength = -1)
65+
: this(Utilities.Assembler.Assemble(asmCode), functionAddress, new AsmHookOptions() { Behaviour = behaviour, hookLength = hookLength })
66+
{ }
5367

5468
/// <summary>
5569
/// 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).
@@ -73,6 +87,17 @@ public AsmHook(byte[] asmCode, nuint functionAddress, AsmHookBehaviour behaviour
7387
/// <param name="options">The options used for creating the assembly hook.</param>
7488
public AsmHook(string[] asmCode, nuint functionAddress, AsmHookOptions options = default) : this(Utilities.Assembler.Assemble(asmCode), functionAddress, options)
7589
{ }
90+
91+
/// <summary>
92+
/// 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).
93+
/// </summary>
94+
/// <param name="asmCode">
95+
/// The assembly code to execute, in FASM syntax.
96+
/// </param>
97+
/// <param name="functionAddress">The address of the function or mid-function to hook.</param>
98+
/// <param name="options">The options used for creating the assembly hook.</param>
99+
public AsmHook(string asmCode, nuint functionAddress, AsmHookOptions options = default) : this(Utilities.Assembler.Assemble(asmCode), functionAddress, options)
100+
{ }
76101

77102
/// <summary>
78103
/// 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).

source/Reloaded.Hooks/ReloadedHooks.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,21 @@ public IFunctionPtr<TDelegate> CreateFunctionPtr<
172172
#endif
173173
TDelegate>(ulong functionPointer) where TDelegate : Delegate => new FunctionPtr<TDelegate>(functionPointer);
174174
public IAsmHook CreateAsmHook(string[] asmCode, long functionAddress) => CreateAsmHook(asmCode, functionAddress, AsmHookBehaviour.ExecuteFirst, -1);
175+
public IAsmHook CreateAsmHook(string asmCode, long functionAddress) => CreateAsmHook(asmCode, functionAddress, AsmHookBehaviour.ExecuteFirst, -1);
175176

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

178179
public IAsmHook CreateAsmHook(string[] asmCode, long functionAddress, AsmHookBehaviour behaviour) => CreateAsmHook(asmCode, functionAddress, behaviour, -1);
179-
180+
public IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookBehaviour behaviour) => CreateAsmHook(asmCode, functionAddress, behaviour, -1);
181+
180182
public IAsmHook CreateAsmHook(byte[] asmCode, long functionAddress, AsmHookBehaviour behaviour) => CreateAsmHook(asmCode, functionAddress, behaviour, -1);
183+
public IAsmHook CreateAsmHook(string asmCode, long functionAddress, AsmHookBehaviour behaviour, int hookLength) => new AsmHook(asmCode, (nuint)functionAddress.ToUnsigned(), behaviour, hookLength);
181184

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

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

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

0 commit comments

Comments
 (0)