Hook / Unhook consistently

Add our hooks when a SourceTV bot is added as well as on late loading
the extension.
Make sure to remove all our hooks on unload.
This commit is contained in:
Peace-Maker 2016-02-29 17:44:27 +01:00
parent 3210a8d249
commit 6e0ec48a2a
3 changed files with 53 additions and 38 deletions

View File

@ -119,13 +119,13 @@ void SourceTVManager::SDK_OnAllLoaded()
// Hook all the exisiting servers. // Hook all the exisiting servers.
for (int i = 0; i < hltvdirector->GetHLTVServerCount(); i++) for (int i = 0; i < hltvdirector->GetHLTVServerCount(); i++)
{ {
g_pSTVForwards.HookRecorder(GetDemoRecorderPtr(hltvdirector->GetHLTVServer(i))); HookSourceTVServer(hltvdirector->GetHLTVServer(i));
} }
#else #else
if (hltvdirector->GetHLTVServer()) if (hltvdirector->GetHLTVServer())
{ {
SelectSourceTVServer(hltvdirector->GetHLTVServer()); SelectSourceTVServer(hltvdirector->GetHLTVServer());
g_pSTVForwards.HookRecorder(GetDemoRecorderPtr(hltvdirector->GetHLTVServer())); HookSourceTVServer(hltvdirector->GetHLTVServer());
} }
#endif #endif
} }
@ -153,12 +153,12 @@ void SourceTVManager::SDK_OnUnload()
// Unhook all the existing servers. // Unhook all the existing servers.
for (int i = 0; i < hltvdirector->GetHLTVServerCount(); i++) for (int i = 0; i < hltvdirector->GetHLTVServerCount(); i++)
{ {
g_pSTVForwards.UnhookRecorder(GetDemoRecorderPtr(hltvdirector->GetHLTVServer(i))); UnhookSourceTVServer(hltvdirector->GetHLTVServer(i));
} }
#else #else
// Unhook the server // Unhook the server
if (hltvdirector->GetHLTVServer()) if (hltvdirector->GetHLTVServer())
g_pSTVForwards.UnhookRecorder(GetDemoRecorderPtr(hltvdirector->GetHLTVServer())); UnhookSourceTVServer(hltvdirector->GetHLTVServer());
#endif #endif
g_pSTVForwards.Shutdown(); g_pSTVForwards.Shutdown();
} }
@ -171,35 +171,47 @@ bool SourceTVManager::QueryRunning(char *error, size_t maxlength)
return true; return true;
} }
void SourceTVManager::SelectSourceTVServer(IHLTVServer *hltv) void SourceTVManager::HookSourceTVServer(IHLTVServer *hltv)
{ {
// Need to unhook the old server first? if (hltv != nullptr)
if (hltvserver != nullptr && iserver != nullptr)
{ {
IClient *pClient = iserver->GetClient(hltvserver->GetHLTVSlot()); g_pSTVForwards.HookRecorder(GetDemoRecorderPtr(hltv));
if (pClient)
if (iserver)
{ {
SH_REMOVE_HOOK(IClient, ExecuteStringCommand, pClient, SH_MEMBER(this, &SourceTVManager::OnHLTVBotExecuteStringCommand), false); IClient *pClient = iserver->GetClient(hltv->GetHLTVSlot());
SH_REMOVE_HOOK(IClient, ExecuteStringCommand, pClient, SH_MEMBER(this, &SourceTVManager::OnHLTVBotExecuteStringCommand_Post), true); if (pClient)
{
SH_ADD_HOOK(IClient, ExecuteStringCommand, pClient, SH_MEMBER(this, &SourceTVManager::OnHLTVBotExecuteStringCommand), false);
SH_ADD_HOOK(IClient, ExecuteStringCommand, pClient, SH_MEMBER(this, &SourceTVManager::OnHLTVBotExecuteStringCommand_Post), true);
}
} }
} }
}
void SourceTVManager::UnhookSourceTVServer(IHLTVServer *hltv)
{
if (hltv != nullptr)
{
g_pSTVForwards.UnhookRecorder(GetDemoRecorderPtr(hltv));
if (iserver)
{
IClient *pClient = iserver->GetClient(hltv->GetHLTVSlot());
if (pClient)
{
SH_REMOVE_HOOK(IClient, ExecuteStringCommand, pClient, SH_MEMBER(this, &SourceTVManager::OnHLTVBotExecuteStringCommand), false);
SH_REMOVE_HOOK(IClient, ExecuteStringCommand, pClient, SH_MEMBER(this, &SourceTVManager::OnHLTVBotExecuteStringCommand_Post), true);
}
}
}
}
void SourceTVManager::SelectSourceTVServer(IHLTVServer *hltv)
{
// Select the new server. // Select the new server.
hltvserver = hltv; hltvserver = hltv;
demorecorder = GetDemoRecorderPtr(hltvserver);
UpdateDemoRecorderPointer();
if (!hltvserver)
return;
if (!iserver)
return;
IClient *pClient = iserver->GetClient(hltvserver->GetHLTVSlot());
if (!pClient)
return;
SH_ADD_HOOK(IClient, ExecuteStringCommand, pClient, SH_MEMBER(this, &SourceTVManager::OnHLTVBotExecuteStringCommand), false);
SH_ADD_HOOK(IClient, ExecuteStringCommand, pClient, SH_MEMBER(this, &SourceTVManager::OnHLTVBotExecuteStringCommand_Post), true);
} }
IDemoRecorder *SourceTVManager::GetDemoRecorderPtr(IHLTVServer *hltv) IDemoRecorder *SourceTVManager::GetDemoRecorderPtr(IHLTVServer *hltv)
@ -217,15 +229,10 @@ IDemoRecorder *SourceTVManager::GetDemoRecorderPtr(IHLTVServer *hltv)
return nullptr; return nullptr;
} }
void SourceTVManager::UpdateDemoRecorderPointer()
{
demorecorder = GetDemoRecorderPtr(hltvserver);
}
#if SOURCE_ENGINE == SE_CSGO #if SOURCE_ENGINE == SE_CSGO
void SourceTVManager::OnAddHLTVServer_Post(IHLTVServer *hltv) void SourceTVManager::OnAddHLTVServer_Post(IHLTVServer *hltv)
{ {
g_pSTVForwards.HookRecorder(GetDemoRecorderPtr(hltv)); HookSourceTVServer(hltv);
// We already selected some SourceTV server. Keep it. // We already selected some SourceTV server. Keep it.
if (hltvserver != nullptr) if (hltvserver != nullptr)
@ -238,23 +245,28 @@ void SourceTVManager::OnAddHLTVServer_Post(IHLTVServer *hltv)
void SourceTVManager::OnRemoveHLTVServer_Post(IHLTVServer *hltv) void SourceTVManager::OnRemoveHLTVServer_Post(IHLTVServer *hltv)
{ {
g_pSTVForwards.UnhookRecorder(GetDemoRecorderPtr(hltv)); UnhookSourceTVServer(hltv);
// We got this SourceTV server selected. Now it's gone :( // We got this SourceTV server selected. Now it's gone :(
if (hltvserver == hltv) if (hltvserver == hltv)
{ {
// Is there another one available? Try to keep us operable. // Is there another one available? Try to keep us operable.
if (hltvdirector->GetHLTVServerCount() > 0) if (hltvdirector->GetHLTVServerCount() > 0)
{
SelectSourceTVServer(hltvdirector->GetHLTVServer(0)); SelectSourceTVServer(hltvdirector->GetHLTVServer(0));
HookSourceTVServer(hltvserver);
}
// No sourcetv active.
else else
{
SelectSourceTVServer(nullptr); SelectSourceTVServer(nullptr);
}
} }
RETURN_META(MRES_IGNORED); RETURN_META(MRES_IGNORED);
} }
#else #else
void SourceTVManager::OnSetHLTVServer_Post(IHLTVServer *hltv) void SourceTVManager::OnSetHLTVServer_Post(IHLTVServer *hltv)
{ {
META_CONPRINTF("%x == %x ?", hltv, hltvdirector->GetHLTVServer());
// Server shut down? // Server shut down?
if (!hltv) if (!hltv)
{ {
@ -262,11 +274,11 @@ void SourceTVManager::OnSetHLTVServer_Post(IHLTVServer *hltv)
if (!hltvserver) if (!hltvserver)
RETURN_META(MRES_IGNORED); RETURN_META(MRES_IGNORED);
g_pSTVForwards.UnhookRecorder(GetDemoRecorderPtr(hltvserver)); UnhookSourceTVServer(hltvserver);
} }
else else
{ {
g_pSTVForwards.HookRecorder(GetDemoRecorderPtr(hltv)); HookSourceTVServer(hltv);
} }
SelectSourceTVServer(hltv); SelectSourceTVServer(hltv);
RETURN_META(MRES_IGNORED); RETURN_META(MRES_IGNORED);

View File

@ -137,7 +137,8 @@ private:
bool OnHLTVBotExecuteStringCommand_Post(const char *s); bool OnHLTVBotExecuteStringCommand_Post(const char *s);
private: private:
void UpdateDemoRecorderPointer(); void HookSourceTVServer(IHLTVServer *hltv);
void UnhookSourceTVServer(IHLTVServer *hltv);
}; };
/* Interfaces from SourceMod */ /* Interfaces from SourceMod */

View File

@ -63,14 +63,16 @@ public __ext_stvmngr_SetNTVOptional()
MarkNativeAsOptional("SourceTV_GetLocalStats"); MarkNativeAsOptional("SourceTV_GetLocalStats");
MarkNativeAsOptional("SourceTV_GetBroadcastTick"); MarkNativeAsOptional("SourceTV_GetBroadcastTick");
MarkNativeAsOptional("SourceTV_GetDelay"); MarkNativeAsOptional("SourceTV_GetDelay");
MarkNativeAsOptional("SourceTV_SendHintMessage"); MarkNativeAsOptional("SourceTV_BroadcastHintMessage");
MarkNativeAsOptional("SourceTV_BroadcastConsoleMessage"); MarkNativeAsOptional("SourceTV_BroadcastConsoleMessage");
MarkNativeAsOptional("SourceTV_GetViewEntity");
MarkNativeAsOptional("SourceTV_GetViewCoordinates");
MarkNativeAsOptional("SourceTV_StartRecording"); MarkNativeAsOptional("SourceTV_StartRecording");
MarkNativeAsOptional("SourceTV_StopRecording"); MarkNativeAsOptional("SourceTV_StopRecording");
MarkNativeAsOptional("SourceTV_IsRecording"); MarkNativeAsOptional("SourceTV_IsRecording");
MarkNativeAsOptional("SourceTV_GetDemoFileName"); MarkNativeAsOptional("SourceTV_GetDemoFileName");
MarkNativeAsOptional("SourceTV_GetRecordingTick"); MarkNativeAsOptional("SourceTV_GetRecordingTick");
MarkNativeAsOptional("SourceTV_PrintBotConsole"); MarkNativeAsOptional("SourceTV_PrintToDemoConsole");
MarkNativeAsOptional("SourceTV_GetSpectatorCount"); MarkNativeAsOptional("SourceTV_GetSpectatorCount");
MarkNativeAsOptional("SourceTV_GetMaxClients"); MarkNativeAsOptional("SourceTV_GetMaxClients");
MarkNativeAsOptional("SourceTV_GetClientCount"); MarkNativeAsOptional("SourceTV_GetClientCount");