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

Using SourceHook


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BeetleFart
SourceMod Donor
Join Date: Apr 2004
Old 10-08-2008 , 08:32   Using SourceHook
Reply With Quote #1

Ive seen some sourcecode I believe it was for sdktools...used for hooking into IVoiceServer.

Ive tried taking the source and hooking into the IVoiceServer myself In my plugin...but I cant get it to work.
As soon as I try to do the hook..it crashs
I call
SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, g_pVoiceServer, &g_EmtpyServerPlugin,&CEmptyServerPlugin::OnS etClientListening, false);
but am having trouble.
Can anyone help me out to be able to hook into the setclientlistening functions so I can set up a proper mute function?
__________________
CSource Server:
69.90.34.10:27015 Beetlesmod.com And CS-Addicts{US}



http://www.clanuseast.com
BeetleFart is offline
pRED*
Join Date: Dec 2006
Old 10-08-2008 , 16:05   Re: Using SourceHook
Reply With Quote #2

That line looks fine..

Probably need to see the full code to try spot the problem.
pRED* is offline
BeetleFart
SourceMod Donor
Join Date: Apr 2004
Old 10-08-2008 , 16:16   Re: Using SourceHook
Reply With Quote #3

I scrapped out the code from sdktools source that I have from somewhere.

I changed it a bit because I dont have a IPluginContext pointer (passing null to test but each use of it is checked).

PHP Code:
cell_t t[4];
t[0]=4;

I call  SetClientListeningFlags(NULL,t); to set it up passing in my index (4)









size_t g_VoiceFlags[65];
size_t g_VoiceFlagsCount 0;


SH_DECL_HOOK3(IVoiceServerSetClientListeningSH_NOATTRIB0boolintintbool);



static 
cell_t SetClientListeningFlags(IPluginContext pContext, const cell_t *params)
{
    
//IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
    
int index=params[1];

    
edict_t player=g_EmtpyServerPlugin.GetPlayer(index);
    if (!
player)
    {
        return 
1;
        
//return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
    
}
     else if (
player->IsFree()) {
        return 
1;
        
//return pContext->ThrowNativeError("Client %d is not connected", params[1]);
    
}

    if (!
params[2] && g_VoiceFlags[params[1]])
    {
        if (!--
g_VoiceFlagsCount)
        {
            
SH_REMOVE_HOOK_MEMFUNC(IVoiceServerSetClientListeningg_pVoiceServer,&g_EmtpyServerPlugin, &CEmptyServerPlugin::OnSetClientListeningfalse);
        }
    } else if (!
g_VoiceFlags[params[1]] && params[2]) {

        if (!
g_VoiceFlagsCount++)
        {
            
//SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, g_pVoiceServer, &g_EmtpyServerPlugin,&CEmptyServerPlugin::OnSetClientListening, false);
            
SH_ADD_HOOK_MEMFUNC(IVoiceServerSetClientListeningg_pVoiceServer,&g_EmtpyServerPlugin,&CEmptyServerPlugin::OnSetClientListeningfalse);
        }
    }

    
g_VoiceFlags[params[1]] = params[2];

    return 
1;
}

static 
cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
{
//    IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
    
edict_t player=g_EmtpyServerPlugin.GetPlayer(params[1]);
    if (
player == NULL)
    {   if(
pContext)
        return 
pContext->ThrowNativeError("Client index %d is invalid"params[1]);

    }
    else 
    if (
player->IsFree()) 
    { if(
pContext)
        return 
pContext->ThrowNativeError("Client %d is not connected"params[1]);
    }

    return 
g_VoiceFlags[params[1]];
}

static 
cell_t SetClientListening(IPluginContext *pContext, const cell_t *params)
{
    
//IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
    
edict_t player=g_EmtpyServerPlugin.GetPlayer(params[1]);
    if (
player == NULL)
    { if(
pContext)
        return 
pContext->ThrowNativeError("Client index %d is invalid"params[1]);
    } else if (
player->IsFree())
    { if(
pContext)
        return 
pContext->ThrowNativeError("Client %d is not connected"params[1]);
    }

    
bool bListen = !params[3] ? false true;

    return 
g_pVoiceServer->SetClientListening(params[1], params[2], bListen) ? 0;
}

static 
cell_t GetClientListening(IPluginContext *pContext, const cell_t *params)
{
    
//IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
    
edict_t player=g_EmtpyServerPlugin.GetPlayer(params[1]);
    if (
player == NULL)
    { if(
pContext)
        return 
pContext->ThrowNativeError("Client index %d is invalid"params[1]);
    } else if (
player->IsFree()) {
         if(
pContext)
        return 
pContext->ThrowNativeError("Client %d is not connected"params[1]);
    }

    return 
g_pVoiceServer->GetClientListening(params[1], params[2]) ? 0;
}



bool CEmptyServerPlugin::OnSetClientListening(int iReceiverint iSenderbool bListen)
{
    if (
g_VoiceFlags[iSender] & SPEAK_MUTED)
    {
        
RETURN_META_VALUE_NEWPARAMS(MRES_IGNOREDbListen, &IVoiceServer::SetClientListening, (iReceiveriSenderfalse));
    }

    if ((
g_VoiceFlags[iSender] & SPEAK_ALL) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENALL))
    {
        
RETURN_META_VALUE_NEWPARAMS(MRES_IGNOREDbListen, &IVoiceServer::SetClientListening, (iReceiveriSendertrue));
    }

    if ((
g_VoiceFlags[iSender] & SPEAK_TEAM) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENTEAM))
    {
    
        
edict_t pReceiver=GetPlayer(iReceiver);
        
edict_t pSender=GetPlayer(iSender);
        
//IGamePlayer *pReceiver = playerhelpers->GetGamePlayer(iReceiver);
        //IGamePlayer *pSender = playerhelpers->GetGamePlayer(iSender);

        //if (pReceiver && pSender && pReceiver->IsInGame() && pSender->IsInGame())
        
if (pReceiver && pSender)
        {
            
IPlayerInfo *pRInfo playerinfomanager->GetPlayerInfo(pReceiver);//pReceiver->GetPlayerInfo();
            
IPlayerInfo *pSInfo =  playerinfomanager->GetPlayerInfo(pSender);//pSender->GetPlayerInfo();

            //if (pRInfo && pSInfo && pRInfo->GetTeamIndex() == pSInfo->GetTeamIndex())
            
if (pRInfo && pSInfo && pRInfo->GetTeamIndex() == pSInfo->GetTeamIndex())
            {
                
RETURN_META_VALUE_NEWPARAMS(MRES_IGNOREDbListen, &IVoiceServer::SetClientListening, (iReceiveriSendertrue));
            }
        }
    }

    
RETURN_META_VALUE(MRES_IGNOREDbListen);
}







void CEmptyServerPlugin::OnClientDisconnecting(int client)
{
    if (
g_VoiceFlags[client])
    {
        
g_VoiceFlags[client] = 0;
        if (!--
g_VoiceFlagsCount)
        {
            
//SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, g_pVoiceServer , &g_SdkTools, &SDKTools::OnSetClientListening, false);
            
SH_REMOVE_HOOK_MEMFUNC(IVoiceServerSetClientListeningg_pVoiceServer , &g_EmtpyServerPlugin, &CEmptyServerPlugin::OnSetClientListeningfalse);
          
        }
    }
}



sp_nativeinfo_t g_VoiceNatives[] =
{
    {
"SetClientListeningFlags",        SetClientListeningFlags},
    {
"GetClientListeningFlags",        GetClientListeningFlags},
    {
"SetClientListening",            SetClientListening},
    {
"GetClientListening",            GetClientListening},
    {
NULL,                            NULL},
}; 
__________________
CSource Server:
69.90.34.10:27015 Beetlesmod.com And CS-Addicts{US}



http://www.clanuseast.com

Last edited by pRED*; 10-08-2008 at 16:39.
BeetleFart is offline
pRED*
Join Date: Dec 2006
Old 10-08-2008 , 16:42   Re: Using SourceHook
Reply With Quote #4

Params arrays have the first index (0) as the number of params.

Notice you set
t[0] = 4;
then
int index=params[1];

t[1] is uninitialised..

You don't need the sp_nativeinfo_t since thats just to register the commands as natives.
pRED* is offline
BeetleFart
SourceMod Donor
Join Date: Apr 2004
Old 10-08-2008 , 17:40   Re: Using SourceHook
Reply With Quote #5

i actually have it
t[0]=4;
t[1]=4;
I call the function and step thru it until it does the sh_add_hook_memfunc
and it then crashs at the
SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool);
line of code
__________________
CSource Server:
69.90.34.10:27015 Beetlesmod.com And CS-Addicts{US}



http://www.clanuseast.com
BeetleFart is offline
pRED*
Join Date: Dec 2006
Old 10-08-2008 , 19:14   Re: Using SourceHook
Reply With Quote #6

Thought that sounded too easy.

One of the pointers you are passing maybe?
g_pVoiceServer?
pRED* is offline
BeetleFart
SourceMod Donor
Join Date: Apr 2004
Old 10-08-2008 , 22:23   Re: Using SourceHook
Reply With Quote #7

i verified its not null ...
and its appears good in :Load
__________________
CSource Server:
69.90.34.10:27015 Beetlesmod.com And CS-Addicts{US}



http://www.clanuseast.com
BeetleFart is offline
BeetleFart
SourceMod Donor
Join Date: Apr 2004
Old 10-09-2008 , 13:38   Re: Using SourceHook
Reply With Quote #8

When I Call the SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, g_pVoiceServer,&g_EmtpyServerPlugin,&CEmptySe rverPlugin::OnSetClientListening, false);

g_pVoiceServer is valid, g_EmptyServerPlugin is My main plugin class
and is valid as well..
I still dont see why it crashs...
with a breakpoint on SH_DECL_HOOK3
it appears to be hooking in ...i see it going thru the macro's etc for fastdelegate etc..
but 3rd time it goes thru sh_decl_hook3 it crashs

does the class that I pass in for handler_inst have to be derived from a specific class or something?
__________________
CSource Server:
69.90.34.10:27015 Beetlesmod.com And CS-Addicts{US}



http://www.clanuseast.com

Last edited by BeetleFart; 10-09-2008 at 13:42.
BeetleFart is offline
BeetleFart
SourceMod Donor
Join Date: Apr 2004
Old 10-09-2008 , 15:41   Re: Using SourceHook
Reply With Quote #9

The problem is with my not initializing source hook variable
SourceHook::ISourceHook *g_SHPtr

How do I initialize sourcehook correctly.
Im trying to follow the wiki at
http://wiki.alliedmods.net/SourceHoo...t#Simple_Hooks
but Im not having much luck.
__________________
CSource Server:
69.90.34.10:27015 Beetlesmod.com And CS-Addicts{US}



http://www.clanuseast.com
BeetleFart is offline
BAILOPAN
Join Date: Jan 2004
Old 10-09-2008 , 16:53   Re: Using SourceHook
Reply With Quote #10

You should really consider using MM:S, conflicting copies of SourceHook in the same address space is really problematic. ESTools does this and it causes conflicts with SourceMod.

One thing to do is what Mani does. VSP version with SourceHook embedded, MM:S version without. This works pretty nicely as the API stays the same.

The whole point of SourceHook is to unify hooking (making it easier through macros is just a side effect). If there's multiple SourceHooks, the original purpose is lost
__________________
egg
BAILOPAN is offline
Reply



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 22:32.


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