From a6294620881e3613e9e8cadcbf250d56894d5ef9 Mon Sep 17 00:00:00 2001 From: Marcel N <67131061+xLuxy@users.noreply.github.com> Date: Tue, 30 Jan 2024 03:02:24 +0100 Subject: [PATCH] fix(client): Temporary workaround for RmlElement ScriptObjects creation / removal (#167) --- client/src/classes/RmlDocument.cpp | 7 +++++++ client/src/classes/RmlElement.cpp | 7 ++++++- shared/src/classes/BaseObject.cpp | 3 +++ shared/src/interfaces/IAltResource.h | 24 ++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/client/src/classes/RmlDocument.cpp b/client/src/classes/RmlDocument.cpp index a962811f4..25f5e2a01 100644 --- a/client/src/classes/RmlDocument.cpp +++ b/client/src/classes/RmlDocument.cpp @@ -1,4 +1,5 @@ #include "Class.h" +#include "interfaces/IResource.h" static void Show(js::FunctionContext& ctx) { @@ -25,6 +26,9 @@ static void CreateElement(js::FunctionContext& ctx) alt::IRmlElement* element = document->CreateElement(tag); if(!ctx.Check(element != nullptr, "Failed to create element")) return; + // TODO(xLuxy): alt:V currently doesn't create BaseObject for RmlElements + ctx.GetResource()->GetOrCreateScriptObject(ctx.GetContext(), element); + ctx.Return(element); } @@ -40,6 +44,9 @@ static void CreateTextNode(js::FunctionContext& ctx) alt::IRmlElement* node = document->CreateTextNode(text); if(!ctx.Check(node != nullptr, "Failed to create text node")) return; + // TODO(xLuxy): alt:V currently doesn't create BaseObject for RmlElements + ctx.GetResource()->GetOrCreateScriptObject(ctx.GetContext(), node); + ctx.Return(node); } diff --git a/client/src/classes/RmlElement.cpp b/client/src/classes/RmlElement.cpp index fce7c2597..7e8288eac 100644 --- a/client/src/classes/RmlElement.cpp +++ b/client/src/classes/RmlElement.cpp @@ -1,4 +1,5 @@ #include "Class.h" +#include "interfaces/IResource.h" static void ChildrenGetter(js::PropertyContext& ctx) { @@ -9,7 +10,11 @@ static void ChildrenGetter(js::PropertyContext& ctx) js::Array arr(size); for(size_t i = 0; i < size; i++) { - arr.Set(i, element->GetChild(i)); + const auto children = element->GetChild(i); + // TODO(xLuxy): alt:V currently doesn't create BaseObject for RmlElements + ctx.GetResource()->GetOrCreateScriptObject(ctx.GetContext(), children); + + arr.Set(i, children); } ctx.Return(arr); diff --git a/shared/src/classes/BaseObject.cpp b/shared/src/classes/BaseObject.cpp index 29ea996ef..94a5bcd30 100644 --- a/shared/src/classes/BaseObject.cpp +++ b/shared/src/classes/BaseObject.cpp @@ -1,8 +1,11 @@ #include "Class.h" #include "cpp-sdk/ICore.h" +#include "interfaces/IResource.h" static void ValidGetter(js::PropertyContext& ctx) { + if(!ctx.CheckThis()) return; + ctx.Return(ctx.GetThisObject() != nullptr); } diff --git a/shared/src/interfaces/IAltResource.h b/shared/src/interfaces/IAltResource.h index 8c0885a4a..390ac0459 100644 --- a/shared/src/interfaces/IAltResource.h +++ b/shared/src/interfaces/IAltResource.h @@ -60,11 +60,35 @@ namespace js IScriptObjectHandler::GetOrCreateScriptObject(GetContext(), object); } +#if ALT_CLIENT_API + // NOTE(xLuxy): This is a workaround for alt:V not calling BaseObject for RmlElements + static void RemoveRmlChildrens(alt::IRmlElement* element, IResource* resource) + { + for (size_t i = 0; i < element->GetChildCount(); i++) + { + const auto children = element->GetChild(i); + + RemoveRmlChildrens(children, resource); + resource->DestroyScriptObject(children); + + alt::ICore::Instance().DestroyBaseObject(children); + } + } +#endif + void OnRemoveBaseObject(alt::IBaseObject* object) override { if(context.IsEmpty()) return; IResource::Scope scope(this); +#if ALT_CLIENT_API + // TODO(xLuxy): alt:V currently doesn't create BaseObject for RmlElements + if (object->GetType() == alt::IBaseObject::Type::RML_DOCUMENT) + { + RemoveRmlChildrens(dynamic_cast(object), GetCurrentResource()); + } +#endif + IScriptObjectHandler::DestroyScriptObject(object); }