Team Fortress 2 Source Code as on 22/4/2020
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.

172 lines
5.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SHARED_OBJECT_MANAGER_H
  8. #define SHARED_OBJECT_MANAGER_H
  9. #include "GameEventListener.h"
  10. #include "econ_item_constants.h"
  11. #include "econ_item_inventory.h"
  12. #ifdef GAME_DLL
  13. #include "tf_player.h"
  14. #else
  15. #include "c_tf_player.h"
  16. #include "local_steam_shared_object_listener.h"
  17. #endif
  18. #if defined( _WIN32 )
  19. #pragma once
  20. #endif
  21. using namespace GCSDK;
  22. class CSOTrackerManager;
  23. extern short g_nQuestSpewFlags;
  24. #define SO_TRACKER_SPEW_OBJECTIVES 1<<0
  25. #define SO_TRACKER_SPEW_ITEM_TRACKER_MANAGEMENT 1<<1
  26. #define SO_TRACKER_SPEW_GC_COMMITS 1<<2
  27. #define SO_TRACKER_SPEW_OBJECTIVE_TRACKER_MANAGEMENT 1<<3
  28. #define SO_TRACKER_SPEW_SOCACHE_ACTIVITY 1<<4
  29. #define SO_TRACKER_SPEW_TRACKER_ACCEPTANCE 1<<5
  30. void SOTrackerSpew( const char* pszBuff, int nType );
  31. #define SO_TRACKER_SPEW( pszBuff, nType ) SOTrackerSpew( pszBuff, nType );
  32. class CBaseSOTracker
  33. {
  34. public:
  35. DECLARE_CLASS_NOBASE( CBaseSOTracker )
  36. CBaseSOTracker( const CSharedObject* pSObject, CSteamID steamIDOwner, CSOTrackerManager* pManager );
  37. virtual ~CBaseSOTracker();
  38. const CSharedObject* GetSObject() const { return m_pSObject; }
  39. const CSteamID GetOwnerSteamID() const { return m_steamIDOwner; }
  40. const CTFPlayer* GetTrackedPlayer() const { return ToTFPlayer( GetPlayerBySteamID( m_steamIDOwner ) ); }
  41. virtual void Spew() const;
  42. virtual void CommitChangesToDB() = 0;
  43. virtual void OnUpdate() = 0;
  44. virtual void OnRemove() = 0;
  45. protected:
  46. const CSharedObject* m_pSObject;
  47. CSteamID m_steamIDOwner;
  48. CSOTrackerManager* m_pManager;
  49. };
  50. struct CommitRecord_t
  51. {
  52. CommitRecord_t( ::google::protobuf::Message* pMessage )
  53. : m_flLastCommitTime( Plat_FloatTime() )
  54. , m_flReportedTime( Plat_FloatTime() )
  55. , m_pProtoMsg( pMessage )
  56. {}
  57. ~CommitRecord_t()
  58. {
  59. if ( m_pProtoMsg )
  60. delete m_pProtoMsg;
  61. }
  62. double m_flLastCommitTime;
  63. double m_flReportedTime;
  64. ::google::protobuf::Message* m_pProtoMsg;
  65. private:
  66. CommitRecord_t(); // Nope
  67. };
  68. typedef CUtlMap< uint64, CommitRecord_t* > CommitsMap_t;
  69. // A class to handle the creation and deletion of shared object trackers. Automatically
  70. // subscribes to the local player's SOCache and will subscribe to any connecting players'
  71. // SOCaches when they connect.
  72. #ifdef GAME_DLL
  73. class CSOTrackerManager : public ISharedObjectListener, public CGameEventListener, public CAutoGameSystemPerFrame
  74. #else
  75. class CSOTrackerManager : public CLocalSteamSharedObjectListener, public CGameEventListener, public CAutoGameSystemPerFrame
  76. #endif
  77. {
  78. public:
  79. DECLARE_CLASS_NOBASE( CSOTrackerManager )
  80. typedef CUtlMap< uint64, CBaseSOTracker * > SOTrackerMap_t;
  81. CSOTrackerManager();
  82. virtual ~CSOTrackerManager();
  83. virtual void Initialize();
  84. virtual void Shutdown();
  85. virtual void FireGameEvent( IGameEvent *pEvent ) OVERRIDE;
  86. void EnsureTrackersForPlayer( const CSteamID& steamIDPlayer );
  87. void EnsureTrackersForPlayer( CTFPlayer* pPlayer );
  88. void SOCreated( const CSteamID & steamIDOwner, const CSharedObject *pObject, ESOCacheEvent eEvent ) OVERRIDE;
  89. void PreSOUpdate( const CSteamID & steamIDOwner, ESOCacheEvent eEvent ) OVERRIDE {};
  90. void SOUpdated( const CSteamID & steamIDOwner, const CSharedObject *pObject, ESOCacheEvent eEvent ) OVERRIDE;
  91. void PostSOUpdate( const CSteamID & steamIDOwner, ESOCacheEvent eEvent ) OVERRIDE {};
  92. void SODestroyed( const CSteamID & steamIDOwner, const CSharedObject *pObject, ESOCacheEvent eEvent ) OVERRIDE;
  93. void SOCacheSubscribed( const CSteamID & steamIDOwner, ESOCacheEvent eEvent ) OVERRIDE;
  94. void SOCacheUnsubscribed( const CSteamID & steamIDOwner, ESOCacheEvent eEvent ) OVERRIDE;
  95. #ifdef GAME_DLL
  96. virtual void FrameUpdatePreEntityThink() OVERRIDE;
  97. void AddCommitRecord( const ::google::protobuf::Message* pRecord, uint64 nKey, bool bRequireResponse );
  98. void AcknowledgeCommit( const ::google::protobuf::Message* pRecord, uint64 nKey );
  99. void DBG_SpewPendingCommits();
  100. #endif
  101. void Spew();
  102. virtual SOTrackerMap_t::KeyType_t GetKeyForObjectTracker( const CSharedObject* pItem, CSteamID steamIDOwner ) = 0;
  103. CBaseSOTracker* GetTracker( SOTrackerMap_t::KeyType_t nKey ) const;
  104. template< class T >
  105. T GetTypedTracker( SOTrackerMap_t::KeyType_t nKey ) const { return assert_cast< T >( GetTracker( nKey ) ); }
  106. CommitRecord_t* GetCommitRecord( CommitsMap_t::KeyType_t );
  107. protected:
  108. enum ETrackerHandling_t
  109. {
  110. TRACKER_CREATE_OR_UPDATE = 0,
  111. TRACKER_REMOVE,
  112. };
  113. void UpdateTrackerForItem( const CSharedObject* pItem, ETrackerHandling_t eHandling, CSteamID steamIDOwner );
  114. private:
  115. virtual int GetType() const = 0;
  116. virtual const char* GetName() const = 0;
  117. virtual CFmtStr GetDebugObjectDescription( const CSharedObject* pItem ) const = 0;
  118. virtual CBaseSOTracker* AllocateNewTracker( const CSharedObject* pItem, CSteamID steamIDOwner, CSOTrackerManager* pManager ) const = 0;
  119. virtual ::google::protobuf::Message* AllocateNewProtoMessage() const = 0;
  120. virtual void OnCommitRecieved( const ::google::protobuf::Message* pProtoMsg ) = 0;
  121. virtual bool ShouldTrackObject( const CSteamID & steamIDOwner, const CSharedObject *pObject ) const = 0;
  122. virtual int CompareRecords( const ::google::protobuf::Message* pNewProtoMsg, const ::google::protobuf::Message* pExistingProtoMsg ) const = 0;
  123. void HandleSOEvent( const CSteamID & steamIDOwner, const CSharedObject *pObject, ETrackerHandling_t eHandling );
  124. void CommitAllChanges();
  125. void CreateAndAddTracker( const CSharedObject* pItem, CSteamID steamIDOwner );
  126. void RemoveAndDeleteTrackerAtIndex( SOTrackerMap_t::IndexType_t idx );
  127. void RemoveTrackersForSteamID( const CSteamID & steamIDOwner );
  128. #ifdef GAME_DLL
  129. void CommitRecord( CommitRecord_t* pRecord ) const;
  130. virtual void SendMessageForCommit( const ::google::protobuf::Message* pProtoMessage ) const = 0;
  131. #endif
  132. double m_flLastUnacknowledgeCommitTime;
  133. CommitsMap_t m_mapUnacknowledgedCommits;
  134. SOTrackerMap_t m_mapItemTrackers;
  135. };
  136. #endif // SHARED_OBJECT_MANAGER_H