2022-01-16 01:18:36 +01:00
//=============================================================================//
//
// Purpose: Windows terminal utilities
//
//=============================================================================//
2021-12-25 22:36:38 +01:00
# include "core/stdafx.h"
# include "core/init.h"
2022-01-14 20:45:36 +01:00
# include "core/logdef.h"
2021-12-25 22:36:38 +01:00
# ifndef DEDICATED
# include "windows/id3dx.h"
# endif // !DEDICATED
# include "windows/console.h"
# include "client/IVEngineClient.h"
# include "common/opcodes.h"
2021-11-05 00:57:52 +01:00
2022-01-16 01:18:36 +01:00
//-----------------------------------------------------------------------------
// Purpose: sets the windows terminal background color
// Input : color -
//-----------------------------------------------------------------------------
void SetConsoleBackgroundColor ( COLORREF color )
{
CONSOLE_SCREEN_BUFFER_INFOEX sbInfoEx { } ;
sbInfoEx . cbSize = sizeof ( CONSOLE_SCREEN_BUFFER_INFOEX ) ;
HANDLE consoleOut = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
GetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
COLORREF storedBG = sbInfoEx . ColorTable [ 0 ] ;
sbInfoEx . ColorTable [ 0 ] = color ;
SetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
}
//-----------------------------------------------------------------------------
// Purpose: flashes the windows terminal background color
// Input : nFlashCount -
// nFlashInterval - color -
//-----------------------------------------------------------------------------
void FlashConsoleBackground ( int nFlashCount , int nFlashInterval , COLORREF color )
{
CONSOLE_SCREEN_BUFFER_INFOEX sbInfoEx { } ;
sbInfoEx . cbSize = sizeof ( CONSOLE_SCREEN_BUFFER_INFOEX ) ;
HANDLE consoleOut = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
GetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
COLORREF storedBG = sbInfoEx . ColorTable [ 0 ] ;
2021-04-13 04:45:22 -07:00
2022-01-16 01:18:36 +01:00
for ( int i = 0 ; i < nFlashCount ; + + i )
{
//-- set BG color
Sleep ( nFlashInterval ) ;
sbInfoEx . ColorTable [ 0 ] = color ;
SetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
//-- restore previous color
Sleep ( nFlashInterval ) ;
sbInfoEx . ColorTable [ 0 ] = storedBG ;
SetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
}
}
//-----------------------------------------------------------------------------
// Purpose: terminal window setup
//-----------------------------------------------------------------------------
2021-12-25 22:36:38 +01:00
void Console_Init ( )
2021-04-13 04:45:22 -07:00
{
2021-06-28 15:51:32 -07:00
///////////////////////////////////////////////////////////////////////////
2021-04-13 04:45:22 -07:00
// Create the console window
2021-12-25 22:36:38 +01:00
if ( AllocConsole ( ) = = FALSE )
2021-04-13 04:45:22 -07:00
{
2021-04-16 14:15:29 +02:00
OutputDebugString ( " Failed to create console window! \n " ) ;
2021-04-13 04:45:22 -07:00
return ;
}
2022-01-16 01:18:36 +01:00
//-- Set the window title
2021-04-17 04:51:04 -07:00
FILE * sBuildTxt ;
2021-04-25 14:36:55 -07:00
CHAR sBuildBuf [ 1024 ] = { 0 } ;
2021-06-08 10:54:49 -07:00
2021-06-19 07:18:39 -07:00
fopen_s ( & sBuildTxt , " build.txt " , " r " ) ;
2021-12-25 22:36:38 +01:00
2021-04-16 14:12:39 +02:00
if ( sBuildTxt )
{
2021-04-16 11:06:20 -07:00
while ( fgets ( sBuildBuf , sizeof ( sBuildBuf ) , sBuildTxt ) ! = NULL )
2021-04-25 14:36:55 -07:00
{
2021-04-16 14:12:39 +02:00
fclose ( sBuildTxt ) ;
2021-04-25 14:36:55 -07:00
}
2021-04-16 14:12:39 +02:00
}
2021-04-16 11:06:20 -07:00
SetConsoleTitle ( sBuildBuf ) ;
2021-04-13 04:45:22 -07:00
2022-01-16 01:18:36 +01:00
//-- Open input/output streams
2021-04-13 04:45:22 -07:00
FILE * fDummy ;
2021-11-05 00:57:52 +01:00
freopen_s ( & fDummy , " CONIN$ " , " r " , stdin ) ;
2021-04-13 04:45:22 -07:00
freopen_s ( & fDummy , " CONOUT$ " , " w " , stdout ) ;
freopen_s ( & fDummy , " CONOUT$ " , " w " , stderr ) ;
2022-01-16 01:18:36 +01:00
//-- Create a worker thread to process console commands
2022-01-14 20:45:36 +01:00
DWORD dwMode = NULL ;
DWORD dwThreadId = NULL ;
2021-12-25 22:36:38 +01:00
DWORD __stdcall ProcessConsoleWorker ( LPVOID ) ;
2022-01-14 20:45:36 +01:00
HANDLE hThread = CreateThread ( NULL , 0 , ProcessConsoleWorker , NULL , 0 , & dwThreadId ) ;
2021-10-05 00:25:58 +02:00
2022-01-16 01:18:36 +01:00
HANDLE hInput = GetStdHandle ( STD_INPUT_HANDLE ) ;
2022-01-14 20:45:36 +01:00
HANDLE hOutput = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
2021-12-25 22:36:38 +01:00
2021-11-05 00:57:52 +01:00
if ( hThread )
2021-04-17 04:51:04 -07:00
{
2021-11-05 00:57:52 +01:00
CloseHandle ( hThread ) ;
2021-04-17 04:51:04 -07:00
}
2022-01-14 20:45:36 +01:00
if ( strstr ( GetCommandLineA ( ) , " -ansiclr " ) )
{
GetConsoleMode ( hOutput , & dwMode ) ;
dwMode | = ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING ;
if ( ! SetConsoleMode ( hOutput , dwMode ) ) // Some editions of Windows have 'VirtualTerminalLevel' disabled by default.
{
// Warn the user if 'VirtualTerminalLevel' could not be set on users environment.
2022-01-16 01:18:36 +01:00
MessageBox ( NULL , " Failed to set console mode 'VirtualTerminalLevel'. \n Please omit the '-ansiclr' parameter and restart \n the game if output logging appears distorted. " , " SDK Warning " , MB_ICONEXCLAMATION | MB_OK ) ;
2022-01-14 20:45:36 +01:00
}
2022-01-17 23:20:32 +01:00
SetConsoleBackgroundColor ( 0x0000 ) ;
2022-01-16 01:18:36 +01:00
AnsiColors_Init ( ) ;
2022-01-14 20:45:36 +01:00
}
2021-12-25 22:36:38 +01:00
}
//#############################################################################
// WORKER THREAD
//#############################################################################
DWORD __stdcall ProcessConsoleWorker ( LPVOID )
{
// Loop forever
while ( true )
{
static std : : string sCommand = " " ;
2022-01-16 01:18:36 +01:00
//printf("] ");
//-- Get the user input on the debug console
2021-12-25 22:36:38 +01:00
std : : getline ( std : : cin , sCommand ) ;
2022-01-16 01:18:36 +01:00
//-- Debug toggles
2021-12-31 03:41:33 +01:00
if ( sCommand = = " pattern test " ) { PrintHAddress ( ) ; continue ; }
2021-12-25 22:36:38 +01:00
if ( sCommand = = " opcodes test " ) { RuntimePtc_Toggle ( ) ; continue ; }
2022-01-16 01:18:36 +01:00
2021-12-25 22:36:38 +01:00
// Execute the command in the r5 SQVM
IVEngineClient_CommandExecute ( NULL , sCommand . c_str ( ) ) ;
sCommand . clear ( ) ;
///////////////////////////////////////////////////////////////////////
// Sleep and loop
Sleep ( 50 ) ;
}
return NULL ;
}