Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: gameeventmanager.h: interface for the CGameEventManager class.
  4. //
  5. // $Workfile: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #if !defined ( GAMEEVENTMANAGER_H )
  9. #define GAMEEVENTMANAGER_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include <igameevents.h>
  14. #include <utlvector.h>
  15. #include <keyvalues.h>
  16. #include <networkstringtabledefs.h>
  17. #include <utlsymbol.h>
  18. #include <utldict.h>
  19. #include "netmessages.h"
  20. class CSVCMsg_GameEventList;
  21. class CCLCMsg_ListenEvents;
  22. class CGameEventCallback
  23. {
  24. public:
  25. void *m_pCallback; // callback pointer
  26. int m_nListenerType; // client or server side ?
  27. };
  28. class CGameEventDescriptor
  29. {
  30. public:
  31. CGameEventDescriptor()
  32. {
  33. eventid = -1;
  34. keys = NULL;
  35. local = false;
  36. reliable = true;
  37. elementIndex = -1;
  38. numSerialized = 0;
  39. numUnSerialized = 0;
  40. totalSerializedBits = 0;
  41. totalUnserializedBits = 0;
  42. }
  43. public:
  44. int eventid; // network index number, -1 = not networked
  45. int elementIndex;
  46. KeyValues *keys; // KeyValue describing data types, if NULL only name
  47. CUtlVector<CGameEventCallback*> listeners; // registered listeners
  48. bool local; // local event, never tell clients about that
  49. bool reliable; // send this event as reliable message
  50. // Extra data for network monitoring
  51. int numSerialized;
  52. int numUnSerialized;
  53. int totalSerializedBits;
  54. int totalUnserializedBits;
  55. };
  56. class CGameEvent : public IGameEvent
  57. {
  58. public:
  59. CGameEvent( CGameEventDescriptor *descriptor, const char *name );
  60. virtual ~CGameEvent();
  61. virtual const char *GetName() const OVERRIDE;
  62. virtual bool IsEmpty(const char *keyName = NULL) const OVERRIDE;
  63. virtual bool IsLocal() const OVERRIDE;
  64. virtual bool IsReliable() const OVERRIDE;
  65. virtual bool GetBool( const char *keyName = NULL, bool defaultValue = false ) const OVERRIDE;
  66. virtual int GetInt( const char *keyName = NULL, int defaultValue = 0 ) const OVERRIDE;
  67. virtual uint64 GetUint64( const char *keyName = NULL, uint64 defaultValue = 0 ) const OVERRIDE;
  68. virtual float GetFloat( const char *keyName = NULL, float defaultValue = 0.0f ) const OVERRIDE;
  69. virtual const char *GetString( const char *keyName = NULL, const char *defaultValue = "" ) const OVERRIDE;
  70. virtual const wchar_t *GetWString( const char *keyName = NULL, const wchar_t *defaultValue = L"" ) const OVERRIDE;
  71. virtual const void *GetPtr( const char *keyName = NULL ) const OVERRIDE;
  72. virtual void SetBool( const char *keyName, bool value ) OVERRIDE;
  73. virtual void SetInt( const char *keyName, int value ) OVERRIDE;
  74. virtual void SetUint64( const char *keyName, uint64 value ) OVERRIDE;
  75. virtual void SetFloat( const char *keyName, float value ) OVERRIDE;
  76. virtual void SetString( const char *keyName, const char *value ) OVERRIDE;
  77. virtual void SetWString( const char *keyName, const wchar_t *value ) OVERRIDE;
  78. virtual void SetPtr( const char *keyName, const void * value ) OVERRIDE;
  79. virtual bool ForEventData( IGameEventVisitor2* visitor ) const OVERRIDE;
  80. CGameEventDescriptor *m_pDescriptor;
  81. KeyValues *m_pDataKeys;
  82. };
  83. // NOTE: Every non-threadsafe externally-callable function must lock m_mutex before modifying the event manager
  84. // member variables. Client & server threads can call into this class simultaneously
  85. class CGameEventManager : public IGameEventManager2
  86. {
  87. friend class CGameEventManagerOld;
  88. public: // IGameEventManager functions
  89. enum
  90. {
  91. SERVERSIDE = 0, // this is a server side listener, event logger etc
  92. CLIENTSIDE, // this is a client side listenet, HUD element etc
  93. CLIENTSTUB, // this is a serverside stub for a remote client listener (used by engine only)
  94. SERVERSIDE_OLD, // legacy support for old server event listeners
  95. CLIENTSIDE_OLD, // legecy support for old client event listeners
  96. };
  97. enum
  98. {
  99. TYPE_LOCAL = 0, // not networked
  100. TYPE_STRING, // zero terminated ASCII string
  101. TYPE_FLOAT, // float 32 bit
  102. TYPE_LONG, // signed int 32 bit
  103. TYPE_SHORT, // signed int 16 bit
  104. TYPE_BYTE, // unsigned int 8 bit
  105. TYPE_BOOL, // unsigned int 1 bit
  106. TYPE_UINT64, // unsigned int 64 bit
  107. TYPE_WSTRING, // zero terminated wide char string
  108. TYPE_COUNT,
  109. };
  110. CGameEventManager();
  111. virtual ~CGameEventManager();
  112. int LoadEventsFromFile( const char * filename );
  113. void Reset();
  114. bool AddListener( IGameEventListener2 *listener, const char *name, bool bServerSide );
  115. bool FindListener( IGameEventListener2 *listener, const char *name );
  116. void RemoveListener( IGameEventListener2 *listener);
  117. virtual bool AddListenerGlobal( IGameEventListener2 *listener, bool bServerSide );
  118. IGameEvent *CreateEvent( const char *name, bool bForce = false, int *pCookie = NULL );
  119. IGameEvent *DuplicateEvent( IGameEvent *event);
  120. bool FireEvent( IGameEvent *event, bool bDontBroadcast = false );
  121. bool FireEventClientSide( IGameEvent *event );
  122. void FreeEvent( IGameEvent *event );
  123. bool SerializeEvent( IGameEvent *event, CSVCMsg_GameEvent *eventMsg );
  124. IGameEvent *UnserializeEvent( const CSVCMsg_GameEvent& eventMsg );
  125. virtual KeyValues* GetEventDataTypes( IGameEvent* event );
  126. void DumpEventNetworkStats();
  127. public:
  128. bool Init();
  129. void Shutdown();
  130. void ReloadEventDefinitions(); // called by server on new map
  131. bool AddListener( void *listener, CGameEventDescriptor *descriptor, int nListenerType );
  132. CGameEventDescriptor *GetEventDescriptor( const char *name, int *pCookie = NULL );
  133. CGameEventDescriptor *GetEventDescriptor( IGameEvent *event );
  134. CGameEventDescriptor *GetEventDescriptor( int eventid );
  135. void WriteEventList(CSVCMsg_GameEventList *msg);
  136. bool ParseEventList(const CSVCMsg_GameEventList& msg);
  137. void WriteListenEventList(CCLCMsg_ListenEvents *msg);
  138. bool HasClientListenersChanged( bool bReset = true );
  139. void ConPrintEvent( IGameEvent *event);
  140. // legacy support
  141. bool AddListenerAll( void *listener, int nListenerType );
  142. void RemoveListenerOld( void *listener);
  143. // Debug!
  144. void VerifyListenerList( void );
  145. protected:
  146. IGameEvent *CreateEvent( CGameEventDescriptor *descriptor, const char *name );
  147. bool RegisterEvent( KeyValues * keys );
  148. void UnregisterEvent(int index);
  149. bool FireEventIntern( IGameEvent *event, bool bServerSide, bool bClientOnly );
  150. CGameEventCallback* FindEventListener( void* listener );
  151. CUtlVector<CGameEventDescriptor> m_GameEvents; // list of all known events
  152. CUtlVector<CGameEventCallback*> m_Listeners; // list of all registered listeners
  153. CUtlSymbolTable m_EventFiles; // list of all loaded event files
  154. CUtlVector<CUtlSymbol> m_EventFileNames;
  155. CUtlDict<int, int> m_EventMap;
  156. CThreadFastMutex m_mutex; // lock this when modifying the event table
  157. bool m_bClientListenersChanged; // true every time client changed listeners
  158. };
  159. extern CGameEventManager &g_GameEventManager;
  160. #endif