Would it be possible to add a return type that skips the game function call entirely for void functions ?
Currently it explicity checks when using MRES_Supercede.
That code is just checking for allocating space to store the return value - supercede should work fine for void functions.
Аfter sourcemod 1.11.6609 and above, dhooks detours causes server crashes on startup. After updating sdkhooks extension (OnEntitySpawned forward)
Code:
---- Host_NewGame ----
maxplayers set to 32
Host_NewGame on map c1m1_hotel
ConVarRef room_type doesn't point to an existing ConVar
Executing dedicated server config file
./srcds_run: line 341: 5383 Segmentation fault $HL_CMD
Add "-debug" to the ./srcds_run command line to generate a debug.log to help with solving this problem
Even the "Accelerator" extension fails to create a crash dump.
This should be fixed in the next-ish SM build, thanks!
In the future, best to file crashes caused by SM upgrades as GitHub issues against SourceMod - they're more likely to be seen quickly.
So i have a plugin that removes some entities on SDKHook_Spawn and another plugin that dhooks an entity on SDKHook_Spawn. Whenever I kill an entity that is hooked as well, the server crashes.
I changed SDKHook_Spawn to SDKHook_SpawnPost for the dhook thing, and it's fine now. Should i dunhook the entitiy on EntityDestroyed?
I hook AcceptInput and i use AcceptEntityInput to kill entities.
Would you please help me to find a reason why I can't run dhook on my server?
I have the latest metamode and sourcemode. I copied 9, 10, 13 and 14 version of dhooks to sourcemod folder to check. All of them are for sourcemode 1.10 (dhooks-2.2.0-detours14a-sm110).
After server restart I do not see dhooks in the ext lists. I do not see any errors in the console and in sourcemod logs.
Please tell me where to look for...
Code:
hostname: GunGame
version : 1.37.6.8/13768 1181/7929 secure [G:1:2741606]
udp/ip : 195.211.102.52:27510 (public ip: 195.211.102.52)
os : Linux
Metamod:Source version 1.10.7-dev
Built from: https://github.com/alliedmodders/metamod-source/commit/6c8495f
Build ID: 971:6c8495f
Loaded As: Valve Server Plugin
Compiled on: Sep 2 2019
Plugin interface version: 15:14
SourceHook version: 5:5
http://www.metamodsource.net/
SourceMod Version: 1.10.0.6497
SourcePawn Engine: 1.10.0.6497, jit-x86 (build 1.10.0.6497)
SourcePawn API: v1 = 5, v2 = 12
Compiled on: Sep 24 2020 19:39:13
Built from: https://github.com/alliedmodders/sourcemod/commit/39c2dc6
Build ID: 6497:39c2dc6
Meta list
Listing 5 plugins:
[01] SourceMod (1.10.0.6497) by AlliedModders LLC
[02] Query Cache 2 (1.0.0.1) by Hashira
[03] CS Tools (1.10.0.6497) by AlliedModders LLC
[04] SDK Tools (1.10.0.6497) by AlliedModders LLC
[05] SDK Hooks (1.10.0.6497) by AlliedModders LLC
[SM] Displaying 13 extensions:
[01] Automatic Updater (1.10.0.6497): Updates SourceMod gamedata files
[02] Webternet (1.10.0.6497): Extension for interacting with URLs
[03] cURL Extension (1.3.0.0): cURL Extension
[04] CS Tools (1.10.0.6497): CS extended functionality
[05] BinTools (1.10.0.6497): Low-level C/C++ Calling API
[06] SDK Tools (1.10.0.6497): Source SDK Tools
[07] GeoIP (1.10.0.6497): Geographical IP information
[08] Client Preferences (1.10.0.6497): Saves client preference settings
[09] SQLite (1.10.0.6497): SQLite Driver
[10] Top Menus (1.10.0.6497): Creates sorted nested menus
[11] Regex (1.10.0.6497): Provides regex natives for plugins
[12] SDK Hooks (1.10.0.6497): Source SDK Hooks
[13] MySQL-DBI (1.10.0.6497): MySQL driver implementation for DBI
I've put up a new version detours15 which adds a methodmap API for your pleasure. Call arguments are saved before calling the function now by default too, so it's save to access the parameters in a post-hook.
There is a new DynamicHook methodmap for virtual hooks replacing the DHookCreate family and a new DynamicDetour methodmap for the DHookCreateDetour stuff. Return value and parameter handles pushed to the callbacks have new shiny methodmaps too. The old natives haven't been touched and work just as before.
The magic bool post parameter during hooking has been replaced with a more readable enum in the methodmap to tell if you want a pre- or post-hook.
PHP Code:
enum HookMode { Hook_Pre, // Callback will be executed BEFORE the original function. Hook_Post // Callback will be executed AFTER the original function. };
Methodmap definition:
Spoiler
PHP Code:
// Represents the parameters of the hooked function. methodmap DHookParam < Handle { // Get the value of a parameter. // Use only for: int, entity, edict, bool or float parameter types. // // @param num Parameter number to get, starting at 1. Parameter number 0 returns // the number of parameters. // // @return Value if num greater than 0. If 0 returns parameter count. // If CBaseEntity returns entity index. // @error Invalid handle, invalid param number or invalid param type. public native any Get(int num);
// Get the value of a vector parameter. // Use only for: vector or vectorptr parameter types. // // @param num Parameter number to get, starting at 1. // @param vec Vector buffer to store result. // // @error Invalid handle, invalid param number or invalid param type. public native void GetVector(int num, float vec[3]);
// Get the value of a string parameter. // Use only for: string, stringptr or charptr parameter types. // // @param num Parameter number to get, starting at 1. // @param buffer String buffer to store result. // @param size Buffer size. // // @error Invalid handle, invalid param number or invalid param type. public native void GetString(int num, char[] buffer, int size);
// Set the value of a parameter. // Use only for: int, entity, edict, bool or float parameter types. // // The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride // is returned in the callback. // // @param num Parameter number to set starting at 1. // @param value Value to set it as (only pass int, bool, float or entity index). // // @error Invalid handle, invalid param number or invalid param type. public native void Set(int num, any value);
// Set the value of a vector parameter. // Use only for: vector or vectorptr parameter types. // // The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride // is returned in the callback. // // @param num Parameter number to set, starting at 1. // @param vec Value to set vector as. // // @error Invalid handle, invalid param number or invalid param type. public native void SetVector(int num, const float vec[3]);
// Set the value of a string parameter. // Use only for: string, stringptr or charptr parameter types. // // The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride // is returned in the callback. // // @param num Parameter number to set, starting at 1. // @param value Value to set string as. // // @error Invalid handle, invalid param number or invalid param type. public native void SetString(int num, const char[] value);
// Gets an object's variable value. // // @param num Parameter number to get, starting at 1. // @param offset Byte offset within the object to the var to get. // @param type Type of var it is. // // @return Value of the objects var. If EHANDLE type or entity returns entity index. // @error Invalid handle, invalid param number, invalid param type or invalid Object type. public native any GetObjectVar(int num, int offset, ObjectValueType type);
// Gets an object's vector variable value. // // @param num Parameter number to get, starting at 1. // @param offset Byte offset within the object to the var to get. // @param type Type of var it is. // @param vec Buffer to store the result vector. // // @error Invalid handle, invalid param number, invalid param type or invalid Object type. public native void GetObjectVarVector(int num, int offset, ObjectValueType type, float vec[3]);
// Gets an object's string variable value. // // @param num Parameter number to get, starting at 1. // @param offset Byte offset within the object to the var to get. // @param type Type of var it is. // @param buffer Buffer to store the result string. // @param size Size of the buffer. // // @error Invalid handle, invalid param number, invalid param type or invalid Object type. public native void GetObjectVarString(int num, int offset, ObjectValueType type, char[] buffer, int size);
// Sets an object's variable value. // // The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride // is returned in the callback. // // @param num Parameter number to set, starting at 1. // @param offset Byte offset within the object to the var to set. // @param type Type of var it is. // @param value The value to set the var to. // // @error Invalid handle, invalid param number, invalid param type or invalid Object type. public native void SetObjectVar(int num, int offset, ObjectValueType type, any value);
// Sets an object's vector variable value. // // The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride // is returned in the callback. // // @param num Parameter number to set, starting at 1. // @param offset Byte offset within the object to the var to set. // @param type Type of var it is. // @param vec The value to set the vector var to. // // @error Invalid handle, invalid param number, invalid param type or invalid Object type. public native void SetObjectVarVector(int num, int offset, ObjectValueType type, const float vec[3]);
// No setter for object strings yet. Open an issue if you really need it.
// Checks if a pointer parameter is null. // // @param num Parameter number to check, starting at 1. // // @return True if null, false otherwise. // @error Non-pointer parameter. public native bool IsNull(int num); };
// Represents the return value of the hooked function. methodmap DHookReturn < Handle { // Retrieves or sets the return value. // Use only for: int, entity, edict, bool or float return types. // // The return value is only readable in a post hook. // The value is only applied when MRES_Override or MRES_Supercede is returned // in the callback. property any Value { public native get(); public native set(any value); }
// Get return vector value. // Use only for: vector or vectorptr return types. // // Only useful in post hooks. // // @param vec Vector buffer to store result in. // // @error Invalid Handle or invalid type. public native void GetVector(float vec[3]);
// Get return string value. // Use only for: string, stringptr or charptr return types. // // Only useful in post hooks. // // @param buffer String buffer to store result in. // @param size String buffer size. // // @error Invalid Handle or invalid type. public native void GetString(char[] buffer, int size);
// Set return vector value. // Use only for: vector or vectorptr return types. // // The value is only applied when MRES_Override or MRES_Supercede is returned // in the callback. // // @param vec Value to set return vector to. // // @error Invalid Handle or invalid type. public native void SetVector(const float vec[3]);
// Set return string value. // Use only for: string, stringptr or charptr return types. // // The value is only applied when MRES_Override or MRES_Supercede is returned // in the callback. // // @param buffer Value to set return string to. // // @error Invalid Handle or invalid type. public native void SetString(const char[] buffer); };
// Base method map for common functions between virtual hooks and detours. methodmap DHookSetup < Handle { // Load address or offset for a vtable hook or detour from a gamedata file. // // @param gameconf GameData handle. // @param source Whether to look in Offsets, Signatures, or Addresses. // @param name Name of the property to find. // // @return True on success, false if nothing was found. // @error Invalid setup or gamedata handle. public native bool SetFromConf(GameData gameconf, SDKFuncConfSource source, const char[] name);
// Adds a parameter to a hook setup. // // @param type Parameter type. // @param size Used for Objects (not Object ptr) to define the size of the object. // @param flag Used to change the pass type (ignored by detours). // @param custom_register The register this argument is passed in instead of the stack (ignored by vhooks). // // @error Invalid setup handle or too many params added (request upping the max in thread). public native void AddParam(HookParamType type, int size=-1, DHookPassFlag flag=DHookPass_ByVal, DHookRegister custom_register=DHookRegister_Default); };
// A DynamicHook allows to hook a virtual function on any C++ object. // Currently CBaseEntity and CGameRules have a convenience API for easy entity hooking, // but it's possible to provide a raw this-pointer to hook any object in memory too. // // Internally this intercepts function calls by replacing the function pointer // in the virtual table of the object with our own function. methodmap DynamicHook < DHookSetup { // Creates a vtable hook. // // @param offset Virtual table offset of function to hook. // @param hooktype Type of hook. // @param returntype Type of return value. // @param thistype Type of this pointer or ignore (ignore can be used if not needed). // // @error Failed to create hook setup handle or invalid callback function. public native DynamicHook(int offset, HookType hooktype, ReturnType returntype, ThisPointerType thistype);
// Setup a vtable hook for a function as described in a "Functions" section in gamedata. // The "Functions" section is parsed once the gamedata file is loaded and cached globally. // // @param gameconf GameData handle to use for address lookup. // Doesn't have to be the same as the one with the "Functions" section. // @param name Name of the function in a "Functions" section to load. // // @return Setup handle for the detour or null if offset wasn't found. // @error Failed to create detour setup handle, invalid gamedata handle, // invalid callback function or failed to find function in cached "Functions" sections. public static native DynamicHook FromConf(GameData gameconf, const char[] name);
// Hook an entity. // // Entity hooks are auto-removed when the entity is destroyed. // If you need to read the return value of the function, choose a post hook. // // @param mode The desired hook mode - pre or post. // A pre hook calls your callback BEFORE the original function is called. // You can access the parameters, set the return value, and skip the original function. // A post hook calls your callback AFTER the original function executed. // You can access the parameters and get/set the return value. // @param entity Entity index to hook on. // @param callback Callback function. // @param removalcb Optional callback for when the hook is removed. // // @return A hookid on success, INVALID_HOOK_ID otherwise. // @error Invalid setup handle, invalid address, invalid hook type or invalid callback. public native int HookEntity(HookMode mode, int entity, DHookCallback callback, DHookRemovalCB removalcb=INVALID_FUNCTION);
// Hook gamerules object. // // Game rules hooks are auto-removed on map end. // If you need to read the return value of the function, choose a post hook. // // @param mode The desired hook mode - pre or post. // A pre hook calls your callback BEFORE the original function is called. // You can access the parameters, set the return value, and skip the original function. // A post hook calls your callback AFTER the original function executed. // You can access the parameters and get/set the return value. // @param callback Callback function. // @param removalcb Optional callback for when the hook is removed. // // @return A hookid on success, INVALID_HOOK_ID otherwise. // @error Invalid setup handle, invalid address, invalid hook type or invalid callback. public native int HookGamerules(HookMode mode, DHookCallback callback, DHookRemovalCB removalcb=INVALID_FUNCTION);
// Hook a raw this-pointer. // If you need to read the return value of the function, choose a post hook. // // @param mode The desired hook mode - pre or post. // A pre hook calls your callback BEFORE the original function is called. // You can access the parameters, set the return value, and skip the original function. // A post hook calls your callback AFTER the original function executed. // You can access the parameters and get/set the return value. // @param addr This pointer address. // @param callback Callback function. // // @return A hookid on success, INVALID_HOOK_ID otherwise. // @error Invalid setup handle, invalid address, invalid hook type or invalid callback. public native int HookRaw(HookMode mode, Address addr, DHookCallback callback);
// Remove hook by hook id: // This will NOT fire the removal callback! // // @param hookid Hook id to remove. // // @return True on success, false otherwise public static native bool RemoveHook(int hookid); };
// A DynamicDetour is a way to hook and block any function in memory. // Given the address of a function, it can call a callback in your script whenever // the function gets called. The callback has access to all parameters of the function // as well as the return value. // // Internally this works by replacing the first instructions of the function // with a jump to our own code. This means that the signature used to find // the function address in the first place might not match anymore after a detour. // If you need to detour the same function in different plugins make sure to // wildcard \x2a the first 6 bytes of the signature to accommodate for the patched // jump introduced by the detour. methodmap DynamicDetour < DHookSetup { // Creates a detour. // // @param funcaddr The address of the function to detour. // Can be Address_Null if you want to load the address from gamedata using DHookSetFromConf. // @param callConv Calling convention of the function. // @param returnType Type of the return value. // @param thisType Type of this pointer or ignore (ignore can be used if not needed). // Only used for thiscall detours. // // @error Failed to create detour setup handle. public native DynamicDetour(Address funcaddr, CallingConvention callConv, ReturnType returntype, ThisPointerType thisType=ThisPointer_Ignore);
// Setup a detour for a function as described in a "Functions" section in gamedata. // The "Functions" section is parsed once the gamedata file is loaded and cached globally. // // @param gameconf GameData handle to use for address lookup. // Doesn't have to be the same as the one with the "Functions" section. // @param name Name of the function in a "Functions" section to load. // // @return Setup handle for the detour or null if offset wasn't found. // @error Failed to create detour setup handle, invalid gamedata handle, // invalid callback function or failed to find function in cached "Functions" sections. public static native DynamicDetour FromConf(GameData gameconf, const char[] name);
// Enable the detour of the function described in this detour setup. // If you need to read the return value of the function, choose a post hook. // // @param mode The desired hook mode - pre or post. // A pre hook calls your callback BEFORE the original function is called. // You can access the parameters, set the return value, and skip the original function. // A post hook calls your callback AFTER the original function executed. // You can access the parameters and get/set the return value. // @param callback Callback function. // // @return True if detour was enabled, false otherwise. // @error Hook handle is not setup for a detour. public native bool Enable(HookMode mode, DHookCallback callback);
// Disable the detour of the function described in this detour setup. // // @param mode The hook mode to disable - pre or post. // @param callback Callback function. // // @return True if detour was disabled, false otherwise. // @error Hook handle is not setup for a detour or function is not detoured. public native bool Disable(HookMode mode, DHookCallback callback); };
Whenever I try to compile, I get these errors from the .inc file.
Code:
// C:\Program Files (x86)\Steam\steamapps\common\Counter-Strike Source\cstrike\addons\sourcemod\scripting\include\dhooks.inc(414) : error 139: could not find type "GameData"
// C:\Program Files (x86)\Steam\steamapps\common\Counter-Strike Source\cstrike\addons\sourcemod\scripting\include\dhooks.inc(455) : error 139: could not find type "GameData"
// C:\Program Files (x86)\Steam\steamapps\common\Counter-Strike Source\cstrike\addons\sourcemod\scripting\include\dhooks.inc(551) : error 139: could not find type "GameData"