-
-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend DynamicHooks
to support virtual functions
#617
Open
KillStr3aK
wants to merge
14
commits into
roflmuffin:main
Choose a base branch
from
KillStr3aK:virtual-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+375
−254
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8284e52
feat: `CreateValveFunctionByOffset`
KillStr3aK 679ce6c
feat: overloaded constructor
KillStr3aK 73fe4ff
feat: now inherits from `MemoryFunction*`
KillStr3aK 05fde7c
Merge branch 'main' into virtual-hooks
KillStr3aK 3ddc0ba
Merge branch 'main' into virtual-hooks
KillStr3aK 033d69a
Merge branch 'main' into virtual-hooks
KillStr3aK 0f4b855
Merge branch 'main' into virtual-hooks
KillStr3aK 077d1eb
Merge branch 'main' into virtual-hooks
KillStr3aK 96489f3
Merge branch 'main' into virtual-hooks
KillStr3aK 8d60cf9
Merge branch 'main' into virtual-hooks
KillStr3aK 78639d4
Merge branch 'main' into virtual-hooks
KillStr3aK e56eb54
Merge branch 'main' into virtual-hooks
KillStr3aK dc87751
Merge branch 'main' into virtual-hooks
KillStr3aK d999bf1
Merge branch 'main' into virtual-hooks
KillStr3aK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,9 @@ namespace CounterStrikeSharp.API.Modules.Memory.DynamicFunctions; | |
|
||
public abstract class BaseMemoryFunction : NativeObject | ||
{ | ||
private static Dictionary<string, IntPtr> _createdFunctions = new(); | ||
private static Dictionary<string, IntPtr> _createdFunctions = new(); | ||
|
||
private static Dictionary<IntPtr, Dictionary<int, IntPtr>> _createdOffsetFunctions = new(); | ||
|
||
private static IntPtr CreateValveFunctionBySignature(string signature, DataType returnType, | ||
DataType[] argumentTypes) | ||
|
@@ -45,6 +47,30 @@ private static IntPtr CreateValveFunctionBySignature(string signature, string bi | |
} | ||
|
||
return function; | ||
} | ||
|
||
private static IntPtr CreateValveFunctionByOffset(IntPtr objectPtr, int offset, DataType returnType, | ||
DataType[] argumentTypes) | ||
{ | ||
if (!_createdOffsetFunctions.TryGetValue(objectPtr, out var createdFunctions)) | ||
{ | ||
createdFunctions = new Dictionary<int, IntPtr>(); | ||
_createdOffsetFunctions[objectPtr] = createdFunctions; | ||
} | ||
|
||
if (!createdFunctions.TryGetValue(offset, out var function)) | ||
{ | ||
try | ||
{ | ||
function = NativeAPI.CreateVirtualFunction(objectPtr, offset, | ||
argumentTypes.Length, (int)returnType, argumentTypes.Cast<object>().ToArray()); | ||
createdFunctions[offset] = function; | ||
} catch (Exception) | ||
{ | ||
} | ||
} | ||
|
||
return function; | ||
} | ||
|
||
public BaseMemoryFunction(string signature, DataType returnType, DataType[] parameters) : base( | ||
|
@@ -55,6 +81,14 @@ public BaseMemoryFunction(string signature, DataType returnType, DataType[] para | |
public BaseMemoryFunction(string signature, string binarypath, DataType returnType, DataType[] parameters) : base( | ||
CreateValveFunctionBySignature(signature, binarypath, returnType, parameters)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// <b>WARNING:</b> this is only supposed to be used with <see cref="VirtualFunctionVoid"/> and <see cref="VirtualFunctionWithReturn{TResult}"/> | ||
/// </summary> | ||
internal BaseMemoryFunction(IntPtr objectPtr, int offset, DataType returnType, DataType[] parameters) : base( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and related constructors are |
||
CreateValveFunctionByOffset(objectPtr, offset, returnType, parameters)) | ||
{ | ||
} | ||
|
||
public void Hook(Func<DynamicHook, HookResult> handler, HookMode mode) | ||
|
@@ -76,4 +110,4 @@ protected void InvokeInternalVoid(params object[] args) | |
{ | ||
NativeAPI.ExecuteVirtualFunction<object>(Handle, args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might need a better way? if we pass another entity instance (ptr) with the same offset it will probably try to hook the same function again and fail? (untested currently, will answer here when I can test it)