aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/sourceconsole.cpp
blob: 88d038df3dbe50d51c3a958eadbd58661c73f5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "pch.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();
}

void ConCommand_help(const CCommand& arg)
{
	// todo: this should basically just call FindConVar once we have that working, then just print convar.GetHelpString
}

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_NONE);
}


// 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_() {}