From c01043cfb17407280c01e34722bae62b26d5057c Mon Sep 17 00:00:00 2001 From: Andreas Maerten <24669514+Yimura@users.noreply.github.com> Date: Sun, 26 Nov 2023 21:59:01 +0100 Subject: [PATCH] refactor(Metrics): filter out "bad" metrics and allow normal ones to be sent (#2453) --- src/gta/json_serializer.hpp | 36 ++++++++ src/hooking.hpp | 4 +- src/hooks/info/prepare_metric_for_sending.cpp | 88 +++++++++++-------- 3 files changed, 90 insertions(+), 38 deletions(-) create mode 100644 src/gta/json_serializer.hpp diff --git a/src/gta/json_serializer.hpp b/src/gta/json_serializer.hpp new file mode 100644 index 00000000..b2fb56c1 --- /dev/null +++ b/src/gta/json_serializer.hpp @@ -0,0 +1,36 @@ +#pragma once + +namespace rage +{ + #pragma pack(push, 1) + class json_serializer + { + uint32_t unk0; // 0x00 + uint32_t unk1; // 0x00 + char* buffer; // 0x08 + uint32_t curlen; // 0x10 + uint32_t maxlen; // 0x14 + uint32_t unk4; // 0x18 + uint8_t flags; // 0x1C + + public: + json_serializer(char* _buffer, uint32_t _length) : + buffer(_buffer), + maxlen(_length) + { + unk0 + = 0; + unk1 = 0; + curlen = 0; + unk4 = 1; + flags = 0; + } + + inline char* get_string() const + { + return buffer; + } + }; + static_assert(sizeof(json_serializer) == 0x1D); // size is actually 0x20 + #pragma pack(pop) +} \ No newline at end of file diff --git a/src/hooking.hpp b/src/hooking.hpp index 8453f64b..60eaea5c 100644 --- a/src/hooking.hpp +++ b/src/hooking.hpp @@ -5,6 +5,7 @@ #include "gta/enums.hpp" #include "gta/fwddec.hpp" #include "gta/script_thread.hpp" +#include "gta/json_serializer.hpp" #include "vmt_hook.hpp" #include "vtable_hook.hpp" #include "call_hook.hpp" @@ -128,7 +129,8 @@ namespace big static int nt_query_virtual_memory(void* _this, HANDLE handle, PVOID base_addr, int info_class, MEMORY_BASIC_INFORMATION* info, int size, size_t* return_len); static int queue_dependency(void* a1, int a2, void* dependency); - static bool prepare_metric_for_sending(rage::datBitBuffer* bit_buffer, int unk, int time, rage::rlMetric* metric); + + static bool prepare_metric_for_sending(rage::json_serializer* bit_buffer, int unk, int time, rage::rlMetric* metric); static bool http_start_request(void* request, const char* uri); static bool received_array_update(rage::netArrayHandlerBase* array, CNetGamePlayer* sender, rage::datBitBuffer* buffer, int size, int16_t cycle); diff --git a/src/hooks/info/prepare_metric_for_sending.cpp b/src/hooks/info/prepare_metric_for_sending.cpp index 975e72a9..1b900253 100644 --- a/src/hooks/info/prepare_metric_for_sending.cpp +++ b/src/hooks/info/prepare_metric_for_sending.cpp @@ -3,50 +3,64 @@ namespace big { -#pragma pack(push, 1) - class json_serializer + const auto bad_metrics = std::unordered_set({ + "REPORTER", + "REPORT_INVALIDMODEL", + "MEM_NEW", + "DEBUGGER_ATTACH", + "DIG", + "XP_LOSS", + "AWARD_XP", + "CF", + "CC", + "CNR", + "SCRIPT", + "CHEAT", + "AUX_DEUX", + "WEATHER", + "HARDWARE_OS", + "HARDWARE_GPU", + "HARDWARE_MOBO", + "HARDWARE_MEM", + "HARDWARE_CPU", + "PCSETTINGS", + "CASH_CREATED", + "DR_PS", + "UVC", + "W_L", + "ESVCS", + "IDLEKICK", + "GSCB", + "GSINV", + "GSCW", + "GSINT", + "EARN", + "GARAGE_TAMPER", + "LAST_VEH", + "FAIL_SERV", + "CCF_UPDATE", + "CODE_CRC", + "COLLECTIBLE", + "FIRST_VEH", + }); + + bool hooks::prepare_metric_for_sending(rage::json_serializer* serializer, int unk, int time, rage::rlMetric* metric) { - uint32_t unk0; // 0x00 - uint32_t unk1; // 0x00 - char* buffer; // 0x08 - uint32_t curlen;// 0x10 - uint32_t maxlen;// 0x14 - uint32_t unk4; // 0x18 - uint8_t flags; // 0x1C + const auto ret = g_hooking->get_original()(serializer, unk, time, metric); - public: - json_serializer(char* _buffer, uint32_t _length) : - buffer(_buffer), - maxlen(_length) + const auto is_bad_metric = bad_metrics.contains(metric->get_name()); + if (is_bad_metric) { - unk0 - = 0; - unk1 = 0; - curlen = 0; - unk4 = 1; - flags = 0; + LOG(WARNING) << "BAD METRIC: " << metric->get_name() << "; DATA: " << serializer->get_string(); + + return false; } - inline char* get_string() const + if (!is_bad_metric && g.debug.logs.metric_logs) { - return buffer; - } - }; - static_assert(sizeof(json_serializer) == 0x1D);// size is actually 0x20 -#pragma pack(pop) - - bool hooks::prepare_metric_for_sending(rage::datBitBuffer* bit_buffer, int unk, int time, rage::rlMetric* metric) - { - if (g.debug.logs.metric_logs) - { - char buffer[256]{}; - json_serializer serializer(buffer, sizeof(buffer)); - - metric->serialize(&serializer); - - LOG(INFO) << "METRIC: " << metric->get_name() << "; DATA: " << serializer.get_string(); + LOG(INFO) << "METRIC: " << metric->get_name() << "; DATA: " << serializer->get_string(); } - return false; + return ret; } }