Skip to content

Commit

Permalink
Merge Add NETWORK_DOES_ENTITY_EXIST_WITH_NETWORK_ID as a Cfx native (…
Browse files Browse the repository at this point in the history
…pr-2856)

91cbd2a - feat(rdr): add NETWORK_DOES_ENTITY_EXIST_WITH_NETWORK_ID as a Cfx native
  • Loading branch information
prikolium-cfx committed Oct 21, 2024
2 parents 591756b + 91cbd2a commit df16d52
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
1 change: 1 addition & 0 deletions code/components/gta-core-rdr3/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"dependencies": [
"fx[2]",
"citizen:scripting:core",
"pool-sizes-state",
"rage:allocator",
"rage:nutsnbolts",
Expand Down
58 changes: 38 additions & 20 deletions code/components/gta-core-rdr3/src/EntityDeletionHacks.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "StdInc.h"
#include "Hooking.h"

#include "ScriptWarnings.h"
#include "scrEngine.h"

#include <boost/type_index.hpp>
Expand Down Expand Up @@ -103,25 +104,47 @@ static hook::cdecl_stub<netObject* (uint16_t id)> getNetObjById([]()
{
return hook::get_call(hook::get_pattern("48 8B F8 8A 46 ? 3C 03 75", -13));
});

static fwEntity* GetNetworkObject(int objectId)
{
auto object = getNetObjById(objectId);

if (!object)
{
fx::scripting::Warningf("entity", __FUNCTION__ ": no object by ID %d\n", objectId);
return nullptr;
}

auto gameObject = object->gameObject;

if (!gameObject)
{
fx::scripting::Warningf("entity", __FUNCTION__ ": no game object for ID %d\n", objectId);
return nullptr;
}

return gameObject;
}

static HookFunction hookFunction([]()
{
// get network ID by entity
rage::scrEngine::NativeHandler getNetID = [](rage::scrNativeCallContext* context)
{
auto entity = getScriptEntity(context->GetArgument<int>(0));
auto entityId = context->GetArgument<int>(0);
auto entity = getScriptEntity(entityId);

if (!entity)
{
trace("NETWORK_GET_NETWORK_ID_FROM_ENTITY: no such entity\n");
fx::scripting::Warningf("entity", "NETWORK_GET_NETWORK_ID_FROM_ENTITY: no such entity (script ID %d)\n", entityId);
return;
}

auto netObject = entity->netObject;

if (!netObject)
{
trace("NETWORK_GET_NETWORK_ID_FROM_ENTITY: no net object for entity\n");
fx::scripting::Warningf("entity", "NETWORK_GET_NETWORK_ID_FROM_ENTITY: no net object for entity (script id %d)\n", entityId);
return;
}

Expand All @@ -131,22 +154,12 @@ static HookFunction hookFunction([]()
// get entity by network ID
rage::scrEngine::NativeHandler getNetObj = [](rage::scrNativeCallContext* context)
{
auto objectId = context->GetArgument<int>(0);
auto object = getNetObjById(objectId);

if (!object)
{
trace("NETWORK_GET_ENTITY_FROM_NETWORK_ID: no object by ID %d\n", objectId);
return;
}

auto gameObject = object->gameObject;

auto gameObject = GetNetworkObject(context->GetArgument<int>(0));

if (!gameObject)
{
trace("NETWORK_GET_ENTITY_FROM_NETWORK_ID: no game object for ID %d\n", objectId);
return;
}
return;
}

auto entityHandle = getScriptGuidForEntity(gameObject);
context->SetResult(0, entityHandle);
Expand Down Expand Up @@ -188,14 +201,19 @@ static HookFunction hookFunction([]()
context->SetResult<uint32_t>(0, object != nullptr);
});

// NETWORK_DOES_ENTITY_EXIST_WITH_NETWORK_ID
rage::scrEngine::RegisterNativeHandler(0x18A47D074708FD68, [](rage::scrNativeCallContext* context)
auto doesEntityExistWithNetworkId = [](rage::scrNativeCallContext* context)
{
auto objectId = context->GetArgument<int>(0);
auto object = getNetObjById(objectId);

context->SetResult<uint32_t>(0, object && object->gameObject);
});
};

// NETWORK_DOES_ENTITY_EXIST_WITH_NETWORK_ID
// Kept to keep compatibility with any resource that directly invoked it previously
rage::scrEngine::RegisterNativeHandler(0x18A47D074708FD68, doesEntityExistWithNetworkId);

rage::scrEngine::RegisterNativeHandler("NETWORK_DOES_ENTITY_EXIST_WITH_NETWORK_ID", doesEntityExistWithNetworkId);

// CGameScriptHandlerObject::GetOwner vs. GetCurrentScriptHandler checks
{
Expand Down
16 changes: 16 additions & 0 deletions ext/native-decls/NetworkDoesEntityExistWithNetworkId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
ns: CFX
apiset: client
game: rdr3
---
## NETWORK_DOES_ENTITY_EXIST_WITH_NETWORK_ID

```c
BOOL NETWORK_DOES_ENTITY_EXIST_WITH_NETWORK_ID(int netId);
```
## Parameters
* **netId**: The network id to check
## Return value
Returns `true` if both the network id exist, and the network id has a game object.

0 comments on commit df16d52

Please sign in to comment.