34
34
35
35
// #define ENTITY_HANDLER_ASSERTION
36
36
37
+ extern CCSGameRules* g_pGameRules;
38
+
37
39
class InputData_t
38
40
{
39
41
public:
@@ -724,15 +726,60 @@ void EntityHandler_OnEntitySpawned(CBaseEntity* pEntity)
724
726
CPointViewControlHandler::OnCreated (pEntity);
725
727
}
726
728
727
- std::map <int , bool > mapRecentEnts;
728
729
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
+
729
775
bool IsButtonWatchEnabled ()
730
776
{
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 " ;
733
779
});
734
780
}
735
781
782
+ std::map <int , bool > mapRecentEnts;
736
783
void ButtonWatch (const CEntityIOOutput* pThis, CEntityInstance* pActivator, CEntityInstance* pCaller, const CVariant* value, float flDelay)
737
784
{
738
785
if (!IsButtonWatchEnabled () || V_stricmp (pThis->m_pDesc ->m_pName , " OnPressed" ) ||
@@ -783,64 +830,4 @@ void ButtonWatch(const CEntityIOOutput* pThis, CEntityInstance* pActivator, CEnt
783
830
mapRecentEnts.erase (iIndex);
784
831
return -1 .0f ;
785
832
});
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);
846
833
}
0 commit comments