h2-mod/src/client/component/logger.cpp

171 lines
3.0 KiB
C++
Raw Normal View History

2022-01-30 18:23:42 -05:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
2022-06-17 14:22:12 -04:00
#include "console.hpp"
2022-07-18 13:20:09 -04:00
#include "loadscreen.hpp"
#include "game/game.hpp"
2022-01-30 18:23:42 -05:00
#include "game/dvars.hpp"
#include <utils/hook.hpp>
namespace logger
{
namespace
{
utils::hook::detour com_error_hook;
utils::hook::detour nullsub_48_hook;
utils::hook::detour sub_32AEF0;
game::dvar_t* logger_dev = nullptr;
2022-01-30 18:23:42 -05:00
void print_error(const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
2022-06-17 14:22:12 -04:00
console::error("%s", buffer);
2022-01-30 18:23:42 -05:00
}
void print_com_error(int, const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
2022-06-17 14:22:12 -04:00
console::error("%s", buffer);
2022-01-30 18:23:42 -05:00
}
void com_error_stub(const int error, const char* msg, ...)
{
char buffer[2048];
{
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
2022-06-17 14:22:12 -04:00
console::error("Error: %s\n", buffer);
2022-01-30 18:23:42 -05:00
}
2022-07-18 13:20:09 -04:00
loadscreen::clear();
2022-01-30 18:23:42 -05:00
com_error_hook.invoke<void>(error, "%s", buffer);
}
void print_warning(const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
2022-06-17 14:22:12 -04:00
console::warn("%s", buffer);
2022-01-30 18:23:42 -05:00
}
void print(const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
2022-06-17 14:22:12 -04:00
console::info("%s", buffer);
2022-01-30 18:23:42 -05:00
}
void print_dev(const char* msg, ...)
{
if (!logger_dev->current.enabled)
2022-01-30 18:23:42 -05:00
{
return;
}
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
2022-06-17 14:22:12 -04:00
console::info("%s", buffer);
2022-01-30 18:23:42 -05:00
}
void lui_print(const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
if (strstr(msg, "LUI WARNING:"))
{
2022-06-17 14:22:12 -04:00
console::warn("%s", buffer);
2022-01-30 18:23:42 -05:00
}
else
{
if (!logger_dev->current.enabled)
2022-01-30 18:23:42 -05:00
{
return;
}
2022-06-17 14:22:12 -04:00
console::info("%s", buffer);
2022-01-30 18:23:42 -05:00
}
}
2022-06-18 14:01:21 -04:00
void r_warn_once_per_frame_vsnprintf_stub(char* buffer, size_t buffer_length, char* msg, va_list va)
{
vsnprintf(buffer, buffer_length, msg, va);
console::warn(buffer);
}
2022-01-30 18:23:42 -05:00
}
class component final : public component_interface
{
public:
void post_unpack() override
{
2022-03-18 17:02:44 -04:00
utils::hook::jump(0x14032C620, print_warning, true);
utils::hook::jump(0x14032C630, print_warning, true);
2022-06-18 14:01:21 -04:00
2022-03-18 17:02:44 -04:00
utils::hook::jump(0x14032AEF0, lui_print, true);
com_error_hook.create(0x1405A2D80, com_error_stub);
2022-06-18 14:01:21 -04:00
2022-04-29 15:51:25 -04:00
utils::hook::jump(0x14013A98C, print);
2022-06-18 14:01:21 -04:00
utils::hook::call(0x140791A01, r_warn_once_per_frame_vsnprintf_stub);
logger_dev = dvars::register_bool("logger_dev", false, game::DVAR_FLAG_SAVED, "Print dev stuff");
2022-01-30 18:23:42 -05:00
}
};
}
REGISTER_COMPONENT(logger::component)