t7x/src/client/component/console.cpp

96 lines
1.8 KiB
C++
Raw Normal View History

2022-05-21 06:04:08 -04:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include <utils/thread.hpp>
#include <utils/hook.hpp>
namespace console
{
2022-05-23 11:57:45 -04:00
namespace
{
2022-05-30 14:37:58 -04:00
volatile bool g_started = false;
2022-05-23 11:57:45 -04:00
void create_game_console()
{
2022-05-27 13:08:39 -04:00
reinterpret_cast<void(*)()>(0x142333F80_g)();
2022-05-23 11:57:45 -04:00
}
2022-05-30 14:37:58 -04:00
void print_message(const char* message)
{
if (g_started)
{
reinterpret_cast<void(*)(int, int, const char*, ...)>(0x1421499C0_g)(0, 0, "%s", message);
}
}
void print_stub(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char buffer[1024]{0};
const int res = vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, fmt, ap);
print_message(buffer);
va_end(ap);
}
2022-05-23 11:57:45 -04:00
}
2022-05-21 06:04:08 -04:00
class component final : public component_interface
{
public:
void post_unpack() override
{
2022-05-30 14:37:58 -04:00
utils::hook::jump(printf, print_stub);
2022-05-21 06:04:08 -04:00
this->terminate_runner_ = false;
this->console_runner_ = utils::thread::create_named_thread("Console IO", [this]
{
2022-05-27 13:08:39 -04:00
{
utils::hook::detour d;
d.create(0x142333B40_g, utils::hook::assemble([](utils::hook::assembler& a)
{
a.mov(r8, "BOIII Console");
a.mov(r9d, 0x80CA0000);
a.sub(eax, edx);
a.jmp(0x142333B4F_g);
}));
2022-05-27 13:08:39 -04:00
create_game_console();
2022-05-30 14:37:58 -04:00
g_started = true;
2022-05-27 13:08:39 -04:00
}
2022-05-23 11:57:45 -04:00
MSG msg{};
while (!this->terminate_runner_)
{
2022-05-29 12:51:24 -04:00
if (PeekMessageW(&msg, nullptr, NULL, NULL, PM_REMOVE))
2022-05-23 11:57:45 -04:00
{
TranslateMessage(&msg);
2022-05-29 12:51:24 -04:00
DispatchMessageW(&msg);
2022-05-23 11:57:45 -04:00
}
else
{
std::this_thread::sleep_for(1ms);
}
}
2022-05-21 06:04:08 -04:00
});
}
void pre_destroy() override
{
this->terminate_runner_ = true;
if (this->console_runner_.joinable())
{
this->console_runner_.join();
}
}
private:
std::atomic_bool terminate_runner_{false};
std::thread console_runner_;
};
}
REGISTER_COMPONENT(console::component)