diff --git a/src/client/component/logger.cpp b/src/client/component/logger.cpp new file mode 100644 index 00000000..361ac339 --- /dev/null +++ b/src/client/component/logger.cpp @@ -0,0 +1,180 @@ +#include +#include "loader/component_loader.hpp" + +#include "party.hpp" +#include "console.hpp" + +#include "game/game.hpp" +#include "game/dvars.hpp" + +#include + +namespace logger +{ + namespace + { + utils::hook::detour com_error_hook; + + 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); + + console::error(buffer); + } + + 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); + + console::error(buffer); + } + + 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); + + console::error("Error: %s\n", buffer); + } + + party::clear_sv_motd(); // clear sv_motd on error if it exists + + com_error_hook.invoke(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); + + console::warn(buffer); + } + + 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); + + console::info(buffer); + } + + void print_dev(const char* msg, ...) + { + static auto* enabled = dvars::register_bool("logger_dev", false, game::DVAR_FLAG_SAVED, true); + if (!enabled->current.enabled) + { + return; + } + + char buffer[2048]; + + va_list ap; + va_start(ap, msg); + + vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap); + + va_end(ap); + + console::info(buffer); + } + + void lui_error() + { + utils::hook::call(0x140162809, print_warning); + utils::hook::call(0x140162815, print_warning); + utils::hook::call(0x14016281D, print_warning); + utils::hook::call(0x140162829, print_warning); + + utils::hook::call(0x140162E32, print_warning); + utils::hook::call(0x140162E3E, print_warning); + utils::hook::call(0x140162E46, print_warning); + utils::hook::call(0x140162E52, print_warning); + + utils::hook::call(0x140168435, print_warning); + utils::hook::call(0x140168441, print_warning); + utils::hook::call(0x140168449, print_warning); + utils::hook::call(0x140168455, print_warning); + + utils::hook::call(0x14016914D, print_warning); + utils::hook::call(0x140169161, print_warning); + + utils::hook::call(0x140169C04, print_warning); + utils::hook::call(0x140169C0C, print_warning); + utils::hook::call(0x140169C18, print_warning); + + utils::hook::call(0x140169CB7, print_warning); + utils::hook::call(0x140169CDE, print_warning); + utils::hook::call(0x140169CEA, print_warning); + utils::hook::call(0x140169D03, print_warning); + + utils::hook::call(0x14016BE72, print_warning); + utils::hook::call(0x14016C020, print_warning); + } + + void lui_interface_debug_print() + { + utils::hook::call(0x14015C0B2, print_warning); + utils::hook::call(0x140162453, print_warning); + utils::hook::call(0x1401625DF, print_warning); + utils::hook::call(0x14016713C, print_dev); + utils::hook::call(0x1401687CD, print_dev); + utils::hook::call(0x14016BB8A, print_dev); + utils::hook::call(0x14016CA9C, print_dev); + } + } + + class component final : public component_interface + { + public: + void post_unpack() override + { + if (game::environment::is_mp()) + { + lui_error(); + lui_interface_debug_print(); + } + + if (!game::environment::is_sp()) + { + utils::hook::call(0x14051347F, print_com_error); + } + + com_error_hook.create(game::Com_Error, com_error_stub); + } + }; +} + +REGISTER_COMPONENT(logger::component) diff --git a/src/client/component/party.cpp b/src/client/component/party.cpp index 0605afad..38dd7277 100644 --- a/src/client/component/party.cpp +++ b/src/client/component/party.cpp @@ -142,7 +142,7 @@ namespace party void cl_disconnect_stub(int a1) { - party::sv_motd.clear(); + party::clear_sv_motd(); cldisconnect_hook.invoke(a1); } @@ -160,6 +160,11 @@ namespace party } } + void clear_sv_motd() + { + party::sv_motd.clear(); + } + int get_client_num_by_name(const std::string& name) { for (auto i = 0; !name.empty() && i < *game::mp::svs_numclients; ++i) diff --git a/src/client/component/party.hpp b/src/client/component/party.hpp index cd90ae9f..0feb2e98 100644 --- a/src/client/component/party.hpp +++ b/src/client/component/party.hpp @@ -8,6 +8,7 @@ namespace party void connect(const game::netadr_s& target); void start_map(const std::string& mapname); + void clear_sv_motd(); int server_client_count(); int get_client_num_by_name(const std::string& name);