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.

123 lines
3.5 KiB

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