2022-01-16 01:18:36 +01:00
|
|
|
//=============================================================================//
|
|
|
|
//
|
|
|
|
// Purpose: Windows terminal utilities
|
|
|
|
//
|
|
|
|
//=============================================================================//
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
#include "core/stdafx.h"
|
2023-03-27 02:01:48 +02:00
|
|
|
#ifndef NETCONSOLE
|
2021-12-25 22:36:38 +01:00
|
|
|
#include "core/init.h"
|
2022-01-14 20:45:36 +01:00
|
|
|
#include "core/logdef.h"
|
2022-08-19 21:33:31 +02:00
|
|
|
#include "tier0/frametask.h"
|
2023-05-10 00:05:38 +02:00
|
|
|
#include "engine/cmd.h"
|
2021-12-25 22:36:38 +01:00
|
|
|
#ifndef DEDICATED
|
|
|
|
#include "windows/id3dx.h"
|
|
|
|
#endif // !DEDICATED
|
2023-03-27 02:01:48 +02:00
|
|
|
#endif // !NETCONSOLE
|
2022-07-26 00:36:35 +02:00
|
|
|
#include "windows/system.h"
|
2021-12-25 22:36:38 +01:00
|
|
|
#include "windows/console.h"
|
2021-11-05 00:57:52 +01:00
|
|
|
|
2023-03-15 21:32:08 +01:00
|
|
|
static std::string s_ConsoleInput;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
sbInfoEx.ColorTable[0] = color;
|
|
|
|
SetConsoleScreenBufferInfoEx(consoleOut, &sbInfoEx);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: flashes the windows terminal background color
|
|
|
|
// Input : nFlashCount -
|
2023-03-15 21:32:08 +01:00
|
|
|
// nFlashInterval -
|
|
|
|
// color -
|
2022-01-16 01:18:36 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
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
|
|
|
{
|
2023-03-27 02:01:48 +02:00
|
|
|
#ifndef NETCONSOLE
|
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
|
|
|
{
|
2022-03-04 12:22:17 +01:00
|
|
|
OutputDebugStringA("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
|
2022-03-26 01:23:06 +01:00
|
|
|
SetConsoleTitleA("R5");
|
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 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-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
|
|
|
}
|
2023-03-27 02:01:48 +02:00
|
|
|
#endif // !NETCONSOLE
|
|
|
|
|
|
|
|
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
DWORD dwMode = NULL;
|
2022-01-14 20:45:36 +01:00
|
|
|
|
2023-03-27 02:01:48 +02:00
|
|
|
if (g_svCmdLine.find("-ansicolor") != string::npos)
|
2022-01-14 20:45:36 +01:00
|
|
|
{
|
|
|
|
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-03-16 02:03:06 +01:00
|
|
|
MessageBoxA(NULL, "Failed to set console mode 'VirtualTerminalLevel'.\n"
|
2023-03-27 02:01:48 +02:00
|
|
|
"Please omit the '-ansicolor' parameter and restart \nthe program if output logging appears distorted.", "SDK Warning", MB_ICONEXCLAMATION | MB_OK);
|
2022-01-14 20:45:36 +01:00
|
|
|
}
|
2023-03-15 21:11:20 +01:00
|
|
|
|
2023-02-18 15:49:04 +01:00
|
|
|
SetConsoleBackgroundColor(0x00000000);
|
2022-01-16 01:18:36 +01:00
|
|
|
AnsiColors_Init();
|
2022-01-14 20:45:36 +01:00
|
|
|
}
|
2023-03-27 02:01:48 +02:00
|
|
|
|
|
|
|
#ifndef NETCONSOLE
|
2022-07-26 00:36:35 +02:00
|
|
|
SetConsoleCtrlHandler(ConsoleHandlerRoutine, true);
|
2023-03-27 02:01:48 +02:00
|
|
|
#endif // !NETCONSOLE
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2023-03-18 14:10:29 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: terminal window shutdown
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void Console_Shutdown()
|
|
|
|
{
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Destroy the console window
|
|
|
|
if (FreeConsole() == FALSE)
|
|
|
|
{
|
|
|
|
OutputDebugStringA("Failed to destroy console window!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 02:01:48 +02:00
|
|
|
#ifndef NETCONSOLE
|
2021-12-25 22:36:38 +01:00
|
|
|
//#############################################################################
|
2023-03-27 02:01:48 +02:00
|
|
|
// CONSOLE WORKER
|
2021-12-25 22:36:38 +01:00
|
|
|
//#############################################################################
|
|
|
|
DWORD __stdcall ProcessConsoleWorker(LPVOID)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2022-01-16 01:18:36 +01:00
|
|
|
//printf("] ");
|
|
|
|
//-- Get the user input on the debug console
|
2023-03-15 21:32:08 +01:00
|
|
|
std::getline(std::cin, s_ConsoleInput);
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-07-26 03:00:51 +02:00
|
|
|
// Execute the command.
|
2023-03-15 21:32:08 +01:00
|
|
|
Cbuf_AddText(Cbuf_GetCurrentPlayer(), s_ConsoleInput.c_str(), cmd_source_t::kCommandSrcCode);
|
2022-03-25 13:17:57 +01:00
|
|
|
|
2023-03-15 21:32:08 +01:00
|
|
|
if (!s_ConsoleInput.empty())
|
|
|
|
s_ConsoleInput.clear();
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
|
|
Sleep(50);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-03-27 02:01:48 +02:00
|
|
|
#endif // !NETCONSOLE
|