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

87 lines
1.4 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"
namespace game_console
{
void print(int type, const std::string& data);
}
namespace console
{
namespace
{
2022-05-18 07:35:39 -04:00
static bool ingame = false;
DWORD WINAPI console(LPVOID)
2022-02-28 20:39:22 -05:00
{
2022-05-18 04:26:48 -04:00
ShowWindow(GetConsoleWindow(), SW_SHOW);
SetConsoleTitle("H1-Mod");
2022-02-03 14:05:24 -05:00
std::string cmd;
2022-02-03 14:05:24 -05:00
while (true)
2022-02-03 14:05:24 -05:00
{
std::getline(std::cin, cmd);
2022-05-18 07:35:39 -04:00
if (ingame)
{
game::Cbuf_AddText(0, 0, cmd.data());
}
2022-02-28 20:39:22 -05:00
}
return 0;
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 {};
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
{
printf("%s\n", message.data());
//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:
void post_start() override
{
CreateThread(0, 0, console, 0, 0, 0);
}
2022-05-18 07:35:39 -04:00
void post_unpack() override
{
ingame = true;
}
void pre_destroy() override
{
ingame = false;
}
};
2022-02-03 14:05:24 -05:00
}
REGISTER_COMPONENT(console::component)