From c6c46faf7c9f0db31f26c2745f561fea2fb96a3e Mon Sep 17 00:00:00 2001 From: Bottinator22 Date: Mon, 10 Feb 2025 10:55:37 -0800 Subject: scriptable threads --- source/game/scripting/StarLuaComponents.cpp | 43 ++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'source/game/scripting/StarLuaComponents.cpp') diff --git a/source/game/scripting/StarLuaComponents.cpp b/source/game/scripting/StarLuaComponents.cpp index 9a1b1c4..7a6db8b 100644 --- a/source/game/scripting/StarLuaComponents.cpp +++ b/source/game/scripting/StarLuaComponents.cpp @@ -1,16 +1,22 @@ #include "StarLuaComponents.hpp" #include "StarUtilityLuaBindings.hpp" #include "StarRootLuaBindings.hpp" +#include "StarScriptableThread.hpp" +#include "StarRpcThreadPromise.hpp" +#include "StarLuaGameConverters.hpp" namespace Star { LuaBaseComponent::LuaBaseComponent() { addCallbacks("sb", LuaBindings::makeUtilityCallbacks()); addCallbacks("root", LuaBindings::makeRootCallbacks()); + addCallbacks("threads", makeThreadsCallbacks()); setAutoReInit(true); } -LuaBaseComponent::~LuaBaseComponent() {} +LuaBaseComponent::~LuaBaseComponent() { + m_threads.clear(); +} StringList const& LuaBaseComponent::scripts() const { return m_scripts; @@ -109,6 +115,9 @@ void LuaBaseComponent::uninit() { contextShutdown(); m_context.reset(); } + for (auto p : m_threads) { + p.second->stop(); + } m_error.reset(); } @@ -152,4 +161,36 @@ bool LuaBaseComponent::checkInitialization() { return initialized(); } +LuaCallbacks LuaBaseComponent::makeThreadsCallbacks() { + LuaCallbacks callbacks; + + callbacks.registerCallback("create", [this](Json parameters) { + auto name = parameters.getString("name"); + if (m_threads.contains(name)) { + m_threads.get(name)->stop(); + m_threads.remove(name); + } + auto thread = make_shared(parameters); + thread->setPause(false); + thread->start(); + m_threads.set(name,thread); + return name; + }); + callbacks.registerCallback("setPause", [this](String const& threadName, bool paused) { + m_threads.get(threadName)->setPause(paused); + }); + callbacks.registerCallback("stop", [this](String const& threadName) { + m_threads.get(threadName)->stop(); + m_threads.remove(threadName); + }); + callbacks.registerCallback("sendMessage", [this](String const& threadName, String const& message, LuaVariadic args) { + auto pair = RpcThreadPromise::createPair(); + RecursiveMutexLocker locker(m_threadLock); + m_threads.get(threadName)->passMessage({ message, args, pair.second }); + return pair.first; + }); + + return callbacks; +} + } -- cgit v1.2.3