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.

228 lines
7.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 CImportSFMV3 : public CSFMBaseImporter
  19. {
  20. typedef CSFMBaseImporter BaseClass;
  21. public:
  22. CImportSFMV3( 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 CImportSFMV3 s_ImportSFMV3( "sfm_v3", "sfm_v4" );
  33. void InstallSFMV3Importer( IDataModel *pFactory )
  34. {
  35. pFactory->AddLegacyUpdater( &s_ImportSFMV3 );
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Constructor
  39. //-----------------------------------------------------------------------------
  40. CImportSFMV3::CImportSFMV3( char const *formatName, char const *nextFormatName ) :
  41. BaseClass( formatName, nextFormatName )
  42. {
  43. }
  44. struct LogToCurveInfoTypeMap_t
  45. {
  46. const char *pLogType;
  47. const char *pLogLayerType;
  48. const char *pCurveInfoType;
  49. };
  50. LogToCurveInfoTypeMap_t g_typeMap[] =
  51. {
  52. { "DmeIntLog", "DmeIntLogLayer", "DmeIntCurveInfo" },
  53. { "DmeFloatLog", "DmeFloatLogLayer", "DmeFloatCurveInfo" },
  54. { "DmeBoolLog", "DmeBoolLogLayer", "DmeBoolCurveInfo" },
  55. // string,
  56. // void,
  57. // objectid,
  58. { "DmeColorLog", "DmeColorLogLayer", "DmeColorCurveInfo" },
  59. { "DmeVector2Log", "DmeVector2LogLayer", "DmeVector2CurveInfo" },
  60. { "DmeVector3Log", "DmeVector3LogLayer", "DmeVector3CurveInfo" },
  61. { "DmeVector4Log", "DmeVector4LogLayer", "DmeVector4CurveInfo" },
  62. { "DmeQAngleLog", "DmeQAngleLogLayer", "DmeQAngleCurveInfo" },
  63. { "DmeQuaternionLog", "DmeQuaternionLogLayer","DmeQuaternionCurveInfo" },
  64. { "DmeVMatrixLog", "DmeVMatrixLogLayer", "DmeVMatrixCurveInfo" },
  65. };
  66. const char *GetCurveInfoTypeFromLogType( const char *pLogType )
  67. {
  68. int c = ARRAYSIZE( g_typeMap );
  69. for ( int i = 0; i < c; ++i )
  70. {
  71. if ( !Q_stricmp( pLogType, g_typeMap[ i ].pLogType ) )
  72. return g_typeMap[ i ].pCurveInfoType;
  73. }
  74. return NULL;
  75. }
  76. bool IsLogLayerType( const char *pLogLayerType )
  77. {
  78. int c = ARRAYSIZE( g_typeMap );
  79. for ( int i = 0; i < c; ++i )
  80. {
  81. if ( !Q_stricmp( pLogLayerType, g_typeMap[ i ].pLogLayerType ) )
  82. return true;
  83. }
  84. return false;
  85. }
  86. void MoveAttribute( CDmElement *pFromElement, const char *pFromAttrName, CDmElement *pToElement = NULL, const char *pToAttrName = NULL, DmAttributeType_t toType = AT_UNKNOWN )
  87. {
  88. if ( !pToAttrName )
  89. {
  90. pToAttrName = pFromAttrName;
  91. }
  92. if ( pToElement )
  93. {
  94. CDmAttribute *pFromAttr = pFromElement->GetAttribute( pFromAttrName );
  95. const void *pValue = pFromAttr->GetValueUntyped();
  96. DmAttributeType_t fromType = pFromAttr->GetType();
  97. if ( toType == AT_UNKNOWN )
  98. {
  99. toType = fromType;
  100. }
  101. CDmAttribute *pToAttr = pToElement->AddAttribute( pToAttrName, toType );
  102. if ( !pToAttr )
  103. {
  104. Warning( "*** Problem in converter encountered!\n" );
  105. Warning( "*** Unable to find or add attribute \"%s\" to element \"%s\"!\n", pToAttrName, pToElement->GetName() );
  106. }
  107. else if ( fromType != toType )
  108. {
  109. Warning( "*** Problem in file encountered!\n" );
  110. Warning( "*** Element \"%s\" has attribute \"%s\" with an unexpected type!\n", pFromElement->GetName(), pFromAttrName );
  111. }
  112. else
  113. {
  114. pToAttr->SetValue( toType, pValue );
  115. }
  116. }
  117. pFromElement->RemoveAttribute( pFromAttrName );
  118. }
  119. // Fixes up all elements
  120. //-----------------------------------------------------------------------------
  121. void CImportSFMV3::FixupElement( CDmElement *pElement )
  122. {
  123. if ( !pElement )
  124. return;
  125. const char *pType = pElement->GetTypeString();
  126. // log layer
  127. if ( IsLogLayerType( pType ) )
  128. {
  129. pElement->RemoveAttribute( "ownerlog" );
  130. return;
  131. }
  132. // log
  133. const char *pCurveInfoType = GetCurveInfoTypeFromLogType( pType );
  134. if ( !pCurveInfoType )
  135. return;
  136. CDmElement *pCurveInfo = NULL;
  137. CDmAttribute *pUseCurveTypeAttr = pElement->GetAttribute( "usecurvetypes" );
  138. if ( pUseCurveTypeAttr && pUseCurveTypeAttr->GetValue<bool>() )
  139. {
  140. DmElementHandle_t hElement = g_pDataModel->CreateElement( "curve info", pCurveInfoType, pElement->GetFileId() );
  141. pCurveInfo = g_pDataModel->GetElement( hElement );
  142. }
  143. pElement->RemoveAttribute( "usecurvetypes" );
  144. MoveAttribute( pElement, "defaultcurvetype", pCurveInfo, "defaultCurveType", AT_INT );
  145. MoveAttribute( pElement, "defaultedgezerovalue",pCurveInfo, "defaultEdgeZeroValue" );
  146. MoveAttribute( pElement, "useedgeinfo", pCurveInfo, "useEdgeInfo", AT_BOOL );
  147. MoveAttribute( pElement, "rightedgetime", pCurveInfo, "rightEdgeTime", AT_INT );
  148. MoveAttribute( pElement, "left_edge_active", pCurveInfo, "leftEdgeActive", AT_BOOL );
  149. MoveAttribute( pElement, "right_edge_active", pCurveInfo, "rightEdgeActive", AT_BOOL );
  150. MoveAttribute( pElement, "left_edge_curvetype", pCurveInfo, "leftEdgeCurveType", AT_INT );
  151. MoveAttribute( pElement, "right_edge_curvetype",pCurveInfo, "rightEdgeCurveType", AT_INT );
  152. MoveAttribute( pElement, "left_edge_value", pCurveInfo, "leftEdgeValue" );
  153. MoveAttribute( pElement, "right_edge_value", pCurveInfo, "rightEdgeValue" );
  154. }
  155. // Fixes up all elements
  156. //-----------------------------------------------------------------------------
  157. void CImportSFMV3::BuildList( CDmElement *pElement, CUtlRBTree< CDmElement *, int >& list )
  158. {
  159. if ( !pElement )
  160. return;
  161. if ( list.Find( pElement ) != list.InvalidIndex() )
  162. return;
  163. list.Insert( pElement );
  164. // Descend to bottom of tree, then do fixup coming back up the tree
  165. for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
  166. {
  167. if ( pAttribute->GetType() == AT_ELEMENT )
  168. {
  169. CDmElement *pElement = pAttribute->GetValueElement<CDmElement>( );
  170. BuildList( pElement, list );
  171. continue;
  172. }
  173. if ( pAttribute->GetType() == AT_ELEMENT_ARRAY )
  174. {
  175. CDmrElementArray<> array( pAttribute );
  176. int nCount = array.Count();
  177. for ( int i = 0; i < nCount; ++i )
  178. {
  179. CDmElement *pChild = array[ i ];
  180. BuildList( pChild, list );
  181. }
  182. continue;
  183. }
  184. }
  185. }
  186. bool CImportSFMV3::DoFixup( CDmElement *pSourceRoot )
  187. {
  188. CUtlRBTree< CDmElement *, int > fixlist( 0, 0, DefLessFunc( CDmElement * ) );
  189. BuildList( pSourceRoot, fixlist );
  190. for ( int i = fixlist.FirstInorder(); i != fixlist.InvalidIndex() ; i = fixlist.NextInorder( i ) )
  191. {
  192. // Search and replace in the entire tree!
  193. FixupElement( fixlist[ i ] );
  194. }
  195. return true;
  196. }