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 Dec 20, 2023
2 parents 2b826f7 + 0f5c762 commit a43afcb
Show file tree
Hide file tree
Showing 22 changed files with 318 additions and 242 deletions.
11 changes: 9 additions & 2 deletions client/src/bindings/AudioFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,18 @@ static void AddVolumeEffect(const v8::FunctionCallbackInfo<v8::Value>& info)

V8_GET_THIS_BASE_OBJECT(filter, alt::IAudioFilter);

V8_CHECK_ARGS_LEN(2);
V8_CHECK_ARGS_LEN2(2, 3);
V8_ARG_TO_NUMBER(1, fVolume);
V8_ARG_TO_INT(2, priority);

V8_RETURN_UINT(filter->AddVolumeEffect(fVolume, priority));
int channel = -1;
if (info.Length() > 2)
{
V8_TO_UINT(info[2], channel2);
channel = channel2;
}

V8_RETURN_UINT(filter->AddVolumeEffect(fVolume, priority, channel));
}

static void AddPeakeqEffect(const v8::FunctionCallbackInfo<v8::Value>& info)
Expand Down
37 changes: 37 additions & 0 deletions client/src/bindings/ClientBindingsMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,39 @@ static void IsFullScreen(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_RETURN_BOOLEAN(alt::ICore::Instance().IsFullScreen());
}

static void GetPoolSize(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_CHECK_ARGS_LEN(1);
V8_ARG_TO_STRING(1, pool);

V8_RETURN_UINT(alt::ICore::Instance().GetPoolSize(pool));
}

static void GetPoolCount(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_CHECK_ARGS_LEN(1);
V8_ARG_TO_STRING(1, pool);

V8_RETURN_UINT(alt::ICore::Instance().GetPoolCount(pool));
}

static void GetPoolEntities(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_CHECK_ARGS_LEN(1);
V8_ARG_TO_STRING(1, pool);

auto entities = alt::ICore::Instance().GetPoolEntities(pool);
v8::Local<v8::Array> arr = v8::Array::New(isolate, entities.size());

for (uint32_t i = 0; i < entities.size(); ++i)
arr->Set(ctx, i, v8::Integer::NewFromUnsigned(isolate, entities[i]));

V8_RETURN(arr);
}

extern V8Module sharedModule;
extern V8Class v8Player, v8Player, v8Vehicle, v8WebView, v8HandlingData, v8LocalStorage, v8MemoryBuffer, v8MapZoomData, v8Discord, v8Voice, v8WebSocketClient, v8Checkpoint, v8HttpClient,
v8Audio, v8LocalPlayer, v8Profiler, v8Worker, v8RmlDocument, v8RmlElement, v8WeaponData, v8FocusData, v8LocalObject, v8TextEncoder, v8TextDecoder, v8Object, v8VirtualEntityGroup,
Expand Down Expand Up @@ -1390,4 +1423,8 @@ extern V8Module altModule("alt",

V8Helpers::RegisterFunc(exports, "evalModule", &EvalModule);
V8Helpers::RegisterFunc(exports, "isFullScreen", &IsFullScreen);

V8Helpers::RegisterFunc(exports, "getPoolSize", &GetPoolSize);
V8Helpers::RegisterFunc(exports, "getPoolCount", &GetPoolCount);
V8Helpers::RegisterFunc(exports, "getPoolEntities", &GetPoolEntities);
});
26 changes: 25 additions & 1 deletion client/src/bindings/TextLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
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());
alt::ICore::Instance().CreateTextLabel(text, fontName, fontSize, scale, pos, rot, color, outlineWidth, outlineColor, useStreaming, streamingDistance, resource->GetResource());
V8_BIND_BASE_OBJECT(textLabel, "Failed to create textlabel");
}

Expand Down Expand Up @@ -51,6 +51,24 @@ static void StaticGetByID(const v8::FunctionCallbackInfo<v8::Value>& info)
}
}

static void AlignGetter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE();

V8_GET_THIS_BASE_OBJECT(label, alt::ITextLabel);

V8_RETURN_NUMBER(label->GetAlign());
}

static void AlignSetter(v8::Local<v8::String>, v8::Local<v8::Value> val, const v8::PropertyCallbackInfo<void>& info)
{
V8_GET_ISOLATE_CONTEXT();
V8_GET_THIS_BASE_OBJECT(label, alt::ITextLabel);

V8_TO_NUMBER(val, align);
label->SetAlign((alt::ITextLabel::Alignment) align);
}

extern V8Class v8WorldObject;
extern V8Class v8TextLabel("TextLabel",
v8WorldObject,
Expand All @@ -69,6 +87,12 @@ extern V8Class v8TextLabel("TextLabel",
V8Helpers::SetAccessor<ITextLabel, IPlayer*, &ITextLabel::GetTarget>(isolate, tpl, "target");
V8Helpers::SetAccessor<ITextLabel, bool, &ITextLabel::IsVisible, &ITextLabel::SetVisible>(isolate, tpl, "visible");
V8Helpers::SetAccessor<ITextLabel, RGBA, &ITextLabel::GetColor, &ITextLabel::SetColor>(isolate, tpl, "color");
V8Helpers::SetAccessor<ITextLabel, RGBA, &ITextLabel::GetOutlineColor, &ITextLabel::SetOutlineColor>(isolate, tpl, "outlineColor");
V8Helpers::SetAccessor<ITextLabel, float, &ITextLabel::GetOutlineWidth, &ITextLabel::SetOutlineWidth>(isolate, tpl, "outlineWidth");
V8Helpers::SetAccessor<ITextLabel, float, &ITextLabel::GetFontSize, &ITextLabel::SetFontSize>(isolate, tpl, "fontSize");
V8Helpers::SetAccessor(isolate, tpl, "align", &AlignGetter, &AlignSetter);
V8Helpers::SetAccessor<ITextLabel, std::string, &ITextLabel::GetText, const std::string&, &ITextLabel::SetText>(isolate, tpl, "text");
V8Helpers::SetAccessor<ITextLabel, std::string, &ITextLabel::GetFont, const std::string&, &ITextLabel::SetFont>(isolate, tpl, "font");
V8Helpers::SetAccessor<ITextLabel, float, &ITextLabel::GetScale, &ITextLabel::SetScale>(isolate, tpl, "scale");
V8Helpers::SetAccessor<ITextLabel, Rotation, &ITextLabel::GetRotation, &ITextLabel::SetRotation>(isolate, tpl, "rot");
V8Helpers::SetAccessor<ITextLabel, bool, &ITextLabel::IsFacingCamera, &ITextLabel::SetFaceCamera>(isolate, tpl, "faceCamera");
Expand Down
44 changes: 25 additions & 19 deletions server/src/CNodeResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ static const char bootstrap_code[] =
#include "bootstrap.js.gen"
;

CNodeResourceImpl::CNodeResourceImpl(CNodeScriptRuntime* _runtime, v8::Isolate* isolate, alt::IResource* resource) : V8ResourceImpl(isolate, resource), runtime(_runtime)
CNodeResourceImpl::CNodeResourceImpl(CNodeScriptRuntime* _runtime, alt::IResource* resource) : V8ResourceImpl(nullptr, resource), runtime(_runtime)
{
uvLoop = new uv_loop_t;
uv_loop_init(uvLoop);

auto allocator = node::CreateArrayBufferAllocator();
isolate = node::NewIsolate(allocator, uvLoop, runtime->GetPlatform());
nodeData = node::CreateIsolateData(isolate, uvLoop, runtime->GetPlatform(), allocator);

v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);

v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);

v8::Local<v8::String> resourceName = V8Helpers::JSValue(resource->GetName());

v8::Local<v8::Context> _context = node::NewContext(isolate, global);
v8::Context::Scope scope(_context);

v8::Local<v8::Context> _context = node::NewContext(isolate);
_context->SetAlignedPointerInEmbedderData(1, resource);
context.Reset(isolate, _context);
}
Expand All @@ -48,29 +49,28 @@ bool CNodeResourceImpl::Start()
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Local<v8::Context> _context = GetContext();

auto _context = GetContext();
v8::Context::Scope scope(_context);

_context->Global()->Set(_context, V8Helpers::JSValue("__resourceLoaded"), v8::Function::New(_context, &ResourceLoaded).ToLocalChecked());
_context->Global()->Set(_context, V8Helpers::JSValue("__internal_bindings_code"), V8Helpers::JSValue(JSBindings::GetBindingsCode()));

V8Class::LoadAll(isolate);
V8ResourceImpl::Start();
V8ResourceImpl::SetupScriptGlobals();

node::EnvironmentFlags::Flags flags = (node::EnvironmentFlags::Flags)(node::EnvironmentFlags::kOwnsProcessState & node::EnvironmentFlags::kNoCreateInspector);

uvLoop = new uv_loop_t;
uv_loop_init(uvLoop);
node::ThreadId threadId = node::AllocateEnvironmentThreadId();
auto flags = static_cast<node::EnvironmentFlags::Flags>(node::EnvironmentFlags::kNoFlags);
auto inspector = node::GetInspectorParentHandle(runtime->GetParentEnv(), threadId, resource->GetName().c_str());

nodeData = node::CreateIsolateData(isolate, uvLoop, runtime->GetPlatform());
std::vector<std::string> argv = { "altv-resource" };
env = node::CreateEnvironment(nodeData, _context, argv, argv, flags);

node::IsolateSettings is;
node::SetIsolateUpForNode(isolate, is);
std::vector<std::string> args{ resource->GetName() };
std::vector<std::string> execArgs{ };

env = node::CreateEnvironment(nodeData, _context, args, execArgs, flags, threadId, std::move(inspector));
node::LoadEnvironment(env, bootstrap_code);

// Not sure it's needed anymore
asyncResource.Reset(isolate, v8::Object::New(isolate));
asyncContext = node::EmitAsyncInit(isolate, asyncResource.Get(isolate), "CNodeResourceImpl");

Expand All @@ -80,6 +80,7 @@ bool CNodeResourceImpl::Start()
OnTick();
}

Log::Debug << "Started" << Log::Endl;
DispatchStartEvent(startError);

return !startError;
Expand All @@ -91,6 +92,8 @@ bool CNodeResourceImpl::Stop()
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);

Log::Debug << "Before stop" << Log::Endl;

{
v8::Context::Scope scope(GetContext());
DispatchStopEvent();
Expand All @@ -99,6 +102,8 @@ bool CNodeResourceImpl::Stop()
asyncResource.Reset();
}

Log::Debug << "After stop" << Log::Endl;

node::EmitProcessBeforeExit(env);
node::EmitProcessExit(env);

Expand Down Expand Up @@ -357,6 +362,7 @@ void CNodeResourceImpl::OnTick()
v8::Context::Scope scope(GetContext());
node::CallbackScope callbackScope(isolate, asyncResource.Get(isolate), asyncContext);

runtime->GetPlatform()->DrainTasks(isolate);
uv_run(uvLoop, UV_RUN_NOWAIT);
V8ResourceImpl::OnTick();
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/CNodeResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CNodeScriptRuntime;
class CNodeResourceImpl : public V8ResourceImpl
{
public:
CNodeResourceImpl(CNodeScriptRuntime* _runtime, v8::Isolate* isolate, alt::IResource* resource);
CNodeResourceImpl(CNodeScriptRuntime* _runtime, alt::IResource* resource);

CNodeResourceImpl(const CNodeResourceImpl&) = delete;

Expand Down
45 changes: 31 additions & 14 deletions server/src/CNodeScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,45 @@
bool CNodeScriptRuntime::Init()
{
ProcessConfigOptions();
std::vector<std::string> argv = GetNodeArgs();
std::vector<std::string> execArgv;
std::vector<std::string> errors;
node::InitializeNodeWithArgs(&argv, &execArgv, &errors);
if(errors.size() > 0)
auto result = node::InitializeOncePerProcess(GetNodeArgs());

if (result->early_return())
{
for(std::string& error : errors)
for (auto& error : result->errors())
{
Log::Error << "Error while initializing node: " << error << Log::Endl;
}

return false;
}

platform = node::MultiIsolatePlatform::Create(4);
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
platform.reset(result->platform());

isolate = node::NewIsolate(node::CreateArrayBufferAllocator(), uv_default_loop(), platform.get());
auto allocator = node::CreateArrayBufferAllocator();
isolate = node::NewIsolate(allocator, uv_default_loop(), platform.get());
node::IsolateData* nodeData = node::CreateIsolateData(isolate, uv_default_loop(), platform.get(), allocator);

// node::IsolateSettings is;
// node::SetIsolateUpForNode(isolate, is);

// IsWorker data slot
isolate->SetData(v8::Isolate::GetNumberOfDataSlots() - 1, new bool(false));
// isolate->SetData(v8::Isolate::GetNumberOfDataSlots() - 1, new bool(false));

{
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);

V8Class::LoadAll(isolate);
context.Reset(isolate, node::NewContext(isolate));
v8::Context::Scope scope(context.Get(isolate));

parentEnv = node::CreateEnvironment(nodeData, context.Get(isolate), result->args(), result->exec_args());

/*
Load here only needs for debugging as this environment only used as a parent for real environments
*/

// node::LoadEnvironment(parentEnv, "console.log('PARENT INIT'); setInterval(() => {}, 1000);");
}

IRuntimeEventHandler::Start();
Expand All @@ -45,7 +56,7 @@ bool CNodeScriptRuntime::Init()

alt::IResource::Impl* CNodeScriptRuntime::CreateImpl(alt::IResource* resource)
{
auto res = new CNodeResourceImpl{ this, isolate, resource };
auto res = new CNodeResourceImpl{ this, resource };
resources.insert(res);
return res;
}
Expand All @@ -54,8 +65,10 @@ void CNodeScriptRuntime::OnTick()
{
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::SealHandleScope seal(isolate);
v8::HandleScope seal(isolate);
v8::Context::Scope scope(context.Get(isolate));

uv_run(uv_default_loop(), UV_RUN_NOWAIT);
platform->DrainTasks(isolate);

UpdateMetrics();
Expand Down Expand Up @@ -89,6 +102,9 @@ std::vector<std::string> CNodeScriptRuntime::GetNodeArgs()
Config::Value::ValuePtr moduleConfig = alt::ICore::Instance().GetServerConfig()["js-module"];
if(!moduleConfig->IsDict()) return args;

/*
* Not working as expected anyway
*
// https://nodejs.org/api/cli.html#--inspecthostport
Config::Value::ValuePtr inspector = moduleConfig["inspector"];
if(!inspector->IsNone())
Expand All @@ -97,6 +113,7 @@ std::vector<std::string> CNodeScriptRuntime::GetNodeArgs()
int inspectorPort = inspector["port"]->AsNumber(9229);
args.push_back("--inspect=" + inspectorHost + ":" + std::to_string(inspectorPort));
}
*/

// https://nodejs.org/api/cli.html#--enable-source-maps
Config::Value::ValuePtr enableSourceMaps = moduleConfig["source-maps"];
Expand Down
15 changes: 14 additions & 1 deletion server/src/CNodeScriptRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
class CNodeScriptRuntime : public alt::IScriptRuntime, public IRuntimeEventHandler
{
v8::Isolate* isolate;
V8Helpers::CPersistent<v8::Context> context;
node::Environment* parentEnv;

std::unique_ptr<node::MultiIsolatePlatform> platform;
std::unordered_set<CNodeResourceImpl*> resources;

Expand All @@ -34,9 +37,19 @@ class CNodeScriptRuntime : public alt::IScriptRuntime, public IRuntimeEventHandl
CNodeScriptRuntime() = default;
bool Init();

v8::Isolate* GetIsolate()
/*v8::Isolate* GetIsolate()
{
return isolate;
}*/

node::Environment* GetParentEnv() const
{
return parentEnv;
}

v8::Local<v8::Context> GetContext()
{
return context.Get(isolate);
}

alt::IResource::Impl* CreateImpl(alt::IResource* resource) override;
Expand Down
2 changes: 1 addition & 1 deletion server/src/bindings/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ static void GetWeaponModelByHash(const v8::FunctionCallbackInfo<v8::Value>& info

infoObj->Set(ctx, V8Helpers::JSValue("hash"), V8Helpers::JSValue(modelInfo.hash));
infoObj->Set(ctx, V8Helpers::JSValue("name"), V8Helpers::JSValue(modelInfo.name));
infoObj->Set(ctx, V8Helpers::JSValue("modelName"), V8Helpers::JSValue(modelInfo.modelName));
infoObj->Set(ctx, V8Helpers::JSValue("modelName"), V8Helpers::JSValue(modelInfo.model));
infoObj->Set(ctx, V8Helpers::JSValue("modelHash"), V8Helpers::JSValue(modelInfo.modelHash));
infoObj->Set(ctx, V8Helpers::JSValue("ammoTypeHash"), V8Helpers::JSValue(modelInfo.ammoTypeHash));
infoObj->Set(ctx, V8Helpers::JSValue("ammoType"), V8Helpers::JSValue(modelInfo.ammoType));
Expand Down
Loading

0 comments on commit a43afcb

Please sign in to comment.