Raised This Month: $12 Target: $400
 3% 

Hooking BaseEntity Functions in CSGO


Post New Thread Reply   
 
Thread Tools Display Modes
spidershift
Member
Join Date: Oct 2014
Old 10-31-2014 , 11:18   Re: Hooking BaseEntity Functions in CSGO
Reply With Quote #21

So I tried to see if I could get any more information for you from my tests but that's about all I got. I basically took the FX_FireBullets() detour code out of my extension, and put it inside the SM sample extension exactly as you did, and that's what I got. Since this is my first SourceMod extension though, I wouldn't be surprised if I messed something up somewhere, so I wanted to provide as many details as I could. Please let me know if you need any more information from me or if there is something else I should be testing. Thanks.
spidershift is offline
donrevan
AlliedModders Donor
Join Date: Jul 2010
Old 10-31-2014 , 11:22   Re: Hooking BaseEntity Functions in CSGO
Reply With Quote #22

OnPluginLoaded/Unloaded is fired for each plugin that is loaded by SM.
You remove the detour as soon as any plugin is unloaded. Move CreateFXFireBulletsDetour to SDK_OnLoad, destroy the detour on SDK_OnUnload and see if that helps.

see my extension.cpp
PHP Code:
#include "extension.h"

GOTest g_Ext;
SMEXT_LINK(&g_Ext);
CDetour *g_pDetour NULL;
IGameConfig *g_pGameConf;
#define MODE_PRIMARY 0
#define MODE_SECONDARY 1

DETOUR_DECL_STATIC10(Detour_FXFireBulletsvoidVector const&, vOriginQAngle const&, vAnglesintweaponIdintmodeintseedfloatflSpreadfloata9floata10floata11inta12)
{
    
int iPlayerId;
    
__asm mov iPlayerIdecx
    int definitionIndex
;
    
__asm mov definitionIndexedx

    
// Trick the compiler into thinking that we're using ESI(to preserve it - just to be sure).
    
__asm mov esiesi;

    
printf("playerID: %d\n"iPlayerId);
    
printf("definitionIndex: %d\n"definitionIndex);
    
printf("wpnId: %d\n"weaponId);
    
printf("mode: %d\n"mode);
    
printf("seed: %d\n"seed);
    
printf("spread: %f\n"flSpread);

    
/* Call original function.
     * I had to manually 'construct' this call because the compiler was destroying register content while pushing the args. */
    
void *addr DETOUR_STATIC_CALL(Detour_FXFireBullets);
    
__asm {
        
push a12
        push a11
        push a10
        push a9
        push flSpread
        push seed
        push mode
        push weaponId
        push vAngles
        push vOrigin
        mov edx
definitionIndex
        mov ecx
iPlayerId
        call addr
        add esp
0x28
    
}
}  

bool GOTest::SDK_OnLoad(char *errorsize_t maxlengthbool late)
{
    if(!
gameconfs->LoadGameConfigFile("gotest", &g_pGameConferrormaxlength))
    {
        
g_pSM->Format(errormaxlength"Couldn't load gamedata");
        return 
false;
    }

    
CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
    
g_pDetour DETOUR_CREATE_STATIC(Detour_FXFireBullets"FX_FireBullets");
    if(!
g_pDetour)
    {
        
g_pSM->Format(errormaxlength"Couldn't create detour");
        return 
false;
    }

    
//printf("Detouring: %x\n", g_pDetour->Address());
    
g_pDetour->EnableDetour();
    return 
true;
}

void GOTest::SDK_OnUnload()
{
    if(
g_pDetour)
    {
        
g_pDetour->Destroy();
    }


Last edited by donrevan; 10-31-2014 at 11:23.
donrevan is offline
spidershift
Member
Join Date: Oct 2014
Old 10-31-2014 , 11:28   Re: Hooking BaseEntity Functions in CSGO
Reply With Quote #23

Agh, I can't believe that was it. I actually had it in SDK_OnLoad() and SDK_OnUnload() when I was putting it in the sample extension, but since the flashtools plugin I was provided earlier was my starting point for understanding detours, I saw they had it in there and thought that's what I should be doing. I will fix that right now and test this out. Thank you!
spidershift is offline
spidershift
Member
Join Date: Oct 2014
Old 10-31-2014 , 11:46   Re: Hooking BaseEntity Functions in CSGO
Reply With Quote #24

Still getting the same error.

Also, if I uncomment your line:

g_pDetour->Address()

It tells me that "CDetour has no member Address", so I'm guessing I'm doing something wrong here. Do you think my includes are screwed up?

PHP Code:
#include "extension.h"

/**
 * @file extension.cpp
 * @brief Implement extension code here.
 */

Sample g_Sample;        /**< Global singleton for extension's main interface */

SMEXT_LINK(&g_Sample);

IGameConfig *g_pGameConf NULL;

CDetour  *g_pDetour     NULL;
IForward *g_pFireBullet NULL;

DETOUR_DECL_STATIC10(Detour_FXFireBulletsvoidVector const&, vOriginQAngle const&, vAnglesintweaponIdintmodeintseedfloatflSpreadfloata9floata10floata11inta12)
{
    
int iPlayerId;
    
__asm mov iPlayerIdecx
    int definitionIndex
;
    
__asm mov definitionIndexedx

    
// Trick the compiler into thinking that we're using ESI(to preserve it - just to be sure).
    
__asm mov esiesi;

    
/*cell_t result = Pl_Continue;
    g_pFireBullet->PushCell(iPlayerId);
    g_pFireBullet->Execute(&result);

    printf("playerID: %d\n", iPlayerId);
    printf("definitionIndex: %d\n", definitionIndex);
    printf("wpnId: %d\n", weaponId);
    printf("mode: %d\n", mode);
    printf("seed: %d\n", seed);
    printf("spread: %f\n", flSpread);*/

    /*Vector *ptr3;
    
    ptr3  = (Vector*)( &vOrigin );
    *ptr3 =  Vector(vOrigin.x, vOrigin.y, vOrigin.z);
    
    QAngle *ptr4;
    
    ptr4  = (QAngle*)( &vAngles );
    *ptr4 =  QAngle(vAngles.x, vAngles.y, vAngles.z);*/

    /* Call original function.
     * I had to manually 'construct' this call because the compiler was destroying register content while pushing the args. */
    
void *addr DETOUR_STATIC_CALL(Detour_FXFireBullets);
    
__asm {
        
push a12
        push a11
        push a10
        push a9
        push flSpread
        push seed
        push mode
        push weaponId
        push vAngles
        push vOrigin
        mov edx
definitionIndex
        mov ecx
iPlayerId
        call addr
        add esp
0x28
    
}
}

bool Sample::SDK_OnLoad(char *errorsize_t maxlengthbool late)
{
    
char conf_error[255];
    if (!
gameconfs->LoadGameConfigFile("sample.games", &g_pGameConfconf_errorsizeof(conf_error)))
    {
        if (
error)
        {
            
snprintf(errormaxlength"Could not read sample.games.txt: %s"conf_error);
        }
        return 
false;
    }

    
sharesys->RegisterLibrary(myself"sample");

    
//plsys->AddPluginsListener(this);

    
CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);

    
g_pDetour DETOUR_CREATE_STATIC(Detour_FXFireBullets"FX_FireBullets");
    if(!
g_pDetour)
    {
        
g_pSM->Format(errormaxlength"Couldn't create detour");
        return 
false;
    }

    
//printf("Detouring: %x\n", g_pDetour->Address());
    
g_pDetour->EnableDetour();
    
    
g_pFireBullet forwards->CreateForward("OnFireBullet"ET_Event1NULLParam_Cell);

    return 
true;
}
void Sample::SDK_OnUnload()
{
    if(
g_pDetour)
    {
        
g_pDetour->Destroy();
    }

    
gameconfs->CloseGameConfigFile(g_pGameConf);
    
//plsys->RemovePluginsListener(this);
    
forwards->ReleaseForward(g_pFireBullet);

spidershift is offline
spidershift
Member
Join Date: Oct 2014
Old 10-31-2014 , 11:49   Re: Hooking BaseEntity Functions in CSGO
Reply With Quote #25

I'm using MM 1.10.3, SM 1.6.2, and I'm not sure what version my hl2sdk-csgo is...
spidershift is offline
API
Veteran Member
Join Date: May 2006
Old 01-07-2015 , 12:25   Re: Hooking BaseEntity Functions in CSGO
Reply With Quote #26

I am here late but I see that you ran into LTCG optimizing to a __fastcall. When this happens, you actually don't have to use inline assembly, but you will indeed have to trick CDetour (not really trick it, but the macros don't allow you to define a __fastcall.
__________________
API is offline
Send a message via AIM to API
Reply


Thread Tools
Display Modes

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 01:14.


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