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.

139 lines
3.9 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 CImportSFMV8 : public CSFMBaseImporter
  18. {
  19. typedef CSFMBaseImporter BaseClass;
  20. public:
  21. CImportSFMV8( const char *formatName, const char *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 CImportSFMV8 s_ImportSFMV8( "sfm_v8", "sfm_v9" );
  32. void InstallSFMV8Importer( IDataModel *pFactory )
  33. {
  34. pFactory->AddLegacyUpdater( &s_ImportSFMV8 );
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Constructor
  38. //-----------------------------------------------------------------------------
  39. CImportSFMV8::CImportSFMV8( const char *formatName, const char *nextFormatName ) :
  40. BaseClass( formatName, nextFormatName )
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Fixes up all elements
  45. //-----------------------------------------------------------------------------
  46. void CImportSFMV8::FixupElement( CDmElement *pElement )
  47. {
  48. if ( !pElement )
  49. return;
  50. const char *pType = pElement->GetTypeString();
  51. if ( !V_stricmp( pType, "DmeAnimationSet" ) )
  52. {
  53. // Remove 'midpoint' from all controls, and
  54. // Add 'defaultBalance' and 'defaultMultilevel' to all non-transform controls
  55. CDmrElementArray<> srcControls( pElement, "controls" );
  56. if ( srcControls.IsValid() )
  57. {
  58. int nCount = srcControls.Count();
  59. for ( int i = 0; i < nCount; ++i )
  60. {
  61. CDmElement *pControl = srcControls[i];
  62. if ( pControl )
  63. {
  64. if ( !pControl->GetValue<bool>( "transform" ) )
  65. {
  66. pControl->InitValue( "defaultBalance", 0.5f );
  67. pControl->InitValue( "defaultMultilevel", 0.5f );
  68. pControl->RemoveAttribute( "midpoint" );
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Fixes up all elements
  77. //-----------------------------------------------------------------------------
  78. void CImportSFMV8::BuildList( CDmElement *pElement, CUtlRBTree< CDmElement *, int >& list )
  79. {
  80. if ( !pElement )
  81. return;
  82. if ( list.Find( pElement ) != list.InvalidIndex() )
  83. return;
  84. list.Insert( pElement );
  85. // Descend to bottom of tree, then do fixup coming back up the tree
  86. for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
  87. {
  88. if ( pAttribute->GetType() == AT_ELEMENT )
  89. {
  90. CDmElement *pElementAt = pAttribute->GetValueElement<CDmElement>( );
  91. BuildList( pElementAt, list );
  92. continue;
  93. }
  94. if ( pAttribute->GetType() == AT_ELEMENT_ARRAY )
  95. {
  96. CDmrElementArray<> array( pAttribute );
  97. int nCount = array.Count();
  98. for ( int i = 0; i < nCount; ++i )
  99. {
  100. CDmElement *pChild = array[ i ];
  101. BuildList( pChild, list );
  102. }
  103. continue;
  104. }
  105. }
  106. }
  107. bool CImportSFMV8::DoFixup( CDmElement *pSourceRoot )
  108. {
  109. CUtlRBTree< CDmElement *, int > fixlist( 0, 0, DefLessFunc( CDmElement * ) );
  110. BuildList( pSourceRoot, fixlist );
  111. for ( int i = fixlist.FirstInorder(); i != fixlist.InvalidIndex() ; i = fixlist.NextInorder( i ) )
  112. {
  113. // Search and replace in the entire tree!
  114. FixupElement( fixlist[ i ] );
  115. }
  116. return true;
  117. }