r5sdk/r5dev/src/console.cpp

110 lines
3.5 KiB
C++
Raw Normal View History

#include "pch.h"
2021-07-12 08:47:54 -07:00
#include "id3dx.h"
#include "hooks.h"
#include "opcptc.h"
#include "console.h"
#include "patterns.h"
2021-04-13 04:45:22 -07:00
2021-11-05 00:57:52 +01:00
//#############################################################################
// WORKER THREAD
//#############################################################################
DWORD __stdcall ProcessConsoleWorker(LPVOID)
{
while (true) // Loop forever
{
std::string sCommand = std::string();
///////////////////////////////////////////////////////////////////////
// Get the user input on the debug console
std::getline(std::cin, sCommand);
///////////////////////////////////////////////////////////////////////
// Engine toggles
if (sCommand == "toggle net") { Hooks::ToggleNetTrace(); continue; }
if (sCommand == "toggle dev") { Hooks::ToggleDevCommands(); continue; }
if (sCommand == "toggle fal") { g_bReturnAllFalse = !g_bReturnAllFalse; continue; }
///////////////////////////////////////////////////////////////////////
// Debug toggles
if (sCommand == "pattern test") { PrintHAddress(); PrintOAddress(); continue; }
if (sCommand == "directx test") { PrintDXAddress(); continue; }
if (sCommand == "console test") { g_bDebugConsole = !g_bDebugConsole; continue; }
///////////////////////////////////////////////////////////////////////
// Exec toggles
if (sCommand == "1") { Hooks::ToggleDevCommands(); addr_CommandExecute(NULL, "exec autoexec_dev"); }
if (sCommand == "2") { g_bDebugLoading = !g_bDebugLoading; continue; }
///////////////////////////////////////////////////////////////////////
// Execute the command in the r5 SQVM
addr_CommandExecute(NULL, sCommand.c_str());
sCommand.clear();
///////////////////////////////////////////////////////////////////////
// Sleep and loop
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
return 0;
}
2021-06-28 15:51:32 -07:00
//#############################################################################
// INITIALIZATION
//#############################################################################
2021-04-13 04:45:22 -07:00
void SetupConsole()
{
2021-06-28 15:51:32 -07:00
///////////////////////////////////////////////////////////////////////////
2021-04-13 04:45:22 -07:00
// Create the console window
2021-11-05 00:09:49 +01:00
if (!AllocConsole())
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;
}
2021-06-28 15:51:32 -07:00
///////////////////////////////////////////////////////////////////////////
2021-04-13 04:45:22 -07:00
// Set the window title
FILE* sBuildTxt;
CHAR sBuildBuf[1024] = { 0 };
fopen_s(&sBuildTxt, "build.txt", "r");
if (sBuildTxt)
{
2021-04-16 11:06:20 -07:00
while (fgets(sBuildBuf, sizeof(sBuildBuf), sBuildTxt) != NULL)
{
fclose(sBuildTxt);
}
}
2021-04-16 11:06:20 -07:00
SetConsoleTitle(sBuildBuf);
2021-04-13 04:45:22 -07:00
2021-06-28 15:51:32 -07:00
///////////////////////////////////////////////////////////////////////////
2021-04-13 04:45:22 -07:00
// Open input/output streams
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);
2021-06-28 15:51:32 -07:00
///////////////////////////////////////////////////////////////////////////
// Create a worker thread to process console commands
2021-11-05 00:57:52 +01:00
DWORD threadID = NULL;
HANDLE hThread = CreateThread(NULL, 0, ProcessConsoleWorker, NULL, 0, &threadID);
// Initialize global spdlog.
auto console = spdlog::stdout_logger_mt("console");
console->set_pattern("[%I:%M:%S:%e] [%L] %v"); // Set pattern.
spdlog::set_default_logger(console); // Set as default.
spdlog::flush_every(std::chrono::seconds(5)); // Flush buffers every 5 seconds for every logger.
2021-11-05 00:57:52 +01:00
#ifdef _DEBUG
console->set_level(spdlog::level::debug);
#endif
2021-04-13 04:45:22 -07:00
2021-11-05 00:57:52 +01:00
spdlog::debug("Console and spdlog are setup now!\n");
2021-04-13 04:45:22 -07:00
2021-11-05 00:57:52 +01:00
if (hThread)
{
2021-11-05 00:57:52 +01:00
spdlog::info("THREAD ID: {}\n\n", threadID);
CloseHandle(hThread);
}
}