diff options
author | Bottinator22 <bottinator22@gmail.com> | 2025-02-10 10:55:37 -0800 |
---|---|---|
committer | Bottinator22 <bottinator22@gmail.com> | 2025-02-10 13:13:19 -0800 |
commit | c6c46faf7c9f0db31f26c2745f561fea2fb96a3e (patch) | |
tree | a1857a9d887cac72ed3121de9d443d0716b39c8c /source/game/scripting/StarLuaComponents.cpp | |
parent | eef0da37e1b206a9d486c5ad74bb10b11944503b (diff) |
scriptable threads
Diffstat (limited to 'source/game/scripting/StarLuaComponents.cpp')
-rw-r--r-- | source/game/scripting/StarLuaComponents.cpp | 43 |
1 files changed, 42 insertions, 1 deletions
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<ScriptableThread>(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<Json> args) { + auto pair = RpcThreadPromise<Json>::createPair(); + RecursiveMutexLocker locker(m_threadLock); + m_threads.get(threadName)->passMessage({ message, args, pair.second }); + return pair.first; + }); + + return callbacks; +} + } |