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.

124 lines
3.6 KiB

  1. //====== Copyright � 1996-2006, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "dmserializers.h"
  7. #include "dmebaseimporter.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 "tier1/utlmap.h"
  14. #include <limits.h>
  15. //-----------------------------------------------------------------------------
  16. // Format converter
  17. //-----------------------------------------------------------------------------
  18. class CImportSFMV4 : public CSFMBaseImporter
  19. {
  20. typedef CSFMBaseImporter BaseClass;
  21. public:
  22. CImportSFMV4( char const *formatName, char const *nextFormatName );
  23. private:
  24. virtual bool DoFixup( CDmElement *pSourceRoot );
  25. void FixupElement( CDmElement *pElement );
  26. // Fixes up all elements
  27. void BuildList( CDmElement *pElement, CUtlRBTree< CDmElement *, int >& list );
  28. };
  29. //-----------------------------------------------------------------------------
  30. // Singleton instance
  31. //-----------------------------------------------------------------------------
  32. static CImportSFMV4 s_ImportSFMV4( "sfm_v4", "sfm_v5" );
  33. void InstallSFMV4Importer( IDataModel *pFactory )
  34. {
  35. pFactory->AddLegacyUpdater( &s_ImportSFMV4 );
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Constructor
  39. //-----------------------------------------------------------------------------
  40. CImportSFMV4::CImportSFMV4( char const *formatName, char const *nextFormatName ) :
  41. BaseClass( formatName, nextFormatName )
  42. {
  43. }
  44. // Fixes up all elements
  45. //-----------------------------------------------------------------------------
  46. void CImportSFMV4::FixupElement( CDmElement *pElement )
  47. {
  48. if ( !pElement )
  49. return;
  50. const char *pType = pElement->GetTypeString();
  51. if ( !V_stricmp( pType, "DmeCamera" ) )
  52. {
  53. CDmAttribute *pOldToneMapScaleAttr = pElement->GetAttribute( "toneMapScale" );
  54. float fNewBloomScale = pOldToneMapScaleAttr->GetValue<float>( );
  55. Assert( !pElement->HasAttribute("bloomScale") );
  56. CDmAttribute *pNewBloomScaleAttr = pElement->AddAttribute( "bloomScale", AT_FLOAT );
  57. pNewBloomScaleAttr->SetValue( fNewBloomScale );
  58. pOldToneMapScaleAttr->SetValue( 1.0f );
  59. }
  60. }
  61. // Fixes up all elements
  62. //-----------------------------------------------------------------------------
  63. void CImportSFMV4::BuildList( CDmElement *pElement, CUtlRBTree< CDmElement *, int >& list )
  64. {
  65. if ( !pElement )
  66. return;
  67. if ( list.Find( pElement ) != list.InvalidIndex() )
  68. return;
  69. list.Insert( pElement );
  70. // Descend to bottom of tree, then do fixup coming back up the tree
  71. for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
  72. {
  73. if ( pAttribute->GetType() == AT_ELEMENT )
  74. {
  75. CDmElement *pElement = pAttribute->GetValueElement<CDmElement>( );
  76. BuildList( pElement, list );
  77. continue;
  78. }
  79. if ( pAttribute->GetType() == AT_ELEMENT_ARRAY )
  80. {
  81. CDmrElementArray<> array( pAttribute );
  82. int nCount = array.Count();
  83. for ( int i = 0; i < nCount; ++i )
  84. {
  85. CDmElement *pChild = array[ i ];
  86. BuildList( pChild, list );
  87. }
  88. continue;
  89. }
  90. }
  91. }
  92. bool CImportSFMV4::DoFixup( CDmElement *pSourceRoot )
  93. {
  94. CUtlRBTree< CDmElement *, int > fixlist( 0, 0, DefLessFunc( CDmElement * ) );
  95. BuildList( pSourceRoot, fixlist );
  96. for ( int i = fixlist.FirstInorder(); i != fixlist.InvalidIndex() ; i = fixlist.NextInorder( i ) )
  97. {
  98. // Search and replace in the entire tree!
  99. FixupElement( fixlist[ i ] );
  100. }
  101. return true;
  102. }