From a45532e3fa211225e27045605b434430a63ed8d1 Mon Sep 17 00:00:00 2001 From: xLuxy <67131061+xLuxy@users.noreply.github.com> Date: Tue, 1 Oct 2024 05:32:16 +0200 Subject: [PATCH 1/5] feat: add Interior API --- client/src/bindings/ClientBindingsMain.cpp | 5 +- client/src/bindings/Interior.cpp | 304 +++++++++++++++++++++ shared/V8Class.h | 1 + shared/deps/cpp-sdk | 2 +- 4 files changed, 309 insertions(+), 3 deletions(-) create mode 100644 client/src/bindings/Interior.cpp diff --git a/client/src/bindings/ClientBindingsMain.cpp b/client/src/bindings/ClientBindingsMain.cpp index 4e445435..c646b481 100644 --- a/client/src/bindings/ClientBindingsMain.cpp +++ b/client/src/bindings/ClientBindingsMain.cpp @@ -1360,7 +1360,7 @@ extern V8Module sharedModule; extern V8Class v8Player, v8Player, v8Vehicle, v8WebView, v8HandlingData, v8LocalStorage, v8MemoryBuffer, v8MapZoomData, v8Discord, v8Voice, v8WebSocketClient, v8Checkpoint, v8HttpClient, v8Audio, v8LocalPlayer, v8Profiler, v8Worker, v8RmlDocument, v8RmlElement, v8WeaponData, v8FocusData, v8LocalObject, v8TextEncoder, v8TextDecoder, v8Object, v8VirtualEntityGroup, v8VirtualEntity, v8AudioFilter, v8Marker, v8Ped, v8Colshape, v8ColshapeCylinder, v8ColshapeSphere, v8ColshapeCircle, v8ColshapeCuboid, v8ColshapeRectangle, v8ColshapePolygon, v8TextLabel, - v8LocalPed, v8LocalVehicle, v8Font, v8WeaponObject, v8AudioOutput, v8AudioOutputFrontend, v8AudioOutputWorld, v8AudioOutputAttached, v8AudioCategory; + v8LocalPed, v8LocalVehicle, v8Font, v8WeaponObject, v8AudioOutput, v8AudioOutputFrontend, v8AudioOutputWorld, v8AudioOutputAttached, v8AudioCategory, v8Interior; extern V8Module altModule("alt", &sharedModule, { v8Player, @@ -1408,7 +1408,8 @@ extern V8Module altModule("alt", v8AudioOutputFrontend, v8AudioOutputWorld, v8AudioOutputAttached, - v8AudioCategory }, + v8AudioCategory, + v8Interior }, [](v8::Local ctx, v8::Local exports) { v8::Isolate* isolate = ctx->GetIsolate(); diff --git a/client/src/bindings/Interior.cpp b/client/src/bindings/Interior.cpp new file mode 100644 index 00000000..869317b8 --- /dev/null +++ b/client/src/bindings/Interior.cpp @@ -0,0 +1,304 @@ +#include "../CV8Resource.h" +#include "V8Class.h" + +static void Constructor(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_CONSTRUCTOR(); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, interiorId); + + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist"); + + V8Helpers::SetObjectClass(info.GetIsolate(), info.This(), V8Class::ObjectClass::INTERIOR); + info.This()->SetInternalField(1, v8::External::New(isolate, interior.get())); +} + +static void GetForInteriorID(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, interiorId); + + std::vector> args{ V8Helpers::JSValue(interiorId) }; + + extern V8Class v8Interior; + V8_RETURN(v8Interior.New(isolate->GetEnteredOrMicrotaskContext(), args)); +} + +static void GetRoomIndexByHash(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, hash); + + V8_RETURN_INT(interior->GetRoomIndexByHash(hash)); +} + +static void GetRoomName(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, roomIndex); + + V8_RETURN_STRING(interior->GetRoomName(roomIndex)); +} + +static void GetRoomFlag(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, roomIndex); + + V8_RETURN_INT(interior->GetRoomFlag(roomIndex)); +} + +static void SetRoomFlag(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(2); + V8_ARG_TO_UINT(1, roomIndex); + V8_ARG_TO_UINT(1, roomFlag); + + interior->SetRoomFlag(roomIndex, roomFlag); +} + +static void GetRoomExtents(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, roomId); + + auto extentInfo = interior->GetRoomExtents(roomId); + + v8::Local obj = v8::Object::New(isolate); + obj->Set(ctx, V8Helpers::JSValue("min"), resource->CreateVector3(extentInfo.min)); + obj->Set(ctx, V8Helpers::JSValue("max"), resource->CreateVector3(extentInfo.max)); + V8_RETURN(obj); +} + +static void SetRoomExtents(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(3); + V8_ARG_TO_UINT(1, roomIndex); + V8_ARG_TO_VECTOR3(2, min); + V8_ARG_TO_VECTOR3(3, max); + + interior->SetRoomExtents(roomIndex, { min, max }); +} + +static void GetRoomTimecycle(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, roomIndex); + + V8_RETURN_UINT(interior->GetRoomTimecycle(roomIndex)); +} + +static void SetRoomTimecycle(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(2); + V8_ARG_TO_UINT(1, roomIndex); + V8_ARG_TO_UINT(2, timecycleHash); + + interior->SetRoomTimecycle(roomIndex, timecycleHash); +} + +static void GetPortalCornerPosition(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(2); + V8_ARG_TO_UINT(1, portalIndex); + V8_ARG_TO_UINT(2, cornerIndex); + + V8_RETURN_VECTOR3(interior->GetPortalCornerPosition(portalIndex, cornerIndex)); +} + +static void SetPortalCornerPosition(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(3); + V8_ARG_TO_UINT(1, portalIndex); + V8_ARG_TO_UINT(2, cornerIndex); + V8_ARG_TO_VECTOR3(3, position); + + interior->SetPortalCornerPosition(portalIndex, cornerIndex, position); +} + +static void GetPortalRoomFrom(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, portalIndex); + + V8_RETURN_INT(interior->GetPortalRoomFrom(portalIndex)); +} + +static void SetPortalRoomFrom(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(2); + V8_ARG_TO_UINT(1, portalIndex); + V8_ARG_TO_INT(2, roomFrom); + + interior->SetPortalRoomFrom(portalIndex, roomFrom); +} + +static void GetPortalRoomTo(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, portalIndex); + + V8_RETURN_INT(interior->GetPortalRoomTo(portalIndex)); +} + +static void SetPortalRoomTo(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(2); + V8_ARG_TO_UINT(1, portalIndex); + V8_ARG_TO_INT(2, roomTo); + + interior->SetPortalRoomTo(portalIndex, roomTo); +} + +static void GetPortalFlag(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, portalIndex); + + V8_RETURN_INT(interior->GetPortalFlag(portalIndex)); +} + +static void SetPortalFlag(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_CHECK_ARGS_LEN(2); + V8_ARG_TO_UINT(1, portalIndex); + V8_ARG_TO_INT(2, flag); + + interior->SetPortalFlag(portalIndex, flag); +} + +static void RoomCountGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_RETURN_UINT(interior->GetRoomCount()); +} + +static void PortalCountGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_RETURN_UINT(interior->GetPortalCount()); +} + +static void PositionGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_RETURN_VECTOR3(interior->GetPosition()); +} + +static void RotationGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + V8_RETURN_VECTOR3(interior->GetRotation()); +} + +static void EntitiesExtentsGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + + auto extentInfo = interior->GetEntitiesExtents(); + + v8::Local obj = v8::Object::New(isolate); + obj->Set(ctx, V8Helpers::JSValue("min"), resource->CreateVector3(extentInfo.min)); + obj->Set(ctx, V8Helpers::JSValue("max"), resource->CreateVector3(extentInfo.max)); + V8_RETURN(obj); +} + +extern V8Class v8Interior("Interior", Constructor, [](v8::Local tpl) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + + tpl->InstanceTemplate()->SetInternalFieldCount(static_cast(V8Class::InternalFields::COUNT)); + + V8Helpers::SetMethod(isolate, tpl, "getRoomIndexByHash", &GetRoomIndexByHash); + V8Helpers::SetMethod(isolate, tpl, "getRoomName", &GetRoomName); + + V8Helpers::SetMethod(isolate, tpl, "getRoomFlag", &GetRoomFlag); + V8Helpers::SetMethod(isolate, tpl, "setRoomFlag", &SetRoomFlag); + + V8Helpers::SetMethod(isolate, tpl, "getRoomExtents", &GetRoomExtents); + V8Helpers::SetMethod(isolate, tpl, "setRoomExtents", &SetRoomExtents); + + V8Helpers::SetMethod(isolate, tpl, "getRoomTimecycle", &GetRoomTimecycle); + V8Helpers::SetMethod(isolate, tpl, "setRoomTimecycle", &SetRoomTimecycle); + + V8Helpers::SetMethod(isolate, tpl, "getPortalCornerPosition", &GetPortalCornerPosition); + V8Helpers::SetMethod(isolate, tpl, "setPortalCornerPosition", &SetPortalCornerPosition); + + V8Helpers::SetMethod(isolate, tpl, "getPortalRoomFrom", &GetPortalRoomFrom); + V8Helpers::SetMethod(isolate, tpl, "setPortalRoomFrom", &SetPortalRoomFrom); + + V8Helpers::SetMethod(isolate, tpl, "getPortalRoomTo", &GetPortalRoomTo); + V8Helpers::SetMethod(isolate, tpl, "setPortalRoomTo", &SetPortalRoomTo); + + V8Helpers::SetMethod(isolate, tpl, "getPortalFlag", &GetPortalFlag); + V8Helpers::SetMethod(isolate, tpl, "setPortalFlag", &SetPortalFlag); + + V8Helpers::SetAccessor(isolate, tpl, "roomCount", &RoomCountGetter); + V8Helpers::SetAccessor(isolate, tpl, "portalCount", &PortalCountGetter); + V8Helpers::SetAccessor(isolate, tpl, "pos", &PositionGetter); + V8Helpers::SetAccessor(isolate, tpl, "rot", &RotationGetter); + V8Helpers::SetAccessor(isolate, tpl, "entitiesExtents", &EntitiesExtentsGetter); + + V8Helpers::SetStaticMethod(isolate, tpl, "getForInteriorID", &GetForInteriorID); +}); diff --git a/shared/V8Class.h b/shared/V8Class.h index 4bda0eb1..c38c47b2 100644 --- a/shared/V8Class.h +++ b/shared/V8Class.h @@ -43,6 +43,7 @@ class V8Class AUDIO_CATEGORY, HANDLING, HANDLING_DATA, + INTERIOR, MAP_ZOOM_DATA, WEAPON_DATA }; diff --git a/shared/deps/cpp-sdk b/shared/deps/cpp-sdk index b7825258..45a82a3a 160000 --- a/shared/deps/cpp-sdk +++ b/shared/deps/cpp-sdk @@ -1 +1 @@ -Subproject commit b7825258fafcb6d9aa3906b41e27cfe2d8bf93af +Subproject commit 45a82a3ae3cbed371a46f6390db261377a59693d From 523ff8b81e3b9c2dd2b9820bb4f1d87ffaa6fdaa Mon Sep 17 00:00:00 2001 From: xLuxy <67131061+xLuxy@users.noreply.github.com> Date: Thu, 3 Oct 2024 19:48:33 +0200 Subject: [PATCH 2/5] store weak ptr --- client/src/bindings/Interior.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/client/src/bindings/Interior.cpp b/client/src/bindings/Interior.cpp index 869317b8..06b43c3a 100644 --- a/client/src/bindings/Interior.cpp +++ b/client/src/bindings/Interior.cpp @@ -9,11 +9,15 @@ static void Constructor(const v8::FunctionCallbackInfo& info) V8_CHECK_ARGS_LEN(1); V8_ARG_TO_UINT(1, interiorId); - auto interior = alt::ICore::Instance().GetInterior(interiorId); + std::shared_ptr interior = alt::ICore::Instance().GetInterior(interiorId); V8_CHECK(interior, "interior doesn't exist"); - V8Helpers::SetObjectClass(info.GetIsolate(), info.This(), V8Class::ObjectClass::INTERIOR); - info.This()->SetInternalField(1, v8::External::New(isolate, interior.get())); + v8::Local interiorRef = v8::External::New(isolate, new std::weak_ptr(interior)); + + v8::Persistent persistent(isolate, info.This()); + persistent.SetWeak(); + + info.This()->SetInternalField(1, interiorRef); } static void GetForInteriorID(const v8::FunctionCallbackInfo& info) From 6e34f77b4749c49efdf4a961af9ff988bbad2524 Mon Sep 17 00:00:00 2001 From: Till Schreiber Date: Fri, 4 Oct 2024 16:34:50 +0200 Subject: [PATCH 3/5] ALTV-426 refactore for api changes --- client/src/bindings/ClientBindingsMain.cpp | 5 +- client/src/bindings/Interior.cpp | 303 +++++---------------- client/src/bindings/InteriorRoom.cpp | 67 +++++ shared/V8Class.h | 2 + shared/deps/cpp-sdk | 2 +- shared/helpers/Macros.h | 4 + 6 files changed, 147 insertions(+), 236 deletions(-) create mode 100644 client/src/bindings/InteriorRoom.cpp diff --git a/client/src/bindings/ClientBindingsMain.cpp b/client/src/bindings/ClientBindingsMain.cpp index c646b481..9f1b96b8 100644 --- a/client/src/bindings/ClientBindingsMain.cpp +++ b/client/src/bindings/ClientBindingsMain.cpp @@ -1360,7 +1360,7 @@ extern V8Module sharedModule; extern V8Class v8Player, v8Player, v8Vehicle, v8WebView, v8HandlingData, v8LocalStorage, v8MemoryBuffer, v8MapZoomData, v8Discord, v8Voice, v8WebSocketClient, v8Checkpoint, v8HttpClient, v8Audio, v8LocalPlayer, v8Profiler, v8Worker, v8RmlDocument, v8RmlElement, v8WeaponData, v8FocusData, v8LocalObject, v8TextEncoder, v8TextDecoder, v8Object, v8VirtualEntityGroup, v8VirtualEntity, v8AudioFilter, v8Marker, v8Ped, v8Colshape, v8ColshapeCylinder, v8ColshapeSphere, v8ColshapeCircle, v8ColshapeCuboid, v8ColshapeRectangle, v8ColshapePolygon, v8TextLabel, - v8LocalPed, v8LocalVehicle, v8Font, v8WeaponObject, v8AudioOutput, v8AudioOutputFrontend, v8AudioOutputWorld, v8AudioOutputAttached, v8AudioCategory, v8Interior; + v8LocalPed, v8LocalVehicle, v8Font, v8WeaponObject, v8AudioOutput, v8AudioOutputFrontend, v8AudioOutputWorld, v8AudioOutputAttached, v8AudioCategory, v8Interior, v8InteriorRoom; extern V8Module altModule("alt", &sharedModule, { v8Player, @@ -1409,7 +1409,8 @@ extern V8Module altModule("alt", v8AudioOutputWorld, v8AudioOutputAttached, v8AudioCategory, - v8Interior }, + v8Interior, + v8InteriorRoom }, [](v8::Local ctx, v8::Local exports) { v8::Isolate* isolate = ctx->GetIsolate(); diff --git a/client/src/bindings/Interior.cpp b/client/src/bindings/Interior.cpp index 06b43c3a..a494b99a 100644 --- a/client/src/bindings/Interior.cpp +++ b/client/src/bindings/Interior.cpp @@ -4,28 +4,24 @@ static void Constructor(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_CHECK_CONSTRUCTOR(); + V8_CHECK_CONSTRUCTOR() - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, interiorId); + V8_CHECK_ARGS_LEN(1) + V8_ARG_TO_UINT(1, interiorId) std::shared_ptr interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist"); + V8_CHECK(interior, "interior doesn't exist") - v8::Local interiorRef = v8::External::New(isolate, new std::weak_ptr(interior)); - - v8::Persistent persistent(isolate, info.This()); - persistent.SetWeak(); - - info.This()->SetInternalField(1, interiorRef); + V8Helpers::SetObjectClass(info.GetIsolate(), info.This(), V8Class::ObjectClass::INTERIOR); + info.This()->SetInternalField(1, info[0]); } static void GetForInteriorID(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, interiorId); + V8_CHECK_ARGS_LEN(1) + V8_ARG_TO_UINT(1, interiorId) std::vector> args{ V8Helpers::JSValue(interiorId) }; @@ -33,60 +29,59 @@ static void GetForInteriorID(const v8::FunctionCallbackInfo& info) V8_RETURN(v8Interior.New(isolate->GetEnteredOrMicrotaskContext(), args)); } -static void GetRoomIndexByHash(const v8::FunctionCallbackInfo& info) +static void RoomCountGetter(v8::Local, const v8::PropertyCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, hash); + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") - V8_RETURN_INT(interior->GetRoomIndexByHash(hash)); + V8_RETURN_UINT(interior->GetRoomCount()); } -static void GetRoomName(const v8::FunctionCallbackInfo& info) +static void PortalCountGetter(v8::Local, const v8::PropertyCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, roomIndex); + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") - V8_RETURN_STRING(interior->GetRoomName(roomIndex)); + V8_RETURN_UINT(interior->GetPortalCount()); } -static void GetRoomFlag(const v8::FunctionCallbackInfo& info) +static void PositionGetter(v8::Local, const v8::PropertyCallbackInfo& info) { - V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + V8_GET_ISOLATE_CONTEXT_RESOURCE() + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, roomIndex); + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") - V8_RETURN_INT(interior->GetRoomFlag(roomIndex)); + V8_RETURN_VECTOR3(interior->GetPosition()); } -static void SetRoomFlag(const v8::FunctionCallbackInfo& info) +static void RotationGetter(v8::Local, const v8::PropertyCallbackInfo& info) { - V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + V8_GET_ISOLATE_CONTEXT_RESOURCE() + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) - V8_CHECK_ARGS_LEN(2); - V8_ARG_TO_UINT(1, roomIndex); - V8_ARG_TO_UINT(1, roomFlag); + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") - interior->SetRoomFlag(roomIndex, roomFlag); + V8_RETURN_VECTOR3(interior->GetRotation()); } -static void GetRoomExtents(const v8::FunctionCallbackInfo& info) +static void EntitiesExtentsGetter(v8::Local, const v8::PropertyCallbackInfo& info) { - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + V8_GET_ISOLATE_CONTEXT_RESOURCE() + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, roomId); + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") - auto extentInfo = interior->GetRoomExtents(roomId); + auto extentInfo = interior->GetEntitiesExtents(); v8::Local obj = v8::Object::New(isolate); obj->Set(ctx, V8Helpers::JSValue("min"), resource->CreateVector3(extentInfo.min)); @@ -94,215 +89,57 @@ static void GetRoomExtents(const v8::FunctionCallbackInfo& info) V8_RETURN(obj); } -static void SetRoomExtents(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_CHECK_ARGS_LEN(3); - V8_ARG_TO_UINT(1, roomIndex); - V8_ARG_TO_VECTOR3(2, min); - V8_ARG_TO_VECTOR3(3, max); - - interior->SetRoomExtents(roomIndex, { min, max }); -} - -static void GetRoomTimecycle(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, roomIndex); - - V8_RETURN_UINT(interior->GetRoomTimecycle(roomIndex)); -} - -static void SetRoomTimecycle(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_CHECK_ARGS_LEN(2); - V8_ARG_TO_UINT(1, roomIndex); - V8_ARG_TO_UINT(2, timecycleHash); - - interior->SetRoomTimecycle(roomIndex, timecycleHash); -} - -static void GetPortalCornerPosition(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_CHECK_ARGS_LEN(2); - V8_ARG_TO_UINT(1, portalIndex); - V8_ARG_TO_UINT(2, cornerIndex); - - V8_RETURN_VECTOR3(interior->GetPortalCornerPosition(portalIndex, cornerIndex)); -} - -static void SetPortalCornerPosition(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_CHECK_ARGS_LEN(3); - V8_ARG_TO_UINT(1, portalIndex); - V8_ARG_TO_UINT(2, cornerIndex); - V8_ARG_TO_VECTOR3(3, position); - - interior->SetPortalCornerPosition(portalIndex, cornerIndex, position); -} - -static void GetPortalRoomFrom(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, portalIndex); - - V8_RETURN_INT(interior->GetPortalRoomFrom(portalIndex)); -} - -static void SetPortalRoomFrom(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_CHECK_ARGS_LEN(2); - V8_ARG_TO_UINT(1, portalIndex); - V8_ARG_TO_INT(2, roomFrom); - - interior->SetPortalRoomFrom(portalIndex, roomFrom); -} - -static void GetPortalRoomTo(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, portalIndex); - - V8_RETURN_INT(interior->GetPortalRoomTo(portalIndex)); -} - -static void SetPortalRoomTo(const v8::FunctionCallbackInfo& info) +static void GetRoomByHash(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) - V8_CHECK_ARGS_LEN(2); - V8_ARG_TO_UINT(1, portalIndex); - V8_ARG_TO_INT(2, roomTo); + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") - interior->SetPortalRoomTo(portalIndex, roomTo); -} + V8_CHECK_ARGS_LEN(1) + V8_ARG_TO_UINT(1, roomHash) -static void GetPortalFlag(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + std::vector> args{ V8Helpers::JSValue(interiorId), V8Helpers::JSValue(roomHash), V8Helpers::JSValue(false) }; - V8_CHECK_ARGS_LEN(1); - V8_ARG_TO_UINT(1, portalIndex); - - V8_RETURN_INT(interior->GetPortalFlag(portalIndex)); + extern V8Class v8InteriorRoom; + V8_RETURN(v8InteriorRoom.New(isolate->GetEnteredOrMicrotaskContext(), args)); } -static void SetPortalFlag(const v8::FunctionCallbackInfo& info) +static void GetRoomByIndex(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_CHECK_ARGS_LEN(2); - V8_ARG_TO_UINT(1, portalIndex); - V8_ARG_TO_INT(2, flag); - - interior->SetPortalFlag(portalIndex, flag); -} - -static void RoomCountGetter(v8::Local, const v8::PropertyCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_RETURN_UINT(interior->GetRoomCount()); -} - -static void PortalCountGetter(v8::Local, const v8::PropertyCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - V8_RETURN_UINT(interior->GetPortalCount()); -} + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) -static void PositionGetter(v8::Local, const v8::PropertyCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") - V8_RETURN_VECTOR3(interior->GetPosition()); -} + V8_CHECK_ARGS_LEN(1) + V8_ARG_TO_UINT(1, roomIndex) -static void RotationGetter(v8::Local, const v8::PropertyCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); + std::vector> args{ V8Helpers::JSValue(interiorId), V8Helpers::JSValue(roomIndex), V8Helpers::JSValue(true) }; - V8_RETURN_VECTOR3(interior->GetRotation()); + extern V8Class v8InteriorRoom; + V8_RETURN(v8InteriorRoom.New(isolate->GetEnteredOrMicrotaskContext(), args)); } -static void EntitiesExtentsGetter(v8::Local, const v8::PropertyCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_INTERNAL_FIELD_EXTERNAL(1, interior, alt::IInterior); - - auto extentInfo = interior->GetEntitiesExtents(); - - v8::Local obj = v8::Object::New(isolate); - obj->Set(ctx, V8Helpers::JSValue("min"), resource->CreateVector3(extentInfo.min)); - obj->Set(ctx, V8Helpers::JSValue("max"), resource->CreateVector3(extentInfo.max)); - V8_RETURN(obj); -} - -extern V8Class v8Interior("Interior", Constructor, [](v8::Local tpl) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - - tpl->InstanceTemplate()->SetInternalFieldCount(static_cast(V8Class::InternalFields::COUNT)); - - V8Helpers::SetMethod(isolate, tpl, "getRoomIndexByHash", &GetRoomIndexByHash); - V8Helpers::SetMethod(isolate, tpl, "getRoomName", &GetRoomName); - - V8Helpers::SetMethod(isolate, tpl, "getRoomFlag", &GetRoomFlag); - V8Helpers::SetMethod(isolate, tpl, "setRoomFlag", &SetRoomFlag); - - V8Helpers::SetMethod(isolate, tpl, "getRoomExtents", &GetRoomExtents); - V8Helpers::SetMethod(isolate, tpl, "setRoomExtents", &SetRoomExtents); - - V8Helpers::SetMethod(isolate, tpl, "getRoomTimecycle", &GetRoomTimecycle); - V8Helpers::SetMethod(isolate, tpl, "setRoomTimecycle", &SetRoomTimecycle); - - V8Helpers::SetMethod(isolate, tpl, "getPortalCornerPosition", &GetPortalCornerPosition); - V8Helpers::SetMethod(isolate, tpl, "setPortalCornerPosition", &SetPortalCornerPosition); - - V8Helpers::SetMethod(isolate, tpl, "getPortalRoomFrom", &GetPortalRoomFrom); - V8Helpers::SetMethod(isolate, tpl, "setPortalRoomFrom", &SetPortalRoomFrom); +extern V8Class v8Interior("Interior", + Constructor, + [](v8::Local tpl) + { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); - V8Helpers::SetMethod(isolate, tpl, "getPortalRoomTo", &GetPortalRoomTo); - V8Helpers::SetMethod(isolate, tpl, "setPortalRoomTo", &SetPortalRoomTo); + tpl->InstanceTemplate()->SetInternalFieldCount(static_cast(V8Class::InternalFields::COUNT)); - V8Helpers::SetMethod(isolate, tpl, "getPortalFlag", &GetPortalFlag); - V8Helpers::SetMethod(isolate, tpl, "setPortalFlag", &SetPortalFlag); + V8Helpers::SetMethod(isolate, tpl, "getRoomByHash", &GetRoomByHash); + V8Helpers::SetMethod(isolate, tpl, "getRoomByIndex", &GetRoomByIndex); + //V8Helpers::SetMethod(isolate, tpl, "getPortalByIndex", &GetPortalByIndex); - V8Helpers::SetAccessor(isolate, tpl, "roomCount", &RoomCountGetter); - V8Helpers::SetAccessor(isolate, tpl, "portalCount", &PortalCountGetter); - V8Helpers::SetAccessor(isolate, tpl, "pos", &PositionGetter); - V8Helpers::SetAccessor(isolate, tpl, "rot", &RotationGetter); - V8Helpers::SetAccessor(isolate, tpl, "entitiesExtents", &EntitiesExtentsGetter); + V8Helpers::SetAccessor(isolate, tpl, "roomCount", &RoomCountGetter); + V8Helpers::SetAccessor(isolate, tpl, "portalCount", &PortalCountGetter); + V8Helpers::SetAccessor(isolate, tpl, "pos", &PositionGetter); + V8Helpers::SetAccessor(isolate, tpl, "rot", &RotationGetter); + V8Helpers::SetAccessor(isolate, tpl, "entitiesExtents", &EntitiesExtentsGetter); - V8Helpers::SetStaticMethod(isolate, tpl, "getForInteriorID", &GetForInteriorID); -}); + V8Helpers::SetStaticMethod(isolate, tpl, "getForInteriorID", &GetForInteriorID); + }); diff --git a/client/src/bindings/InteriorRoom.cpp b/client/src/bindings/InteriorRoom.cpp new file mode 100644 index 00000000..de1e7be5 --- /dev/null +++ b/client/src/bindings/InteriorRoom.cpp @@ -0,0 +1,67 @@ +#include "../CV8Resource.h" +#include "V8Class.h" +#include "cpp-sdk/script-objects/IInteriorRoom.h" + +static void Constructor(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_CONSTRUCTOR() + + V8_CHECK_ARGS_LEN(3) + V8_ARG_TO_UINT(1, interiorId) + V8_ARG_TO_UINT(2, value) + V8_ARG_TO_BOOLEAN(3, isIndex) + + std::shared_ptr interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") + + if(isIndex) + { + std::shared_ptr room = interior->GetRoomByIndex(value); + V8_CHECK(interior, "room doesn't exist") + } + else + { + std::shared_ptr room = interior->GetRoomByHash(value); + V8_CHECK(interior, "room doesn't exist") + } + + V8Helpers::SetObjectClass(info.GetIsolate(), info.This(), V8Class::ObjectClass::INTERIOR_ROOM); + info.This()->SetInternalField(1, info[0]); + info.This()->SetInternalField(2, info[1]); + info.This()->SetInternalField(3, info[2]); +} + +static void GetRoomName(v8::Local, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) + V8_GET_THIS_INTERNAL_FIELD_UINT32(2, value) + V8_GET_THIS_INTERNAL_FIELD_BOOLEAN(3, isIndex) + + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") + std::shared_ptr room; + if(isIndex) + { + room = interior->GetRoomByIndex(value); + V8_CHECK(interior, "room doesn't exist") + } + else + { + room = interior->GetRoomByHash(value); + V8_CHECK(interior, "room doesn't exist") + } + + V8_RETURN_STRING(room->GetName()); +} + +extern V8Class v8InteriorRoom("InteriorRoom", + Constructor, + [](v8::Local tpl) + { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + + tpl->InstanceTemplate()->SetInternalFieldCount(4); + V8Helpers::SetAccessor(isolate, tpl, "name", &GetRoomName); + }); diff --git a/shared/V8Class.h b/shared/V8Class.h index c38c47b2..d4e3708f 100644 --- a/shared/V8Class.h +++ b/shared/V8Class.h @@ -44,6 +44,8 @@ class V8Class HANDLING, HANDLING_DATA, INTERIOR, + INTERIOR_ROOM, + INTERIOR_PORTAL, MAP_ZOOM_DATA, WEAPON_DATA }; diff --git a/shared/deps/cpp-sdk b/shared/deps/cpp-sdk index 45a82a3a..aaa0a1c4 160000 --- a/shared/deps/cpp-sdk +++ b/shared/deps/cpp-sdk @@ -1 +1 @@ -Subproject commit 45a82a3ae3cbed371a46f6390db261377a59693d +Subproject commit aaa0a1c40cf7a1cbb9e4043b9f157159d9727009 diff --git a/shared/helpers/Macros.h b/shared/helpers/Macros.h index b0604b5c..26f965a4 100644 --- a/shared/helpers/Macros.h +++ b/shared/helpers/Macros.h @@ -66,6 +66,10 @@ V8_CHECK(val, "baseobject is not of type " #type); \ } +#define V8_GET_THIS_INTERNAL_FIELD_BOOLEAN(idx, val) \ + V8_CHECK(info.This()->InternalFieldCount() > idx - 1, "Invalid internal field count (is the 'this' context correct?)"); \ + auto val = info.This()->GetInternalField(idx).As()->BooleanValue(isolate); + #define V8_GET_THIS_INTERNAL_FIELD_INTEGER(idx, val) \ V8_CHECK(info.This()->InternalFieldCount() > idx - 1, "Invalid internal field count (is the 'this' context correct?)"); \ auto val = info.This()->GetInternalField(idx).As()->IntegerValue(ctx).ToChecked(); From f4c8bb5d12bc086cbf4de5681db56fdcc6133a71 Mon Sep 17 00:00:00 2001 From: xshady <54737754+xxshady@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:32:55 +0300 Subject: [PATCH 4/5] add interior bindings (#328) --- client/src/bindings/ClientBindingsMain.cpp | 5 +- client/src/bindings/Interior.cpp | 81 ++++++---- client/src/bindings/InteriorPortal.cpp | 177 +++++++++++++++++++++ client/src/bindings/InteriorRoom.cpp | 123 ++++++++++---- shared/deps/cpp-sdk | 2 +- shared/helpers/Macros.h | 23 +++ 6 files changed, 344 insertions(+), 67 deletions(-) create mode 100644 client/src/bindings/InteriorPortal.cpp diff --git a/client/src/bindings/ClientBindingsMain.cpp b/client/src/bindings/ClientBindingsMain.cpp index 9f1b96b8..95a9d08c 100644 --- a/client/src/bindings/ClientBindingsMain.cpp +++ b/client/src/bindings/ClientBindingsMain.cpp @@ -1360,7 +1360,7 @@ extern V8Module sharedModule; extern V8Class v8Player, v8Player, v8Vehicle, v8WebView, v8HandlingData, v8LocalStorage, v8MemoryBuffer, v8MapZoomData, v8Discord, v8Voice, v8WebSocketClient, v8Checkpoint, v8HttpClient, v8Audio, v8LocalPlayer, v8Profiler, v8Worker, v8RmlDocument, v8RmlElement, v8WeaponData, v8FocusData, v8LocalObject, v8TextEncoder, v8TextDecoder, v8Object, v8VirtualEntityGroup, v8VirtualEntity, v8AudioFilter, v8Marker, v8Ped, v8Colshape, v8ColshapeCylinder, v8ColshapeSphere, v8ColshapeCircle, v8ColshapeCuboid, v8ColshapeRectangle, v8ColshapePolygon, v8TextLabel, - v8LocalPed, v8LocalVehicle, v8Font, v8WeaponObject, v8AudioOutput, v8AudioOutputFrontend, v8AudioOutputWorld, v8AudioOutputAttached, v8AudioCategory, v8Interior, v8InteriorRoom; + v8LocalPed, v8LocalVehicle, v8Font, v8WeaponObject, v8AudioOutput, v8AudioOutputFrontend, v8AudioOutputWorld, v8AudioOutputAttached, v8AudioCategory, v8Interior, v8InteriorRoom, v8InteriorPortal; extern V8Module altModule("alt", &sharedModule, { v8Player, @@ -1410,7 +1410,8 @@ extern V8Module altModule("alt", v8AudioOutputAttached, v8AudioCategory, v8Interior, - v8InteriorRoom }, + v8InteriorRoom, + v8InteriorPortal }, [](v8::Local ctx, v8::Local exports) { v8::Isolate* isolate = ctx->GetIsolate(); diff --git a/client/src/bindings/Interior.cpp b/client/src/bindings/Interior.cpp index a494b99a..08c1f8b1 100644 --- a/client/src/bindings/Interior.cpp +++ b/client/src/bindings/Interior.cpp @@ -4,13 +4,13 @@ static void Constructor(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_CHECK_CONSTRUCTOR() + V8_CHECK_CONSTRUCTOR(); - V8_CHECK_ARGS_LEN(1) - V8_ARG_TO_UINT(1, interiorId) + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, interiorId); std::shared_ptr interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") + V8_CHECK(interior, "interior doesn't exist"); V8Helpers::SetObjectClass(info.GetIsolate(), info.This(), V8Class::ObjectClass::INTERIOR); info.This()->SetInternalField(1, info[0]); @@ -20,9 +20,8 @@ static void GetForInteriorID(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_CHECK_ARGS_LEN(1) - V8_ARG_TO_UINT(1, interiorId) - + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, interiorId); std::vector> args{ V8Helpers::JSValue(interiorId) }; extern V8Class v8Interior; @@ -32,10 +31,10 @@ static void GetForInteriorID(const v8::FunctionCallbackInfo& info) static void RoomCountGetter(v8::Local, const v8::PropertyCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); auto interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") + V8_CHECK(interior, "interior doesn't exist"); V8_RETURN_UINT(interior->GetRoomCount()); } @@ -43,63 +42,61 @@ static void RoomCountGetter(v8::Local, const v8::PropertyCallbackInf static void PortalCountGetter(v8::Local, const v8::PropertyCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); auto interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") + V8_CHECK(interior, "interior doesn't exist"); V8_RETURN_UINT(interior->GetPortalCount()); } static void PositionGetter(v8::Local, const v8::PropertyCallbackInfo& info) { - V8_GET_ISOLATE_CONTEXT_RESOURCE() - V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); auto interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") + V8_CHECK(interior, "interior doesn't exist"); V8_RETURN_VECTOR3(interior->GetPosition()); } static void RotationGetter(v8::Local, const v8::PropertyCallbackInfo& info) { - V8_GET_ISOLATE_CONTEXT_RESOURCE() - V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); auto interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") + V8_CHECK(interior, "interior doesn't exist"); V8_RETURN_VECTOR3(interior->GetRotation()); } static void EntitiesExtentsGetter(v8::Local, const v8::PropertyCallbackInfo& info) { - V8_GET_ISOLATE_CONTEXT_RESOURCE() - V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); auto interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") + V8_CHECK(interior, "interior doesn't exist"); auto extentInfo = interior->GetEntitiesExtents(); - - v8::Local obj = v8::Object::New(isolate); - obj->Set(ctx, V8Helpers::JSValue("min"), resource->CreateVector3(extentInfo.min)); - obj->Set(ctx, V8Helpers::JSValue("max"), resource->CreateVector3(extentInfo.max)); + INTERIOR_EXTENT_TO_OBJECT(extentInfo, obj); V8_RETURN(obj); } static void GetRoomByHash(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); auto interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") + V8_CHECK(interior, "interior doesn't exist"); - V8_CHECK_ARGS_LEN(1) - V8_ARG_TO_UINT(1, roomHash) + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, roomHash); + // 3 args: (interiorId: uint, value: uint, valueIsRoomIndex: bool) std::vector> args{ V8Helpers::JSValue(interiorId), V8Helpers::JSValue(roomHash), V8Helpers::JSValue(false) }; extern V8Class v8InteriorRoom; @@ -109,20 +106,38 @@ static void GetRoomByHash(const v8::FunctionCallbackInfo& info) static void GetRoomByIndex(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); auto interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") + V8_CHECK(interior, "interior doesn't exist"); - V8_CHECK_ARGS_LEN(1) - V8_ARG_TO_UINT(1, roomIndex) + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, roomIndex); + // 3 args: (interiorId: uint, value: uint, valueIsRoomIndex: bool) std::vector> args{ V8Helpers::JSValue(interiorId), V8Helpers::JSValue(roomIndex), V8Helpers::JSValue(true) }; extern V8Class v8InteriorRoom; V8_RETURN(v8InteriorRoom.New(isolate->GetEnteredOrMicrotaskContext(), args)); } +static void GetPortalByIndex(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); + + auto interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist"); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, portalIndex); + + std::vector> args{ V8Helpers::JSValue(interiorId), V8Helpers::JSValue(portalIndex) }; + + extern V8Class v8InteriorPortal; + V8_RETURN(v8InteriorPortal.New(isolate->GetEnteredOrMicrotaskContext(), args)); +} + extern V8Class v8Interior("Interior", Constructor, [](v8::Local tpl) @@ -133,7 +148,7 @@ extern V8Class v8Interior("Interior", V8Helpers::SetMethod(isolate, tpl, "getRoomByHash", &GetRoomByHash); V8Helpers::SetMethod(isolate, tpl, "getRoomByIndex", &GetRoomByIndex); - //V8Helpers::SetMethod(isolate, tpl, "getPortalByIndex", &GetPortalByIndex); + V8Helpers::SetMethod(isolate, tpl, "getPortalByIndex", &GetPortalByIndex); V8Helpers::SetAccessor(isolate, tpl, "roomCount", &RoomCountGetter); V8Helpers::SetAccessor(isolate, tpl, "portalCount", &PortalCountGetter); diff --git a/client/src/bindings/InteriorPortal.cpp b/client/src/bindings/InteriorPortal.cpp new file mode 100644 index 00000000..0bb929b5 --- /dev/null +++ b/client/src/bindings/InteriorPortal.cpp @@ -0,0 +1,177 @@ +#include "../CV8Resource.h" +#include "V8Class.h" +#include "cpp-sdk/script-objects/IInteriorPortal.h" + +static void Constructor(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_CONSTRUCTOR() + + V8_CHECK_ARGS_LEN(3) + V8_ARG_TO_UINT(1, interiorId) + V8_ARG_TO_UINT(2, portalIndex) + + std::shared_ptr interior = alt::ICore::Instance().GetInterior(interiorId); + V8_CHECK(interior, "interior doesn't exist") + + std::shared_ptr portal = interior->GetPortalByIndex(portalIndex); + V8_CHECK(portal, "interior portal doesn't exist"); + + V8Helpers::SetObjectClass(info.GetIsolate(), info.This(), V8Class::ObjectClass::INTERIOR_PORTAL); + info.This()->SetInternalField(1, info[0]); + info.This()->SetInternalField(2, info[1]); +} + +static void IndexGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_INTERNAL_FIELD_UINT32(2, portalIndex); + V8_RETURN_UINT(portalIndex); +} + +static void RoomFromGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + // TODO: is is expected that it returns uint but setter takes int? (and same for RoomTo, same for Flag) + V8_RETURN_INT(portal->GetRoomFrom()); +} + +static void RoomFromSetter(v8::Local, v8::Local value, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_TO_UINT(value, roomFrom); + portal->SetRoomFrom(roomFrom); +} + +static void RoomToGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_RETURN_INT(portal->GetRoomTo()); +} + +static void RoomToSetter(v8::Local, v8::Local value, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_TO_UINT(value, roomTo); + portal->SetRoomTo(roomTo); +} + +static void FlagGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_RETURN_INT(portal->GetFlag()); +} + +static void FlagSetter(v8::Local, v8::Local value, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_TO_UINT(value, flag); + portal->SetFlag(flag); +} + +static void CornerCountGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_RETURN_UINT(portal->GetCornerCount()); +} + +static void GetEntityArchetype(const v8::FunctionCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, entityIndex); + V8_RETURN_UINT(portal->GetEntityArcheType(entityIndex)); +} + +static void GetEntityFlag(const v8::FunctionCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_INT(1, entityIndex); + V8_RETURN_INT(portal->GetEntityFlag(entityIndex)); +} + +static void SetEntityFlag(const v8::FunctionCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_CHECK_ARGS_LEN(2); + V8_ARG_TO_INT(1, entityIndex); + V8_ARG_TO_INT(2, flag); + portal->SetEntityFlag(entityIndex, flag); +} + +static void GetEntityPos(const v8::FunctionCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_INT(1, entityIndex); + V8_GET_RESOURCE(); + V8_RETURN_VECTOR3(portal->GetEntityPosition(entityIndex)); +} + +static void GetEntityRot(const v8::FunctionCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_INT(1, entityIndex); + V8_GET_RESOURCE(); + V8_RETURN_VECTOR3(portal->GetEntityRotation(entityIndex)); +} + +static void GetCornerPos(const v8::FunctionCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_UINT(1, cornerIndex); + V8_GET_RESOURCE(); + V8_RETURN_VECTOR3(portal->GetCornerPosition(cornerIndex)); +} + + +static void SetCornerPos(const v8::FunctionCallbackInfo& info) +{ + GET_THIS_INTERIOR_PORTAL(portal); + + V8_CHECK_ARGS_LEN(2); + V8_ARG_TO_INT(1, cornerIndex); + V8_ARG_TO_VECTOR3(2, pos); + portal->SetCornerPosition(cornerIndex, pos); +} + +extern V8Class v8InteriorPortal("InteriorPortal", + Constructor, + [](v8::Local tpl) + { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + + // TODO: not sure about this one + tpl->InstanceTemplate()->SetInternalFieldCount(3); + + V8Helpers::SetAccessor(isolate, tpl, "index", &IndexGetter); + V8Helpers::SetAccessor(isolate, tpl, "cornerCount", &CornerCountGetter); + V8Helpers::SetAccessor(isolate, tpl, "roomFrom", &RoomFromGetter, &RoomFromSetter); + V8Helpers::SetAccessor(isolate, tpl, "roomTo", &RoomToGetter, &RoomToSetter); + V8Helpers::SetAccessor(isolate, tpl, "flag", &FlagGetter, &FlagSetter); + + V8Helpers::SetMethod(isolate, tpl, "getCornerPos", &GetCornerPos); + V8Helpers::SetMethod(isolate, tpl, "getEntityArchetype", &GetEntityArchetype); + V8Helpers::SetMethod(isolate, tpl, "getEntityFlag", &GetEntityFlag); + V8Helpers::SetMethod(isolate, tpl, "setEntityFlag", &SetEntityFlag); + V8Helpers::SetMethod(isolate, tpl, "getEntityPos", &GetEntityPos); + V8Helpers::SetMethod(isolate, tpl, "getEntityRot", &GetEntityRot); + + V8Helpers::SetMethod(isolate, tpl, "setCornerPos", &SetCornerPos); + }); diff --git a/client/src/bindings/InteriorRoom.cpp b/client/src/bindings/InteriorRoom.cpp index de1e7be5..ef19a595 100644 --- a/client/src/bindings/InteriorRoom.cpp +++ b/client/src/bindings/InteriorRoom.cpp @@ -5,63 +5,124 @@ static void Constructor(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_CHECK_CONSTRUCTOR() + V8_CHECK_CONSTRUCTOR(); - V8_CHECK_ARGS_LEN(3) - V8_ARG_TO_UINT(1, interiorId) - V8_ARG_TO_UINT(2, value) - V8_ARG_TO_BOOLEAN(3, isIndex) + V8_CHECK_ARGS_LEN(3); + V8_ARG_TO_UINT(1, interiorId); + V8_ARG_TO_UINT(2, value); + V8_ARG_TO_BOOLEAN(3, isIndex); std::shared_ptr interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") + V8_CHECK(interior, "interior doesn't exist"); + std::shared_ptr room; if(isIndex) { - std::shared_ptr room = interior->GetRoomByIndex(value); - V8_CHECK(interior, "room doesn't exist") + room = interior->GetRoomByIndex(value); + V8_CHECK(room, "interior room doesn't exist"); } else { - std::shared_ptr room = interior->GetRoomByHash(value); - V8_CHECK(interior, "room doesn't exist") + room = interior->GetRoomByHash(value); } + V8_CHECK(room, "interior room doesn't exist"); + + uint32_t roomIndex = room->GetIndex(); V8Helpers::SetObjectClass(info.GetIsolate(), info.This(), V8Class::ObjectClass::INTERIOR_ROOM); info.This()->SetInternalField(1, info[0]); - info.This()->SetInternalField(2, info[1]); - info.This()->SetInternalField(3, info[2]); + info.This()->SetInternalField(2, V8Helpers::JSValue(roomIndex)); } -static void GetRoomName(v8::Local, const v8::PropertyCallbackInfo& info) +static void IndexGetter(v8::Local, const v8::PropertyCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId) - V8_GET_THIS_INTERNAL_FIELD_UINT32(2, value) - V8_GET_THIS_INTERNAL_FIELD_BOOLEAN(3, isIndex) + V8_GET_THIS_INTERNAL_FIELD_UINT32(2, roomIndex); + V8_RETURN_UINT(roomIndex); +} - auto interior = alt::ICore::Instance().GetInterior(interiorId); - V8_CHECK(interior, "interior doesn't exist") - std::shared_ptr room; - if(isIndex) - { - room = interior->GetRoomByIndex(value); - V8_CHECK(interior, "room doesn't exist") - } - else - { - room = interior->GetRoomByHash(value); - V8_CHECK(interior, "room doesn't exist") - } +static void NameGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_ROOM(room); V8_RETURN_STRING(room->GetName()); } +static void NameHashGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_ROOM(room); + + V8_RETURN_UINT(room->GetNameHash()); +} + +static void FlagGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_ROOM(room); + + V8_RETURN_INT(room->GetFlag()); +} + +static void FlagSetter(v8::Local, v8::Local value, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_ROOM(room); + + V8_TO_UINT(value, flag); + room->SetFlag(flag); +} + +static void TimecycleGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_ROOM(room); + + V8_RETURN_UINT(room->GetTimecycle()); +} + +static void TimecycleSetter(v8::Local, v8::Local value, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_ROOM(room); + + V8_TO_UINT(value, timecycle); + room->SetTimecycle(timecycle); +} + +static void ExtentsGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_ROOM(room); + + auto extentInfo = room->GetExtents(); + V8_GET_RESOURCE(); + INTERIOR_EXTENT_TO_OBJECT(extentInfo, obj); + V8_RETURN(obj); +} + +static void ExtentsSetter(v8::Local, v8::Local value, const v8::PropertyCallbackInfo& info) +{ + GET_THIS_INTERIOR_ROOM(room); + + V8_TO_OBJECT(value, extentInfo); + + auto minRaw = extentInfo->Get(ctx, v8::String::NewFromUtf8(isolate, "min").ToLocalChecked()).ToLocalChecked(); + V8_TO_VECTOR3(minRaw, min); + + auto maxRaw = extentInfo->Get(ctx, v8::String::NewFromUtf8(isolate, "max").ToLocalChecked()).ToLocalChecked(); + V8_TO_VECTOR3(maxRaw, max); + + room->SetExtents({ min, max }); +} + extern V8Class v8InteriorRoom("InteriorRoom", Constructor, [](v8::Local tpl) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); - tpl->InstanceTemplate()->SetInternalFieldCount(4); - V8Helpers::SetAccessor(isolate, tpl, "name", &GetRoomName); + // TODO: not sure about this one + tpl->InstanceTemplate()->SetInternalFieldCount(3); + + V8Helpers::SetAccessor(isolate, tpl, "index", &IndexGetter); + V8Helpers::SetAccessor(isolate, tpl, "name", &NameGetter); + V8Helpers::SetAccessor(isolate, tpl, "nameHash", &NameHashGetter); + V8Helpers::SetAccessor(isolate, tpl, "flag", &FlagGetter, FlagSetter); + V8Helpers::SetAccessor(isolate, tpl, "timecycle", &TimecycleGetter, TimecycleSetter); + V8Helpers::SetAccessor(isolate, tpl, "extents", &ExtentsGetter, ExtentsSetter); }); diff --git a/shared/deps/cpp-sdk b/shared/deps/cpp-sdk index aaa0a1c4..f713e288 160000 --- a/shared/deps/cpp-sdk +++ b/shared/deps/cpp-sdk @@ -1 +1 @@ -Subproject commit aaa0a1c40cf7a1cbb9e4043b9f157159d9727009 +Subproject commit f713e288b5fa34fb6407ed0ff090519bb17fc60c diff --git a/shared/helpers/Macros.h b/shared/helpers/Macros.h index 26f965a4..68d21e3e 100644 --- a/shared/helpers/Macros.h +++ b/shared/helpers/Macros.h @@ -376,3 +376,26 @@ Log::Warning << V8Helpers::SourceLocation::GetCurrent(isolate, resource).ToString(isolate) << " " << oldName << " is deprecated and will be removed in future versions. Consider using " \ << newName << " instead" << Log::Endl; \ } + +#define GET_THIS_INTERIOR_PORTAL(val) \ + V8_GET_ISOLATE_CONTEXT(); \ + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); \ + V8_GET_THIS_INTERNAL_FIELD_UINT32(2, portalIndex); \ + auto interior = alt::ICore::Instance().GetInterior(interiorId); \ + V8_CHECK(interior, "interior doesn't exist"); \ + std::shared_ptr portal = interior->GetPortalByIndex(portalIndex); \ + V8_CHECK(portal, "interior portal doesn't exist") + +#define GET_THIS_INTERIOR_ROOM(val) \ + V8_GET_ISOLATE_CONTEXT(); \ + V8_GET_THIS_INTERNAL_FIELD_UINT32(1, interiorId); \ + V8_GET_THIS_INTERNAL_FIELD_UINT32(2, roomIndex); \ + auto interior = alt::ICore::Instance().GetInterior(interiorId); \ + V8_CHECK(interior, "interior doesn't exist"); \ + std::shared_ptr val = interior->GetRoomByIndex(roomIndex); \ + V8_CHECK(val, "interior room doesn't exist") + +#define INTERIOR_EXTENT_TO_OBJECT(extentInfo, val) \ + v8::Local val = v8::Object::New(isolate); \ + val->Set(ctx, V8Helpers::JSValue("min"), resource->CreateVector3(extentInfo.min)); \ + val->Set(ctx, V8Helpers::JSValue("max"), resource->CreateVector3(extentInfo.max)); \ From 63de02d6025b7a3bd60c7c9cbe0fa1744ce3dcd1 Mon Sep 17 00:00:00 2001 From: Till Schreiber Date: Fri, 25 Oct 2024 17:09:36 +0200 Subject: [PATCH 5/5] ALTV-426 update sdk --- shared/deps/cpp-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/deps/cpp-sdk b/shared/deps/cpp-sdk index f713e288..ed282b6c 160000 --- a/shared/deps/cpp-sdk +++ b/shared/deps/cpp-sdk @@ -1 +1 @@ -Subproject commit f713e288b5fa34fb6407ed0ff090519bb17fc60c +Subproject commit ed282b6c1f63e13c10900b7061f95823f0c7244b