refactor(CustomText): Changed custom_text global to be static

This commit is contained in:
Yimura
2021-02-08 20:57:58 +01:00
parent 35f9cea162
commit 884ef8390e
5 changed files with 17 additions and 35 deletions

View File

@ -2,34 +2,24 @@
namespace big namespace big
{ {
custom_text::custom_text()
{
g_custom_text = this;
}
custom_text::~custom_text()
{
g_custom_text = nullptr;
}
void custom_text::add_text(Hash hash, const char* text) void custom_text::add_text(Hash hash, const char* text)
{ {
auto size = strlen(text) + 1; auto size = strlen(text) + 1;
auto buffer = std::make_unique<char[]>(size); auto buffer = std::make_unique<char[]>(size);
std::copy_n(text, size, buffer.get()); std::copy_n(text, size, buffer.get());
this->m_text_map.emplace(hash, std::move(buffer)); custom_text::m_text_map.emplace(hash, std::move(buffer));
} }
const char* custom_text::get_text(Hash hash) const char* custom_text::get_text(Hash hash)
{ {
if (auto it = this->m_text_map.find(hash); it != m_text_map.end()) if (auto it = custom_text::m_text_map.find(hash); it != custom_text::m_text_map.end())
return it->second.get(); return it->second.get();
return nullptr; return nullptr;
} }
void custom_text::remove_text(Hash hash) void custom_text::remove_text(Hash hash)
{ {
this->m_text_map.erase(hash); custom_text::m_text_map.erase(hash);
} }
} }

View File

@ -4,17 +4,13 @@ namespace big
{ {
class custom_text class custom_text
{ {
public:
explicit custom_text();
~custom_text();
void add_text(Hash hash, const char* text);
const char* get_text(Hash hash);
void remove_text(Hash hash);
private: private:
std::unordered_map<Hash, std::unique_ptr<char[]>> m_text_map; inline static std::unordered_map<Hash, std::unique_ptr<char[]>> m_text_map;
};
inline custom_text* g_custom_text{}; public:
static void add_text(Hash hash, const char* text);
static const char* get_text(Hash hash);
static void remove_text(Hash hash);
};
} }

View File

@ -126,14 +126,14 @@ namespace big
void gui::script_init() void gui::script_init()
{ {
g_custom_text->add_text(RAGE_JOAAT("LOADING_SPLAYER_L"), "Preparing for awesomeness."); custom_text::add_text(RAGE_JOAAT("LOADING_SPLAYER_L"), "Preparing for awesomeness.");
g_custom_text->add_text(RAGE_JOAAT("HUD_JOINING"), "Yim's Menu"); custom_text::add_text(RAGE_JOAAT("HUD_JOINING"), "Yim's Menu");
g_custom_text->add_text(RAGE_JOAAT("HUD_TRANSP"), "Transaction's fucked..."); custom_text::add_text(RAGE_JOAAT("HUD_TRANSP"), "Transaction's fucked...");
g_custom_text->add_text(RAGE_JOAAT("HUD_QUITTING"), "Leaving are we?"); custom_text::add_text(RAGE_JOAAT("HUD_QUITTING"), "Leaving are we?");
g_custom_text->add_text(RAGE_JOAAT("HUD_QUITRACE"), "Are you a pussy?"); custom_text::add_text(RAGE_JOAAT("HUD_QUITRACE"), "Are you a pussy?");
g_custom_text->add_text(RAGE_JOAAT("HUD_SAVDNWARN"), "Rockstar crashed their toaster again..."); custom_text::add_text(RAGE_JOAAT("HUD_SAVDNWARN"), "Rockstar crashed their toaster again...");
notify::above_map("Yim's Menu is ready."); notify::above_map("Yim's Menu is ready.");
} }

View File

@ -7,7 +7,7 @@ namespace big
const char* hooks::get_label_text(void* unk, const char* label) const char* hooks::get_label_text(void* unk, const char* label)
{ {
if (g_running) if (g_running)
if (auto text = g_custom_text->get_text(rage::joaat(label))) if (auto text = custom_text::get_text(rage::joaat(label)))
return text; return text;
return g_hooking->m_get_label_text_hook.get_original<decltype(&get_label_text)>()(unk, label); return g_hooking->m_get_label_text_hook.get_original<decltype(&get_label_text)>()(unk, label);

View File

@ -1,6 +1,5 @@
#include "common.hpp" #include "common.hpp"
#include "features.hpp" #include "features.hpp"
#include "features/custom_text.hpp"
#include "fiber_pool.hpp" #include "fiber_pool.hpp"
#include "gui.hpp" #include "gui.hpp"
#include "logger.hpp" #include "logger.hpp"
@ -46,9 +45,6 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
auto hooking_instance = std::make_unique<hooking>(); auto hooking_instance = std::make_unique<hooking>();
LOG(INFO) << "Hooking initialized."; LOG(INFO) << "Hooking initialized.";
auto custom_text_instance = std::make_unique<custom_text>();
LOG(INFO) << "Custom Text Initialized.";
g_settings.load(); g_settings.load();
LOG(INFO) << "Settings Loaded."; LOG(INFO) << "Settings Loaded.";