diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-02-11 08:20:00 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-11 08:20:00 +1100 |
commit | 345865931c3c36b4c467e5366ef36611f996acb1 (patch) | |
tree | 6552c16524f3e756140d7c2a5bfde15c2faaa810 /source/game/scripting/StarLuaComponents.cpp | |
parent | 9eae03a968d0b2294d09d831a972cc3bbcae59c6 (diff) | |
parent | c6c46faf7c9f0db31f26c2745f561fea2fb96a3e (diff) |
Merge pull request #182 from Bottinator22/scriptablethreads
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; +} + } |