Skip to content

Commit

Permalink
feat: add virtual function invocation via pointer or signature
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed Oct 14, 2023
1 parent 238408e commit 1b9053f
Show file tree
Hide file tree
Showing 13 changed files with 860 additions and 143 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Project(counterstrikesharp C CXX)
include("makefiles/shared.cmake")

add_subdirectory(libraries/spdlog)
add_subdirectory(libraries/dyncall)
add_subdirectory(libraries/funchook)

SET(SOURCE_FILES
Expand Down Expand Up @@ -52,6 +53,9 @@ SET(SOURCE_FILES
src/core/managers/con_command_manager.cpp
src/core/managers/con_command_manager.h
src/scripting/natives/natives_commands.cpp
src/core/function.cpp
src/core/function.h
src/scripting/natives/natives_memory.cpp
)

set(PROTO_DIRS -I${CMAKE_CURRENT_SOURCE_DIR}/libraries/GameTracking-CS2/Protobufs)
Expand Down
2 changes: 2 additions & 0 deletions makefiles/linux.base.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ SET(
${SOURCESDK_LIB}/linux64/interfaces.a
${SOURCESDK_LIB}/linux64/mathlib.a
spdlog
dynload_s
dyncall_s
distorm
funchook-shared
)
2 changes: 2 additions & 0 deletions makefiles/shared.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ include_directories(
${SOURCESDK}/public/entity2
${METAMOD_DIR}/core
${METAMOD_DIR}/core/sourcehook
libraries/dyncall/dynload
libraries/dyncall/dyncall
libraries/spdlog/include
libraries/tl
libraries/funchook/include
Expand Down
76 changes: 64 additions & 12 deletions managed/CounterStrikeSharp.API/Core/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,6 @@ public static IntPtr GetValveInterface(int interfacetype, string interfacename){
}
}

public static IntPtr FindSignature(string modulepath, string signature){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(modulepath);
ScriptContext.GlobalScriptContext.Push(signature);
ScriptContext.GlobalScriptContext.SetIdentifier(0xE9E1819B);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (IntPtr)ScriptContext.GlobalScriptContext.GetResult(typeof(IntPtr));
}
}

public static void HookEvent(string name, InputArgument callback, bool ispost){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
Expand Down Expand Up @@ -597,6 +585,70 @@ public static void SetEventUint64(IntPtr gameevent, string name, ulong value){
}
}

public static IntPtr CreateVirtualFunction(IntPtr pointer, int vtableoffset, int numarguments, int returntype, object[] arguments){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(pointer);
ScriptContext.GlobalScriptContext.Push(vtableoffset);
ScriptContext.GlobalScriptContext.Push(numarguments);
ScriptContext.GlobalScriptContext.Push(returntype);
foreach (var obj in arguments)
{
ScriptContext.GlobalScriptContext.Push(obj);
}
ScriptContext.GlobalScriptContext.SetIdentifier(0x2531DA2);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (IntPtr)ScriptContext.GlobalScriptContext.GetResult(typeof(IntPtr));
}
}

public static IntPtr CreateVirtualFunctionBySignature(IntPtr pointer, string binaryname, string signature, int numarguments, int returntype, object[] arguments){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(pointer);
ScriptContext.GlobalScriptContext.Push(binaryname);
ScriptContext.GlobalScriptContext.Push(signature);
ScriptContext.GlobalScriptContext.Push(numarguments);
ScriptContext.GlobalScriptContext.Push(returntype);
foreach (var obj in arguments)
{
ScriptContext.GlobalScriptContext.Push(obj);
}
ScriptContext.GlobalScriptContext.SetIdentifier(0x8D25187D);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (IntPtr)ScriptContext.GlobalScriptContext.GetResult(typeof(IntPtr));
}
}

public static object ExecuteVirtualFunction(IntPtr function, object[] arguments){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(function);
foreach (var obj in arguments)
{
ScriptContext.GlobalScriptContext.Push(obj);
}
ScriptContext.GlobalScriptContext.SetIdentifier(0x376A0359);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (object)ScriptContext.GlobalScriptContext.GetResult(typeof(object));
}
}

public static IntPtr FindSignature(string modulepath, string signature){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(modulepath);
ScriptContext.GlobalScriptContext.Push(signature);
ScriptContext.GlobalScriptContext.SetIdentifier(0xE9E1819B);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (IntPtr)ScriptContext.GlobalScriptContext.GetResult(typeof(IntPtr));
}
}

public static IntPtr CreateTimer(float interval, InputArgument callback, int flags){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
using System;
using System.Collections.Generic;

namespace CounterStrikeSharp.API.Modules.Memory.Constants
namespace CounterStrikeSharp.API.Modules.Memory
{
public enum DataType
{
DATA_TYPE_VOID,
DATA_TYPE_VOID,
DATA_TYPE_BOOL,
DATA_TYPE_CHAR,
DATA_TYPE_UCHAR,
Expand All @@ -44,13 +44,13 @@ public static class DataTypeExtensions
{
private static Dictionary<Type, DataType> types = new Dictionary<Type, DataType>()
{
{typeof(float), DataType.DATA_TYPE_FLOAT},
{typeof(IntPtr), DataType.DATA_TYPE_POINTER},
{typeof(int), DataType.DATA_TYPE_INT},
{typeof(bool), DataType.DATA_TYPE_BOOL},
{typeof(string), DataType.DATA_TYPE_STRING},
{typeof(ulong), DataType.DATA_TYPE_ULONG },
{typeof(short), DataType.DATA_TYPE_VARIANT }
{ typeof(float), DataType.DATA_TYPE_FLOAT },
{ typeof(IntPtr), DataType.DATA_TYPE_POINTER },
{ typeof(int), DataType.DATA_TYPE_INT },
{ typeof(bool), DataType.DATA_TYPE_BOOL },
{ typeof(string), DataType.DATA_TYPE_STRING },
{ typeof(ulong), DataType.DATA_TYPE_ULONG },
{ typeof(short), DataType.DATA_TYPE_VARIANT }
};

public static DataType? ToDataType(this Type type)
Expand Down
Loading

0 comments on commit 1b9053f

Please sign in to comment.