Hello everyone,
Dhooks with detours is great extension for saving me time when i want to hook functions, but i have a great problem when i try to post hook a function
Description:
I'm hooking non-virtual member function and when I use a pre hook, it works fine and prints the message in the test plugin, but the return value is invalid(expected, because original function is not called). When I try to use post hook, when i try to build a building, it crashes the server. I firstly used detours6 version, then after the crash i updated to detours7, but crashes still occurs.
Test plugin:
PHP Code:
#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <tf2>
#include <tf2_stocks>
#include <dhooks>
#pragma newdecls required
#define PLUGIN_NAME "Build on buildings plugin"
#define PLUGIN_AUTHOR "Naydef"
#define PLUGIN_VERSION "1.0"
public Plugin myinfo =
{
name = PLUGIN_NAME,
author = PLUGIN_AUTHOR,
version = PLUGIN_VERSION,
};
Handle hDetourIsPlacementPosValid;
public void OnPluginStart()
{
Handle gamedatafile=LoadGameConfigFile("buildonbuildings_defs.games");
if(gamedatafile==null)
{
SetFailState("Cannot find file buildonbuildings_defs.games!");
}
hDetourIsPlacementPosValid=DHookCreateDetour(Address_Null, CallConv_THISCALL, ReturnType_Bool, ThisPointer_CBaseEntity);
if(hDetourIsPlacementPosValid==null)
{
SetFailState("Failed to create CBaseObject::IsPlacementPosValid detour!");
}
// Load the address of the function from PTaH's signature gamedata file.
if(!DHookSetFromConf(hDetourIsPlacementPosValid, gamedatafile, SDKConf_Signature, "CBaseObject::IsPlacementPosValid"))
{
SetFailState("Failed to load CBaseObject::IsPlacementPosValid signature from gamedata");
}
delete gamedatafile;
// Add a post hook on the function.
//if(!DHookEnableDetour(hDetourIsPlacementPosValid, false, Detour_IsPlacementPosValid)) // Not crashing
if(!DHookEnableDetour(hDetourIsPlacementPosValid, true, Detour_IsPlacementPosValid)) // Crashing
{
SetFailState("Failed to detour CBaseObject::IsPlacementPosValid!");
}
}
public MRESReturn Detour_IsPlacementPosValid(int pThis, Handle hReturn, Handle hParams)
{
if(pThis==-1)
{
return MRES_Ignored;
}
/*
if(DHookGetReturn(hReturn))
{
return MRES_Ignored;
}
*/
//bool result=false;
PrintToChatAll("Placement pos valid for: %i is %i", pThis, DHookGetReturn(hReturn));
//DHookSetReturn(hReturn, result);
//return MRES_ChangedOverride;
return MRES_Ignored;
}
The gamedata:
PHP Code:
"Games"
{
"#default"
{
"Signatures"
{
"CBaseObject::IsPlacementPosValid"
{
"library" "server"
"windows" "\x53\x8b\xdc\x83\xec\x08\x83\xe4\xf0\x83\xc4\x04\x55\x8b\x6b\x04\x89\x6c\x24\x04\x8b\xec\x81\xec\x48\x01\x00\x00\x56\x57\x8b\xf9\x8b\x07\x8b\x80\x18"
"linux" "_ZN11CBaseObject19IsPlacementPosValidEv"
}
}
}
}
Info:
DHooks with detours(detours6, upgraded to detours7)
Sourcemod 1.8.0.6028
Windows platform
Accelerator crash report from detours6 -
https://crash.limetech.org/jag75n4rfrem
Accelerator crash report from detours7 -
https://crash.limetech.org/rb6m2np5yorp
The crash looks to me like a NULL pointer problem
Edit:
The functions is actually a virtual function, but i doubt this will create cause the crash.
__________________