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.

120 lines
3.4 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 CImportSFMV5 : public CSFMBaseImporter
  18. {
  19. typedef CSFMBaseImporter BaseClass;
  20. public:
  21. CImportSFMV5( 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 CImportSFMV5 s_ImportSFMV5( "sfm_v5", "sfm_v6" );
  32. void InstallSFMV5Importer( IDataModel *pFactory )
  33. {
  34. pFactory->AddLegacyUpdater( &s_ImportSFMV5 );
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Constructor
  38. //-----------------------------------------------------------------------------
  39. CImportSFMV5::CImportSFMV5( char const *formatName, char const *nextFormatName ) :
  40. BaseClass( formatName, nextFormatName )
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Fixes up all elements
  45. //-----------------------------------------------------------------------------
  46. void CImportSFMV5::FixupElement( CDmElement *pElement )
  47. {
  48. if ( !pElement )
  49. return;
  50. const char *pType = pElement->GetTypeString();
  51. if ( !V_stricmp( pType, "DmeSpotLight" ) )
  52. {
  53. pElement->SetType( "DmeProjectedLight" );
  54. }
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Fixes up all elements
  58. //-----------------------------------------------------------------------------
  59. void CImportSFMV5::BuildList( CDmElement *pElement, CUtlRBTree< CDmElement *, int >& list )
  60. {
  61. if ( !pElement )
  62. return;
  63. if ( list.Find( pElement ) != list.InvalidIndex() )
  64. return;
  65. list.Insert( pElement );
  66. // Descend to bottom of tree, then do fixup coming back up the tree
  67. for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
  68. {
  69. if ( pAttribute->GetType() == AT_ELEMENT )
  70. {
  71. CDmElement *pElementAt = pAttribute->GetValueElement<CDmElement>( );
  72. BuildList( pElementAt, list );
  73. continue;
  74. }
  75. if ( pAttribute->GetType() == AT_ELEMENT_ARRAY )
  76. {
  77. CDmrElementArray<> array( pAttribute );
  78. int nCount = array.Count();
  79. for ( int i = 0; i < nCount; ++i )
  80. {
  81. CDmElement *pChild = array[ i ];
  82. BuildList( pChild, list );
  83. }
  84. continue;
  85. }
  86. }
  87. }
  88. bool CImportSFMV5::DoFixup( CDmElement *pSourceRoot )
  89. {
  90. CUtlRBTree< CDmElement *, int > fixlist( 0, 0, DefLessFunc( CDmElement * ) );
  91. BuildList( pSourceRoot, fixlist );
  92. for ( int i = fixlist.FirstInorder(); i != fixlist.InvalidIndex() ; i = fixlist.NextInorder( i ) )
  93. {
  94. // Search and replace in the entire tree!
  95. FixupElement( fixlist[ i ] );
  96. }
  97. return true;
  98. }