Skip to content

Commit

Permalink
Merge branch 'rc' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
C0kkie committed Jul 29, 2024
2 parents e00b005 + 426e67e commit 68b9283
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated
*.gen
shared/JSBindings.h
shared/JSEnums.h

node_modules/
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-2024 alt:V Multiplayer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include(FetchContent)
include(cmake/DepsDownload.cmake)
set(BINDINGS_SCOPE "CLIENT")
include(../shared/cmake/GenerateBindings.cmake)
include(../shared/cmake/GenerateEnums.cmake)

project(altv-client-js)

Expand Down Expand Up @@ -112,6 +113,7 @@ if(DYNAMIC_BUILD EQUAL 1)
)

add_dependencies(${PROJECT_NAME} ${SDK_PROJECT_NAME} js-bindings)
add_dependencies(${PROJECT_NAME} ${SDK_PROJECT_NAME} js-enums)
else()
## STATIC
add_library(
Expand All @@ -130,6 +132,7 @@ else()
)

add_dependencies(${PROJECT_NAME}-static js-bindings)
add_dependencies(${PROJECT_NAME}-static js-enums)
endif()

if(ALTV_JS_DEINIT_CPPSDK)
Expand Down
14 changes: 12 additions & 2 deletions client/src/bindings/HandlingData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
auto handling = alt::ICore::Instance().GetHandlingData(modelHash);
V8_CHECK(handling, "model doesn't exist");


V8Helpers::SetObjectClass(info.GetIsolate(), info.This(), V8Class::ObjectClass::HANDLING_DATA);
info.This()->SetInternalField(1, info[0]);
}
Expand All @@ -31,6 +30,16 @@ static void GetForHandlingName(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_RETURN(v8HandlingData.New(isolate->GetEnteredOrMicrotaskContext(), args));
}

static void ReloadVehiclePhysics(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();

V8_CHECK_ARGS_LEN(1);
V8_ARG_TO_UINT(1, modelHash);

V8_RETURN_BOOLEAN(alt::ICore::Instance().ReloadVehiclePhysics(modelHash));
}

static void HandlingNameHashGetter(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
Expand Down Expand Up @@ -1733,10 +1742,11 @@ static void DamageFlagsSetter(v8::Local<v8::String>, v8::Local<v8::Value> val, c

extern V8Class v8HandlingData("HandlingData", Constructor, [](v8::Local<v8::FunctionTemplate> tpl) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();

tpl->InstanceTemplate()->SetInternalFieldCount(static_cast<int>(V8Class::InternalFields::COUNT));

V8Helpers::SetStaticMethod(isolate, tpl, "getForHandlingName", &GetForHandlingName);
V8Helpers::SetStaticMethod(isolate, tpl, "reloadVehiclePhysics", &ReloadVehiclePhysics);

V8Helpers::SetAccessor(isolate, tpl, "handlingNameHash", &HandlingNameHashGetter);
V8Helpers::SetAccessor(isolate, tpl, "mass", &MassGetter, &MassSetter);
Expand Down
20 changes: 20 additions & 0 deletions client/src/bindings/Ped.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ static void StaticGetByScriptID(const v8::FunctionCallbackInfo<v8::Value>& info)
}
}

static void StaticGetByRemoteId(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_CHECK_ARGS_LEN(1);

V8_ARG_TO_INT32(1, id);

alt::IBaseObject* entity = alt::ICore::Instance().GetBaseObjectByRemoteID(alt::IBaseObject::Type::PED, id);

if(entity)
{
V8_RETURN_BASE_OBJECT(entity);
}
else
{
V8_RETURN_NULL();
}
}

// clang-format off
extern V8Class v8Entity;
extern V8Class v8Ped("Ped", v8Entity, [](v8::Local<v8::FunctionTemplate> tpl)
Expand All @@ -81,6 +100,7 @@ extern V8Class v8Ped("Ped", v8Entity, [](v8::Local<v8::FunctionTemplate> tpl)
V8Helpers::SetStaticAccessor(isolate, tpl, "streamedIn", &StreamedInGetter);
V8Helpers::SetStaticMethod(isolate, tpl, "getByID", StaticGetByID);
V8Helpers::SetStaticMethod(isolate, tpl, "getByScriptID", StaticGetByScriptID);
V8Helpers::SetStaticMethod(isolate, tpl, "getByRemoteID", StaticGetByRemoteId);

V8Helpers::SetAccessor<IPed, uint32_t, &IPed::GetCurrentWeapon>(isolate, tpl, "currentWeapon");
V8Helpers::SetAccessor<IPed, uint16_t, &IPed::GetHealth>(isolate, tpl, "health");
Expand Down
2 changes: 2 additions & 0 deletions server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include(../shared/deps/cpp-sdk/CMakeLists.txt)
include(cmake/DepsDownload.cmake)
set(BINDINGS_SCOPE "SERVER")
include(../shared/cmake/GenerateBindings.cmake)
include(../shared/cmake/GenerateEnums.cmake)

project(js-module)

Expand Down Expand Up @@ -145,6 +146,7 @@ add_library(
)

add_dependencies(${PROJECT_NAME} alt-sdk js-bindings)
add_dependencies(${PROJECT_NAME} alt-sdk js-enums)

if(MSVC AND WIN32)
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG")
Expand Down
14 changes: 12 additions & 2 deletions server/src/CNodeResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool CNodeResourceImpl::Start()
std::vector<std::string> args{ resource->GetName() };
std::vector<std::string> execArgs{ };

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

// Not sure it's needed anymore
Expand Down Expand Up @@ -171,7 +171,17 @@ void CNodeResourceImpl::OnEvent(const alt::CEvent* e)
auto ev = static_cast<const alt::CPlayerDisconnectEvent*>(e);
auto player = ev->GetTarget();

remoteRPCHandlers.erase(player);
if (remoteRPCHandlers[player].size() > 0)
{
for (const auto rpcHandler : remoteRPCHandlers[player])
{
const auto promise = rpcHandler.PromiseResolver.Get(GetIsolate());
if (promise->IsPromise())
promise->Reject(GetContext(), V8Helpers::JSValue("Player disconnected"));
}

remoteRPCHandlers.erase(player);
}

for (auto it = awaitableRPCHandlers.rbegin(); it != awaitableRPCHandlers.rend(); ++it)
{
Expand Down
2 changes: 1 addition & 1 deletion shared/V8ResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class V8ResourceImpl : public alt::IResource::Impl
template<class T>
v8::Local<v8::Value> GetBaseObjectOrNull(const T*& handle)
{
return GetBaseObjectOrNull(handle.Get());
return GetBaseObjectOrNull(handle->Get());
}

v8::Local<v8::Value> CreateVector3(alt::Vector3f vec);
Expand Down
52 changes: 52 additions & 0 deletions shared/bindings/BindingsMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../V8Helpers.h"
#include "../V8ResourceImpl.h"
#include "../V8Module.h"
#include "JSEnums.h"

static void HashCb(const v8::FunctionCallbackInfo<v8::Value>& info)
{
Expand Down Expand Up @@ -465,6 +466,55 @@ static void GetNetTime(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_RETURN_UINT(netTime);
}

// Enums module doesn't need to import any modules
static v8::MaybeLocal<v8::Module> HandleEnumsModuleImport(v8::Local<v8::Context> context, v8::Local<v8::String> specifier, v8::Local<v8::FixedArray>, v8::Local<v8::Module> referrer) {
v8::MaybeLocal<v8::Module> maybeModule;
Log::Error << "Enums module must not import anything" << Log::Endl;
return maybeModule;
}

static void AddEnumsToSharedModuleExports(v8::Isolate* isolate, v8::Local<v8::Context> ctx, v8::Local<v8::Object> exports) {
v8::Local<v8::String> sourceCode = V8Helpers::JSValue(JSEnums::GetBindingsCode());

v8::ScriptOrigin scriptOrigin(isolate, V8Helpers::JSValue("js-enums"), 0, 0, false, -1, v8::Local<v8::Value>(), false, false, true, v8::Local<v8::PrimitiveArray>());

v8::ScriptCompiler::Source source{ sourceCode, scriptOrigin };
auto maybeModule = v8::ScriptCompiler::CompileModule(isolate, &source);
if (maybeModule.IsEmpty()) {
Log::Error << "Failed to compile js-enums module" << Log::Endl;
return;
}

auto mod = maybeModule.ToLocalChecked();
v8::Maybe<bool> result = mod->InstantiateModule(ctx, HandleEnumsModuleImport);
if(result.IsNothing() || result.ToChecked() == false)
{
Log::Error << "Failed to instantiate js-enums module" << Log::Endl;
return;
}

auto returnValue = mod->Evaluate(ctx);
if(returnValue.IsEmpty())
{
Log::Error << "Failed to evaluate js-enums module" << Log::Endl;
return;
}

auto enumsNamespace = mod->GetModuleNamespace();
auto enums = enumsNamespace.As<v8::Object>();
v8::Local<v8::Array> keys;
enums->GetOwnPropertyNames(ctx).ToLocal(&keys);

for(uint32_t i = 0; i < keys->Length(); ++i)
{
v8::Local<v8::Value> key;
keys->Get(ctx, i).ToLocal(&key);
v8::Local<v8::Value> value;
enums->Get(ctx, key).ToLocal(&value);
exports->Set(ctx, key, value);
}
}

extern V8Class v8BaseObject, v8WorldObject, v8Entity, v8File, v8RGBA, v8Vector2, v8Vector3, v8Quaternion, v8Blip, v8AreaBlip, v8RadiusBlip, v8PointBlip, v8Resource, v8Utils;

extern V8Module
Expand Down Expand Up @@ -531,6 +581,8 @@ extern V8Module
V8_OBJECT_SET_INT(exports, "defaultDimension", alt::DEFAULT_DIMENSION);
V8_OBJECT_SET_INT(exports, "globalDimension", alt::GLOBAL_DIMENSION);

AddEnumsToSharedModuleExports(isolate, ctx, exports);

#ifdef ALT_CLIENT_API
V8_OBJECT_SET_BOOLEAN(exports, "isClient", true);
V8_OBJECT_SET_BOOLEAN(exports, "isServer", false);
Expand Down
16 changes: 16 additions & 0 deletions shared/cmake/GenerateEnums.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generates the header files for the JavaScript enums transpiled from altv-types
if(NOT BINDINGS_SCOPE)
set(BINDINGS_SCOPE "SHARED")
endif()

if (CMAKE_HOST_WIN32)
add_custom_target(js-enums
call generate-enums.bat ${BINDINGS_SCOPE}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)
else()
add_custom_target(js-enums
bash generate-enums.sh ${BINDINGS_SCOPE}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)
endif()
1 change: 1 addition & 0 deletions shared/cmake/generate-enums.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node ../../tools/enums-transpiler.js ..
1 change: 1 addition & 0 deletions shared/cmake/generate-enums.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node ../../tools/enums-transpiler.js ..
2 changes: 1 addition & 1 deletion shared/helpers/Bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace V8Helpers
v8::Isolate* isolate, v8::Local<v8::Context> ctx, v8::Local<v8::Object> target, const char* name, v8::FunctionCallback slowCb, FastFunc&& fastCb, const char* className = "")
{
V8FastFunction* func = V8FastFunction::GetOrCreate(name, className, slowCb, fastCb);
target->Set(ctx, v8::String::NewFromUtf8(isolate, name), func->GetTemplate(isolate)->GetFunction(ctx).ToLocalChecked());
target->Set(ctx, v8::String::NewFromUtf8(isolate, name).ToLocalChecked(), func->GetTemplate(isolate)->GetFunction(ctx).ToLocalChecked());
}
#endif
} // namespace V8Helpers
Loading

0 comments on commit 68b9283

Please sign in to comment.