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.

140 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 CImportSFMV6 : public CSFMBaseImporter
  18. {
  19. typedef CSFMBaseImporter BaseClass;
  20. public:
  21. CImportSFMV6( char const *formatName, char const *nextFormatName );
  22. private:
  23. virtual bool DoFixup( CDmElement *pSourceRoot );
  24. Quaternion DirectionToOrientation( const Vector &dir );
  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 CImportSFMV6 s_ImportSFMV6( "sfm_v6", "sfm_v7" );
  33. void InstallSFMV6Importer( IDataModel *pFactory )
  34. {
  35. pFactory->AddLegacyUpdater( &s_ImportSFMV6 );
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Constructor
  39. //-----------------------------------------------------------------------------
  40. CImportSFMV6::CImportSFMV6( char const *formatName, char const *nextFormatName ) :
  41. BaseClass( formatName, nextFormatName )
  42. {
  43. }
  44. Quaternion CImportSFMV6::DirectionToOrientation( const Vector &dir )
  45. {
  46. Vector up( 0, 0, 1 );
  47. Vector right = CrossProduct( dir, up );
  48. if ( right.IsLengthLessThan( 0.001f ) )
  49. {
  50. up.Init( 1, 0, 0 );
  51. right = CrossProduct( dir, up );
  52. }
  53. right.NormalizeInPlace();
  54. up = CrossProduct( right, dir );
  55. Quaternion q;
  56. BasisToQuaternion( dir, right, up, q );
  57. return q;
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Fixes up all elements
  61. //-----------------------------------------------------------------------------
  62. void CImportSFMV6::FixupElement( CDmElement *pElement )
  63. {
  64. if ( !pElement )
  65. return;
  66. const char *pType = pElement->GetTypeString();
  67. if ( !V_stricmp( pType, "DmeProjectedLight" ) )
  68. {
  69. Vector vDir = pElement->GetValue<Vector>( "direction" );
  70. pElement->RemoveAttribute( "direction" );
  71. Quaternion q = DirectionToOrientation( vDir );
  72. pElement->SetValue<Quaternion>( "orientation", q );
  73. }
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Fixes up all elements
  77. //-----------------------------------------------------------------------------
  78. void CImportSFMV6::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 CImportSFMV6::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. }