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.

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