#include "pch.h" #include "hooks.h" #include "convar.h" #include "sourceconsole.h" #include "sourceinterface.h" #include "concommand.h" #include "hookutils.h" SourceInterface* g_SourceGameConsole; void ConCommand_toggleconsole(const CCommand& arg) { if ((*g_SourceGameConsole)->IsConsoleVisible()) (*g_SourceGameConsole)->Hide(); else (*g_SourceGameConsole)->Activate(); } void ConCommand_showconsole(const CCommand& arg) { (*g_SourceGameConsole)->Activate(); } void ConCommand_hideconsole(const CCommand& arg) { (*g_SourceGameConsole)->Hide(); } typedef void (*OnCommandSubmittedType)(CConsoleDialog* consoleDialog, const char* pCommand); OnCommandSubmittedType onCommandSubmittedOriginal; void OnCommandSubmittedHook(CConsoleDialog* consoleDialog, const char* pCommand) { consoleDialog->m_pConsolePanel->Print("] "); consoleDialog->m_pConsolePanel->Print(pCommand); consoleDialog->m_pConsolePanel->Print("\n"); // todo: call the help command in the future onCommandSubmittedOriginal(consoleDialog, pCommand); } // called from sourceinterface.cpp in client createinterface hooks, on GameClientExports001 void InitialiseConsoleOnInterfaceCreation() { (*g_SourceGameConsole)->Initialize(); auto consoleLogger = std::make_shared(); consoleLogger->set_pattern("[%l] %v"); spdlog::default_logger()->sinks().push_back(consoleLogger); // hook OnCommandSubmitted so we print inputted commands HookEnabler hook; ENABLER_CREATEHOOK( hook, (void*)((*g_SourceGameConsole)->m_pConsole->m_vtable->OnCommandSubmitted), &OnCommandSubmittedHook, reinterpret_cast(&onCommandSubmittedOriginal)); } // logging stuff SourceConsoleSink::SourceConsoleSink() { logColours.emplace(spdlog::level::trace, SourceColor(0, 255, 255, 255)); logColours.emplace(spdlog::level::debug, SourceColor(0, 255, 255, 255)); logColours.emplace(spdlog::level::info, SourceColor(255, 255, 255, 255)); logColours.emplace(spdlog::level::warn, SourceColor(255, 255, 0, 255)); logColours.emplace(spdlog::level::err, SourceColor(255, 0, 0, 255)); logColours.emplace(spdlog::level::critical, SourceColor(255, 0, 0, 255)); logColours.emplace(spdlog::level::off, SourceColor(0, 0, 0, 0)); } void SourceConsoleSink::sink_it_(const spdlog::details::log_msg& msg) { if (!(*g_SourceGameConsole)->m_bInitialized) return; spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); (*g_SourceGameConsole) ->m_pConsole->m_pConsolePanel->ColorPrint(logColours[msg.level], fmt::to_string(formatted).c_str()); // todo needs colour support } void SourceConsoleSink::flush_() {} ON_DLL_LOAD_CLIENT_RELIESON("client.dll", SourceConsole, ConCommand, (HMODULE baseAddress) { g_SourceGameConsole = new SourceInterface("client.dll", "GameConsole004"); RegisterConCommand("toggleconsole", ConCommand_toggleconsole, "toggles the console", FCVAR_DONTRECORD); RegisterConCommand("showconsole", ConCommand_showconsole, "shows the console", FCVAR_DONTRECORD); RegisterConCommand("hideconsole", ConCommand_hideconsole, "hides the console", FCVAR_DONTRECORD); })