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

156 lines
2.7 KiB
C++
Raw Normal View History

2022-01-30 18:23:42 -05:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/game.hpp"
2022-06-17 14:22:12 -04:00
#include "console.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
}
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
}
}
}
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);
utils::hook::jump(0x14032AEF0, lui_print, true);
com_error_hook.create(0x1405A2D80, com_error_stub);
2022-04-29 15:51:25 -04:00
utils::hook::jump(0x14013A98C, print);
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)