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.

140 lines
5.5 KiB

  1. //====== Copyright �, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Base class for objects that are kept in synch between client and server
  4. //
  5. //=============================================================================
  6. #ifndef SHAREDOBJECT_H
  7. #define SHAREDOBJECT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "utlsortvector.h"
  12. #include "tier0/memdbgon.h"
  13. namespace GCSDK
  14. {
  15. class CSQLAccess;
  16. class CSharedObject;
  17. typedef CSharedObject *(*SOCreationFunc_t)( );
  18. class CSharedObjectCache;
  19. //----------------------------------------------------------------------------
  20. // Purpose: Abstract base class for objects that are shared between the GC and
  21. // a gameserver/client. These can also be stored in the database.
  22. //----------------------------------------------------------------------------
  23. class CSharedObject
  24. {
  25. friend class CGCSharedObjectCache;
  26. friend class CSharedObjectCache;
  27. public:
  28. virtual ~CSharedObject() {}
  29. virtual int GetTypeID() const = 0;
  30. virtual bool BParseFromMessage( const CUtlBuffer & buffer ) = 0;
  31. virtual bool BParseFromMessage( const std::string &buffer ) = 0;
  32. virtual bool BUpdateFromNetwork( const CSharedObject & objUpdate ) = 0;
  33. virtual bool BIsKeyLess( const CSharedObject & soRHS ) const = 0;
  34. virtual void Copy( const CSharedObject & soRHS ) = 0;
  35. virtual void Dump() const = 0;
  36. virtual bool BAddToMessage( std::string *pBuffer ) const = 0;
  37. virtual bool BAddDestroyToMessage( std::string *pBuffer ) const = 0;
  38. bool BIsKeyEqual( const CSharedObject & soRHS ) const;
  39. static void RegisterFactory( int nTypeID, SOCreationFunc_t fnFactory, uint32 unFlags, const char *pchClassName, const char* pszBuildCacheName, const char* pszCreateName, const char* pszUpdateName );
  40. static CSharedObject *Create( int nTypeID );
  41. static uint32 GetTypeFlags( int nTypeID );
  42. static const char *PchClassName( int nTypeID );
  43. static const char *PchClassBuildCacheNodeName( int nTypeID );
  44. static const char *PchClassCreateNodeName( int nTypeID );
  45. static const char *PchClassUpdateNodeName( int nTypeID );
  46. #ifdef GC
  47. virtual bool BIsNetworked() const { return true; }
  48. virtual bool BIsDatabaseBacked() const { return true; }
  49. virtual bool BYieldingAddToDatabase();
  50. virtual bool BYieldingWriteToDatabase( const CUtlVector< int > &fields );
  51. virtual bool BYieldingRemoveFromDatabase();
  52. virtual bool BYieldingAddInsertToTransaction( CSQLAccess & sqlAccess ) { return false; }
  53. virtual bool BYieldingAddWriteToTransaction( CSQLAccess & sqlAccess, const CUtlVector< int > &fields ) { return false; }
  54. virtual bool BYieldingAddRemoveToTransaction( CSQLAccess & sqlAccess ) { return false; }
  55. virtual bool BParseFromMemcached( CUtlBuffer & buffer ) { return false; }
  56. virtual bool BAddToMemcached( CUtlBuffer & bufOutput ) const { return false; }
  57. bool BSendCreateToSteamIDs( const CUtlVector<CSteamID> & vecRecipients, const SOID_t ownerID, uint64 ulVersion ) const;
  58. bool BSendDestroyToSteamIDs( const CUtlVector<CSteamID> & vecRecipients, const SOID_t ownerID, uint64 ulVersion ) const;
  59. protected:
  60. /*
  61. // Dirty bit modification. Do not call these directly on SharedObjects. Call them
  62. // on the cache that owns the object so they can be added/removed from the right lists.
  63. virtual void DirtyField( int nField ) = 0;
  64. virtual void MakeDatabaseClean() = 0;
  65. virtual void MakeNetworkClean() = 0;
  66. */
  67. #endif // GC
  68. private:
  69. struct SharedObjectInfo_t
  70. {
  71. int m_nID;
  72. uint32 m_unFlags;
  73. SOCreationFunc_t m_pFactoryFunction;
  74. const char *m_pchClassName;
  75. const char *m_pchBuildCacheSubNodeName;
  76. const char *m_pchUpdateNodeName;
  77. const char *m_pchCreateNodeName;
  78. };
  79. //compare class that supports sorting shared objects themselves, as well as searching based upon an integer key value
  80. class CCompareSharedObject
  81. {
  82. public:
  83. bool Less( const SharedObjectInfo_t& lhs, const SharedObjectInfo_t& rhs, void* ) const { return lhs.m_nID < rhs.m_nID; }
  84. bool Less( int lhs, const SharedObjectInfo_t& rhs, void* ) const { return lhs < rhs.m_nID; }
  85. bool Less( const SharedObjectInfo_t& lhs, int rhs, void* ) const { return lhs.m_nID < rhs; }
  86. };
  87. typedef CUtlSortVector< SharedObjectInfo_t, CCompareSharedObject > TVecFactories;
  88. static TVecFactories sm_vecFactories;
  89. static const SharedObjectInfo_t* FindSharedObjectInfo( int nTypeID );
  90. public:
  91. static TVecFactories & GetFactories() { return sm_vecFactories; }
  92. };
  93. typedef CUtlVectorFixedGrowable<CSharedObject *, 1> CSharedObjectVec;
  94. //----------------------------------------------------------------------------
  95. // Purpose: Templatized function to use as a factory method for
  96. // CSharedObject subclasses
  97. //----------------------------------------------------------------------------
  98. template<typename SharedObjectSubclass_t>
  99. CSharedObject *CreateSharedObjectSubclass()
  100. {
  101. return new SharedObjectSubclass_t();
  102. }
  103. //internal utility to expand the provided shared object class name into all the names needed for the node
  104. #define REG_SHARED_OBJECT_NAMES_INTERNAL( name ) #name, "BuildCacheSubscribed(" #name ")", "Create(" #name ")", "Update(" #name ")"
  105. #ifdef GC
  106. #define REG_SHARED_OBJECT_SUBCLASS( derivedClass, flags ) GCSDK::CSharedObject::RegisterFactory( derivedClass::k_nTypeID, GCSDK::CreateSharedObjectSubclass<derivedClass>, (flags), REG_SHARED_OBJECT_NAMES_INTERNAL( derivedClass ) )
  107. #else
  108. #define REG_SHARED_OBJECT_SUBCLASS( derivedClass ) GCSDK::CSharedObject::RegisterFactory( derivedClass::k_nTypeID, GCSDK::CreateSharedObjectSubclass<derivedClass>, 0, REG_SHARED_OBJECT_NAMES_INTERNAL( derivedClass ) )
  109. #endif
  110. } // namespace GCSDK
  111. #include "tier0/memdbgoff.h"
  112. #endif //SHAREDOBJECT_H