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.

144 lines
4.1 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 CImportSFMV7 : public CSFMBaseImporter
  18. {
  19. typedef CSFMBaseImporter BaseClass;
  20. public:
  21. CImportSFMV7( 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 CImportSFMV7 s_ImportSFMV7( "sfm_v7", "sfm_v8" );
  32. void InstallSFMV7Importer( IDataModel *pFactory )
  33. {
  34. pFactory->AddLegacyUpdater( &s_ImportSFMV7 );
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Constructor
  38. //-----------------------------------------------------------------------------
  39. CImportSFMV7::CImportSFMV7( char const *formatName, char const *nextFormatName ) :
  40. BaseClass( formatName, nextFormatName )
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Fixes up all elements
  45. //-----------------------------------------------------------------------------
  46. void CImportSFMV7::FixupElement( CDmElement *pElement )
  47. {
  48. if ( !pElement )
  49. return;
  50. const char *pType = pElement->GetTypeString();
  51. if ( !V_stricmp( pType, "DmeAnimationSet" ) )
  52. {
  53. // Add a level of indirection in animation sets
  54. // Modify the type of all controls from DmElement to DmeAnimationSetControl
  55. CDmrElementArray<> srcPresets( pElement, "presets" );
  56. if ( srcPresets.IsValid() )
  57. {
  58. CDmrElementArray<> presetGroupArray( pElement, "presetGroups", true );
  59. CDmElement *pPresetGroup = CreateElement< CDmElement >( "custom", pElement->GetFileId() );
  60. pPresetGroup->SetType( "DmePresetGroup" );
  61. CDmrElementArray<> presets( pPresetGroup, "presets", true );
  62. presetGroupArray.AddToTail( pPresetGroup );
  63. int nCount = srcPresets.Count();
  64. for ( int i = 0; i < nCount; ++i )
  65. {
  66. CDmElement *pPreset = srcPresets[i];
  67. if ( pPreset )
  68. {
  69. pPreset->SetType( "DmePreset" );
  70. presets.AddToTail( pPreset );
  71. }
  72. }
  73. srcPresets.RemoveAll();
  74. }
  75. pElement->RemoveAttribute( "presets" );
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Fixes up all elements
  80. //-----------------------------------------------------------------------------
  81. void CImportSFMV7::BuildList( CDmElement *pElement, CUtlRBTree< CDmElement *, int >& list )
  82. {
  83. if ( !pElement )
  84. return;
  85. if ( list.Find( pElement ) != list.InvalidIndex() )
  86. return;
  87. list.Insert( pElement );
  88. // Descend to bottom of tree, then do fixup coming back up the tree
  89. for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
  90. {
  91. if ( pAttribute->GetType() == AT_ELEMENT )
  92. {
  93. CDmElement *pElementAt = pAttribute->GetValueElement<CDmElement>( );
  94. BuildList( pElementAt, list );
  95. continue;
  96. }
  97. if ( pAttribute->GetType() == AT_ELEMENT_ARRAY )
  98. {
  99. CDmrElementArray<> array( pAttribute );
  100. int nCount = array.Count();
  101. for ( int i = 0; i < nCount; ++i )
  102. {
  103. CDmElement *pChild = array[ i ];
  104. BuildList( pChild, list );
  105. }
  106. continue;
  107. }
  108. }
  109. }
  110. bool CImportSFMV7::DoFixup( CDmElement *pSourceRoot )
  111. {
  112. CUtlRBTree< CDmElement *, int > fixlist( 0, 0, DefLessFunc( CDmElement * ) );
  113. BuildList( pSourceRoot, fixlist );
  114. for ( int i = fixlist.FirstInorder(); i != fixlist.InvalidIndex() ; i = fixlist.NextInorder( i ) )
  115. {
  116. // Search and replace in the entire tree!
  117. FixupElement( fixlist[ i ] );
  118. }
  119. return true;
  120. }