Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripting engine revamp #103

Merged
merged 9 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions code/server/src/core/builtins/builtins.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include "scripting/engines/node/engine.h"
#include "scripting/engines/node/sdk.h"
#include <sol/sol.hpp>

#include "scripting/server_engine.h"

#include "chat.h"
#include "player.h"
Expand All @@ -11,11 +12,11 @@
namespace MafiaMP::Scripting {
class Builtins final {
public:
static void Register(v8::Isolate *isolate, v8pp::module *rootModule) {
Scripting::Chat::Register(isolate, rootModule);
Scripting::Human::Register(isolate, rootModule);
Scripting::Vehicle::Register(isolate, rootModule);
Scripting::World::Register(isolate, rootModule);
static void Register(sol::state &luaEngine) {
Scripting::Chat::Register(luaEngine);
Scripting::Human::Register(luaEngine);
Scripting::Vehicle::Register(luaEngine);
Scripting::World::Register(luaEngine);
}
};
} // namespace MafiaMP::Scripting
24 changes: 11 additions & 13 deletions code/server/src/core/builtins/chat.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include "scripting/engines/node/engine.h"
#include "scripting/engines/node/sdk.h"
#include <sol/sol.hpp>

#include "scripting/server_engine.h"

#include "shared/rpc/chat_message.h"

Expand Down Expand Up @@ -29,22 +30,19 @@ namespace MafiaMP::Scripting {
}

static void EventChatMessage(flecs::entity e, std::string message) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
engine->InvokeEvent("chatMessage", Human::WrapHuman(engine, e), message);
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("chatMessage", Human(e), message);
}

static void EventChatCommand(flecs::entity e, std::string message, std::string command, std::vector<std::string> args) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
engine->InvokeEvent("chatCommand", Human::WrapHuman(engine, e), message, command, args);
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("chatCommand", Human(e), message, command, args);
}

static void Register(v8::Isolate *isolate, v8pp::module *rootModule) {
v8pp::module chat(isolate);
chat.function("sendToPlayer", &Chat::SendToPlayer);
chat.function("sendToAll", &Chat::SendToAll);
rootModule->submodule("Chat", chat);
static void Register(sol::state &luaEngine) {
sol::usertype<Chat> cls = luaEngine.new_usertype<Chat>("Chat");
cls["sendToPlayer"] = &Chat::SendToPlayer;
cls["sendToAll"] = &Chat::SendToAll;
}
};
} // namespace MafiaMP::Scripting
58 changes: 21 additions & 37 deletions code/server/src/core/builtins/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
#include "shared/rpc/chat_message.h"

namespace MafiaMP::Scripting {
v8::Local<v8::Object> Human::WrapHuman(Framework::Scripting::Engines::Node::Engine *engine, flecs::entity e) {
return v8pp::class_<Scripting::Human>::create_object(engine->GetIsolate(), e.id());
}

std::string Human::ToString() const {
std::ostringstream ss;
ss << "Human{ id: " << _ent.id() << " }";
return ss.str();
}

void Human::Destroy(v8::Isolate *isolate) {
isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(isolate, "Human object can not be destroyed!").ToLocalChecked()));
void Human::Destroy() {
// Nothing should happen here, as the entity is destroyed by the game and network systems
}

void Human::AddWeapon(int weaponId, int ammo) {
Expand All @@ -31,13 +27,13 @@ namespace MafiaMP::Scripting {
FW_SEND_COMPONENT_RPC_TO(Shared::RPC::ChatMessage, SLNet::RakNetGUID(str->guid), message);
}

v8::Local<v8::Value> Human::GetVehicle() const {
Vehicle Human::GetVehicle() const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);
if (carEnt.is_valid() && carEnt.is_alive()) {
return v8pp::class_<Vehicle>::create_object(v8::Isolate::GetCurrent(), carEnt.id());
return Vehicle(carEnt);
}
return v8::Undefined(v8::Isolate::GetCurrent());
return Vehicle(-1);
}

int Human::GetVehicleSeat() const {
Expand Down Expand Up @@ -67,41 +63,29 @@ namespace MafiaMP::Scripting {
}

void Human::EventPlayerDied(flecs::entity e) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
auto playerObj = WrapHuman(engine, e);
engine->InvokeEvent("playerDied", playerObj);
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("playerDied", Human(e));
}

void Human::EventPlayerConnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
auto playerObj = WrapHuman(engine, e);
engine->InvokeEvent("playerConnected", playerObj);
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("playerConnected", Human(e));
}

void Human::EventPlayerDisconnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
auto playerObj = WrapHuman(engine, e);
engine->InvokeEvent("playerDisconnected", playerObj);
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("playerDisconnected", Human(e));
}

void Human::Register(v8::Isolate *isolate, v8pp::module *rootModule) {
if (!rootModule) {
return;
}

v8pp::class_<Human> cls(isolate);
cls.inherit<Framework::Integrations::Scripting::Entity>();
cls.function("destroy", &Human::Destroy);
cls.function("addWeapon", &Human::AddWeapon);
cls.function("setHealth", &Human::SetHealth);
cls.function("getHealth", &Human::GetHealth);
cls.function("getVehicle", &Human::GetVehicle);
cls.function("getVehicleSeat", &Human::GetVehicleSeat);
cls.function("sendChat", &Human::SendChat);
cls.function("sendChatToAll", &Human::SendChatToAll);
rootModule->class_("Human", cls);
void Human::Register(sol::state &luaEngine) {
sol::usertype<Human> cls = luaEngine.new_usertype<Human>("Human", sol::constructors<Human(uint64_t)>(), sol::base_classes, sol::bases<Entity>());
cls["destroy"] = &Human::Destroy;
cls["addWeapon"] = &Human::AddWeapon;
cls["setHealth"] = &Human::SetHealth;
cls["getHealth"] = &Human::GetHealth;
cls["getVehicle"] = &Human::GetVehicle;
cls["getVehicleSeat"] = &Human::GetVehicleSeat;
cls["sendChat"] = &Human::SendChat;
cls["sendChatToAll"] = &Human::SendChatToAll;
}
}
14 changes: 8 additions & 6 deletions code/server/src/core/builtins/player.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
#pragma once

#include <sol/sol.hpp>

#include "integrations/server/scripting/builtins/node/entity.h"
#include "scripting/engines/node/engine.h"
#include "scripting/server_engine.h"

namespace MafiaMP::Scripting {
class Vehicle;
class Human final: public Framework::Integrations::Scripting::Entity {
public:
Human(flecs::entity_t ent): Entity(ent) {}
Human(flecs::entity ent): Entity(ent) {}

static void EventPlayerDied(flecs::entity e);
static void EventPlayerConnected(flecs::entity e);
static void EventPlayerDisconnected(flecs::entity e);

static void Register(v8::Isolate *isolate, v8pp::module *rootModule);

static v8::Local<v8::Object> WrapHuman(Framework::Scripting::Engines::Node::Engine *engine, flecs::entity e);
static void Register(sol::state &luaEngine);

std::string ToString() const override;

void Destroy(v8::Isolate *isolate);
void Destroy();

void AddWeapon(int weaponId, int ammo);

void SendChat(std::string message);

v8::Local<v8::Value> GetVehicle() const;
Vehicle GetVehicle() const;
int GetVehicleSeat() const;

static void SendChatToAll(std::string message);
Expand Down
Loading