AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Using SourceHook (https://forums.alliedmods.net/showthread.php?t=78669)

BeetleFart 10-08-2008 08:32

Using SourceHook
 
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?

pRED* 10-08-2008 16:05

Re: Using SourceHook
 
That line looks fine..

Probably need to see the full code to try spot the problem.

BeetleFart 10-08-2008 16:16

Re: Using SourceHook
 
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},
}; 


pRED* 10-08-2008 16:42

Re: Using SourceHook
 
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.

BeetleFart 10-08-2008 17:40

Re: Using SourceHook
 
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

pRED* 10-08-2008 19:14

Re: Using SourceHook
 
Thought that sounded too easy.

One of the pointers you are passing maybe?
g_pVoiceServer?

BeetleFart 10-08-2008 22:23

Re: Using SourceHook
 
i verified its not null ...
and its appears good in :Load

BeetleFart 10-09-2008 13:38

Re: Using SourceHook
 
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?

BeetleFart 10-09-2008 15:41

Re: Using SourceHook
 
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.

pRED* 10-09-2008 15:59

Re: Using SourceHook
 
Is this a metamod plugin or a VSP?

The embedding section on that wiki page suggests that multiple versions of sourcehook in the same process will probably conflict, so not entirely sure how you'd add it to a VSP if metamod is running.

If it's metamod you need something like this in your metamod load function:

g_SHPtr = static_cast<SourceHook::ISourceHook *>(ismm->MetaFactory(MMIFACE_SOURCEHOOK, NULL, NULL));
g_PLID = id;

or look at the PLUGIN_SAVEVARS() macro in ISmmPlugin.h


All times are GMT -4. The time now is 21:03.

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