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.

224 lines
8.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #if !defined( IGAMEEVENTS_H )
  14. #define IGAMEEVENTS_H
  15. #ifdef _WIN32
  16. #pragma once
  17. #endif
  18. #include "tier1/interface.h"
  19. #define INTERFACEVERSION_GAMEEVENTSMANAGER "GAMEEVENTSMANAGER001" // old game event manager, don't use it!
  20. #define INTERFACEVERSION_GAMEEVENTSMANAGER2 "GAMEEVENTSMANAGER002" // new game event manager,
  21. #include "tier1/bitbuf.h"
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Engine interface into global game event management
  24. //-----------------------------------------------------------------------------
  25. /*
  26. The GameEventManager keeps track and fires of all global game events. Game events
  27. are fired by game.dll for events like player death or team wins. Each event has a
  28. unique name and comes with a KeyValue structure providing informations about this
  29. event. Some events are generated also by the engine.
  30. Events are networked to connected clients and invoked there to. Therefore you
  31. have to specify all data fields and there data types in an public resource
  32. file which is parsed by server and broadcasted to it's clients. A typical game
  33. event is defined like this:
  34. "game_start" // a new game starts
  35. {
  36. "roundslimit" "long" // max round
  37. "timelimit" "long" // time limit
  38. "fraglimit" "long" // frag limit
  39. "objective" "string" // round objective
  40. }
  41. All events must have unique names (case sensitive) and may have a list
  42. of data fields. each data field must specify a data type, so the engine
  43. knows how to serialize/unserialize that event for network transmission.
  44. Valid data types are string, float, long, short, byte & bool. If a
  45. data field should not be broadcasted to clients, use the type "local".
  46. */
  47. #define MAX_EVENT_NAME_LENGTH 32 // max game event name length
  48. #define MAX_EVENT_BITS 9 // max bits needed for an event index
  49. #define MAX_EVENT_NUMBER (1<<MAX_EVENT_BITS) // max number of events allowed
  50. #define MAX_EVENT_BYTES 1024 // max size in bytes for a serialized event
  51. class KeyValues;
  52. class CGameEvent;
  53. class CSVCMsg_GameEvent;
  54. // Class for visiting every key of data on an event
  55. abstract_class IGameEventVisitor2
  56. {
  57. public:
  58. // return true to keep iterating, false to abort iteration
  59. virtual bool VisitLocal( const char* name, const void* value ) { return true; }
  60. virtual bool VisitString( const char* name, const char* value ) { return true; }
  61. virtual bool VisitFloat( const char* name, float value ) { return true; }
  62. virtual bool VisitInt( const char* name, int value ) { return true; }
  63. virtual bool VisitUint64( const char* name, uint64 value ) { return true; }
  64. virtual bool VisitWString( const char* name, const wchar_t* value ) { return true; }
  65. virtual bool VisitBool( const char* name, bool value ) { return true; }
  66. };
  67. abstract_class IGameEvent
  68. {
  69. public:
  70. virtual ~IGameEvent() {};
  71. virtual const char *GetName() const = 0; // get event name
  72. virtual bool IsReliable() const = 0; // if event handled reliable
  73. virtual bool IsLocal() const = 0; // if event is never networked
  74. virtual bool IsEmpty(const char *keyName = NULL) const = 0; // check if data field exists
  75. // Data access
  76. virtual bool GetBool( const char *keyName = NULL, bool defaultValue = false ) const = 0;
  77. virtual int GetInt( const char *keyName = NULL, int defaultValue = 0 ) const = 0;
  78. virtual uint64 GetUint64( const char *keyName = NULL, uint64 defaultValue = 0 ) const = 0;
  79. virtual float GetFloat( const char *keyName = NULL, float defaultValue = 0.0f ) const = 0;
  80. virtual const char *GetString( const char *keyName = NULL, const char *defaultValue = "" ) const = 0;
  81. virtual const wchar_t *GetWString( const char *keyName = NULL, const wchar_t *defaultValue = L"" ) const = 0;
  82. virtual const void *GetPtr( const char *keyName = NULL ) const = 0; // LOCAL only
  83. virtual void SetBool( const char *keyName, bool value ) = 0;
  84. virtual void SetInt( const char *keyName, int value ) = 0;
  85. virtual void SetUint64( const char *keyName, uint64 value ) = 0;
  86. virtual void SetFloat( const char *keyName, float value ) = 0;
  87. virtual void SetString( const char *keyName, const char *value ) = 0;
  88. virtual void SetWString( const char *keyName, const wchar_t *value ) = 0;
  89. virtual void SetPtr( const char *keyName, const void * value ) = 0; // LOCAL only
  90. // returns true if iteration aborted normally, false if it was aborted by the visitor callback
  91. virtual bool ForEventData( IGameEventVisitor2* event ) const = 0;
  92. };
  93. #define EVENT_DEBUG_ID_INIT 42
  94. #define EVENT_DEBUG_ID_SHUTDOWN 13
  95. abstract_class IGameEventListener2
  96. {
  97. public:
  98. virtual ~IGameEventListener2( void ) {};
  99. // FireEvent is called by EventManager if event just occured
  100. // KeyValue memory will be freed by manager if not needed anymore
  101. virtual void FireGameEvent( IGameEvent *event ) = 0;
  102. virtual int GetEventDebugID( void ) = 0;
  103. };
  104. abstract_class IGameEventManager2 : public IBaseInterface
  105. {
  106. public:
  107. virtual ~IGameEventManager2( void ) {};
  108. // load game event descriptions from a file eg "resource\gameevents.res"
  109. virtual int LoadEventsFromFile( const char *filename ) = 0;
  110. // removes all and anything
  111. virtual void Reset() = 0;
  112. // adds a listener for a particular event
  113. virtual bool AddListener( IGameEventListener2 *listener, const char *name, bool bServerSide ) = 0;
  114. // returns true if this listener is listens to given event
  115. virtual bool FindListener( IGameEventListener2 *listener, const char *name ) = 0;
  116. // removes a listener
  117. virtual void RemoveListener( IGameEventListener2 *listener) = 0;
  118. // add a listener that listens to all events.
  119. virtual bool AddListenerGlobal( IGameEventListener2 *listener, bool bServerSide ) = 0;
  120. // create an event by name, but doesn't fire it. returns NULL is event is not
  121. // known or no listener is registered for it. bForce forces the creation even if no listener is active
  122. virtual IGameEvent *CreateEvent( const char *name, bool bForce = false, int *pCookie = NULL ) = 0;
  123. // fires a server event created earlier, if bDontBroadcast is set, event is not send to clients
  124. virtual bool FireEvent( IGameEvent *event, bool bDontBroadcast = false ) = 0;
  125. // fires an event for the local client only, should be used only by client code
  126. virtual bool FireEventClientSide( IGameEvent *event ) = 0;
  127. // create a new copy of this event, must be free later
  128. virtual IGameEvent *DuplicateEvent( IGameEvent *event ) = 0;
  129. // if an event was created but not fired for some reason, it has to bee freed, same UnserializeEvent
  130. virtual void FreeEvent( IGameEvent *event ) = 0;
  131. // write/read event to/from bitbuffer
  132. virtual bool SerializeEvent( IGameEvent *event, CSVCMsg_GameEvent *eventMsg ) = 0;
  133. virtual IGameEvent *UnserializeEvent( const CSVCMsg_GameEvent& eventMsg ) = 0; // create new KeyValues, must be deleted
  134. };
  135. // the old game event manager interface, don't use it. Rest is legacy support:
  136. abstract_class IGameEventListener
  137. {
  138. public:
  139. virtual ~IGameEventListener( void ) {};
  140. // FireEvent is called by EventManager if event just occured
  141. // KeyValue memory will be freed by manager if not needed anymore
  142. virtual void FireGameEvent( KeyValues * event) = 0;
  143. };
  144. abstract_class IGameEventManager : public IBaseInterface
  145. {
  146. public:
  147. virtual ~IGameEventManager( void ) {};
  148. // load game event descriptions from a file eg "resource\gameevents.res"
  149. virtual int LoadEventsFromFile( const char * filename ) = 0;
  150. // removes all and anything
  151. virtual void Reset() = 0;
  152. virtual KeyValues *GetEvent(const char * name) = 0; // returns keys for event
  153. // adds a listener for a particular event
  154. virtual bool AddListener( IGameEventListener * listener, const char * event, bool bIsServerSide ) = 0;
  155. // registers for all known events
  156. virtual bool AddListener( IGameEventListener * listener, bool bIsServerSide ) = 0;
  157. // removes a listener
  158. virtual void RemoveListener( IGameEventListener * listener) = 0;
  159. // fires an global event, specific event data is stored in KeyValues
  160. // local listeners will receive the event instantly
  161. // a network message will be send to all connected clients to invoke
  162. // the same event there
  163. virtual bool FireEvent( KeyValues * event ) = 0;
  164. // fire a side server event, that it wont be broadcasted to player clients
  165. virtual bool FireEventServerOnly( KeyValues * event ) = 0;
  166. // fires an event only on this local client
  167. // can be used to fake events coming over the network
  168. virtual bool FireEventClientOnly( KeyValues * event ) = 0;
  169. // write/read event to/from bitbuffer
  170. virtual bool SerializeKeyValues( KeyValues *event, bf_write *buf, CGameEvent *eventtype = NULL ) = 0;
  171. virtual KeyValues *UnserializeKeyValue( bf_read *msg ) = 0; // create new KeyValues, must be deleted
  172. };
  173. #endif // IGAMEEVENTS_H