Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add alt.Object.streamedIn #313

Merged
merged 3 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions client/src/CV8ScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ void CV8ScriptRuntime::OnEntityStreamIn(alt::IEntity* entity)
streamedInPeds.insert({ entity->GetID(), dynamic_cast<alt::IPed*>(entity) });
break;
}
case alt::IEntity::Type::LOCAL_OBJECT:
case alt::IEntity::Type::OBJECT:
{
streamedInObjects.insert({ entity->GetID(), dynamic_cast<alt::IObject*>(entity) });
break;
}
}
}

Expand All @@ -437,12 +443,19 @@ void CV8ScriptRuntime::OnEntityStreamOut(alt::IEntity* entity)
streamedInPeds.erase(entity->GetID());
break;
}
case alt::IEntity::Type::LOCAL_OBJECT:
case alt::IEntity::Type::OBJECT:
{
streamedInObjects.erase(entity->GetID());
break;
}
}
}
void CV8ScriptRuntime::OnDisconnect()
{
streamedInPlayers.clear();
streamedInVehicles.clear();
streamedInPeds.clear();
streamedInObjects.clear();
resourcesLoaded = false;
}
12 changes: 8 additions & 4 deletions client/src/CV8ScriptRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class CV8ScriptRuntime : public alt::IScriptRuntime, public IRuntimeEventHandler
v8::CpuProfiler* profiler;
uint32_t profilerSamplingInterval = 100;

std::unordered_map<uint16_t, alt::IPlayer*> streamedInPlayers;
std::unordered_map<uint16_t, alt::IVehicle*> streamedInVehicles;
std::unordered_map<uint16_t, alt::IPed*> streamedInPeds;
std::unordered_map<uint32_t, alt::IPlayer*> streamedInPlayers;
std::unordered_map<uint32_t, alt::IVehicle*> streamedInVehicles;
std::unordered_map<uint32_t, alt::IPed*> streamedInPeds;
std::unordered_map<uint32_t, alt::IObject*> streamedInObjects;

uint32_t activeWorkers = 0;

Expand Down Expand Up @@ -218,7 +219,10 @@ class CV8ScriptRuntime : public alt::IScriptRuntime, public IRuntimeEventHandler
{
return streamedInPeds;
}

auto GetStreamedInObjects()
{
return streamedInObjects;
}

void OnDisconnect();
};
18 changes: 18 additions & 0 deletions client/src/bindings/Object.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "V8Helpers.h"
#include "helpers/BindHelpers.h"
#include "../CV8ScriptRuntime.h"

static void AllGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
{
Expand All @@ -25,6 +26,22 @@ static void CountGetter(v8::Local<v8::String> name, const v8::PropertyCallbackIn
V8_RETURN_UINT(alt::ICore::Instance().GetBaseObjects(alt::IBaseObject::Type::OBJECT).size());
}

static void StreamedInGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();

auto streamedIn = CV8ScriptRuntime::Instance().GetStreamedInObjects();
auto arr = v8::Array::New(isolate, streamedIn.size());
int i = 0;
for(auto kv : streamedIn)
{
arr->Set(ctx, i, resource->GetOrCreateEntity(kv.second, "Object")->GetJSVal(isolate));
i++;
}

V8_RETURN(arr);
}

static void StaticGetByScriptID(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
Expand Down Expand Up @@ -94,6 +111,7 @@ extern V8Class v8Object("Object", v8Entity, [](v8::Local<v8::FunctionTemplate> t

V8Helpers::SetStaticAccessor(isolate, tpl, "all", &AllGetter);
V8Helpers::SetStaticAccessor(isolate, tpl, "count", &CountGetter);
V8Helpers::SetStaticAccessor(isolate, tpl, "streamedIn", &StreamedInGetter);

V8Helpers::SetAccessor<alt::IObject, uint8_t, &alt::IObject::GetAlpha>(isolate, tpl, "alpha");
V8Helpers::SetAccessor<alt::IObject, uint8_t, &alt::IObject::GetTextureVariation>(isolate, tpl, "textureVariation");
Expand Down
Loading