aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/sourceconsole.cpp
diff options
context:
space:
mode:
authorBobTheBob <32057864+BobTheBob9@users.noreply.github.com>2022-08-24 00:32:31 +0100
committerGeckoEidechse <gecko.eidechse+git@pm.me>2022-08-25 21:32:55 +0200
commit46bed8d09f32360377350d71816a76abdfbf2c7c (patch)
tree7feedae4910dbfe0e53d26ea1e00c4616d2c1850 /NorthstarDLL/sourceconsole.cpp
parenta97f1f07bb5aa31629cd87023e09669eb4bf1fe9 (diff)
downloadNorthstarLauncher-46bed8d09f32360377350d71816a76abdfbf2c7c.tar.gz
NorthstarLauncher-46bed8d09f32360377350d71816a76abdfbf2c7c.zip
Adjust folder structure (#242)v1.9.5-rc1v1.9.5
* Adjust folder structure * change launcher directory name
Diffstat (limited to 'NorthstarDLL/sourceconsole.cpp')
-rw-r--r--NorthstarDLL/sourceconsole.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/NorthstarDLL/sourceconsole.cpp b/NorthstarDLL/sourceconsole.cpp
new file mode 100644
index 00000000..a6d54989
--- /dev/null
+++ b/NorthstarDLL/sourceconsole.cpp
@@ -0,0 +1,80 @@
+#include "pch.h"
+#include "convar.h"
+#include "sourceconsole.h"
+#include "sourceinterface.h"
+#include "concommand.h"
+#include "hookutils.h"
+
+SourceInterface<CGameConsole>* g_SourceGameConsole;
+
+void ConCommand_toggleconsole(const CCommand& arg)
+{
+ if ((*g_SourceGameConsole)->IsConsoleVisible())
+ (*g_SourceGameConsole)->Hide();
+ else
+ (*g_SourceGameConsole)->Activate();
+}
+
+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<SourceConsoleSink>();
+ 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<LPVOID*>(&onCommandSubmittedOriginal));
+}
+
+void InitialiseSourceConsole(HMODULE baseAddress)
+{
+ g_SourceGameConsole = new SourceInterface<CGameConsole>("client.dll", "GameConsole004");
+ RegisterConCommand("toggleconsole", ConCommand_toggleconsole, "toggles the console", FCVAR_DONTRECORD);
+}
+
+// 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<std::mutex>::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_() {} \ No newline at end of file