From f9b59da4436abf78bf362916d14940eae44e6a35 Mon Sep 17 00:00:00 2001 From: Aure7138 <100095051+Aure7138@users.noreply.github.com> Date: Thu, 9 Feb 2023 05:46:08 +0800 Subject: [PATCH] refactor: take_control_of (#962) --- src/function_types.hpp | 1 + src/pointers.cpp | 14 ++++++------- src/pointers.hpp | 2 ++ src/util/entity.hpp | 36 +++++++++++++++++++--------------- src/views/vehicle/view_lsc.cpp | 2 +- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/function_types.hpp b/src/function_types.hpp index b3c0a6a1..46a113c4 100644 --- a/src/function_types.hpp +++ b/src/function_types.hpp @@ -109,6 +109,7 @@ namespace big::functions using send_network_damage = void(*)(CEntity* source, CEntity* target, rage::fvector3* position, int hit_component, bool override_default_damage, int weapon_type, float override_damage, int tire_index, int suspension_index, int flags, std::uint32_t action_result_hash, std::int16_t action_result_id, int action_unk, bool hit_weapon, bool hit_weapon_ammo_attachment, bool silenced, bool unk, rage::fvector3* impact_direction); using request_ragdoll = void(*)(uint16_t object_id); + using request_control = void(*)(rage::netObject* net_object); using get_connection_peer = rage::netConnectionPeer* (*)(rage::netConnectionManager* manager, int peer_id); using send_remove_gamer_cmd = void(*)(rage::netConnectionManager* net_connection_mgr, rage::netConnectionPeer* player, int connection_id, rage::snMsgRemoveGamersFromSessionCmd* cmd, int flags); diff --git a/src/pointers.cpp b/src/pointers.cpp index 00b3f823..e5e32419 100644 --- a/src/pointers.cpp +++ b/src/pointers.cpp @@ -213,12 +213,6 @@ namespace big m_write_player_game_state_data_node = ptr.as(); }); - // Request Control of Entity PATCH - main_batch.add("RCOE-Patch", "48 89 5C 24 ? 57 48 83 EC 20 8B D9 E8 ? ? ? ? ? ? ? ? 8B CB", [this](memory::handle ptr) - { - memory::byte_patch::make(ptr.add(0x13).as(), 0x9090)->apply(); - }); - // Replay Interface main_batch.add("RI", "0F B7 44 24 ? 66 89 44 4E", [this](memory::handle ptr) { @@ -580,11 +574,17 @@ namespace big }); // Request Ragdoll - main_batch.add("RR", "E8 ? ? ? ? 09 B3 ? ? ? ? 48 8B 5C 24 ?", [this](memory::handle ptr) + main_batch.add("RR", "E8 ? ? ? ? 09 B3 ? ? ? ? 48 8B 5C 24", [this](memory::handle ptr) { m_request_ragdoll = ptr.add(1).rip().as(); }); + // Request Control + main_batch.add("RC", "E8 ? ? ? ? EB 3E 48 8B D3", [this](memory::handle ptr) + { + m_request_control = ptr.add(1).rip().as(); + }); + // Get Connection Peer & Send Remove Gamer Command main_batch.add("GCP&SRGC", "8D 42 FF 83 F8 FD 77 3D", [this](memory::handle ptr) { diff --git a/src/pointers.hpp b/src/pointers.hpp index f6f653bc..d12730bb 100644 --- a/src/pointers.hpp +++ b/src/pointers.hpp @@ -191,6 +191,8 @@ namespace big functions::send_network_damage m_send_network_damage; functions::request_ragdoll m_request_ragdoll; + functions::request_control m_request_control; + functions::get_connection_peer m_get_connection_peer{}; functions::send_remove_gamer_cmd m_send_remove_gamer_cmd{}; diff --git a/src/util/entity.hpp b/src/util/entity.hpp index c1ec29b0..11241f24 100644 --- a/src/util/entity.hpp +++ b/src/util/entity.hpp @@ -56,25 +56,29 @@ namespace big::entity return (bool)hit; } + inline bool network_has_control_of_entity(rage::netObject* net_object) + { + return net_object && !net_object->m_next_owner_id && (net_object->m_control_id == -1); + } + inline bool take_control_of(Entity ent, int timeout = 300) { - if (NETWORK::NETWORK_HAS_CONTROL_OF_ENTITY(ent) || !*g_pointers->m_is_session_started) - return true; - - for (int i = 0; !NETWORK::NETWORK_HAS_CONTROL_OF_ENTITY(ent) && i < timeout; i++) + auto ptr = g_pointers->m_handle_to_ptr(ent); + if (ptr) { - NETWORK::NETWORK_REQUEST_CONTROL_OF_ENTITY(ent); - - if (timeout != 0) - script::get_current()->yield(); - } - - if (!NETWORK::NETWORK_HAS_CONTROL_OF_ENTITY(ent)) - return false; - - int netHandle = NETWORK::NETWORK_GET_NETWORK_ID_FROM_ENTITY(ent); - NETWORK::SET_NETWORK_ID_CAN_MIGRATE(netHandle, true); - + if (!*g_pointers->m_is_session_started || network_has_control_of_entity(ptr->m_net_object)) + return true; + for (int i = 0; !network_has_control_of_entity(ptr->m_net_object) && i < timeout; i++) + { + g_pointers->m_request_control(ptr->m_net_object); + if (timeout != 0) + script::get_current()->yield(); + } + if (!network_has_control_of_entity(ptr->m_net_object)) + return false; + int netHandle = NETWORK::NETWORK_GET_NETWORK_ID_FROM_ENTITY(ent); + NETWORK::SET_NETWORK_ID_CAN_MIGRATE(netHandle, true); + } return true; } } \ No newline at end of file diff --git a/src/views/vehicle/view_lsc.cpp b/src/views/vehicle/view_lsc.cpp index 8b44afe5..4ff267c6 100644 --- a/src/views/vehicle/view_lsc.cpp +++ b/src/views/vehicle/view_lsc.cpp @@ -318,7 +318,7 @@ namespace big if (ImGui::Selectable(name.c_str(), item_selected)) { g_fiber_pool->queue_job([&mod, is_wheel_mod, wheel_stock_mod, wheel_custom] { - NETWORK::NETWORK_REQUEST_CONTROL_OF_ENTITY(self::veh); + entity::take_control_of(self::veh); if (selected_slot >= 0) {