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.

134 lines
3.2 KiB

  1. //===== Copyright � Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Defines scripting system.
  4. //
  5. //===========================================================================//
  6. #include "gameuiscriptsystem.h"
  7. #include "tier1/utlbuffer.h"
  8. #include "tier1/fmtstr.h"
  9. #include "filesystem.h"
  10. #include "gameuisystemmgr.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include <tier0/memdbgon.h>
  13. IScriptVM *GameUIScriptSystemCreate( )
  14. {
  15. IScriptVM *pScriptVM = NULL;
  16. ScriptLanguage_t scriptLanguage = SL_LUA;
  17. if( scriptLanguage != SL_NONE )
  18. {
  19. pScriptVM = g_pScriptManager->CreateVM( scriptLanguage );
  20. if( pScriptVM )
  21. {
  22. DevMsg("VSCRIPT: Started VScript virtual machine using script language '%s'\n", pScriptVM->GetLanguageName() );
  23. char SZFullPath[ MAX_PATH ];
  24. g_pFullFileSystem->RelativePathToFullPath( "scripts/vguiedit/modules", "GAME", SZFullPath, sizeof( SZFullPath ) );
  25. pScriptVM->AddSearchPath( SZFullPath );
  26. }
  27. }
  28. return pScriptVM;
  29. }
  30. HSCRIPT GameUIScriptSystemCompile( IScriptVM *pScriptVM, const char *pszScriptName, bool bWarnMissing )
  31. {
  32. if ( !pScriptVM )
  33. {
  34. return NULL;
  35. }
  36. static const char *pszExtensions[] =
  37. {
  38. "", // SL_NONE
  39. ".gm", // SL_GAMEMONKEY
  40. ".nut", // SL_SQUIRREL
  41. ".lua", // SL_LUA
  42. ".py", // SL_PYTHON
  43. };
  44. const char *pszVMExtension = pszExtensions[ pScriptVM->GetLanguage() ];
  45. const char *pszIncomingExtension = V_strrchr( pszScriptName , '.' );
  46. if ( pszIncomingExtension && V_stricmp( pszIncomingExtension, pszVMExtension ) != 0 )
  47. {
  48. Msg( "Script file type does not match VM type\n" );
  49. return NULL;
  50. }
  51. CFmtStr scriptPath;
  52. if ( pszIncomingExtension )
  53. {
  54. scriptPath.sprintf( "scripts/%s", pszScriptName );
  55. }
  56. else
  57. {
  58. scriptPath.sprintf( "scripts/%s%s", pszScriptName, pszVMExtension );
  59. }
  60. const char *pBase;
  61. CUtlBuffer bufferScript;
  62. if ( pScriptVM->GetLanguage() == SL_PYTHON )
  63. {
  64. // python auto-loads raw or precompiled modules - don't load data here
  65. pBase = NULL;
  66. }
  67. else
  68. {
  69. bool bResult = g_pFullFileSystem->ReadFile( scriptPath, "GAME", bufferScript );
  70. if( !bResult )
  71. {
  72. Warning( "Script not found (%s) \n", scriptPath.operator const char *() );
  73. }
  74. pBase = (const char *) bufferScript.Base();
  75. if ( !pBase || !*pBase )
  76. {
  77. return NULL;
  78. }
  79. }
  80. const char *pszFilename = V_strrchr( scriptPath, '/' );
  81. pszFilename++;
  82. HSCRIPT hScript = pScriptVM->CompileScript( pBase, pszFilename );
  83. if ( hScript == INVALID_HSCRIPT )
  84. {
  85. DevMsg( "FAILED to compile and execute script file named %s\n", scriptPath.operator const char *() );
  86. }
  87. return hScript;
  88. }
  89. bool GameUIScriptSystemRun( IScriptVM *pScriptVM, const char *pszScriptName, HSCRIPT hScope, bool bWarnMissing )
  90. {
  91. HSCRIPT hScript = GameUIScriptSystemCompile( pScriptVM, pszScriptName, bWarnMissing );
  92. bool bSuccess = false;
  93. if ( hScript != INVALID_HSCRIPT )
  94. {
  95. // if ( gpGlobals->maxClients == 1 )
  96. //{
  97. //CBaseEntity *pPlayer = UTIL_GetLocalPlayer();
  98. //if ( pPlayer )
  99. //{
  100. //g_pScriptVM->SetValue( "player", pPlayer->GetScriptInstance() );
  101. //}
  102. //}
  103. bSuccess = ( pScriptVM->Run( hScript, hScope ) != SCRIPT_ERROR );
  104. if ( !bSuccess )
  105. {
  106. DevMsg( "Error running script named %s\n", pszScriptName );
  107. }
  108. pScriptVM->ReleaseScript( hScript );
  109. }
  110. return bSuccess;
  111. }
  112. #include <tier0/memdbgoff.h>