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.

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