From 2b77baefbec5a0e3829e9b0c9bb8f669282bbc7a Mon Sep 17 00:00:00 2001 From: samisalreadytaken <46823719+samisalreadytaken@users.noreply.github.com> Date: Mon, 3 Mar 2025 20:54:42 +0300 Subject: [PATCH] Add script_connect_debugger_on_mapspawn cvar --- sp/src/game/client/vscript_client.cpp | 9 ++++++--- sp/src/game/server/vscript_server.cpp | 9 ++++++--- sp/src/game/shared/vscript_shared.cpp | 16 +--------------- sp/src/game/shared/vscript_shared.h | 6 ++++++ sp/src/public/vscript/ivscript.h | 2 +- sp/src/vscript/vscript.cpp | 1 - sp/src/vscript/vscript_squirrel.cpp | 21 ++++++++++++++++++--- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/sp/src/game/client/vscript_client.cpp b/sp/src/game/client/vscript_client.cpp index 14b40837..34d47291 100644 --- a/sp/src/game/client/vscript_client.cpp +++ b/sp/src/game/client/vscript_client.cpp @@ -29,7 +29,7 @@ extern IScriptManager *scriptmanager; extern ScriptClassDesc_t * GetScriptDesc( CBaseEntity * ); #ifdef MAPBASE_VSCRIPT -extern int vscript_debugger_port; +ConVar script_connect_debugger_on_mapspawn_client( "script_connect_debugger_on_mapspawn_client", "0" ); #endif // #define VMPROFILE 1 @@ -687,10 +687,13 @@ bool VScriptClientInit() #endif #ifdef MAPBASE_VSCRIPT - if ( vscript_debugger_port ) + if ( script_connect_debugger_on_mapspawn_client.GetInt() == 2 ) + { + g_pScriptVM->ConnectDebugger( vscript_debugger_port, 10.0f ); + } + else if ( script_connect_debugger_on_mapspawn_client.GetInt() != 0 ) { g_pScriptVM->ConnectDebugger( vscript_debugger_port ); - vscript_debugger_port = 0; } #endif diff --git a/sp/src/game/server/vscript_server.cpp b/sp/src/game/server/vscript_server.cpp index 121ecaef..2f59fa62 100644 --- a/sp/src/game/server/vscript_server.cpp +++ b/sp/src/game/server/vscript_server.cpp @@ -23,7 +23,7 @@ extern ScriptClassDesc_t * GetScriptDesc( CBaseEntity * ); #ifdef MAPBASE_VSCRIPT -extern int vscript_debugger_port; +ConVar script_connect_debugger_on_mapspawn( "script_connect_debugger_on_mapspawn", "0" ); #endif // #define VMPROFILE 1 @@ -668,10 +668,13 @@ bool VScriptServerInit() #endif #ifdef MAPBASE_VSCRIPT - if ( vscript_debugger_port ) + if ( script_connect_debugger_on_mapspawn.GetInt() == 2 ) + { + g_pScriptVM->ConnectDebugger( vscript_debugger_port, 10.0f ); + } + else if ( script_connect_debugger_on_mapspawn.GetInt() != 0 ) { g_pScriptVM->ConnectDebugger( vscript_debugger_port ); - vscript_debugger_port = 0; } #endif diff --git a/sp/src/game/shared/vscript_shared.cpp b/sp/src/game/shared/vscript_shared.cpp index f487ac4b..8b5452ec 100644 --- a/sp/src/game/shared/vscript_shared.cpp +++ b/sp/src/game/shared/vscript_shared.cpp @@ -37,7 +37,6 @@ extern ScriptClassDesc_t * GetScriptDesc( CBaseEntity * ); #ifdef MAPBASE_VSCRIPT // This is to ensure a dependency exists between the vscript library and the game DLLs extern int vscript_token; -extern int vscript_debugger_port; int vscript_token_hack = vscript_token; #endif @@ -391,27 +390,14 @@ CON_COMMAND_F( script_debug, "Connect the vscript VM to the script debugger", FC if ( !IsCommandIssuedByServerAdmin() ) return; -#ifdef MAPBASE_VSCRIPT -#ifdef GAME_DLL - int port = 1212; -#else - int port = 1213; -#endif -#endif - if ( !g_pScriptVM ) { -#ifdef MAPBASE_VSCRIPT - vscript_debugger_port = port; - CGMsg( 0, CON_GROUP_VSCRIPT, "VScript VM is not running, waiting for it to attach the debugger to port %d...\n", port ); -#else CGWarning( 0, CON_GROUP_VSCRIPT, "Scripting disabled or no server running\n" ); -#endif return; } #ifdef MAPBASE_VSCRIPT - g_pScriptVM->ConnectDebugger( port ); + g_pScriptVM->ConnectDebugger( vscript_debugger_port ); #else g_pScriptVM->ConnectDebugger(); #endif diff --git a/sp/src/game/shared/vscript_shared.h b/sp/src/game/shared/vscript_shared.h index 50834220..db2e7d79 100644 --- a/sp/src/game/shared/vscript_shared.h +++ b/sp/src/game/shared/vscript_shared.h @@ -43,6 +43,12 @@ class CBaseEntityScriptInstanceHelper : public IScriptInstanceHelper extern CBaseEntityScriptInstanceHelper g_BaseEntityScriptInstanceHelper; #ifdef MAPBASE_VSCRIPT +#ifdef GAME_DLL +const int vscript_debugger_port = 1212; +#else +const int vscript_debugger_port = 1213; +#endif + void RegisterSharedScriptConstants(); void RegisterSharedScriptFunctions(); diff --git a/sp/src/public/vscript/ivscript.h b/sp/src/public/vscript/ivscript.h index 8081968e..6408aa51 100644 --- a/sp/src/public/vscript/ivscript.h +++ b/sp/src/public/vscript/ivscript.h @@ -839,7 +839,7 @@ public: virtual void Shutdown() = 0; #ifdef MAPBASE_VSCRIPT - virtual bool ConnectDebugger( int port = 0 ) = 0; + virtual bool ConnectDebugger( int port = 0, float timeout = 0.0f ) = 0; #else virtual bool ConnectDebugger() = 0; #endif diff --git a/sp/src/vscript/vscript.cpp b/sp/src/vscript/vscript.cpp index 2bf0a8d6..a91008d7 100644 --- a/sp/src/vscript/vscript.cpp +++ b/sp/src/vscript/vscript.cpp @@ -17,7 +17,6 @@ IScriptVM* makeSquirrelVM(); int vscript_token = 0; -int vscript_debugger_port = 0; class CScriptManager : public CTier1AppSystem { diff --git a/sp/src/vscript/vscript_squirrel.cpp b/sp/src/vscript/vscript_squirrel.cpp index 038a790a..be9aebd1 100644 --- a/sp/src/vscript/vscript_squirrel.cpp +++ b/sp/src/vscript/vscript_squirrel.cpp @@ -141,7 +141,7 @@ public: virtual bool Init() override; virtual void Shutdown() override; - virtual bool ConnectDebugger( int port = 0 ) override; + virtual bool ConnectDebugger( int port = 0, float timeout = 0.0f ) override; virtual void DisconnectDebugger() override; virtual ScriptLanguage_t GetLanguage() override; @@ -2077,12 +2077,27 @@ void SquirrelVM::Shutdown() bool VScriptRunScript( const char *pszScriptName, HSCRIPT hScope, bool bWarnMissing ); -bool SquirrelVM::ConnectDebugger( int port ) +bool SquirrelVM::ConnectDebugger( int port, float timeout ) { if ( !debugger_ ) { debugger_ = sqdbg_attach_debugger( vm_ ); - sqdbg_listen_socket( debugger_, port ); + + if ( sqdbg_listen_socket( debugger_, port ) == 0 && timeout ) + { + float startTime = Plat_FloatTime(); + + while ( !sqdbg_is_client_connected( debugger_ ) ) + { + float time = Plat_FloatTime(); + if ( time - startTime > timeout ) + break; + + ThreadSleep( 50 ); + + sqdbg_frame( debugger_ ); + } + } } else {