View Single Post
Naydef
Senior Member
Join Date: Dec 2015
Location: Doom Town, Nevada
Old 01-11-2019 , 14:12   Re: DHooks (Dynamic Hooks - Dev Preview)
Reply With Quote #642

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_NullCallConv_THISCALLReturnType_BoolThisPointer_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(hDetourIsPlacementPosValidgamedatafileSDKConf_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(hDetourIsPlacementPosValidtrueDetour_IsPlacementPosValid)) // Crashing
    
{
        
SetFailState("Failed to detour CBaseObject::IsPlacementPosValid!");
    }
}

public 
MRESReturn Detour_IsPlacementPosValid(int pThisHandle hReturnHandle hParams)
{
    if(
pThis==-1)
    {
        return 
MRES_Ignored;
    }
    
/*
    if(DHookGetReturn(hReturn))
    {
        return MRES_Ignored;
    }
    */
    //bool result=false;
    
    
PrintToChatAll("Placement pos valid for: %i is %i"pThisDHookGetReturn(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.
__________________
My plugins:
*None for now*


Steam:
naydef

Last edited by Naydef; 01-11-2019 at 14:24.
Naydef is offline