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/src/gta_util.hpp

123 lines
2.6 KiB
C++
Raw Normal View History

2019-03-21 20:18:31 +01:00
#pragma once
#include "gta/script_thread.hpp"
#include "pointers.hpp"
#include "script/tlsContext.hpp"
2022-12-06 16:12:02 +00:00
#include <network/CNetworkPlayerMgr.hpp>
#include <ped/CPedFactory.hpp>
2022-12-06 16:12:02 +00:00
#include <script/scrProgramTable.hpp>
2019-03-21 20:18:31 +01:00
namespace big::gta_util
{
inline CPed* get_local_ped()
2019-03-21 20:18:31 +01:00
{
2023-04-14 18:54:07 +02:00
if (auto ped_factory = *g_pointers->m_gta.m_ped_factory)
2019-03-21 20:18:31 +01:00
{
return ped_factory->m_local_ped;
}
return nullptr;
}
inline CVehicle* get_local_vehicle()
{
if (const auto ped = get_local_ped(); ped)
{
if (const auto veh = ped->m_vehicle; veh)
{
return veh;
}
}
return nullptr;
}
inline CPlayerInfo* get_local_playerinfo()
2019-03-21 20:18:31 +01:00
{
2023-04-14 18:54:07 +02:00
if (auto ped_factory = *g_pointers->m_gta.m_ped_factory)
2019-03-21 20:18:31 +01:00
{
if (auto ped = ped_factory->m_local_ped)
{
return ped->m_player_info;
2019-03-21 20:18:31 +01:00
}
}
return nullptr;
}
inline CNetworkPlayerMgr* get_network_player_mgr()
{
2023-04-14 18:54:07 +02:00
return *g_pointers->m_gta.m_network_player_mgr;
}
inline Network* get_network()
{
2023-04-14 18:54:07 +02:00
return *g_pointers->m_gta.m_network;
}
template<typename F, typename... Args>
void execute_as_script(rage::scrThread* thread, F&& callback, Args&&... args)
2019-03-21 20:18:31 +01:00
{
auto tls_ctx = rage::tlsContext::get();
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;
}
template<typename F, typename... Args>
void execute_as_script(rage::joaat_t script_hash, F&& callback, Args&&... args)
{
2023-04-14 18:54:07 +02:00
for (auto thread : *g_pointers->m_gta.m_script_threads)
2019-03-21 20:18:31 +01:00
{
if (!thread || !thread->m_context.m_thread_id || thread->m_context.m_script_hash != script_hash)
continue;
execute_as_script(thread, callback, args...);
2019-03-21 20:18:31 +01:00
return;
}
}
inline GtaThread* find_script_thread(rage::joaat_t hash)
{
2023-04-14 18:54:07 +02:00
for (auto thread : *g_pointers->m_gta.m_script_threads)
{
if (thread && thread->m_context.m_thread_id && thread->m_handler && thread->m_script_hash == hash)
{
return thread;
}
}
return nullptr;
}
2022-12-06 16:12:02 +00:00
inline GtaThread* find_script_thread_by_id(uint32_t id)
{
2023-04-14 18:54:07 +02:00
for (auto thread : *g_pointers->m_gta.m_script_threads)
{
if (thread && thread->m_handler && thread->m_context.m_thread_id == id)
{
return thread;
}
}
return nullptr;
}
2022-12-06 16:12:02 +00:00
inline rage::scrProgram* find_script_program(rage::joaat_t hash)
{
2023-04-14 18:54:07 +02:00
for (auto& script : *g_pointers->m_gta.m_script_program_table)
2022-12-06 16:12:02 +00:00
{
if (script.m_program && script.m_program->m_name_hash == hash)
return script.m_program;
}
return nullptr;
}
2019-03-21 20:18:31 +01:00
}