Skip to content

Commit

Permalink
Merge branch 'dev' into rc
Browse files Browse the repository at this point in the history
Former-commit-id: 34ce93f
  • Loading branch information
martonp96 committed Dec 8, 2020
2 parents 5690e9f + 1c93fd3 commit 9f2fc7d
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 44 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_definitions(-DBUILDING_CHAKRASHIM=1)
add_definitions(-DHTTP_PARSER_STRICT=0)
add_definitions(-DNGHTTP2_STATICLIB)
add_definitions(-DHAVE_INSPECTOR=1)
add_definitions(-DALT_SERVER_API)

file(GLOB_RECURSE PROJECT_SOURCE_FILES "src/*.h" "src/*.hpp" "src/*.cpp")

Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
# altv-js-module
JS module for alt:V Multiplayer. Powered by NodeJS & v8

## Troubleshooting
**Problem**: I want to build a native Node.js addon with
[node-gyp](https://github.com/nodejs/node-gyp) but the alt:V server can not load
it and responds with following error:
```
./altv-server: symbol lookup error: /usr/share/addon/binding.node: undefined symbol: node_module_register
```
**Solution**: Add a C++ file to your project with following content and include
it the ```sources``` in your ```binding.gyp``` file.
```cpp
#include <node.h>
#include <dlfcn.h>

extern "C" NODE_EXTERN void node_module_register(void* mod) {
auto base_ptr = dlopen("libnode.so.72", RTLD_NOW | RTLD_GLOBAL);
if (base_ptr == nullptr) {
return;
}
auto register_func = reinterpret_cast<decltype(&node_module_register)>(dlsym(base_ptr, "node_module_register"));
if (register_func == nullptr) {
return;
}
register_func(mod);
}
2 changes: 2 additions & 0 deletions src/CNodeResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "helpers/V8Entity.h"
#include "helpers/V8Helpers.h"

#include "node.h"

class CNodeScriptRuntime;

class CNodeResourceImpl : public V8ResourceImpl
Expand Down
8 changes: 2 additions & 6 deletions src/CNodeScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,9 @@ void CNodeScriptRuntime::OnDispose()
} while (uv_loop_alive(uv_default_loop()));
}*/

platform->DrainTasks(isolate);
platform->CancelPendingDelayedTasks(isolate);
platform->UnregisterIsolate(isolate);

isolate->Dispose();

v8::V8::Dispose();
v8::V8::ShutdownPlatform();

platform.release();
//node::FreePlatform(platform.release());
}
5 changes: 3 additions & 2 deletions src/bindings/Blip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ static void PointConstructor(const v8::FunctionCallbackInfo<v8::Value>& info)
V8Helpers::Throw(isolate, "Failed to create Blip");
}

static V8Class v8Blip("Blip", "WorldObject", nullptr, [](v8::Local<v8::FunctionTemplate> tpl) {
extern V8Class v8WorldObject;
extern V8Class v8Blip("Blip", v8WorldObject, nullptr, [](v8::Local<v8::FunctionTemplate> tpl) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
});

static V8Class v8PointBlip("PointBlip", "Blip", PointConstructor, [](v8::Local<v8::FunctionTemplate> tpl) {
extern V8Class v8PointBlip("PointBlip", v8Blip, PointConstructor, [](v8::Local<v8::FunctionTemplate> tpl) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
});

Expand Down
3 changes: 2 additions & 1 deletion src/bindings/Checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
V8Helpers::Throw(isolate, "Failed to create Checkpoint");
}

static V8Class v8Checkpoint("Checkpoint", "Colshape", Constructor, [](v8::Local<v8::FunctionTemplate> tpl) {
extern V8Class v8Colshape;
extern V8Class v8Checkpoint("Checkpoint", v8Colshape, Constructor, [](v8::Local<v8::FunctionTemplate> tpl) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
});
13 changes: 7 additions & 6 deletions src/bindings/ColShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ static void PlayersOnlySetter(v8::Local<v8::String> name, v8::Local<v8::Value> v
_this->SetPlayersOnly(playersOnly);
}

static V8Class v8Colshape("Colshape", "WorldObject", nullptr, [](v8::Local<v8::FunctionTemplate> tpl) {
extern V8Class v8WorldObject;
extern V8Class v8Colshape("Colshape", v8WorldObject, nullptr, [](v8::Local<v8::FunctionTemplate> tpl) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();

V8::SetAccessor(isolate, tpl, "colshapeType", ColshapeTypeGetter);
Expand All @@ -80,7 +81,7 @@ static V8Class v8Colshape("Colshape", "WorldObject", nullptr, [](v8::Local<v8::F
V8::SetMethod(isolate, tpl, "isPointIn", IsPointIn);
});

static V8Class v8ColshapeCylinder("ColshapeCylinder", "Colshape", [](const v8::FunctionCallbackInfo<v8::Value>& info)
extern V8Class v8ColshapeCylinder("ColshapeCylinder", v8Colshape, [](const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();

Expand All @@ -105,7 +106,7 @@ static V8Class v8ColshapeCylinder("ColshapeCylinder", "Colshape", [](const v8::F
V8Helpers::Throw(isolate, "Failed to create ColshapeCylinder");
}, [](v8::Local<v8::FunctionTemplate> tpl) {});

static V8Class v8ColshapeSphere("ColshapeSphere", "Colshape", [](const v8::FunctionCallbackInfo<v8::Value>& info)
extern V8Class v8ColshapeSphere("ColshapeSphere", v8Colshape, [](const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();

Expand All @@ -128,7 +129,7 @@ static V8Class v8ColshapeSphere("ColshapeSphere", "Colshape", [](const v8::Funct
V8Helpers::Throw(isolate, "Failed to create ColshapeSphere");
}, [](v8::Local<v8::FunctionTemplate> tpl) {});

static V8Class v8ColshapeCircle("ColshapeCircle", "Colshape", [](const v8::FunctionCallbackInfo<v8::Value>& info)
extern V8Class v8ColshapeCircle("ColshapeCircle", v8Colshape, [](const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();

Expand All @@ -150,7 +151,7 @@ static V8Class v8ColshapeCircle("ColshapeCircle", "Colshape", [](const v8::Funct
V8Helpers::Throw(isolate, "Failed to create ColshapeCircle");
}, [](v8::Local<v8::FunctionTemplate> tpl) {});

static V8Class v8ColshapeCuboid("ColshapeCuboid", "Colshape", [](const v8::FunctionCallbackInfo<v8::Value>& info)
extern V8Class v8ColshapeCuboid("ColshapeCuboid", v8Colshape, [](const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();

Expand All @@ -175,7 +176,7 @@ static V8Class v8ColshapeCuboid("ColshapeCuboid", "Colshape", [](const v8::Funct
V8Helpers::Throw(isolate, "Failed to create ColshapeCuboid");
}, [](v8::Local<v8::FunctionTemplate> tpl) {});

static V8Class v8ColshapeRectangle("ColshapeRectangle", "Colshape", [](const v8::FunctionCallbackInfo<v8::Value>& info)
extern V8Class v8ColshapeRectangle("ColshapeRectangle", v8Colshape, [](const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();

Expand Down
62 changes: 40 additions & 22 deletions src/bindings/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,26 +242,45 @@ static void ResourceLoaded(const v8::FunctionCallbackInfo<v8::Value>& info)
}
}

static V8Module v8Alt("alt",
extern V8Class v8Vector3,
v8RGBA,
v8File,
v8BaseObject,
v8WorldObject,
v8Entity,
v8Player,
v8Vehicle,
v8Blip,
v8PointBlip,
v8Checkpoint,
v8VoiceChannel,
v8Colshape,
v8ColshapeCylinder,
v8ColshapeSphere,
v8ColshapeCircle,
v8ColshapeCuboid,
v8ColshapeRectangle;

extern V8Module v8Alt("alt",
{
"Vector3",
"RGBA",
"File",
"BaseObject",
"WorldObject",
"Entity",
"Player",
"Vehicle",
"Blip",
"PointBlip",
"Checkpoint",
"VoiceChannel",
"Colshape",
"ColshapeCylinder",
"ColshapeSphere",
"ColshapeCircle",
"ColshapeCuboid",
"ColshapeRectangle"
v8Vector3,
v8RGBA,
v8File,
v8BaseObject,
v8WorldObject,
v8Entity,
v8Player,
v8Vehicle,
v8Blip,
v8PointBlip,
v8Checkpoint,
v8VoiceChannel,
v8Colshape,
v8ColshapeCylinder,
v8ColshapeSphere,
v8ColshapeCircle,
v8ColshapeCuboid,
v8ColshapeRectangle
},
[](v8::Local<v8::Context> ctx, v8::Local<v8::Object> exports) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
Expand Down Expand Up @@ -292,7 +311,6 @@ static V8Module v8Alt("alt",
alt::StringView rootDir = alt::ICore::Instance().GetRootDirectory();
exports->Set(isolate->GetEnteredContext(), v8::String::NewFromUtf8(isolate, "rootDir"), v8::String::NewFromUtf8(isolate, rootDir.CStr()));

alt::IResource* resource = V8ResourceImpl::GetResource(ctx);
V8_CHECK(resource, "invalid resource");
exports->Set(isolate->GetEnteredContext(), v8::String::NewFromUtf8(isolate, "resourceName"), v8::String::NewFromUtf8(isolate, resource->GetName().CStr()));
exports->Set(isolate->GetEnteredContext(), v8::String::NewFromUtf8(isolate, "defaultDimension"), v8::Integer::New(isolate, alt::DEFAULT_DIMENSION));
exports->Set(isolate->GetEnteredContext(), v8::String::NewFromUtf8(isolate, "globalDimension"), v8::Integer::New(isolate, alt::GLOBAL_DIMENSION));
});
19 changes: 18 additions & 1 deletion src/bindings/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,21 @@ static void RemoveAllWeapons(const v8::FunctionCallbackInfo<v8::Value>& info)
player->RemoveAllWeapons();
}

static void ClearBloodDamage(const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();

V8ResourceImpl* resource = V8ResourceImpl::Get(isolate->GetEnteredContext());
V8_CHECK(resource, "invalid resource");

V8Entity* _this = V8Entity::Get(info.This());
V8_CHECK(_this, "entity is invalid");

Ref<IPlayer> player = _this->GetHandle().As<IPlayer>();

player->ClearBloodDamage();
}

static void AddWeaponComponent(const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();
Expand Down Expand Up @@ -589,7 +604,8 @@ static void StaticGetByID(const v8::FunctionCallbackInfo<v8::Value>& info)
}
}

static V8Class v8Player("Player", "Entity", nullptr, [](v8::Local<v8::FunctionTemplate> tpl) {
extern V8Class v8Entity;
extern V8Class v8Player("Player", v8Entity, nullptr, [](v8::Local<v8::FunctionTemplate> tpl) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();

v8::Local<v8::ObjectTemplate> proto = tpl->PrototypeTemplate();
Expand Down Expand Up @@ -626,6 +642,7 @@ static V8Class v8Player("Player", "Entity", nullptr, [](v8::Local<v8::FunctionTe
proto->Set(v8::String::NewFromUtf8(isolate, "setDateTime"), v8::FunctionTemplate::New(isolate, &SetDateTime));
proto->Set(v8::String::NewFromUtf8(isolate, "setWeather"), v8::FunctionTemplate::New(isolate, &SetWeather));

proto->Set(v8::String::NewFromUtf8(isolate, "clearBloodDamage"), v8::FunctionTemplate::New(isolate, &ClearBloodDamage));
proto->Set(v8::String::NewFromUtf8(isolate, "giveWeapon"), v8::FunctionTemplate::New(isolate, &GiveWeapon));
proto->Set(v8::String::NewFromUtf8(isolate, "removeWeapon"), v8::FunctionTemplate::New(isolate, &RemoveWeapon));
proto->Set(v8::String::NewFromUtf8(isolate, "removeAllWeapons"), v8::FunctionTemplate::New(isolate, &RemoveAllWeapons));
Expand Down
22 changes: 21 additions & 1 deletion src/bindings/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ static void DriverGetter(v8::Local<v8::String> name, const v8::PropertyCallbackI
V8_RETURN_BASE_OBJECT(_this->GetDriver());
}

static void GetAttached(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_GET_THIS_BASE_OBJECT(_this, IVehicle);
V8_RETURN_BASE_OBJECT(_this->GetAttached());
}

static void GetAttachedTo(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_GET_THIS_BASE_OBJECT(_this, IVehicle);
V8_RETURN_BASE_OBJECT(_this->GetAttachedTo());
}

static void DestroyedGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT();
Expand Down Expand Up @@ -101,7 +115,8 @@ static void StaticGetByID(const v8::FunctionCallbackInfo<v8::Value>& info)
}
}

static V8Class v8Vehicle("Vehicle", "Entity", Constructor, [](v8::Local<v8::FunctionTemplate> tpl) {
extern V8Class v8Entity;
extern V8Class v8Vehicle("Vehicle", v8Entity, Constructor, [](v8::Local<v8::FunctionTemplate> tpl) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();

v8::Local<v8::ObjectTemplate> proto = tpl->PrototypeTemplate();
Expand Down Expand Up @@ -215,11 +230,16 @@ static V8Class v8Vehicle("Vehicle", "Entity", Constructor, [](v8::Local<v8::Func
proto->Set(v8::String::NewFromUtf8(isolate, "setArmoredWindowShootCount"), v8::FunctionTemplate::New(isolate, &SetArmoredWindowShootCount));
proto->Set(v8::String::NewFromUtf8(isolate, "getDamageStatusBase64"), v8::FunctionTemplate::New(isolate, &GetDamageStatus));
proto->Set(v8::String::NewFromUtf8(isolate, "setDamageStatusBase64"), v8::FunctionTemplate::New(isolate, &SetDamageStatus));
proto->Set(v8::String::NewFromUtf8(isolate, "repair"), v8::FunctionTemplate::New(isolate, &SetFixed));

//Script getters/setters
proto->SetAccessor(v8::String::NewFromUtf8(isolate, "manualEngineControl"), &ManualEngineControlGetter, &ManualEngineControlSetter);

//Script methods
proto->Set(v8::String::NewFromUtf8(isolate, "getScriptDataBase64"), v8::FunctionTemplate::New(isolate, &GetScriptData));
proto->Set(v8::String::NewFromUtf8(isolate, "setScriptDataBase64"), v8::FunctionTemplate::New(isolate, &SetScriptData));


proto->Set(v8::String::NewFromUtf8(isolate, "getAttached"), v8::FunctionTemplate::New(isolate, &GetAttached));
proto->Set(v8::String::NewFromUtf8(isolate, "getAttachedTo"), v8::FunctionTemplate::New(isolate, &GetAttachedTo));
});
3 changes: 2 additions & 1 deletion src/bindings/VoiceChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
"Failed to create VoiceChannel, make sure voice chat is enabled");
}

static V8Class v8VoiceChannel("VoiceChannel", "BaseObject", Constructor, [](v8::Local<v8::FunctionTemplate> tpl) {
extern V8Class v8BaseObject;
extern V8Class v8VoiceChannel("VoiceChannel", v8BaseObject, Constructor, [](v8::Local<v8::FunctionTemplate> tpl) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();

V8::SetMethod(isolate, tpl, "addPlayer", &AddPlayer);
Expand Down
12 changes: 12 additions & 0 deletions src/bindings/vehicle/Damage.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,18 @@ namespace V8::Vehicle
vehicle->LoadDamageDataFromBase64(*damageStatus);
}

void SetFixed(const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();

V8Entity* _this = V8Entity::Get(info.This());
V8_CHECK(_this, "entity is invalid");

Ref<IVehicle> vehicle = _this->GetHandle().As<IVehicle>();

vehicle->SetFixed();
}

void GetDamageStatus(const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();
Expand Down
13 changes: 13 additions & 0 deletions src/events/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cpp-sdk/events/CPlayerDamageEvent.h"
#include "cpp-sdk/events/CPlayerDeathEvent.h"
#include "cpp-sdk/events/CPlayerEnterVehicleEvent.h"
#include "cpp-sdk/events/CPlayerEnteringVehicleEvent.h"
#include "cpp-sdk/events/CPlayerLeaveVehicleEvent.h"
#include "cpp-sdk/events/CPlayerChangeVehicleSeatEvent.h"
#include "cpp-sdk/events/CPlayerWeaponChangeEvent.h"
Expand Down Expand Up @@ -74,6 +75,18 @@ V8::LocalEventHandler playerEnterVehicle(
}
);

V8::LocalEventHandler playerEnteringVehicle(
EventType::PLAYER_ENTERING_VEHICLE,
"playerEnteringVehicle", // TODO: don't change names, it's okay
[](V8ResourceImpl* resource, const CEvent* e, std::vector<v8::Local<v8::Value>>& args) {
auto ev = static_cast<const alt::CPlayerEnteringVehicleEvent*>(e);

args.push_back(resource->GetBaseObjectOrNull(ev->GetPlayer()));
args.push_back(resource->GetBaseObjectOrNull(ev->GetTarget()));
args.push_back(v8::Integer::New(resource->GetIsolate(), ev->GetSeat()));
}
);

V8::LocalEventHandler playerLeaveVehicle(
EventType::PLAYER_LEAVE_VEHICLE,
"playerLeftVehicle", // TODO: change name for consistency
Expand Down
Loading

0 comments on commit 9f2fc7d

Please sign in to comment.