Skip to content

Commit

Permalink
Merge branch 'dev' into rc
Browse files Browse the repository at this point in the history
  • Loading branch information
C0kkie committed Aug 5, 2023
2 parents 9f6316e + 32a5b41 commit c59a017
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 34 deletions.
8 changes: 5 additions & 3 deletions client/src/bindings/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_CHECK_CONSTRUCTOR();
V8_CHECK_ARGS_LEN(2);
V8_CHECK_ARGS_LEN_MIN_MAX(1, 3);

V8_ARG_TO_STRING(1, source);
V8_ARG_TO_NUMBER(2, volume);
V8_ARG_TO_NUMBER_OPT(2, volume, 1.f);
V8_ARG_TO_BOOLEAN_OPT(3, radio, false);

auto audio = alt::ICore::Instance().CreateAudio(source, volume, resource->GetResource());
std::string origin = V8Helpers::GetCurrentSourceOrigin(isolate);
auto audio = alt::ICore::Instance().CreateAudio(source, volume, radio, origin, resource->GetResource());
V8_BIND_BASE_OBJECT(audio, "Failed to create Audio");
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/bindings/ClientBindingsMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ static void GetPermissionState(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_CHECK_ARGS_LEN(1);
V8_ARG_TO_INT(1, permnum);

V8_RETURN_INT((uint8_t)alt::ICore::Instance().GetPermissionState((alt::Permission)permnum));
V8_RETURN_BOOLEAN(alt::ICore::Instance().GetPermissionState((alt::Permission)permnum));
}

static void IsInStreamerMode(const v8::FunctionCallbackInfo<v8::Value>& info)
Expand Down
4 changes: 2 additions & 2 deletions client/src/bindings/LocalPed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_ARG_TO_INT32(2, dimension);
V8_ARG_TO_VECTOR3(3, pos);
V8_ARG_TO_VECTOR3(4, rot);
V8_ARG_TO_BOOLEAN(5, useStreaming);
V8_ARG_TO_UINT(6, streamingDistance);
V8_ARG_TO_BOOLEAN_OPT(5, useStreaming, false);
V8_ARG_TO_UINT_OPT(6, streamingDistance, 0);

auto ped = alt::ICore::Instance().CreateLocalPed(modelHash, dimension, pos, rot, useStreaming, streamingDistance, resource->GetResource());
V8_BIND_BASE_OBJECT(ped, "Failed to create localPed");
Expand Down
4 changes: 2 additions & 2 deletions client/src/bindings/LocalVehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_ARG_TO_INT32(2, dimension);
V8_ARG_TO_VECTOR3(3, pos);
V8_ARG_TO_VECTOR3(4, rot);
V8_ARG_TO_BOOLEAN(5, useStreaming);
V8_ARG_TO_UINT(6, streamingDistance);
V8_ARG_TO_BOOLEAN_OPT(5, useStreaming, false);
V8_ARG_TO_UINT_OPT(6, streamingDistance, 0);

auto vehicle = alt::ICore::Instance().CreateLocalVehicle(modelHash, dimension, pos, rot, useStreaming, streamingDistance, resource->GetResource());
V8_BIND_BASE_OBJECT(vehicle, "Failed to create localVehicle");
Expand Down
30 changes: 30 additions & 0 deletions client/src/bindings/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ static void StaticGetByRemoteId(const v8::FunctionCallbackInfo<v8::Value>& info)
}
}

static void GetFilter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_GET_THIS_BASE_OBJECT(player, alt::IPlayer);

V8_RETURN_BASE_OBJECT(player->GetFilter());
}

static void SetFilter(v8::Local<v8::String>, v8::Local<v8::Value> val, const v8::PropertyCallbackInfo<void>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_GET_THIS_BASE_OBJECT(player, alt::IPlayer);

if(val->IsNull())
{
player->RemoveFilter();
}
else if(val->IsObject())
{
V8_TO_BASEOBJECT(val, filter);
player->AddFilter(filter->SharedAs<alt::IAudioFilter>().get());
}
else
{
V8Helpers::Throw(isolate, "AudioOutput.filter setter expects null or AudioFilter object");
}
}

extern V8Class v8Entity;
extern V8Class v8Player("Player",
v8Entity,
Expand Down Expand Up @@ -186,4 +214,6 @@ extern V8Class v8Player("Player",
// V8Helpers::SetAccessor<IPlayer, bool, &IPlayer::IsSuperJumpEnabled>(isolate, tpl, "isSuperJumpEnabled");
// V8Helpers::SetAccessor<IPlayer, bool, &IPlayer::IsCrouching>(isolate, tpl, "isCrouching");
// V8Helpers::SetAccessor<IPlayer, bool, &IPlayer::IsStealthy>(isolate, tpl, "isStealthy");

V8Helpers::SetAccessor(isolate, tpl, "filter", &GetFilter, &SetFilter);
});
4 changes: 2 additions & 2 deletions client/src/bindings/TextLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_ARG_TO_RGBA(7, color);
V8_ARG_TO_NUMBER(8, outlineWidth);
V8_ARG_TO_RGBA(9, outlineColor);
V8_ARG_TO_BOOLEAN(10, useStreaming);
V8_ARG_TO_UINT(11, streamingDistance);
V8_ARG_TO_BOOLEAN_OPT(10, useStreaming, false);
V8_ARG_TO_UINT_OPT(11, streamingDistance, 0);

auto textLabel =
alt::ICore::Instance().CreateTextLabel(text, fontName, fontSize, scale, pos, rot, color, outlineWidth, outlineColor, useStreaming, streamingDistance, resource->GetResource());
Expand Down
3 changes: 3 additions & 0 deletions client/src/bindings/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ extern V8Class v8Vehicle("Vehicle",

V8Helpers::SetMethod(isolate, tpl, "getWheelSurfaceMaterial", GetWheelSurfaceMaterial);

V8Helpers::SetAccessor<IVehicle, float, &IVehicle::GetSteeringAngle>(isolate, tpl, "steeringAngle");
V8Helpers::SetAccessor<IVehicle, float, &IVehicle::GetSuspensionHeight, &IVehicle::SetSuspensionHeight>(isolate, tpl, "suspensionHeight");

/*GETTERS BELOW ARE UNIMPLEMENTED
V8Helpers::SetAccessor(isolate, tpl, "isDestroyed", &IsDestroyedGetter);
V8Helpers::SetAccessor(isolate, tpl, "driver", &DriverGetter);
Expand Down
46 changes: 46 additions & 0 deletions client/src/bindings/WebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,48 @@ static void StaticGetByID(const v8::FunctionCallbackInfo<v8::Value>& info)
}
}

static void AddOutput(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_GET_THIS_BASE_OBJECT(audio, alt::IWebView);
V8_CHECK_ARGS_LEN(1);

V8_ARG_TO_BASE_OBJECT(1, entity, alt::IAudioOutput, "AudioOutput");
audio->AddOutput(entity);
}

static void RemoveOutput(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_GET_THIS_BASE_OBJECT(audio, alt::IWebView);
V8_CHECK_ARGS_LEN(1);

V8_ARG_TO_BASE_OBJECT(1, entity, alt::IAudioOutput, "AudioOutput");
audio->RemoveOutput(entity);
}

static void GetOutputs(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_GET_THIS_BASE_OBJECT(audio, alt::IWebView);

auto list = audio->GetOutputs();
auto arr = v8::Array::New(isolate, list->GetSize());
for (int i = 0; i < list->GetSize(); i++)
{
auto val = list->Get(i);
if (val->GetType() == alt::IMValue::Type::BASE_OBJECT)
{
auto baseObj = resource->GetBaseObjectOrNull(std::dynamic_pointer_cast<alt::IMValueBaseObject>(val)->RawValue());
arr->Set(ctx, i, baseObj);
}
else if (val->GetType() == alt::IMValue::Type::UINT)
arr->Set(ctx, i, v8::Integer::NewFromUnsigned(isolate, std::dynamic_pointer_cast<alt::IMValueUInt>(val)->Value()));
}

V8_RETURN(arr);
}

extern V8Class v8BaseObject;
extern V8Class v8WebView("WebView",
v8BaseObject,
Expand Down Expand Up @@ -305,4 +347,8 @@ extern V8Class v8WebView("WebView",
V8Helpers::SetMethod(isolate, tpl, "setExtraHeader", &SetExtraHeader);
V8Helpers::SetMethod(isolate, tpl, "setZoomLevel", &SetZoomLevel);
V8Helpers::SetMethod(isolate, tpl, "reload", &Reload);

V8Helpers::SetMethod(isolate, tpl, "addOutput", &AddOutput);
V8Helpers::SetMethod(isolate, tpl, "removeOutput", &RemoveOutput);
V8Helpers::SetMethod(isolate, tpl, "getOutputs", &GetOutputs);
});
34 changes: 34 additions & 0 deletions server/src/bindings/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,39 @@ static void GetLocalMetaDataKeys(const v8::FunctionCallbackInfo<v8::Value>& info
V8_RETURN(arr);
}

static void RequestCloudID(const v8::FunctionCallbackInfo<v8::Value>& info)
{
static std::list<v8::Global<v8::Promise::Resolver>> promises;

V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_GET_THIS_BASE_OBJECT(player, alt::IPlayer);

auto& persistent = promises.emplace_back(v8::Global<v8::Promise::Resolver>(isolate, v8::Promise::Resolver::New(ctx).ToLocalChecked()));

player->RequestCloudID([&persistent, resource](bool ok, const std::string& result) {
resource->RunOnNextTick(
[=, &persistent, &resource]()
{
if(!resource->GetResource()->IsStarted())
{
promises.remove(persistent);
return;
}

auto isolate = resource->GetIsolate();
auto context = resource->GetContext();

if (ok)
persistent.Get(isolate)->Resolve(context, V8Helpers::JSValue(result));
else
persistent.Get(isolate)->Reject(context, v8::Exception::Error(V8Helpers::JSValue(result)));
promises.remove(persistent);
});
});

V8_RETURN(persistent.Get(isolate)->GetPromise());
}

static void DiscordIDGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE();
Expand Down Expand Up @@ -1372,6 +1405,7 @@ extern V8Class v8Player("Player",
V8Helpers::SetMethod(isolate, tpl, "getLocalMeta", &GetLocalMeta);
V8Helpers::SetMethod(isolate, tpl, "deleteLocalMeta", &DeleteLocalMeta);
V8Helpers::SetMethod(isolate, tpl, "getLocalMetaKeys", &GetLocalMetaDataKeys);
V8Helpers::SetMethod(isolate, tpl, "requestCloudID", &RequestCloudID);

V8Helpers::SetAccessor<IPlayer, uint32_t, &IPlayer::GetPing>(isolate, tpl, "ping");
V8Helpers::SetAccessor<IPlayer, std::string, &IPlayer::GetIP>(isolate, tpl, "ip");
Expand Down
5 changes: 5 additions & 0 deletions server/src/bindings/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ extern V8Class v8Vehicle("Vehicle",
V8Helpers::SetAccessor<IVehicle, float, &IVehicle::GetLightsMultiplier, &IVehicle::SetLightsMultiplier>(isolate, tpl, "lightsMultiplier");
V8Helpers::SetAccessor<IVehicle, bool, &IVehicle::IsDriftMode, &IVehicle::SetDriftMode>(isolate, tpl, "driftModeEnabled");
V8Helpers::SetAccessor<IVehicle, uint8_t, &IVehicle::GetLightState, &IVehicle::SetLightState>(isolate, tpl, "lightState");
V8Helpers::SetAccessor<IVehicle, bool, &IVehicle::IsHornActive>(isolate, tpl, "hornActive");

// Gamestate methods
V8Helpers::SetMethod(isolate, tpl, "getDoorState", &GetDoorState);
Expand Down Expand Up @@ -367,6 +368,10 @@ extern V8Class v8Vehicle("Vehicle",
V8Helpers::SetAccessor<IVehicle, IVehicle*, &IVehicle::GetAttached>(isolate, tpl, "attached");
V8Helpers::SetAccessor<IVehicle, IVehicle*, &IVehicle::GetAttachedTo>(isolate, tpl, "attachedTo");

V8Helpers::SetAccessor<IVehicle, float, &IVehicle::GetSteeringAngle>(isolate, tpl, "steeringAngle");
V8Helpers::SetAccessor<IVehicle, float, &IVehicle::GetAccelerationLevel>(isolate, tpl, "accelerationLevel");
V8Helpers::SetAccessor<IVehicle, float, &IVehicle::GetBrakeLevel>(isolate, tpl, "brakeLevel");

// Train getter/setter
V8Helpers::SetAccessor<IVehicle, bool, &IVehicle::IsTrainMissionTrain, &IVehicle::SetTrainMissionTrain>(isolate, tpl, "isMissionTrain");
V8Helpers::SetAccessor<IVehicle, int8_t, &IVehicle::GetTrainTrackId, &IVehicle::SetTrainTrackId>(isolate, tpl, "trainTrackId");
Expand Down
61 changes: 61 additions & 0 deletions server/src/events/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,64 @@ V8Helpers::LocalEventHandler metaChange(EventType::META_CHANGE,
});

V8Helpers::LocalEventHandler serverStarted(EventType::SERVER_STARTED, "serverStarted", [](V8ResourceImpl* resource, const CEvent* e, std::vector<v8::Local<v8::Value>>& args) {});

V8_LOCAL_EVENT_HANDLER requestSyncedScene(EventType::REQUEST_SYNCED_SCENE,
"requestSyncedScene",
[](V8ResourceImpl* resource, const alt::CEvent* e, std::vector<v8::Local<v8::Value>>& args)
{
auto ev = static_cast<const alt::CRequestSyncedSceneEvent*>(e);
v8::Isolate* isolate = resource->GetIsolate();

args.push_back(resource->GetBaseObjectOrNull(ev->GetSource()));
args.push_back(V8Helpers::JSValue(ev->GetSceneID()));
});

V8_LOCAL_EVENT_HANDLER startSyncedScene(EventType::START_SYNCED_SCENE,
"startSyncedScene",
[](V8ResourceImpl* resource, const alt::CEvent* e, std::vector<v8::Local<v8::Value>>& args)
{
auto ev = static_cast<const alt::CStartSyncedSceneEvent*>(e);
v8::Isolate* isolate = resource->GetIsolate();
const auto ctx = resource->GetContext();

args.push_back(resource->GetBaseObjectOrNull(ev->GetSource()));
args.push_back(V8Helpers::JSValue(ev->GetSceneID()));
args.push_back(resource->CreateVector3(ev->GetStartPosition()));
args.push_back(resource->CreateVector3(ev->GetStartRotation()));
args.push_back(V8Helpers::JSValue(ev->GetAnimDictHash()));

const auto entityAndAnimHashPairs = ev->GetEntityAndAnimHashPairs();
const auto animHashArray = v8::Array::New(isolate, entityAndAnimHashPairs.size());
auto idx = 0;
for (auto [entity, animHash] : entityAndAnimHashPairs)
{
V8_NEW_OBJECT(entityAnimPair)
V8_OBJECT_SET_BASE_OBJECT(entityAnimPair, "entity", entity.get())
V8_OBJECT_SET_UINT(entityAnimPair, "animHash", animHash)
animHashArray->Set(ctx, idx++, entityAnimPair);
}
args.push_back(animHashArray);
});

V8_LOCAL_EVENT_HANDLER stopSyncedScene(EventType::STOP_SYNCED_SCENE,
"stopSyncedScene",
[](V8ResourceImpl* resource, const alt::CEvent* e, std::vector<v8::Local<v8::Value>>& args)
{
auto ev = static_cast<const alt::CStopSyncedSceneEvent*>(e);
v8::Isolate* isolate = resource->GetIsolate();

args.push_back(resource->GetBaseObjectOrNull(ev->GetSource()));
args.push_back(V8Helpers::JSValue(ev->GetSceneID()));
});

V8_LOCAL_EVENT_HANDLER updateSyncedScene(EventType::UPDATE_SYNCED_SCENE,
"updateSyncedScene",
[](V8ResourceImpl* resource, const alt::CEvent* e, std::vector<v8::Local<v8::Value>>& args)
{
auto ev = static_cast<const alt::CUpdateSyncedSceneEvent*>(e);
v8::Isolate* isolate = resource->GetIsolate();

args.push_back(resource->GetBaseObjectOrNull(ev->GetSource()));
args.push_back(V8Helpers::JSValue(ev->GetStartRate()));
args.push_back(V8Helpers::JSValue(ev->GetSceneID()));
});
2 changes: 2 additions & 0 deletions shared/helpers/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@

#define V8_OBJECT_SET_NULL(v8Val, prop) (v8Val)->Set(ctx, v8::String::NewFromUtf8(isolate, prop).ToLocalChecked(), v8::Null(isolate));

#define V8_OBJECT_SET_BASE_OBJECT(v8Val, prop, val) (v8Val)->Set(ctx, v8::String::NewFromUtf8(isolate, prop).ToLocalChecked(), resource->GetBaseObjectOrNull(val));

#define V8_NEW_OBJECT(val) v8::Local<v8::Object> val = v8::Object::New(isolate);

#define V8_NEW_ARGS(val) std::vector<v8::Local<v8::Value>> val;
Expand Down
Loading

0 comments on commit c59a017

Please sign in to comment.