diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-23 13:11:22 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-23 13:11:22 +1000 |
commit | d93b02682994730a2b4d33c6604e0b997b7ee977 (patch) | |
tree | 0595583ef8806f58d83f95b0770ca1ef9b6571e8 | |
parent | 5fa97741e509f813d28db90e7bce1aac45e22829 (diff) |
Add WorldServer script contexts
-rw-r--r-- | assets/opensb/scripts/opensb/worldserver/test.lua | 12 | ||||
-rw-r--r-- | assets/opensb/scripts/opensb/worldserver/worldserver.lua | 2 | ||||
-rw-r--r-- | assets/opensb/worldserver.config.patch | 3 | ||||
-rw-r--r-- | source/game/StarUniverseServer.cpp | 7 | ||||
-rw-r--r-- | source/game/StarWorldServer.cpp | 27 | ||||
-rw-r--r-- | source/game/StarWorldServer.hpp | 8 |
6 files changed, 58 insertions, 1 deletions
diff --git a/assets/opensb/scripts/opensb/worldserver/test.lua b/assets/opensb/scripts/opensb/worldserver/test.lua new file mode 100644 index 0000000..eaaf08f --- /dev/null +++ b/assets/opensb/scripts/opensb/worldserver/test.lua @@ -0,0 +1,12 @@ +local module = {} +modules.test = module + +function module.init() + message.setHandler("type", function() + return world.type() + end) +end + +function module.update() + +end
\ No newline at end of file diff --git a/assets/opensb/scripts/opensb/worldserver/worldserver.lua b/assets/opensb/scripts/opensb/worldserver/worldserver.lua new file mode 100644 index 0000000..a84fa9d --- /dev/null +++ b/assets/opensb/scripts/opensb/worldserver/worldserver.lua @@ -0,0 +1,2 @@ +require "/scripts/opensb/util/modules.lua" +modules("/scripts/opensb/worldserver/", {"test"})
\ No newline at end of file diff --git a/assets/opensb/worldserver.config.patch b/assets/opensb/worldserver.config.patch new file mode 100644 index 0000000..79c4640 --- /dev/null +++ b/assets/opensb/worldserver.config.patch @@ -0,0 +1,3 @@ +{ + "scriptContexts" : { "OpenStarbound" : ["/scripts/opensb/worldserver/worldserver.lua"] } +}
\ No newline at end of file diff --git a/source/game/StarUniverseServer.cpp b/source/game/StarUniverseServer.cpp index dcf76e3..51f4cec 100644 --- a/source/game/StarUniverseServer.cpp +++ b/source/game/StarUniverseServer.cpp @@ -18,6 +18,7 @@ #include "StarSky.hpp" #include "StarAiDatabase.hpp" #include "StarBiomeDatabase.hpp" +#include "StarUniverseServerLuaBindings.hpp" namespace Star { @@ -1946,6 +1947,8 @@ Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::shipWorldPromise( else shipWorld->setOrbitalSky(celestialSkyParameters(clientContext->shipCoordinate())); + shipWorld->initLua(this); + auto shipWorldThread = make_shared<WorldServerThread>(shipWorld, ClientShipWorldId(clientShipWorldId)); shipWorldThread->setPause(m_pause); clientContext->updateShipChunks(shipWorldThread->readChunks()); @@ -1986,6 +1989,8 @@ Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::celestialWorldPro worldServer->setUniverseSettings(m_universeSettings); worldServer->setReferenceClock(universeClock); + worldServer->initLua(this); + auto worldThread = make_shared<WorldServerThread>(worldServer, celestialWorldId); worldThread->setPause(m_pause); worldThread->start(); @@ -2105,6 +2110,8 @@ Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::instanceWorldProm } } + worldServer->initLua(this); + auto worldThread = make_shared<WorldServerThread>(worldServer, instanceWorldId); worldThread->setPause(m_pause); worldThread->start(); diff --git a/source/game/StarWorldServer.cpp b/source/game/StarWorldServer.cpp index 83e5cea..f519dc4 100644 --- a/source/game/StarWorldServer.cpp +++ b/source/game/StarWorldServer.cpp @@ -23,6 +23,7 @@ #include "StarFallingBlocksAgent.hpp" #include "StarWarpTargetEntity.hpp" #include "StarUniverseSettings.hpp" +#include "StarUniverseServerLuaBindings.hpp" namespace Star { @@ -71,6 +72,10 @@ WorldServer::WorldServer(WorldChunks const& chunks) { } WorldServer::~WorldServer() { + for (auto& p : m_scriptContexts) + p.second->uninit(); + + m_scriptContexts.clear(); m_spawner.uninit(); writeMetadata(); m_worldStorage->unloadAll(true); @@ -97,6 +102,18 @@ void WorldServer::setReferenceClock(ClockPtr clock) { m_sky->setReferenceClock(clock); } +void WorldServer::initLua(UniverseServer* universe) { + auto assets = Root::singleton().assets(); + for (auto& p : assets->json("/worldserver.config:scriptContexts").toObject()) { + auto scriptComponent = make_shared<ScriptComponent>(); + scriptComponent->setScripts(jsonToStringList(p.second.toArray())); + scriptComponent->addCallbacks("universe", LuaBindings::makeUniverseServerCallbacks(universe)); + + m_scriptContexts.set(p.first, scriptComponent); + scriptComponent->init(this); + } +} + WorldStructure WorldServer::setCentralStructure(WorldStructure centralStructure) { removeCentralStructure(); @@ -516,7 +533,12 @@ List<PacketPtr> WorldServer::getOutgoingPackets(ConnectionId clientId) { } Maybe<Json> WorldServer::receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args) { - return "what a wonderful world"; + Maybe<Json> result; + for (auto& p : m_scriptContexts) { + if (result = p.second->handleMessage(message, fromConnection == ServerConnectionId, args)) + break; + } + return result; } WorldServerFidelity WorldServer::fidelity() const { @@ -582,6 +604,9 @@ void WorldServer::update(float dt) { return a->entityType() < b->entityType(); }); + for (auto& pair : m_scriptContexts) + pair.second->update(pair.second->updateDt(dt)); + updateDamage(dt); if (shouldRunThisStep("wiringUpdate")) m_wireProcessor->process(); diff --git a/source/game/StarWorldServer.hpp b/source/game/StarWorldServer.hpp index b8f3965..8c240d4 100644 --- a/source/game/StarWorldServer.hpp +++ b/source/game/StarWorldServer.hpp @@ -12,6 +12,7 @@ #include "StarInterpolationTracker.hpp" #include "StarWorldStructure.hpp" #include "StarLuaRoot.hpp" +#include "StarLuaComponents.hpp" #include "StarWorldRenderData.hpp" #include "StarWarping.hpp" #include "StarRpcThreadPromise.hpp" @@ -31,6 +32,7 @@ STAR_CLASS(DungeonDefinition); STAR_CLASS(WorldServer); STAR_CLASS(TileEntity); STAR_CLASS(UniverseSettings); +STAR_CLASS(UniverseServer); STAR_EXCEPTION(WorldServerException, StarException); @@ -66,6 +68,8 @@ public: void setReferenceClock(ClockPtr clock); + void initLua(UniverseServer* universe); + // Give this world a central structure. If there is a previous central // structure it is removed first. Returns the structure with transformed // coordinates. @@ -346,6 +350,10 @@ private: WireProcessorPtr m_wireProcessor; LuaRootPtr m_luaRoot; + typedef LuaMessageHandlingComponent<LuaUpdatableComponent<LuaWorldComponent<LuaBaseComponent>>> ScriptComponent; + typedef shared_ptr<ScriptComponent> ScriptComponentPtr; + StringMap<ScriptComponentPtr> m_scriptContexts; + WorldGeometry m_geometry; uint64_t m_currentStep; mutable CellularLightIntensityCalculator m_lightIntensityCalculator; |