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.

93 lines
3.3 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "importkeyvaluebase.h"
  7. #include "dmserializers.h"
  8. #include "datamodel/idatamodel.h"
  9. #include "datamodel/dmelement.h"
  10. #include "datamodel/dmattributevar.h"
  11. #include "tier1/KeyValues.h"
  12. #include "tier1/UtlBuffer.h"
  13. #include "datamodel/dmattribute.h"
  14. //-----------------------------------------------------------------------------
  15. // Serialization class for Key Values
  16. //-----------------------------------------------------------------------------
  17. class CImportCommentary : public CImportKeyValueBase
  18. {
  19. public:
  20. virtual const char *GetName() const { return "commentary"; }
  21. virtual const char *GetDescription() const { return "Commentary File"; }
  22. virtual int GetCurrentVersion() const { return 0; } // doesn't store a version
  23. virtual const char *GetImportedFormat() const { return "commentary"; }
  24. virtual int GetImportedVersion() const { return 1; }
  25. virtual bool Serialize( CUtlBuffer &outBuf, CDmElement *pRoot );
  26. virtual bool Unserialize( CUtlBuffer &buf, const char *pEncodingName, int nEncodingVersion,
  27. const char *pSourceFormatName, int nSourceFormatVersion,
  28. DmFileId_t fileid, DmConflictResolution_t idConflictResolution, CDmElement **ppRoot );
  29. protected:
  30. // Main entry point for derived classes to implement unserialization
  31. virtual CDmElement* UnserializeFromKeyValues( KeyValues *pKeyValues ) { Assert( 0 ); return NULL; }
  32. };
  33. //-----------------------------------------------------------------------------
  34. // Singleton instance
  35. //-----------------------------------------------------------------------------
  36. static CImportCommentary s_ImportCommentary;
  37. void InstallCommentaryImporter( IDataModel *pFactory )
  38. {
  39. pFactory->AddSerializer( &s_ImportCommentary );
  40. }
  41. bool CImportCommentary::Serialize( CUtlBuffer &buf, CDmElement *pRoot )
  42. {
  43. IDmSerializer *pKVSerializer = g_pDataModel->FindSerializer( "keyvalues" );
  44. if ( !pKVSerializer )
  45. return false;
  46. return pKVSerializer->Serialize( buf, pRoot );
  47. }
  48. //-----------------------------------------------------------------------------
  49. // Handles creation of the right element for a keyvalue
  50. //-----------------------------------------------------------------------------
  51. class CElementForKeyValueCallback : public IElementForKeyValueCallback
  52. {
  53. public:
  54. const char *GetElementForKeyValue( const char *pszKeyName, int iNestingLevel )
  55. {
  56. if ( iNestingLevel == 1 && !Q_strncmp(pszKeyName, "entity", 6) )
  57. return "DmeCommentaryNodeEntity";
  58. return NULL;
  59. }
  60. };
  61. bool CImportCommentary::Unserialize( CUtlBuffer &buf, const char *pEncodingName, int nEncodingVersion,
  62. const char *pSourceFormatName, int nSourceFormatVersion,
  63. DmFileId_t fileid, DmConflictResolution_t idConflictResolution, CDmElement **ppRoot )
  64. {
  65. *ppRoot = NULL;
  66. IDmSerializer *pKVSerializer = g_pDataModel->FindSerializer( "keyvalues" );
  67. if ( !pKVSerializer )
  68. return false;
  69. CElementForKeyValueCallback KeyValuesCallback;
  70. g_pDataModel->SetKeyValuesElementCallback( &KeyValuesCallback );
  71. bool bSuccess = pKVSerializer->Unserialize( buf, "keyvalues", nEncodingVersion, pSourceFormatName, nSourceFormatVersion, fileid, idConflictResolution, ppRoot );
  72. g_pDataModel->SetKeyValuesElementCallback( NULL );
  73. return bSuccess;
  74. }