h1-mod/src/client/component/console.cpp

88 lines
1.5 KiB
C++
Raw Normal View History

2022-02-03 14:05:24 -05:00
#include <std_include.hpp>
#include "console.hpp"
#include "loader/component_loader.hpp"
2022-02-03 14:05:24 -05:00
#include "game/game.hpp"
#include "command.hpp"
2022-05-19 15:45:00 -04:00
#include <utils/thread.hpp>
2022-05-19 18:05:51 -04:00
#include <utils/hook.hpp>
2022-05-19 15:45:00 -04:00
2022-02-03 14:05:24 -05:00
namespace game_console
{
void print(int type, const std::string& data);
}
namespace console
{
namespace
{
2022-05-19 15:45:00 -04:00
bool kill = false;
std::thread console_thread;
2022-02-03 14:05:24 -05:00
}
std::string format(va_list* ap, const char* message)
2022-02-03 14:05:24 -05:00
{
static thread_local char buffer[0x1000];
2022-02-03 14:05:24 -05:00
const auto count = _vsnprintf_s(buffer, sizeof(buffer), sizeof(buffer), message, *ap);
2022-02-03 14:05:24 -05:00
if (count < 0) return {};
2022-05-19 15:45:00 -04:00
return {buffer, static_cast<size_t>(count)};
2022-02-03 14:05:24 -05:00
}
void dispatch_message(const int type, const std::string& message)
2022-02-03 14:05:24 -05:00
{
2022-05-20 03:51:17 -04:00
printf("%s", message.data());
2022-05-19 18:05:51 -04:00
game_console::print(type, message);
2022-02-03 14:05:24 -05:00
}
void print(const int type, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
const auto result = format(&ap, fmt);
va_end(ap);
dispatch_message(type, result);
}
class component final : public component_interface
{
public:
2022-05-19 15:45:00 -04:00
component()
{
2022-05-19 15:45:00 -04:00
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
2022-05-18 07:35:39 -04:00
void post_unpack() override
{
2022-05-19 15:45:00 -04:00
ShowWindow(GetConsoleWindow(), SW_SHOW);
SetConsoleTitle("H1-Mod");
console_thread = utils::thread::create_named_thread("Console", []()
{
std::string cmd;
while (!kill)
{
std::getline(std::cin, cmd);
game::Cbuf_AddText(0, 0, cmd.data());
}
});
2022-05-18 07:35:39 -04:00
}
void pre_destroy() override
{
2022-05-19 15:45:00 -04:00
kill = true;
if (console_thread.joinable())
{
console_thread.join();
}
2022-05-18 07:35:39 -04:00
}
};
2022-02-03 14:05:24 -05:00
}
REGISTER_COMPONENT(console::component)