Skip to content

Commit 82e3f7b

Browse files
committed
Fix issues
1 parent f46dc01 commit 82e3f7b

File tree

3 files changed

+51
-72
lines changed

3 files changed

+51
-72
lines changed

src/detours.cpp

-9
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252

5353
#include "tier0/memdbgon.h"
5454

55-
5655
extern CGlobalVars *gpGlobals;
5756
extern CGameEntitySystem *g_pEntitySystem;
5857
extern IGameEventManager2 *g_gameEventManager;
@@ -685,14 +684,6 @@ bool InitDetours(CGameConfig *gameConfig)
685684

686685
FOR_EACH_VEC(g_vecDetours, i)
687686
{
688-
if (!V_strcmp(g_vecDetours[i]->GetName(), "CEntityIOOutput_FireOutputInternal"))
689-
{
690-
// Check if features needing this detour are actually enabled.
691-
// If not, leave this detour disabled for CS# compatibility
692-
if (!IsButtonWatchEnabled())
693-
continue;
694-
}
695-
696687
if (!g_vecDetours[i]->CreateDetour(gameConfig))
697688
success = false;
698689

src/detours.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "cdetour.h"
2222
#include <utlsymbollarge.h>
2323

24+
2425
class CCheckTransmitInfo;
2526
class IRecipientFilter;
2627
class ISoundEmitterSystemBase;

src/entities.cpp

+50-63
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
// #define ENTITY_HANDLER_ASSERTION
3636

37+
extern CCSGameRules* g_pGameRules;
38+
3739
class InputData_t
3840
{
3941
public:
@@ -724,15 +726,60 @@ void EntityHandler_OnEntitySpawned(CBaseEntity* pEntity)
724726
CPointViewControlHandler::OnCreated(pEntity);
725727
}
726728

727-
std::map <int, bool> mapRecentEnts;
728729
CDetour<decltype(Detour_CEntityIOOutput_FireOutputInternal)>* CEntityIOOutput_FireOutputInternal = nullptr;
730+
using IOCallback = std::function<void(const CEntityIOOutput*, CEntityInstance*, CEntityInstance*, const CVariant*, float)>;
731+
// Add callback functions to this map that wish to hook into Detour_CEntityIOOutput_FireOutputInternal
732+
// to make it more modular/cleaner than shoving everything into the detour (buttonwatch, entwatch, etc.)
733+
std::map<std::string, IOCallback> mapIOFunctions;
734+
void FASTCALL Detour_CEntityIOOutput_FireOutputInternal(const CEntityIOOutput* pThis, CEntityInstance* pActivator, CEntityInstance* pCaller, const CVariant* value, float flDelay)
735+
{
736+
for (const auto& [name, cb] : mapIOFunctions)
737+
cb(pThis, pActivator, pCaller, value, flDelay);
738+
739+
(*CEntityIOOutput_FireOutputInternal)(pThis, pActivator, pCaller, value, flDelay);
740+
}
741+
742+
// Tries to setup Detour_CEntityIOOutput_FireOutputInternal if it is not already setup.
743+
// Returns true if detour is usable, otherwise false.
744+
bool SetupFireOutputInternalDetour()
745+
{
746+
if (CEntityIOOutput_FireOutputInternal != nullptr)
747+
return true;
748+
749+
CEntityIOOutput_FireOutputInternal = new CDetour(Detour_CEntityIOOutput_FireOutputInternal, "CEntityIOOutput_FireOutputInternal");
750+
if (!CEntityIOOutput_FireOutputInternal->CreateDetour(g_GameConfig))
751+
{
752+
Msg("Failed to detour CEntityIOOutput_FireOutputInternal\n");
753+
delete CEntityIOOutput_FireOutputInternal;
754+
CEntityIOOutput_FireOutputInternal = nullptr;
755+
return false;
756+
}
757+
CEntityIOOutput_FireOutputInternal->EnableDetour();
758+
return true;
759+
}
760+
761+
CON_COMMAND_F(cs2f_enable_button_watch, "CS# BREAKS IF THIS IS EVER ENABLED. Whether to enable button watch or not.", FCVAR_LINKED_CONCOMMAND | FCVAR_SPONLY | FCVAR_PROTECTED)
762+
{
763+
if (args.ArgC() < 2)
764+
{
765+
Msg("%s %i\n", args[0], IsButtonWatchEnabled());
766+
return;
767+
}
768+
769+
if (!V_StringToBool(args[1], false) || !SetupFireOutputInternalDetour())
770+
mapIOFunctions.erase("buttonwatch");
771+
else if (!IsButtonWatchEnabled())
772+
mapIOFunctions["buttonwatch"] = ButtonWatch;
773+
}
774+
729775
bool IsButtonWatchEnabled()
730776
{
731-
return std::any_of(vecIOFunctions.begin(), vecIOFunctions.end(), [](IOCallback& cb) {
732-
return cb.target<decltype(ButtonWatch)>() == &ButtonWatch;
777+
return std::any_of(mapIOFunctions.begin(), mapIOFunctions.end(), [](const auto& p) {
778+
return p.first == "buttonwatch";
733779
});
734780
}
735781

782+
std::map <int, bool> mapRecentEnts;
736783
void ButtonWatch(const CEntityIOOutput* pThis, CEntityInstance* pActivator, CEntityInstance* pCaller, const CVariant* value, float flDelay)
737784
{
738785
if (!IsButtonWatchEnabled() || V_stricmp(pThis->m_pDesc->m_pName, "OnPressed") ||
@@ -783,64 +830,4 @@ void ButtonWatch(const CEntityIOOutput* pThis, CEntityInstance* pActivator, CEnt
783830
mapRecentEnts.erase(iIndex);
784831
return -1.0f;
785832
});
786-
}
787-
788-
extern CCSGameRules* g_pGameRules;
789-
// Tries to setup Detour_CEntityIOOutput_FireOutputInternal if it is not already setup.
790-
// Returns true if detour is usable, otherwise false.
791-
bool SetupFireOutputInternalDetour()
792-
{
793-
if (CEntityIOOutput_FireOutputInternal != nullptr)
794-
return true;
795-
796-
CEntityIOOutput_FireOutputInternal = new CDetour(Detour_CEntityIOOutput_FireOutputInternal, "CEntityIOOutput_FireOutputInternal");
797-
if (!CEntityIOOutput_FireOutputInternal->CreateDetour(g_GameConfig))
798-
{
799-
Msg("Failed to detour CEntityIOOutput_FireOutputInternal\n");
800-
delete CEntityIOOutput_FireOutputInternal;
801-
CEntityIOOutput_FireOutputInternal = nullptr;
802-
return false;
803-
}
804-
CEntityIOOutput_FireOutputInternal->EnableDetour();
805-
return true;
806-
}
807-
808-
CON_COMMAND_F(cs2f_enable_button_watch, "CS# BREAKS IF THIS IS EVER ENABLED. Whether to enable button watch or not.", FCVAR_LINKED_CONCOMMAND | FCVAR_SPONLY | FCVAR_PROTECTED)
809-
{
810-
if (args.ArgC() < 2)
811-
{
812-
Msg("%s %b\n", args[0], IsButtonWatchEnabled());
813-
return;
814-
}
815-
816-
if (!V_StringToBool(args[1], false) || !SetupFireOutputInternalDetour())
817-
{
818-
vecIOFunctions.erase(std::remove_if(vecIOFunctions.begin(), vecIOFunctions.end(), [](const IOCallback& cb) {
819-
return cb.target<decltype(ButtonWatch)>() == &ButtonWatch;
820-
}), vecIOFunctions.end());
821-
}
822-
else if (!IsButtonWatchEnabled())
823-
{
824-
vecIOFunctions.push_back(ButtonWatch);
825-
}
826-
}
827-
828-
using IOCallback = std::function<void(const CEntityIOOutput*, CEntityInstance*, CEntityInstance*, const CVariant*, float)>;
829-
// Add callback functions to this vector that wish to hook into Detour_CEntityIOOutput_FireOutputInternal
830-
// to make it more modular/cleaner than shoving everything into the detour
831-
std::vector<IOCallback> vecIOFunctions;
832-
void FASTCALL Detour_CEntityIOOutput_FireOutputInternal(const CEntityIOOutput* pThis, CEntityInstance* pActivator, CEntityInstance* pCaller, const CVariant* value, float flDelay)
833-
{
834-
#ifdef DEBUG
835-
// pCaller can absolutely be null. Needs to be checked
836-
if(pCaller)
837-
ConMsg("Output %s fired on %s\n", pThis->m_pDesc->m_pName, pCaller->GetClassname());
838-
else
839-
ConMsg("Output %s fired with no caller\n", pThis->m_pDesc->m_pName);
840-
#endif
841-
842-
for (const auto& cb : vecIOFunctions)
843-
cb(pThis, pActivator, pCaller, value, flDelay);
844-
845-
(*CEntityIOOutput_FireOutputInternal)(pThis, pActivator, pCaller, value, flDelay);
846833
}

0 commit comments

Comments
 (0)