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.

142 lines
4.3 KiB

  1. //========= Copyright 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 "tier1/utlbuffer.h"
  13. #include "tier1/utlmap.h"
  14. #include <limits.h>
  15. //-----------------------------------------------------------------------------
  16. // Format converter
  17. //-----------------------------------------------------------------------------
  18. class CImportSFMV9 : public CSFMBaseImporter
  19. {
  20. typedef CSFMBaseImporter BaseClass;
  21. public:
  22. CImportSFMV9( 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 CImportSFMV9 s_ImportSFMV9( "sfm_v9", "sfm_v10" );
  33. void InstallSFMV9Importer( IDataModel *pFactory )
  34. {
  35. pFactory->AddLegacyUpdater( &s_ImportSFMV9 );
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Constructor
  39. //-----------------------------------------------------------------------------
  40. CImportSFMV9::CImportSFMV9( const char *formatName, const char *nextFormatName ) :
  41. BaseClass( formatName, nextFormatName )
  42. {
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Fixes up all elements
  46. //-----------------------------------------------------------------------------
  47. void CImportSFMV9::FixupElement( CDmElement *pElement )
  48. {
  49. if ( !pElement )
  50. return;
  51. const char *pType = pElement->GetTypeString();
  52. if ( !V_stricmp( pType, "DmeLight" ) ||
  53. !V_stricmp( pType, "DmeDirectionalLight" ) ||
  54. !V_stricmp( pType, "DmeProjectedLight" ) ||
  55. !V_stricmp( pType, "DmePointLight" ) ||
  56. !V_stricmp( pType, "DmeSpotLight" ) ||
  57. !V_stricmp( pType, "DmeAmbientLight" ) )
  58. {
  59. const CDmAttribute *pOldAttr = pElement->GetAttribute( "color", AT_VECTOR4 );
  60. if ( !pOldAttr )
  61. return;
  62. Color color;
  63. { // scoping this section of code since vecColor is invalid after RemoveAttribute
  64. const Vector4D &vecColor = pOldAttr->GetValue< Vector4D >();
  65. for ( int i = 0; i < 4; ++i )
  66. {
  67. color[ i ] = ( int )clamp( vecColor[ i ], 0.0f, 255.0f );
  68. }
  69. pElement->RemoveAttribute( "color" );
  70. }
  71. CDmAttribute *pNewAttr = pElement->AddAttribute( "color", AT_COLOR );
  72. pNewAttr->SetValue( color );
  73. }
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Fixes up all elements
  77. //-----------------------------------------------------------------------------
  78. void CImportSFMV9::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 CImportSFMV9::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. }