2021-12-25 22:36:38 +01:00
//=============================================================================//
//
// Purpose: Squirrel VM
//
//=============================================================================//
# include "core/stdafx.h"
# include "core/logdef.h"
2022-01-14 20:45:36 +01:00
# include "tier0/commandline.h"
2022-04-09 16:16:40 +02:00
# include "tier1/cvar.h"
# include "tier1/IConVar.h"
2021-12-25 22:36:38 +01:00
# include "engine/sys_utils.h"
2022-02-08 16:32:00 +01:00
# ifdef DEDICATED
2022-05-20 11:52:19 +02:00
# include "engine/server/sv_rcon.h"
2022-04-02 12:27:35 +02:00
# else // DEDICATED
# include "client/cdll_engine_int.h"
2022-02-19 02:31:16 +01:00
# include "vgui/vgui_debugpanel.h"
2021-12-25 22:36:38 +01:00
# include "gameui/IConsole.h"
2022-04-02 12:27:35 +02:00
# endif
2022-03-30 22:54:33 +02:00
# include "squirrel/sqtype.h"
2022-01-14 20:45:36 +01:00
# include "squirrel/sqvm.h"
2022-01-16 00:35:39 +01:00
# include "squirrel/sqinit.h"
2022-05-09 21:21:05 +02:00
# include "squirrel/sqstdaux.h"
2022-05-19 02:19:43 +02:00
# include "squirrel/sqstate.h"
2021-12-25 22:36:38 +01:00
//---------------------------------------------------------------------------------
// Purpose: prints the output of each VM to the console
2022-03-04 15:34:09 +01:00
// Input : *sqvm -
// *fmt -
// ... -
2021-12-25 22:36:38 +01:00
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
SQRESULT SQVM_PrintFunc ( HSQUIRRELVM v , SQChar * fmt , . . . )
2021-12-25 22:36:38 +01:00
{
2022-03-31 02:26:05 +02:00
static SQCONTEXT context { } ;
2022-02-17 18:00:29 +01:00
// We use the sqvm pointer as index for SDK usage as the function prototype has to match assembly.
2022-03-31 02:26:05 +02:00
switch ( static_cast < SQCONTEXT > ( reinterpret_cast < int > ( v ) ) )
2022-02-17 18:00:29 +01:00
{
2022-03-31 02:26:05 +02:00
case SQCONTEXT : : SERVER :
context = SQCONTEXT : : SERVER ;
2022-02-17 18:00:29 +01:00
break ;
2022-03-31 02:26:05 +02:00
case SQCONTEXT : : CLIENT :
context = SQCONTEXT : : CLIENT ;
2022-02-17 18:00:29 +01:00
break ;
2022-03-31 02:26:05 +02:00
case SQCONTEXT : : UI :
context = SQCONTEXT : : UI ;
2022-02-17 18:00:29 +01:00
break ;
2022-03-31 02:26:05 +02:00
case SQCONTEXT : : NONE :
context = SQCONTEXT : : NONE ;
2022-02-17 18:00:29 +01:00
break ;
default :
2022-05-19 02:19:43 +02:00
# if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) && !defined (GAMEDLL_S2)
2022-03-31 02:26:05 +02:00
context = * reinterpret_cast < SQCONTEXT * > ( reinterpret_cast < std : : uintptr_t > ( v ) + 0x18 ) ;
2022-05-19 02:19:43 +02:00
# else // Nothing equal to 'rdx + 18h' exist in the vm structs for anything below S3.
context = SQVM_GetContextIndex ( v ) ;
2021-12-25 22:36:38 +01:00
# endif
2022-02-17 18:00:29 +01:00
break ;
}
2022-03-31 02:26:05 +02:00
static SQChar buf [ 1024 ] = { } ;
2022-02-17 18:00:29 +01:00
static std : : regex rxAnsiExp ( " \\ \033 \\ [.*?m " ) ;
2021-12-25 22:36:38 +01:00
2022-01-14 20:45:36 +01:00
static std : : shared_ptr < spdlog : : logger > iconsole = spdlog : : get ( " game_console " ) ;
static std : : shared_ptr < spdlog : : logger > wconsole = spdlog : : get ( " win_console " ) ;
2022-05-17 23:00:30 +02:00
static std : : shared_ptr < spdlog : : logger > sqlogger = spdlog : : get ( " sqvm_info " ) ;
2021-12-25 22:36:38 +01:00
2022-05-09 02:20:07 +02:00
s_LogMutex . lock ( ) ;
2022-01-14 20:45:36 +01:00
{ /////////////////////////////
va_list args { } ;
va_start ( args , fmt ) ;
2021-12-25 22:36:38 +01:00
2022-01-14 20:45:36 +01:00
vsnprintf ( buf , sizeof ( buf ) , fmt , args ) ;
2021-12-25 22:36:38 +01:00
2022-01-14 20:45:36 +01:00
buf [ sizeof ( buf ) - 1 ] = 0 ;
va_end ( args ) ;
} /////////////////////////////
2021-12-25 22:36:38 +01:00
2022-03-31 02:26:05 +02:00
std : : string vmStr = SQVM_LOG_T [ static_cast < SQInteger > ( context ) ] . c_str ( ) ;
2021-12-25 22:36:38 +01:00
vmStr . append ( buf ) ;
2022-05-09 21:21:05 +02:00
if ( sq_showvmoutput - > GetInt ( ) > 0 ) {
2021-12-25 22:36:38 +01:00
sqlogger - > debug ( vmStr ) ;
}
2022-01-09 17:17:05 +01:00
if ( sq_showvmoutput - > GetInt ( ) > 1 )
2021-12-25 22:36:38 +01:00
{
2022-05-09 21:21:05 +02:00
bool bError = false ;
2022-05-20 02:14:54 +02:00
bool bColorOverride = false ;
2022-01-14 20:45:36 +01:00
if ( ! g_bSpdLog_UseAnsiClr )
{
wconsole - > debug ( vmStr ) ;
2022-02-08 16:32:00 +01:00
# ifdef DEDICATED
g_pRConServer - > Send ( vmStr . c_str ( ) ) ;
# endif // DEDICATED
2022-01-14 20:45:36 +01:00
}
else
{
2022-05-09 21:21:05 +02:00
std : : string vmStrAnsi ;
if ( g_bSQAuxError )
{
2022-05-20 02:14:54 +02:00
bColorOverride = true ;
2022-05-09 21:21:05 +02:00
if ( strstr ( buf , " SCRIPT ERROR: " ) | | strstr ( buf , " -> " ) )
{
bError = true ;
vmStrAnsi = SQVM_ERROR_ANSI_LOG_T [ static_cast < SQInteger > ( context ) ] . c_str ( ) ;
}
2022-05-20 02:14:54 +02:00
else {
2022-05-09 21:21:05 +02:00
vmStrAnsi = SQVM_WARNING_ANSI_LOG_T [ static_cast < SQInteger > ( context ) ] . c_str ( ) ;
}
}
2022-05-20 01:16:30 +02:00
else if ( g_bSQAuxBadLogic )
2022-05-20 01:07:42 +02:00
{
if ( strstr ( buf , " There was a problem processing game logic. " ) )
{
bError = true ;
2022-05-20 02:14:54 +02:00
bColorOverride = true ;
2022-05-20 01:16:30 +02:00
g_bSQAuxBadLogic = false ;
2022-05-20 01:07:42 +02:00
vmStrAnsi = SQVM_ERROR_ANSI_LOG_T [ static_cast < SQInteger > ( context ) ] . c_str ( ) ;
}
2022-05-20 02:14:54 +02:00
else {
2022-05-20 01:07:42 +02:00
vmStrAnsi = SQVM_ANSI_LOG_T [ static_cast < SQInteger > ( context ) ] . c_str ( ) ;
}
}
2022-05-20 02:14:54 +02:00
else {
2022-05-09 21:21:05 +02:00
vmStrAnsi = SQVM_ANSI_LOG_T [ static_cast < SQInteger > ( context ) ] . c_str ( ) ;
}
2022-01-14 20:45:36 +01:00
vmStrAnsi . append ( buf ) ;
wconsole - > debug ( vmStrAnsi ) ;
2022-02-08 16:32:00 +01:00
# ifdef DEDICATED
g_pRConServer - > Send ( vmStrAnsi . c_str ( ) ) ;
# endif // DEDICATED
2022-01-14 20:45:36 +01:00
}
2021-12-25 22:36:38 +01:00
# ifndef DEDICATED
2022-02-17 18:00:29 +01:00
vmStr = std : : regex_replace ( vmStr , rxAnsiExp , " " ) ;
2022-01-14 20:45:36 +01:00
iconsole - > debug ( vmStr ) ;
if ( sq_showvmoutput - > GetInt ( ) > 2 )
{
2022-05-09 02:20:07 +02:00
ImVec4 color ;
2022-05-20 02:14:54 +02:00
if ( bColorOverride )
2022-05-09 21:21:05 +02:00
{
if ( bError ) {
color = ImVec4 ( 1.00f , 0.00f , 0.00f , 0.80f ) ;
}
else {
color = ImVec4 ( 1.00f , 1.00f , 0.00f , 0.80f ) ;
}
}
else
2022-05-09 02:20:07 +02:00
{
2022-05-09 21:21:05 +02:00
switch ( context )
{
case SQCONTEXT : : SERVER :
color = ImVec4 ( 0.59f , 0.58f , 0.73f , 1.00f ) ;
break ;
case SQCONTEXT : : CLIENT :
color = ImVec4 ( 0.59f , 0.58f , 0.63f , 1.00f ) ;
break ;
case SQCONTEXT : : UI :
color = ImVec4 ( 0.59f , 0.48f , 0.53f , 1.00f ) ;
break ;
default :
color = ImVec4 ( 0.59f , 0.58f , 0.63f , 1.00f ) ;
break ;
}
2022-05-09 02:20:07 +02:00
}
2022-05-10 23:32:44 +02:00
g_pIConsole - > m_ivConLog . push_back ( CConLog ( PrintPercentageEscape ( g_spd_sys_w_oss . str ( ) ) , color ) ) ;
2022-05-09 02:20:07 +02:00
g_pLogSystem . AddLog ( static_cast < LogType_t > ( context ) , g_spd_sys_w_oss . str ( ) ) ;
2022-02-08 16:32:00 +01:00
g_spd_sys_w_oss . str ( " " ) ;
g_spd_sys_w_oss . clear ( ) ;
2022-01-14 20:45:36 +01:00
}
2021-12-25 22:36:38 +01:00
# endif // !DEDICATED
}
2022-05-09 02:20:07 +02:00
s_LogMutex . unlock ( ) ;
2022-03-31 02:26:05 +02:00
return SQ_OK ;
2021-12-25 22:36:38 +01:00
}
//---------------------------------------------------------------------------------
// Purpose: prints the warning output of each VM to the console
2022-03-04 15:34:09 +01:00
// Input : *sqvm -
// a2 -
// a3 -
// *nStringSize -
// **ppString -
2021-12-25 22:36:38 +01:00
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
SQRESULT SQVM_WarningFunc ( HSQUIRRELVM v , SQInteger a2 , SQInteger a3 , SQInteger * nStringSize , SQChar * * ppString )
2021-12-25 22:36:38 +01:00
{
2022-04-10 19:59:34 +02:00
static void * retaddr = reinterpret_cast < void * > ( p_SQVM_WarningCmd . Offset ( 0x10 ) . FindPatternSelf ( " 85 ?? ?? 99 " , CMemory : : Direction : : DOWN ) . GetPtr ( ) ) ;
2022-05-19 02:19:43 +02:00
static SQCONTEXT context { } ;
SQRESULT result = v_SQVM_WarningFunc ( v , a2 , a3 , nStringSize , ppString ) ;
2022-01-14 20:45:36 +01:00
2022-05-09 02:20:07 +02:00
if ( retaddr ! = _ReturnAddress ( ) | | ! sq_showvmwarning - > GetBool ( ) ) // Check if its SQVM_Warning calling.
2021-12-25 22:36:38 +01:00
{
2022-03-31 02:26:05 +02:00
return result ;
2021-12-25 22:36:38 +01:00
}
2022-05-09 02:20:07 +02:00
s_LogMutex . lock ( ) ;
2021-12-25 22:36:38 +01:00
# ifdef GAMEDLL_S3
2022-05-19 02:19:43 +02:00
context = * reinterpret_cast < SQCONTEXT * > ( reinterpret_cast < std : : uintptr_t > ( v ) + 0x18 ) ;
# else // Nothing equal to 'rdx + 18h' exist in the vm structs for anything below S3.
context = SQVM_GetContextIndex ( v ) ;
2021-12-25 22:36:38 +01:00
# endif
2022-01-14 20:45:36 +01:00
static std : : shared_ptr < spdlog : : logger > iconsole = spdlog : : get ( " game_console " ) ;
static std : : shared_ptr < spdlog : : logger > wconsole = spdlog : : get ( " win_console " ) ;
2022-05-17 23:00:30 +02:00
static std : : shared_ptr < spdlog : : logger > sqlogger = spdlog : : get ( " sqvm_warn " ) ;
2021-12-25 22:36:38 +01:00
2022-05-10 18:09:31 +02:00
std : : string vmStr = SQVM_LOG_T [ static_cast < int > ( context ) ] . c_str ( ) ;
2022-05-09 02:20:07 +02:00
std : : string svConstructor ( * ppString , * nStringSize ) ; // Get string from memory via std::string constructor.
2022-01-14 20:45:36 +01:00
vmStr . append ( svConstructor ) ;
2021-12-25 22:36:38 +01:00
2022-05-09 02:20:07 +02:00
sqlogger - > debug ( vmStr ) ; // Emit to file.
2022-01-09 17:17:05 +01:00
if ( sq_showvmwarning - > GetInt ( ) > 1 )
2021-12-25 22:36:38 +01:00
{
2022-01-14 20:45:36 +01:00
if ( ! g_bSpdLog_UseAnsiClr )
{
wconsole - > debug ( vmStr ) ;
2022-02-08 16:32:00 +01:00
# ifdef DEDICATED
g_pRConServer - > Send ( vmStr . c_str ( ) ) ;
# endif // DEDICATED
2022-01-14 20:45:36 +01:00
}
else
{
2022-03-31 02:26:05 +02:00
std : : string vmStrAnsi = SQVM_WARNING_ANSI_LOG_T [ static_cast < int > ( context ) ] . c_str ( ) ;
2022-01-14 20:45:36 +01:00
vmStrAnsi . append ( svConstructor ) ;
wconsole - > debug ( vmStrAnsi ) ;
2022-02-08 16:32:00 +01:00
# ifdef DEDICATED
g_pRConServer - > Send ( vmStrAnsi . c_str ( ) ) ;
# endif // DEDICATED
2022-01-14 20:45:36 +01:00
}
2021-12-25 22:36:38 +01:00
# ifndef DEDICATED
2022-01-14 20:45:36 +01:00
iconsole - > debug ( vmStr ) ; // Emit to in-game console.
2022-05-10 23:32:44 +02:00
g_pIConsole - > m_ivConLog . push_back ( CConLog ( PrintPercentageEscape ( g_spd_sys_w_oss . str ( ) ) , ImVec4 ( 1.00f , 1.00f , 0.00f , 0.80f ) ) ) ;
2022-05-09 02:20:07 +02:00
g_pLogSystem . AddLog ( LogType_t : : WARNING_C , g_spd_sys_w_oss . str ( ) ) ;
2022-01-14 20:45:36 +01:00
2022-05-09 02:20:07 +02:00
g_spd_sys_w_oss . str ( " " ) ;
g_spd_sys_w_oss . clear ( ) ;
2021-12-25 22:36:38 +01:00
# endif // !DEDICATED
}
2022-05-09 02:20:07 +02:00
s_LogMutex . unlock ( ) ;
2021-12-25 22:36:38 +01:00
return result ;
}
2022-03-04 15:34:09 +01:00
//---------------------------------------------------------------------------------
// Purpose: prints the compile error and context to the console
// Input : *sqvm -
// *pszError -
// *pszFile -
// nLine -
// nColumn -
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
void SQVM_CompileError ( HSQUIRRELVM v , const SQChar * pszError , const SQChar * pszFile , SQUnsignedInteger nLine , SQInteger nColumn )
2022-03-04 15:34:09 +01:00
{
2022-03-31 02:26:05 +02:00
static SQCONTEXT context { } ;
2022-03-04 15:34:09 +01:00
static char szContextBuf [ 256 ] { } ;
2022-05-19 02:19:43 +02:00
# if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) && !defined (GAMEDLL_S2)
2022-03-31 02:26:05 +02:00
context = * reinterpret_cast < SQCONTEXT * > ( reinterpret_cast < std : : uintptr_t > ( v ) + 0x18 ) ;
2022-05-19 02:19:43 +02:00
# else // Nothing equal to 'rdx + 18h' exist in the vm structs for anything below S3.
context = SQVM_GetContextIndex ( v ) ;
2022-03-04 15:34:09 +01:00
# endif
2022-05-19 02:19:43 +02:00
v_SQVM_GetErrorLine ( pszFile , nLine , szContextBuf , sizeof ( szContextBuf ) ) ;
2022-03-04 15:34:09 +01:00
2022-05-19 02:19:43 +02:00
Error ( static_cast < eDLL_T > ( context ) , " %s SCRIPT COMPILE ERROR: %s \n " , SQVM_GetContextName ( context ) , pszError ) ;
2022-03-31 02:26:05 +02:00
Error ( static_cast < eDLL_T > ( context ) , " -> %s \n \n " , szContextBuf ) ;
Error ( static_cast < eDLL_T > ( context ) , " %s line [%d] column [%d] \n " , pszFile , nLine , nColumn ) ;
2022-03-04 15:34:09 +01:00
}
2021-12-25 22:36:38 +01:00
//---------------------------------------------------------------------------------
2022-01-19 19:02:18 +01:00
// Purpose: prints the global include file the compiler loads for loading scripts
2022-03-04 15:34:09 +01:00
// Input : *szRsonName -
2021-12-25 22:36:38 +01:00
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
SQInteger SQVM_LoadRson ( const SQChar * szRsonName )
2021-12-25 22:36:38 +01:00
{
2022-01-19 19:02:18 +01:00
if ( sq_showrsonloading - > GetBool ( ) )
2021-12-25 22:36:38 +01:00
{
2022-01-19 19:02:18 +01:00
DevMsg ( eDLL_T : : ENGINE , " \n " ) ;
DevMsg ( eDLL_T : : ENGINE , " ______________________________________________________________ \n " ) ;
DevMsg ( eDLL_T : : ENGINE , " ] RSON_SQVM -------------------------------------------------- \n " ) ;
DevMsg ( eDLL_T : : ENGINE , " ] PATH: '%s' \n " , szRsonName ) ;
DevMsg ( eDLL_T : : ENGINE , " -------------------------------------------------------------- \n " ) ;
DevMsg ( eDLL_T : : ENGINE , " \n " ) ;
2021-12-25 22:36:38 +01:00
}
2022-05-19 02:19:43 +02:00
return v_SQVM_LoadRson ( szRsonName ) ;
2021-12-25 22:36:38 +01:00
}
//---------------------------------------------------------------------------------
2022-01-19 19:02:18 +01:00
// Purpose: prints the scripts the compiler loads from global include to be compiled
2022-03-04 15:34:09 +01:00
// Input : *sqvm -
// *szScriptPath -
2022-05-19 02:19:43 +02:00
// *szScriptName -
2022-03-04 15:34:09 +01:00
// nFlag -
2021-12-25 22:36:38 +01:00
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
SQBool SQVM_LoadScript ( HSQUIRRELVM v , const SQChar * szScriptPath , const SQChar * szScriptName , SQInteger nFlag )
2021-12-25 22:36:38 +01:00
{
2022-01-09 17:17:05 +01:00
if ( sq_showscriptloading - > GetBool ( ) )
2021-12-25 22:36:38 +01:00
{
2022-01-19 19:02:18 +01:00
DevMsg ( eDLL_T : : ENGINE , " Loading SQVM Script '%s' \n " , szScriptName ) ;
2021-12-25 22:36:38 +01:00
}
///////////////////////////////////////////////////////////////////////////////
2022-05-19 02:19:43 +02:00
return v_SQVM_LoadScript ( v , szScriptPath , szScriptName , nFlag ) ;
2021-12-25 22:36:38 +01:00
}
2022-01-16 00:35:39 +01:00
//---------------------------------------------------------------------------------
2022-02-08 16:32:00 +01:00
// Purpose: registers and exposes code functions to target context
2022-03-04 15:34:09 +01:00
// Input : *sqvm -
// *szName -
// *szHelpString -
// *szRetValType -
// *szArgTypes -
// *pFunction -
2022-01-16 00:35:39 +01:00
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
SQRESULT SQVM_RegisterFunction ( HSQUIRRELVM v , const SQChar * szName , const SQChar * szHelpString , const SQChar * szRetValType , const SQChar * szArgTypes , void * pFunction )
2021-12-25 22:36:38 +01:00
{
2022-05-20 20:33:37 +01:00
ScriptFunctionBinding_t * sqFunc = new ScriptFunctionBinding_t ( ) ;
2021-12-25 22:36:38 +01:00
sqFunc - > m_szScriptName = szName ;
sqFunc - > m_szNativeName = szName ;
sqFunc - > m_szHelpString = szHelpString ;
sqFunc - > m_szRetValType = szRetValType ;
sqFunc - > m_szArgTypes = szArgTypes ;
sqFunc - > m_pFunction = pFunction ;
2022-05-19 02:19:43 +02:00
return v_SQVM_RegisterFunc ( v , sqFunc , 1 ) ;
2021-12-25 22:36:38 +01:00
}
2022-04-27 16:48:18 +02:00
# ifndef CLIENT_DLL
2022-01-16 00:35:39 +01:00
//---------------------------------------------------------------------------------
2022-02-08 16:32:00 +01:00
// Purpose: registers script functions in SERVER context
2022-03-04 15:34:09 +01:00
// Input : *sqvm -
2022-01-16 00:35:39 +01:00
//---------------------------------------------------------------------------------
2022-03-31 02:26:05 +02:00
void SQVM_RegisterServerScriptFunctions ( HSQUIRRELVM v )
2022-01-16 00:35:39 +01:00
{
2022-05-19 02:19:43 +02:00
SQVM_RegisterFunction ( v , " SDKNativeTest " , " Native SERVER test function " , " void " , " " , & VSquirrel : : SHARED : : SDKNativeTest ) ;
SQVM_RegisterFunction ( v , " GetSDKVersion " , " Gets the SDK version as a string " , " string " , " " , & VSquirrel : : SHARED : : GetSDKVersion ) ;
SQVM_RegisterFunction ( v , " GetNumHumanPlayers " , " Gets the number of human players on the server " , " int " , " " , & VSquirrel : : SERVER : : GetNumHumanPlayers ) ;
SQVM_RegisterFunction ( v , " GetNumFakeClients " , " Gets the number of bot players on the server " , " int " , " " , & VSquirrel : : SERVER : : GetNumFakeClients ) ;
2022-01-16 00:35:39 +01:00
}
2022-04-27 16:48:18 +02:00
# endif // !CLIENT_DLL
2022-01-16 00:35:39 +01:00
# ifndef DEDICATED
//---------------------------------------------------------------------------------
2022-02-08 16:32:00 +01:00
// Purpose: registers script functions in CLIENT context
2022-03-04 15:34:09 +01:00
// Input : *sqvm -
2022-01-16 00:35:39 +01:00
//---------------------------------------------------------------------------------
2022-03-31 02:26:05 +02:00
void SQVM_RegisterClientScriptFunctions ( HSQUIRRELVM v )
2021-12-25 22:36:38 +01:00
{
2022-05-19 02:19:43 +02:00
SQVM_RegisterFunction ( v , " SDKNativeTest " , " Native CLIENT test function " , " void " , " " , & VSquirrel : : SHARED : : SDKNativeTest ) ;
SQVM_RegisterFunction ( v , " GetSDKVersion " , " Gets the SDK version as a string " , " string " , " " , & VSquirrel : : SHARED : : GetSDKVersion ) ;
2021-12-25 22:36:38 +01:00
}
2022-01-16 00:35:39 +01:00
//---------------------------------------------------------------------------------
2022-02-08 16:32:00 +01:00
// Purpose: registers script functions in UI context
2022-03-04 15:34:09 +01:00
// Input : *sqvm -
2022-01-16 00:35:39 +01:00
//---------------------------------------------------------------------------------
2022-03-31 02:26:05 +02:00
void SQVM_RegisterUIScriptFunctions ( HSQUIRRELVM v )
2021-12-25 22:36:38 +01:00
{
2022-05-19 02:19:43 +02:00
SQVM_RegisterFunction ( v , " SDKNativeTest " , " Native UI test function " , " void " , " " , & VSquirrel : : SHARED : : SDKNativeTest ) ;
2022-01-15 17:57:18 +00:00
2022-03-28 19:34:51 +02:00
// Functions for retrieving server browser data
2022-05-19 02:19:43 +02:00
SQVM_RegisterFunction ( v , " GetServerName " , " Gets the name of the server at the specified index of the server list " , " string " , " int " , & VSquirrel : : UI : : GetServerName ) ;
SQVM_RegisterFunction ( v , " GetServerPlaylist " , " Gets the playlist of the server at the specified index of the server list " , " string " , " int " , & VSquirrel : : UI : : GetServerPlaylist ) ;
SQVM_RegisterFunction ( v , " GetServerMap " , " Gets the map of the server at the specified index of the server list " , " string " , " int " , & VSquirrel : : UI : : GetServerMap ) ;
SQVM_RegisterFunction ( v , " GetServerCount " , " Gets the number of public servers " , " int " , " " , & VSquirrel : : UI : : GetServerCount ) ;
2022-01-15 17:57:18 +00:00
2022-03-28 19:34:51 +02:00
// Misc main menu functions
2022-05-19 02:19:43 +02:00
SQVM_RegisterFunction ( v , " GetSDKVersion " , " Gets the SDK version as a string " , " string " , " " , & VSquirrel : : SHARED : : GetSDKVersion ) ;
SQVM_RegisterFunction ( v , " GetPromoData " , " Gets promo data for specified slot type " , " string " , " int " , & VSquirrel : : UI : : GetPromoData ) ;
2022-01-15 17:57:18 +00:00
2022-03-28 19:34:51 +02:00
// Functions for connecting to servers
2022-05-19 02:19:43 +02:00
SQVM_RegisterFunction ( v , " CreateServer " , " Start server with the specified settings " , " void " , " string,string,string,int " , & VSquirrel : : UI : : CreateServerFromMenu ) ;
SQVM_RegisterFunction ( v , " SetEncKeyAndConnect " , " Set the encryption key to that of the specified server and connects to it " , " void " , " int " , & VSquirrel : : UI : : SetEncKeyAndConnect ) ;
SQVM_RegisterFunction ( v , " JoinPrivateServerFromMenu " , " Joins private server by token " , " void " , " string " , & VSquirrel : : UI : : JoinPrivateServerFromMenu ) ;
SQVM_RegisterFunction ( v , " GetPrivateServerMessage " , " Gets private server join status message " , " string " , " string " , & VSquirrel : : UI : : GetPrivateServerMessage ) ;
SQVM_RegisterFunction ( v , " ConnectToIPFromMenu " , " Joins server by ip and encryption key " , " void " , " string,string " , & VSquirrel : : UI : : ConnectToIPFromMenu ) ;
2022-01-15 17:57:18 +00:00
2022-05-19 02:19:43 +02:00
SQVM_RegisterFunction ( v , " GetAvailableMaps " , " Gets an array of all the available maps that can be used to host a server " , " array<string> " , " " , & VSquirrel : : UI : : GetAvailableMaps ) ;
2021-12-25 22:36:38 +01:00
}
2022-01-16 00:35:39 +01:00
//---------------------------------------------------------------------------------
2022-03-28 19:34:51 +02:00
// Purpose: Initialize all CLIENT/UI global structs and register SDK (CLIENT/UI) script functions
2022-03-04 15:34:09 +01:00
// Input : *sqvm -
2022-03-28 19:34:51 +02:00
// context - (1 = CLIENT 2 = UI)
2022-01-16 00:35:39 +01:00
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
SQInteger SQVM_InitializeCLGlobalScriptStructs ( HSQUIRRELVM v , SQCONTEXT context )
2022-01-15 15:25:19 +01:00
{
2022-05-19 02:19:43 +02:00
int results = v_SQVM_InitializeCLGlobalScriptStructs ( v , context ) ;
2022-03-28 19:34:51 +02:00
if ( context = = SQCONTEXT : : CLIENT )
2022-03-31 02:26:05 +02:00
SQVM_RegisterClientScriptFunctions ( g_pClientVM . GetValue < HSQUIRRELVM > ( ) ) ;
2022-03-28 19:34:51 +02:00
if ( context = = SQCONTEXT : : UI )
2022-03-31 02:26:05 +02:00
SQVM_RegisterUIScriptFunctions ( g_pUIVM . GetValue < HSQUIRRELVM > ( ) ) ;
2022-03-28 19:34:51 +02:00
return results ;
}
# endif // !DEDICATED
# ifndef CLIENT_DLL
//---------------------------------------------------------------------------------
// Purpose: Initialize all SERVER global structs and register SDK (SERVER) script functions
// Input : *sqvm -
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
void SQVM_InitializeSVGlobalScriptStructs ( HSQUIRRELVM v )
2022-03-28 19:34:51 +02:00
{
2022-05-19 02:19:43 +02:00
v_SQVM_InitializeSVGlobalScriptStructs ( v ) ;
2022-03-31 02:26:05 +02:00
SQVM_RegisterServerScriptFunctions ( g_pServerVM . GetValue < HSQUIRRELVM > ( ) ) ;
2022-03-28 19:34:51 +02:00
}
//---------------------------------------------------------------------------------
// Purpose: Creates the SERVER Squirrel VM
// Output : True on success, false on failure
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
SQBool SQVM_CreateServerVM ( )
2022-03-28 19:34:51 +02:00
{
2022-05-19 02:19:43 +02:00
bool results = v_SQVM_CreateServerVM ( ) ;
2022-03-28 19:34:51 +02:00
if ( results )
2022-03-31 02:26:05 +02:00
DevMsg ( eDLL_T : : SERVER , " Created SERVER VM: '%p' \n " , g_pServerVM . GetValue < HSQUIRRELVM > ( ) ) ;
2022-03-28 19:34:51 +02:00
else
Error ( eDLL_T : : SERVER , " Failed to create SERVER VM \n " ) ;
return results ;
}
# endif // !CLIENT_DLL
# ifndef DEDICATED
//---------------------------------------------------------------------------------
// Purpose: Creates the CLIENT Squirrel VM
// Input : *chlclient -
// Output : True on success, false on failure
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
SQBool SQVM_CreateClientVM ( CHLClient * hlclient )
2022-03-28 19:34:51 +02:00
{
2022-05-19 02:19:43 +02:00
bool results = v_SQVM_CreateClientVM ( hlclient ) ;
2022-03-28 19:34:51 +02:00
if ( results )
2022-03-31 02:26:05 +02:00
DevMsg ( eDLL_T : : CLIENT , " Created CLIENT VM: '%p' \n " , g_pClientVM . GetValue < HSQUIRRELVM > ( ) ) ;
2022-01-15 15:25:19 +01:00
else
2022-03-28 19:34:51 +02:00
Error ( eDLL_T : : CLIENT , " Failed to create CLIENT VM \n " ) ;
return results ;
}
2022-01-15 15:25:19 +01:00
2022-03-28 19:34:51 +02:00
//---------------------------------------------------------------------------------
// Purpose: Creates the UI Squirrel VM
// Output : True on success, false on failure
//---------------------------------------------------------------------------------
2022-05-19 02:19:43 +02:00
SQBool SQVM_CreateUIVM ( )
2022-03-28 19:34:51 +02:00
{
2022-05-19 02:19:43 +02:00
bool results = v_SQVM_CreateUIVM ( ) ;
2022-03-28 19:34:51 +02:00
if ( results )
2022-03-31 02:26:05 +02:00
DevMsg ( eDLL_T : : UI , " Created UI VM: '%p' \n " , g_pUIVM . GetValue < HSQUIRRELVM > ( ) ) ;
2022-03-28 19:34:51 +02:00
else
Error ( eDLL_T : : UI , " Failed to create UI VM \n " ) ;
return results ;
2022-01-15 15:25:19 +01:00
}
2022-01-16 00:35:39 +01:00
# endif // !DEDICATED
2022-01-15 15:25:19 +01:00
2022-03-30 22:54:33 +02:00
//---------------------------------------------------------------------------------
// Purpose: Returns the VM name by context
// Input : context -
// Output : const SQChar*
//---------------------------------------------------------------------------------
const SQChar * SQVM_GetContextName ( SQCONTEXT context )
{
switch ( context )
{
case SQCONTEXT : : SERVER :
return " SERVER " ;
case SQCONTEXT : : CLIENT :
return " CLIENT " ;
case SQCONTEXT : : UI :
return " UI " ;
default :
return nullptr ;
}
}
2022-05-19 02:19:43 +02:00
//---------------------------------------------------------------------------------
// Purpose: Returns the VM context by name
// Input : *sqvm -
// Output : const SQCONTEXT*
//---------------------------------------------------------------------------------
const SQCONTEXT SQVM_GetContextIndex ( HSQUIRRELVM v )
{
if ( strcmp ( v - > _sharedstate - > _contextname , " SERVER " ) = = 0 )
return SQCONTEXT : : SERVER ;
if ( strcmp ( v - > _sharedstate - > _contextname , " CLIENT " ) = = 0 )
return SQCONTEXT : : CLIENT ;
if ( strcmp ( v - > _sharedstate - > _contextname , " UI " ) = = 0 )
return SQCONTEXT : : UI ;
return SQCONTEXT : : NONE ;
}
2022-03-30 22:54:33 +02:00
//---------------------------------------------------------------------------------
// Purpose: Returns the VM pointer by context
// Input : context -
// Output : SQVM*
//---------------------------------------------------------------------------------
HSQUIRRELVM SQVM_GetVM ( SQCONTEXT context )
{
switch ( context )
{
2022-03-31 17:23:14 +02:00
# ifndef CLIENT_DLL
2022-03-30 22:54:33 +02:00
case SQCONTEXT : : SERVER :
return g_pServerVM . GetValue < HSQUIRRELVM > ( ) ;
2022-03-31 17:23:14 +02:00
# endif // !CLIENT_DLL
2022-03-31 02:26:05 +02:00
# ifndef DEDICATED
2022-03-30 22:54:33 +02:00
case SQCONTEXT : : CLIENT :
return g_pClientVM . GetValue < HSQUIRRELVM > ( ) ;
case SQCONTEXT : : UI :
return g_pUIVM . GetValue < HSQUIRRELVM > ( ) ;
2022-03-31 02:26:05 +02:00
# endif // !DEDICATED
2022-03-30 22:54:33 +02:00
default :
return nullptr ;
}
}
//---------------------------------------------------------------------------------
// Purpose: Compiles and executes input code on target VM by context
// Input : *code -
// context -
//---------------------------------------------------------------------------------
void SQVM_Execute ( const SQChar * code , SQCONTEXT context )
{
HSQUIRRELVM v = SQVM_GetVM ( context ) ;
if ( ! v )
{
2022-04-22 02:27:35 +02:00
Error ( eDLL_T : : ENGINE , " Attempted to run %s script while VM isn't initialized \n " , SQVM_GetContextName ( context ) ) ;
2022-03-30 22:54:33 +02:00
return ;
}
SQVM * vTable = v - > GetVTable ( ) ;
SQRESULT compileResult { } ;
SQBufState bufState = SQBufState ( code ) ;
compileResult = sq_compilebuffer ( vTable , & bufState , " console " , - 1 ) ;
if ( compileResult > = 0 )
{
sq_pushroottable ( vTable ) ;
SQRESULT callResult = sq_call ( vTable , 1 , false , false ) ;
}
}
2022-05-19 02:19:43 +02:00
//---------------------------------------------------------------------------------
2021-12-25 22:36:38 +01:00
void SQVM_Attach ( )
{
2022-05-19 02:19:43 +02:00
DetourAttach ( ( LPVOID * ) & v_SQVM_PrintFunc , & SQVM_PrintFunc ) ;
DetourAttach ( ( LPVOID * ) & v_SQVM_WarningFunc , & SQVM_WarningFunc ) ;
DetourAttach ( ( LPVOID * ) & v_SQVM_CompileError , & SQVM_CompileError ) ;
DetourAttach ( ( LPVOID * ) & v_SQVM_LoadRson , & SQVM_LoadRson ) ;
DetourAttach ( ( LPVOID * ) & v_SQVM_LoadScript , & SQVM_LoadScript ) ;
2022-01-16 00:35:39 +01:00
# ifndef DEDICATED
2022-05-19 02:19:43 +02:00
DetourAttach ( ( LPVOID * ) & v_SQVM_InitializeCLGlobalScriptStructs , & SQVM_InitializeCLGlobalScriptStructs ) ;
2022-03-28 19:34:51 +02:00
# endif // !DEDICATED
# ifndef CLIENT_DLL
2022-05-19 02:19:43 +02:00
DetourAttach ( ( LPVOID * ) & v_SQVM_InitializeSVGlobalScriptStructs , & SQVM_InitializeSVGlobalScriptStructs ) ;
DetourAttach ( ( LPVOID * ) & v_SQVM_CreateServerVM , & SQVM_CreateServerVM ) ;
2022-03-28 19:34:51 +02:00
# endif // !CLIENT_DLL
# ifndef DEDICATED
2022-05-19 02:19:43 +02:00
DetourAttach ( ( LPVOID * ) & v_SQVM_CreateClientVM , & SQVM_CreateClientVM ) ;
DetourAttach ( ( LPVOID * ) & v_SQVM_CreateUIVM , & SQVM_CreateUIVM ) ;
2022-01-16 00:35:39 +01:00
# endif // !DEDICATED
2021-12-25 22:36:38 +01:00
}
2022-05-19 02:19:43 +02:00
//---------------------------------------------------------------------------------
2021-12-25 22:36:38 +01:00
void SQVM_Detach ( )
{
2022-05-19 02:19:43 +02:00
DetourDetach ( ( LPVOID * ) & v_SQVM_PrintFunc , & SQVM_PrintFunc ) ;
DetourDetach ( ( LPVOID * ) & v_SQVM_WarningFunc , & SQVM_WarningFunc ) ;
DetourDetach ( ( LPVOID * ) & v_SQVM_CompileError , & SQVM_CompileError ) ;
DetourDetach ( ( LPVOID * ) & v_SQVM_LoadRson , & SQVM_LoadRson ) ;
DetourDetach ( ( LPVOID * ) & v_SQVM_LoadScript , & SQVM_LoadScript ) ;
2022-01-16 00:35:39 +01:00
# ifndef DEDICATED
2022-05-19 02:19:43 +02:00
DetourDetach ( ( LPVOID * ) & v_SQVM_InitializeCLGlobalScriptStructs , & SQVM_InitializeCLGlobalScriptStructs ) ;
2022-03-28 19:34:51 +02:00
# endif // !DEDICATED
# ifndef CLIENT_DLL
2022-05-19 02:19:43 +02:00
DetourDetach ( ( LPVOID * ) & v_SQVM_InitializeSVGlobalScriptStructs , & SQVM_InitializeSVGlobalScriptStructs ) ;
DetourDetach ( ( LPVOID * ) & v_SQVM_CreateServerVM , & SQVM_CreateServerVM ) ;
2022-03-28 19:34:51 +02:00
# endif // !CLIENT_DLL
# ifndef DEDICATED
2022-05-19 02:19:43 +02:00
DetourDetach ( ( LPVOID * ) & v_SQVM_CreateClientVM , & SQVM_CreateClientVM ) ;
DetourDetach ( ( LPVOID * ) & v_SQVM_CreateUIVM , & SQVM_CreateUIVM ) ;
2022-01-16 00:35:39 +01:00
# endif // !DEDICATED
2021-12-25 22:36:38 +01:00
}