Add custom text service (#370)

This commit is contained in:
Yimura 2022-07-29 14:32:02 +02:00 committed by GitHub
parent dbe3777b12
commit 400ba48fa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 254 additions and 119 deletions

View File

@ -23,6 +23,9 @@ namespace big
// Script Hook
m_run_script_threads_hook("SH", g_pointers->m_run_script_threads, &hooks::run_script_threads),
// Get Label Text
m_get_label_text("GLT", g_pointers->m_get_label_text, &hooks::get_label_text),
// GTA Thead Start
m_gta_thread_start_hook("GTS", g_pointers->m_gta_thread_start, &hooks::gta_thread_start),
// GTA Thread Kill
@ -77,6 +80,8 @@ namespace big
m_run_script_threads_hook.enable();
m_get_label_text.enable();
m_gta_thread_start_hook.enable();
m_gta_thread_kill_hook.enable();
@ -124,6 +129,8 @@ namespace big
m_gta_thread_kill_hook.disable();
m_gta_thread_start_hook.disable();
m_get_label_text.disable();
m_run_script_threads_hook.disable();
SetWindowLongPtrW(g_pointers->m_hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(m_og_wndproc));

View File

@ -21,6 +21,8 @@ namespace big
static LRESULT wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
static const char* get_label_text(void* unk, const char* label);
static GtaThread* gta_thread_start(unsigned int** a1, unsigned int a2);
static rage::eThreadState gta_thread_kill(GtaThread* thread);
@ -83,6 +85,8 @@ namespace big
detour_hook m_run_script_threads_hook;
detour_hook m_get_label_text;
detour_hook m_gta_thread_start_hook;
detour_hook m_gta_thread_kill_hook;

View File

@ -0,0 +1,13 @@
#include "hooking.hpp"
#include "services/custom_text/custom_text_service.hpp"
namespace big
{
const char* hooks::get_label_text(void* unk, const char* label)
{
if (const auto text = g_custom_text_service->get_text(label); text)
return text;
return g_hooking->m_get_label_text.get_original<decltype(&get_label_text)>()(unk, label);
}
}

View File

@ -12,6 +12,7 @@
#include "backend/backend.hpp"
#include "native_hooks/native_hooks.hpp"
#include "services/context_menu/context_menu_service.hpp"
#include "services/custom_text/custom_text_service.hpp"
#include "services/globals/globals_service.hpp"
#include "services/gui/gui_service.hpp"
#include "services/gta_data/gta_data_service.hpp"
@ -72,6 +73,7 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
LOG(INFO) << "Thread pool initialized.";
auto context_menu_service_instance = std::make_unique<context_menu_service>();
auto custom_text_service_instance = std::make_unique<custom_text_service>();
auto globals_service_instace = std::make_unique<globals_service>();
auto mobile_service_instance = std::make_unique<mobile_service>();
auto notification_service_instance = std::make_unique<notification_service>();
@ -136,6 +138,8 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
LOG(INFO) << "Pickup Service reset.";
globals_service_instace.reset();
LOG(INFO) << "Globals Service reset.";
custom_text_service_instance.reset();
LOG(INFO) << "Custom Text Service reset.";
context_menu_service_instance.reset();
LOG(INFO) << "Context Service reset.";
LOG(INFO) << "Services uninitialized.";

View File

@ -361,6 +361,12 @@ namespace big
}*/
});
// Get Label Text
main_batch.add("GLT", "75 ? E8 ? ? ? ? 8B 0D ? ? ? ? 65 48 8B 04 25 ? ? ? ? BA ? ? ? ? 48 8B 04 C8 8B 0C 02 D1 E9", [this](memory::handle ptr)
{
m_get_label_text = ptr.sub(19).as<PVOID>();
});
auto mem_region = memory::module(nullptr);
main_batch.run(mem_region);

View File

@ -50,6 +50,7 @@ namespace big
PVOID m_is_dlc_present;
PVOID m_network_group_override;
PUSHORT m_spectator_check;
PVOID m_get_label_text;
FriendRegistry* m_friend_registry{};

View File

@ -0,0 +1,12 @@
#include "custom_text_callbacks.hpp"
namespace big
{
const char* respawn_label_callback(const char* label)
{
if (g->self.god_mode)
return "~r~Dying with god mode, how?";
return nullptr;
}
}

View File

@ -0,0 +1,4 @@
namespace big
{
extern const char* respawn_label_callback(const char* label);
}

View File

@ -0,0 +1,51 @@
#include "custom_text_service.hpp"
#include "custom_text_callbacks.hpp"
namespace big
{
custom_text_service::custom_text_service()
{
add_callback_for_labels({ RAGE_JOAAT("RESPAWN_W"), RAGE_JOAAT("RESPAWN_W_MP") }, respawn_label_callback);
add_label_overwrite(RAGE_JOAAT("GC_OTR_TMR"), "HIDING FROM CLOWNS");
add_label_overwrite(RAGE_JOAAT("TICK_LEFTCHEAT"), "~a~~HUD_COLOUR_WHITE~ has been swatted by Rockstar.");
g_custom_text_service = this;
}
custom_text_service::~custom_text_service()
{
g_custom_text_service = nullptr;
}
bool custom_text_service::add_callback_for_label(rage::joaat_t hash, custom_label_callback&& cb)
{
return m_callbacks.insert({ hash, cb }).second;
}
bool custom_text_service::add_callback_for_labels(std::list<rage::joaat_t> hashes, custom_label_callback&& cb)
{
bool result = true;
for (const auto& hash : hashes)
result = m_callbacks.insert({ hash, cb }).second;
return result;
}
bool custom_text_service::add_label_overwrite(rage::joaat_t hash, const std::string_view overwrite)
{
const auto size = std::strlen(overwrite.data()) + 1;
auto buffer = std::make_unique<char[]>(size);
memcpy(buffer.get(), overwrite.data(), size);
return m_label_overwrites.insert({ hash, std::move(buffer) }).second;
}
const char* custom_text_service::get_text(const char* label) const
{
const auto hash = rage::joaat(label);
if (const auto& it = m_callbacks.find(hash); it != m_callbacks.end())
return it->second(label);
if (const auto& it = m_label_overwrites.find(hash); it != m_label_overwrites.end())
return it->second.get();
return nullptr;
}
}

View File

@ -0,0 +1,33 @@
#pragma once
#include "gta/joaat.hpp"
namespace big
{
using custom_label_callback = std::function<const char* (const char*)>;
class custom_text_service final
{
std::map<rage::joaat_t, custom_label_callback> m_callbacks;
std::map<rage::joaat_t, std::unique_ptr<char[]>> m_label_overwrites;
public:
custom_text_service();
~custom_text_service();
custom_text_service(const custom_text_service&) = delete;
custom_text_service(custom_text_service&&) noexcept = delete;
custom_text_service& operator=(const custom_text_service&) = delete;
custom_text_service& operator=(custom_text_service&&) noexcept = delete;
bool add_callback_for_label(rage::joaat_t hash, custom_label_callback&& cb);
bool add_callback_for_labels(std::list<rage::joaat_t> hashes, custom_label_callback&& cb);
bool add_label_overwrite(rage::joaat_t hash, std::string_view overwrite);
/**
* \brief Get the custom text for a label.
* \return nullptr if no custom text exists
*/
[[nodiscard]] const char* get_text(const char* label) const;
};
inline custom_text_service* g_custom_text_service;
}