From 121d27446b42c960014b2e69999dad73322b05f3 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Sun, 23 Jul 2023 13:49:34 +1000 Subject: [PATCH] world.callScriptContext --- source/game/StarWorldServer.cpp | 7 +++++++ source/game/StarWorldServer.hpp | 7 +++++-- source/game/scripting/StarWorldLuaBindings.cpp | 8 ++++++++ source/game/scripting/StarWorldLuaBindings.hpp | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/source/game/StarWorldServer.cpp b/source/game/StarWorldServer.cpp index f519dc4..0685e9e 100644 --- a/source/game/StarWorldServer.cpp +++ b/source/game/StarWorldServer.cpp @@ -1578,6 +1578,13 @@ bool WorldServer::regionActive(RectI const& region) { return true; } +WorldServer::ScriptComponentPtr WorldServer::scriptContext(String const& contextName) { + if (auto context = m_scriptContexts.ptr(contextName)) + return *context; + else + return nullptr; +} + RpcPromise WorldServer::enqueuePlacement(List distributions, Maybe id) { return m_worldStorage->enqueuePlacement(move(distributions), id); } diff --git a/source/game/StarWorldServer.hpp b/source/game/StarWorldServer.hpp index 8c240d4..163cc1b 100644 --- a/source/game/StarWorldServer.hpp +++ b/source/game/StarWorldServer.hpp @@ -49,6 +49,9 @@ extern EnumMap const WorldServerFidelityNames; class WorldServer : public World { public: + typedef LuaMessageHandlingComponent>> ScriptComponent; + typedef shared_ptr ScriptComponentPtr; + // Create a new world with the given template, writing new storage file. WorldServer(WorldTemplatePtr const& worldTemplate, IODevicePtr storage); // Synonym for WorldServer(make_shared(size), storage); @@ -193,6 +196,8 @@ public: // Returns true if a region is fully active without signaling it. bool regionActive(RectI const& region); + ScriptComponentPtr scriptContext(String const& contextName); + // Queues a microdungeon for placement RpcPromise enqueuePlacement(List distributions, Maybe id); @@ -350,8 +355,6 @@ private: WireProcessorPtr m_wireProcessor; LuaRootPtr m_luaRoot; - typedef LuaMessageHandlingComponent>> ScriptComponent; - typedef shared_ptr ScriptComponentPtr; StringMap m_scriptContexts; WorldGeometry m_geometry; diff --git a/source/game/scripting/StarWorldLuaBindings.cpp b/source/game/scripting/StarWorldLuaBindings.cpp index da4f7c8..6d1648e 100644 --- a/source/game/scripting/StarWorldLuaBindings.cpp +++ b/source/game/scripting/StarWorldLuaBindings.cpp @@ -383,6 +383,7 @@ namespace LuaBindings { callbacks.registerCallbackWithSignature>("setPlayerStart", bind(ServerWorldCallbacks::setPlayerStart, world, _1, _2)); callbacks.registerCallbackWithSignature>("players", bind(ServerWorldCallbacks::players, world)); callbacks.registerCallbackWithSignature("fidelity", bind(ServerWorldCallbacks::fidelity, world, _1)); + callbacks.registerCallbackWithSignature, String, String, LuaVariadic>("callScriptContext", bind(ServerWorldCallbacks::callScriptContext, world, _1, _2, _3)); callbacks.registerCallbackWithSignature("skyTime", [serverWorld]() { return serverWorld->sky()->epochTime(); @@ -1168,6 +1169,13 @@ namespace LuaBindings { return engine.createString(WorldServerFidelityNames.getRight(as(world)->fidelity())); } + Maybe ServerWorldCallbacks::callScriptContext(World* world, String const& contextName, String const& function, LuaVariadic const& args) { + auto context = as(world)->scriptContext(contextName); + if (!context) + throw StarException::format("Context {} does not exist", contextName); + return context->invoke(function, args); + } + void WorldDebugCallbacks::debugPoint(Vec2F const& arg1, Color const& arg2) { SpatialLogger::logPoint("world", arg1, arg2.toRgba()); } diff --git a/source/game/scripting/StarWorldLuaBindings.hpp b/source/game/scripting/StarWorldLuaBindings.hpp index 87a0367..725e824 100644 --- a/source/game/scripting/StarWorldLuaBindings.hpp +++ b/source/game/scripting/StarWorldLuaBindings.hpp @@ -86,6 +86,7 @@ namespace LuaBindings { void setPlayerStart(World* world, Vec2F const& playerStart, Maybe respawnInWorld); List players(World* world); LuaString fidelity(World* world, LuaEngine& engine); + Maybe callScriptContext(World* world, String const& contextName, String const& function, LuaVariadic const& args); } namespace WorldDebugCallbacks {