2019-03-21 20:18:31 +01:00
# pragma once
# include "gta/script_thread.hpp"
# include "pointers.hpp"
2023-11-30 04:47:39 -05:00
# include "script/tlsContext.hpp"
2023-01-03 16:48:32 +00:00
2022-12-06 16:12:02 +00:00
# include <network/CNetworkPlayerMgr.hpp>
2023-03-01 21:27:15 +00:00
# include <ped/CPedFactory.hpp>
2022-12-06 16:12:02 +00:00
# include <script/scrProgramTable.hpp>
2019-03-21 20:18:31 +01:00
2024-06-27 08:32:17 +00:00
namespace rage
{
class netObjectIds ;
}
2019-03-21 20:18:31 +01:00
namespace big : : gta_util
{
2023-03-01 21:27:15 +00:00
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 ;
}
2022-10-28 16:54:33 +02:00
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 ;
}
2023-03-01 21:27:15 +00:00
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 )
{
2021-07-25 22:24:22 +02:00
return ped - > m_player_info ;
2019-03-21 20:18:31 +01:00
}
}
return nullptr ;
}
2022-01-31 18:27:35 +01:00
inline CNetworkPlayerMgr * get_network_player_mgr ( )
{
2023-04-14 18:54:07 +02:00
return * g_pointers - > m_gta . m_network_player_mgr ;
2022-01-31 18:27:35 +01:00
}
2022-09-12 18:44:47 +00:00
inline Network * get_network ( )
{
2023-04-14 18:54:07 +02:00
return * g_pointers - > m_gta . m_network ;
2022-09-12 18:44:47 +00:00
}
2024-06-27 08:32:17 +00:00
inline rage : : netObjectIds * get_net_object_ids ( )
{
if ( ! * g_pointers - > m_gta . m_network_object_mgr )
return nullptr ;
return ( rage : : netObjectIds * ) ( ( ( std : : uintptr_t ) * g_pointers - > m_gta . m_network_object_mgr ) + * g_pointers - > m_gta . m_object_ids_offset ) ; // TODO: map out CNetworkObjectMgr eventually
}
2023-03-01 21:27:15 +00:00
template < typename F , typename . . . Args >
void execute_as_script ( rage : : scrThread * thread , F & & callback , Args & & . . . args )
2019-03-21 20:18:31 +01:00
{
2023-03-01 21:27:15 +00:00
auto tls_ctx = rage : : tlsContext : : get ( ) ;
2023-02-13 20:38:30 +00:00
auto og_thread = tls_ctx - > m_script_thread ;
2023-03-01 21:27:15 +00:00
tls_ctx - > m_script_thread = thread ;
2023-02-13 20:38:30 +00:00
tls_ctx - > m_is_script_thread_active = true ;
std : : invoke ( std : : forward < F > ( callback ) , std : : forward < Args > ( args ) . . . ) ;
2023-03-01 21:27:15 +00:00
tls_ctx - > m_script_thread = og_thread ;
2023-02-13 20:38:30 +00:00
tls_ctx - > m_is_script_thread_active = og_thread ! = nullptr ;
}
2023-03-01 21:27:15 +00:00
template < typename F , typename . . . Args >
void execute_as_script ( rage : : joaat_t script_hash , F & & callback , Args & & . . . args )
2023-02-13 20:38:30 +00:00
{
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 ;
2023-02-13 20:38:30 +00:00
execute_as_script ( thread , callback , args . . . ) ;
2019-03-21 20:18:31 +01:00
return ;
}
}
2022-01-08 01:43:04 +01:00
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 )
2022-01-08 01:43:04 +01:00
{
2023-03-01 21:27:15 +00:00
if ( thread & & thread - > m_context . m_thread_id & & thread - > m_handler & & thread - > m_script_hash = = hash )
2022-01-08 01:43:04 +01:00
{
return thread ;
}
}
return nullptr ;
}
2022-12-06 16:12:02 +00:00
2023-07-20 22:46:32 +02:00
inline GtaThread * find_script_thread_by_id ( uint32_t id )
2023-02-13 20:38:30 +00:00
{
2023-04-14 18:54:07 +02:00
for ( auto thread : * g_pointers - > m_gta . m_script_threads )
2023-02-13 20:38:30 +00:00
{
2023-03-01 21:27:15 +00:00
if ( thread & & thread - > m_handler & & thread - > m_context . m_thread_id = = id )
2023-02-13 20:38:30 +00:00
{
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
}