Raised This Month: $ Target: $400
 0% 

Detour AcceptInput


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
glhf3000
Member
Join Date: Aug 2010
Location: middle-universe
Old 02-12-2024 , 11:34   Detour AcceptInput
Reply With Quote #1

Hello!
I tried to detour CBaseEntity::AcceptInput(char const*,CBaseEntity*,CBaseEntity*,variant_t,in t) reusing @Silvers Input Hooks code and got these:
Code:
L 02/12/2024 - 19:26:09: [SM] Exception reported: Invalid Handle 245 (error 7)
L 02/12/2024 - 19:26:09: [SM] Blaming: hooktp.smx
L 02/12/2024 - 19:26:09: [SM] Call stack trace:
L 02/12/2024 - 19:26:09: [SM]   [0] DHookGetParamString
L 02/12/2024 - 19:26:09: [SM]   [1] Line 66, D:\SourceMod\scripting\hooktp.sp::Detour_AcceptInput
Gamedata:
Code:
"Games"
{
	"left4dead2"
	{
		"Functions"
		{
			"AcceptInput"
			{
				"signature"     "AcceptInput"
				"callconv"      "thiscall"
				"return"        "int"
				"this"			"entity"
				"arguments"
				{
					"name"
					{
						"type" 	 "charptr"
					}
					"activator"
					{
						"type"    "cbaseentity"
					}
					"caller"
					{
						"type"    "cbaseentity"
					}
					"variant_t"
					{
						"type"    "objectptr"
						"size"	  "20"
					}
					"output"
					{
						"type"    "int"
					}
				}
			}
		}
		
		"Signatures"
		{
			"AcceptInput"
			{
				"linux"		"@_ZN11CBaseEntity11AcceptInputEPKcPS_S2_9variant_ti"
			}
		}
	}
}
PHP Code:
#include <sdktools>
#include <dhooks>
#include <sdktools>

#define MAX_ENTS            4096
#define LEN_CLASS            64

Handle hAcceptInput;

enum fieldtype_t
{
    
FIELD_VOID 0,
    
FIELD_FLOAT 1,
    
FIELD_STRING 2,
    
FIELD_VECTOR 3,
    
FIELD_INTEGER 5,
    
FIELD_BOOLEAN 6,
    
FIELD_SHORT 7,
    
FIELD_CHARACTER 8,
    
FIELD_COLOR32 9,
    
FIELD_CLASSPTR 12,
    
FIELD_EHANDLE 13,
    
FIELD_POSITION_VECTOR 15
}

enum struct variant_t
{
    
bool bValue;
    
int iValue;
    
float flValue;
    
char iszValue[256];
    
int rgbaValue[4];
    
float vecValue[3];
    
fieldtype_t fieldType;
}

public 
void OnPluginStart()
{
    
Handle hGameData LoadGameConfigFile("l4d2_accept_input");
    if(!
hGameData)
        
SetFailState("Couldn't find l4d2_accept_input gamedata.");
    
    
int offset GameConfGetOffset(hGameData"AcceptInput");
    
Handle hAcceptInput DHookCreateDetour(0HookType_EntityReturnType_BoolThisPointer_CBaseEntity);
    
    if (!
DHookSetFromConf(hAcceptInputhGameDataSDKConf_Signature"AcceptInput"))
        
SetFailState("Failed to load AcceptInput signature from gamedata");

    if (!
DHookEnableDetour(hAcceptInputfalseDetour_AcceptInput))
        
SetFailState("Failed to detour ExecuteStringCommand.");

}

public 
MRESReturn Detour_AcceptInput(int pThisHandle hReturnHandle hParams)
{
    
// Get args
    
static char result[128];
    static 
char command[128];
    
DHookGetParamString(hParams1commandsizeof(command));

    
variant_t params;
    
params.fieldType view_as<fieldtype_t>(DHookGetParamObjectPtrVar(hParams416ObjectValueType_Int));

    switch (
params.fieldType)
    {
        case 
FIELD_FLOAT:
        {
            
params.flValue DHookGetParamObjectPtrVar(hParams40ObjectValueType_Float);
            
FloatToString(params.flValueresultsizeof(result));
        }

        case 
FIELD_STRING:
        {
            
DHookGetParamObjectPtrString(hParams40ObjectValueType_Stringresultsizeof(result));
        }

        case 
FIELD_VECTORFIELD_POSITION_VECTOR:
        {
            
DHookGetParamObjectPtrVarVector(hParams40ObjectValueType_Vectorparams.vecValue);
            
Format(resultsizeof(result), "%f, %f, %f"params.vecValue[0], params.vecValue[1], params.vecValue[2]);
        }

        case 
FIELD_INTEGERFIELD_SHORTFIELD_CHARACTER:
        {
            
params.iValue DHookGetParamObjectPtrVar(hParams40ObjectValueType_Int);
            
IntToString(params.iValueresultsizeof(result));
        }

        case 
FIELD_BOOLEAN:
        {
            
params.bValue DHookGetParamObjectPtrVar(hParams40ObjectValueType_Bool);
            
IntToString(params.bValueresultsizeof(result));
        }

        case 
FIELD_COLOR32:
        {
            
int color DHookGetParamObjectPtrVar(hParams40ObjectValueType_Int);
            
params.rgbaValue[0] = color 0xFF;
            
params.rgbaValue[1] = (color >> 8) & 0xFF;
            
params.rgbaValue[2] = (color >> 16) & 0xFF;
            
params.rgbaValue[3] = (color >> 24) & 0xFF;

            
Format(resultsizeof(result), "%d %d %d %d"params.rgbaValue[0], params.rgbaValue[1], params.rgbaValue[2], params.rgbaValue[3]);
        }

        case 
FIELD_CLASSPTRFIELD_EHANDLE:
        {
            
params.iValue DHookGetParamObjectPtrVar(hParams40ObjectValueType_Ehandle);
            
IntToString(params.iValueresultsizeof(result));
        }

        default:
        {
            
Format(resultsizeof(result), "Unknown type: %d"params.fieldType);
        }
    }

    static 
char classname[LEN_CLASS];
    
GetEntPropString(pThisProp_Data"m_iClassname"classnamesizeof(classname));

    if( 
pThis )
        
pThis EntRefToEntIndex(pThis);

    
int entity = -1;
    if( 
DHookIsNullParam(hParams2) == false )
        
entity DHookGetParam(hParams2);

    
// Activator + classname
    
static char activator[LEN_CLASS];
    static 
char sName[128];

    
activator[0] = 0;
    
sName[0] = 0;

    if( 
entity != -)
    {
        if( 
entity && entity <= MaxClients )
            
Format(activatorsizeof(activator), "%N"entity);
        else
        {
            
GetEntPropString(entityProp_Data"m_iClassname"activatorsizeof(activator));
            if( 
entity )
                
entity EntRefToEntIndex(entity);
        }
    }

    if( 
HasEntProp(pThisProp_Data"m_iName") )
    {
        
GetEntPropString(pThisProp_Data"m_iName"sNamesizeof(sName));
    }
    
    if(
StrEqual(classname"func_elevator") && StrEqual(command"MoveToFloor"))
    {
        
// Print
        
PrintToChatAll("\x01Ent %4d \x04%36s \x01Cmd \x05%20s \x01Name \x03%45s \x01Param \x03%12s \x01Act \x01%4d \x04%s"pThisclassnamecommandsNameresultentityactivator);
        
PrintToServer("\x01Ent %4d \x04%36s \x01Cmd \x05%20s \x01Name \x03%45s \x01Param \x03%12s \x01Act \x01%4d \x04%s"pThisclassnamecommandsNameresultentityactivator);
    }

    return 
MRES_Ignored;

What am i missing?


Background: i used HookEntitySingleOutput in one of the plugins, but up to 50 frames can pass between Output and Input, and I had to track the state of several entities for those 50-100 frames. Using a detour for AcceptInput, it would be possible to reduce the complexity/code length by half.

Last edited by glhf3000; 02-12-2024 at 11:48.
glhf3000 is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:46.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode