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.

143 lines
4.3 KiB

  1. //====== Copyright � 1996-2006, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: fixed "color" attribute of lights to be of type Color, rather than Vector4
  4. // this should have been put in a *long* time ago, but I somehow missed creating the updater between 3 and 4
  5. // fortunately, since all updates happen on untyped elements, it's reasonably safe to do this out of order
  6. //
  7. //=============================================================================
  8. #include "dmserializers.h"
  9. #include "dmebaseimporter.h"
  10. #include "datamodel/idatamodel.h"
  11. #include "datamodel/dmelement.h"
  12. #include "datamodel/dmattributevar.h"
  13. #include "tier1/utlbuffer.h"
  14. #include "tier1/utlmap.h"
  15. #include <limits.h>
  16. //-----------------------------------------------------------------------------
  17. // Format converter
  18. //-----------------------------------------------------------------------------
  19. class CImportSFMV9 : public CSFMBaseImporter
  20. {
  21. typedef CSFMBaseImporter BaseClass;
  22. public:
  23. CImportSFMV9( const char *formatName, const char *nextFormatName );
  24. private:
  25. virtual bool DoFixup( CDmElement *pSourceRoot );
  26. void FixupElement( CDmElement *pElement );
  27. // Fixes up all elements
  28. void BuildList( CDmElement *pElement, CUtlRBTree< CDmElement *, int >& list );
  29. };
  30. //-----------------------------------------------------------------------------
  31. // Singleton instance
  32. //-----------------------------------------------------------------------------
  33. static CImportSFMV9 s_ImportSFMV9( "sfm_v9", "sfm_v10" );
  34. void InstallSFMV9Importer( IDataModel *pFactory )
  35. {
  36. pFactory->AddLegacyUpdater( &s_ImportSFMV9 );
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Constructor
  40. //-----------------------------------------------------------------------------
  41. CImportSFMV9::CImportSFMV9( const char *formatName, const char *nextFormatName ) :
  42. BaseClass( formatName, nextFormatName )
  43. {
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Fixes up all elements
  47. //-----------------------------------------------------------------------------
  48. void CImportSFMV9::FixupElement( CDmElement *pElement )
  49. {
  50. if ( !pElement )
  51. return;
  52. const char *pType = pElement->GetTypeString();
  53. if ( !V_stricmp( pType, "DmeLight" ) ||
  54. !V_stricmp( pType, "DmeDirectionalLight" ) ||
  55. !V_stricmp( pType, "DmeProjectedLight" ) ||
  56. !V_stricmp( pType, "DmePointLight" ) ||
  57. !V_stricmp( pType, "DmeSpotLight" ) ||
  58. !V_stricmp( pType, "DmeAmbientLight" ) )
  59. {
  60. const CDmAttribute *pOldAttr = pElement->GetAttribute( "color", AT_VECTOR4 );
  61. if ( !pOldAttr )
  62. return;
  63. Color color;
  64. { // scoping this section of code since vecColor is invalid after RemoveAttribute
  65. const Vector4D &vecColor = pOldAttr->GetValue< Vector4D >();
  66. for ( int i = 0; i < 4; ++i )
  67. {
  68. color[ i ] = ( int )clamp( vecColor[ i ], 0.0f, 255.0f );
  69. }
  70. pElement->RemoveAttribute( "color" );
  71. }
  72. CDmAttribute *pNewAttr = pElement->AddAttribute( "color", AT_COLOR );
  73. pNewAttr->SetValue( color );
  74. }
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Fixes up all elements
  78. //-----------------------------------------------------------------------------
  79. void CImportSFMV9::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 CImportSFMV9::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. }