This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
YimMenu/BigBaseV2/src/gta_util.hpp
2019-06-23 22:00:18 +02:00

58 lines
1.2 KiB
C++

#pragma once
#include "common.hpp"
#include "gta/array.hpp"
#include "gta/ped_factory.hpp"
#include "gta/player.hpp"
#include "gta/script_thread.hpp"
#include "gta/tls_context.hpp"
#include "pointers.hpp"
namespace big::gta_util
{
inline CPed *get_local_ped()
{
if (auto ped_factory = *g_pointers->m_ped_factory)
{
return ped_factory->m_local_ped;
}
return nullptr;
}
inline CPlayerInfo *get_local_playerinfo()
{
if (auto ped_factory = *g_pointers->m_ped_factory)
{
if (auto ped = ped_factory->m_local_ped)
{
return ped->m_playerinfo;
}
}
return nullptr;
}
template <typename F, typename ...Args>
void execute_as_script(rage::joaat_t script_hash, F &&callback, Args &&...args)
{
auto tls_ctx = rage::tlsContext::get();
for (auto thread : *g_pointers->m_script_threads)
{
if (!thread || !thread->m_context.m_thread_id || thread->m_context.m_script_hash != script_hash)
continue;
auto og_thread = tls_ctx->m_script_thread;
tls_ctx->m_script_thread = thread;
tls_ctx->m_is_script_thread_active = true;
std::invoke(std::forward<F>(callback), std::forward<Args>(args)...);
tls_ctx->m_script_thread = og_thread;
tls_ctx->m_is_script_thread_active = og_thread != nullptr;
return;
}
}
}