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.

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