Skip to content

Commit

Permalink
fix(client): Temporary workaround for RmlElement ScriptObjects creati…
Browse files Browse the repository at this point in the history
…on / removal (#167)
  • Loading branch information
xLuxy authored Jan 30, 2024
1 parent 51e1e20 commit a629462
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions client/src/classes/RmlDocument.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Class.h"
#include "interfaces/IResource.h"

static void Show(js::FunctionContext& ctx)
{
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
7 changes: 6 additions & 1 deletion client/src/classes/RmlElement.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Class.h"
#include "interfaces/IResource.h"

static void ChildrenGetter(js::PropertyContext& ctx)
{
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions shared/src/classes/BaseObject.cpp
Original file line number Diff line number Diff line change
@@ -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<alt::IBaseObject>() != nullptr);
}

Expand Down
24 changes: 24 additions & 0 deletions shared/src/interfaces/IAltResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<alt::IRmlElement*>(object), GetCurrentResource());
}
#endif

IScriptObjectHandler::DestroyScriptObject(object);
}

Expand Down

0 comments on commit a629462

Please sign in to comment.