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.

153 lines
4.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "stdafx.h"
  7. #include "scriplib.h"
  8. #include "choreoscene.h"
  9. #include "iscenetokenprocessor.h"
  10. #include "filesystem_tools.h"
  11. //-----------------------------------------------------------------------------
  12. // Purpose: Helper to scene module for parsing the .vcd file
  13. //-----------------------------------------------------------------------------
  14. class CSceneTokenProcessor : public ISceneTokenProcessor
  15. {
  16. public:
  17. const char *CurrentToken( void );
  18. bool GetToken( bool crossline );
  19. bool TokenAvailable( void );
  20. void Error( const char *fmt, ... );
  21. };
  22. //-----------------------------------------------------------------------------
  23. // Purpose:
  24. // Output : const
  25. //-----------------------------------------------------------------------------
  26. const char *CSceneTokenProcessor::CurrentToken( void )
  27. {
  28. return token;
  29. }
  30. //-----------------------------------------------------------------------------
  31. // Purpose:
  32. // Input : crossline -
  33. // Output : Returns true on success, false on failure.
  34. //-----------------------------------------------------------------------------
  35. bool CSceneTokenProcessor::GetToken( bool crossline )
  36. {
  37. return ::GetToken( crossline ) ? true : false;
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Purpose:
  41. // Output : Returns true on success, false on failure.
  42. //-----------------------------------------------------------------------------
  43. bool CSceneTokenProcessor::TokenAvailable( void )
  44. {
  45. return ::TokenAvailable() ? true : false;
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose:
  49. // Input : *fmt -
  50. // ... -
  51. //-----------------------------------------------------------------------------
  52. void CSceneTokenProcessor::Error( const char *fmt, ... )
  53. {
  54. char string[ 2048 ];
  55. va_list argptr;
  56. va_start( argptr, fmt );
  57. vsprintf( string, fmt, argptr );
  58. va_end( argptr );
  59. Warning( "%s", string );
  60. }
  61. static CSceneTokenProcessor g_TokenProcessor;
  62. //-----------------------------------------------------------------------------
  63. // Purpose: Normally implemented in cmdlib.cpp but we don't want that in Hammer.
  64. //-----------------------------------------------------------------------------
  65. char *ExpandPath (char *path)
  66. {
  67. static char fullpath[ 512 ];
  68. g_pFullFileSystem->RelativePathToFullPath( path, "GAME", fullpath, sizeof( fullpath ) );
  69. return fullpath;
  70. }
  71. //-----------------------------------------------------------------------------
  72. // This is here because scriplib.cpp is included in this project but cmdlib.cpp
  73. // is not, but scriplib.cpp uses some stuff from cmdlib.cpp, same with
  74. // LoadFile and ExpandPath above. The only thing that currently uses this
  75. // is $include in scriptlib, if this function returns 0, $include will
  76. // behave the way it did before this change
  77. //-----------------------------------------------------------------------------
  78. int CmdLib_ExpandWithBasePaths( CUtlVector< CUtlString > &expandedPathList, const char *pszPath )
  79. {
  80. return 0;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: Normally implemented in cmdlib.cpp but we don't want that in Hammer.
  84. //-----------------------------------------------------------------------------
  85. int LoadFile( const char *filename, void **bufferptr )
  86. {
  87. FileHandle_t f = g_pFullFileSystem->Open( filename, "rb" );
  88. if ( FILESYSTEM_INVALID_HANDLE != f )
  89. {
  90. int length = g_pFullFileSystem->Size( f );
  91. void *buffer = malloc (length+1);
  92. ((char *)buffer)[length] = 0;
  93. g_pFullFileSystem->Read( buffer, length, f );
  94. g_pFullFileSystem->Close (f);
  95. *bufferptr = buffer;
  96. return length;
  97. }
  98. else
  99. {
  100. *bufferptr = NULL;
  101. return 0;
  102. }
  103. }
  104. CChoreoScene* HammerLoadScene( const char *pFilename )
  105. {
  106. if ( g_pFullFileSystem->FileExists( pFilename ) )
  107. {
  108. LoadScriptFile( (char*)pFilename );
  109. CChoreoScene *scene = ChoreoLoadScene( pFilename, NULL, &g_TokenProcessor, Msg );
  110. return scene;
  111. }
  112. return NULL;
  113. }
  114. bool GetFirstSoundInScene( const char *pSceneFilename, char *pSoundName, int soundNameLen )
  115. {
  116. CChoreoScene *pScene = HammerLoadScene( pSceneFilename );
  117. if ( !pScene )
  118. return false;
  119. for ( int i = 0; i < pScene->GetNumEvents(); i++ )
  120. {
  121. CChoreoEvent *e = pScene->GetEvent( i );
  122. if ( !e || e->GetType() != CChoreoEvent::SPEAK )
  123. continue;
  124. const char *pParameters = e->GetParameters();
  125. V_strncpy( pSoundName, pParameters, soundNameLen );
  126. delete pScene;
  127. return true;
  128. }
  129. delete pScene;
  130. return false;
  131. }