Skip to content

Commit

Permalink
Merge pull request #114 from MafiaHub/improvement/scripting-engine-re…
Browse files Browse the repository at this point in the history
…vamp

Scripting engine revamp
  • Loading branch information
zpl-zak authored Aug 27, 2024
2 parents 146e967 + fed4fef commit c5326a2
Show file tree
Hide file tree
Showing 3,233 changed files with 44,474 additions and 1,169,149 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
8 changes: 2 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/vendors)

# Load the global headers one by one (a few ones have relative include path, so we are forced to do so)
include_directories(
"${PROJECT_SOURCE_DIR}/vendors/nodejs/include"
"${PROJECT_SOURCE_DIR}/vendors/glm"
"${PROJECT_SOURCE_DIR}/vendors/minhook/include"
"${PROJECT_SOURCE_DIR}/vendors/firebase/include"
Expand All @@ -27,26 +26,23 @@ include_directories(
"${PROJECT_SOURCE_DIR}/vendors/ntdll"
"${PROJECT_SOURCE_DIR}/vendors/semver"
"${PROJECT_SOURCE_DIR}/vendors/imgui"
"${PROJECT_SOURCE_DIR}/vendors/lua/src"
"${PROJECT_SOURCE_DIR}/vendors/sdl2/include"
"${PROJECT_SOURCE_DIR}/vendors/spdlog/include"
"${PROJECT_SOURCE_DIR}/vendors/optick/src"
"${PROJECT_SOURCE_DIR}/vendors/steamworks/include"
"${PROJECT_SOURCE_DIR}/vendors/galaxy/Include"
"${PROJECT_SOURCE_DIR}/vendors/udis86"
"${PROJECT_SOURCE_DIR}/vendors/httplib"
"${PROJECT_SOURCE_DIR}/vendors/lua-5.2.1/src"
"${PROJECT_SOURCE_DIR}/vendors/lua-5.3.6/src"
"${PROJECT_SOURCE_DIR}/vendors/lua-5.4.4/src"
"${PROJECT_SOURCE_DIR}/vendors/lua-5.4.4/src/lua54"
"${PROJECT_SOURCE_DIR}/vendors/discord/src"
"${PROJECT_SOURCE_DIR}/vendors/sentry/include"
"${PROJECT_SOURCE_DIR}/vendors/curl/include"
"${PROJECT_SOURCE_DIR}/vendors/v8pp"
"${PROJECT_SOURCE_DIR}/vendors/fmt/include"
"${PROJECT_SOURCE_DIR}/vendors/libsig"
"${PROJECT_SOURCE_DIR}/vendors/sfd"
"${PROJECT_SOURCE_DIR}/vendors/cxxopts"
"${PROJECT_SOURCE_DIR}/vendors/fu2"
"${PROJECT_SOURCE_DIR}/vendors/sol"
"${PROJECT_SOURCE_DIR}/vendors/freetype/include"
"${PROJECT_SOURCE_DIR}/vendors/StackWalker/Main/StackWalker"
"${PROJECT_SOURCE_DIR}/vendors/ultralight/include"
Expand Down
11 changes: 8 additions & 3 deletions cmake/FrameworkSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ if(MSVC)
endif()

if(UNIX OR APPLE)
set(DISABLED_WARNS "-Wno-pedantic -Wno-unused-parameter -Wno-unused-result"
"-Wno-deprecated-declarations" "-Wno-invalid-offsetof")
set(DISABLED_WARNS
-Wno-pedantic
-Wno-unused-parameter
-Wno-unused-result
-Wno-deprecated-declarations
-Wno-invalid-offsetof
)
add_compile_options(${DISABLED_WARNS})
set(CMAKE_CXX_FLAGS "-std=c++20 ${CMAKE_CXX_FLAGS}")
endif()
endif()
11 changes: 5 additions & 6 deletions code/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ set(FRAMEWORK_SRC
)

set(FRAMEWORK_SERVER_SRC
src/scripting/module.cpp
src/scripting/engines/node/engine.cpp
src/scripting/engines/node/sdk.cpp
src/scripting/client_engine.cpp
src/scripting/engine.cpp
src/scripting/events.cpp

src/scripting/engines/node/v8_helpers/v8_try_catch.cpp
src/scripting/module.cpp
src/scripting/server_engine.cpp

src/http/webserver.cpp

Expand Down Expand Up @@ -153,7 +152,7 @@ if(WIN32)
endif()

link_shared_deps(FrameworkServer)
target_link_libraries(FrameworkServer NodeJS libsig v8pp)
target_link_libraries(FrameworkServer libsig Lua54)

# Platform-dependent post processing
if(WIN32)
Expand Down
32 changes: 13 additions & 19 deletions code/framework/src/integrations/server/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#include "../shared/modules/mod.hpp"

#include "scripting/engines/node/sdk.h"

#include "utils/version.h"

#include "cxxopts.hpp"
Expand Down Expand Up @@ -113,14 +111,14 @@ namespace Framework::Integrations::Server {
return ServerError::SERVER_WORLD_INIT_FAILED;
}

const auto sdkCallback = [this](Framework::Scripting::Engines::SDKRegisterWrapper sdk) {
this->RegisterScriptingBuiltins(sdk);
const auto sdkCallback = [this](Framework::Scripting::SDKRegisterWrapper<Framework::Scripting::ServerEngine> sdk) {
this->RegisterScriptingBuiltins(sdk.GetEngine());
};

// Initialize the scripting engine
_scriptingEngine->SetProcessArguments(opts.argc, opts.argv);
_scriptingEngine->SetModName(opts.modName);
if (_scriptingEngine->Init(Framework::Scripting::EngineTypes::ENGINE_NODE, sdkCallback) != Framework::Scripting::ModuleError::MODULE_NONE) {
_scriptingEngine->SetMainPath("gamemode");
_scriptingEngine->LoadManifest();
if (_scriptingEngine->InitServerEngine(sdkCallback) != Framework::Scripting::ModuleError::MODULE_NONE) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->critical("Failed to initialize the scripting engine");
return ServerError::SERVER_SCRIPTING_INIT_FAILED;
}
Expand All @@ -130,9 +128,9 @@ namespace Framework::Integrations::Server {
return ServerError::SERVER_FIREBASE_WRAPPER_INIT_FAILED;
}

if (_opts.bindPublicServer && !_masterlist->Init(_opts.bindSecretKey)) {
/*if (_opts.bindPublicServer && !_masterlist->Init(_opts.bindSecretKey)) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->error("Failed to contact masterlist server: Push key is empty");
}
}*/
else if (!_opts.bindPublicServer) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->warn("Server will not be announced to masterlist");
}
Expand All @@ -156,7 +154,7 @@ namespace Framework::Integrations::Server {
PostInit();

// Load the gamemode
_scriptingEngine->LoadGamemode();
_scriptingEngine->GetServerEngine()->LoadScript();

Logging::GetLogger(FRAMEWORK_INNER_SERVER)->info("Host:\t{}", _opts.bindHost);
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->info("Port:\t{}", _opts.bindPort);
Expand Down Expand Up @@ -381,7 +379,7 @@ namespace Framework::Integrations::Server {

if (_masterlist->IsInitialized()) {
Services::ServerInfo info {};
info.gameMode = _scriptingEngine->GetEngine()->GetGameModeName();
// info.gameMode = _scriptingEngine->GetGameModeName();
info.version = Utils::Version::rel;
info.maxPlayers = _opts.maxPlayers;
info.currentPlayers = _networkingEngine->GetNetworkServer()->GetPeer()->NumberOfConnections();
Expand Down Expand Up @@ -418,15 +416,11 @@ namespace Framework::Integrations::Server {
Shutdown();
}

void Instance::RegisterScriptingBuiltins(Framework::Scripting::Engines::SDKRegisterWrapper sdk) {
switch (sdk.GetKind()) {
case Framework::Scripting::EngineTypes::ENGINE_NODE: {
const auto nodeSDK = sdk.GetNodeSDK();
Framework::Integrations::Scripting::Entity::Register(nodeSDK->GetIsolate(), nodeSDK->GetModule());
} break;
}
void Instance::RegisterScriptingBuiltins(Framework::Scripting::ServerEngine *engine) {
// Register the entity builtin
Framework::Integrations::Scripting::Entity::Register(engine->GetLuaEngine());

// mod-specific builtins
ModuleRegister(sdk);
ModuleRegister(engine);
}
} // namespace Framework::Integrations::Server
7 changes: 3 additions & 4 deletions code/framework/src/integrations/server/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "http/webserver.h"
#include "logging/logger.h"
#include "networking/engine.h"
#include "scripting/engines/callback.h"
#include "scripting/server.h"
#include "services/masterlist.h"
#include "utils/config.h"
Expand Down Expand Up @@ -96,7 +95,7 @@ namespace Framework::Integrations::Server {
void InitModules() const;
void InitNetworkingMessages() const;
bool LoadConfigFromJSON();
void RegisterScriptingBuiltins(Framework::Scripting::Engines::SDKRegisterWrapper);
void RegisterScriptingBuiltins(Framework::Scripting::ServerEngine *);

// managers
flecs::entity _weatherManager;
Expand All @@ -122,8 +121,8 @@ namespace Framework::Integrations::Server {

virtual void PreShutdown() {}

virtual void ModuleRegister(Framework::Scripting::Engines::SDKRegisterWrapper sdk) {
(void)sdk;
virtual void ModuleRegister(Framework::Scripting::ServerEngine *engine) {
(void)engine;
}

void Update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

#pragma once

#include "scripting/engines/node/builtins/quaternion.h"
#include "scripting/engines/node/builtins/vector_3.h"
#include "scripting/builtins/quaternion.h"
#include "scripting/builtins/vector_3.h"

#include <glm/glm.hpp>
#include <v8pp/class.hpp>
#include <v8pp/module.hpp>
#include <sol/sol.hpp>

#include "networking/network_server.h"

Expand All @@ -38,9 +37,8 @@ namespace Framework::Integrations::Scripting {
Entity(flecs::entity_t ent) {
_ent = flecs::entity(CoreModules::GetWorldEngine()->GetWorld()->get_world(), ent);
}

static v8::Local<v8::Object> WrapEntity(v8::Isolate *isolate, flecs::entity e) {
return v8pp::class_<Framework::Integrations::Scripting::Entity>::create_object(isolate, e.id());
Entity(flecs::entity ent) {
_ent = ent;
}

flecs::entity_t GetID() const {
Expand All @@ -66,31 +64,31 @@ namespace Framework::Integrations::Scripting {
return ss.str();
}

void SetPosition(Framework::Scripting::Engines::Node::Builtins::Vector3 v3) const {
void SetPosition(Framework::Scripting::Builtins::Vector3 v3) const {
const auto tr = _ent.get_mut<Framework::World::Modules::Base::Transform>();
tr->pos = glm::vec3(v3.GetX(), v3.GetY(), v3.GetZ());
tr->IncrementGeneration();
FW_SEND_SERVER_COMPONENT_GAME_RPC(Framework::World::RPC::SetTransform, _ent, *tr);
CoreModules::GetWorldEngine()->WakeEntity(_ent);
}

void SetRotation(Framework::Scripting::Engines::Node::Builtins::Quaternion q) const {
void SetRotation(Framework::Scripting::Builtins::Quaternion q) const {
const auto tr = _ent.get_mut<Framework::World::Modules::Base::Transform>();
tr->rot = glm::quat(q.GetW(), q.GetX(), q.GetY(), q.GetZ());
tr->IncrementGeneration();
FW_SEND_SERVER_COMPONENT_GAME_RPC(Framework::World::RPC::SetTransform, _ent, *tr);
CoreModules::GetWorldEngine()->WakeEntity(_ent);
}

void SetVelocity(Framework::Scripting::Engines::Node::Builtins::Vector3 v3) const {
void SetVelocity(Framework::Scripting::Builtins::Vector3 v3) const {
const auto tr = _ent.get_mut<Framework::World::Modules::Base::Transform>();
tr->vel = glm::vec3(v3.GetX(), v3.GetY(), v3.GetZ());
tr->IncrementGeneration();
FW_SEND_SERVER_COMPONENT_GAME_RPC(Framework::World::RPC::SetTransform, _ent, *tr);
CoreModules::GetWorldEngine()->WakeEntity(_ent);
}

void SetScale(Framework::Scripting::Engines::Node::Builtins::Vector3 v3) const {
void SetScale(Framework::Scripting::Builtins::Vector3 v3) const {
const auto fr = _ent.get_mut<Framework::World::Modules::Base::Frame>();
fr->scale = glm::vec3(v3.GetX(), v3.GetY(), v3.GetZ());
FW_SEND_SERVER_COMPONENT_GAME_RPC(Framework::World::RPC::SetFrame, _ent, *fr);
Expand All @@ -108,24 +106,24 @@ namespace Framework::Integrations::Scripting {
FW_SEND_SERVER_COMPONENT_GAME_RPC(Framework::World::RPC::SetFrame, _ent, *fr);
}

v8::Local<v8::Object> GetPosition() const {
Framework::Scripting::Builtins::Vector3 GetPosition() const {
const auto tr = _ent.get<Framework::World::Modules::Base::Transform>();
return v8pp::class_<Framework::Scripting::Engines::Node::Builtins::Vector3>::create_object(v8::Isolate::GetCurrent(), tr->pos.x, tr->pos.y, tr->pos.z);
return Framework::Scripting::Builtins::Vector3(tr->pos.x, tr->pos.y, tr->pos.z);
}

v8::Local<v8::Object> GetRotation() const {
Framework::Scripting::Builtins::Quaternion GetRotation() const {
const auto tr = _ent.get<Framework::World::Modules::Base::Transform>();
return v8pp::class_<Framework::Scripting::Engines::Node::Builtins::Quaternion>::create_object(v8::Isolate::GetCurrent(), tr->rot.w, tr->rot.x, tr->rot.y, tr->rot.z);
return Framework::Scripting::Builtins::Quaternion(tr->rot.w, tr->rot.x, tr->rot.y, tr->rot.z);
}

v8::Local<v8::Object> GetVelocity() const {
Framework::Scripting::Builtins::Vector3 GetVelocity() const {
const auto tr = _ent.get<Framework::World::Modules::Base::Transform>();
return v8pp::class_<Framework::Scripting::Engines::Node::Builtins::Vector3>::create_object(v8::Isolate::GetCurrent(), tr->vel.x, tr->vel.y, tr->vel.z);
return Framework::Scripting::Builtins::Vector3(tr->vel.x, tr->vel.y, tr->vel.z);
}

v8::Local<v8::Object> GetScale() const {
Framework::Scripting::Builtins::Vector3 GetScale() const {
const auto fr = _ent.get<Framework::World::Modules::Base::Frame>();
return v8pp::class_<Framework::Scripting::Engines::Node::Builtins::Vector3>::create_object(v8::Isolate::GetCurrent(), fr->scale.x, fr->scale.y, fr->scale.z);
return Framework::Scripting::Builtins::Vector3(fr->scale.x, fr->scale.y, fr->scale.z);
}

std::string GetModelName() const {
Expand Down Expand Up @@ -182,40 +180,34 @@ namespace Framework::Integrations::Scripting {
Framework::World::ServerEngine::RemoveEntity(_ent);
}

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

v8pp::class_<Entity> cls(isolate);
cls.property("id", &Entity::GetID);
cls.property("name", &Entity::GetName);
cls.property("nickname", &Entity::GetNickname);

cls.function("destroy", &Entity::Destroy);
cls.function("getAlwaysVisible", &Entity::IsAlwaysVisible);
cls.function("getModelHash", &Entity::GetModelHash);
cls.function("getModelName", &Entity::GetModelName);
cls.function("getPosition", &Entity::GetPosition);
cls.function("getRotation", &Entity::GetRotation);
cls.function("getScale", &Entity::GetScale);
cls.function("getUpdateInterval", &Entity::GetUpdateInterval);
cls.function("getVelocity", &Entity::GetVelocity);
cls.function("getVirtualWorld", &Entity::GetVirtualWorld);
cls.function("getVisible", &Entity::IsVisible);
cls.function("setAlwaysVisible", &Entity::SetAlwaysVisible);
cls.function("setModelHash", &Entity::SetModelHash);
cls.function("setModelName", &Entity::SetModelName);
cls.function("setPosition", &Entity::SetPosition);
cls.function("setRotation", &Entity::SetRotation);
cls.function("setScale", &Entity::SetScale);
cls.function("setUpdateInterval", &Entity::SetUpdateInterval);
cls.function("setVelocity", &Entity::SetVelocity);
cls.function("setVirtualWorld", &Entity::SetVirtualWorld);
cls.function("setVisible", &Entity::SetVisible);
cls.function("toString", &Entity::ToString);

rootModule->class_("Entity", cls);
static void Register(sol::state &luaEngine) {
sol::usertype<Entity> cls = luaEngine.new_usertype<Entity>("Entity", sol::constructors<Entity(uint64_t)>());
cls.set("id", sol::readonly(&Entity::GetID));
cls.set("name", sol::readonly(&Entity::GetName));
cls.set("nickname", sol::readonly(&Entity::GetNickname));

cls["destroy"] = &Entity::Destroy;
cls["getAlwaysVisible"] = &Entity::IsAlwaysVisible;
cls["getModelHash"] = &Entity::GetModelHash;
cls["getModelName"] = &Entity::GetModelName;
cls["getPosition"] = &Entity::GetPosition;
cls["getRotation"] = &Entity::GetRotation;
cls["getScale"] = &Entity::GetScale;
cls["getUpdateInterval"] = &Entity::GetUpdateInterval;
cls["getVelocity"] = &Entity::GetVelocity;
cls["getVirtualWorld"] = &Entity::GetVirtualWorld;
cls["getVisible"] = &Entity::IsVisible;
cls["setAlwaysVisible"] = &Entity::SetAlwaysVisible;
cls["setModelHash"] = &Entity::SetModelHash;
cls["setModelName"] = &Entity::SetModelName;
cls["setPosition"] = &Entity::SetPosition;
cls["setRotation"] = &Entity::SetRotation;
cls["setScale"] = &Entity::SetScale;
cls["setUpdateInterval"] = &Entity::SetUpdateInterval;
cls["setVelocity"] = &Entity::SetVelocity;
cls["setVirtualWorld"] = &Entity::SetVirtualWorld;
cls["setVisible"] = &Entity::SetVisible;
cls["toString"] = &Entity::ToString;
}
};
} // namespace Framework::Integrations::Scripting
Loading

0 comments on commit c5326a2

Please sign in to comment.