2023-09-12 11:06:16 +02:00
//===== Copyright <20> 1996-2005, Valve Corporation, All rights reserved. ======//
2022-04-15 04:02:33 +02:00
//
// Purpose:
//
// $NoKeywords: $
//===========================================================================//
2021-12-25 22:36:38 +01:00
# include "core/stdafx.h"
2022-11-07 22:25:20 +01:00
# include "tier0/commandline.h"
2022-04-15 04:02:33 +02:00
# include "tier1/cmd.h"
# include "tier1/cvar.h"
2022-08-15 22:29:16 +02:00
# include "tier1/strtools.h"
2023-09-09 15:12:58 +02:00
# include "engine/sys_engine.h"
2021-12-25 22:36:38 +01:00
# include "engine/sys_dll.h"
# include "engine/sys_dll2.h"
2022-11-07 22:25:20 +01:00
# include "engine/host_cmd.h"
# include "engine/traceinit.h"
2022-08-17 02:04:01 +02:00
# ifndef DEDICATED
2023-09-11 22:20:24 +02:00
# include "engine/sys_mainwind.h"
2023-12-24 16:29:42 +01:00
# include "inputsystem/inputsystem.h"
# include "vgui/vgui_baseui_interface.h"
2023-09-13 22:24:59 +02:00
# include "materialsystem/cmaterialsystem.h"
2023-09-09 15:12:58 +02:00
# include "windows/id3dx.h"
2022-04-15 04:02:33 +02:00
# include "client/vengineclient_impl.h"
2023-09-12 00:08:46 +02:00
# include "geforce/reflex.h"
2022-08-17 02:04:01 +02:00
# endif // !DEDICATED
2022-11-07 22:25:20 +01:00
# include "filesystem/filesystem.h"
2023-02-26 19:37:12 +01:00
constexpr char DFS_ENABLE_PATH [ ] = " /vpk/enable.txt " ;
2022-04-15 04:02:33 +02:00
//-----------------------------------------------------------------------------
// Figure out if we're running a Valve mod or not.
//-----------------------------------------------------------------------------
static bool IsValveMod ( const char * pModName )
{
2022-08-15 22:29:16 +02:00
return ( Q_stricmp ( pModName , " cstrike " ) = = 0 | |
Q_stricmp ( pModName , " dod " ) = = 0 | |
Q_stricmp ( pModName , " hl1mp " ) = = 0 | |
Q_stricmp ( pModName , " tf " ) = = 0 | |
Q_stricmp ( pModName , " hl2mp " ) = = 0 | |
Q_stricmp ( pModName , " csgo " ) = = 0 ) ;
2022-04-15 04:02:33 +02:00
}
//-----------------------------------------------------------------------------
// Figure out if we're running a Respawn mod or not.
//-----------------------------------------------------------------------------
static bool IsRespawnMod ( const char * pModName )
{
2022-08-17 02:04:01 +02:00
return ( Q_stricmp ( pModName , " r1 " ) = = 0 | |
2022-08-15 22:29:16 +02:00
Q_stricmp ( pModName , " r2 " ) = = 0 | |
Q_stricmp ( pModName , " r5 " ) = = 0 ) ;
2022-04-15 04:02:33 +02:00
}
2022-11-07 23:49:05 +01:00
//-----------------------------------------------------------------------------
// Initialize the VPK and file cache system
//-----------------------------------------------------------------------------
static void InitVPKSystem ( )
{
2023-02-26 19:37:12 +01:00
char szCacheEnableFilePath [ 260 ] ; // [rsp+20h] [rbp-118h] BYREF
char bFixSlashes = FileSystem ( ) - > GetCurrentDirectory ( szCacheEnableFilePath , sizeof ( szCacheEnableFilePath ) ) ? szCacheEnableFilePath [ 0 ] : ' \0 ' ;
2022-11-07 23:49:05 +01:00
size_t nCachePathLen = strlen ( szCacheEnableFilePath ) ;
2023-02-26 19:37:12 +01:00
size_t nCacheFileLen = sizeof ( DFS_ENABLE_PATH ) - 1 ;
2022-11-07 23:49:05 +01:00
2023-02-26 19:37:12 +01:00
if ( ( nCachePathLen + nCacheFileLen ) < 0x104 | | ( nCacheFileLen = ( sizeof ( szCacheEnableFilePath ) - 1 ) - nCachePathLen , nCachePathLen ! = ( sizeof ( szCacheEnableFilePath ) - 1 ) ) )
2022-11-07 23:49:05 +01:00
{
2023-02-26 19:37:12 +01:00
strncat ( szCacheEnableFilePath , DFS_ENABLE_PATH , nCacheFileLen ) [ sizeof ( szCacheEnableFilePath ) - 1 ] = ' \0 ' ;
2022-11-07 23:49:05 +01:00
bFixSlashes = szCacheEnableFilePath [ 0 ] ;
}
if ( bFixSlashes )
{
V_FixSlashes ( szCacheEnableFilePath , ' / ' ) ;
}
if ( ! CommandLine ( ) - > CheckParm ( " -novpk " ) & & FileSystem ( ) - > FileExists ( szCacheEnableFilePath , nullptr ) )
{
FileSystem ( ) - > AddSearchPath ( " . " , " MAIN " , SearchPathAdd_t : : PATH_ADD_TO_TAIL ) ;
# ifndef DEDICATED
FileSystem ( ) - > SetVPKCacheModeClient ( ) ;
FileSystem ( ) - > MountVPKFile ( " vpk/client_frontend.bsp " ) ;
2023-02-26 19:37:12 +01:00
# else // Dedicated runs server vpk's and must have 'vpk/mp_common.bsp' mounted.
2022-11-07 23:49:05 +01:00
FileSystem ( ) - > SetVPKCacheModeServer ( ) ;
2023-02-26 19:37:12 +01:00
FileSystem ( ) - > MountVPKFile ( " vpk/server_mp_common.bsp " ) ;
2022-11-07 23:49:05 +01:00
# endif // !DEDICATED
}
}
2023-03-26 16:47:47 +02:00
InitReturnVal_t CEngineAPI : : VInit ( CEngineAPI * pEngineAPI )
{
2024-01-02 15:21:36 +01:00
return CEngineAPI__Init ( pEngineAPI ) ;
2023-03-26 16:47:47 +02:00
}
2022-04-15 04:02:33 +02:00
//-----------------------------------------------------------------------------
// Initialization, shutdown of a mod.
//-----------------------------------------------------------------------------
2022-11-07 22:25:20 +01:00
bool CEngineAPI : : VModInit ( CEngineAPI * pEngineAPI , const char * pModName , const char * pGameDir )
2022-04-15 04:02:33 +02:00
{
2022-12-20 01:50:51 +01:00
// Register new Pak Assets here!
2022-12-20 01:53:05 +01:00
//RTech_RegisterAsset(0, 1, "", nullptr, nullptr, nullptr, CMemory(0x1660AD0A8).RCast<void**>(), 8, 8, 8, 0, 0xFFFFFFC);
2022-12-20 01:50:51 +01:00
2024-01-02 15:21:36 +01:00
bool results = CEngineAPI__ModInit ( pEngineAPI , pModName , pGameDir ) ;
2022-08-17 02:04:01 +02:00
if ( ! IsValveMod ( pModName ) & & ! IsRespawnMod ( pModName ) )
2022-04-15 04:02:33 +02:00
{
2022-08-17 02:04:01 +02:00
# ifndef DEDICATED
g_pEngineClient - > SetRestrictServerCommands ( true ) ; // Restrict server commands.
g_pEngineClient - > SetRestrictClientCommands ( true ) ; // Restrict client commands.
# endif // !DEDICATED
2022-04-15 04:02:33 +02:00
}
2022-12-20 01:50:51 +01:00
2022-04-15 04:02:33 +02:00
return results ;
}
2022-11-07 22:25:20 +01:00
//-----------------------------------------------------------------------------
// Sets startup info
//-----------------------------------------------------------------------------
void CEngineAPI : : VSetStartupInfo ( CEngineAPI * pEngineAPI , StartupInfo_t * pStartupInfo )
{
if ( * g_bTextMode )
{
return ;
}
2023-04-23 11:35:09 +02:00
const size_t nBufLen = sizeof ( pStartupInfo - > m_szBaseDirectory ) ;
strncpy ( g_szBaseDir , pStartupInfo - > m_szBaseDirectory , nBufLen ) ;
2022-11-07 23:49:05 +01:00
2023-04-23 11:35:09 +02:00
g_pEngineParms - > baseDirectory = g_szBaseDir ;
g_szBaseDir [ nBufLen - 1 ] = ' \0 ' ;
2022-11-07 22:25:20 +01:00
2022-11-07 23:49:05 +01:00
void * * pCurrentInstance = & pEngineAPI - > m_StartupInfo . m_pInstance ;
2022-11-07 22:25:20 +01:00
size_t nInstances = 6 ;
do
{
2022-11-07 23:49:05 +01:00
pCurrentInstance + = 16 ;
uint64_t pInstance = * ( _QWORD * ) & pStartupInfo - > m_pInstance ;
2022-11-07 22:25:20 +01:00
pStartupInfo = ( StartupInfo_t * ) ( ( char * ) pStartupInfo + 128 ) ;
2022-11-07 23:49:05 +01:00
* ( ( _QWORD * ) pCurrentInstance - 8 ) = pInstance ;
* ( ( _QWORD * ) pCurrentInstance - 7 ) = * ( _QWORD * ) & pStartupInfo [ - 1 ] . m_pParentAppSystemGroup [ 132 ] ;
* ( ( _QWORD * ) pCurrentInstance - 6 ) = * ( _QWORD * ) & pStartupInfo [ - 1 ] . m_pParentAppSystemGroup [ 148 ] ;
* ( ( _QWORD * ) pCurrentInstance - 5 ) = * ( _QWORD * ) & pStartupInfo [ - 1 ] . m_pParentAppSystemGroup [ 164 ] ;
* ( ( _QWORD * ) pCurrentInstance - 4 ) = * ( _QWORD * ) & pStartupInfo [ - 1 ] . m_pParentAppSystemGroup [ 180 ] ;
* ( ( _QWORD * ) pCurrentInstance - 3 ) = * ( _QWORD * ) & pStartupInfo [ - 1 ] . m_pParentAppSystemGroup [ 196 ] ;
* ( ( _QWORD * ) pCurrentInstance - 2 ) = * ( _QWORD * ) & pStartupInfo [ - 1 ] . m_pParentAppSystemGroup [ 212 ] ;
* ( ( _QWORD * ) pCurrentInstance - 1 ) = * ( _QWORD * ) & pStartupInfo [ - 1 ] . m_pParentAppSystemGroup [ 228 ] ;
2022-11-07 22:25:20 +01:00
- - nInstances ;
} while ( nInstances ) ;
2022-11-07 23:49:05 +01:00
* ( _QWORD * ) pCurrentInstance = * ( _QWORD * ) & pStartupInfo - > m_pInstance ;
* ( ( _QWORD * ) pCurrentInstance + 1 ) = * ( _QWORD * ) & pStartupInfo - > m_szBaseDirectory [ 8 ] ;
2022-11-07 22:25:20 +01:00
2022-11-07 23:49:05 +01:00
InitVPKSystem ( ) ;
2022-11-07 22:25:20 +01:00
v_TRACEINIT ( NULL , " COM_InitFilesystem( m_StartupInfo.m_szInitialMod ) " , " COM_ShutdownFileSystem() " ) ;
2023-10-22 17:00:56 +02:00
v_COM_InitFilesystem ( pEngineAPI - > m_StartupInfo . m_szInitialMod ) ;
2022-11-07 22:25:20 +01:00
* g_bTextMode = true ;
}
2023-09-12 11:06:16 +02:00
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CEngineAPI : : PumpMessages ( )
{
# ifndef DEDICATED
2023-12-24 16:29:42 +01:00
MSG msg ;
while ( PeekMessageW ( & msg , NULL , 0 , 0 , PM_REMOVE ) )
{
TranslateMessage ( & msg ) ;
DispatchMessageW ( & msg ) ;
}
if ( in_syncRT - > GetBool ( ) )
( * g_fnSyncRTWithIn ) ( ) ;
2024-01-02 15:21:36 +01:00
g_pInputSystem - > PollInputState ( v_UIEventDispatcher ) ;
2023-12-24 16:29:42 +01:00
g_pGame - > DispatchAllStoredGameMessages ( ) ;
2023-09-12 11:06:16 +02:00
# endif // !DEDICATED
}
2024-02-24 02:15:09 +01:00
# ifndef DEDICATED
//-----------------------------------------------------------------------------
// Purpose: force update NVIDIA Reflex Low Latency parameters
//-----------------------------------------------------------------------------
static void GFX_NVN_Changed_f ( IConVar * pConVar , const char * pOldString )
{
GFX_MarkLowLatencyParametersOutOfDate ( ) ;
}
static ConVar fps_max_gfx ( " fps_max_gfx " , " 0 " , FCVAR_RELEASE , " Frame rate limiter using NVIDIA Reflex Low Latency SDK. -1 indicates the use of desktop refresh. 0 is disabled. " , true , - 1.f , true , 295.f , GFX_NVN_Changed_f ) ;
static ConVar gfx_nvnUseLowLatency ( " gfx_nvnUseLowLatency " , " 1 " , FCVAR_RELEASE | FCVAR_ARCHIVE , " Enables NVIDIA Reflex Low Latency SDK. " , GFX_NVN_Changed_f ) ;
static ConVar gfx_nvnUseLowLatencyBoost ( " gfx_nvnUseLowLatencyBoost " , " 0 " , FCVAR_RELEASE | FCVAR_ARCHIVE , " Enables NVIDIA Reflex Low Latency Boost. " , GFX_NVN_Changed_f ) ;
// NOTE: defaulted to 0 as it causes rubber banding on some hardware.
static ConVar gfx_nvnUseMarkersToOptimize ( " gfx_nvnUseMarkersToOptimize " , " 0 " , FCVAR_RELEASE , " Use NVIDIA Reflex Low Latency markers to optimize (requires Low Latency Boost to be enabled) . " , GFX_NVN_Changed_f) ;
# endif // !DEDICATED
2023-12-25 01:46:39 +01:00
void CEngineAPI : : UpdateLowLatencyParameters ( )
{
# ifndef DEDICATED
2024-02-24 02:15:09 +01:00
const bool bUseLowLatencyMode = gfx_nvnUseLowLatency . GetBool ( ) ;
const bool bUseLowLatencyBoost = gfx_nvnUseLowLatencyBoost . GetBool ( ) ;
const bool bUseMarkersToOptimize = gfx_nvnUseMarkersToOptimize . GetBool ( ) ;
2023-12-25 01:46:39 +01:00
2024-02-24 02:15:09 +01:00
float fpsMax = fps_max_gfx . GetFloat ( ) ;
2023-12-25 01:46:39 +01:00
if ( fpsMax = = - 1.0f )
{
const float globalFps = fps_max - > GetFloat ( ) ;
// Make sure the global fps limiter is 'unlimited'
// before we let the gfx frame limiter cap it to
// the desktop's refresh rate; not adhering to
// this will result in a major performance drop.
if ( globalFps = = 0.0f )
fpsMax = g_pGame - > GetTVRefreshRate ( ) ;
else
fpsMax = 0.0f ; // Don't let NVIDIA limit the frame rate.
}
GFX_UpdateLowLatencyParameters ( D3D11Device ( ) , bUseLowLatencyMode ,
bUseLowLatencyBoost , bUseMarkersToOptimize , fpsMax ) ;
# endif // !DEDICATED
}
void CEngineAPI : : RunLowLatencyFrame ( )
{
# ifndef DEDICATED
if ( GFX_IsLowLatencySDKEnabled ( ) )
{
if ( GFX_HasPendingLowLatencyParameterUpdates ( ) )
{
UpdateLowLatencyParameters ( ) ;
}
GFX_RunLowLatencyFrame ( D3D11Device ( ) ) ;
}
# endif // !DEDICATED
}
2023-09-09 15:12:58 +02:00
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CEngineAPI : : MainLoop ( )
{
2023-12-25 19:23:01 +01:00
# ifndef DEDICATED
bool bRunLowLatency = false ;
# endif // !DEDICATED
2023-09-09 15:12:58 +02:00
// Main message pump
while ( true )
{
// Pump messages unless someone wants to quit
if ( g_pEngine - > GetQuitting ( ) ! = IEngine : : QUIT_NOTQUITTING )
{
2023-12-25 19:23:01 +01:00
if ( g_pEngine - > GetQuitting ( ) ! = IEngine : : QUIT_TODESKTOP ) {
2023-09-09 15:12:58 +02:00
return true ;
2023-12-25 19:23:01 +01:00
}
2023-09-09 15:12:58 +02:00
return false ;
}
# ifndef DEDICATED
2023-12-25 19:23:01 +01:00
if ( bRunLowLatency ) {
CEngineAPI : : RunLowLatencyFrame ( ) ;
2023-12-25 22:36:35 +01:00
bRunLowLatency = false ;
2023-12-25 19:23:01 +01:00
}
2023-09-12 11:06:16 +02:00
CEngineAPI : : PumpMessages ( ) ;
2023-09-09 15:12:58 +02:00
# endif // !DEDICATED
2023-09-12 20:50:38 +02:00
if ( g_pEngine - > Frame ( ) )
{
# ifndef DEDICATED
2023-12-25 19:23:01 +01:00
// Only run reflex if we ran an actual engine frame.
bRunLowLatency = true ;
2023-09-12 20:50:38 +02:00
# endif // !DEDICATED
}
2023-09-09 15:12:58 +02:00
}
}
2022-04-15 04:02:33 +02:00
///////////////////////////////////////////////////////////////////////////////
2023-11-26 13:21:20 +01:00
void VSys_Dll2 : : Detour ( const bool bAttach ) const
2022-04-15 04:02:33 +02:00
{
2024-01-02 15:21:36 +01:00
DetourSetup ( & CEngineAPI__Init , & CEngineAPI : : VInit , bAttach ) ;
DetourSetup ( & CEngineAPI__ModInit , & CEngineAPI : : VModInit , bAttach ) ;
DetourSetup ( & CEngineAPI__PumpMessages , & CEngineAPI : : PumpMessages , bAttach ) ;
DetourSetup ( & CEngineAPI__MainLoop , & CEngineAPI : : MainLoop , bAttach ) ;
DetourSetup ( & CEngineAPI__SetStartupInfo , & CEngineAPI : : VSetStartupInfo , bAttach ) ;
2022-04-15 04:02:33 +02:00
}