Skip to content

Commit

Permalink
server: cloudid changes
Browse files Browse the repository at this point in the history
  • Loading branch information
C0kkie committed Oct 7, 2023
1 parent f24c4d2 commit 2188394
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 66 deletions.
33 changes: 3 additions & 30 deletions server/src/bindings/ConnectionInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,38 +175,11 @@ static void SetText(v8::Local<v8::String>, v8::Local<v8::Value> val, const v8::P
con->SetText(text);
}

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

V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_GET_THIS_BASE_OBJECT(con, alt::IConnectionInfo);

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

con->RequestCloudID(
[&persistent, resource](bool ok, const std::string& result)
{
resource->RunOnNextTick(
[=, &persistent]()
{
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());
V8_RETURN_STRING(con->GetCloudID());
}

extern V8Class v8BaseObject;
Expand All @@ -224,7 +197,6 @@ extern V8Class v8ConnectionInfo("ConnectionInfo",

V8Helpers::SetMethod(isolate, tpl, "accept", &Accept);
V8Helpers::SetMethod(isolate, tpl, "decline", &Decline);
V8Helpers::SetMethod(isolate, tpl, "requestCloudID", &RequestCloudID);
V8Helpers::SetAccessor(isolate, tpl, "isAccepted", &IsAcceptedGetter);

V8Helpers::SetAccessor(isolate, tpl, "name", &NameGetter);
Expand All @@ -241,5 +213,6 @@ extern V8Class v8ConnectionInfo("ConnectionInfo",
V8Helpers::SetAccessor(isolate, tpl, "discordUserID", &DiscordUserIDGetter);
V8Helpers::SetAccessor(isolate, tpl, "socialClubName", &SocialClubNameGetter);
V8Helpers::SetAccessor(isolate, tpl, "id", &ConnectionIDGetter);
V8Helpers::SetAccessor(isolate, tpl, "cloudID", &GetCloudIDGetter);
V8Helpers::SetAccessor(isolate, tpl, "text", &GetText, &SetText);
});
36 changes: 1 addition & 35 deletions server/src/bindings/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,40 +866,6 @@ 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]()
{
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 @@ -1427,9 +1393,9 @@ 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::GetCloudID>(isolate, tpl, "cloudID");
V8Helpers::SetAccessor<IPlayer, std::string, &IPlayer::GetIP>(isolate, tpl, "ip");
V8Helpers::SetAccessor<IPlayer, std::string, &IPlayer::GetName>(isolate, tpl, "name");
V8Helpers::SetAccessor<IPlayer, IVehicle*, &IPlayer::GetVehicle>(isolate, tpl, "vehicle");
Expand Down
2 changes: 1 addition & 1 deletion shared/deps/cpp-sdk

0 comments on commit 2188394

Please sign in to comment.