Add WorldServer script contexts

This commit is contained in:
Kae 2023-07-23 13:11:22 +10:00
parent 5fa97741e5
commit d93b026829
6 changed files with 58 additions and 1 deletions

View File

@ -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

View File

@ -0,0 +1,2 @@
require "/scripts/opensb/util/modules.lua"
modules("/scripts/opensb/worldserver/", {"test"})

View File

@ -0,0 +1,3 @@
{
"scriptContexts" : { "OpenStarbound" : ["/scripts/opensb/worldserver/worldserver.lua"] }
}

View File

@ -18,6 +18,7 @@
#include "StarSky.hpp" #include "StarSky.hpp"
#include "StarAiDatabase.hpp" #include "StarAiDatabase.hpp"
#include "StarBiomeDatabase.hpp" #include "StarBiomeDatabase.hpp"
#include "StarUniverseServerLuaBindings.hpp"
namespace Star { namespace Star {
@ -1946,6 +1947,8 @@ Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::shipWorldPromise(
else else
shipWorld->setOrbitalSky(celestialSkyParameters(clientContext->shipCoordinate())); shipWorld->setOrbitalSky(celestialSkyParameters(clientContext->shipCoordinate()));
shipWorld->initLua(this);
auto shipWorldThread = make_shared<WorldServerThread>(shipWorld, ClientShipWorldId(clientShipWorldId)); auto shipWorldThread = make_shared<WorldServerThread>(shipWorld, ClientShipWorldId(clientShipWorldId));
shipWorldThread->setPause(m_pause); shipWorldThread->setPause(m_pause);
clientContext->updateShipChunks(shipWorldThread->readChunks()); clientContext->updateShipChunks(shipWorldThread->readChunks());
@ -1986,6 +1989,8 @@ Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::celestialWorldPro
worldServer->setUniverseSettings(m_universeSettings); worldServer->setUniverseSettings(m_universeSettings);
worldServer->setReferenceClock(universeClock); worldServer->setReferenceClock(universeClock);
worldServer->initLua(this);
auto worldThread = make_shared<WorldServerThread>(worldServer, celestialWorldId); auto worldThread = make_shared<WorldServerThread>(worldServer, celestialWorldId);
worldThread->setPause(m_pause); worldThread->setPause(m_pause);
worldThread->start(); worldThread->start();
@ -2105,6 +2110,8 @@ Maybe<WorkerPoolPromise<WorldServerThreadPtr>> UniverseServer::instanceWorldProm
} }
} }
worldServer->initLua(this);
auto worldThread = make_shared<WorldServerThread>(worldServer, instanceWorldId); auto worldThread = make_shared<WorldServerThread>(worldServer, instanceWorldId);
worldThread->setPause(m_pause); worldThread->setPause(m_pause);
worldThread->start(); worldThread->start();

View File

@ -23,6 +23,7 @@
#include "StarFallingBlocksAgent.hpp" #include "StarFallingBlocksAgent.hpp"
#include "StarWarpTargetEntity.hpp" #include "StarWarpTargetEntity.hpp"
#include "StarUniverseSettings.hpp" #include "StarUniverseSettings.hpp"
#include "StarUniverseServerLuaBindings.hpp"
namespace Star { namespace Star {
@ -71,6 +72,10 @@ WorldServer::WorldServer(WorldChunks const& chunks) {
} }
WorldServer::~WorldServer() { WorldServer::~WorldServer() {
for (auto& p : m_scriptContexts)
p.second->uninit();
m_scriptContexts.clear();
m_spawner.uninit(); m_spawner.uninit();
writeMetadata(); writeMetadata();
m_worldStorage->unloadAll(true); m_worldStorage->unloadAll(true);
@ -97,6 +102,18 @@ void WorldServer::setReferenceClock(ClockPtr clock) {
m_sky->setReferenceClock(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) { WorldStructure WorldServer::setCentralStructure(WorldStructure centralStructure) {
removeCentralStructure(); removeCentralStructure();
@ -516,7 +533,12 @@ List<PacketPtr> WorldServer::getOutgoingPackets(ConnectionId clientId) {
} }
Maybe<Json> WorldServer::receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args) { 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 { WorldServerFidelity WorldServer::fidelity() const {
@ -582,6 +604,9 @@ void WorldServer::update(float dt) {
return a->entityType() < b->entityType(); return a->entityType() < b->entityType();
}); });
for (auto& pair : m_scriptContexts)
pair.second->update(pair.second->updateDt(dt));
updateDamage(dt); updateDamage(dt);
if (shouldRunThisStep("wiringUpdate")) if (shouldRunThisStep("wiringUpdate"))
m_wireProcessor->process(); m_wireProcessor->process();

View File

@ -12,6 +12,7 @@
#include "StarInterpolationTracker.hpp" #include "StarInterpolationTracker.hpp"
#include "StarWorldStructure.hpp" #include "StarWorldStructure.hpp"
#include "StarLuaRoot.hpp" #include "StarLuaRoot.hpp"
#include "StarLuaComponents.hpp"
#include "StarWorldRenderData.hpp" #include "StarWorldRenderData.hpp"
#include "StarWarping.hpp" #include "StarWarping.hpp"
#include "StarRpcThreadPromise.hpp" #include "StarRpcThreadPromise.hpp"
@ -31,6 +32,7 @@ STAR_CLASS(DungeonDefinition);
STAR_CLASS(WorldServer); STAR_CLASS(WorldServer);
STAR_CLASS(TileEntity); STAR_CLASS(TileEntity);
STAR_CLASS(UniverseSettings); STAR_CLASS(UniverseSettings);
STAR_CLASS(UniverseServer);
STAR_EXCEPTION(WorldServerException, StarException); STAR_EXCEPTION(WorldServerException, StarException);
@ -66,6 +68,8 @@ public:
void setReferenceClock(ClockPtr clock); void setReferenceClock(ClockPtr clock);
void initLua(UniverseServer* universe);
// Give this world a central structure. If there is a previous central // Give this world a central structure. If there is a previous central
// structure it is removed first. Returns the structure with transformed // structure it is removed first. Returns the structure with transformed
// coordinates. // coordinates.
@ -346,6 +350,10 @@ private:
WireProcessorPtr m_wireProcessor; WireProcessorPtr m_wireProcessor;
LuaRootPtr m_luaRoot; LuaRootPtr m_luaRoot;
typedef LuaMessageHandlingComponent<LuaUpdatableComponent<LuaWorldComponent<LuaBaseComponent>>> ScriptComponent;
typedef shared_ptr<ScriptComponent> ScriptComponentPtr;
StringMap<ScriptComponentPtr> m_scriptContexts;
WorldGeometry m_geometry; WorldGeometry m_geometry;
uint64_t m_currentStep; uint64_t m_currentStep;
mutable CellularLightIntensityCalculator m_lightIntensityCalculator; mutable CellularLightIntensityCalculator m_lightIntensityCalculator;