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.

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