From ac388c9730b2664f75fb814508cb21585fef881e Mon Sep 17 00:00:00 2001 From: xshady <54737754+xxshady@users.noreply.github.com> Date: Mon, 24 Jul 2023 18:21:39 +0300 Subject: [PATCH 1/3] Fix baseobject (de)serialization in raw emits --- shared/helpers/Serialization.cpp | 58 +++++++++++++++++++------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/shared/helpers/Serialization.cpp b/shared/helpers/Serialization.cpp index 32292814..4c6ccc6f 100644 --- a/shared/helpers/Serialization.cpp +++ b/shared/helpers/Serialization.cpp @@ -263,7 +263,7 @@ enum class RawValueType : uint8_t { INVALID, GENERIC, - ENTITY, + BASEOBJECT, VECTOR3, VECTOR2, RGBA @@ -279,20 +279,9 @@ static inline RawValueType GetValueType(v8::Local ctx, v8::LocalGetHandle(); - switch(ent->GetType()) - { - case alt::IBaseObject::Type::PLAYER: - case alt::IBaseObject::Type::VEHICLE: - case alt::IBaseObject::Type::LOCAL_PLAYER: - { - return RawValueType::ENTITY; - } - default: - { - return RawValueType::INVALID; - } - } + alt::IBaseObject* object = entity->GetHandle(); + if(!object) return RawValueType::INVALID; + return RawValueType::BASEOBJECT; } if(resource->IsVector3(val)) return RawValueType::VECTOR3; if(resource->IsVector2(val)) return RawValueType::VECTOR2; @@ -307,13 +296,23 @@ static inline bool WriteRawValue(v8::Local ctx, v8::ValueSerializer serializer.WriteRawBytes(&type, sizeof(uint8_t)); switch(type) { - case RawValueType::ENTITY: + case RawValueType::BASEOBJECT: { V8Entity* entity = V8Entity::Get(val); if(!entity) return false; - alt::IEntity* handle = dynamic_cast(entity->GetHandle()); - uint16_t id = handle->GetID(); + alt::IBaseObject* handle = entity->GetHandle(); + uint8_t type = static_cast(handle->GetType()); + + uint32_t id; +#ifdef ALT_CLIENT_API + if (handle->IsRemote()) id = handle->GetRemoteID(); + else id = handle->GetID(); +#else + id = handle->GetID(); +#endif // ALT_CLIENT_API + serializer.WriteRawBytes(&id, sizeof(id)); + serializer.WriteRawBytes(&type, sizeof(type)); break; } case RawValueType::VECTOR3: @@ -364,13 +363,24 @@ static inline v8::MaybeLocal ReadRawValue(v8::Local ctx switch(type) { - case RawValueType::ENTITY: + case RawValueType::BASEOBJECT: { - uint16_t* id; - if(!deserializer.ReadRawBytes(sizeof(uint16_t), (const void**)&id)) return v8::MaybeLocal(); - alt::IEntity* entity = alt::ICore::Instance().GetEntityBySyncID(*id); - if(!entity) return v8::MaybeLocal(); - return V8ResourceImpl::Get(ctx)->GetOrCreateEntity(entity, "Entity")->GetJSVal(isolate); + uint32_t* id; + alt::IBaseObject::Type* type; + if(!deserializer.ReadRawBytes(sizeof(uint32_t), (const void**)&id) || + !deserializer.ReadRawBytes(sizeof(uint8_t), (const void**)&type)) + return v8::MaybeLocal(); + + alt::IBaseObject* object; +#ifdef ALT_CLIENT_API + object = alt::ICore::Instance().GetBaseObjectByRemoteID(*type, *id); + if(!object) object = alt::ICore::Instance().GetBaseObjectByID(*type, *id); +#else + if (*type == alt::IBaseObject::Type::LOCAL_PLAYER) *type = alt::IBaseObject::Type::PLAYER; + object = alt::ICore::Instance().GetBaseObjectByID(*type, *id); +#endif // ALT_CLIENT_API + if(!object) return v8::MaybeLocal(); + return V8ResourceImpl::Get(ctx)->GetOrCreateEntity(object, "BaseObject")->GetJSVal(isolate); } case RawValueType::VECTOR3: { From 3a0610c9618d717a321fab575c8c0d416166851f Mon Sep 17 00:00:00 2001 From: xshady <54737754+xxshady@users.noreply.github.com> Date: Mon, 24 Jul 2023 18:57:29 +0300 Subject: [PATCH 2/3] shared: Add remote to raw baseobject (de)serialization --- shared/helpers/Serialization.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/shared/helpers/Serialization.cpp b/shared/helpers/Serialization.cpp index 4c6ccc6f..52666e5b 100644 --- a/shared/helpers/Serialization.cpp +++ b/shared/helpers/Serialization.cpp @@ -304,15 +304,27 @@ static inline bool WriteRawValue(v8::Local ctx, v8::ValueSerializer uint8_t type = static_cast(handle->GetType()); uint32_t id; + bool remote; #ifdef ALT_CLIENT_API - if (handle->IsRemote()) id = handle->GetRemoteID(); - else id = handle->GetID(); + if (handle->IsRemote()) + { + id = handle->GetRemoteID(); + remote = true; + } + else + { + id = handle->GetID(); + remote = false; + } #else id = handle->GetID(); + remote = true; + #endif // ALT_CLIENT_API serializer.WriteRawBytes(&id, sizeof(id)); serializer.WriteRawBytes(&type, sizeof(type)); + serializer.WriteRawBytes(&remote, sizeof(remote)); break; } case RawValueType::VECTOR3: @@ -367,14 +379,22 @@ static inline v8::MaybeLocal ReadRawValue(v8::Local ctx { uint32_t* id; alt::IBaseObject::Type* type; + bool* remote; if(!deserializer.ReadRawBytes(sizeof(uint32_t), (const void**)&id) || - !deserializer.ReadRawBytes(sizeof(uint8_t), (const void**)&type)) + !deserializer.ReadRawBytes(sizeof(uint8_t), (const void**)&type) || + !deserializer.ReadRawBytes(sizeof(bool), (const void**)&remote)) return v8::MaybeLocal(); alt::IBaseObject* object; #ifdef ALT_CLIENT_API - object = alt::ICore::Instance().GetBaseObjectByRemoteID(*type, *id); - if(!object) object = alt::ICore::Instance().GetBaseObjectByID(*type, *id); + if (*remote) + { + object = alt::ICore::Instance().GetBaseObjectByRemoteID(*type, *id); + } + else + { + object = alt::ICore::Instance().GetBaseObjectByID(*type, *id); + } #else if (*type == alt::IBaseObject::Type::LOCAL_PLAYER) *type = alt::IBaseObject::Type::PLAYER; object = alt::ICore::Instance().GetBaseObjectByID(*type, *id); From c083a044e9eca19b23a169c6a13eeaf8cade35b2 Mon Sep 17 00:00:00 2001 From: xshady <54737754+xxshady@users.noreply.github.com> Date: Mon, 24 Jul 2023 18:57:32 +0300 Subject: [PATCH 3/3] shared: Add remote to raw baseobject (de)serialization