From 4ca5da5666c74f310c78fa2ae147b17cb6fea533 Mon Sep 17 00:00:00 2001 From: Koncord Date: Wed, 16 Jan 2019 23:52:22 +0800 Subject: [PATCH] [Server] Rework Plugin API, move Lua system to external library Use LibFFI for Public & Timer APIs Use "PlayerId" type instead "unsigned short" Add GetPluginDir() function --- apps/openmw-mp/CMakeLists.txt | 51 +-- apps/openmw-mp/Cell.cpp | 16 +- apps/openmw-mp/CellController.cpp | 4 +- apps/openmw-mp/MasterClient.cpp | 2 +- apps/openmw-mp/MasterClient.hpp | 2 +- apps/openmw-mp/Networking.cpp | 18 +- apps/openmw-mp/Player.cpp | 4 +- apps/openmw-mp/Player.hpp | 4 +- apps/openmw-mp/Script/API/PublicFnAPI.cpp | 46 ++- apps/openmw-mp/Script/API/PublicFnAPI.hpp | 19 +- apps/openmw-mp/Script/API/TimerAPI.cpp | 77 +++-- apps/openmw-mp/Script/API/TimerAPI.hpp | 26 +- apps/openmw-mp/Script/ArgsStore.hpp | 25 ++ apps/openmw-mp/Script/CTypes.h | 28 ++ apps/openmw-mp/Script/Callback.hpp | 44 +++ apps/openmw-mp/Script/Callbacks.cpp | 13 + apps/openmw-mp/Script/Callbacks.hpp | 105 ++++++ apps/openmw-mp/Script/FFI.cpp | 149 ++++++++ apps/openmw-mp/Script/FFI.hpp | 77 +++++ apps/openmw-mp/Script/Functions/Actors.cpp | 8 +- apps/openmw-mp/Script/Functions/Actors.h | 261 +++++--------- apps/openmw-mp/Script/Functions/Books.cpp | 14 +- apps/openmw-mp/Script/Functions/Books.h | 25 +- apps/openmw-mp/Script/Functions/Cells.cpp | 26 +- apps/openmw-mp/Script/Functions/Cells.h | 46 +-- apps/openmw-mp/Script/Functions/CharClass.cpp | 34 +- apps/openmw-mp/Script/Functions/CharClass.h | 55 +-- apps/openmw-mp/Script/Functions/Chat.cpp | 6 +- apps/openmw-mp/Script/Functions/Chat.h | 11 +- apps/openmw-mp/Script/Functions/Dialogue.cpp | 18 +- apps/openmw-mp/Script/Functions/Dialogue.h | 32 +- apps/openmw-mp/Script/Functions/Factions.cpp | 24 +- apps/openmw-mp/Script/Functions/Factions.h | 53 +-- apps/openmw-mp/Script/Functions/GUI.cpp | 28 +- apps/openmw-mp/Script/Functions/GUI.h | 54 +-- apps/openmw-mp/Script/Functions/Items.cpp | 56 +-- apps/openmw-mp/Script/Functions/Items.h | 102 ++---- apps/openmw-mp/Script/Functions/Mechanics.cpp | 54 +-- apps/openmw-mp/Script/Functions/Mechanics.h | 91 ++--- .../Script/Functions/Miscellaneous.cpp | 2 +- .../Script/Functions/Miscellaneous.h | 28 +- apps/openmw-mp/Script/Functions/Objects.cpp | 12 +- apps/openmw-mp/Script/Functions/Objects.h | 226 ++++++------ apps/openmw-mp/Script/Functions/Positions.cpp | 28 +- apps/openmw-mp/Script/Functions/Positions.h | 48 +-- apps/openmw-mp/Script/Functions/Public.cpp | 34 +- apps/openmw-mp/Script/Functions/Public.h | 10 +- apps/openmw-mp/Script/Functions/Quests.cpp | 44 +-- apps/openmw-mp/Script/Functions/Quests.h | 44 +-- .../Script/Functions/RecordsDynamic.cpp | 4 +- .../Script/Functions/RecordsDynamic.h | 279 +++++---------- apps/openmw-mp/Script/Functions/Server.cpp | 17 +- apps/openmw-mp/Script/Functions/Server.h | 82 ++--- apps/openmw-mp/Script/Functions/Settings.cpp | 18 +- apps/openmw-mp/Script/Functions/Settings.h | 30 +- .../openmw-mp/Script/Functions/Shapeshift.cpp | 20 +- apps/openmw-mp/Script/Functions/Shapeshift.h | 33 +- apps/openmw-mp/Script/Functions/Spells.cpp | 18 +- apps/openmw-mp/Script/Functions/Spells.h | 16 +- apps/openmw-mp/Script/Functions/Stats.cpp | 102 +++--- apps/openmw-mp/Script/Functions/Stats.h | 112 +++--- apps/openmw-mp/Script/Functions/Timer.cpp | 27 +- apps/openmw-mp/Script/Functions/Timer.h | 31 +- .../openmw-mp/Script/Functions/Worldstate.cpp | 12 +- apps/openmw-mp/Script/Functions/Worldstate.h | 138 +++----- apps/openmw-mp/Script/LangLua/LangLua.cpp | 323 ------------------ apps/openmw-mp/Script/LangLua/LangLua.hpp | 54 --- apps/openmw-mp/Script/LangLua/LuaFunc.cpp | 184 ---------- .../Script/LangNative/LangNative.cpp | 102 ------ .../Script/LangNative/LangNative.hpp | 28 -- apps/openmw-mp/Script/Language.hpp | 28 -- apps/openmw-mp/Script/Platform.h | 2 + apps/openmw-mp/Script/Plugin.cpp | 85 +++++ apps/openmw-mp/Script/Plugin.hpp | 69 ++++ apps/openmw-mp/Script/Script.cpp | 108 ------ apps/openmw-mp/Script/Script.hpp | 121 ------- apps/openmw-mp/Script/ScriptFunction.cpp | 73 ---- apps/openmw-mp/Script/ScriptFunction.hpp | 54 --- apps/openmw-mp/Script/ScriptFunctions.cpp | 70 ---- apps/openmw-mp/Script/ScriptFunctions.hpp | 148 -------- apps/openmw-mp/Script/SystemInterface.hpp | 20 +- apps/openmw-mp/Script/Types.hpp | 128 ++----- apps/openmw-mp/Script/api.h | 84 ++++- apps/openmw-mp/main.cpp | 22 +- apps/openmw-mp/processors/ActorProcessor.hpp | 1 - apps/openmw-mp/processors/ObjectProcessor.hpp | 1 - .../processors/ProcessorInitializer.cpp | 1 - .../processors/actor/ProcessorActorAI.hpp | 2 +- .../actor/ProcessorActorCellChange.hpp | 2 +- .../processors/actor/ProcessorActorDeath.hpp | 2 +- .../actor/ProcessorActorEquipment.hpp | 2 +- .../processors/actor/ProcessorActorList.hpp | 2 +- .../processors/actor/ProcessorActorTest.hpp | 2 +- .../processors/object/ProcessorContainer.hpp | 2 +- .../processors/object/ProcessorDoorState.hpp | 2 +- .../object/ProcessorObjectActivate.hpp | 2 +- .../object/ProcessorObjectDelete.hpp | 2 +- .../processors/object/ProcessorObjectLock.hpp | 2 +- .../object/ProcessorObjectPlace.hpp | 2 +- .../object/ProcessorObjectScale.hpp | 2 +- .../object/ProcessorObjectSpawn.hpp | 2 +- .../object/ProcessorObjectState.hpp | 2 +- .../processors/object/ProcessorObjectTrap.hpp | 2 +- .../processors/object/ProcessorVideoPlay.hpp | 2 +- .../processors/player/ProcessorChatMsg.hpp | 3 +- .../player/ProcessorGUIMessageBox.hpp | 2 +- .../player/ProcessorPlayerAttribute.hpp | 2 +- .../processors/player/ProcessorPlayerBook.hpp | 2 +- .../player/ProcessorPlayerBounty.hpp | 2 +- .../player/ProcessorPlayerCellChange.hpp | 4 +- .../player/ProcessorPlayerCellState.hpp | 1 - .../player/ProcessorPlayerCharGen.hpp | 2 +- .../player/ProcessorPlayerDeath.hpp | 2 +- .../player/ProcessorPlayerDisposition.hpp | 2 +- .../player/ProcessorPlayerEquipment.hpp | 2 +- .../player/ProcessorPlayerFaction.hpp | 2 +- .../player/ProcessorPlayerInput.hpp | 2 +- .../player/ProcessorPlayerInventory.hpp | 2 +- .../player/ProcessorPlayerItemUse.hpp | 2 +- .../player/ProcessorPlayerJournal.hpp | 2 +- .../player/ProcessorPlayerLevel.hpp | 2 +- .../player/ProcessorPlayerMiscellaneous.hpp | 2 +- .../player/ProcessorPlayerQuickKeys.hpp | 2 +- .../player/ProcessorPlayerReputation.hpp | 2 +- .../processors/player/ProcessorPlayerRest.hpp | 2 +- .../player/ProcessorPlayerResurrect.hpp | 2 +- .../player/ProcessorPlayerShapeshift.hpp | 2 +- .../player/ProcessorPlayerSkill.hpp | 2 +- .../player/ProcessorPlayerSpellbook.hpp | 2 +- .../player/ProcessorPlayerTopic.hpp | 2 +- .../player/ProcessorWorldKillCount.hpp | 2 +- .../worldstate/ProcessorRecordDynamic.hpp | 2 +- .../worldstate/ProcessorWorldMap.hpp | 2 +- .../worldstate/ProcessorWorldWeather.hpp | 2 +- cmake/FindLibFFI.cmake | 31 ++ components/openmw-mp/Master/MasterData.hpp | 6 +- 136 files changed, 1879 insertions(+), 3083 deletions(-) create mode 100644 apps/openmw-mp/Script/ArgsStore.hpp create mode 100644 apps/openmw-mp/Script/CTypes.h create mode 100644 apps/openmw-mp/Script/Callback.hpp create mode 100644 apps/openmw-mp/Script/Callbacks.cpp create mode 100644 apps/openmw-mp/Script/Callbacks.hpp create mode 100644 apps/openmw-mp/Script/FFI.cpp create mode 100644 apps/openmw-mp/Script/FFI.hpp delete mode 100644 apps/openmw-mp/Script/LangLua/LangLua.cpp delete mode 100644 apps/openmw-mp/Script/LangLua/LangLua.hpp delete mode 100644 apps/openmw-mp/Script/LangLua/LuaFunc.cpp delete mode 100644 apps/openmw-mp/Script/LangNative/LangNative.cpp delete mode 100644 apps/openmw-mp/Script/LangNative/LangNative.hpp delete mode 100644 apps/openmw-mp/Script/Language.hpp create mode 100644 apps/openmw-mp/Script/Plugin.cpp create mode 100644 apps/openmw-mp/Script/Plugin.hpp delete mode 100644 apps/openmw-mp/Script/Script.cpp delete mode 100644 apps/openmw-mp/Script/Script.hpp delete mode 100644 apps/openmw-mp/Script/ScriptFunction.cpp delete mode 100644 apps/openmw-mp/Script/ScriptFunction.hpp delete mode 100644 apps/openmw-mp/Script/ScriptFunctions.cpp delete mode 100644 apps/openmw-mp/Script/ScriptFunctions.hpp create mode 100644 cmake/FindLibFFI.cmake diff --git a/apps/openmw-mp/CMakeLists.txt b/apps/openmw-mp/CMakeLists.txt index b213a39ac..01ab959df 100644 --- a/apps/openmw-mp/CMakeLists.txt +++ b/apps/openmw-mp/CMakeLists.txt @@ -14,31 +14,6 @@ if(ENABLE_BREAKPAD) include_directories(${CMAKE_SOURCE_DIR}/extern/breakpad/src ${Breakpad_Headers}) endif(ENABLE_BREAKPAD) -option(BUILD_WITH_LUA "Enable Lua language" ON) -if(BUILD_WITH_LUA) - - find_package(LuaJit REQUIRED) - - MESSAGE(STATUS "Found LuaJit_LIBRARIES: ${LuaJit_LIBRARIES}") - MESSAGE(STATUS "Found LuaJit_INCLUDE_DIRS: ${LuaJit_INCLUDE_DIRS}") - - set(LuaScript_Sources - Script/LangLua/LangLua.cpp - Script/LangLua/LuaFunc.cpp) - set(LuaScript_Headers - Script/LangLua/LangLua.hpp) - - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_LUA") - include_directories(SYSTEM ${LuaJit_INCLUDE_DIRS} SYSTEM ${CMAKE_SOURCE_DIR}/extern/LuaBridge) -endif(BUILD_WITH_LUA) - -set(NativeScript_Sources - Script/LangNative/LangNative.cpp - ) -set(NativeScript_Headers - Script/LangNative/LangNative.hpp - ) - # local files set(SERVER main.cpp @@ -48,8 +23,8 @@ set(SERVER Cell.cpp CellController.cpp Utils.cpp - Script/Script.cpp Script/ScriptFunction.cpp - Script/ScriptFunctions.cpp + Script/Plugin.cpp + Script/Callbacks.cpp Script/Functions/Actors.cpp Script/Functions/Objects.cpp Script/Functions/Miscellaneous.cpp Script/Functions/Worldstate.cpp @@ -60,20 +35,15 @@ set(SERVER Script/Functions/Positions.cpp Script/Functions/Quests.cpp Script/Functions/RecordsDynamic.cpp Script/Functions/Server.cpp Script/Functions/Settings.cpp Script/Functions/Shapeshift.cpp Script/Functions/Spells.cpp Script/Functions/Stats.cpp Script/Functions/Timer.cpp - Script/Functions/Public.cpp + Script/Functions/Public.cpp Script/FFI.cpp Script/API/TimerAPI.cpp Script/API/PublicFnAPI.cpp - ${LuaScript_Sources} - ${NativeScript_Sources} - ) set(SERVER_HEADER - Script/Types.hpp Script/Script.hpp Script/SystemInterface.hpp - Script/ScriptFunction.hpp Script/Platform.h Script/Language.hpp - Script/ScriptFunctions.hpp Script/API/TimerAPI.hpp Script/API/PublicFnAPI.hpp - ${LuaScript_Headers} - ${NativeScript_Headers} + Script/Types.hpp Script/Plugin.hpp Script/SystemInterface.hpp + Script/Platform.h + Script/Callbacks.hpp Script/API/TimerAPI.hpp Script/API/PublicFnAPI.hpp Script/FFI.hpp ) source_group(tes3mp-server FILES ${SERVER} ${SERVER_HEADER}) @@ -154,6 +124,8 @@ add_executable(tes3mp-server ${APPLE_BUNDLE_RESOURCES} ) +target_compile_definitions(tes3mp-server PRIVATE _HOST) + target_compile_options(tes3mp-server PRIVATE $<$:/permissive->) if (OPENMW_MP_BUILD) @@ -161,7 +133,7 @@ if (OPENMW_MP_BUILD) endif() set_target_properties(tes3mp-server PROPERTIES - CXX_STANDARD 14 + CXX_STANDARD 17 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS YES ) @@ -203,6 +175,11 @@ if (MSVC) add_definitions("-D_USE_MATH_DEFINES") endif (MSVC) + +find_package(LibFFI REQUIRED) +include_directories(SYSTEM ${LIBFFI_INCLUDE_DIR}) +target_link_libraries(tes3mp-server ${LIBFFI_LIBRARIES}) + if (ENABLE_PVS) pvs_studio_add_target(TARGET tes3mp-server.analyze ALL OUTPUT FORMAT errorfile diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index f6138251d..06e6ffcc1 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -8,7 +8,7 @@ #include #include "Player.hpp" -#include "Script/Script.hpp" +#include "Script/Plugin.hpp" using namespace std; @@ -34,7 +34,8 @@ void Cell::addPlayer(Player *player) if (it != end()) { - LOG_APPEND(Log::LOG_INFO, "- Attempt to add %s to Cell %s again was ignored", player->npc.mName.c_str(), getDescription().c_str()); + LOG_APPEND(Log::LOG_INFO, "- Attempt to add %s to Cell %s again was ignored", player->npc.mName.c_str(), + getDescription().c_str()); return; } @@ -48,9 +49,12 @@ void Cell::addPlayer(Player *player) LOG_APPEND(Log::LOG_INFO, "- Adding %s to Cell %s", player->npc.mName.c_str(), getDescription().c_str()); - Script::Call(player->getId(), getDescription().c_str()); - - players.push_back(player); + PlayerId id = player->getId(); + if (id != InvalidPID) + { + Plugin::Call(id, getDescription().c_str()); + players.push_back(player); + } } void Cell::removePlayer(Player *player, bool cleanPlayer) @@ -72,7 +76,7 @@ void Cell::removePlayer(Player *player, bool cleanPlayer) LOG_APPEND(Log::LOG_INFO, "- Removing %s from Cell %s", player->npc.mName.c_str(), getDescription().c_str()); - Script::Call(player->getId(), getDescription().c_str()); + Plugin::Call(player->getId(), getDescription().c_str()); players.erase(it); return; diff --git a/apps/openmw-mp/CellController.cpp b/apps/openmw-mp/CellController.cpp index bdb3c4a87..88eb73969 100644 --- a/apps/openmw-mp/CellController.cpp +++ b/apps/openmw-mp/CellController.cpp @@ -3,7 +3,7 @@ #include #include "Cell.hpp" #include "Player.hpp" -#include "Script/Script.hpp" +#include "Script/Plugin.hpp" using namespace std; @@ -123,7 +123,7 @@ void CellController::removeCell(Cell *cell) { if (*it != nullptr && *it == cell) { - Script::Call(cell->getDescription().c_str()); + Plugin::Call(cell->getDescription().c_str()); LOG_APPEND(Log::LOG_INFO, "- Removing %s from CellController", cell->getDescription().c_str()); delete *it; diff --git a/apps/openmw-mp/MasterClient.cpp b/apps/openmw-mp/MasterClient.cpp index 92d96dab3..85a609dde 100644 --- a/apps/openmw-mp/MasterClient.cpp +++ b/apps/openmw-mp/MasterClient.cpp @@ -105,7 +105,7 @@ void MasterClient::SetRuleValue(const std::string &key, double value) mutexData.unlock(); } -void MasterClient::PushPlugin(const Plugin &plugin) +void MasterClient::PushPlugin(const _Plugin &plugin) { mutexData.lock(); queryData.plugins.push_back(plugin); diff --git a/apps/openmw-mp/MasterClient.hpp b/apps/openmw-mp/MasterClient.hpp index b8b80be33..9420e217d 100644 --- a/apps/openmw-mp/MasterClient.hpp +++ b/apps/openmw-mp/MasterClient.hpp @@ -26,7 +26,7 @@ public: void SetModname(const std::string &hostname); void SetRuleString(const std::string &key, std::string value); void SetRuleValue(const std::string &key, double value); - void PushPlugin(const Plugin &plugin); + void PushPlugin(const _Plugin &plugin); bool Process(RakNet::Packet *packet); void Start(); diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 36f700ff0..bb7af4af9 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -10,7 +10,7 @@ #include #include -#include