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.

185 lines
7.3 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "importkeyvaluebase.h"
  7. #include "dmserializers.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 "datamodel/dmattribute.h"
  14. //-----------------------------------------------------------------------------
  15. // Serialization class for Key Values
  16. //-----------------------------------------------------------------------------
  17. class CImportActBusy : public CImportKeyValueBase
  18. {
  19. public:
  20. virtual const char *GetName() const { return "actbusy"; }
  21. virtual const char *GetDescription() const { return "ActBusy Script File"; }
  22. virtual int GetCurrentVersion() const { return 0; } // doesn't store a version
  23. virtual const char *GetImportedFormat() const { return "actbusy"; }
  24. virtual int GetImportedVersion() const { return 1; }
  25. bool Serialize( CUtlBuffer &outBuf, CDmElement *pRoot );
  26. CDmElement* UnserializeFromKeyValues( KeyValues *pKeyValues );
  27. private:
  28. // Reads a single element
  29. bool UnserializeActBusyKey( CDmAttribute *pChildren, KeyValues *pKeyValues );
  30. // Writes out the actbusy header
  31. void SerializeHeader( CUtlBuffer &buf );
  32. };
  33. //-----------------------------------------------------------------------------
  34. // Singleton instance
  35. //-----------------------------------------------------------------------------
  36. static CImportActBusy s_ImportActBusy;
  37. void InstallActBusyImporter( IDataModel *pFactory )
  38. {
  39. pFactory->AddSerializer( &s_ImportActBusy );
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Writes out the actbusy header
  43. //-----------------------------------------------------------------------------
  44. void CImportActBusy::SerializeHeader( CUtlBuffer &buf )
  45. {
  46. buf.Printf( "// \"act busy name\"\t\tThis is the name that a mapmaker must specify in the hint node.\n" );
  47. buf.Printf( "// {\n" );
  48. buf.Printf( "// \t\"busy_anim\"\t\t\t\"Activity Name\".\n" );
  49. buf.Printf( "// \t\"entry_anim\"\t\t\"Activity Name\"\n" );
  50. buf.Printf( "// \t\"exit_anim\"\t\t\t\"Activity Name\"\n" );
  51. buf.Printf( "// \t\"busy_sequence\"\t\t\"Sequence Name\". If specified, this is used over the activity name. Specify it in the hint node.\n" );
  52. buf.Printf( "// \t\"entry_sequence\"\t\"Sequence Name\". If specified, this is used over the entry anim.\n" );
  53. buf.Printf( "// \t\"exit_sequence\"\t\t\"Sequence Name\". If specified, this is used over the exit anim.\n" );
  54. buf.Printf( "// \t\"min_time\"\t\t\t\"Minimum time to spend in this busy anim\"\n" );
  55. buf.Printf( "// \t\"max_time\"\t\t\t\"Maximum time to spend in this busy anim\" 0 = only stop when interrupted by external event\n" );
  56. buf.Printf( "// \t\"interrupts\"\t\tOne of:\n" );
  57. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_NONE\"\t\tbreak out only when time runs out. No external influence will break me out.\n" );
  58. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_DANGER\"\t\tbreak out of this anim only if threatened\n" );
  59. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_PLAYER\"\t\tbreak out of this anim if I can see the player, or I'm threatened\n" );
  60. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_AMBUSH\"\t\tsomeone please define this - I have no idea what it does\n" );
  61. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_COMBAT\"\t\tbreak out of this anim if combat occurs in my line of sight (bullet hits, grenades, etc), -OR- the max time is reached\n" );
  62. buf.Printf( "// }\n" );
  63. buf.Printf( "//\n" );
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Writes out a new actbusy file
  67. //-----------------------------------------------------------------------------
  68. bool CImportActBusy::Serialize( CUtlBuffer &buf, CDmElement *pRoot )
  69. {
  70. SerializeHeader( buf );
  71. buf.Printf( "\"ActBusy.txt\"\n" );
  72. buf.Printf( "{\n" );
  73. CDmAttribute *pChildren = pRoot->GetAttribute( "children" );
  74. if ( !pChildren || pChildren->GetType() != AT_ELEMENT_ARRAY )
  75. return NULL;
  76. CDmrElementArray<> children( pChildren );
  77. int nCount = children.Count();
  78. buf.PushTab();
  79. for ( int i = 0; i < nCount; ++i )
  80. {
  81. CDmElement *pChild = children[i];
  82. buf.Printf( "\"%s\"\n", pChild->GetName() );
  83. buf.Printf( "{\n" );
  84. buf.PushTab();
  85. PrintStringAttribute( pChild, buf, "busy_anim", true );
  86. PrintStringAttribute( pChild, buf, "entry_anim", true );
  87. PrintStringAttribute( pChild, buf, "exit_anim", true );
  88. PrintStringAttribute( pChild, buf, "busy_sequence", true );
  89. PrintStringAttribute( pChild, buf, "entry_sequence", true );
  90. PrintStringAttribute( pChild, buf, "exit_sequence", true );
  91. PrintFloatAttribute( pChild, buf, "min_time" );
  92. PrintFloatAttribute( pChild, buf, "max_time" );
  93. PrintStringAttribute( pChild, buf, "interrupts" );
  94. buf.PopTab();
  95. buf.Printf( "}\n" );
  96. }
  97. buf.PopTab();
  98. buf.Printf( "}\n" );
  99. return true;
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Reads a single element
  103. //-----------------------------------------------------------------------------
  104. bool CImportActBusy::UnserializeActBusyKey( CDmAttribute *pChildren, KeyValues *pKeyValues )
  105. {
  106. CDmElement *pActBusy = CreateDmElement( "DmElement", pKeyValues->GetName(), NULL );
  107. if ( !pActBusy )
  108. return false;
  109. // Each act busy needs to have an editortype associated with it so it displays nicely in editors
  110. pActBusy->SetValue( "editorType", "actBusy" );
  111. float flZero = 0.0f;
  112. AddStringAttribute( pActBusy, pKeyValues, "busy_anim", "" );
  113. AddStringAttribute( pActBusy, pKeyValues, "entry_anim", "" );
  114. AddStringAttribute( pActBusy, pKeyValues, "exit_anim", "" );
  115. AddStringAttribute( pActBusy, pKeyValues, "busy_sequence", "" );
  116. AddStringAttribute( pActBusy, pKeyValues, "entry_sequence", "" );
  117. AddStringAttribute( pActBusy, pKeyValues, "exit_sequence", "" );
  118. AddFloatAttribute( pActBusy, pKeyValues, "min_time", &flZero );
  119. AddFloatAttribute( pActBusy, pKeyValues, "max_time", &flZero );
  120. AddStringAttribute( pActBusy, pKeyValues, "interrupts", "BA_INT_NONE" );
  121. CDmrElementArray<> children( pChildren );
  122. children.AddToTail( pActBusy );
  123. return true;
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Main entry point for the unserialization
  127. //-----------------------------------------------------------------------------
  128. CDmElement* CImportActBusy::UnserializeFromKeyValues( KeyValues *pKeyValues )
  129. {
  130. // Create the main element
  131. CDmElement *pElement = CreateDmElement( "DmElement", "ActBusyList", NULL );
  132. if ( !pElement )
  133. return NULL;
  134. // Each act busy list needs to have an editortype associated with it so it displays nicely in editors
  135. pElement->SetValue( "editorType", "actBusyList" );
  136. // All actbusy keys are elements of a single element array attribute 'children'
  137. CDmAttribute *pChildren = pElement->AddAttribute( "children", AT_ELEMENT_ARRAY );
  138. if ( !pChildren )
  139. return NULL;
  140. // Under the root are all the actbusy keys
  141. for ( KeyValues *pActBusyKey = pKeyValues->GetFirstTrueSubKey(); pActBusyKey != NULL; pActBusyKey = pActBusyKey->GetNextTrueSubKey() )
  142. {
  143. if ( !UnserializeActBusyKey( pChildren, pActBusyKey ) )
  144. {
  145. Warning( "Error importing actbusy element %s\n", pActBusyKey->GetName() );
  146. return NULL;
  147. }
  148. }
  149. // Resolve all element references recursively
  150. RecursivelyResolveElement( pElement );
  151. return pElement;
  152. }