diff --git a/client/src/bindings/Audio.cpp b/client/src/bindings/Audio.cpp index e43760f5..43ed6bb3 100644 --- a/client/src/bindings/Audio.cpp +++ b/client/src/bindings/Audio.cpp @@ -2,7 +2,9 @@ #include "helpers/BindHelpers.h" #include "V8ResourceImpl.h" #include "V8Class.h" + #include "../CV8ScriptRuntime.h" + #include "cpp-sdk/script-objects/IAudio.h" #include "cpp-sdk/script-objects/IAudioOutput.h" @@ -133,6 +135,25 @@ static void AudioCountGetter(v8::Local name, const v8::PropertyCallb V8_RETURN_UINT(alt::ICore::Instance().GetBaseObjects(alt::IBaseObject::Type::AUDIO).size()); } +static void StaticGetByID(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_INT(1, id); + + alt::IBaseObject* baseObject = alt::ICore::Instance().GetBaseObjectByID(alt::IBaseObject::Type::AUDIO, id); + + if(baseObject) + { + V8_RETURN_BASE_OBJECT(baseObject); + } + else + { + V8_RETURN_NULL(); + } +} + extern V8Class v8BaseObject; extern V8Class v8Audio("Audio", v8BaseObject, @@ -142,6 +163,8 @@ extern V8Class v8Audio("Audio", using namespace alt; v8::Isolate* isolate = v8::Isolate::GetCurrent(); + V8Helpers::SetStaticMethod(isolate, tpl, "getByID", StaticGetByID); + V8Helpers::SetStaticAccessor(isolate, tpl, "all", &AllAudioGetter); V8Helpers::SetStaticAccessor(isolate, tpl, "count", &AudioCountGetter); diff --git a/client/src/bindings/AudioFilter.cpp b/client/src/bindings/AudioFilter.cpp index ea44fe1b..9a6d2c03 100644 --- a/client/src/bindings/AudioFilter.cpp +++ b/client/src/bindings/AudioFilter.cpp @@ -174,7 +174,7 @@ static void AddChorusEffect(const v8::FunctionCallbackInfo& info) V8_GET_THIS_BASE_OBJECT(filter, alt::IAudioFilter); - V8_CHECK_ARGS_LEN(6); + V8_CHECK_ARGS_LEN(7); V8_ARG_TO_NUMBER(1, fDryMix); V8_ARG_TO_NUMBER(2, fWetMix); V8_ARG_TO_NUMBER(3, fFeedback); @@ -244,7 +244,7 @@ static void AddEcho4Effect(const v8::FunctionCallbackInfo& info) V8_GET_THIS_BASE_OBJECT(filter, alt::IAudioFilter); - V8_CHECK_ARGS_LEN(4); + V8_CHECK_ARGS_LEN(5); V8_ARG_TO_NUMBER(1, fDryMix); V8_ARG_TO_NUMBER(2, fWetMix); V8_ARG_TO_NUMBER(3, fFeedback); diff --git a/client/src/bindings/AudioOutput.cpp b/client/src/bindings/AudioOutput.cpp index da28133f..3330b10e 100644 --- a/client/src/bindings/AudioOutput.cpp +++ b/client/src/bindings/AudioOutput.cpp @@ -2,12 +2,11 @@ #include "helpers/BindHelpers.h" #include "V8ResourceImpl.h" #include "V8Class.h" + #include "../CV8ScriptRuntime.h" + #include "cpp-sdk/objects/IWorldObject.h" #include "cpp-sdk/script-objects/IAudioOutput.h" -#include "cpp-sdk/script-objects/IAudioFrontendOutput.h" -#include "cpp-sdk/script-objects/IAudioWorldOutput.h" -#include "cpp-sdk/script-objects/IAudioAttachedOutput.h" static void Constructor(const v8::FunctionCallbackInfo& info) { @@ -16,44 +15,6 @@ static void Constructor(const v8::FunctionCallbackInfo& info) V8_CHECK(false, "You can't use constructor of abstract class"); } -static void ConstructorFrontend(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_CHECK_CONSTRUCTOR(); - V8_CHECK_ARGS_LEN_MIN_MAX(0, 1); - - V8_ARG_TO_INT_OPT(1, categoryHash, alt::ICore::Instance().Hash("radio")); - - auto output = alt::ICore::Instance().CreateFrontendOutput(categoryHash, resource->GetResource()); - V8_BIND_BASE_OBJECT(output, "Failed to create AudioOutputFrontend"); -} - -static void ConstructorWorld(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_CHECK_CONSTRUCTOR(); - V8_CHECK_ARGS_LEN_MIN_MAX(1, 2); - - V8_ARG_TO_VECTOR3(1, pos); - V8_ARG_TO_INT_OPT(2, categoryHash, alt::ICore::Instance().Hash("radio")); - - auto output = alt::ICore::Instance().CreateWorldOutput(categoryHash, pos, resource->GetResource()); - V8_BIND_BASE_OBJECT(output, "Failed to create AudioOutputWorld"); -} - -static void ConstructorAttached(const v8::FunctionCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_CHECK_CONSTRUCTOR(); - V8_CHECK_ARGS_LEN_MIN_MAX(1, 2); - - V8_ARG_TO_BASE_OBJECT(1, entity, alt::IWorldObject, "IWorldObject"); - V8_ARG_TO_INT_OPT(2, categoryHash, alt::ICore::Instance().Hash("radio")); - - auto output = alt::ICore::Instance().CreateAttachedOutput(categoryHash, entity, resource->GetResource()); - V8_BIND_BASE_OBJECT(output, "Failed to create AudioOutputAttached"); -} - static void AllAudioOutputGetter(v8::Local name, const v8::PropertyCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT_RESOURCE(); @@ -66,23 +27,6 @@ static void AudioOutputCountGetter(v8::Local name, const v8::Propert V8_RETURN_UINT(alt::ICore::Instance().GetBaseObjects(alt::IBaseObject::Type::AUDIO_OUTPUT).size()); } -static void EntityGetter(v8::Local, const v8::PropertyCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_GET_THIS_BASE_OBJECT(ent, alt::IAudioAttachedOutput); - V8_RETURN_BASE_OBJECT(ent->GetEntity()); -} - -static void EntitySetter(v8::Local, v8::Local val, const v8::PropertyCallbackInfo& info) -{ - V8_GET_ISOLATE_CONTEXT(); - V8_GET_THIS_BASE_OBJECT(_this, alt::IAudioAttachedOutput); - - V8_TO_WORLDOBJECT(val, entity); - - _this->SetEntity(entity); -} - static void GetFilter(v8::Local, const v8::PropertyCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT_RESOURCE(); @@ -129,28 +73,3 @@ extern V8Class v8AudioOutput("AudioOutput", V8Helpers::SetAccessor(isolate, tpl, "category"); V8Helpers::SetAccessor(isolate, tpl, "filter", &GetFilter, &SetFilter); }); - -extern V8Class v8AudioOutputFrontend("AudioOutputFrontend", - v8AudioOutput, - &ConstructorFrontend, - [](v8::Local tpl) { - - }); - -extern V8Class v8AudioOutputWorld("AudioOutputWorld", - v8AudioOutput, - &ConstructorWorld, - [](v8::Local tpl) - { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - V8Helpers::SetAccessor(isolate, tpl, "pos"); - }); - -extern V8Class v8AudioOutputAttached("AudioOutputAttached", - v8AudioOutput, - &ConstructorAttached, - [](v8::Local tpl) - { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - V8Helpers::SetAccessor(isolate, tpl, "entity", &EntityGetter, &EntitySetter); - }); diff --git a/client/src/bindings/AudioOutputAttached.cpp b/client/src/bindings/AudioOutputAttached.cpp new file mode 100644 index 00000000..6344853f --- /dev/null +++ b/client/src/bindings/AudioOutputAttached.cpp @@ -0,0 +1,74 @@ +#include "V8Helpers.h" +#include "helpers/BindHelpers.h" +#include "V8ResourceImpl.h" +#include "V8Class.h" + +#include "../CV8ScriptRuntime.h" + +#include "cpp-sdk/objects/IWorldObject.h" +#include "cpp-sdk/script-objects/IAudioOutput.h" +#include "cpp-sdk/script-objects/IAudioAttachedOutput.h" + +static void Constructor(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_CONSTRUCTOR(); + V8_CHECK_ARGS_LEN_MIN_MAX(1, 2); + + V8_ARG_TO_BASE_OBJECT(1, entity, alt::IWorldObject, "IWorldObject"); + V8_ARG_TO_INT_OPT(2, categoryHash, alt::ICore::Instance().Hash("radio")); + + auto output = alt::ICore::Instance().CreateAttachedOutput(categoryHash, entity, resource->GetResource()); + V8_BIND_BASE_OBJECT(output, "Failed to create AudioOutputAttached"); +} + +static void EntityGetter(v8::Local, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_BASE_OBJECT(ent, alt::IAudioAttachedOutput); + V8_RETURN_BASE_OBJECT(ent->GetEntity()); +} + +static void EntitySetter(v8::Local, v8::Local val, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_GET_THIS_BASE_OBJECT(_this, alt::IAudioAttachedOutput); + + V8_TO_WORLDOBJECT(val, entity); + + _this->SetEntity(entity); +} + +static void StaticGetByID(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_INT(1, id); + + alt::IBaseObject* baseObject = alt::ICore::Instance().GetBaseObjectByID(alt::IBaseObject::Type::AUDIO_OUTPUT_ATTACHED, id); + + if(baseObject) + { + V8_RETURN_BASE_OBJECT(baseObject); + } + else + { + V8_RETURN_NULL(); + } +} + +using namespace alt; + +extern V8Class v8AudioOutput; +extern V8Class v8AudioOutputAttached("AudioOutputAttached", + v8AudioOutput, + &Constructor, + [](v8::Local tpl) + { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + + V8Helpers::SetStaticMethod(isolate, tpl, "getByID", StaticGetByID); + + V8Helpers::SetAccessor(isolate, tpl, "entity", &EntityGetter, &EntitySetter); + }); diff --git a/client/src/bindings/AudioOutputFrontend.cpp b/client/src/bindings/AudioOutputFrontend.cpp new file mode 100644 index 00000000..cbe31d43 --- /dev/null +++ b/client/src/bindings/AudioOutputFrontend.cpp @@ -0,0 +1,54 @@ +#include "V8Helpers.h" +#include "helpers/BindHelpers.h" +#include "V8ResourceImpl.h" +#include "V8Class.h" + +#include "../CV8ScriptRuntime.h" + +#include "cpp-sdk/objects/IWorldObject.h" +#include "cpp-sdk/script-objects/IAudioOutput.h" +#include "cpp-sdk/script-objects/IAudioFrontendOutput.h" + +static void Constructor(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_CONSTRUCTOR(); + V8_CHECK_ARGS_LEN_MIN_MAX(0, 1); + + V8_ARG_TO_INT_OPT(1, categoryHash, alt::ICore::Instance().Hash("radio")); + + auto output = alt::ICore::Instance().CreateFrontendOutput(categoryHash, resource->GetResource()); + V8_BIND_BASE_OBJECT(output, "Failed to create AudioOutputFrontend"); +} + +static void StaticGetByID(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_INT(1, id); + + alt::IBaseObject* baseObject = alt::ICore::Instance().GetBaseObjectByID(alt::IBaseObject::Type::AUDIO_OUTPUT_FRONTEND, id); + + if(baseObject) + { + V8_RETURN_BASE_OBJECT(baseObject); + } + else + { + V8_RETURN_NULL(); + } +} + +using namespace alt; + +extern V8Class v8AudioOutput; +extern V8Class v8AudioOutputFrontend("AudioOutputFrontend", + v8AudioOutput, + &Constructor, + [](v8::Local tpl) + { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + + V8Helpers::SetStaticMethod(isolate, tpl, "getByID", StaticGetByID); + }); diff --git a/client/src/bindings/AudioOutputWorld.cpp b/client/src/bindings/AudioOutputWorld.cpp new file mode 100644 index 00000000..f337dd95 --- /dev/null +++ b/client/src/bindings/AudioOutputWorld.cpp @@ -0,0 +1,57 @@ +#include "V8Helpers.h" +#include "helpers/BindHelpers.h" +#include "V8ResourceImpl.h" +#include "V8Class.h" + +#include "../CV8ScriptRuntime.h" + +#include "cpp-sdk/objects/IWorldObject.h" +#include "cpp-sdk/script-objects/IAudioOutput.h" +#include "cpp-sdk/script-objects/IAudioWorldOutput.h" + +static void Constructor(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_CONSTRUCTOR(); + V8_CHECK_ARGS_LEN_MIN_MAX(1, 2); + + V8_ARG_TO_VECTOR3(1, pos); + V8_ARG_TO_INT_OPT(2, categoryHash, alt::ICore::Instance().Hash("radio")); + + auto output = alt::ICore::Instance().CreateWorldOutput(categoryHash, pos, resource->GetResource()); + V8_BIND_BASE_OBJECT(output, "Failed to create AudioOutputWorld"); +} + +static void StaticGetByID(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_INT(1, id); + + alt::IBaseObject* baseObject = alt::ICore::Instance().GetBaseObjectByID(alt::IBaseObject::Type::AUDIO_OUTPUT_WORLD, id); + + if(baseObject) + { + V8_RETURN_BASE_OBJECT(baseObject); + } + else + { + V8_RETURN_NULL(); + } +} + +using namespace alt; + +extern V8Class v8AudioOutput; +extern V8Class v8AudioOutputWorld("AudioOutputWorld", + v8AudioOutput, + &Constructor, + [](v8::Local tpl) + { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + + V8Helpers::SetStaticMethod(isolate, tpl, "getByID", StaticGetByID); + + V8Helpers::SetAccessor(isolate, tpl, "pos"); + }); diff --git a/client/src/bindings/LocalObject.cpp b/client/src/bindings/LocalObject.cpp index 411a7864..867304c5 100644 --- a/client/src/bindings/LocalObject.cpp +++ b/client/src/bindings/LocalObject.cpp @@ -210,6 +210,43 @@ static void TextureVariatioGetter(v8::Local, const v8::PropertyCallb V8_RETURN_UINT(object->GetTextureVariation()); } +static void StaticGetByID(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_INT(1, id); + + alt::IBaseObject* baseObject = alt::ICore::Instance().GetBaseObjectByID(alt::IBaseObject::Type::LOCAL_OBJECT, id); + + if(baseObject && baseObject->GetType() == alt::IEntity::Type::LOCAL_OBJECT) + { + V8_RETURN_BASE_OBJECT(baseObject); + } + else + { + V8_RETURN_NULL(); + } +} + +static void StaticGetByScriptID(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_INT(1, scriptGuid); + + alt::IWorldObject* entity = alt::ICore::Instance().GetWorldObjectByScriptID(scriptGuid); + + if(entity && (entity->GetType() == alt::IWorldObject::Type::LOCAL_OBJECT)) + { + V8_RETURN_BASE_OBJECT(entity); + } + else + { + V8_RETURN_NULL(); + } +} + extern V8Class v8Object; extern V8Class v8LocalObject("LocalObject", v8Object, @@ -221,6 +258,9 @@ extern V8Class v8LocalObject("LocalObject", V8Helpers::SetMethod(isolate, tpl, "toString", ToString); + V8Helpers::SetStaticMethod(isolate, tpl, "getByID", StaticGetByID); + V8Helpers::SetStaticMethod(isolate, tpl, "getByScriptID", StaticGetByScriptID); + V8Helpers::SetStaticAccessor(isolate, tpl, "all", &AllGetter); V8Helpers::SetStaticAccessor(isolate, tpl, "count", &CountGetter); diff --git a/client/src/bindings/Player.cpp b/client/src/bindings/Player.cpp index 72bfbb74..e1fd1981 100644 --- a/client/src/bindings/Player.cpp +++ b/client/src/bindings/Player.cpp @@ -153,6 +153,60 @@ static void SetFilter(v8::Local, v8::Local val, const v8: } } +static void WeaponHasComponent(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(2); + V8_GET_THIS_BASE_OBJECT(player, IPlayer); + + uint32_t hash; + if(info[0]->IsNumber()) + { + V8_ARG_TO_UINT(1, weaponHash); + hash = weaponHash; + } + else + { + V8_ARG_TO_STRING(1, weaponHash); + hash = alt::ICore::Instance().Hash(weaponHash); + } + + uint32_t componentHash; + if(info[0]->IsNumber()) + { + V8_ARG_TO_UINT(2, componenthash); + componentHash = componenthash; + } + else + { + V8_ARG_TO_STRING(2, componenthash); + componentHash = alt::ICore::Instance().Hash(componenthash); + } + + V8_RETURN_BOOLEAN(player->HasWeaponComponent(hash, componentHash)); +} + +static void GetWeaponTintIndex(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(2); + V8_GET_THIS_BASE_OBJECT(player, IPlayer); + + uint32_t hash; + if(info[0]->IsNumber()) + { + V8_ARG_TO_UINT(1, weaponHash); + hash = weaponHash; + } + else + { + V8_ARG_TO_STRING(1, weaponHash); + hash = alt::ICore::Instance().Hash(weaponHash); + } + + V8_RETURN_UINT(player->GetWeaponTintIndex(hash)); +} + extern V8Class v8Entity; extern V8Class v8Player("Player", v8Entity, @@ -187,7 +241,9 @@ extern V8Class v8Player("Player", // Weapon getters V8Helpers::SetAccessor(isolate, tpl, "currentWeaponComponents", &CurrentWeaponComponentsGetter); - // V8Helpers::SetAccessor(isolate, tpl, "currentWeaponTintIndex", &CurrentWeaponTintIndexGetter); + V8Helpers::SetMethod(isolate, tpl, "hasWeaponComponent", &WeaponHasComponent); + V8Helpers::SetMethod(isolate, tpl, "getWeaponTintIndex", &GetWeaponTintIndex); + V8Helpers::SetAccessor(isolate, tpl, "currentWeaponTintIndex"); V8Helpers::SetAccessor(isolate, tpl, "currentWeapon"); V8Helpers::SetAccessor(isolate, tpl, "entityAimingAt"); V8Helpers::SetAccessor(isolate, tpl, "entityAimOffset"); @@ -213,8 +269,8 @@ extern V8Class v8Player("Player", V8Helpers::SetAccessor(isolate, tpl, "isParachuting"); // V8Helpers::SetAccessor(isolate, tpl, "isSuperJumpEnabled"); - // V8Helpers::SetAccessor(isolate, tpl, "isCrouching"); - // V8Helpers::SetAccessor(isolate, tpl, "isStealthy"); + V8Helpers::SetAccessor(isolate, tpl, "isCrouching"); + V8Helpers::SetAccessor(isolate, tpl, "isStealthy"); V8Helpers::SetAccessor(isolate, tpl, "filter", &GetFilter, &SetFilter); }); diff --git a/client/src/bindings/Vehicle.cpp b/client/src/bindings/Vehicle.cpp index b564fbcc..9c206b07 100644 --- a/client/src/bindings/Vehicle.cpp +++ b/client/src/bindings/Vehicle.cpp @@ -345,7 +345,7 @@ extern V8Class v8Vehicle("Vehicle", V8Helpers::SetMethod(isolate, tpl, "getWheelSurfaceMaterial", GetWheelSurfaceMaterial); - V8Helpers::SetAccessor(isolate, tpl, "steeringAngle"); + V8Helpers::SetAccessor(isolate, tpl, "steeringAngle"); V8Helpers::SetAccessor(isolate, tpl, "suspensionHeight"); /*GETTERS BELOW ARE UNIMPLEMENTED diff --git a/client/src/bindings/WeaponObject.cpp b/client/src/bindings/WeaponObject.cpp index 602a9390..fc28dbf3 100644 --- a/client/src/bindings/WeaponObject.cpp +++ b/client/src/bindings/WeaponObject.cpp @@ -111,6 +111,40 @@ static void CountGetter(v8::Local name, const v8::PropertyCallbackIn V8_RETURN_UINT(alt::ICore::Instance().GetWeaponObjects().size()); } +static void StaticGetByID(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_INT(1, id); + + alt::IBaseObject* baseObject = alt::ICore::Instance().GetBaseObjectByID(alt::IBaseObject::Type::LOCAL_OBJECT, id); + + if(baseObject && baseObject->GetType() == alt::IEntity::Type::LOCAL_OBJECT) + { + V8_RETURN_BASE_OBJECT(baseObject); + return; + } + + V8_RETURN_NULL(); +} + +static void StaticGetByScriptID(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_CHECK_ARGS_LEN(1); + V8_ARG_TO_INT(1, scriptId); + + alt::IWorldObject* entity = alt::ICore::Instance().GetWorldObjectByScriptID(scriptId); + + if(entity && (entity->GetType() == alt::IWorldObject::Type::LOCAL_OBJECT)) + { + V8_RETURN_BASE_OBJECT(entity); + return; + } + + V8_RETURN_NULL(); +} + extern V8Class v8LocalObject; extern V8Class v8WeaponObject("WeaponObject", v8LocalObject, @@ -122,6 +156,8 @@ extern V8Class v8WeaponObject("WeaponObject", V8Helpers::SetStaticAccessor(isolate, tpl, "all", &AllGetter); V8Helpers::SetStaticAccessor(isolate, tpl, "count", &CountGetter); + V8Helpers::SetStaticMethod(isolate, tpl, "getByScriptID", StaticGetByScriptID); + V8Helpers::SetStaticMethod(isolate, tpl, "getByID", StaticGetByID); V8Helpers::SetAccessor(isolate, tpl, "tintIndex"); V8Helpers::SetMethod(isolate, tpl, "setComponentTintIndex", &GetComponentTintIndex); diff --git a/server/src/CNodeResourceImpl.cpp b/server/src/CNodeResourceImpl.cpp index 0c70c51a..09e32741 100644 --- a/server/src/CNodeResourceImpl.cpp +++ b/server/src/CNodeResourceImpl.cpp @@ -140,6 +140,8 @@ void CNodeResourceImpl::OnEvent(const alt::CEvent* e) v8::Context::Scope scope(GetContext()); // env->PushAsyncCallbackScope(); + HandleVehiclePassengerSeatEvents(e); + V8Helpers::EventHandler* handler = V8Helpers::EventHandler::Get(e); if(!handler) return; @@ -186,6 +188,55 @@ void CNodeResourceImpl::OnEvent(const alt::CEvent* e) return; } +void CNodeResourceImpl::HandleVehiclePassengerSeatEvents(const alt::CEvent* ev) +{ + auto evType = ev->GetType(); + + if (evType == alt::CEvent::Type::PLAYER_ENTER_VEHICLE) + { + auto event = static_cast(ev); + auto vehicle = event->GetTarget(); + auto player = event->GetPlayer(); + auto seat = event->GetSeat(); + + if (!vehiclePassengers.contains(vehicle)) + vehiclePassengers[vehicle] = {}; + + vehiclePassengers[vehicle][seat] = player; + } + else if (evType == alt::CEvent::Type::PLAYER_LEAVE_VEHICLE) + { + auto event = static_cast(ev); + auto vehicle = event->GetTarget(); + auto player = event->GetPlayer(); + + if (vehiclePassengers.contains(vehicle)) + { + for ( auto it = vehiclePassengers[vehicle].begin(); it != vehiclePassengers[vehicle].end(); ) + { + if (it->second == player) + it = vehiclePassengers[vehicle].erase(it); + else + ++it; + } + + if (vehiclePassengers[vehicle].empty()) + vehiclePassengers.erase(vehicle); + } + } + else if (evType == alt::CEvent::Type::PLAYER_CHANGE_VEHICLE_SEAT) + { + auto event = static_cast(ev); + auto vehicle = event->GetTarget(); + + if (!vehiclePassengers.contains(vehicle)) + vehiclePassengers[vehicle] = {}; + + vehiclePassengers[vehicle].erase(event->GetOldSeat()); + vehiclePassengers[vehicle][event->GetNewSeat()] = event->GetPlayer(); + } +} + void CNodeResourceImpl::OnTick() { v8::Locker locker(isolate); diff --git a/server/src/CNodeResourceImpl.h b/server/src/CNodeResourceImpl.h index d17242f4..3581c63a 100644 --- a/server/src/CNodeResourceImpl.h +++ b/server/src/CNodeResourceImpl.h @@ -25,6 +25,7 @@ class CNodeResourceImpl : public V8ResourceImpl bool Stop() override; void OnEvent(const alt::CEvent* ev) override; + void HandleVehiclePassengerSeatEvents(const alt::CEvent* ev); void OnTick() override; bool MakeClient(alt::IResource::CreationInfo* info, std::vector) override; diff --git a/server/src/bindings/Main.cpp b/server/src/bindings/Main.cpp index 3c4515d1..c432d5e6 100644 --- a/server/src/bindings/Main.cpp +++ b/server/src/bindings/Main.cpp @@ -589,6 +589,198 @@ static void SetVoiceExternal(const v8::FunctionCallbackInfo& info) alt::ICore::Instance().SetVoiceExternal(host, port); } +static void GetStreamingDistance(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetStreamingDistance()); +} + +static void SetStreamingDistance(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, distance); + + alt::ICore::Instance().SetStreamingDistance(distance); +} + +static void GetStreamingTickRate(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetStreamingTickRate()); +} + +static void SetStreamingTickRate(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, tickrate); + + alt::ICore::Instance().SetStreamingTickRate(tickrate); +} + +static void GetStreamerThreadCount(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetStreamerThreadCount()); +} + +static void SetStreamerThreadCount(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, count); + + alt::ICore::Instance().SetStreamerThreadCount(count); +} + +static void GetMaxStreamingPeds(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetMaxStreamingPeds()); +} + +static void SetMaxStreamingPeds(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, limit); + + alt::ICore::Instance().SetMaxStreamingPeds(limit); +} + +static void GetMaxStreamingObjects(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetMaxStreamingObjects()); +} + +static void SetMaxStreamingObjects(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, limit); + + alt::ICore::Instance().SetMaxStreamingObjects(limit); +} + +static void GetMaxStreamingVehicles(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetMaxStreamingVehicles()); +} + +static void SetMaxStreamingVehicles(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, limit); + + alt::ICore::Instance().SetMaxStreamingVehicles(limit); +} + +static void GetMigrationThreadCount(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetMigrationThreadCount()); +} + +static void SetMigrationThreadCount(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, count); + + alt::ICore::Instance().SetMigrationThreadCount(count); +} + +static void GetSyncSendThreadCount(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetSyncSendThreadCount()); +} + +static void SetSyncSendThreadCount(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, count); + + alt::ICore::Instance().SetSyncSendThreadCount(count); +} + +static void GetSyncReceiveThreadCount(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetSyncReceiveThreadCount()); +} + +static void SetSyncReceiveThreadCount(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, count); + + alt::ICore::Instance().SetSyncReceiveThreadCount(count); +} + +static void GetMigrationTickRate(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetMigrationTickRate()); +} + +static void SetMigrationTickRate(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, tickrate); + + alt::ICore::Instance().SetMigrationTickRate(tickrate); +} + +static void GetColShapeTickRate(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetColShapeTickRate()); +} + +static void SetColShapeTickRate(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, tickrate); + + alt::ICore::Instance().SetColShapeTickRate(tickrate); +} + +static void GetMigrationDistance(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_RETURN_UINT(alt::ICore::Instance().GetMigrationDistance()); +} + +static void SetMigrationDistance(const v8::FunctionCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT(); + V8_CHECK_ARGS_LEN(1); + + V8_ARG_TO_UINT(1, tickrate); + + alt::ICore::Instance().SetMigrationDistance(tickrate); +} + extern V8Class v8Player, v8Vehicle, v8Blip, v8AreaBlip, v8RadiusBlip, v8PointBlip, v8Checkpoint, v8VoiceChannel, v8Colshape, v8ColshapeCylinder, v8ColshapeSphere, v8ColshapeCircle, v8ColshapeCuboid, v8ColshapeRectangle, v8ColshapePolygon, v8Ped, v8Object, v8VirtualEntity, v8VirtualEntityGroup, v8Marker, v8ConnectionInfo; @@ -645,5 +837,39 @@ extern V8Module V8Helpers::RegisterFunc(exports, "setVoiceExternalPublic", &SetVoiceExternalPublic); V8Helpers::RegisterFunc(exports, "setVoiceExternal", &SetVoiceExternal); + V8Helpers::RegisterFunc(exports, "getMaxStreamingPeds", &GetMaxStreamingPeds); + V8Helpers::RegisterFunc(exports, "getMaxStreamingObjects", &GetMaxStreamingObjects); + V8Helpers::RegisterFunc(exports, "getMaxStreamingVehicles", &GetMaxStreamingVehicles); + V8Helpers::RegisterFunc(exports, "setMaxStreamingPeds", &SetMaxStreamingPeds); + V8Helpers::RegisterFunc(exports, "setMaxStreamingObjects", &SetMaxStreamingObjects); + V8Helpers::RegisterFunc(exports, "setMaxStreamingVehicles", &SetMaxStreamingVehicles); + + V8Helpers::RegisterFunc(exports, "getStreamerThreadCount", &GetStreamerThreadCount); + V8Helpers::RegisterFunc(exports, "setStreamerThreadCount", &SetStreamerThreadCount); + + V8Helpers::RegisterFunc(exports, "getMigrationThreadCount", &GetMigrationThreadCount); + V8Helpers::RegisterFunc(exports, "setMigrationThreadCount", &SetMigrationThreadCount); + + V8Helpers::RegisterFunc(exports, "getSyncSendThreadCount", &GetSyncSendThreadCount); + V8Helpers::RegisterFunc(exports, "setSyncSendThreadCount", &SetSyncSendThreadCount); + + V8Helpers::RegisterFunc(exports, "getSyncReceiveThreadCount", &GetSyncReceiveThreadCount); + V8Helpers::RegisterFunc(exports, "setSyncReceiveThreadCount", &SetSyncReceiveThreadCount); + + V8Helpers::RegisterFunc(exports, "getStreamingTickRate", &GetStreamingTickRate); + V8Helpers::RegisterFunc(exports, "setStreamingTickRate", &SetStreamingTickRate); + + V8Helpers::RegisterFunc(exports, "getMigrationTickRate", &GetMigrationTickRate); + V8Helpers::RegisterFunc(exports, "setMigrationTickRate", &SetMigrationTickRate); + + V8Helpers::RegisterFunc(exports, "getColShapeTickRate", &GetColShapeTickRate); + V8Helpers::RegisterFunc(exports, "setColShapeTickRate", &SetColShapeTickRate); + + V8Helpers::RegisterFunc(exports, "getStreamingDistance", &GetStreamingDistance); + V8Helpers::RegisterFunc(exports, "setStreamingDistance", &SetStreamingDistance); + + V8Helpers::RegisterFunc(exports, "getMigrationDistance", &GetMigrationDistance); + V8Helpers::RegisterFunc(exports, "setMigrationDistance", &SetMigrationDistance); + V8_OBJECT_SET_STRING(exports, "rootDir", alt::ICore::Instance().GetRootDirectory()); }); diff --git a/server/src/bindings/Vehicle.cpp b/server/src/bindings/Vehicle.cpp index dd841a35..48f79231 100644 --- a/server/src/bindings/Vehicle.cpp +++ b/server/src/bindings/Vehicle.cpp @@ -229,6 +229,27 @@ static void RoofClosedGetter(v8::Local name, const v8::PropertyCallb V8_RETURN_BOOLEAN(_this->GetRoofState() == 1); } +static void GetPassengers(v8::Local, const v8::PropertyCallbackInfo& info) +{ + V8_GET_ISOLATE_CONTEXT_RESOURCE(); + V8_GET_THIS_BASE_OBJECT(_this, IVehicle); + + auto obj = v8::Object::New(isolate); + auto passengers = V8ResourceImpl::vehiclePassengers[_this]; + + if (!passengers.empty()) + { + for (auto& [seat, player] : passengers) + { + auto entity = resource->GetBaseObjectOrNull(player); + if (!entity->IsNull()) + obj->Set(ctx, seat, entity); + } + } + + V8_RETURN(obj); +} + extern V8Class v8Entity; extern V8Class v8Vehicle("Vehicle", v8Entity, @@ -244,6 +265,7 @@ extern V8Class v8Vehicle("Vehicle", // Common getter/setters V8Helpers::SetAccessor(isolate, tpl, "destroyed"); V8Helpers::SetAccessor(isolate, tpl, "driver"); + V8Helpers::SetAccessor(isolate, tpl, "passengers", &GetPassengers); V8Helpers::SetAccessor(isolate, tpl, "velocity"); V8Helpers::SetAccessor(isolate, tpl, "quaternion"); diff --git a/server/src/events/Player.cpp b/server/src/events/Player.cpp index 873cfb23..e1af453f 100644 --- a/server/src/events/Player.cpp +++ b/server/src/events/Player.cpp @@ -218,3 +218,17 @@ V8_LOCAL_EVENT_HANDLER playerSpawn(EventType::PLAYER_SPAWN, args.push_back(resource->GetBaseObjectOrNull(ev->GetPlayer())); }); + +V8_LOCAL_EVENT_HANDLER playerHeal(EventType::PLAYER_HEAL, + "playerHeal", + [](V8ResourceImpl* resource, const CEvent* e, std::vector>& args) + { + auto ev = static_cast(e); + v8::Isolate* isolate = resource->GetIsolate(); + + args.push_back(resource->GetBaseObjectOrNull(ev->GetTarget())); + args.push_back(V8Helpers::JSValue(ev->GetOldHealth())); + args.push_back(V8Helpers::JSValue(ev->GetNewHealth())); + args.push_back(V8Helpers::JSValue(ev->GetOldArmour())); + args.push_back(V8Helpers::JSValue(ev->GetNewArmour())); + }); diff --git a/shared/IRuntimeEventHandler.h b/shared/IRuntimeEventHandler.h index e18cc9e2..f5507202 100644 --- a/shared/IRuntimeEventHandler.h +++ b/shared/IRuntimeEventHandler.h @@ -15,6 +15,10 @@ class IRuntimeEventHandler // All events the module uses, which need to be enabled and never disabled static constexpr std::array internalEvents = { EventType::CONNECTION_COMPLETE, EventType::DISCONNECT_EVENT, EventType::GAME_ENTITY_CREATE, EventType::GAME_ENTITY_DESTROY, EventType::RESOURCE_STOP +#ifdef ALT_SERVER_API + // required for vehicle seat stuff + , EventType::PLAYER_ENTER_VEHICLE, EventType::PLAYER_LEAVE_VEHICLE, EventType::PLAYER_CHANGE_VEHICLE_SEAT +#endif }; // Keeps track of the event handlers registered for all events diff --git a/shared/V8ResourceImpl.cpp b/shared/V8ResourceImpl.cpp index de9429f8..ea54422b 100644 --- a/shared/V8ResourceImpl.cpp +++ b/shared/V8ResourceImpl.cpp @@ -515,7 +515,7 @@ void V8ResourceImpl::InvokeEventHandlers(const alt::CEvent* ev, const std::vecto if(retn.IsEmpty()) return false; v8::Local returnValue = retn.ToLocalChecked(); - if(ev && returnValue->IsFalse()) ev->Cancel(); + if(ev && returnValue->IsFalse() && ev->IsCancellable()) static_cast(ev)->Cancel(); else if(ev && ev->GetType() == alt::CEvent::Type::WEAPON_DAMAGE_EVENT && returnValue->IsNumber()) static_cast(const_cast(ev))->SetDamageValue((uint32_t)returnValue->NumberValue(GetContext()).ToChecked()); // todo: add this once a generic Cancel() with string as arg has been added to the sdk @@ -544,7 +544,7 @@ void V8ResourceImpl::InvokeEventHandlers(const alt::CEvent* ev, const std::vecto else if(state == v8::Promise::PromiseState::kFulfilled) { v8::Local value = promise->Result(); - if(value->IsFalse()) ev->Cancel(); + if(value->IsFalse() && ev->IsCancellable()) static_cast(ev)->Cancel(); break; } else if(state == v8::Promise::PromiseState::kRejected) diff --git a/shared/V8ResourceImpl.h b/shared/V8ResourceImpl.h index a382e9f4..8e7d69d3 100644 --- a/shared/V8ResourceImpl.h +++ b/shared/V8ResourceImpl.h @@ -313,6 +313,10 @@ class V8ResourceImpl : public alt::IResource::Impl return static_cast(ctx->GetAlignedPointerFromEmbedderData(1)); } +#ifdef ALT_SERVER_API + static inline std::unordered_map> vehiclePassengers{}; +#endif + protected: v8::Isolate* isolate; alt::IResource* resource; diff --git a/shared/bindings/Utils.js b/shared/bindings/Utils.js index cd62140d..4ecb0142 100644 --- a/shared/bindings/Utils.js +++ b/shared/bindings/Utils.js @@ -1,4 +1,5 @@ -// // @ts-check // uncomment if you need typescript checks, e.g. for gta natives update +// // @ts-check // uncomment if you need typescript checks, e.g. for gta natives +// update /// // clang-format off // Utils JS bindings @@ -291,6 +292,14 @@ if (alt.isClient && !alt.isWorker) { return alt.Utils.waitFor(() => this.scriptID !== 0, timeout); } + alt.LocalVehicle.prototype.waitForSpawn = function(timeout = 2000) { + return alt.Utils.waitFor(() => this.scriptID !== 0, timeout); + } + + alt.LocalPed.prototype.waitForSpawn = function(timeout = 2000) { + return alt.Utils.waitFor(() => this.scriptID !== 0, timeout); + } + alt.Utils.drawText2dThisFrame = function( text, pos2d = new alt.Vector2(0.5), diff --git a/shared/bindings/Vector2.js b/shared/bindings/Vector2.js index f7110589..4be2b62b 100644 --- a/shared/bindings/Vector2.js +++ b/shared/bindings/Vector2.js @@ -111,7 +111,7 @@ alt.Vector2.prototype.inverse = function() { alt.Vector2.prototype.normalize = function() { const length = this.length; - return new alt.Vector2(this.x / length, this.y / length); + return new alt.Vector2(this.x == 0 ? 0 : this.x / length, this.y == 0 ? 0 : this.y / length); } alt.Vector2.prototype.distanceTo = function(vector) { diff --git a/shared/bindings/Vector3.js b/shared/bindings/Vector3.js index eb2a6755..8f65d83b 100644 --- a/shared/bindings/Vector3.js +++ b/shared/bindings/Vector3.js @@ -136,7 +136,7 @@ alt.Vector3.prototype.inverse = function() { alt.Vector3.prototype.normalize = function() { const length = this.length; - return new alt.Vector3(this.x / length, this.y / length, this.z / length); + return new alt.Vector3(this.x == 0 ? 0 : this.x / length, this.y == 0 ? 0: this.y / length, this.z == 0 ? 0 : this.z / length); } alt.Vector3.prototype.distanceTo = function(vector) { diff --git a/shared/deps/cpp-sdk b/shared/deps/cpp-sdk index d7aed4c1..ab604174 160000 --- a/shared/deps/cpp-sdk +++ b/shared/deps/cpp-sdk @@ -1 +1 @@ -Subproject commit d7aed4c1639b10c438d78d9697379693a2b8c74e +Subproject commit ab604174853306108976057a6c1e9c5f0b54c27c diff --git a/shared/helpers/Convert.h b/shared/helpers/Convert.h index 687d16dd..8a8295cf 100644 --- a/shared/helpers/Convert.h +++ b/shared/helpers/Convert.h @@ -82,16 +82,6 @@ namespace V8Helpers return v8::BigInt::NewFromUnsigned(v8::Isolate::GetCurrent(), val); } template - inline v8::Local JSValue(alt::Array& arr) - { - auto jsArr = v8::Array::New(v8::Isolate::GetCurrent(), arr.GetSize()); - for(int i = 0; i < arr.GetSize(); i++) - { - jsArr->Set(v8::Isolate::GetCurrent()->GetEnteredOrMicrotaskContext(), i, JSValue(arr[i])); - } - return jsArr; - } - template inline v8::Local JSValue(std::vector& arr) { auto jsArr = v8::Array::New(v8::Isolate::GetCurrent(), arr.size());