Skip to content

Commit

Permalink
Only create ScriptObject inside OnCreateBaseObject & `OnRemoveBaseO…
Browse files Browse the repository at this point in the history
…bject` + cleanup (#157)

* fix(client, compat): Remove code duplicates
* client: Only create / delete ScriptObjects in `OnCreateBaseObject` / `OnRemoveBaseObject`
* fix(shared, bindings): Fix `addEntityToAll` throwing `Cannot read properties of null`
* Cleanup NativeInvoker GetArg for pointers
* move resource cache init to IScriptObjectHandler
  • Loading branch information
xLuxy authored Jan 3, 2024
1 parent 11fe217 commit 4f2fbfb
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 25 deletions.
9 changes: 0 additions & 9 deletions client/js/compatibility/classes/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ export class Entity extends alt.Entity {
extendAltEntityClass(this, SharedEntity, WorldObject, BaseObject);
}

static getByScriptID(...args) {
// NOTE (xLuxy): This method in v1 only returns WorldObject
if (typeof super.getByScriptID == "function") {
return super.getByScriptID(...args);
}

return null;
}

get isSpawned() {
if (![alt.Enums.BaseObjectType.PLAYER, alt.Enums.BaseObjectType.LOCAL_PLAYER].includes(super.type)) {
return super.isSpawned;
Expand Down
2 changes: 1 addition & 1 deletion client/js/compatibility/classes/weaponObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ class WeaponObject extends alt.LocalObject {
}
}

copyStaticAltEntityClassProperties(alt.WeaponObject, WeaponObject, Entity, WorldObject, BaseObject);
copyStaticAltEntityClassProperties(WeaponObject, Entity, WorldObject, BaseObject);

cppBindings.registerCompatibilityExport("WeaponObject", WeaponObject);
2 changes: 1 addition & 1 deletion client/src/helpers/NativeInvoker.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace js
using CleanT = std::remove_pointer_t<T>;

CleanT value;
if(!ctx.GetArg(index, value, Type::INVALID, isPointer)) return false;
if(!ctx.GetArg<CleanT>(index, value)) return false;

if constexpr(isPointer)
{
Expand Down
1 change: 1 addition & 0 deletions shared/js/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ alt.Events.onBaseObjectRemove(({ object }) => {
export function addEntityToAll(entity) {
addEntityToAllWithType(entity, entity.type);
}

export function removeEntityFromAll(entity) {
entityAllSetDirty = true;
entityAllSet.delete(entity);
Expand Down
10 changes: 2 additions & 8 deletions shared/src/helpers/CallContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ namespace js

// If no type to check is specified, it will try to convert the value to the specified type
template<class T>
bool GetArg(int index, T& outValue, Type typeToCheck = Type::INVALID, bool allowNull = false)
bool GetArg(int index, T& outValue, Type typeToCheck = Type::INVALID)
{
if(errored) return false;
if(index >= info.Length() && !allowNull)
if(index >= info.Length())
{
Throw("Missing argument at index " + std::to_string(index));
return false;
Expand All @@ -255,12 +255,6 @@ namespace js
std::optional<T> result = CppValue<T>(info[index]);
if(!result.has_value())
{
if (allowNull)
{
outValue = T{};
return true;
}

Throw("Invalid argument type at index " + std::to_string(index) + ", expected " + CppTypeToString<T>() + " but got " + TypeToString(GetArgType(index)));
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions shared/src/helpers/Convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ v8::Local<v8::Value> js::JSValue(alt::IBaseObject* object)
if(!object) return v8::Null(isolate);
IResource* resource = GetCurrentResource(isolate);
if(!resource) return v8::Null(isolate);
ScriptObject* scriptObject = resource->GetOrCreateScriptObject(resource->GetContext(), object);
ScriptObject* scriptObject = resource->GetScriptObject(object);
if(!scriptObject) return v8::Null(isolate);
return scriptObject->Get();
}
Expand Down Expand Up @@ -270,7 +270,7 @@ v8::Local<v8::Value> js::MValueToJS(alt::MValueConst val)
case alt::IMValue::Type::BASE_OBJECT:
{
alt::IBaseObject* ref = std::dynamic_pointer_cast<const alt::IMValueBaseObject>(val)->RawValue();
return resource->GetOrCreateScriptObject(ctx, ref)->Get();
return resource->GetScriptObject(ref)->Get();
}
case alt::IMValue::Type::FUNCTION:
{
Expand Down
2 changes: 1 addition & 1 deletion shared/src/helpers/ValueBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ bool js::ValueDeserializer::Entity(v8::Local<v8::Value>& value)
#endif

if(!entity) return false;
value = resource->GetOrCreateScriptObject(resource->GetContext(), entity)->Get();
value = resource->GetScriptObject(entity)->Get();
return true;
}

Expand Down
7 changes: 5 additions & 2 deletions shared/src/interfaces/IAltResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ namespace js

void OnCreateBaseObject(alt::IBaseObject* object) override
{
// IScriptObjectHandler::GetOrCreateScriptObject(GetContext(), object);
if(context.IsEmpty()) return;
IResource::Scope scope(this);

IScriptObjectHandler::GetOrCreateScriptObject(GetContext(), object);
}

void OnRemoveBaseObject(alt::IBaseObject* object) override
Expand All @@ -70,7 +73,7 @@ namespace js
if(context.IsEmpty()) return;
IResource::Scope scope(this);

if(ev->GetType() == alt::CEvent::Type::RESOURCE_STOP) DestroyResourceObject(static_cast<const alt::CResourceStopEvent*>(ev)->GetResource());
if (ev->GetType() == alt::CEvent::Type::RESOURCE_STOP) DestroyResourceObject(static_cast<const alt::CResourceStopEvent*>(ev)->GetResource());

Event::SendEvent(ev, this);
}
Expand Down
1 change: 1 addition & 0 deletions shared/src/interfaces/IResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace js
{
context.Get(isolate)->SetAlignedPointerInEmbedderData(ContextInternalFieldIdx, this);
ICompatibilityHandler::Initialize();
IScriptObjectHandler::Initialize(context.Get(isolate));
}

virtual void Reset()
Expand Down
10 changes: 10 additions & 0 deletions shared/src/interfaces/IScriptObjectHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ namespace js
return customFactoryMap.contains(type);
}

void Initialize(v8::Local<v8::Context> context)
{
auto entites = alt::ICore::Instance().GetEntities();

for (auto entity : entites)
{
GetOrCreateScriptObject(context, entity);
}
}

static void BindClassToType(alt::IBaseObject::Type type, Class* class_);
};
} // namespace js
3 changes: 2 additions & 1 deletion shared/src/modules/SharedCppBindingsModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static void CreateEntity(js::FunctionContext& ctx)
return;
}

scriptObject = resource->GetOrCreateScriptObject(ctx.GetContext(), object);
scriptObject = resource->GetScriptObject(object);
if(!scriptObject)
{
if(!tryCatch.HasCaught()) ctx.Throw("Failed to create entity of type " + std::string(magic_enum::enum_name(type)));
Expand All @@ -89,6 +89,7 @@ static void CreateEntity(js::FunctionContext& ctx)
static void GetAllEntities(js::FunctionContext& ctx)
{
std::vector<alt::IEntity*> entities = alt::ICore::Instance().GetEntities();

ctx.Return(entities);
}

Expand Down

0 comments on commit 4f2fbfb

Please sign in to comment.